diff --git a/Makefile b/Makefile index ee1d40faa0..c11e00cc86 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,7 @@ trace-pointer: bash ./scripts/go_executable_build.sh -t debug: + rm -rf .dht-127.0.0.1* bash ./test/debug.sh debug-kill: @@ -167,3 +168,6 @@ docker: travis_go_checker: bash ./scripts/travis_go_checker.sh + +travis_rpc_checker: + bash ./scripts/travis_rpc_checker.sh diff --git a/consensus/consensus_service.go b/consensus/consensus_service.go index 3ebfa5e307..4fc61d8155 100644 --- a/consensus/consensus_service.go +++ b/consensus/consensus_service.go @@ -95,7 +95,7 @@ func (consensus *Consensus) updatePublicKeys(pubKeys, allowlist []bls_cosi.Publi if len(allKeys) != 0 { consensus.LeaderPubKey = &allKeys[0] consensus.getLogger().Info(). - Str("info", consensus.LeaderPubKey.Bytes.Hex()).Msg("My Leader") + Str("info", consensus.LeaderPubKey.Bytes.Hex()).Msg("Setting leader as first validator, because provided new keys") } else { consensus.getLogger().Error(). Msg("[UpdatePublicKeys] Participants is empty") diff --git a/core/preimages.go b/core/preimages.go index 78fd36899e..ac57290b42 100644 --- a/core/preimages.go +++ b/core/preimages.go @@ -7,6 +7,8 @@ import ( "os" "strconv" + "github.com/ethereum/go-ethereum/crypto" + ethCommon "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/harmony-one/harmony/api/service/prometheus" @@ -142,26 +144,28 @@ func ExportPreimages(chain BlockChain, path string) error { dbReader := chain.ChainDb() for accountIterator.Next(true) { // the leaf nodes of the MPT represent accounts - if accountIterator.Leaf() { - // the leaf key is the hashed address - hashed := accountIterator.LeafKey() - asHash := ethCommon.BytesToHash(hashed) - // obtain the corresponding address - preimage := rawdb.ReadPreimage( - dbReader, asHash, - ) - if len(preimage) == 0 { - utils.Logger().Warn(). - Msgf("Address not found for %x", asHash) - continue - } - address := ethCommon.BytesToAddress(preimage) - // key value format, so hash of value is first - csvWriter.Write([]string{ - fmt.Sprintf("%x", asHash.Bytes()), - fmt.Sprintf("%x", address.Bytes()), - }) + if !accountIterator.Leaf() { + continue } + // the leaf key is the hashed address + hashed := accountIterator.LeafKey() + asHash := ethCommon.BytesToHash(hashed) + // obtain the corresponding address + preimage := rawdb.ReadPreimage( + dbReader, asHash, + ) + if len(preimage) == 0 { + utils.Logger().Warn(). + Msgf("Address not found for %x", asHash) + continue + } + address := ethCommon.BytesToAddress(preimage) + // key value format, so hash of value is first + csvWriter.Write([]string{ + fmt.Sprintf("%x", asHash.Bytes()), + fmt.Sprintf("%x", address.Bytes()), + }) + } // lastly, write the block number csvWriter.Write( diff --git a/core/state_processor.go b/core/state_processor.go index c1a9050d2d..a0abf31f69 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -141,11 +141,11 @@ func (p *StateProcessor) Process( gp, header, statedb, p.bc, p.config, ) if err != nil { - if err == ErrNoMigrationPossible { + if errors.Is(err, ErrNoMigrationPossible) { // ran out of accounts processTxsAndStxs = false } - if err != ErrNoMigrationRequired { + if !errors.Is(err, ErrNoMigrationRequired) { return nil, nil, nil, nil, 0, nil, statedb, err } } else { @@ -538,7 +538,7 @@ func MayBalanceMigration( ) (*types.CXReceipt, error) { isMainnet := nodeconfig.GetDefaultConfig().GetNetworkType() == nodeconfig.Mainnet if isMainnet { - if config.IsEpochBeforeHIP30(header.Epoch()) { + if config.IsOneEpochBeforeHIP30(header.Epoch()) { nxtShards := shard.Schedule.InstanceForEpoch( new(big.Int).Add(header.Epoch(), common.Big1), ).NumShards() @@ -574,24 +574,30 @@ func MayBalanceMigration( isDevnet := nodeconfig.GetDefaultConfig().GetNetworkType() == nodeconfig.Devnet isLocalnet := nodeconfig.GetDefaultConfig().GetNetworkType() == nodeconfig.Localnet if isDevnet || isLocalnet { - if config.IsEpochBeforeHIP30(header.Epoch()) { + if config.IsOneEpochBeforeHIP30(header.Epoch()) { if myShard := chain.ShardID(); myShard != shard.BeaconChainShardID { - parentRoot := chain.GetBlockByHash( + parent := chain.GetBlockByHash( header.ParentHash(), - ).Root() // for examining MPT at this root, should exist + ) + if parent == nil { + return nil, errors.Wrap(ErrNoMigrationPossible, "parent is nil") + } + parentRoot := parent.Root() + // for examining MPT at this root, should exist cx, err := generateOneMigrationMessage( db, parentRoot, header.NumberU64(), myShard, shard.BeaconChainShardID, // dstShard ) if err != nil { - return nil, err + return nil, errors.Wrap(err, "generateOneMigrationMessage") } if cx != nil { gasPool.SubGas(params.TxGasXShard) return cx, nil } - return nil, ErrNoMigrationPossible + //return nil, errors.Wrap(ErrNoMigrationPossible, "MayBalanceMigration: cx is nil") + return nil, nil } } } @@ -635,11 +641,13 @@ func generateOneMigrationMessage( key := accountIterator.LeafKey() preimage := rawdb.ReadPreimage(db, common.BytesToHash(key)) if len(preimage) == 0 { - return nil, errors.New( + e := errors.New( fmt.Sprintf( "cannot find preimage for %x", key, ), ) + fmt.Println(e) + continue } address := common.BytesToAddress(preimage) // skip blank address diff --git a/internal/configs/sharding/localnet.go b/internal/configs/sharding/localnet.go index fb6050b1de..5c75967da0 100644 --- a/internal/configs/sharding/localnet.go +++ b/internal/configs/sharding/localnet.go @@ -31,8 +31,8 @@ const ( localnetV1Epoch = 1 localnetEpochBlock1 = 5 - localnetBlocksPerEpoch = 64 - localnetBlocksPerEpochV2 = 64 + localnetBlocksPerEpoch = 8 + localnetBlocksPerEpochV2 = 8 localnetVdfDifficulty = 5000 // This takes about 10s to finish the vdf ) diff --git a/internal/params/config.go b/internal/params/config.go index 5d2053ce82..1eade113a2 100644 --- a/internal/params/config.go +++ b/internal/params/config.go @@ -797,8 +797,9 @@ func (c *ChainConfig) IsHIP30(epoch *big.Int) bool { // During this epoch, shards 2 and 3 will start sending // their balances over to shard 0 or 1. -func (c *ChainConfig) IsEpochBeforeHIP30(epoch *big.Int) bool { - return isForked(new(big.Int).Sub(c.HIP30Epoch, common.Big1), epoch) +func (c *ChainConfig) IsOneEpochBeforeHIP30(epoch *big.Int) bool { + return epoch.Sub(c.HIP30Epoch, epoch).Cmp(common.Big1) == 0 + //return isForked(new(big.Int).Sub(c.HIP30Epoch, common.Big1), epoch) } // UpdateEthChainIDByShard update the ethChainID based on shard ID. diff --git a/internal/params/config_test.go b/internal/params/config_test.go new file mode 100644 index 0000000000..c650437a29 --- /dev/null +++ b/internal/params/config_test.go @@ -0,0 +1,17 @@ +package params + +import ( + "math/big" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestIsOneEpochBeforeHIP30(t *testing.T) { + c := ChainConfig{ + HIP30Epoch: big.NewInt(3), + } + + require.True(t, c.IsOneEpochBeforeHIP30(big.NewInt(2))) + require.False(t, c.IsOneEpochBeforeHIP30(big.NewInt(3))) +} diff --git a/node/node.go b/node/node.go index 5d9e77763a..a758b5c237 100644 --- a/node/node.go +++ b/node/node.go @@ -261,7 +261,7 @@ func addPendingTransactions(registry *registry.Registry, newTxs types.Transactio poolTxs = types.PoolTransactions{} epoch = bc.CurrentHeader().Epoch() acceptCx = bc.Config().AcceptsCrossTx(epoch) - isBeforeHIP30 = bc.Config().IsEpochBeforeHIP30(epoch) + isBeforeHIP30 = bc.Config().IsOneEpochBeforeHIP30(epoch) nxtShards = shard.Schedule.InstanceForEpoch(new(big.Int).Add(epoch, common.Big1)).NumShards() ) for _, tx := range newTxs { @@ -497,7 +497,8 @@ func (node *Node) validateNodeMessage(ctx context.Context, payload []byte) ( if err := rlp.DecodeBytes(blocksPayload, &blocks); err != nil { return nil, 0, errors.Wrap(err, "block decode error") } - curBeaconHeight := node.Beaconchain().CurrentBlock().NumberU64() + curBeaconBlock := node.EpochChain().CurrentBlock() + curBeaconHeight := curBeaconBlock.NumberU64() for _, block := range blocks { // Ban blocks number that is smaller than tolerance if block.NumberU64()+beaconBlockHeightTolerance <= curBeaconHeight { @@ -507,7 +508,7 @@ func (node *Node) validateNodeMessage(ctx context.Context, payload []byte) ( } else if block.NumberU64()-beaconBlockHeightTolerance > curBeaconHeight { utils.Logger().Debug().Uint64("receivedNum", block.NumberU64()). Uint64("currentNum", curBeaconHeight).Msg("beacon block sync message rejected") - return nil, 0, errors.New("beacon block height too much higher than current height beyond tolerance") + return nil, 0, errors.Errorf("beacon block height too much higher than current height beyond tolerance, block %d, current %d, epoch %d , current %d", block.NumberU64(), curBeaconHeight, block.Epoch().Uint64(), curBeaconBlock.Epoch().Uint64()) } else if block.NumberU64() <= curBeaconHeight { utils.Logger().Debug().Uint64("receivedNum", block.NumberU64()). Uint64("currentNum", curBeaconHeight).Msg("beacon block sync message ignored") diff --git a/node/node_newblock.go b/node/node_newblock.go index 2c4138cd2f..19a9c9b231 100644 --- a/node/node_newblock.go +++ b/node/node_newblock.go @@ -157,7 +157,7 @@ func (node *Node) ProposeNewBlock(commitSigs chan []byte) (*types.Block, error) } } - if !shard.Schedule.IsLastBlock(header.Number().Uint64()) { + if !shard.Schedule.IsLastBlock(header.Number().Uint64()) || node.Consensus.ShardID != 0 { // Prepare normal and staking transactions retrieved from transaction pool utils.AnalysisStart("proposeNewBlockChooseFromTxnPool") @@ -255,7 +255,7 @@ func (node *Node) ProposeNewBlock(commitSigs chan []byte) (*types.Block, error) len(crossLinksToPropose), len(allPending), ) } else { - utils.Logger().Error().Err(err).Msgf( + utils.Logger().Warn().Err(err).Msgf( "[ProposeNewBlock] Unable to Read PendingCrossLinks, number of crosslinks: %d", len(allPending), ) @@ -291,7 +291,7 @@ func (node *Node) ProposeNewBlock(commitSigs chan []byte) (*types.Block, error) utils.Logger().Error().Err(err).Msg("[ProposeNewBlock] Failed finalizing the new block") return nil, err } - utils.Logger().Info().Msg("[ProposeNewBlock] verifying the new block header") + utils.Logger().Info().Msgf("[ProposeNewBlock] verifying the new block: shard %d, number %d, epoch %d", finalizedBlock.ShardID(), finalizedBlock.NumberU64(), finalizedBlock.Epoch().Uint64()) err = node.Blockchain().Validator().ValidateHeader(finalizedBlock, true) if err != nil { diff --git a/node/worker/worker.go b/node/worker/worker.go index 77c3361bac..5336a27a75 100644 --- a/node/worker/worker.go +++ b/node/worker/worker.go @@ -160,17 +160,18 @@ func (w *Worker) CommitTransactions( // but the balance will immediately be returned to shard 1 cx, err := core.MayBalanceMigration( w.current.gasPool, - w.beacon.CurrentHeader(), + w.GetCurrentHeader(), w.current.state, w.chain, w.chain.Config(), ) if err != nil { - if err == core.ErrNoMigrationPossible { + if errors.Is(err, core.ErrNoMigrationPossible) { // means we do not accept transactions from the network return nil } - if err != core.ErrNoMigrationRequired { + if !errors.Is(err, core.ErrNoMigrationRequired) { + // this shard not migrating => ErrNoMigrationRequired // any other error means exit this block return err @@ -413,16 +414,6 @@ func (w *Worker) GetCurrentReceipts() []*types.Receipt { return w.current.receipts } -// OutgoingReceipts get the receipts generated starting from the last state. -func (w *Worker) OutgoingReceipts() []*types.CXReceipt { - return w.current.outcxs -} - -// IncomingReceipts get incoming receipts in destination shard that is received from source shard -func (w *Worker) IncomingReceipts() []*types.CXReceiptsProof { - return w.current.incxs -} - // CollectVerifiedSlashes sets w.current.slashes only to those that // past verification func (w *Worker) CollectVerifiedSlashes() error { diff --git a/rpc/preimages.go b/rpc/preimages.go index ceb3ff8471..71887030ac 100644 --- a/rpc/preimages.go +++ b/rpc/preimages.go @@ -29,57 +29,3 @@ func (s *PreimagesService) Export(_ context.Context, path string) error { return core.ExportPreimages(s.hmy.BlockChain, path) } -func (s *PreimagesService) Import(_ context.Context, path string) error { - // these are by default not blocking - return core.ImportPreimages(s.hmy.BlockChain, path) -} -func (s *PreimagesService) Generate(_ context.Context, start, end rpc.BlockNumber) error { - // earliestBlock: the number of blocks in the past where you can generate the preimage from the last block - earliestBlock := uint64(2) - currentBlockNum := s.hmy.CurrentBlock().NumberU64() - - var startBlock uint64 - switch start { - case rpc.EarliestBlockNumber: - startBlock = earliestBlock - case rpc.LatestBlockNumber: - startBlock = earliestBlock - case rpc.PendingBlockNumber: - startBlock = earliestBlock - default: - startBlock = uint64(start) - } - - var endBlock = uint64(end) - switch end { - case rpc.EarliestBlockNumber: - endBlock = currentBlockNum - case rpc.LatestBlockNumber: - endBlock = currentBlockNum - case rpc.PendingBlockNumber: - endBlock = currentBlockNum - default: - endBlock = uint64(end) - } - - fmt.Printf("Generating preimage from block %d to %d\n", startBlock, endBlock) - - if number := currentBlockNum; number > endBlock { - fmt.Printf( - "Cropping generate endpoint from %d to %d\n", - endBlock, number, - ) - endBlock = number - } - - if startBlock >= endBlock { - fmt.Printf( - "Cropping generate startpoint from %d to %d\n", - startBlock, endBlock-earliestBlock, - ) - startBlock = endBlock - earliestBlock - } - - // these are by default not blocking - return core.GeneratePreimages(s.hmy.BlockChain, startBlock, endBlock) -}