forked from Jeiwan/blockchain_go
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathblock.go
119 lines (97 loc) · 2.27 KB
/
block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"encoding/json"
"fmt"
"log"
"os"
"time"
)
/*
Block simple structure
*/
type Block struct {
Transactions []Transaction `json:"Transactions"`
Header Header `json:"BlockHeader"`
}
/*Header of block */
type Header struct {
Timestamp int64 `json:"Timestamp"`
Hash []byte `json:"Hash"`
PrevBlockHash []byte `json:"PrevBlockHash"`
Height int `json:"Height"`
Nonce int `json:"Nonce"`
}
func (b Block) String() string {
var strBlock string
strBlock += fmt.Sprintf("Prev hash: %x\n", b.Header.PrevBlockHash)
strBlock += fmt.Sprintf("Transactions: \n")
for idx, tx := range b.Transactions {
strBlock += fmt.Sprintf(" Tx[%d] : %s\n", idx, tx)
}
strBlock += fmt.Sprintf("Hash: %x\n", b.Header.Hash)
strBlock += fmt.Sprintf("Nonce: %d\n", b.Header.Nonce)
strBlock += fmt.Sprintf("Height: %d\n", b.Header.Height)
strBlock += fmt.Sprintf("Timestamp: %d\n", b.Header.Timestamp)
return strBlock
}
func (b *Block) setHash() {
hash := sha256.Sum256(b.serialize())
b.Header.Hash = hash[:]
}
func newBlock(txs []Transaction, prevBlockHash []byte, height int) *Block {
block := &Block{txs, Header{time.Now().Unix(), []byte{}, prevBlockHash, height, 0}}
block.setHash()
return block
}
func (b *Block) isGenesisBlock() bool {
return len(b.Header.PrevBlockHash) == 0
}
func newGenesisBlock(txs []Transaction) *Block {
return newBlock(txs, []byte{}, 1)
}
func (b *Block) hashTransactions() []byte {
var encoded bytes.Buffer
enc := gob.NewEncoder(&encoded)
err := enc.Encode(b.Transactions)
if err != nil {
log.Panic(err)
}
return encoded.Bytes()
}
func (b *Block) serialize() []byte {
data, err := json.Marshal(b)
if err != nil {
Error.Printf("Marshal block fail\n")
os.Exit(1)
}
return data
}
func deserializeBlock(data []byte) *Block {
b := new(Block)
err := json.Unmarshal(data, b)
if err != nil {
Error.Panic(err)
os.Exit(1)
}
return b
}
func (h *Header) serialize() []byte {
data, err := json.Marshal(h)
if err != nil {
Error.Printf("Marshal block fail\n")
os.Exit(1)
}
return data
}
func deserializeHeader(data []byte) *Header {
h := new(Header)
err := json.Unmarshal(data, h)
if err != nil {
Error.Panic(err)
os.Exit(1)
}
return h
}