juno icon indicating copy to clipboard operation
juno copied to clipboard

Refactor state and contract modules

Open weiihann opened this issue 1 year ago • 1 comments

Description

This PR has 2 major changes:

  1. Combine ContractNonce, ContractClassHash, and ContractDeploymentHeight into a single bucket called Contract. Separation of the contract fields in different buckets creates data duplication. Instead, we can encode them into a single object and store them in a single bucket. The slight performance degradation of encoding/decoding is insignificant as the fields are small. Migration codes are added along with the unit tests.

  2. Refactor State.Update, State.Revert and the removal of history. In the previous implementation, after applying each state diff (i.e. nonce, class hash, storage), the contract commitment is recalculated. This indicates that the higher the number of individual state diffs, the more times we need to recalculate the contract commitment. In this new implementation, a contract's commitment is calculated only once after all state diffs are applied to a contract object. Now, the number of contract commitment calculations depends on the number of contracts updated.

Benchmark Results

goos: darwin
goarch: arm64
pkg: github.com/NethermindEth/juno/core
cpu: Apple M3 Pro
               │   new.txt   │               old.txt               │
               │   sec/op    │   sec/op     vs base                │
StateUpdate-11   3.316m ± 2%   4.146m ± 2%  +25.03% (p=0.000 n=10)

               │   new.txt    │               old.txt                │
               │     B/op     │     B/op      vs base                │
StateUpdate-11   144.1Ki ± 0%   182.4Ki ± 0%  +26.53% (p=0.000 n=10)

               │   new.txt   │               old.txt               │
               │  allocs/op  │  allocs/op   vs base                │
StateUpdate-11   3.037k ± 0%   4.056k ± 0%  +33.54% (p=0.000 n=10)

weiihann avatar Oct 17 '24 09:10 weiihann

Codecov Report

Attention: Patch coverage is 65.50976% with 159 lines in your changes missing coverage. Please review.

Project coverage is 75.35%. Comparing base (3093ca8) to head (c466cca). Report is 202 commits behind head on main.

Files with missing lines Patch % Lines
core/state.go 68.16% 45 Missing and 33 partials :warning:
migration/migration.go 47.05% 42 Missing and 21 partials :warning:
core/contract.go 80.43% 10 Missing and 8 partials :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2224      +/-   ##
==========================================
- Coverage   75.63%   75.35%   -0.28%     
==========================================
  Files         104      103       -1     
  Lines       11102    11168      +66     
==========================================
+ Hits         8397     8416      +19     
- Misses       2073     2104      +31     
- Partials      632      648      +16     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Oct 17 '24 10:10 codecov[bot]

This pull request is stale because it has been open 35 days with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] avatar Dec 29 '24 00:12 github-actions[bot]

Latest changes here

weiihann avatar Feb 25 '25 02:02 weiihann