-
Notifications
You must be signed in to change notification settings - Fork 6
/
blockchain.go
136 lines (117 loc) · 3.01 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
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
package main
import (
"bytes"
"encoding/json"
"github.com/tinychain/algorand/common"
"sync"
)
type Block struct {
Round uint64 `json:"round"` // block round, namely height
ParentHash common.Hash `json:"parent_hash"` // parent block hash
Author common.Address `json:"author"` // proposer address
AuthorVRF []byte `json:"author_vrf"` // sortition hash
AuthorProof []byte `json:"author_proof"` // sortition hash proof
Time int64 `json:"time"` // block timestamp
Seed []byte `json:"seed"` // vrf-based seed for next round
Proof []byte `json:"proof"` // proof of vrf-based seed
Data []byte `json:"data"` // data field, simulate for transactions
// don't induce in hash
Type int8 `json:"type"` // `FINAL` or `TENTATIVE`
Signature []byte `json:"signature"` // signature of block
}
func (blk *Block) Hash() common.Hash {
data := bytes.Join([][]byte{
common.Uint2Bytes(blk.Round),
blk.ParentHash.Bytes(),
}, nil)
if !blk.Author.Nil() {
data = append(data, bytes.Join([][]byte{
blk.Author.Bytes(),
blk.AuthorVRF,
blk.AuthorProof,
}, nil)...)
}
if blk.Time != 0 {
data = append(data, common.Uint2Bytes(uint64(blk.Time))...)
}
if blk.Seed != nil {
data = append(data, blk.Seed...)
}
if blk.Proof != nil {
data = append(data, blk.Proof...)
}
return common.Sha256(data)
}
func (blk *Block) RecoverPubkey() *PublicKey {
return recoverPubkey(blk.Signature)
}
func (blk *Block) Serialize() ([]byte, error) {
return json.Marshal(blk)
}
func (blk *Block) Deserialize(data []byte) error {
return json.Unmarshal(data, blk)
}
type Blockchain struct {
mu sync.RWMutex
last *Block
genesis *Block
blocks map[uint64]map[common.Hash]*Block
}
func newBlockchain() *Blockchain {
bc := &Blockchain{
blocks: make(map[uint64]map[common.Hash]*Block),
}
bc.init()
return bc
}
func (bc *Blockchain) init() {
emptyHash := common.Sha256([]byte{})
// create genesis
bc.genesis = &Block{
Round: 0,
Seed: emptyHash.Bytes(),
ParentHash: emptyHash,
Author: common.HashToAddr(emptyHash),
}
bc.add(bc.genesis)
}
func (bc *Blockchain) get(hash common.Hash, round uint64) *Block {
bc.mu.RLock()
defer bc.mu.RUnlock()
blocks := bc.blocks[round]
if blocks != nil {
return blocks[hash]
}
return nil
}
func (bc *Blockchain) getByRound(round uint64) *Block {
bc.mu.RLock()
defer bc.mu.RUnlock()
last := bc.last
for round > 0 {
if last.Round == round {
return last
}
last = bc.get(last.ParentHash, round-1)
round--
}
return last
}
func (bc *Blockchain) add(blk *Block) {
bc.mu.Lock()
defer bc.mu.Unlock()
blocks := bc.blocks[blk.Round]
if blocks == nil {
blocks = make(map[common.Hash]*Block)
}
blocks[blk.Hash()] = blk
bc.blocks[blk.Round] = blocks
if bc.last == nil || blk.Round > bc.last.Round {
bc.last = blk
}
}
func (bc *Blockchain) resolveFork(fork *Block) {
bc.mu.Lock()
defer bc.mu.Unlock()
bc.last = fork
}