-
Notifications
You must be signed in to change notification settings - Fork 883
fix(evmrpc): cap StoreTracer access log to prevent debug_traceStateAccess issue (PLT-360) #3653
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
Open
amir-deris
wants to merge
4
commits into
main
Choose a base branch
from
amir/plt-360-debug-traceStateAccess-memory
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+179
−17
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
33a9fd4
Added cap on module trace access records, added test
amir-deris b08adc1
Added test to confirm stats remain exact under truncation
amir-deris f01964a
Merge branch 'main' into amir/plt-360-debug-traceStateAccess-memory
amir-deris c9db9ee
Updated comment
amir-deris 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 hidden or 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 hidden or 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,116 @@ | ||
| package types | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| // retainedBytes sums the key/value payload the tracer holds until the trace | ||
| // response is built. | ||
| func retainedBytes(st *StoreTracer) int { | ||
| total := 0 | ||
| for _, mt := range st.Modules { | ||
| for _, a := range mt.Accesses { | ||
| total += len(a.Key) + len(a.Value) | ||
| } | ||
| for _, it := range mt.Iterators { | ||
| total += len(it.Start) + len(it.End) | ||
| for _, k := range it.Keys { | ||
| total += len(k) | ||
| } | ||
| } | ||
| } | ||
| return total | ||
| } | ||
|
|
||
| // TestStoreTracerBoundsIteratorScanMemory reproduces the blowup: mt.Accesses | ||
| // is uncapped, so a large iterator scan clones one key/value per surfaced key | ||
| // and retained memory grows linearly with scan size. The 8 MiB budget matches | ||
| // the file's "a few MB per module" intent. | ||
| func TestStoreTracerBoundsIteratorScanMemory(t *testing.T) { | ||
| const ( | ||
| module = "evm" | ||
| numKeys = 100_000 // a large-but-plausible scan over a big store | ||
| valueSize = 512 // bytes per surfaced value | ||
|
|
||
| maxRetainedBytes = 8 << 20 // 8 MiB | ||
| ) | ||
|
|
||
| st := NewStoreTracer() | ||
| iterID := st.StartIterator([]byte("start"), []byte("end"), true, module, 0) | ||
|
|
||
| value := make([]byte, valueSize) | ||
| for i := range numKeys { | ||
| key := []byte{byte(i), byte(i >> 8), byte(i >> 16)} | ||
| st.RecordIteratorNext(iterID, module, time.Microsecond) | ||
| st.RecordIteratorValue(iterID, key, value, module) | ||
| } | ||
|
|
||
| got := retainedBytes(st) | ||
| if got > maxRetainedBytes { | ||
| t.Fatalf("StoreTracer retained %d bytes after scanning %d keys; want <= %d.\n"+ | ||
| "mt.Accesses is unbounded: each IteratorValue clones key+value into the "+ | ||
| "access log regardless of the %d-key iterator cap, so memory grows linearly "+ | ||
| "with the scan size.", | ||
| got, numKeys, maxRetainedBytes, maxStoreTraceIteratorKeys) | ||
| } | ||
| } | ||
|
|
||
| // TestStoreTracerStatsAccurateWhenTruncated drives Get past | ||
| // maxStoreTraceModuleBytes and asserts that per-op stats (Count/TotalNanos) still | ||
| // count every access once the log is Truncated, while Reads keeps only the | ||
| // retained prefix. These stats feed historicalLookupNanos (evmrpc/trace_profile.go). | ||
| func TestStoreTracerStatsAccurateWhenTruncated(t *testing.T) { | ||
| const ( | ||
| module = "evm" | ||
| numGets = 5_000 // each retained Get costs valueSize+keyLen+overhead; | ||
| valueSize = 4096 // ~4 KiB per access truncates well before all 5k retain | ||
| perGet = time.Microsecond // known per-access duration to verify TotalNanos | ||
| ) | ||
|
|
||
| st := NewStoreTracer() | ||
| value := make([]byte, valueSize) | ||
| for i := range numGets { | ||
| // Distinct keys so each retained Get is a distinct entry in Reads. | ||
| key := []byte{byte(i), byte(i >> 8), byte(i >> 16)} | ||
| st.Get(key, value, module, perGet) | ||
| } | ||
|
|
||
| mt, ok := st.Modules[module] | ||
| if !ok { | ||
| t.Fatalf("module %q missing from tracer", module) | ||
| } | ||
| if !mt.Truncated { | ||
| t.Fatalf("module not Truncated after %d Gets of %d-byte values; cap %d should have been exceeded", | ||
| numGets, valueSize, maxStoreTraceModuleBytes) | ||
| } | ||
| if len(mt.Accesses) >= numGets { | ||
| t.Fatalf("retained %d accesses; truncation should drop some of the %d Gets", | ||
| len(mt.Accesses), numGets) | ||
| } | ||
|
|
||
| // Internal running stats must count every access, truncated or not. | ||
| if got := mt.stats[Get]; got.Count != numGets || got.TotalNanos != int64(numGets)*perGet.Nanoseconds() { | ||
| t.Fatalf("module stats under truncation = {Count:%d TotalNanos:%d}; want {Count:%d TotalNanos:%d}", | ||
| got.Count, got.TotalNanos, numGets, int64(numGets)*perGet.Nanoseconds()) | ||
| } | ||
|
|
||
| // The wire dump (what historicalLookupNanos reads) must agree, and Reads | ||
| // must reflect only the retained prefix. | ||
| d := st.Dump() | ||
| mtd := d.Modules[module] | ||
| if !mtd.Truncated { | ||
| t.Fatal("dump module not flagged Truncated") | ||
| } | ||
| if got := mtd.Stats[Get.String()]; got.Count != numGets || got.TotalNanos != int64(numGets)*perGet.Nanoseconds() { | ||
| t.Fatalf("dump module stats = {Count:%d TotalNanos:%d}; want {Count:%d TotalNanos:%d}", | ||
| got.Count, got.TotalNanos, numGets, int64(numGets)*perGet.Nanoseconds()) | ||
| } | ||
| if top := d.Stats[Get.String()]; top.Count != numGets || top.TotalNanos != int64(numGets)*perGet.Nanoseconds() { | ||
| t.Fatalf("dump top-level stats = {Count:%d TotalNanos:%d}; want {Count:%d TotalNanos:%d}", | ||
| top.Count, top.TotalNanos, numGets, int64(numGets)*perGet.Nanoseconds()) | ||
| } | ||
| if len(mtd.Reads) >= numGets { | ||
| t.Fatalf("dump retained %d reads; truncation should bound this below %d", len(mtd.Reads), numGets) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.