aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWei-Ning Huang <w@dexon.org>2018-11-19 10:54:12 +0800
committerWei-Ning Huang <w@dexon.org>2018-12-19 20:54:27 +0800
commitd41f3b1a9834e7911603b3ec3ddf0714d99ceca9 (patch)
tree18c8fc9a17a418d33c8fe9abe62afd914a50a0a6
parentb04996ff14079c426a0540c38a3457de1fe36394 (diff)
downloaddexon-d41f3b1a9834e7911603b3ec3ddf0714d99ceca9.tar
dexon-d41f3b1a9834e7911603b3ec3ddf0714d99ceca9.tar.gz
dexon-d41f3b1a9834e7911603b3ec3ddf0714d99ceca9.tar.bz2
dexon-d41f3b1a9834e7911603b3ec3ddf0714d99ceca9.tar.lz
dexon-d41f3b1a9834e7911603b3ec3ddf0714d99ceca9.tar.xz
dexon-d41f3b1a9834e7911603b3ec3ddf0714d99ceca9.tar.zst
dexon-d41f3b1a9834e7911603b3ec3ddf0714d99ceca9.zip
core: blockchain: fix concurrent map read and write (#34)
-rw-r--r--core/blockchain.go11
1 files changed, 7 insertions, 4 deletions
diff --git a/core/blockchain.go b/core/blockchain.go
index 2189cb6dd..b6d85b7d7 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