Update EpochLastBlock to use big int

pull/1493/head
Rongjian Lan 5 years ago
parent 66d6ffcb7c
commit 54111b60d7
  1. 8
      core/blockchain.go
  2. 13
      node/node_cross_shard.go
  3. 8
      node/node_handler.go
  4. 3
      node/worker/worker.go

@ -244,11 +244,11 @@ func IsEpochBlock(block *types.Block) bool {
}
// EpochFirstBlock returns the block number of the first block of an epoch.
func EpochFirstBlock(epoch uint64) uint64 {
if epoch == 0 {
return 0
func EpochFirstBlock(epoch *big.Int) *big.Int {
if epoch.Cmp(big.NewInt(0)) == 0 {
return big.NewInt(0)
}
return ShardingSchedule.EpochLastBlock(epoch-1) + 1
return big.NewInt(int64(ShardingSchedule.EpochLastBlock(epoch.Uint64()-1) + 1))
}
// IsEpochLastBlock returns whether this block is the last block of an epoch.

@ -3,7 +3,6 @@ package node
import (
"encoding/binary"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
@ -37,9 +36,9 @@ func (node *Node) ProcessHeaderMessage(msgPayload []byte) {
node.pendingCrossLinks = []*block.Header{}
node.pendingClMutex.Unlock()
firstCrossLinkBlock := core.EpochFirstBlock(node.Blockchain().Config().CrossLinkEpoch.Uint64())
firstCrossLinkBlock := core.EpochFirstBlock(node.Blockchain().Config().CrossLinkEpoch)
for _, header := range headers {
if header.Number.Uint64() >= firstCrossLinkBlock {
if header.Number.Cmp(firstCrossLinkBlock) >= 0 {
// Only process cross link starting from FirstCrossLinkBlock
utils.Logger().Debug().Msgf("[ProcessHeaderMessage] Add Pending CrossLink, shardID %d, blockNum %d", header.ShardID, header.Number)
crossLinkHeadersToProcess = append(crossLinkHeadersToProcess, header)
@ -61,7 +60,7 @@ func (node *Node) ProcessHeaderMessage(msgPayload []byte) {
continue
}
if header.Number.Uint64() > firstCrossLinkBlock { // Directly trust the first cross-link
if header.Number.Cmp(firstCrossLinkBlock) > 0 { // Directly trust the first cross-link
// Sanity check on the previous link with the new link
previousLink, err := node.Blockchain().ReadCrossLink(header.ShardID, header.Number.Uint64()-1, false)
if err != nil {
@ -223,13 +222,13 @@ func (node *Node) ProposeCrossLinkDataForBeaconchain() (types.CrossLinks, error)
shardCrossLinks := make([]types.CrossLinks, numShards)
firstCrossLinkBlock := core.EpochFirstBlock(node.Blockchain().Config().CrossLinkEpoch.Uint64())
firstCrossLinkBlock := core.EpochFirstBlock(node.Blockchain().Config().CrossLinkEpoch)
for i := 0; i < int(numShards); i++ {
curShardID := uint32(i)
lastLink, err := node.Blockchain().ReadShardLastCrossLink(curShardID)
lastLinkblockNum := big.NewInt(int64(firstCrossLinkBlock))
lastLinkblockNum := firstCrossLinkBlock
blockNumoffset := 0
if err == nil && lastLink != nil {
blockNumoffset = 1
@ -242,7 +241,7 @@ func (node *Node) ProposeCrossLinkDataForBeaconchain() (types.CrossLinks, error)
break
}
if link.BlockNum().Uint64() > firstCrossLinkBlock {
if link.BlockNum().Cmp(firstCrossLinkBlock) > 0 {
if lastLink == nil {
utils.Logger().Error().
Err(err).

@ -405,12 +405,12 @@ func (node *Node) VerifyBlockCrossLinks(block *types.Block) error {
)
}
firstCrossLinkBlock := core.EpochFirstBlock(node.Blockchain().Config().CrossLinkEpoch.Uint64())
firstCrossLinkBlock := core.EpochFirstBlock(node.Blockchain().Config().CrossLinkEpoch)
for i, crossLink := range *crossLinks {
lastLink := &types.CrossLink{}
if i == 0 {
if crossLink.BlockNum().Uint64() > firstCrossLinkBlock {
if crossLink.BlockNum().Cmp(firstCrossLinkBlock) > 0 {
lastLink, err = node.Blockchain().ReadShardLastCrossLink(crossLink.ShardID())
if err != nil {
return ctxerror.New("[CrossLinkVerification] no last cross link found 1",
@ -421,7 +421,7 @@ func (node *Node) VerifyBlockCrossLinks(block *types.Block) error {
}
} else {
if (*crossLinks)[i-1].Header().ShardID != crossLink.Header().ShardID {
if crossLink.BlockNum().Uint64() > firstCrossLinkBlock {
if crossLink.BlockNum().Cmp(firstCrossLinkBlock) > 0 {
lastLink, err = node.Blockchain().ReadShardLastCrossLink(crossLink.ShardID())
if err != nil {
return ctxerror.New("[CrossLinkVerification] no last cross link found 2",
@ -435,7 +435,7 @@ func (node *Node) VerifyBlockCrossLinks(block *types.Block) error {
}
}
if crossLink.BlockNum().Uint64() > firstCrossLinkBlock { // TODO: verify genesis block
if crossLink.BlockNum().Cmp(firstCrossLinkBlock) > 0 { // TODO: verify genesis block
err = node.VerifyCrosslinkHeader(lastLink.Header(), crossLink.Header())
if err != nil {
return ctxerror.New("cannot ValidateNewBlock",

@ -2,10 +2,11 @@ package worker
import (
"fmt"
"github.com/harmony-one/harmony/shard"
"math/big"
"time"
"github.com/harmony-one/harmony/shard"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/common"

Loading…
Cancel
Save