6 dbm "github.com/vapor/database/leveldb"
7 "github.com/vapor/protocol"
8 "github.com/vapor/protocol/bc/types"
11 func declChain(name string, baseChain *protocol.Chain, baseHeight uint64, height uint64) (*protocol.Chain, error) {
12 chainDB := dbm.NewDB(name, "leveldb", name)
13 chain, _, _, err := MockChain(chainDB)
19 if err := AppendBlocks(chain, height); err != nil {
25 for i := uint64(1); i <= baseHeight; i++ {
26 block, err := baseChain.GetBlockByHeight(i)
30 if _, err := chain.ProcessBlock(block); err != nil {
35 err = AppendBlocks(chain, height-baseHeight)
39 func ancestorOf(c1 *protocol.Chain, c2 *protocol.Chain) (*types.Block, error) {
40 start := c1.BestBlockHeight()
41 if c2.BestBlockHeight() < c1.BestBlockHeight() {
42 start = c2.BestBlockHeight()
45 for i := start; i >= 0; i-- {
46 b1, err := c1.GetBlockByHeight(i)
50 b2, err := c2.GetBlockByHeight(i)
54 if b1.Hash() == b2.Hash() {
58 return nil, fmt.Errorf("can't find ancestor")
61 func merge(c1 *protocol.Chain, c2 *protocol.Chain) error {
63 if c1.BestBlockHeight() == c2.BestBlockHeight() && *c1.BestBlockHash() == *c2.BestBlockHash() {
67 ancestor, err := ancestorOf(c1, c2)
72 processBlocks := func(dest *protocol.Chain, src *protocol.Chain, height uint64) error {
73 for h := src.BestBlockHeight(); h > height; h-- {
74 block, err := src.GetBlockByHeight(h)
78 _, err = dest.ProcessBlock(block)
86 if err := processBlocks(c1, c2, ancestor.Height); err != nil {
89 return processBlocks(c2, c1, ancestor.Height)