aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSonic <sonic@dexon.org>2019-01-03 17:18:50 +0800
committerWei-Ning Huang <w@dexon.org>2019-01-14 15:26:26 +0800
commitdc18d1eabfc90dfbb6aef3ae999c05238461ccf3 (patch)
treed372e82440cc01ddf8c0c60e2c7104c0cca36446
parente6442ffcabb4c5cb2f7bc97f9dff7c95ef1527c3 (diff)
downloaddexon-dc18d1eabfc90dfbb6aef3ae999c05238461ccf3.tar.gz
dexon-dc18d1eabfc90dfbb6aef3ae999c05238461ccf3.tar.zst
dexon-dc18d1eabfc90dfbb6aef3ae999c05238461ccf3.zip
core, indexer, dex: fix DexconApp block deliver after synced (#122)
When starts a bp node to sync with the network, bc.chainLastHeight map may not be initialized yet. Just return error if we can not get chain last height when preparing payload and verify block.
-rw-r--r--core/blockchain.go11
-rw-r--r--dex/app.go18
-rw-r--r--indexer/blockchain.go2
3 files changed, 16 insertions, 15 deletions
diff --git a/core/blockchain.go b/core/blockchain.go
index 0f190a21b..0bb6b270b 100644
--- a/core/blockchain.go
+++ b/core/blockchain.go
@@ -395,12 +395,13 @@ func (bc *BlockChain) GetCostInConfirmedBlocks(chainID uint32, address common.Ad
return cost, exist
}
-func (bc *BlockChain) GetChainLastConfirmedHeight(chainID uint32) uint64 {
- val, ok := bc.chainLastHeight.Load(chainID)
- if !ok {
- panic(fmt.Errorf("failed to get chain last height, chainID = %d", chainID))
+func (bc *BlockChain) GetChainLastConfirmedHeight(chainID uint32) (uint64, bool) {
+ val := uint64(0)
+ v, ok := bc.chainLastHeight.Load(chainID)
+ if ok {
+ val = v.(uint64)
}
- return val.(uint64)
+ return val, ok
}
func (bc *BlockChain) GetAddressInfo(chainID uint32, address common.Address) (
diff --git a/dex/app.go b/dex/app.go
index d04b2afd6..c52a6c79b 100644
--- a/dex/app.go
+++ b/dex/app.go
@@ -177,11 +177,11 @@ func (d *DexconApp) preparePayload(ctx context.Context, position coreTypes.Posit
if position.Height != 0 {
// Check if chain block height is strictly increamental.
- chainLastHeight := d.blockchain.GetChainLastConfirmedHeight(position.ChainID)
- if chainLastHeight != position.Height-1 {
- log.Error("Check confirmed block height fail",
- "chain", position.ChainID, "height", position.Height-1, "cache height", chainLastHeight)
- return nil, fmt.Errorf("check confirmed block height fail")
+ chainLastHeight, ok := d.blockchain.GetChainLastConfirmedHeight(position.ChainID)
+ if !ok || chainLastHeight != position.Height-1 {
+ log.Debug("Previous confirmed block not exists", "current pos", position.String(),
+ "prev height", chainLastHeight, "ok", ok)
+ return nil, fmt.Errorf("previous block not exists")
}
}
@@ -337,10 +337,10 @@ func (d *DexconApp) VerifyBlock(block *coreTypes.Block) coreTypes.BlockVerifySta
if block.Position.Height != 0 {
// Check if target block is the next height to be verified, we can only
// verify the next block in a given chain.
- chainLastHeight := d.blockchain.GetChainLastConfirmedHeight(block.Position.ChainID)
- if chainLastHeight != block.Position.Height-1 {
- log.Error("Check confirmed block height fail", "chain", block.Position.ChainID,
- "height", block.Position.Height-1, "cache height", chainLastHeight)
+ chainLastHeight, ok := d.blockchain.GetChainLastConfirmedHeight(block.Position.ChainID)
+ if !ok || chainLastHeight != block.Position.Height-1 {
+ log.Debug("Previous confirmed block not exists", "current pos", block.Position.String(),
+ "prev height", chainLastHeight, "ok", ok)
return coreTypes.VerifyRetryLater
}
}
diff --git a/indexer/blockchain.go b/indexer/blockchain.go
index 421535184..f41c05a15 100644
--- a/indexer/blockchain.go
+++ b/indexer/blockchain.go
@@ -42,7 +42,7 @@ type ReadOnlyBlockChain interface {
GetBlocksFromHash(common.Hash, int) (blocks []*types.Block)
GetBody(common.Hash) *types.Body
GetBodyRLP(common.Hash) rlp.RawValue
- GetChainLastConfirmedHeight(uint32) uint64
+ GetChainLastConfirmedHeight(uint32) (uint64, bool)
GetConfirmedBlockByHash(uint32, coreCommon.Hash) (*coreTypes.Block, types.Transactions)
GetCostInConfirmedBlocks(uint32, common.Address) (*big.Int, bool)
GetGovStateByHash(common.Hash) (*types.GovState, error)