OSDN Git Service

fix next leader time (#98)
[bytom/vapor.git] / protocol / protocol.go
1 package protocol
2
3 import (
4         "sync"
5
6         log "github.com/sirupsen/logrus"
7
8         "github.com/vapor/config"
9         "github.com/vapor/event"
10         "github.com/vapor/protocol/bc"
11         "github.com/vapor/protocol/bc/types"
12         "github.com/vapor/protocol/state"
13 )
14
15 const maxProcessBlockChSize = 1024
16
17 // Chain provides functions for working with the Bytom block chain.
18 type Chain struct {
19         index          *state.BlockIndex
20         orphanManage   *OrphanManage
21         txPool         *TxPool
22         store          Store
23         bbft           *bbft
24         processBlockCh chan *processBlockMsg
25
26         cond                 sync.Cond
27         bestNode             *state.BlockNode
28         bestIrreversibleNode *state.BlockNode
29 }
30
31 // NewChain returns a new Chain using store as the underlying storage.
32 func NewChain(store Store, txPool *TxPool, eventDispatcher *event.Dispatcher) (*Chain, error) {
33         c := &Chain{
34                 orphanManage:   NewOrphanManage(),
35                 txPool:         txPool,
36                 store:          store,
37                 processBlockCh: make(chan *processBlockMsg, maxProcessBlockChSize),
38         }
39         c.cond.L = new(sync.Mutex)
40
41         storeStatus := store.GetStoreStatus()
42         if storeStatus == nil {
43                 if err := c.initChainStatus(); err != nil {
44                         return nil, err
45                 }
46                 storeStatus = store.GetStoreStatus()
47         }
48
49         var err error
50         if c.index, err = store.LoadBlockIndex(storeStatus.Height); err != nil {
51                 return nil, err
52         }
53
54         c.bestNode = c.index.GetNode(storeStatus.Hash)
55         c.bestIrreversibleNode = c.index.GetNode(storeStatus.IrreversibleHash)
56         c.index.SetMainChain(c.bestNode)
57         c.bbft = newBbft(store, c.index, c.orphanManage, eventDispatcher)
58         go c.blockProcesser()
59         return c, nil
60 }
61
62 func (c *Chain) initChainStatus() error {
63         genesisBlock := config.GenesisBlock()
64         txStatus := bc.NewTransactionStatus()
65         for i := range genesisBlock.Transactions {
66                 if err := txStatus.SetStatus(i, false); err != nil {
67                         return err
68                 }
69         }
70
71         if err := c.store.SaveBlock(genesisBlock, txStatus); err != nil {
72                 return err
73         }
74
75         utxoView := state.NewUtxoViewpoint()
76         bcBlock := types.MapBlock(genesisBlock)
77         if err := utxoView.ApplyBlock(bcBlock, txStatus); err != nil {
78                 return err
79         }
80
81         node, err := state.NewBlockNode(&genesisBlock.BlockHeader, nil)
82         if err != nil {
83                 return err
84         }
85
86         return c.store.SaveChainStatus(node, node, utxoView, map[uint64]*state.VoteResult{})
87 }
88
89 // BestBlockHeight returns the current height of the blockchain.
90 func (c *Chain) BestBlockHeight() uint64 {
91         c.cond.L.Lock()
92         defer c.cond.L.Unlock()
93         return c.bestNode.Height
94 }
95
96 // BestBlockHash return the hash of the chain tail block
97 func (c *Chain) BestBlockHash() *bc.Hash {
98         c.cond.L.Lock()
99         defer c.cond.L.Unlock()
100         return &c.bestNode.Hash
101 }
102
103 // BestBlockHeader returns the chain tail block
104 func (c *Chain) BestBlockHeader() *types.BlockHeader {
105         node := c.index.BestNode()
106         return node.BlockHeader()
107 }
108
109 // InMainChain checks wheather a block is in the main chain
110 func (c *Chain) InMainChain(hash bc.Hash) bool {
111         return c.index.InMainchain(hash)
112 }
113
114 // This function must be called with mu lock in above level
115 func (c *Chain) setState(node *state.BlockNode, irreversibleNode *state.BlockNode, view *state.UtxoViewpoint, voteMap map[uint64]*state.VoteResult) error {
116         if err := c.store.SaveChainStatus(node, irreversibleNode, view, voteMap); err != nil {
117                 return err
118         }
119
120         c.cond.L.Lock()
121         defer c.cond.L.Unlock()
122
123         c.index.SetMainChain(node)
124         c.bestNode = node
125         c.bestIrreversibleNode = irreversibleNode
126
127         log.WithFields(log.Fields{"module": logModule, "height": c.bestNode.Height, "hash": c.bestNode.Hash.String()}).Debug("chain best status has been update")
128         c.cond.Broadcast()
129         return nil
130 }
131
132 // BlockWaiter returns a channel that waits for the block at the given height.
133 func (c *Chain) BlockWaiter(height uint64) <-chan struct{} {
134         ch := make(chan struct{}, 1)
135         go func() {
136                 c.cond.L.Lock()
137                 defer c.cond.L.Unlock()
138                 for c.bestNode.Height < height {
139                         c.cond.Wait()
140                 }
141                 ch <- struct{}{}
142         }()
143
144         return ch
145 }
146
147 // GetTxPool return chain txpool.
148 func (c *Chain) GetTxPool() *TxPool {
149         return c.txPool
150 }
151
152 // GetBBFT return chain bbft
153 func (c *Chain) GetBBFT() *bbft {
154         return c.bbft
155 }