OSDN Git Service

update master (#487)
[bytom/bytom.git] / protocol / protocol.go
old mode 100644 (file)
new mode 100755 (executable)
index ac838fd..38f405d
@@ -5,10 +5,12 @@ import (
        "sync"
        "time"
 
-       "github.com/bytom/blockchain/txdb"
+       "github.com/bytom/consensus"
+       "github.com/bytom/database"
+       "github.com/bytom/database/storage"
        "github.com/bytom/errors"
        "github.com/bytom/protocol/bc"
-       "github.com/bytom/protocol/bc/legacy"
+       "github.com/bytom/protocol/bc/types"
        "github.com/bytom/protocol/state"
 )
 
@@ -21,31 +23,10 @@ var (
        ErrTheDistantFuture = errors.New("block height too far in future")
 )
 
-// Store provides storage for blockchain data: blocks and state tree
-// snapshots.
-//
-// Note, this is different from a state snapshot. A state snapshot
-// provides access to the state at a given point in time -- outputs
-// and issuance memory. The Chain type uses Store to load state
-// from storage and persist validated data.
-type Store interface {
-       BlockExist(*bc.Hash) bool
-
-       GetBlock(*bc.Hash) (*legacy.Block, error)
-       GetMainchain(*bc.Hash) (map[uint64]*bc.Hash, error)
-       GetSnapshot(*bc.Hash) (*state.Snapshot, error)
-       GetStoreStatus() txdb.BlockStoreStateJSON
-
-       SaveBlock(*legacy.Block) error
-       SaveMainchain(map[uint64]*bc.Hash, *bc.Hash) error
-       SaveSnapshot(*state.Snapshot, *bc.Hash) error
-       SaveStoreStatus(uint64, *bc.Hash)
-}
-
 // OrphanManage is use to handle all the orphan block
 type OrphanManage struct {
        //TODO: add orphan cached block limit
-       orphan     map[bc.Hash]*legacy.Block
+       orphan     map[bc.Hash]*types.Block
        preOrphans map[bc.Hash][]*bc.Hash
        mtx        sync.RWMutex
 }
@@ -53,7 +34,7 @@ type OrphanManage struct {
 // NewOrphanManage return a new orphan block
 func NewOrphanManage() *OrphanManage {
        return &OrphanManage{
-               orphan:     make(map[bc.Hash]*legacy.Block),
+               orphan:     make(map[bc.Hash]*types.Block),
                preOrphans: make(map[bc.Hash][]*bc.Hash),
        }
 }
@@ -67,7 +48,7 @@ func (o *OrphanManage) BlockExist(hash *bc.Hash) bool {
 }
 
 // Add will add the block to OrphanManage
-func (o *OrphanManage) Add(block *legacy.Block) {
+func (o *OrphanManage) Add(block *types.Block) {
        blockHash := block.Hash()
        o.mtx.Lock()
        defer o.mtx.Unlock()
@@ -105,7 +86,7 @@ func (o *OrphanManage) Delete(hash *bc.Hash) {
 }
 
 // Get return the orphan block by hash
-func (o *OrphanManage) Get(hash *bc.Hash) (*legacy.Block, bool) {
+func (o *OrphanManage) Get(hash *bc.Hash) (*types.Block, bool) {
        o.mtx.RLock()
        block, ok := o.orphan[*hash]
        o.mtx.RUnlock()
@@ -125,16 +106,15 @@ type Chain struct {
 
        state struct {
                cond      sync.Cond
-               block     *legacy.Block
+               block     *types.Block
                hash      *bc.Hash
                mainChain map[uint64]*bc.Hash
-               snapshot  *state.Snapshot
        }
-       store Store
+       store database.Store
 }
 
 // NewChain returns a new Chain using store as the underlying storage.
-func NewChain(initialBlockHash bc.Hash, store Store, txPool *TxPool) (*Chain, error) {
+func NewChain(initialBlockHash bc.Hash, store database.Store, txPool *TxPool) (*Chain, error) {
        c := &Chain{
                InitialBlockHash: initialBlockHash,
                orphanManage:     NewOrphanManage(),
@@ -144,8 +124,7 @@ func NewChain(initialBlockHash bc.Hash, store Store, txPool *TxPool) (*Chain, er
        c.state.cond.L = new(sync.Mutex)
        storeStatus := store.GetStoreStatus()
 
-       if storeStatus.Height == 0 {
-               c.state.snapshot = state.Empty()
+       if storeStatus.Hash == nil {
                c.state.mainChain = make(map[uint64]*bc.Hash)
                return c, nil
        }
@@ -155,9 +134,6 @@ func NewChain(initialBlockHash bc.Hash, store Store, txPool *TxPool) (*Chain, er
        if c.state.block, err = store.GetBlock(storeStatus.Hash); err != nil {
                return nil, err
        }
-       if c.state.snapshot, err = store.GetSnapshot(storeStatus.Hash); err != nil {
-               return nil, err
-       }
        if c.state.mainChain, err = store.GetMainchain(storeStatus.Hash); err != nil {
                return nil, err
        }
@@ -174,13 +150,14 @@ func (c *Chain) Height() uint64 {
        return c.state.block.Height
 }
 
+// BestBlockHash return the hash of the chain tail block
 func (c *Chain) BestBlockHash() *bc.Hash {
        c.state.cond.L.Lock()
        defer c.state.cond.L.Unlock()
        return c.state.hash
 }
 
-func (c *Chain) inMainchain(block *legacy.Block) bool {
+func (c *Chain) inMainchain(block *types.Block) bool {
        hash, ok := c.state.mainChain[block.Height]
        if !ok {
                return false
@@ -188,46 +165,72 @@ func (c *Chain) inMainchain(block *legacy.Block) bool {
        return *hash == block.Hash()
 }
 
-// TimestampMS returns the latest known block timestamp.
-func (c *Chain) TimestampMS() uint64 {
+// InMainChain checks wheather a block is in the main chain
+func (c *Chain) InMainChain(height uint64, hash bc.Hash) bool {
+       c.state.cond.L.Lock()
+       h, ok := c.state.mainChain[height]
+       c.state.cond.L.Unlock()
+       if !ok {
+               return false
+       }
+
+       return *h == hash
+}
+
+// Timestamp returns the latest known block timestamp.
+func (c *Chain) Timestamp() uint64 {
        c.state.cond.L.Lock()
        defer c.state.cond.L.Unlock()
        if c.state.block == nil {
                return 0
        }
-       return c.state.block.TimestampMS
+       return c.state.block.Timestamp
 }
 
-// State returns the most recent state available. It will not be current
-// unless the current process is the leader. Callers should examine the
-// returned block header's height if they need to verify the current state.
-func (c *Chain) State() (*legacy.Block, *state.Snapshot) {
+// BestBlock returns the chain tail block
+func (c *Chain) BestBlock() *types.Block {
        c.state.cond.L.Lock()
        defer c.state.cond.L.Unlock()
-       return c.state.block, c.state.snapshot
+       return c.state.block
 }
 
-// This function must be called with mu lock in above level
-func (c *Chain) setState(block *legacy.Block, s *state.Snapshot, m map[uint64]*bc.Hash) error {
-       if block.AssetsMerkleRoot != s.Tree.RootHash() {
-               return ErrBadStateRoot
+// GetUtxo try to find the utxo status in db
+func (c *Chain) GetUtxo(hash *bc.Hash) (*storage.UtxoEntry, error) {
+       return c.store.GetUtxo(hash)
+}
+
+// GetSeed return the seed for the given block
+func (c *Chain) GetSeed(height uint64, preBlock *bc.Hash) (*bc.Hash, error) {
+       if height == 0 {
+               return consensus.InitialSeed, nil
+       } else if height%consensus.SeedPerRetarget == 0 {
+               return preBlock, nil
        }
+       return c.store.GetSeed(preBlock)
+}
+
+// GetTransactionStatus return the transaction status of give block
+func (c *Chain) GetTransactionStatus(hash *bc.Hash) (*bc.TransactionStatus, error) {
+       return c.store.GetTransactionStatus(hash)
+}
 
+// GetTransactionsUtxo return all the utxos that related to the txs' inputs
+func (c *Chain) GetTransactionsUtxo(view *state.UtxoViewpoint, txs []*bc.Tx) error {
+       return c.store.GetTransactionsUtxo(view, txs)
+}
+
+// This function must be called with mu lock in above level
+func (c *Chain) setState(block *types.Block, view *state.UtxoViewpoint, m map[uint64]*bc.Hash) error {
        blockHash := block.Hash()
        c.state.block = block
        c.state.hash = &blockHash
-       c.state.snapshot = s
        for k, v := range m {
                c.state.mainChain[k] = v
        }
 
-       if err := c.store.SaveSnapshot(c.state.snapshot, &blockHash); err != nil {
-               return err
-       }
-       if err := c.store.SaveMainchain(c.state.mainChain, &blockHash); err != nil {
+       if err := c.store.SaveChainStatus(block, view, c.state.mainChain); err != nil {
                return err
        }
-       c.store.SaveStoreStatus(block.Height, &blockHash)
 
        c.state.cond.Broadcast()
        return nil