From ed08ff34de491114ab0d56b296f34b4670642888 Mon Sep 17 00:00:00 2001 From: Soph <35721420+sophoah@users.noreply.github.com> Date: Wed, 20 Sep 2023 04:28:15 +0700 Subject: [PATCH] Hip30 : localnet account migration fix (#4508) * flags: set up preimage flags * hip30: set up preimage import, export, api * save pre-images by default * add pre images api * goimports * commit rpc preimages file * preimages: re-generate them using CLI * add metrics and numbers for pre-images * automate generation after import * move from rpc to core * goimports * add back core/preimages.go file * HIP-30: sharding configuration boilerplate * update comments * goimports * HIP-30: minimum validator commission of 7% Based on #4495, which must be merged before this PR. This PR should be rebased with dev after #4495 is merged to retain atomicity of changes by pull request. * goimports * HIP-30: Emission split implementation Note that the allocated split of the emission goes directly to the recipient (and not via the Reward). This is because rewards are indexed by validator and not by delegator, and the recipient may/may not have any delegations which we can reward. Even if one was guaranteed to exist, it would mess up the math of the validator. * set up mainnet recipient of emission split * HIP-30: Emission split addresses for non mainnet * HIP-30: deactivate shard 2 and 3 validators * goimports * update test * goimports * migrate balance uring epoch T - 1 highly untested code. also missing is the ability to generate a pre-migration report for future verification. * update test * export prometheus metric when no error importing preimage * add comment * test account migration in localnet * add preimages flags to rootflags * enable preimages on the whitelist * add the generate method * fix cropping log * fix cropping log * cropping startpoint when bigger than endpoint * add support for the rpcblocknumer type * enable import api * use earlies block * debug logs * debug logs * debug logs * debug logs * fix error catching * fix error catching * make end optional for the comand line * fix cropping logic * improve error when apply message fails * add balance on the error * fix importing * remove unused imports * create preimage for genesis block * fix consensus with No Migration Possible * use correct header for migration * process all tx in all block for non shard 0 --------- Co-authored-by: MaxMustermann2 <82761650+MaxMustermann2@users.noreply.github.com> Co-authored-by: Diego Nava Co-authored-by: Diego Nava <8563843+diego1q2w@users.noreply.github.com> --- core/genesis.go | 7 +++++++ core/state_processor.go | 14 ++++---------- internal/configs/sharding/localnet.go | 4 ++-- node/node_newblock.go | 1 + rpc/preimages.go | 2 +- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/core/genesis.go b/core/genesis.go index 3e230cf9e..8f6d0777e 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -28,6 +28,7 @@ import ( "strings" "github.com/ethereum/go-ethereum/common" + ethCommon "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/crypto" @@ -252,6 +253,12 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block { for key, value := range account.Storage { statedb.SetState(addr, key, value) } + //statedb.AddPreimage(crypto.Keccak256Hash((addr[:])), addr.Bytes()[:]) + rawdb.WritePreimages( + statedb.Database().DiskDB(), map[ethCommon.Hash][]byte{ + crypto.Keccak256Hash((addr.Bytes())): addr.Bytes(), + }, + ) } root := statedb.IntermediateRoot(false) shardStateBytes, err := shard.EncodeWrapper(g.ShardState, false) diff --git a/core/state_processor.go b/core/state_processor.go index a0abf31f6..f5cd6529a 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -145,7 +145,7 @@ func (p *StateProcessor) Process( // ran out of accounts processTxsAndStxs = false } - if !errors.Is(err, ErrNoMigrationRequired) { + if !errors.Is(err, ErrNoMigrationRequired) && !errors.Is(err, ErrNoMigrationPossible) { return nil, nil, nil, nil, 0, nil, statedb, err } } else { @@ -576,13 +576,9 @@ func MayBalanceMigration( if isDevnet || isLocalnet { if config.IsOneEpochBeforeHIP30(header.Epoch()) { if myShard := chain.ShardID(); myShard != shard.BeaconChainShardID { - parent := chain.GetBlockByHash( + parentRoot := chain.GetBlockByHash( header.ParentHash(), - ) - if parent == nil { - return nil, errors.Wrap(ErrNoMigrationPossible, "parent is nil") - } - parentRoot := parent.Root() + ).Root() // for examining MPT at this root, should exist // for examining MPT at this root, should exist cx, err := generateOneMigrationMessage( db, parentRoot, @@ -641,13 +637,11 @@ func generateOneMigrationMessage( key := accountIterator.LeafKey() preimage := rawdb.ReadPreimage(db, common.BytesToHash(key)) if len(preimage) == 0 { - e := errors.New( + return nil, 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 5c75967da..fb6050b1d 100644 --- a/internal/configs/sharding/localnet.go +++ b/internal/configs/sharding/localnet.go @@ -31,8 +31,8 @@ const ( localnetV1Epoch = 1 localnetEpochBlock1 = 5 - localnetBlocksPerEpoch = 8 - localnetBlocksPerEpochV2 = 8 + localnetBlocksPerEpoch = 64 + localnetBlocksPerEpochV2 = 64 localnetVdfDifficulty = 5000 // This takes about 10s to finish the vdf ) diff --git a/node/node_newblock.go b/node/node_newblock.go index 19a9c9b23..62315f5a7 100644 --- a/node/node_newblock.go +++ b/node/node_newblock.go @@ -157,6 +157,7 @@ func (node *Node) ProposeNewBlock(commitSigs chan []byte) (*types.Block, error) } } + // Execute all the time except for last block of epoch for shard 0 if !shard.Schedule.IsLastBlock(header.Number().Uint64()) || node.Consensus.ShardID != 0 { // Prepare normal and staking transactions retrieved from transaction pool utils.AnalysisStart("proposeNewBlockChooseFromTxnPool") diff --git a/rpc/preimages.go b/rpc/preimages.go index 2935f7e25..d0ab56f38 100644 --- a/rpc/preimages.go +++ b/rpc/preimages.go @@ -23,7 +23,7 @@ func NewPreimagesAPI(hmy *hmy.Harmony, version string) rpc.API { } } -func (s *PreimagesService) Export(_ context.Context, path string) error { +func (s *PreimagesService) Export(ctx context.Context, path string) error { // these are by default not blocking return core.ExportPreimages(s.hmy.BlockChain, path) }