forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stubchain.go
198 lines (156 loc) · 4.36 KB
/
stubchain.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package chain
import (
"bytes"
"errors"
"github.com/aergoio/aergo/message"
"github.com/aergoio/aergo/types"
"time"
)
//StubSyncer receive Syncer, P2P, Chain Service actor message
type StubBlockChain struct {
Best int
Hashes []([]byte)
Blocks []*types.Block
BestBlock *types.Block
}
var (
ErrNotExistHash = errors.New("not exist hash")
ErrNotExistBlock = errors.New("not exist block of the hash")
)
func NewStubBlockChain() *StubBlockChain {
tchain := &StubBlockChain{Best: -1}
tchain.Hashes = make([][]byte, 10240)
tchain.Blocks = make([]*types.Block, 10240)
return tchain
}
func (tchain *StubBlockChain) GenAddBlock() {
newBlock := types.NewBlock(tchain.BestBlock, nil, nil, nil, nil, time.Now().UnixNano())
tchain.AddBlock(newBlock)
time.Sleep(time.Nanosecond * 3)
}
func (tchain *StubBlockChain) AddBlock(newBlock *types.Block) error {
if newBlock.BlockNo() != uint64(tchain.Best+1) {
return ErrBlockOrphan
}
tchain.Best += 1
tchain.Hashes[tchain.Best] = newBlock.BlockHash()
tchain.Blocks[tchain.Best] = newBlock
tchain.BestBlock = newBlock
return nil
}
func (tchain *StubBlockChain) GetHashes(prevInfo *types.BlockInfo, count uint64) ([]message.BlockHash, error) {
if tchain.Best < int(prevInfo.No+count) {
return nil, ErrNotExistHash
}
start := prevInfo.No + 1
resHashes := tchain.Hashes[start : start+count]
blkHashes := make([]message.BlockHash, 0)
for _, hash := range resHashes {
blkHashes = append(blkHashes, hash)
}
return blkHashes, nil
}
func (tchain *StubBlockChain) GetBlockInfo(no uint64) *types.BlockInfo {
return &types.BlockInfo{tchain.Hashes[no], no}
}
func (tchain *StubBlockChain) GetBlockByNo(no uint64) *types.Block {
return tchain.Blocks[no]
}
func (tchain *StubBlockChain) GetBlocks(hashes []message.BlockHash) ([]*types.Block, error) {
startNo := -1
for i, block := range tchain.Blocks {
if bytes.Equal(block.GetHash(), hashes[0]) {
startNo = i
break
}
}
if startNo == -1 {
return nil, ErrNotExistBlock
}
resultBlocks := make([]*types.Block, 0)
i := startNo
for _, hash := range hashes {
if !bytes.Equal(tchain.Blocks[i].GetHash(), hash) {
return nil, ErrNotExistBlock
}
resultBlocks = append(resultBlocks, tchain.Blocks[i])
i++
}
return resultBlocks, nil
}
func (tchain *StubBlockChain) GetBestBlock() (*types.Block, error) {
return tchain.BestBlock, nil
}
func (tchain *StubBlockChain) GetBlock(blockHash []byte) (*types.Block, error) {
for _, block := range tchain.Blocks {
if bytes.Equal(block.GetHash(), blockHash) {
return block, nil
break
}
}
return nil, ErrNotExistBlock
}
func (tchain *StubBlockChain) GetHashByNo(blockNo types.BlockNo) ([]byte, error) {
if uint64(len(tchain.Hashes)) <= blockNo {
return nil, ErrNotExistHash
}
return tchain.Hashes[blockNo], nil
}
//TODO refactoring with getAnchorsNew()
func (tchain *StubBlockChain) GetAnchors() (ChainAnchor, types.BlockNo, error) {
//from top : 8 * 32 = 256
anchors := make(ChainAnchor, 0)
cnt := MaxAnchors
logger.Debug().Msg("get anchors")
bestBlock, _ := tchain.GetBestBlock()
blkNo := bestBlock.BlockNo()
var lastNo types.BlockNo
LOOP:
for i := 0; i < cnt; i++ {
blockHash, err := tchain.GetHashByNo(blkNo)
if err != nil {
logger.Info().Msg("assertion - hash get failed")
// assertion!
return nil, 0, err
}
anchors = append(anchors, blockHash)
lastNo = blkNo
logger.Debug().Uint64("no", blkNo).Msg("anchor added")
switch {
case blkNo == 0:
break LOOP
case blkNo < Skip:
blkNo = 0
default:
blkNo -= Skip
}
}
return anchors, lastNo, nil
}
func (tchain *StubBlockChain) GetAncestorWithHashes(hashes [][]byte) *types.BlockInfo {
for _, hash := range hashes {
for j, chainHash := range tchain.Hashes {
if bytes.Equal(hash, chainHash) {
return &types.BlockInfo{Hash: chainHash, No: uint64(j)}
}
}
}
return nil
}
func (tchain *StubBlockChain) Rollback(ancestor *types.BlockInfo) {
prevBest := tchain.Best
tchain.Best = int(ancestor.No)
tchain.BestBlock = tchain.Blocks[tchain.Best]
logger.Debug().Int("prev", prevBest).Int("Best", tchain.Best).Msg("test local chain is rollbacked")
}
func InitStubBlockChain(prefixChain []*types.Block, genCount int) *StubBlockChain {
newChain := NewStubBlockChain()
//load initial Blocks
for _, block := range prefixChain {
newChain.AddBlock(block)
}
for i := 0; i < genCount; i++ {
newChain.GenAddBlock()
}
return newChain
}