8 "github.com/bytom/blockchain/txdb"
9 "github.com/bytom/blockchain/txdb/storage"
10 "github.com/bytom/errors"
11 "github.com/bytom/protocol/bc"
12 "github.com/bytom/protocol/bc/legacy"
13 "github.com/bytom/protocol/seed"
14 "github.com/bytom/protocol/state"
17 // maxCachedValidatedTxs is the max number of validated txs to cache.
18 const maxCachedValidatedTxs = 1000
21 // ErrTheDistantFuture is returned when waiting for a blockheight
22 // too far in excess of the tip of the blockchain.
23 ErrTheDistantFuture = errors.New("block height too far in future")
26 // Store provides storage for blockchain data: blocks and state tree
29 // Note, this is different from a state snapshot. A state snapshot
30 // provides access to the state at a given point in time -- outputs
31 // and issuance memory. The Chain type uses Store to load state
32 // from storage and persist validated data.
33 type Store interface {
34 BlockExist(*bc.Hash) bool
36 GetBlock(*bc.Hash) (*legacy.Block, error)
37 GetMainchain(*bc.Hash) (map[uint64]*bc.Hash, error)
38 GetStoreStatus() txdb.BlockStoreStateJSON
39 GetTransactionsUtxo(*state.UtxoViewpoint, []*bc.Tx) error
40 GetUtxo(*bc.Hash) (*storage.UtxoEntry, error)
42 SaveBlock(*legacy.Block) error
43 SaveChainStatus(*legacy.Block, *state.UtxoViewpoint, map[uint64]*bc.Hash) error
46 // OrphanManage is use to handle all the orphan block
47 type OrphanManage struct {
48 //TODO: add orphan cached block limit
49 orphan map[bc.Hash]*legacy.Block
50 preOrphans map[bc.Hash][]*bc.Hash
54 // NewOrphanManage return a new orphan block
55 func NewOrphanManage() *OrphanManage {
57 orphan: make(map[bc.Hash]*legacy.Block),
58 preOrphans: make(map[bc.Hash][]*bc.Hash),
62 // BlockExist check is the block in OrphanManage
63 func (o *OrphanManage) BlockExist(hash *bc.Hash) bool {
65 _, ok := o.orphan[*hash]
70 // Add will add the block to OrphanManage
71 func (o *OrphanManage) Add(block *legacy.Block) {
72 blockHash := block.Hash()
76 if _, ok := o.orphan[blockHash]; ok {
80 o.orphan[blockHash] = block
81 o.preOrphans[block.PreviousBlockHash] = append(o.preOrphans[block.PreviousBlockHash], &blockHash)
84 // Delete will delelte the block from OrphanManage
85 func (o *OrphanManage) Delete(hash *bc.Hash) {
88 block, ok := o.orphan[*hash]
92 delete(o.orphan, *hash)
94 preOrphans, ok := o.preOrphans[block.PreviousBlockHash]
95 if !ok || len(preOrphans) == 1 {
96 delete(o.preOrphans, block.PreviousBlockHash)
100 for i, preOrphan := range preOrphans {
101 if preOrphan == hash {
102 o.preOrphans[block.PreviousBlockHash] = append(preOrphans[:i], preOrphans[i+1:]...)
108 // Get return the orphan block by hash
109 func (o *OrphanManage) Get(hash *bc.Hash) (*legacy.Block, bool) {
111 block, ok := o.orphan[*hash]
116 // Chain provides a complete, minimal blockchain database. It
117 // delegates the underlying storage to other objects, and uses
118 // validation logic from package validation to decide what
119 // objects can be safely stored.
121 InitialBlockHash bc.Hash
122 MaxIssuanceWindow time.Duration // only used by generators
124 orphanManage *OrphanManage
131 mainChain map[uint64]*bc.Hash
134 seedCaches *seed.SeedCaches
137 // NewChain returns a new Chain using store as the underlying storage.
138 func NewChain(initialBlockHash bc.Hash, store Store, txPool *TxPool) (*Chain, error) {
140 InitialBlockHash: initialBlockHash,
141 orphanManage: NewOrphanManage(),
144 seedCaches: seed.NewSeedCaches(),
146 c.state.cond.L = new(sync.Mutex)
147 storeStatus := store.GetStoreStatus()
149 if storeStatus.Hash == nil {
150 c.state.mainChain = make(map[uint64]*bc.Hash)
154 c.state.hash = storeStatus.Hash
156 if c.state.block, err = store.GetBlock(storeStatus.Hash); err != nil {
159 if c.state.mainChain, err = store.GetMainchain(storeStatus.Hash); err != nil {
165 // Height returns the current height of the blockchain.
166 func (c *Chain) Height() uint64 {
167 c.state.cond.L.Lock()
168 defer c.state.cond.L.Unlock()
169 if c.state.block == nil {
172 return c.state.block.Height
175 // BestBlockHash return the hash of the chain tail block
176 func (c *Chain) BestBlockHash() *bc.Hash {
177 c.state.cond.L.Lock()
178 defer c.state.cond.L.Unlock()
182 func (c *Chain) inMainchain(block *legacy.Block) bool {
183 hash, ok := c.state.mainChain[block.Height]
187 return *hash == block.Hash()
190 // InMainChain checks wheather a block is in the main chain
191 func (c *Chain) InMainChain(height uint64, hash bc.Hash) bool {
192 c.state.cond.L.Lock()
193 h, ok := c.state.mainChain[height]
194 c.state.cond.L.Unlock()
202 // TimestampMS returns the latest known block timestamp.
203 func (c *Chain) Timestamp() uint64 {
204 c.state.cond.L.Lock()
205 defer c.state.cond.L.Unlock()
206 if c.state.block == nil {
209 return c.state.block.Timestamp
212 // BestBlock returns the chain tail block
213 func (c *Chain) BestBlock() *legacy.Block {
214 c.state.cond.L.Lock()
215 defer c.state.cond.L.Unlock()
219 // SeedCaches return the seedCached manager
220 func (c *Chain) SeedCaches() *seed.SeedCaches {
224 // GetUtxo try to find the utxo status in db
225 func (c *Chain) GetUtxo(hash *bc.Hash) (*storage.UtxoEntry, error) {
226 return c.store.GetUtxo(hash)
229 // GetTransactionsUtxo return all the utxos that related to the txs' inputs
230 func (c *Chain) GetTransactionsUtxo(view *state.UtxoViewpoint, txs []*bc.Tx) error {
231 return c.store.GetTransactionsUtxo(view, txs)
234 // This function must be called with mu lock in above level
235 func (c *Chain) setState(block *legacy.Block, view *state.UtxoViewpoint, m map[uint64]*bc.Hash) error {
236 blockHash := block.Hash()
237 c.state.block = block
238 c.state.hash = &blockHash
239 for k, v := range m {
240 c.state.mainChain[k] = v
243 if err := c.store.SaveChainStatus(block, view, c.state.mainChain); err != nil {
247 c.state.cond.Broadcast()
251 // BlockSoonWaiter returns a channel that
252 // waits for the block at the given height,
253 // but it is an error to wait for a block far in the future.
254 // WaitForBlockSoon will timeout if the context times out.
255 // To wait unconditionally, the caller should use WaitForBlock.
256 func (c *Chain) BlockSoonWaiter(ctx context.Context, height uint64) <-chan error {
257 ch := make(chan error, 1)
261 if height > c.Height()+slop {
262 ch <- ErrTheDistantFuture
267 case <-c.BlockWaiter(height):
277 // BlockWaiter returns a channel that
278 // waits for the block at the given height.
279 func (c *Chain) BlockWaiter(height uint64) <-chan struct{} {
280 ch := make(chan struct{}, 1)
282 c.state.cond.L.Lock()
283 defer c.state.cond.L.Unlock()
284 for c.state.block.Height < height {