OSDN Git Service

8e41a3a8227b6a55b91195df37d7797550639ef8
[bytom/bytom.git] / test / mock / chain.go
1 package mock
2
3 import (
4         "errors"
5         "github.com/bytom/bytom/protocol/bc"
6         "github.com/bytom/bytom/protocol/bc/types"
7 )
8
9 var (
10         ErrFoundHeaderByHash   = errors.New("can't find header by hash")
11         ErrFoundHeaderByHeight = errors.New("can't find header by height")
12 )
13
14 type Chain struct {
15         bestBlockHeader *types.BlockHeader
16         heightMap       map[uint64]*types.Block
17         blockMap        map[bc.Hash]*types.Block
18
19         prevOrphans map[bc.Hash]*types.Block
20 }
21
22 func NewChain() *Chain {
23         return &Chain{
24                 heightMap:   map[uint64]*types.Block{},
25                 blockMap:    map[bc.Hash]*types.Block{},
26                 prevOrphans: make(map[bc.Hash]*types.Block),
27         }
28 }
29
30 func (c *Chain) LastIrreversibleHeader() *types.BlockHeader {
31         return nil
32 }
33
34 func (c *Chain) BestBlockHeader() *types.BlockHeader {
35         return c.bestBlockHeader
36 }
37
38 func (c *Chain) BestBlockHeight() uint64 {
39         return c.bestBlockHeader.Height
40 }
41
42 func (c *Chain) CalcNextSeed(hash *bc.Hash) (*bc.Hash, error) {
43         return &bc.Hash{V0: hash.V1, V1: hash.V2, V2: hash.V3, V3: hash.V0}, nil
44 }
45
46 func (c *Chain) GetBlockByHash(hash *bc.Hash) (*types.Block, error) {
47         block, ok := c.blockMap[*hash]
48         if !ok {
49                 return nil, errors.New("can't find block")
50         }
51         return block, nil
52 }
53
54 func (c *Chain) GetBlockByHeight(height uint64) (*types.Block, error) {
55         block, ok := c.heightMap[height]
56         if !ok {
57                 return nil, errors.New("can't find block")
58         }
59         return block, nil
60 }
61
62 func (c *Chain) GetHeaderByHash(hash *bc.Hash) (*types.BlockHeader, error) {
63         block, ok := c.blockMap[*hash]
64         if !ok {
65                 return nil, errors.New("can't find block")
66         }
67         return &block.BlockHeader, nil
68 }
69
70 func (c *Chain) GetHeaderByHeight(height uint64) (*types.BlockHeader, error) {
71         block, ok := c.heightMap[height]
72         if !ok {
73                 return nil, errors.New("can't find block")
74         }
75         return &block.BlockHeader, nil
76 }
77
78 func (c *Chain) InMainChain(hash bc.Hash) bool {
79         block, ok := c.blockMap[hash]
80         if !ok {
81                 return false
82         }
83         return c.heightMap[block.Height] == block
84 }
85
86 func (c *Chain) ProcessBlock(block *types.Block) (bool, error) {
87         if c.bestBlockHeader.Hash() == block.PreviousBlockHash {
88                 c.heightMap[block.Height] = block
89                 c.blockMap[block.Hash()] = block
90                 c.bestBlockHeader = &block.BlockHeader
91                 return false, nil
92         }
93
94         if _, ok := c.blockMap[block.PreviousBlockHash]; !ok {
95                 c.prevOrphans[block.PreviousBlockHash] = block
96                 return true, nil
97         }
98
99         c.blockMap[block.Hash()] = block
100         for c.prevOrphans[block.Hash()] != nil {
101                 block = c.prevOrphans[block.Hash()]
102                 c.blockMap[block.Hash()] = block
103         }
104
105         if block.Height < c.bestBlockHeader.Height {
106                 return false, nil
107         }
108
109         c.bestBlockHeader = &block.BlockHeader
110         for !c.InMainChain(block.Hash()) {
111                 c.heightMap[block.Height] = block
112                 block = c.blockMap[block.PreviousBlockHash]
113         }
114         return false, nil
115 }
116
117 func (c *Chain) SetBestBlockHeader(header *types.BlockHeader) {
118         c.bestBlockHeader = header
119 }
120
121 func (c *Chain) SetBlockByHeight(height uint64, block *types.Block) {
122         c.heightMap[height] = block
123         c.blockMap[block.Hash()] = block
124 }
125
126 func (c *Chain) ValidateTx(*types.Tx) (bool, error) {
127         return false, nil
128 }