-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain.go
99 lines (78 loc) · 2.06 KB
/
blockchain.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
package capusta
import (
"fmt"
"sync"
"time"
)
type blockchain struct {
blocks []Block
transactions []Transaction
lock sync.Mutex
}
// blockchain.getLenght return lenght of blockchain
func (chain *blockchain) getLenght() int {
return len(chain.blocks)
}
// blockchain.getLastBlock return pointer to last Block of blockchain
func (chain *blockchain) getLastBlock() *Block {
return &chain.blocks[chain.getLenght()-1]
}
// blockchain.getBlockbyIndex return pointer to Block by index (int)
func (chain *blockchain) getBlockbyIndex(index int) *Block {
return &chain.blocks[index]
}
// blockchain.getBlockbyHash search and return pointer to Block by hash ([32]byte)
func (chain *blockchain) getBlockbyHash(hash [32]byte) *Block {
for block := range chain.Iterator() {
if block.hash == hash {
return block
}
}
return nil
}
func (chain *blockchain) GetWallet(owner string) *Wallet {
return &Wallet{owner: owner, blockchain: chain}
}
func (chain *blockchain) Iterator() chan *Block {
output := make(chan *Block)
push := func(block Block) {
output <- &block
}
pusher := func() {
for _, v := range chain.blocks {
push(v)
}
close(output)
}
go pusher()
return output
}
// blockchain.MineBlock mine Block
// Function lock transaction, create reward for miner and starting for searching proof-of-work
func (chain *blockchain) MineBlock(miner string) {
chain.transactions = append(chain.transactions, NewReward(miner))
var block = Block{
index: int64(chain.getLenght()),
timestamp: time.Now().UnixNano(),
data: chain.transactions,
previousHash: chain.getLastBlock().hash,
proof: 1,
}
for {
block.proof++
block.hash = Hash(&block)
if block.checkSum() {
break
}
}
chain.transactions = []Transaction{}
chain.blocks = append(chain.blocks, block)
}
// impliment Stringer interface
func (chain blockchain) String() string {
var blocksInfo string = ""
for b := range chain.Iterator() {
blocksInfo += b.String()
}
return fmt.Sprintf("Blockchain - Length: %d \n%s", chain.getLenght(), blocksInfo)
}