• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: 提交

Golang implemented sidechain for Bytom


Commit MetaInfo

修订版156973a4d143100e69a980a6e3a36f83ece960a6 (tree)
时间2020-02-20 17:08:46
作者ipqhjjybj <250657661@qq.c...>
Commiteripqhjjybj

Log Message

fix one err

更改概述

差异

--- a/protocol/block.go
+++ b/protocol/block.go
@@ -262,7 +262,8 @@ func (c *Chain) reorganizeChain(blockHeader *types.BlockHeader) error {
262262 return err
263263 }
264264
265- if err = c.syncSubProtocols(); err != nil {
265+ err = c.syncSubProtocols()
266+ if err != nil {
266267 return err
267268 }
268269
--- a/test/rollback_test.go
+++ /dev/null
@@ -1,355 +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- db := dbm.NewDB("block_test_db", "leveldb", "block_test_db")
161- defer os.RemoveAll("block_test_db")
162-
163- cfg.CommonConfig = cfg.DefaultConfig()
164- cfg.CommonConfig.Federation = newFederationConfig()
165-
166- xp := xprv("c87f8d0f4bb4b0acbb7f69f1954c4f34d4476e114fffa7b0c853992474a9954a273c2d8f2642a7baf94ebac88f1625af9f5eaf3b13a90de27eec3de78b9fb9ca")
167- cfg.CommonConfig.XPrv = &xp
168- consensus.ActiveNetParams.RoundVoteBlockNums = 3
169-
170- store := database.NewStore(db)
171- dispatcher := event.NewDispatcher()
172-
173- movCore := mov.NewMovCore(cfg.CommonConfig.DBBackend, cfg.CommonConfig.DBDir(), consensus.ActiveNetParams.MovStartHeight)
174- txPool := protocol.NewTxPool(store, []protocol.DustFilterer{movCore}, dispatcher)
175- chain, err := protocol.NewChain(store, txPool, []protocol.Protocoler{movCore}, dispatcher)
176-
177- hsm, err := pseudohsm.New(cfg.CommonConfig.KeysDir())
178- walletDB := dbm.NewDB("wallet", cfg.CommonConfig.DBBackend, cfg.CommonConfig.DBDir())
179- walletStore := database.NewWalletStore(walletDB)
180- accountStore := database.NewAccountStore(walletDB)
181- accounts := account.NewManager(accountStore, chain)
182- assets := asset.NewRegistry(walletDB, chain)
183- wallet, err := w.NewWallet(walletStore, accounts, assets, hsm, chain, dispatcher, cfg.CommonConfig.Wallet.TxIndex)
184- if err != nil {
185- t.Fatal("init NewWallet")
186- }
187-
188- // trigger rescan wallet
189- if cfg.CommonConfig.Wallet.Rescan {
190- wallet.RescanBlocks()
191- }
192-
193- cases := []struct {
194- desc string
195- startRunNum int
196- runBlockNum int
197- }{
198- {
199- desc: "first round block",
200- startRunNum: 5,
201- runBlockNum: 5,
202- },
203- // {
204- // desc: "second add blocks",
205- // startRunNum: 3,
206- // runBlockNum: 2,
207- // },
208- // {
209- // desc: "third add blocks",
210- // startRunNum: 100,
211- // runBlockNum: 100,
212- // },
213- }
214-
215- warnDuration := time.Duration(consensus.ActiveNetParams.BlockTimeInterval*warnTimeNum/warnTimeDenom) * time.Millisecond
216- criticalDuration := time.Duration(consensus.ActiveNetParams.BlockTimeInterval*criticalTimeNum/criticalTimeDenom) * time.Millisecond
217-
218- for caseIndex, c := range cases {
219- beforeBlocks := []*types.Block{}
220- afterBlocks := []*types.Block{}
221- expectConsensusResultsMap := map[uint64]*state.ConsensusResult{}
222- nowConsensusResultsMap := map[uint64]*state.ConsensusResult{}
223-
224- for i := 0; i < c.startRunNum; i++ {
225- timeStamp := chain.BestBlockHeader().Timestamp + consensus.ActiveNetParams.BlockTimeInterval
226- config.CommonConfig.XPrv, err = getXprv(chain, store, timeStamp)
227- if err != nil {
228- t.Fatal(err)
229- }
230-
231- block, err := proposal.NewBlockTemplate(chain, accounts, timeStamp, warnDuration, criticalDuration)
232- if err != nil {
233- t.Fatal(err)
234- }
235-
236- if _, err := chain.ProcessBlock(block); err != nil {
237- t.Fatal(err)
238- }
239-
240- blockHash := block.Hash()
241- gotBlock, err := store.GetBlock(&blockHash)
242- beforeBlocks = append(beforeBlocks, gotBlock)
243- if err != nil {
244- t.Fatal(err)
245- }
246- }
247-
248- for i := 0; i < len(beforeBlocks); i++ {
249- block := beforeBlocks[i]
250- blockHash := block.Hash()
251- consensusResult, err := chain.GetConsensusResultByHash(&blockHash)
252- if err != nil {
253-
254- t.Fatal(err)
255- }
256-
257- expectConsensusResultsMap[state.CalcVoteSeq(block.Height)] = consensusResult
258- }
259-
260- expectChainStatus := store.GetStoreStatus()
261- expectHeight := chain.BestBlockHeight()
262- for i := 0; i < c.runBlockNum; i++ {
263- timeStamp := chain.BestBlockHeader().Timestamp + consensus.ActiveNetParams.BlockTimeInterval
264- config.CommonConfig.XPrv, err = getXprv(chain, store, timeStamp)
265- if err != nil {
266- t.Fatal(err)
267- }
268-
269- //block, err := proposal.NewBlockTemplate(chain, txPool, nil, timeStamp)
270- block, err := proposal.NewBlockTemplate(chain, accounts, timeStamp, warnDuration, criticalDuration)
271- if err != nil {
272- t.Fatal(err)
273- }
274-
275- if _, err := chain.ProcessBlock(block); err != nil {
276- t.Fatal(err)
277- }
278-
279- blockHash := block.Hash()
280- gotBlock, err := store.GetBlock(&blockHash)
281- afterBlocks = append(afterBlocks, gotBlock)
282- if err != nil {
283- t.Fatal(err)
284- }
285- }
286-
287- if err = chain.Rollback(expectHeight); err != nil {
288- t.Fatal(err)
289- }
290-
291- nowHeight := chain.BestBlockHeight()
292- if expectHeight != nowHeight {
293- t.Fatalf("%s test failed, expected: %d, now: %d", c.desc, expectHeight, nowHeight)
294- }
295-
296- if !testutil.DeepEqual(store.GetStoreStatus(), expectChainStatus) {
297- t.Errorf("got block status:%v, expect block status:%v", store.GetStoreStatus(), expectChainStatus)
298- }
299-
300- for i := 0; i < len(beforeBlocks); i++ {
301- block := beforeBlocks[i]
302- blockHash := block.Hash()
303- gotBlock, err := store.GetBlock(&blockHash)
304- if err != nil {
305- t.Fatal(err)
306- }
307-
308- if !testutil.DeepEqual(gotBlock, block) {
309- t.Errorf("case %v,%v: block mismatch: have %x, want %x", caseIndex, i, gotBlock, block)
310- }
311-
312- gotBlockHeader, err := store.GetBlockHeader(&blockHash)
313- if err != nil {
314- t.Fatal(err)
315- }
316-
317- if !testutil.DeepEqual(block.BlockHeader, *gotBlockHeader) {
318- t.Errorf("got block header:%v, expect block header:%v", gotBlockHeader, block.BlockHeader)
319- }
320-
321- consensusResult, err := chain.GetConsensusResultByHash(&blockHash)
322- if err != nil {
323-
324- t.Fatal(err)
325- }
326-
327- nowConsensusResultsMap[state.CalcVoteSeq(block.Height)] = consensusResult
328- }
329-
330- if !testutil.DeepEqual(expectConsensusResultsMap, nowConsensusResultsMap) {
331- t.Errorf("consensusResult is not equal!")
332- }
333-
334- // finalSeq := state.CalcVoteSeq(chain.BestBlockHeight())
335- for i := 0; i < len(afterBlocks); i++ {
336- block := afterBlocks[i]
337- blockHash := block.Hash()
338- _, err := store.GetBlockHeader(&blockHash)
339- if err == nil {
340- t.Errorf("this block should not exists!")
341- }
342-
343- // Code below tests will be update in later PR
344- // this code pr is too big
345- // to test consensusResult whether right or not
346- // seq := state.CalcVoteSeq(block.Height)
347- // if seq > finalSeq {
348- // consensusResult, err := getConsensusResult(chain, store, seq, &block.BlockHeader)
349- // if err == nil {
350- // t.Errorf("why this result existed! %v, %v", consensusResult, err)
351- // }
352- // }
353- }
354- }
355-}
Show on old repository browser