diff options
author | Wei-Ning Huang <w@dexon.org> | 2018-11-19 10:54:12 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-03-12 12:19:09 +0800 |
commit | be1f06a3acfc85f90a2a9f6dec3235b5bf1f0099 (patch) | |
tree | d91d82d857c6b1cca9d76f0d599bc31ee337e556 /core | |
parent | 0e3b223de19e1975453206d8a0d326b575cb0afe (diff) | |
download | dexon-be1f06a3acfc85f90a2a9f6dec3235b5bf1f0099.tar dexon-be1f06a3acfc85f90a2a9f6dec3235b5bf1f0099.tar.gz dexon-be1f06a3acfc85f90a2a9f6dec3235b5bf1f0099.tar.bz2 dexon-be1f06a3acfc85f90a2a9f6dec3235b5bf1f0099.tar.lz dexon-be1f06a3acfc85f90a2a9f6dec3235b5bf1f0099.tar.xz dexon-be1f06a3acfc85f90a2a9f6dec3235b5bf1f0099.tar.zst dexon-be1f06a3acfc85f90a2a9f6dec3235b5bf1f0099.zip |
core: blockchain: fix concurrent map read and write (#34)
Diffstat (limited to 'core')
-rw-r--r-- | core/blockchain.go | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/core/blockchain.go b/core/blockchain.go index 482cd94f0..09b08aa9f 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -150,7 +150,7 @@ type BlockChain struct { addressNonce map[uint32]map[common.Address]uint64 addressCost map[uint32]map[common.Address]*big.Int addressCounter map[uint32]map[common.Address]uint64 - chainLastHeight map[uint32]uint64 + chainLastHeight sync.Map pendingBlockMu sync.RWMutex lastPendingHeight uint64 @@ -201,7 +201,6 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par addressNonce: make(map[uint32]map[common.Address]uint64), addressCost: make(map[uint32]map[common.Address]*big.Int), addressCounter: make(map[uint32]map[common.Address]uint64), - chainLastHeight: make(map[uint32]uint64), } bc.SetValidator(NewBlockValidator(chainConfig, bc, engine)) bc.SetProcessor(NewStateProcessor(chainConfig, bc, engine)) @@ -301,7 +300,7 @@ func (bc *BlockChain) AddConfirmedBlock(block *coreTypes.Block) error { block: block, txs: transactions, } - bc.chainLastHeight[chainID] = block.Position.Height + bc.chainLastHeight.Store(chainID, block.Position.Height) return nil } @@ -344,7 +343,11 @@ func (bc *BlockChain) GetCostInConfirmedBlocks(chainID uint32, address common.Ad } func (bc *BlockChain) GetChainLastConfirmedHeight(chainID uint32) uint64 { - return bc.chainLastHeight[chainID] + val, ok := bc.chainLastHeight.Load(chainID) + if !ok { + panic(fmt.Errorf("failed to get chain last height, chainID = %d", chainID)) + } + return val.(uint64) } // loadLastState loads the last known chain state from the database. This method |