Skip to content

Commit

Permalink
Improved genesis predeploy, allowing initializing immutables and acce…
Browse files Browse the repository at this point in the history
…ss chainID from constructor. (#1838)

* Instead of using the deployedCode of the artifact file, extract the code from the deployment result.
* Respect the genesis file's chainID when preparing the EVM context.
  • Loading branch information
ohijiho authored Aug 22, 2023
1 parent 2684cc5 commit 49216ad
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions command/genesis/predeploy/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (p *predeployParams) updateGenesisConfig() error {
p.artifactsPath,
p.constructorArgs,
p.address,
p.genesisConfig.Params.ChainID,
)
if err != nil {
return err
Expand Down
11 changes: 7 additions & 4 deletions helper/predeployment/predeployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"math/big"
"os"

"github.com/umbracle/ethgo/abi"

"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/helper/hex"
"github.com/0xPolygon/polygon-edge/state"
itrie "github.com/0xPolygon/polygon-edge/state/immutable-trie"
"github.com/0xPolygon/polygon-edge/state/runtime"
"github.com/0xPolygon/polygon-edge/state/runtime/evm"
"github.com/0xPolygon/polygon-edge/types"
"github.com/umbracle/ethgo/abi"
)

var (
Expand Down Expand Up @@ -116,7 +117,7 @@ func getModifiedStorageMap(radix *state.Txn, address types.Address) map[types.Ha
return storageMap
}

func getPredeployAccount(address types.Address, input, deployedBytecode []byte) (*chain.GenesisAccount, error) {
func getPredeployAccount(address types.Address, input []byte, chainID int64) (*chain.GenesisAccount, error) {
// Create an instance of the state
st := itrie.NewState(itrie.NewMemoryStorage())

Expand All @@ -142,6 +143,7 @@ func getPredeployAccount(address types.Address, input, deployedBytecode []byte)

// Create a transition
transition := state.NewTransition(config, snapshot, radix)
transition.ContextPtr().ChainID = chainID

// Run the transition through the EVM
res := evm.NewEVM().Run(contract, transition, &config)
Expand All @@ -161,7 +163,7 @@ func getPredeployAccount(address types.Address, input, deployedBytecode []byte)
return &chain.GenesisAccount{
Balance: transition.GetBalance(address),
Nonce: transition.GetNonce(address),
Code: deployedBytecode,
Code: res.ReturnValue,
Storage: storageMap,
}, nil
}
Expand All @@ -172,6 +174,7 @@ func GenerateGenesisAccountFromFile(
filepath string,
constructorArgs []string,
predeployAddress types.Address,
chainID int64,
) (*chain.GenesisAccount, error) {
// Create the artifact from JSON
artifact, err := loadContractArtifact(filepath)
Expand Down Expand Up @@ -209,5 +212,5 @@ func GenerateGenesisAccountFromFile(
finalBytecode = append(artifact.Bytecode, constructor...)
}

return getPredeployAccount(predeployAddress, finalBytecode, artifact.DeployedBytecode)
return getPredeployAccount(predeployAddress, finalBytecode, chainID)
}

0 comments on commit 49216ad

Please sign in to comment.