-
Notifications
You must be signed in to change notification settings - Fork 61
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
Add ETH dump #1373
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.