Skip to content

Commit 29c7b44

Browse files
tynesprotolambda
andauthored
op-chain-ops: delete memdb (ethereum-optimism#10404)
* op-chain-ops: delete memdb The simple in memory statedb implementation that was backed by a genesis has served us well but is no longer required since we build the genesis files using foundry. This was originally used to enable simple execution to deploy contracts and then dump into a `genesis.json`. Keeping this memdb around will only bloat PRs that update the geth version as they may change the interface to the statedb. Instead of using the memdb interface, the genesis is modified directly. * lint: fix * style: way better Co-authored-by: protolambda <[email protected]> * build: fix * build: fix --------- Co-authored-by: protolambda <[email protected]>
1 parent 7534ac5 commit 29c7b44

File tree

4 files changed

+55
-455
lines changed

4 files changed

+55
-455
lines changed

op-chain-ops/genesis/layer_one.go

+19-23
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ import (
44
"fmt"
55
"math/big"
66

7-
"github.com/holiman/uint256"
8-
97
"github.com/ethereum/go-ethereum/common"
108
"github.com/ethereum/go-ethereum/core"
11-
"github.com/ethereum/go-ethereum/core/vm"
129
"github.com/ethereum/go-ethereum/log"
1310
"github.com/ethereum/go-ethereum/params"
1411

1512
"github.com/ethereum-optimism/optimism/op-bindings/bindings"
16-
"github.com/ethereum-optimism/optimism/op-chain-ops/state"
1713
)
1814

1915
// PrecompileCount represents the number of precompile addresses
@@ -61,43 +57,43 @@ func BuildL1DeveloperGenesis(config *DeployConfig, dump *ForgeAllocs, l1Deployme
6157
}
6258
// copy, for safety when the dump is reused (like in e2e testing)
6359
genesis.Alloc = dump.Copy().Accounts
64-
memDB := state.NewMemoryStateDB(genesis)
65-
FundDevAccounts(memDB)
66-
SetPrecompileBalances(memDB)
60+
FundDevAccounts(genesis)
61+
SetPrecompileBalances(genesis)
6762

6863
l1Deployments.ForEach(func(name string, addr common.Address) {
69-
acc := memDB.GetAccount(addr)
70-
if acc != nil {
64+
acc, ok := genesis.Alloc[addr]
65+
if ok {
7166
log.Info("Included L1 deployment", "name", name, "address", addr, "balance", acc.Balance, "storage", len(acc.Storage), "nonce", acc.Nonce)
7267
} else {
7368
log.Info("Excluded L1 deployment", "name", name, "address", addr)
7469
}
7570
})
7671

77-
return memDB.Genesis(), nil
78-
}
79-
80-
// CreateAccountNotExists creates the account in the `vm.StateDB` if it doesn't exist.
81-
func CreateAccountNotExists(db vm.StateDB, account common.Address) {
82-
if !db.Exist(account) {
83-
db.CreateAccount(account)
84-
}
72+
return genesis, nil
8573
}
8674

8775
// FundDevAccounts will fund each of the development accounts.
88-
func FundDevAccounts(db vm.StateDB) {
76+
func FundDevAccounts(gen *core.Genesis) {
8977
for _, account := range DevAccounts {
90-
CreateAccountNotExists(db, account)
91-
db.AddBalance(account, uint256.MustFromBig(devBalance))
78+
acc := gen.Alloc[account]
79+
if acc.Balance == nil {
80+
acc.Balance = new(big.Int)
81+
}
82+
acc.Balance = acc.Balance.Add(acc.Balance, devBalance)
83+
gen.Alloc[account] = acc
9284
}
9385
}
9486

9587
// SetPrecompileBalances will set a single wei at each precompile address.
9688
// This is an optimization to make calling them cheaper.
97-
func SetPrecompileBalances(db vm.StateDB) {
89+
func SetPrecompileBalances(gen *core.Genesis) {
9890
for i := 0; i < PrecompileCount; i++ {
9991
addr := common.BytesToAddress([]byte{byte(i)})
100-
CreateAccountNotExists(db, addr)
101-
db.AddBalance(addr, uint256.NewInt(1))
92+
acc := gen.Alloc[addr]
93+
if acc.Balance == nil {
94+
acc.Balance = new(big.Int)
95+
}
96+
acc.Balance = acc.Balance.Add(acc.Balance, big.NewInt(1))
97+
gen.Alloc[addr] = acc
10298
}
10399
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package genesis
2+
3+
import (
4+
"math/big"
5+
"testing"
6+
7+
"github.com/ethereum/go-ethereum/core"
8+
"github.com/ethereum/go-ethereum/core/types"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
// TestFundDevAccounts ensures that the developer accounts are
13+
// added to the genesis state correctly.
14+
func TestFundDevAccounts(t *testing.T) {
15+
gen := core.Genesis{
16+
Alloc: make(types.GenesisAlloc),
17+
}
18+
FundDevAccounts(&gen)
19+
require.Equal(t, len(gen.Alloc), len(DevAccounts))
20+
for _, account := range gen.Alloc {
21+
require.Equal(t, devBalance, account.Balance)
22+
}
23+
}
24+
25+
// TestSetPrecompileBalances ensures that the precompiles are
26+
// initialized with a balance of 1.
27+
func TestSetPrecompileBalances(t *testing.T) {
28+
gen := core.Genesis{
29+
Alloc: make(types.GenesisAlloc),
30+
}
31+
SetPrecompileBalances(&gen)
32+
require.Equal(t, len(gen.Alloc), PrecompileCount)
33+
for _, account := range gen.Alloc {
34+
require.Equal(t, big.NewInt(1), account.Balance)
35+
}
36+
}

0 commit comments

Comments
 (0)