Fix last continuous crosslink logic (#2316)

* Fix last continuous crosslink logic

* fix lint

* Add comment
pull/2326/head
Rongjian Lan 5 years ago committed by GitHub
parent c4d744e376
commit 715744f3c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 37
      core/blockchain.go
  2. 10
      core/offchain.go
  3. 10
      internal/params/config.go

@ -1866,38 +1866,29 @@ func (bc *BlockChain) ReadCrossLink(shardID uint32, blockNum uint64) (*types.Cro
// 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(batch rawdb.DatabaseWriter, cl types.CrossLink) error {
if !bc.Config().IsCrossLink(cl.Epoch()) {
func (bc *BlockChain) LastContinuousCrossLink(batch rawdb.DatabaseWriter, shardID uint32) error {
if !bc.Config().IsCrossLink(bc.CurrentBlock().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 {
rawdb.WriteShardLastCrossLink(batch, cl.ShardID(), cl.Serialize())
return nil
}
if err != nil {
oldLink, err := bc.ReadShardLastCrossLink(shardID)
if oldLink == nil || err != nil {
return err
}
newCheckpoint := uint64(0)
for i := cl0.BlockNum() + 1; i < cl.BlockNum(); i++ {
tmp, err := bc.ReadCrossLink(cl.ShardID(), i)
if err != nil || tmp == nil {
newCheckpoint = i - 1
newLink := oldLink
// Starting from last checkpoint, keeping reading immediate next crosslink until there is a gap
for i := oldLink.BlockNum() + 1; ; i++ {
tmp, err := bc.ReadCrossLink(shardID, i)
if err == nil && tmp != nil && tmp.BlockNum() == i {
newLink = tmp
} else {
break
}
if i == cl.BlockNum()-1 {
newCheckpoint = cl.BlockNum()
}
}
if newCheckpoint > 0 {
utils.Logger().Debug().Msgf("LastContinuousCrossLink: latest checkpoint blockNum %d", newCheckpoint)
cln, err := bc.ReadCrossLink(cl.ShardID(), newCheckpoint)
if err != nil {
return err
}
return rawdb.WriteShardLastCrossLink(batch, cln.ShardID(), cln.Serialize())
if newLink.BlockNum() > oldLink.BlockNum() {
utils.Logger().Debug().Msgf("LastContinuousCrossLink: latest checkpoint blockNum %d", newLink.BlockNum())
return rawdb.WriteShardLastCrossLink(batch, shardID, newLink.Serialize())
}
return nil
}

@ -164,13 +164,21 @@ func (bc *BlockChain) CommitOffChainData(
if err := bc.WriteCrossLinks(batch, 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(batch, crossLink)
cl0, _ := bc.ReadShardLastCrossLink(crossLink.ShardID())
if cl0 == nil {
rawdb.WriteShardLastCrossLink(batch, crossLink.ShardID(), crossLink.Serialize())
}
}
//clean/update local database cache after crosslink inserted into blockchain
num, err := bc.DeleteCommittedFromPendingCrossLinks(*crossLinks)
utils.Logger().Debug().Msgf("DeleteCommittedFromPendingCrossLinks, crosslinks in header %d, pending crosslinks: %d, error: %+v", len(*crossLinks), num, err)
}
// Roll up latest crosslinks
for i := uint32(0); i < shard.Schedule.InstanceForEpoch(epoch).NumShards(); i++ {
bc.LastContinuousCrossLink(batch, i)
}
if bc.CurrentHeader().ShardID() == shard.BeaconChainShardID {
if bc.chainConfig.IsStaking(block.Epoch()) {

@ -37,7 +37,7 @@ var (
TestnetChainConfig = &ChainConfig{
ChainID: TestnetChainID,
CrossTxEpoch: big.NewInt(0),
CrossLinkEpoch: big.NewInt(3),
CrossLinkEpoch: big.NewInt(4),
StakingEpoch: big.NewInt(4),
PreStakingEpoch: big.NewInt(2),
EIP155Epoch: big.NewInt(0),
@ -50,7 +50,7 @@ var (
PangaeaChainConfig = &ChainConfig{
ChainID: PangaeaChainID,
CrossTxEpoch: big.NewInt(0),
CrossLinkEpoch: big.NewInt(2),
CrossLinkEpoch: big.NewInt(3),
StakingEpoch: big.NewInt(3),
PreStakingEpoch: big.NewInt(0),
EIP155Epoch: big.NewInt(0),
@ -62,9 +62,9 @@ var (
LocalnetChainConfig = &ChainConfig{
ChainID: TestnetChainID,
CrossTxEpoch: big.NewInt(0),
CrossLinkEpoch: big.NewInt(3),
StakingEpoch: big.NewInt(4),
PreStakingEpoch: big.NewInt(2),
CrossLinkEpoch: big.NewInt(2),
StakingEpoch: big.NewInt(2),
PreStakingEpoch: big.NewInt(0),
EIP155Epoch: big.NewInt(0),
S3Epoch: big.NewInt(0),
ReceiptLogEpoch: big.NewInt(0),

Loading…
Cancel
Save