OSDN Git Service

chain.state using block.height instead of height (#82)
authorPaladz <yzhu101@uottawa.ca>
Thu, 2 Nov 2017 09:41:42 +0000 (04:41 -0500)
committerGuanghua Guo <1536310027@qq.com>
Thu, 2 Nov 2017 09:41:42 +0000 (17:41 +0800)
protocol/block.go
protocol/mempool.go
protocol/protocol.go

index c424e3f..a317c77 100644 (file)
@@ -23,14 +23,17 @@ var (
        ErrBadStateRoot = errors.New("invalid state merkle root")
 )
 
+// BlockExist check is a block in chain or orphan
 func (c *Chain) BlockExist(hash *bc.Hash) bool {
        return c.orphanManage.BlockExist(hash) || c.store.BlockExist(hash)
 }
 
+// GetBlockByHash return a block by given hash
 func (c *Chain) GetBlockByHash(hash *bc.Hash) (*legacy.Block, error) {
        return c.store.GetBlock(hash)
 }
 
+// GetBlockByHeight return a block by given height
 func (c *Chain) GetBlockByHeight(height uint64) (*legacy.Block, error) {
        c.state.cond.L.Lock()
        hash, ok := c.state.mainChain[height]
@@ -53,6 +56,7 @@ func (c *Chain) ValidateBlock(block, prev *legacy.Block) error {
        return nil
 }
 
+// ConnectBlock append block to end of chain
 func (c *Chain) ConnectBlock(block *legacy.Block) error {
        c.state.cond.L.Lock()
        defer c.state.cond.L.Unlock()
@@ -115,6 +119,7 @@ func (c *Chain) reorganizeChain(block *legacy.Block) error {
        return c.setState(block, newSnapshot, chainChanges)
 }
 
+// SaveBlock will validate and save block into storage
 func (c *Chain) SaveBlock(block *legacy.Block) error {
        preBlock, _ := c.GetBlockByHash(&block.PreviousBlockHash)
        if err := c.ValidateBlock(block, preBlock); err != nil {
@@ -159,6 +164,7 @@ func (c *Chain) findBestChainTail(block *legacy.Block) (bestBlock *legacy.Block)
        return
 }
 
+// ProcessBlock is the entry for handle block insert
 func (c *Chain) ProcessBlock(block *legacy.Block) (bool, error) {
        blockHash := block.Hash()
        if c.BlockExist(&blockHash) {
@@ -180,7 +186,7 @@ func (c *Chain) ProcessBlock(block *legacy.Block) (bool, error) {
                return false, c.connectBlock(bestBlock)
        }
 
-       if bestBlock.Height > c.state.height && bestBlock.Bits >= c.state.block.Bits {
+       if bestBlock.Height > c.state.block.Height && bestBlock.Bits >= c.state.block.Bits {
                defer c.state.cond.L.Unlock()
                return false, c.reorganizeChain(bestBlock)
        }
index 814da56..6337bf3 100644 (file)
@@ -12,11 +12,13 @@ import (
 )
 
 var (
-       maxCachedErrTxs        = 1000
-       maxNewTxChSize         = 1000
+       maxCachedErrTxs = 1000
+       maxNewTxChSize  = 1000
+       // ErrTransactionNotExist is the pre-defined error message
        ErrTransactionNotExist = errors.New("transaction are not existed in the mempool")
 )
 
+// TxDesc store tx and related info for mining strategy
 type TxDesc struct {
        Tx       *legacy.Tx
        Added    time.Time
@@ -26,6 +28,7 @@ type TxDesc struct {
        FeePerKB uint64
 }
 
+// TxPool is use for store the unconfirmed transaction
 type TxPool struct {
        lastUpdated int64
        mtx         sync.RWMutex
@@ -34,6 +37,7 @@ type TxPool struct {
        newTxCh     chan *legacy.Tx
 }
 
+// NewTxPool init a new TxPool
 func NewTxPool() *TxPool {
        return &TxPool{
                lastUpdated: time.Now().Unix(),
@@ -43,10 +47,12 @@ func NewTxPool() *TxPool {
        }
 }
 
+// GetNewTxCh return a unconfirmed transaction feed channel
 func (mp *TxPool) GetNewTxCh() chan *legacy.Tx {
        return mp.newTxCh
 }
 
+// AddTransaction add a verified transaction to pool
 func (mp *TxPool) AddTransaction(tx *legacy.Tx, height, fee uint64) *TxDesc {
        txD := &TxDesc{
                Tx:       tx,
@@ -67,6 +73,7 @@ func (mp *TxPool) AddTransaction(tx *legacy.Tx, height, fee uint64) *TxDesc {
        return txD
 }
 
+// AddErrCache add a failed transaction record to lru cache
 func (mp *TxPool) AddErrCache(txHash *bc.Hash, err error) {
        mp.mtx.Lock()
        defer mp.mtx.Unlock()
@@ -74,6 +81,7 @@ func (mp *TxPool) AddErrCache(txHash *bc.Hash, err error) {
        mp.errCache.Add(txHash, err)
 }
 
+// GetErrCache return the error of the transaction
 func (mp *TxPool) GetErrCache(txHash *bc.Hash) error {
        mp.mtx.Lock()
        defer mp.mtx.Unlock()
@@ -85,6 +93,7 @@ func (mp *TxPool) GetErrCache(txHash *bc.Hash) error {
        return v.(error)
 }
 
+// RemoveTransaction remove a transaction from the pool
 func (mp *TxPool) RemoveTransaction(txHash *bc.Hash) {
        mp.mtx.Lock()
        defer mp.mtx.Unlock()
@@ -95,6 +104,7 @@ func (mp *TxPool) RemoveTransaction(txHash *bc.Hash) {
        }
 }
 
+// GetTransaction return the TxDesc by hash
 func (mp *TxPool) GetTransaction(txHash *bc.Hash) (*TxDesc, error) {
        mp.mtx.RLock()
        defer mp.mtx.RUnlock()
@@ -106,6 +116,7 @@ func (mp *TxPool) GetTransaction(txHash *bc.Hash) (*TxDesc, error) {
        return nil, ErrTransactionNotExist
 }
 
+// GetTransactions return all the transactions in the pool
 func (mp *TxPool) GetTransactions() []*TxDesc {
        mp.mtx.RLock()
        defer mp.mtx.RUnlock()
@@ -119,6 +130,7 @@ func (mp *TxPool) GetTransactions() []*TxDesc {
        return txDs
 }
 
+// IsTransactionInPool check wheather a transaction in pool or not
 func (mp *TxPool) IsTransactionInPool(txHash *bc.Hash) bool {
        mp.mtx.RLock()
        defer mp.mtx.RUnlock()
@@ -129,6 +141,7 @@ func (mp *TxPool) IsTransactionInPool(txHash *bc.Hash) bool {
        return false
 }
 
+// IsTransactionInErrCache check wheather a transaction in errCache or not
 func (mp *TxPool) IsTransactionInErrCache(txHash *bc.Hash) bool {
        mp.mtx.RLock()
        defer mp.mtx.RUnlock()
@@ -137,10 +150,12 @@ func (mp *TxPool) IsTransactionInErrCache(txHash *bc.Hash) bool {
        return ok
 }
 
+// HaveTransaction IsTransactionInErrCache check is  transaction in errCache or pool
 func (mp *TxPool) HaveTransaction(txHash *bc.Hash) bool {
        return mp.IsTransactionInPool(txHash) || mp.IsTransactionInErrCache(txHash)
 }
 
+// Count return number of transcation in pool
 func (mp *TxPool) Count() int {
        mp.mtx.RLock()
        defer mp.mtx.RUnlock()
index f8c8449..9297018 100644 (file)
@@ -42,6 +42,7 @@ type Store interface {
        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
@@ -49,6 +50,7 @@ type OrphanManage struct {
        mtx        sync.RWMutex
 }
 
+// NewOrphanManage return a new orphan block
 func NewOrphanManage() *OrphanManage {
        return &OrphanManage{
                orphan:     make(map[bc.Hash]*legacy.Block),
@@ -56,6 +58,7 @@ func NewOrphanManage() *OrphanManage {
        }
 }
 
+// BlockExist check is the block in OrphanManage
 func (o *OrphanManage) BlockExist(hash *bc.Hash) bool {
        o.mtx.RLock()
        _, ok := o.orphan[*hash]
@@ -63,6 +66,7 @@ func (o *OrphanManage) BlockExist(hash *bc.Hash) bool {
        return ok
 }
 
+// Add will add the block to OrphanManage
 func (o *OrphanManage) Add(block *legacy.Block) {
        blockHash := block.Hash()
        o.mtx.Lock()
@@ -76,6 +80,7 @@ func (o *OrphanManage) Add(block *legacy.Block) {
        o.preOrphans[block.PreviousBlockHash] = append(o.preOrphans[block.PreviousBlockHash], &blockHash)
 }
 
+// Delete will delelte the block from OrphanManage
 func (o *OrphanManage) Delete(hash *bc.Hash) {
        o.mtx.Lock()
        defer o.mtx.Unlock()
@@ -99,6 +104,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) {
        o.mtx.RLock()
        block, ok := o.orphan[*hash]
@@ -120,7 +126,6 @@ type Chain struct {
        state struct {
                cond      sync.Cond
                block     *legacy.Block
-               height    uint64
                hash      *bc.Hash
                mainChain map[uint64]*bc.Hash
                snapshot  *state.Snapshot
@@ -138,9 +143,8 @@ func NewChain(initialBlockHash bc.Hash, store Store, txPool *TxPool) (*Chain, er
        }
        c.state.cond.L = new(sync.Mutex)
        storeStatus := store.GetStoreStatus()
-       c.state.height = storeStatus.Height
 
-       if c.state.height == 0 {
+       if storeStatus.Height == 0 {
                c.state.snapshot = state.Empty()
                c.state.mainChain = make(map[uint64]*bc.Hash)
                return c, nil
@@ -164,7 +168,7 @@ func NewChain(initialBlockHash bc.Hash, store Store, txPool *TxPool) (*Chain, er
 func (c *Chain) Height() uint64 {
        c.state.cond.L.Lock()
        defer c.state.cond.L.Unlock()
-       return c.state.height
+       return c.state.block.Height
 }
 
 func (c *Chain) BestBlockHash() *bc.Hash {
@@ -208,7 +212,6 @@ func (c *Chain) setState(block *legacy.Block, s *state.Snapshot, m map[uint64]*b
 
        blockHash := block.Hash()
        c.state.block = block
-       c.state.height = block.Height
        c.state.hash = &blockHash
        c.state.snapshot = s
        for k, v := range m {
@@ -260,7 +263,7 @@ func (c *Chain) BlockWaiter(height uint64) <-chan struct{} {
        go func() {
                c.state.cond.L.Lock()
                defer c.state.cond.L.Unlock()
-               for c.state.height < height {
+               for c.state.block.Height < height {
                        c.state.cond.Wait()
                }
                ch <- struct{}{}