-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ðAddres) | ||
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) | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |