-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
152 lines (134 loc) · 3.16 KB
/
main.go
File metadata and controls
152 lines (134 loc) · 3.16 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"encoding/hex"
"fmt"
"strings"
"time"
)
var difficulty = 4
// Block block in the blockchain
type Block struct {
Timestamp time.Time
PrevHash []byte
Difficulty int
Nonce int
Data []byte
}
// Blockchain the main chain
type Blockchain struct {
chains []*Block
rschan chan *Block
}
func newBlock(data []byte, prevHash []byte) *Block {
b := &Block{
Data: data,
PrevHash: prevHash,
Timestamp: time.Now(),
Difficulty: difficulty,
}
b.Nonce = proofOfWork(b)
return b
}
// CalHash return hash of the block
func (block *Block) CalHash() []byte {
h := sha256.New()
b := bytes.Join([][]byte{toBytes(block.Timestamp), block.PrevHash, block.Data, toBytes(block.Difficulty), toBytes(block.Nonce)}, []byte{})
h.Write(b)
return h.Sum(nil)
}
// Print print block info to console
func (block *Block) Print() {
fmt.Printf("Hash: %s\n", hex.EncodeToString(block.CalHash()))
fmt.Printf("PrevHash: %s\n", hex.EncodeToString(block.PrevHash))
fmt.Println("Nonce:", block.Nonce)
fmt.Printf("Data: %s\n", string(block.Data))
fmt.Println("POW:", validBlock(block))
fmt.Println()
}
func hashit(data []byte) []byte {
h := sha256.New()
h.Write(data)
return h.Sum(nil)
}
func genesis() *Block {
return newBlock([]byte("I'm genesis block"), []byte{})
}
// NewBlockchain return a new blockchain with genesis block inside
func NewBlockchain() *Blockchain {
return &Blockchain{
chains: []*Block{genesis()},
rschan: make(chan *Block),
}
}
// Chains return the blockchain for iterator
func (blockchain *Blockchain) Chains() []*Block {
return blockchain.chains
}
// AddBlock add a new block into the blockchain
func (blockchain *Blockchain) AddBlock(data []byte) {
chains := blockchain.Chains()
prevBlock := chains[len(chains)-1]
b := newBlock(data, prevBlock.CalHash())
if !validBlock(b) {
fmt.Println("ERROR: Fake block....")
}
blockchain.chains = append(blockchain.chains, b)
blockchain.rschan <- b
}
// proofOfWork find nonce so that hash of data of the block and nonce has #Difficulty leading zero
func proofOfWork(b *Block) int {
blockData := func(nonce int) []byte {
return bytes.Join([][]byte{
toBytes(b.Timestamp),
b.PrevHash,
b.Data,
toBytes(b.Difficulty),
toBytes(nonce),
}, []byte{})
}
// find nonce
nonce := 0
prefix := strings.Repeat("0", difficulty)
for {
hashValue := hex.EncodeToString(hashit(blockData(nonce)))
if strings.HasPrefix(hashValue, prefix) {
return nonce
}
nonce++
}
}
// Validate validBlock the proof of work
func validBlock(block *Block) bool {
prefix := strings.Repeat("0", block.Difficulty)
hashValue := hex.EncodeToString(block.CalHash())
return strings.HasPrefix(hashValue, prefix)
}
func toBytes(v interface{}) []byte {
var b bytes.Buffer
err := gob.NewEncoder(&b).Encode(v)
if err != nil {
panic(err)
}
return b.Bytes()
}
func main() {
fmt.Println("---------Basic blockchain----------")
blockchain := NewBlockchain()
blockchain.Chains()[0].Print()
go func() {
for {
select {
case b := <-blockchain.rschan:
b.Print()
}
}
}()
i := 1
for i <= 5 {
blockchain.AddBlock([]byte(fmt.Sprintf("Block %v", i)))
i++
}
}