Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ETH dump #1373

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions l2geth/core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/ethereum-optimism/optimism/l2geth/common"
"github.com/ethereum-optimism/optimism/l2geth/core/types"
"github.com/ethereum-optimism/optimism/l2geth/crypto"
"github.com/ethereum-optimism/optimism/l2geth/ethdumper"
"github.com/ethereum-optimism/optimism/l2geth/log"
"github.com/ethereum-optimism/optimism/l2geth/metrics"
"github.com/ethereum-optimism/optimism/l2geth/rlp"
Expand Down Expand Up @@ -506,6 +507,7 @@ func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) {
bal := value.Big()
bal = bal.Add(bal, amount)
s.SetState(dump.OvmEthAddress, key, common.BigToHash(bal))
ethdumper.Write(addr)
} else {
stateObject := s.GetOrNewStateObject(addr)
if stateObject != nil {
Expand Down Expand Up @@ -536,6 +538,7 @@ func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) {
bal := value.Big()
bal = bal.Sub(bal, amount)
s.SetState(dump.OvmEthAddress, key, common.BigToHash(bal))
ethdumper.Write(addr)
} else {
stateObject := s.GetOrNewStateObject(addr)
if stateObject != nil {
Expand Down
2 changes: 2 additions & 0 deletions l2geth/core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/ethereum-optimism/optimism/l2geth/common"
"github.com/ethereum-optimism/optimism/l2geth/common/hexutil"
"github.com/ethereum-optimism/optimism/l2geth/crypto"
"github.com/ethereum-optimism/optimism/l2geth/ethdumper"
"github.com/ethereum-optimism/optimism/l2geth/log"
"github.com/ethereum-optimism/optimism/l2geth/params"
"github.com/ethereum-optimism/optimism/l2geth/rollup/dump"
Expand Down Expand Up @@ -594,6 +595,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
if len(input) >= 36 && bytes.Equal(input[:4], mintSigHash) {
recipient := common.BytesToAddress(input[16:36])
statedumper.WriteETH(recipient)
ethdumper.Write(recipient)
}
}

Expand Down
99 changes: 99 additions & 0 deletions l2geth/ethdumper/dumper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package ethdumper

import (
"encoding/json"
"io"
"os"
"sync"

"github.com/ethereum-optimism/optimism/l2geth/common"
"github.com/ethereum-optimism/optimism/l2geth/log"
)

type EthAddress struct {
Addresses []common.Address `json:"addresses"`
}

type EthDumper interface {
Write(address common.Address)
}

var DefaultEthDumper EthDumper

func NewEthDumper() EthDumper {
path := os.Getenv("L2GETH_ETH_DUMP_PATH")
if path == "" {
return &noopEthDumper{}
}

f, err := os.Open(path)
if err != nil {
panic(err)
}

byteFile, err := io.ReadAll(f)
if err != nil {
panic(err)
}
if len(byteFile) == 0 {
return &FileStateDumper{
ethAddress: EthAddress{},
ethCache: make(map[common.Address]bool),
}
}
ethAddres := EthAddress{}
err = json.Unmarshal(byteFile, &ethAddres)
if err != nil {
panic(err)
}
ethCache := make(map[common.Address]bool)
for _, address := range ethAddres.Addresses {
ethCache[address] = true
}
return &FileStateDumper{
ethAddress: ethAddres,
ethCache: ethCache,
}
}

type FileStateDumper struct {
ethAddress EthAddress
ethCache map[common.Address]bool
mtx sync.Mutex
}

func (s *FileStateDumper) Write(address common.Address) {
s.mtx.Lock()
defer s.mtx.Unlock()
if s.ethCache[address] {
return
}
s.ethCache[address] = true

s.ethAddress.Addresses = append(s.ethAddress.Addresses, address)

log.Info("Found eth address", "address", address, "total", len(s.ethAddress.Addresses))

content, err := json.Marshal(s.ethAddress)
if err != nil {
panic(err)
}
err = os.WriteFile(os.Getenv("L2GETH_ETH_DUMP_PATH"), content, 0644)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is overwriting the entire file each time a new address is seen. That seems inefficient, but I suppose it doesn't matter if the total dataset is small enough. I'll approve the overall PR, but if you do see performance issues then consider appending to a file one address at a time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if we change how the file is written, we'll need to make some changes in boba-chain-ops.

if err != nil {
panic(err)
}
}

type noopEthDumper struct {
}

func (n *noopEthDumper) Write(address common.Address) {
}

func init() {
DefaultEthDumper = NewEthDumper()
}

func Write(address common.Address) {
DefaultEthDumper.Write(address)
}
31 changes: 31 additions & 0 deletions l2geth/ethdumper/dumper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ethdumper

import (
"io"
"os"
"testing"

"github.com/ethereum-optimism/optimism/l2geth/common"
)

func TestFileEthDumper(t *testing.T) {
f, err := os.CreateTemp("", "")
if err != nil {
t.Fatalf("error creating file: %v", err)
}
err = os.Setenv("L2GETH_ETH_DUMP_PATH", f.Name())
if err != nil {
t.Fatalf("error setting env file: %v", err)
}
dumper := NewEthDumper()
addr := common.Address{19: 0x01}
dumper.Write(addr)
data, err := io.ReadAll(f)
if err != nil {
t.Fatalf("error reading: %v", err)
}
dataStr := string(data)
if dataStr != `{"addresses":["0x0000000000000000000000000000000000000001"]}` {
t.Fatalf("invalid data. got: %s", dataStr)
}
}
Loading