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