Skip to content

Commit

Permalink
Hip30 : localnet account migration fix (#4508)
Browse files Browse the repository at this point in the history
* 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 <diego.nava77@hotmail.com>
Co-authored-by: Diego Nava <8563843+diego1q2w@users.noreply.github.com>
  • Loading branch information
4 people committed Sep 19, 2023
1 parent 29d46b9 commit ed08ff3
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
7 changes: 7 additions & 0 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 4 additions & 10 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/configs/sharding/localnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
1 change: 1 addition & 0 deletions node/node_newblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion rpc/preimages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit ed08ff3

Please sign in to comment.