Skip to content

Commit

Permalink
Merge pull request ethereum-optimism#6841 from ethereum-optimism/fix/…
Browse files Browse the repository at this point in the history
…always-fund-precompile-balances

op-chain-ops: always fund precompile balances in L2 genesis
  • Loading branch information
OptimismBot authored Aug 18, 2023
2 parents 4cd43b1 + 660802e commit 39711b8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
3 changes: 2 additions & 1 deletion op-chain-ops/genesis/layer_two.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ func BuildL2Genesis(config *DeployConfig, l1StartBlock *types.Block) (*core.Gene
if config.FundDevAccounts {
log.Info("Funding developer accounts in L2 genesis")
FundDevAccounts(db)
SetPrecompileBalances(db)
}

SetPrecompileBalances(db)

storage, err := NewL2StorageConfig(config, l1StartBlock)
if err != nil {
return nil, err
Expand Down
10 changes: 8 additions & 2 deletions op-chain-ops/genesis/layer_two_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func testBuildL2Genesis(t *testing.T, config *genesis.DeployConfig) *core.Genesi
}
}

// All of the precompile addresses should be funded with a single wei
for i := 0; i < genesis.PrecompileCount; i++ {
addr := common.BytesToAddress([]byte{byte(i)})
require.Equal(t, common.Big1, gen.Alloc[addr].Balance)
}

if writeFile {
file, _ := json.MarshalIndent(gen, "", " ")
_ = os.WriteFile("genesis.json", file, 0644)
Expand All @@ -79,7 +85,7 @@ func TestBuildL2MainnetGenesis(t *testing.T) {
config.EnableGovernance = true
config.FundDevAccounts = false
gen := testBuildL2Genesis(t, config)
require.Equal(t, 2066, len(gen.Alloc))
require.Equal(t, 2322, len(gen.Alloc))
}

func TestBuildL2MainnetNoGovernanceGenesis(t *testing.T) {
Expand All @@ -88,5 +94,5 @@ func TestBuildL2MainnetNoGovernanceGenesis(t *testing.T) {
config.EnableGovernance = false
config.FundDevAccounts = false
gen := testBuildL2Genesis(t, config)
require.Equal(t, 2066, len(gen.Alloc))
require.Equal(t, 2322, len(gen.Alloc))
}
7 changes: 6 additions & 1 deletion op-chain-ops/genesis/setters.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import (
"github.com/ethereum/go-ethereum/log"
)

// PrecompileCount represents the number of precompile addresses
// starting from `address(0)` to PrecompileCount that are funded
// with a single wei in the genesis state.
const PrecompileCount = 256

// FundDevAccounts will fund each of the development accounts.
func FundDevAccounts(db vm.StateDB) {
for _, account := range DevAccounts {
Expand Down Expand Up @@ -52,7 +57,7 @@ func setProxies(db vm.StateDB, proxyAdminAddr common.Address, namespace *big.Int
// This is an optimization to make calling them cheaper. This should only
// be used for devnets.
func SetPrecompileBalances(db vm.StateDB) {
for i := 0; i < 256; i++ {
for i := 0; i < PrecompileCount; i++ {
addr := common.BytesToAddress([]byte{byte(i)})
db.CreateAccount(addr)
db.AddBalance(addr, common.Big1)
Expand Down

0 comments on commit 39711b8

Please sign in to comment.