Golang implemented sidechain for Bytom
修订版 | 104f6c3bf19cbe2ae1279f4337886eb6a046727d (tree) |
---|---|
时间 | 2020-02-20 12:49:42 |
作者 | ipqhjjybj <250657661@qq.c...> |
Commiter | ipqhjjybj |
fix bad delete
@@ -0,0 +1,285 @@ | ||
1 | +// +build functional | |
2 | + | |
3 | +package test | |
4 | + | |
5 | +import ( | |
6 | + "os" | |
7 | + "testing" | |
8 | + | |
9 | + "github.com/bytom/vapor/consensus" | |
10 | + dbm "github.com/bytom/vapor/database/leveldb" | |
11 | + "github.com/bytom/vapor/protocol/bc/types" | |
12 | + "github.com/bytom/vapor/protocol/vm" | |
13 | +) | |
14 | + | |
15 | +// case1: |------c1(height=7) | |
16 | +// --------(height=5) | |
17 | +// |------------c2(height=9) | |
18 | +func TestForkCase1(t *testing.T) { | |
19 | + c1, err := declChain("chain1", nil, 0, 7) | |
20 | + defer os.RemoveAll("chain1") | |
21 | + if err != nil { | |
22 | + t.Fatal(err) | |
23 | + } | |
24 | + | |
25 | + c2, err := declChain("chain2", c1, 5, 9) | |
26 | + defer os.RemoveAll("chain2") | |
27 | + if err != nil { | |
28 | + t.Fatal(err) | |
29 | + } | |
30 | + | |
31 | + bestBlockHash := c2.BestBlockHash() | |
32 | + if err := merge(c1, c2); err != nil { | |
33 | + t.Fatal(err) | |
34 | + } | |
35 | + | |
36 | + if *c1.BestBlockHash() != *bestBlockHash || *c2.BestBlockHash() != *bestBlockHash { | |
37 | + t.Fatalf("test fork case1 failed") | |
38 | + } | |
39 | + | |
40 | + if !c1.InMainChain(*bestBlockHash) || !c2.InMainChain(*bestBlockHash) { | |
41 | + t.Fatalf("best block is not in main chain") | |
42 | + } | |
43 | +} | |
44 | + | |
45 | +// case2: |----c1(height=6) | |
46 | +// ---------(height 5) | |
47 | +// |----c2(height=6) | |
48 | +func TestForkCase2(t *testing.T) { | |
49 | + c1, err := declChain("chain1", nil, 0, 6) | |
50 | + defer os.RemoveAll("chain1") | |
51 | + if err != nil { | |
52 | + t.Fatal(err) | |
53 | + } | |
54 | + | |
55 | + c2, err := declChain("chain2", c1, 5, 6) | |
56 | + defer os.RemoveAll("chain2") | |
57 | + if err != nil { | |
58 | + t.Fatal(err) | |
59 | + } | |
60 | + | |
61 | + c1BestBlockHash := c1.BestBlockHash() | |
62 | + c2BestBlockHash := c2.BestBlockHash() | |
63 | + if err := merge(c1, c2); err != nil { | |
64 | + t.Fatal(err) | |
65 | + } | |
66 | + | |
67 | + if *c1.BestBlockHash() != *c1BestBlockHash || *c2.BestBlockHash() != *c2BestBlockHash { | |
68 | + t.Fatalf("test fork case2 failed") | |
69 | + } | |
70 | + | |
71 | + if !c1.InMainChain(*c1BestBlockHash) || !c2.InMainChain(*c2BestBlockHash) { | |
72 | + t.Fatalf("best block is not in main chain") | |
73 | + } | |
74 | +} | |
75 | + | |
76 | +func TestBlockSync(t *testing.T) { | |
77 | + c1, err := declChain("chain1", nil, 0, 5) | |
78 | + defer os.RemoveAll("chain1") | |
79 | + if err != nil { | |
80 | + t.Fatal(err) | |
81 | + } | |
82 | + | |
83 | + c2, err := declChain("chain2", c1, 5, 8) | |
84 | + defer os.RemoveAll("chain2") | |
85 | + if err != nil { | |
86 | + t.Fatal(err) | |
87 | + } | |
88 | + | |
89 | + bestBlockHash := c2.BestBlockHash() | |
90 | + if err := merge(c1, c2); err != nil { | |
91 | + t.Fatal(err) | |
92 | + } | |
93 | + | |
94 | + if *c1.BestBlockHash() != *bestBlockHash || *c2.BestBlockHash() != *bestBlockHash { | |
95 | + t.Fatalf("test block sync failed") | |
96 | + } | |
97 | + | |
98 | + if !c1.InMainChain(*bestBlockHash) || !c2.InMainChain(*bestBlockHash) { | |
99 | + t.Fatalf("test block sync failed, best block is not in main chain") | |
100 | + } | |
101 | +} | |
102 | + | |
103 | +func TestDoubleSpentInDiffBlock(t *testing.T) { | |
104 | + chainDB := dbm.NewDB("tx_pool_test", "leveldb", "tx_pool_test") | |
105 | + defer os.RemoveAll("tx_pool_test") | |
106 | + chain, _, txPool, err := MockChain(chainDB) | |
107 | + if err != nil { | |
108 | + t.Fatal(err) | |
109 | + } | |
110 | + if err := AppendBlocks(chain, consensus.ActiveNetParams.CoinbasePendingBlockNumber+1); err != nil { | |
111 | + t.Fatal(err) | |
112 | + } | |
113 | + | |
114 | + // create tx spend the coinbase output in block 1 | |
115 | + block, err := chain.GetBlockByHeight(1) | |
116 | + if err != nil { | |
117 | + t.Fatal(err) | |
118 | + } | |
119 | + tx, err := CreateTxFromTx(block.Transactions[0], 0, 10000, []byte{byte(vm.OP_TRUE)}) | |
120 | + if err != nil { | |
121 | + t.Fatal(err) | |
122 | + } | |
123 | + | |
124 | + newBlock, err := NewBlock(chain, []*types.Tx{tx}, []byte{byte(vm.OP_TRUE)}) | |
125 | + _, err = chain.ProcessBlock(newBlock) | |
126 | + if err != nil { | |
127 | + t.Fatal(err) | |
128 | + } | |
129 | + | |
130 | + // create a double spent tx in another block | |
131 | + tx, err = CreateTxFromTx(block.Transactions[0], 0, 10000, []byte{byte(vm.OP_TRUE)}) | |
132 | + if err != nil { | |
133 | + t.Fatal(err) | |
134 | + } | |
135 | + | |
136 | + if isOrphan, err := chain.ValidateTx(tx); isOrphan == false && err == nil { | |
137 | + t.Fatal("validate double spent tx success") | |
138 | + } | |
139 | + if txPool.HaveTransaction(&tx.ID) { | |
140 | + t.Fatalf("tx pool have double spent tx") | |
141 | + } | |
142 | +} | |
143 | + | |
144 | +func TestDoubleSpentInSameBlock(t *testing.T) { | |
145 | + chainDB := dbm.NewDB("tx_pool_test", "leveldb", "tx_pool_test") | |
146 | + defer os.RemoveAll("tx_pool_test") | |
147 | + chain, _, txPool, err := MockChain(chainDB) | |
148 | + if err != nil { | |
149 | + t.Fatal(err) | |
150 | + } | |
151 | + if err := AppendBlocks(chain, consensus.ActiveNetParams.CoinbasePendingBlockNumber+1); err != nil { | |
152 | + t.Fatal(err) | |
153 | + } | |
154 | + | |
155 | + // create tx spend the coinbase output in block 1 | |
156 | + block, err := chain.GetBlockByHeight(1) | |
157 | + if err != nil { | |
158 | + t.Fatal(err) | |
159 | + } | |
160 | + tx1, err := CreateTxFromTx(block.Transactions[0], 0, 10000, []byte{byte(vm.OP_TRUE)}) | |
161 | + if err != nil { | |
162 | + t.Fatal(err) | |
163 | + } | |
164 | + | |
165 | + // create tx spend the coinbase output in block 1 | |
166 | + tx2, err := CreateTxFromTx(block.Transactions[0], 0, 10000, []byte{byte(vm.OP_TRUE)}) | |
167 | + if err != nil { | |
168 | + t.Fatal(err) | |
169 | + } | |
170 | + | |
171 | + _, err = chain.ValidateTx(tx1) | |
172 | + if err != nil { | |
173 | + t.Fatal(err) | |
174 | + } | |
175 | + _, err = chain.ValidateTx(tx2) | |
176 | + if err != nil { | |
177 | + t.Fatal(err) | |
178 | + } | |
179 | + | |
180 | + if !txPool.HaveTransaction(&tx1.ID) { | |
181 | + t.Fatalf("can't find tx in tx pool") | |
182 | + } | |
183 | + if !txPool.HaveTransaction(&tx2.ID) { | |
184 | + t.Fatalf("can't find tx in tx pool") | |
185 | + } | |
186 | + | |
187 | + block, err = NewBlock(chain, []*types.Tx{tx1, tx2}, []byte{byte(vm.OP_TRUE)}) | |
188 | + if err != nil { | |
189 | + t.Fatal(err) | |
190 | + } | |
191 | + | |
192 | + if _, err := chain.ProcessBlock(block); err == nil { | |
193 | + t.Fatalf("process double spent tx success") | |
194 | + } | |
195 | +} | |
196 | + | |
197 | +func TestTxPoolDependencyTx(t *testing.T) { | |
198 | + chainDB := dbm.NewDB("tx_pool_test", "leveldb", "tx_pool_test") | |
199 | + defer os.RemoveAll("tx_pool_test") | |
200 | + chain, _, txPool, err := MockChain(chainDB) | |
201 | + if err != nil { | |
202 | + t.Fatal(err) | |
203 | + } | |
204 | + if err := AppendBlocks(chain, consensus.ActiveNetParams.CoinbasePendingBlockNumber+1); err != nil { | |
205 | + t.Fatal(err) | |
206 | + } | |
207 | + | |
208 | + block, err := chain.GetBlockByHeight(1) | |
209 | + if err != nil { | |
210 | + t.Fatal(err) | |
211 | + } | |
212 | + | |
213 | + tx, err := CreateTxFromTx(block.Transactions[0], 0, 5000000000, []byte{byte(vm.OP_TRUE)}) | |
214 | + if err != nil { | |
215 | + t.Fatal(err) | |
216 | + } | |
217 | + | |
218 | + outputAmount := uint64(5000000000) | |
219 | + txs := []*types.Tx{nil} | |
220 | + txs[0] = tx | |
221 | + for i := 1; i < 10; i++ { | |
222 | + outputAmount -= 50000000 | |
223 | + tx, err := CreateTxFromTx(txs[i-1], 0, outputAmount, []byte{byte(vm.OP_TRUE)}) | |
224 | + if err != nil { | |
225 | + t.Fatal(err) | |
226 | + } | |
227 | + txs = append(txs, tx) | |
228 | + } | |
229 | + | |
230 | + // validate tx and put it into tx pool | |
231 | + for _, tx := range txs { | |
232 | + if _, err := chain.ValidateTx(tx); err != nil { | |
233 | + t.Fatal(err) | |
234 | + } | |
235 | + if !txPool.HaveTransaction(&tx.ID) { | |
236 | + t.Fatal("can't find tx in txpool") | |
237 | + } | |
238 | + } | |
239 | + | |
240 | + block, err = NewBlock(chain, txs, []byte{byte(vm.OP_TRUE)}) | |
241 | + if err != nil { | |
242 | + t.Fatal(err) | |
243 | + } | |
244 | + | |
245 | + if _, err := chain.ProcessBlock(block); err != nil { | |
246 | + t.Fatal("process dependency tx failed") | |
247 | + } | |
248 | +} | |
249 | + | |
250 | +func TestAddInvalidTxToTxPool(t *testing.T) { | |
251 | + chainDB := dbm.NewDB("tx_pool_test", "leveldb", "tx_pool_test") | |
252 | + defer os.RemoveAll("tx_pool_test") | |
253 | + | |
254 | + chain, _, txPool, err := MockChain(chainDB) | |
255 | + if err != nil { | |
256 | + t.Fatal(err) | |
257 | + } | |
258 | + | |
259 | + if err := AppendBlocks(chain, consensus.ActiveNetParams.CoinbasePendingBlockNumber+1); err != nil { | |
260 | + t.Fatal(err) | |
261 | + } | |
262 | + | |
263 | + block, err := chain.GetBlockByHeight(1) | |
264 | + if err != nil { | |
265 | + t.Fatal(err) | |
266 | + } | |
267 | + | |
268 | + //invalid tx, output amount greater than input | |
269 | + tx, err := CreateTxFromTx(block.Transactions[0], 0, 60000000000, []byte{byte(vm.OP_TRUE)}) | |
270 | + if err != nil { | |
271 | + t.Fatal(err) | |
272 | + } | |
273 | + | |
274 | + if _, err := chain.ValidateTx(tx); err == nil { | |
275 | + t.Fatalf("add invalid tx to txpool success") | |
276 | + } | |
277 | + | |
278 | + if txPool.IsTransactionInPool(&tx.ID) { | |
279 | + t.Fatalf("add invalid tx to txpool success") | |
280 | + } | |
281 | + | |
282 | + if !txPool.IsTransactionInErrCache(&tx.ID) { | |
283 | + t.Fatalf("can't find invalid tx in txpool err cache") | |
284 | + } | |
285 | +} |
@@ -1,357 +0,0 @@ | ||
1 | -package test | |
2 | - | |
3 | -import ( | |
4 | - "fmt" | |
5 | - "log" | |
6 | - "os" | |
7 | - "testing" | |
8 | - "time" | |
9 | - | |
10 | - "github.com/bytom/vapor/account" | |
11 | - "github.com/bytom/vapor/application/mov" | |
12 | - "github.com/bytom/vapor/asset" | |
13 | - "github.com/bytom/vapor/blockchain/pseudohsm" | |
14 | - "github.com/bytom/vapor/config" | |
15 | - cfg "github.com/bytom/vapor/config" | |
16 | - "github.com/bytom/vapor/consensus" | |
17 | - "github.com/bytom/vapor/crypto/ed25519/chainkd" | |
18 | - "github.com/bytom/vapor/database" | |
19 | - dbm "github.com/bytom/vapor/database/leveldb" | |
20 | - "github.com/bytom/vapor/errors" | |
21 | - "github.com/bytom/vapor/event" | |
22 | - "github.com/bytom/vapor/proposal" | |
23 | - "github.com/bytom/vapor/protocol" | |
24 | - "github.com/bytom/vapor/protocol/bc" | |
25 | - "github.com/bytom/vapor/protocol/bc/types" | |
26 | - "github.com/bytom/vapor/protocol/state" | |
27 | - "github.com/bytom/vapor/testutil" | |
28 | - w "github.com/bytom/vapor/wallet" | |
29 | -) | |
30 | - | |
31 | -const ( | |
32 | - n = 1 // 初始化用的block数量 | |
33 | -) | |
34 | - | |
35 | -var fedConsensusPath = [][]byte{ | |
36 | - []byte{0xff, 0xff, 0xff, 0xff}, | |
37 | - []byte{0xff, 0x00, 0x00, 0x00}, | |
38 | - []byte{0xff, 0xff, 0xff, 0xff}, | |
39 | - []byte{0xff, 0x00, 0x00, 0x00}, | |
40 | - []byte{0xff, 0x00, 0x00, 0x00}, | |
41 | -} | |
42 | - | |
43 | -type byTime []*protocol.TxDesc | |
44 | - | |
45 | -func (a byTime) Len() int { return len(a) } | |
46 | -func (a byTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] } | |
47 | -func (a byTime) Less(i, j int) bool { return a[i].Added.Before(a[j].Added) } | |
48 | - | |
49 | -func xpub(str string) (xpub chainkd.XPub) { | |
50 | - if err := xpub.UnmarshalText([]byte(str)); err != nil { | |
51 | - log.Panicf("Fail converts a string to xpub") | |
52 | - } | |
53 | - return xpub | |
54 | -} | |
55 | - | |
56 | -func xprv(str string) (xprv chainkd.XPrv) { | |
57 | - if err := xprv.UnmarshalText([]byte(str)); err != nil { | |
58 | - log.Panicf("Fail converts a string to xprv") | |
59 | - } | |
60 | - return xprv | |
61 | -} | |
62 | - | |
63 | -var Xprvs = []chainkd.XPrv{ | |
64 | - xprv("c87f8d0f4bb4b0acbb7f69f1954c4f34d4476e114fffa7b0c853992474a9954a273c2d8f2642a7baf94ebac88f1625af9f5eaf3b13a90de27eec3de78b9fb9ca"), | |
65 | - xprv("c80fbc34475fc9447753c00820d8448851c87f07e6bdde349260862c9bca5b4bb2e62c15e129067af869ebdf66e5829e61d6f2e47447395cc18c4166b06e8473"), | |
66 | -} | |
67 | - | |
68 | -const ( | |
69 | - warnTimeNum = 2 | |
70 | - warnTimeDenom = 5 | |
71 | - criticalTimeNum = 4 | |
72 | - criticalTimeDenom = 5 | |
73 | -) | |
74 | - | |
75 | -// number 1 | |
76 | -// private key: 483355b66c0e15b0913829d709b04557749b871b3bf56ad1de8fda13d3a4954aa2a56121b8eab313b8f36939e8190fe8f267f19496decb91be5644e92b669914 | |
77 | -// public key: 32fe453097591f288315ef47b1ebdabf20e8bced8ede670f999980205cacddd4a2a56121b8eab313b8f36939e8190fe8f267f19496decb91be5644e92b669914 | |
78 | -// derivied private key: c87f8d0f4bb4b0acbb7f69f1954c4f34d4476e114fffa7b0c853992474a9954a273c2d8f2642a7baf94ebac88f1625af9f5eaf3b13a90de27eec3de78b9fb9ca | |
79 | -// derivied public key: 4d6f710dae8094c111450ca20e054c3aed59dfcb2d29543c29901a5903755e69273c2d8f2642a7baf94ebac88f1625af9f5eaf3b13a90de27eec3de78b9fb9ca | |
80 | - | |
81 | -// number 2 | |
82 | -// private key: d8e786a4eafa3456e35b2a1467d37dd84f64ba36604f8076015b76a8eec55b4b83d4fac0f94d157cfc720b77602f21b6b8a7e86f95c571e4d7986210dbce44c9 | |
83 | -// public key: ebe1060254ec43bd7883e94583ff0a71ef0ec0e1ada4cd0f5ed7e9d37f1d244e83d4fac0f94d157cfc720b77602f21b6b8a7e86f95c571e4d7986210dbce44c9 | |
84 | -// derivied private key: c80fbc34475fc9447753c00820d8448851c87f07e6bdde349260862c9bca5b4bb2e62c15e129067af869ebdf66e5829e61d6f2e47447395cc18c4166b06e8473 | |
85 | -// derivied public key: 59184c0f1f4f13b8b256ac82df30dc12cfd66b6e09a28054933f848dc51b9a89b2e62c15e129067af869ebdf66e5829e61d6f2e47447395cc18c4166b06e8473 | |
86 | - | |
87 | -func getKey() { | |
88 | - xprv, _ := chainkd.NewXPrv(nil) | |
89 | - fmt.Println("secretKey:", xprv) | |
90 | - | |
91 | - xpub := xprv.XPub() | |
92 | - fmt.Println("publicKey:", xpub) | |
93 | - | |
94 | - derivateKey := xprv.Derive(fedConsensusPath) | |
95 | - fmt.Println("derivateSecretKey:", derivateKey) | |
96 | - | |
97 | - derivatePublicKey := derivateKey.XPub() | |
98 | - fmt.Println("derivatePublicKey", derivatePublicKey) | |
99 | -} | |
100 | - | |
101 | -func newFederationConfig() *cfg.FederationConfig { | |
102 | - return &cfg.FederationConfig{ | |
103 | - Xpubs: []chainkd.XPub{ | |
104 | - xpub("32fe453097591f288315ef47b1ebdabf20e8bced8ede670f999980205cacddd4a2a56121b8eab313b8f36939e8190fe8f267f19496decb91be5644e92b669914"), | |
105 | - xpub("ebe1060254ec43bd7883e94583ff0a71ef0ec0e1ada4cd0f5ed7e9d37f1d244e83d4fac0f94d157cfc720b77602f21b6b8a7e86f95c571e4d7986210dbce44c9"), | |
106 | - }, | |
107 | - Quorum: 1, | |
108 | - } | |
109 | -} | |
110 | - | |
111 | -func getBlockerOrder(startTimestamp, blockTimestamp, numOfConsensusNode uint64) uint64 { | |
112 | - // One round of product block time for all consensus nodes | |
113 | - roundBlockTime := consensus.ActiveNetParams.BlockNumEachNode * numOfConsensusNode * consensus.ActiveNetParams.BlockTimeInterval | |
114 | - // The start time of the last round of product block | |
115 | - lastRoundStartTime := startTimestamp + (blockTimestamp-startTimestamp)/roundBlockTime*roundBlockTime | |
116 | - // Order of blocker | |
117 | - return (blockTimestamp - lastRoundStartTime) / (consensus.ActiveNetParams.BlockNumEachNode * consensus.ActiveNetParams.BlockTimeInterval) | |
118 | -} | |
119 | - | |
120 | -func getPrevRoundLastBlock(c *protocol.Chain, store protocol.Store, prevBlockHash *bc.Hash) (*types.BlockHeader, error) { | |
121 | - blockHeader, err := store.GetBlockHeader(prevBlockHash) | |
122 | - if err != nil { | |
123 | - return nil, err | |
124 | - } | |
125 | - | |
126 | - for blockHeader.Height%consensus.ActiveNetParams.RoundVoteBlockNums != 0 { | |
127 | - blockHeader, err = store.GetBlockHeader(&blockHeader.PreviousBlockHash) | |
128 | - if err != nil { | |
129 | - return nil, err | |
130 | - } | |
131 | - } | |
132 | - return blockHeader, nil | |
133 | -} | |
134 | - | |
135 | -// according to getOrder | |
136 | -func getXprv(c *protocol.Chain, store protocol.Store, timeStamp uint64) (*chainkd.XPrv, error) { | |
137 | - prevVoteRoundLastBlock, err := getPrevRoundLastBlock(c, store, c.BestBlockHash()) | |
138 | - if err != nil { | |
139 | - return &(Xprvs[0]), err | |
140 | - } | |
141 | - | |
142 | - startTimestamp := prevVoteRoundLastBlock.Timestamp + consensus.ActiveNetParams.BlockTimeInterval | |
143 | - order := getBlockerOrder(startTimestamp, timeStamp, uint64(len(Xprvs))) | |
144 | - if order >= uint64(len(Xprvs)) { | |
145 | - return nil, errors.New("bad order") | |
146 | - } | |
147 | - return &(Xprvs[order]), nil | |
148 | -} | |
149 | - | |
150 | -func getConsensusResult(c *protocol.Chain, store *database.Store, seq uint64, blockHeader *types.BlockHeader) (*state.ConsensusResult, error) { | |
151 | - consensusResult, err := store.GetConsensusResult(seq) | |
152 | - if err != nil { | |
153 | - return nil, err | |
154 | - } | |
155 | - | |
156 | - return consensusResult, nil | |
157 | -} | |
158 | - | |
159 | -func TestRollback(t *testing.T) { | |
160 | - // genesisBlock := config.GenesisBlock() | |
161 | - | |
162 | - db := dbm.NewDB("block_test_db", "leveldb", "block_test_db") | |
163 | - defer os.RemoveAll("block_test_db") | |
164 | - | |
165 | - cfg.CommonConfig = cfg.DefaultConfig() | |
166 | - cfg.CommonConfig.Federation = newFederationConfig() | |
167 | - | |
168 | - xp := xprv("c87f8d0f4bb4b0acbb7f69f1954c4f34d4476e114fffa7b0c853992474a9954a273c2d8f2642a7baf94ebac88f1625af9f5eaf3b13a90de27eec3de78b9fb9ca") | |
169 | - cfg.CommonConfig.XPrv = &xp | |
170 | - consensus.ActiveNetParams.RoundVoteBlockNums = 3 | |
171 | - | |
172 | - store := database.NewStore(db) | |
173 | - dispatcher := event.NewDispatcher() | |
174 | - | |
175 | - movCore := mov.NewMovCore(cfg.CommonConfig.DBBackend, cfg.CommonConfig.DBDir(), consensus.ActiveNetParams.MovStartHeight) | |
176 | - txPool := protocol.NewTxPool(store, []protocol.DustFilterer{movCore}, dispatcher) | |
177 | - chain, err := protocol.NewChain(store, txPool, []protocol.Protocoler{movCore}, dispatcher) | |
178 | - | |
179 | - hsm, err := pseudohsm.New(cfg.CommonConfig.KeysDir()) | |
180 | - walletDB := dbm.NewDB("wallet", cfg.CommonConfig.DBBackend, cfg.CommonConfig.DBDir()) | |
181 | - walletStore := database.NewWalletStore(walletDB) | |
182 | - accountStore := database.NewAccountStore(walletDB) | |
183 | - accounts := account.NewManager(accountStore, chain) | |
184 | - assets := asset.NewRegistry(walletDB, chain) | |
185 | - wallet, err := w.NewWallet(walletStore, accounts, assets, hsm, chain, dispatcher, cfg.CommonConfig.Wallet.TxIndex) | |
186 | - if err != nil { | |
187 | - t.Fatal("init NewWallet") | |
188 | - } | |
189 | - | |
190 | - // trigger rescan wallet | |
191 | - if cfg.CommonConfig.Wallet.Rescan { | |
192 | - wallet.RescanBlocks() | |
193 | - } | |
194 | - | |
195 | - cases := []struct { | |
196 | - desc string | |
197 | - startRunNum int | |
198 | - runBlockNum int | |
199 | - }{ | |
200 | - { | |
201 | - desc: "first round block", | |
202 | - startRunNum: 5, | |
203 | - runBlockNum: 5, | |
204 | - }, | |
205 | - // { | |
206 | - // desc: "second add blocks", | |
207 | - // startRunNum: 3, | |
208 | - // runBlockNum: 2, | |
209 | - // }, | |
210 | - // { | |
211 | - // desc: "third add blocks", | |
212 | - // startRunNum: 100, | |
213 | - // runBlockNum: 100, | |
214 | - // }, | |
215 | - } | |
216 | - | |
217 | - warnDuration := time.Duration(consensus.ActiveNetParams.BlockTimeInterval*warnTimeNum/warnTimeDenom) * time.Millisecond | |
218 | - criticalDuration := time.Duration(consensus.ActiveNetParams.BlockTimeInterval*criticalTimeNum/criticalTimeDenom) * time.Millisecond | |
219 | - | |
220 | - for caseIndex, c := range cases { | |
221 | - beforeBlocks := []*types.Block{} | |
222 | - afterBlocks := []*types.Block{} | |
223 | - expectConsensusResultsMap := map[uint64]*state.ConsensusResult{} | |
224 | - nowConsensusResultsMap := map[uint64]*state.ConsensusResult{} | |
225 | - | |
226 | - for i := 0; i < c.startRunNum; i++ { | |
227 | - timeStamp := chain.BestBlockHeader().Timestamp + consensus.ActiveNetParams.BlockTimeInterval | |
228 | - config.CommonConfig.XPrv, err = getXprv(chain, store, timeStamp) | |
229 | - if err != nil { | |
230 | - t.Fatal(err) | |
231 | - } | |
232 | - | |
233 | - block, err := proposal.NewBlockTemplate(chain, accounts, timeStamp, warnDuration, criticalDuration) | |
234 | - if err != nil { | |
235 | - t.Fatal(err) | |
236 | - } | |
237 | - | |
238 | - if _, err := chain.ProcessBlock(block); err != nil { | |
239 | - t.Fatal(err) | |
240 | - } | |
241 | - | |
242 | - blockHash := block.Hash() | |
243 | - gotBlock, err := store.GetBlock(&blockHash) | |
244 | - beforeBlocks = append(beforeBlocks, gotBlock) | |
245 | - if err != nil { | |
246 | - t.Fatal(err) | |
247 | - } | |
248 | - } | |
249 | - | |
250 | - for i := 0; i < len(beforeBlocks); i++ { | |
251 | - block := beforeBlocks[i] | |
252 | - blockHash := block.Hash() | |
253 | - consensusResult, err := chain.GetConsensusResultByHash(&blockHash) | |
254 | - if err != nil { | |
255 | - | |
256 | - t.Fatal(err) | |
257 | - } | |
258 | - | |
259 | - expectConsensusResultsMap[state.CalcVoteSeq(block.Height)] = consensusResult | |
260 | - } | |
261 | - | |
262 | - expectChainStatus := store.GetStoreStatus() | |
263 | - expectHeight := chain.BestBlockHeight() | |
264 | - for i := 0; i < c.runBlockNum; i++ { | |
265 | - timeStamp := chain.BestBlockHeader().Timestamp + consensus.ActiveNetParams.BlockTimeInterval | |
266 | - config.CommonConfig.XPrv, err = getXprv(chain, store, timeStamp) | |
267 | - if err != nil { | |
268 | - t.Fatal(err) | |
269 | - } | |
270 | - | |
271 | - //block, err := proposal.NewBlockTemplate(chain, txPool, nil, timeStamp) | |
272 | - block, err := proposal.NewBlockTemplate(chain, accounts, timeStamp, warnDuration, criticalDuration) | |
273 | - if err != nil { | |
274 | - t.Fatal(err) | |
275 | - } | |
276 | - | |
277 | - if _, err := chain.ProcessBlock(block); err != nil { | |
278 | - t.Fatal(err) | |
279 | - } | |
280 | - | |
281 | - blockHash := block.Hash() | |
282 | - gotBlock, err := store.GetBlock(&blockHash) | |
283 | - afterBlocks = append(afterBlocks, gotBlock) | |
284 | - if err != nil { | |
285 | - t.Fatal(err) | |
286 | - } | |
287 | - } | |
288 | - | |
289 | - if err = chain.Rollback(expectHeight); err != nil { | |
290 | - t.Fatal(err) | |
291 | - } | |
292 | - | |
293 | - nowHeight := chain.BestBlockHeight() | |
294 | - if expectHeight != nowHeight { | |
295 | - t.Fatalf("%s test failed, expected: %d, now: %d", c.desc, expectHeight, nowHeight) | |
296 | - } | |
297 | - | |
298 | - if !testutil.DeepEqual(store.GetStoreStatus(), expectChainStatus) { | |
299 | - t.Errorf("got block status:%v, expect block status:%v", store.GetStoreStatus(), expectChainStatus) | |
300 | - } | |
301 | - | |
302 | - for i := 0; i < len(beforeBlocks); i++ { | |
303 | - block := beforeBlocks[i] | |
304 | - blockHash := block.Hash() | |
305 | - gotBlock, err := store.GetBlock(&blockHash) | |
306 | - if err != nil { | |
307 | - t.Fatal(err) | |
308 | - } | |
309 | - | |
310 | - if !testutil.DeepEqual(gotBlock, block) { | |
311 | - t.Errorf("case %v,%v: block mismatch: have %x, want %x", caseIndex, i, gotBlock, block) | |
312 | - } | |
313 | - | |
314 | - gotBlockHeader, err := store.GetBlockHeader(&blockHash) | |
315 | - if err != nil { | |
316 | - t.Fatal(err) | |
317 | - } | |
318 | - | |
319 | - if !testutil.DeepEqual(block.BlockHeader, *gotBlockHeader) { | |
320 | - t.Errorf("got block header:%v, expect block header:%v", gotBlockHeader, block.BlockHeader) | |
321 | - } | |
322 | - | |
323 | - consensusResult, err := chain.GetConsensusResultByHash(&blockHash) | |
324 | - if err != nil { | |
325 | - | |
326 | - t.Fatal(err) | |
327 | - } | |
328 | - | |
329 | - nowConsensusResultsMap[state.CalcVoteSeq(block.Height)] = consensusResult | |
330 | - } | |
331 | - | |
332 | - if !testutil.DeepEqual(expectConsensusResultsMap, nowConsensusResultsMap) { | |
333 | - t.Errorf("consensusResult is not equal!") | |
334 | - } | |
335 | - | |
336 | - finalSeq := state.CalcVoteSeq(chain.BestBlockHeight()) | |
337 | - for i := 0; i < len(afterBlocks); i++ { | |
338 | - block := afterBlocks[i] | |
339 | - blockHash := block.Hash() | |
340 | - _, err := store.GetBlockHeader(&blockHash) | |
341 | - if err == nil { | |
342 | - t.Errorf("this block should not exists!") | |
343 | - } | |
344 | - | |
345 | - // Code below tests will be update in later PR | |
346 | - // this code pr is too big | |
347 | - // to test consensusResult whether right or not | |
348 | - seq := state.CalcVoteSeq(block.Height) | |
349 | - if seq > finalSeq { | |
350 | - consensusResult, err := getConsensusResult(chain, store, seq, &block.BlockHeader) | |
351 | - if err == nil { | |
352 | - t.Errorf("why this result existed! %v, %v", consensusResult, err) | |
353 | - } | |
354 | - } | |
355 | - } | |
356 | - } | |
357 | -} |