Cache codes using KVCachedFile#434
Conversation
1ab2162 to
84c80e6
Compare
0ec86af to
a976281
Compare
There was a problem hiding this comment.
Pull request overview
This PR introduces an on-disk caching layer for contract bytecode in the MPT implementation by replacing the in-memory code map with common.KVCachedFile, reducing steady-state memory pressure at the cost of possible file-backed lookups. It also updates the GetCodeForHash API to return an error to reflect potential I/O failures.
Changes:
- Replace in-memory code storage with
KVCachedFile-backed storage with configured cache and flush-buffer thresholds. - Update
GetCodeForHashto return([]byte, error)and propagate errors throughMptState,ArchiveTrie, and export/visitor flows. - Update and extend tests/mocks to validate error forwarding and persistence behavior.
Reviewed changes
Copilot reviewed 8 out of 10 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| go/database/mpt/verification_test.go | Adjusts verification setup for the new codes file behavior. |
| go/database/mpt/state.go | Updates LiveState/MptState code retrieval API to return errors. |
| go/database/mpt/state_test.go | Updates tests and adds cases ensuring codes read errors are forwarded. |
| go/database/mpt/state_mocks.go | Regenerates mocks for the updated GetCodeForHash signature. |
| go/database/mpt/io/live.go | Updates visitor/export path to handle GetCodeForHash errors. |
| go/database/mpt/io/live_mocks.go | Regenerates mocks for updated visitor interface. |
| go/database/mpt/codes.go | Replaces in-memory codes map with KVCachedFile and updates read/write helpers. |
| go/database/mpt/codes_test.go | Updates/extends tests for cached-file-backed codes persistence and lookups. |
| go/database/mpt/archive_trie.go | Updates archive trie code retrieval to propagate errors. |
| go/database/mpt/archive_trie_test.go | Adds tests validating error forwarding from LiveState/GetCodeForHash. |
Files not reviewed (2)
- go/database/mpt/io/live_mocks.go: Generated file
- go/database/mpt/state_mocks.go: Generated file
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
8b09ac7 to
ec123e2
Compare
2e9406a to
eb4ad67
Compare
ec123e2 to
26718d5
Compare
eb4ad67 to
fbe0162
Compare
26718d5 to
398e739
Compare
fbe0162 to
724e408
Compare
724e408 to
876010b
Compare
398e739 to
5e52d3b
Compare
ccbf504 to
58aa66e
Compare
58aa66e to
8cd3be7
Compare
229b751 to
b3bcaa4
Compare
HerbertJordan
left a comment
There was a problem hiding this comment.
Some initial feedback covering things that I think should be addressed before merging this. The changes may also be substantial enough that looking through the current tests in detail is premature, so I skipped that for now. Let me know what you think.
4ce79f3 to
6e49a70
Compare
4666842 to
5bd407d
Compare
6e49a70 to
51b3801
Compare
5bd407d to
4b66aaa
Compare
873acbb to
fabac29
Compare
3c01c44 to
f3ef979
Compare
fabac29 to
f40583a
Compare
f842b7e to
68ca639
Compare
HerbertJordan
left a comment
There was a problem hiding this comment.
Integration looks fine, but some functions still materialize the full code map. Those should be eliminated.
I did not check the unit tests.
783a190 to
da1097e
Compare
6358d41 to
1ea17d0
Compare
In the MPT implementation, all codes are kept in memory in a hash map. This increases memory consumption significantly and can possibly crash Carmen.
This PR adds a caching layer on top of the codes by using the
KVCachedFile. Most of the existing code logic was generalized and moved toOffsetFile, aKVFilewhich stores the offset of each element in memory.The cache is set to 20,000 codes + 2,000 for the flush buffer, which equals to a worst case memory usage of ~1GB.
Notable changes:
GetCodeForHashandGetCodesreturn an error now, and the interface and implementations have been updated accordingly.database/mpt/io/live.gowas still materializing the whole code file in memory. It has been modified to write the codes while scanning them, and renamed towriteReferencedCodesCloses https://github.com/0xsoniclabs/sonic-admin/issues/844