From 108d01f730a2667491395a8a5ce5efb06dc4a89e Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Mon, 2 Dec 2019 15:33:21 -0800 Subject: [PATCH] Fix cross link verification and change batch size to 50 --- core/blockchain.go | 21 ++++++++++----------- node/node_cross_shard.go | 4 ++-- node/node_handler.go | 15 +++++++-------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 2fafe1ced..832bbc317 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1294,7 +1294,7 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types. if err := bc.WriteCrossLinks(types.CrossLinks{crossLink}); err == nil { utils.Logger().Info().Uint64("blockNum", crossLink.BlockNum()).Uint32("shardID", crossLink.ShardID()).Msg("[insertChain/crosslinks] Cross Link Added to Beaconchain") } - bc.LastContinuousCrossLink(crossLink.ShardID(), crossLink) + bc.LastContinuousCrossLink(crossLink) } //clean/update local database cache after crosslink inserted into blockchain num, err := bc.DeleteCommittedFromPendingCrossLinks(*crossLinks) @@ -2189,19 +2189,18 @@ func (bc *BlockChain) ReadCrossLink(shardID uint32, blockNum uint64) (*types.Cro return crossLink, err } -// WriteShardLastCrossLink saves the last crosslink of a shard -func (bc *BlockChain) WriteShardLastCrossLink(shardID uint32, cl types.CrossLink) error { - return rawdb.WriteShardLastCrossLink(bc.db, cl.ShardID(), cl.Serialize()) -} - // LastContinuousCrossLink saves the last crosslink of a shard // This function will update the latest crosslink in the sense that // any previous block's crosslink is received up to this point // there is no missing hole between genesis to this crosslink of given shardID -func (bc *BlockChain) LastContinuousCrossLink(shardID uint32, cl types.CrossLink) error { - cl0, err := bc.ReadShardLastCrossLink(shardID) +func (bc *BlockChain) LastContinuousCrossLink(cl types.CrossLink) error { + if !bc.Config().IsCrossLink(cl.Epoch()) { + return errors.New("Trying to write last continuous cross link with epoch before cross link starting epoch") + } + + cl0, err := bc.ReadShardLastCrossLink(cl.ShardID()) if cl0 == nil { - bc.WriteShardLastCrossLink(shardID, cl) + rawdb.WriteShardLastCrossLink(bc.db, cl.ShardID(), cl.Serialize()) return nil } if err != nil { @@ -2209,7 +2208,7 @@ func (bc *BlockChain) LastContinuousCrossLink(shardID uint32, cl types.CrossLink } newCheckpoint := uint64(0) for i := cl0.BlockNum() + 1; i < cl.BlockNum(); i++ { - tmp, err := bc.ReadCrossLink(shardID, i) + tmp, err := bc.ReadCrossLink(cl.ShardID(), i) if err != nil || tmp == nil { newCheckpoint = i - 1 break @@ -2221,7 +2220,7 @@ func (bc *BlockChain) LastContinuousCrossLink(shardID uint32, cl types.CrossLink if newCheckpoint > 0 { utils.Logger().Debug().Msgf("LastContinuousCrossLink: latest checkpoint blockNum %d", newCheckpoint) - cln, err := bc.ReadCrossLink(shardID, newCheckpoint) + cln, err := bc.ReadCrossLink(cl.ShardID(), newCheckpoint) if err != nil { return err } diff --git a/node/node_cross_shard.go b/node/node_cross_shard.go index 71a41243d..1d946f6c7 100644 --- a/node/node_cross_shard.go +++ b/node/node_cross_shard.go @@ -230,8 +230,8 @@ func (node *Node) VerifyCrossLink(cl types.CrossLink) error { return ctxerror.New("CrossLink BlockNumber should greater than 1") } - if node.Blockchain().Config().IsCrossLink(cl.Epoch()) { - return ctxerror.New("CrossLink Epoch should >= crosslink epoch", "crossLinkEpoch", node.Blockchain().Config().CrossLinkEpoch) + if !node.Blockchain().Config().IsCrossLink(cl.Epoch()) { + return ctxerror.New("CrossLink Epoch should >= cross link starting epoch", "crossLinkEpoch", cl.Epoch(), "cross_link_starting_eoch", node.Blockchain().Config().CrossLinkEpoch) } // Verify signature of the new cross link header diff --git a/node/node_handler.go b/node/node_handler.go index a25c4ca4c..f061445a4 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -26,8 +26,7 @@ import ( ) const ( - consensusTimeout = 30 * time.Second - crossLinkBatchSize = 7 + crossLinkBatchSize = 50 ) // receiveGroupMessage use libp2p pubsub mechanism to receive broadcast messages @@ -218,23 +217,23 @@ func (node *Node) BroadcastCrossLink(newBlock *types.Block) { if err != nil { utils.Logger().Debug().Err(err).Msg("[BroadcastCrossLink] ReadShardLastCrossLink Failed") header := node.Blockchain().GetHeaderByNumber(newBlock.NumberU64() - 2) - if header != nil { + if header != nil && node.Blockchain().Config().IsCrossLink(header.Epoch()) { headers = append(headers, header) } header = node.Blockchain().GetHeaderByNumber(newBlock.NumberU64() - 1) - if header != nil { + if header != nil && node.Blockchain().Config().IsCrossLink(header.Epoch()) { headers = append(headers, header) } headers = append(headers, newBlock.Header()) } else { latestBlockNum = lastLink.BlockNum() for blockNum := latestBlockNum + 1; blockNum <= newBlock.NumberU64(); blockNum++ { - if blockNum > latestBlockNum+crossLinkBatchSize { - break - } header := node.Blockchain().GetHeaderByNumber(blockNum) - if header != nil { + if header != nil && node.Blockchain().Config().IsCrossLink(header.Epoch()) { headers = append(headers, header) + if len(headers) == crossLinkBatchSize { + break + } } } }