Skip to content

Commit

Permalink
Merge branch 'develop' into sc/liquidity-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
agusduha committed Aug 20, 2024
2 parents 4305edc + d1e1c25 commit 06d662d
Show file tree
Hide file tree
Showing 47 changed files with 1,998 additions and 387 deletions.
1 change: 1 addition & 0 deletions .semgrepignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ tests/
# Semgrep-action log folder
.semgrep_logs/

op-chain-ops/script/testdata

packages/*/node_modules
packages/*/test
13 changes: 7 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/ethereum-optimism/optimism
go 1.21

require (
github.com/BurntSushi/toml v1.4.0
github.com/andybalholm/brotli v1.1.0
github.com/btcsuite/btcd v0.24.2
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0
Expand All @@ -11,7 +12,7 @@ require (
github.com/crate-crypto/go-kzg-4844 v1.0.0
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0
github.com/ethereum-optimism/go-ethereum-hdwallet v0.1.3
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20240814192743-ea7e768a02a6
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20240820005335-12015ad6233b
github.com/ethereum/go-ethereum v1.14.8
github.com/fsnotify/fsnotify v1.7.0
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb
Expand All @@ -38,7 +39,7 @@ require (
github.com/onsi/gomega v1.34.1
github.com/pkg/errors v0.9.1
github.com/pkg/profile v1.7.0
github.com/prometheus/client_golang v1.19.1
github.com/prometheus/client_golang v1.20.0
github.com/protolambda/ctxlock v0.1.0
github.com/stretchr/testify v1.9.0
github.com/urfave/cli/v2 v2.27.4
Expand All @@ -50,7 +51,6 @@ require (
)

require (
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/DataDog/zstd v1.5.6-0.20230824185856-869dae002e5e // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/VictoriaMetrics/fastcache v1.12.2 // indirect
Expand Down Expand Up @@ -163,6 +163,7 @@ require (
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-multistream v0.5.0 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/naoina/go-stringutil v0.1.0 // indirect
github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 // indirect
github.com/onsi/ginkgo/v2 v2.19.0 // indirect
Expand All @@ -188,8 +189,8 @@ require (
github.com/pion/webrtc/v3 v3.2.40 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/protolambda/bls12-381-util v0.1.0 // indirect
github.com/protolambda/zrnt v0.32.2 // indirect
github.com/protolambda/ztyp v0.2.2 // indirect
Expand Down Expand Up @@ -232,7 +233,7 @@ require (
rsc.io/tmplfunc v0.0.3 // indirect
)

replace github.com/ethereum/go-ethereum v1.14.8 => github.com/ethereum-optimism/op-geth v1.101408.0-rc.1
replace github.com/ethereum/go-ethereum v1.14.8 => github.com/ethereum-optimism/op-geth v1.101408.0-rc.2

// replace github.com/ethereum/go-ethereum => ../op-geth

Expand Down
22 changes: 12 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ github.com/elastic/gosigar v0.14.2 h1:Dg80n8cr90OZ7x+bAax/QjoW/XqTI11RmA79ZwIm9/
github.com/elastic/gosigar v0.14.2/go.mod h1:iXRIGg2tLnu7LBdpqzyQfGDEidKCfWcCMS0WKyPWoMs=
github.com/ethereum-optimism/go-ethereum-hdwallet v0.1.3 h1:RWHKLhCrQThMfch+QJ1Z8veEq5ZO3DfIhZ7xgRP9WTc=
github.com/ethereum-optimism/go-ethereum-hdwallet v0.1.3/go.mod h1:QziizLAiF0KqyLdNJYD7O5cpDlaFMNZzlxYNcWsJUxs=
github.com/ethereum-optimism/op-geth v1.101408.0-rc.1 h1:pplgjM8alBpgEdklWhNsEZBv3Vj3LE/3ox9nmzfYO2o=
github.com/ethereum-optimism/op-geth v1.101408.0-rc.1/go.mod h1:WAxE4N2rkFDuE1QcQV2MM2ljtE30NyR2n7fXyukARwk=
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20240814192743-ea7e768a02a6 h1:s51nSnpwPJRRU/F6mob/fnOebFmuPz3Ab1J/HCtC40U=
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20240814192743-ea7e768a02a6/go.mod h1:zy9f3TNPS7pwW4msMitF83fp0Wf452tZ6+Fg6d4JyXM=
github.com/ethereum-optimism/op-geth v1.101408.0-rc.2 h1:JmiDUZtpBGQ+DyXA1b9AHBytxc1NlbpXTwnX/fETYk8=
github.com/ethereum-optimism/op-geth v1.101408.0-rc.2/go.mod h1:29rbyakWPm5ATIf19M1yQJlFZDV2wV8adM5rv+A+ceI=
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20240820005335-12015ad6233b h1:8pGs5+NyCvkmfu7rn+QB3yuwKA1c9zTF5k4bXK/lv9E=
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20240820005335-12015ad6233b/go.mod h1:zy9f3TNPS7pwW4msMitF83fp0Wf452tZ6+Fg6d4JyXM=
github.com/ethereum/c-kzg-4844 v1.0.0 h1:0X1LBXxaEtYD9xsyj9B9ctQEZIpnvVDeoBx8aHEwTNA=
github.com/ethereum/c-kzg-4844 v1.0.0/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0=
github.com/ethereum/go-verkle v0.1.1-0.20240306133620-7d920df305f0 h1:KrE8I4reeVvf7C1tm8elRjj4BdscTYzz/WAbYyf/JI4=
Expand Down Expand Up @@ -533,6 +533,8 @@ github.com/multiformats/go-multistream v0.5.0/go.mod h1:n6tMZiwiP2wUsR8DgfDWw1dy
github.com/multiformats/go-varint v0.0.1/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
github.com/multiformats/go-varint v0.0.7 h1:sWSGR+f/eu5ABZA2ZpYKBILXTTs9JWpdEM/nEGOHFS8=
github.com/multiformats/go-varint v0.0.7/go.mod h1:r8PUYw/fD/SjBCiKOoDlGF6QawOELpZAu9eioSos/OU=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/naoina/go-stringutil v0.1.0 h1:rCUeRUHjBjGTSHl0VC00jUPLz8/F9dDzYI70Hzifhks=
github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0=
Expand Down Expand Up @@ -640,8 +642,8 @@ github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXP
github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU=
github.com/prometheus/client_golang v1.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE=
github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho=
github.com/prometheus/client_golang v1.20.0 h1:jBzTZ7B099Rg24tny+qngoynol8LtVYlA2bqx3vEloI=
github.com/prometheus/client_golang v1.20.0/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand All @@ -651,15 +653,15 @@ github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7q
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4=
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc=
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/protolambda/bls12-381-util v0.1.0 h1:05DU2wJN7DTU7z28+Q+zejXkIsA/MF8JZQGhtBZZiWk=
github.com/protolambda/bls12-381-util v0.1.0/go.mod h1:cdkysJTRpeFeuUVx/TXGDQNMTiRAalk1vQw3TYTHcE4=
github.com/protolambda/ctxlock v0.1.0 h1:rCUY3+vRdcdZXqT07iXgyr744J2DU2LCBIXowYAjBCE=
Expand Down
122 changes: 122 additions & 0 deletions op-chain-ops/foundry/allocs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package foundry

import (
"encoding/json"
"fmt"
"maps"
"math/big"
"os"

"github.com/holiman/uint256"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
)

type ForgeAllocs struct {
Accounts types.GenesisAlloc
}

// FromState takes a geth StateDB, and dumps the accounts into the ForgeAllocs.
// Any previous allocs contents are removed.
// Warning: the state must be committed first, trie-key preimages must be present for iteration,
// and a fresh state around the committed state-root must be presented, for the latest state-contents to be dumped.
func (f *ForgeAllocs) FromState(stateDB StateDB) {
f.Accounts = make(types.GenesisAlloc)
stateDB.DumpToCollector((*forgeAllocsDump)(f), &state.DumpConfig{
OnlyWithAddresses: true,
})
}

// StateDB is a minimal interface to support dumping of Geth EVM state to ForgeAllocs.
type StateDB interface {
DumpToCollector(c state.DumpCollector, conf *state.DumpConfig) (nextKey []byte)
}

// Assert that the Geth StateDB implements this interface still.
var _ StateDB = (*state.StateDB)(nil)

// forgeAllocsDump is a wrapper to hide the error-prone state-dumping interface from public API.
// Use ForgeAllocs.FromState to dump a state to forge-allocs.
type forgeAllocsDump ForgeAllocs

// ForgeAllocs implements state.DumpAllocator, such that the EVM state can be dumped into it:
// with a StateDB.DumpToCollector call.
var _ state.DumpCollector = (*forgeAllocsDump)(nil)

func (d *forgeAllocsDump) OnRoot(hash common.Hash) {
// Unlike the geth raw-state-dump, forge-allocs do not reference the state trie root.
}

func (d *forgeAllocsDump) OnAccount(address *common.Address, account state.DumpAccount) {
if address == nil {
return
}
if _, ok := d.Accounts[*address]; ok {
panic(fmt.Errorf("cannot dump account %s twice", *address))
}
balance, ok := new(big.Int).SetString(account.Balance, 0)
if !ok {
panic("invalid balance")
}
var storage map[common.Hash]common.Hash
if len(account.Storage) > 0 {
storage = make(map[common.Hash]common.Hash, len(account.Storage))
for k, v := range account.Storage {
storage[k] = common.HexToHash(v)
}
}
d.Accounts[*address] = types.Account{
Code: account.Code,
Storage: storage,
Balance: balance,
Nonce: account.Nonce,
}
}

func (d *ForgeAllocs) Copy() *ForgeAllocs {
out := make(types.GenesisAlloc, len(d.Accounts))
maps.Copy(out, d.Accounts)
return &ForgeAllocs{Accounts: out}
}

func (d *ForgeAllocs) UnmarshalJSON(b []byte) error {
// forge, since integrating Alloy, likes to hex-encode everything.
type forgeAllocAccount struct {
Balance hexutil.U256 `json:"balance"`
Nonce hexutil.Uint64 `json:"nonce"`
Code hexutil.Bytes `json:"code,omitempty"`
Storage map[common.Hash]common.Hash `json:"storage,omitempty"`
}
var allocs map[common.Address]forgeAllocAccount
if err := json.Unmarshal(b, &allocs); err != nil {
return err
}
d.Accounts = make(types.GenesisAlloc, len(allocs))
for addr, acc := range allocs {
acc := acc
d.Accounts[addr] = types.Account{
Code: acc.Code,
Storage: acc.Storage,
Balance: (*uint256.Int)(&acc.Balance).ToBig(),
Nonce: (uint64)(acc.Nonce),
PrivateKey: nil,
}
}
return nil
}

func LoadForgeAllocs(allocsPath string) (*ForgeAllocs, error) {
f, err := os.OpenFile(allocsPath, os.O_RDONLY, 0644)
if err != nil {
return nil, fmt.Errorf("failed to open forge allocs %q: %w", allocsPath, err)
}
defer f.Close()
var out ForgeAllocs
if err := json.NewDecoder(f).Decode(&out); err != nil {
return nil, fmt.Errorf("failed to json-decode forge allocs %q: %w", allocsPath, err)
}
return &out, nil
}
98 changes: 98 additions & 0 deletions op-chain-ops/foundry/allocs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package foundry

import (
"os"
"testing"

"github.com/holiman/uint256"
"github.com/stretchr/testify/require"

oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/triedb"
"github.com/ethereum/go-ethereum/triedb/hashdb"
)

func TestForgeAllocs_FromState(t *testing.T) {
// Internals of state-dumping of Geth have silent errors.
cfg := oplog.DefaultCLIConfig()
cfg.Level = log.LevelTrace
oplog.SetGlobalLogHandler(oplog.NewLogHandler(os.Stdout, cfg))

rawDB := rawdb.NewMemoryDatabase()
stateDB := state.NewDatabaseWithConfig(rawDB, &triedb.Config{
Preimages: true,
IsVerkle: false,
HashDB: hashdb.Defaults,
PathDB: nil,
})
st, err := state.New(types.EmptyRootHash, stateDB, nil)
require.NoError(t, err)

alice := common.HexToAddress("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")
st.CreateAccount(alice)
st.SetBalance(alice, uint256.NewInt(123), tracing.BalanceChangeUnspecified)
st.SetNonce(alice, 42)

bob := common.HexToAddress("0x70997970C51812dc3A010C7d01b50e0d17dc79C8")
st.CreateAccount(bob)
st.CreateContract(bob)
st.SetBalance(bob, uint256.NewInt(100), tracing.BalanceChangeUnspecified)
st.SetNonce(bob, 1)
st.SetState(bob, common.Hash{0: 0x42}, common.Hash{0: 7})

contract := common.HexToAddress("0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC")
st.CreateAccount(contract)
st.CreateContract(contract)
st.SetNonce(contract, 30)
st.SetBalance(contract, uint256.NewInt(0), tracing.BalanceChangeUnspecified)
st.SetCode(contract, []byte{10, 11, 12, 13, 14})

// Commit and make a new state, we cannot reuse the state after Commit
// (see doc-comment in Commit, absolute footgun)
root, err := st.Commit(0, false)
require.NoError(t, err)
st, err = state.New(root, stateDB, nil)
require.NoError(t, err)

st.SetState(contract, common.Hash{0: 0xa}, common.Hash{0: 1})
st.SetState(contract, crypto.Keccak256Hash([]byte("hello")), crypto.Keccak256Hash([]byte("world")))

root, err = st.Commit(0, false)
require.NoError(t, err)
st, err = state.New(root, stateDB, nil)
require.NoError(t, err)

var allocs ForgeAllocs
allocs.FromState(st)

require.Len(t, allocs.Accounts, 3)

require.Contains(t, allocs.Accounts, alice)
require.Nil(t, allocs.Accounts[alice].Code)
require.Nil(t, allocs.Accounts[alice].Storage)
require.Equal(t, "123", allocs.Accounts[alice].Balance.String())
require.Equal(t, uint64(42), allocs.Accounts[alice].Nonce)

require.Contains(t, allocs.Accounts, bob)
require.Nil(t, allocs.Accounts[bob].Code)
require.Len(t, allocs.Accounts[bob].Storage, 1)
require.Equal(t, common.Hash{0: 7}, allocs.Accounts[bob].Storage[common.Hash{0: 0x42}])
require.Equal(t, "100", allocs.Accounts[bob].Balance.String())
require.Equal(t, uint64(1), allocs.Accounts[bob].Nonce)

require.Contains(t, allocs.Accounts, contract)
require.Equal(t, []byte{10, 11, 12, 13, 14}, allocs.Accounts[contract].Code)
require.Len(t, allocs.Accounts[contract].Storage, 2)
require.Equal(t, common.Hash{0: 1}, allocs.Accounts[contract].Storage[common.Hash{0: 0xa}])
require.Equal(t, crypto.Keccak256Hash([]byte("world")),
allocs.Accounts[contract].Storage[crypto.Keccak256Hash([]byte("hello"))])
require.Equal(t, "0", allocs.Accounts[contract].Balance.String())
require.Equal(t, uint64(30), allocs.Accounts[contract].Nonce)
}
Loading

0 comments on commit 06d662d

Please sign in to comment.