OSDN Git Service

Merge pull request #15 from Bytom/dev_ci
[bytom/vapor.git] / test / protocol_test_util.go
1 package test
2
3 import (
4         "fmt"
5
6         dbm "github.com/tendermint/tmlibs/db"
7
8         "github.com/vapor/protocol"
9         "github.com/vapor/protocol/bc/types"
10 )
11
12 func declChain(name string, baseChain *protocol.Chain, baseHeight uint64, height uint64) (*protocol.Chain, error) {
13         chainDB := dbm.NewDB(name, "leveldb", name)
14         chain, _, _, err := MockChain(chainDB)
15         if err != nil {
16                 return nil, err
17         }
18
19         if baseChain == nil {
20                 if err := AppendBlocks(chain, height); err != nil {
21                         return nil, err
22                 }
23                 return chain, nil
24         }
25
26         for i := uint64(1); i <= baseHeight; i++ {
27                 block, err := baseChain.GetBlockByHeight(i)
28                 if err != nil {
29                         return nil, err
30                 }
31                 if err := SolveAndUpdate(chain, block); err != nil {
32                         return nil, err
33                 }
34         }
35
36         err = AppendBlocks(chain, height-baseHeight)
37         return chain, err
38 }
39
40 func ancestorOf(c1 *protocol.Chain, c2 *protocol.Chain) (*types.Block, error) {
41         start := c1.BestBlockHeight()
42         if c2.BestBlockHeight() < c1.BestBlockHeight() {
43                 start = c2.BestBlockHeight()
44         }
45
46         for i := start; i >= 0; i-- {
47                 b1, err := c1.GetBlockByHeight(i)
48                 if err != nil {
49                         return nil, err
50                 }
51                 b2, err := c2.GetBlockByHeight(i)
52                 if err != nil {
53                         return nil, err
54                 }
55                 if b1.Hash() == b2.Hash() {
56                         return b1, nil
57                 }
58         }
59         return nil, fmt.Errorf("can't find ancestor")
60 }
61
62 func merge(c1 *protocol.Chain, c2 *protocol.Chain) error {
63         // c1 and c2 are same
64         if c1.BestBlockHeight() == c2.BestBlockHeight() && *c1.BestBlockHash() == *c2.BestBlockHash() {
65                 return nil
66         }
67
68         ancestor, err := ancestorOf(c1, c2)
69         if err != nil {
70                 return err
71         }
72
73         processBlocks := func(dest *protocol.Chain, src *protocol.Chain, height uint64) error {
74                 for h := src.BestBlockHeight(); h > height; h-- {
75                         block, err := src.GetBlockByHeight(h)
76                         if err != nil {
77                                 return err
78                         }
79                         _, err = dest.ProcessBlock(block)
80                         if err != nil {
81                                 return err
82                         }
83                 }
84                 return nil
85         }
86
87         if err := processBlocks(c1, c2, ancestor.Height); err != nil {
88                 return err
89         }
90         return processBlocks(c2, c1, ancestor.Height)
91 }