Skip to content
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

Refactor state and contract modules #2224

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

Conversation

weiihann
Copy link
Contributor

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 weiihann changed the title Weiihann/refactor state Refactor state and contract modules Oct 17, 2024
Copy link

codecov bot commented Oct 17, 2024

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).

Files with missing lines Patch % Lines
core/state.go 68.16% 45 Missing and 33 partials ⚠️
migration/migration.go 47.05% 42 Missing and 21 partials ⚠️
core/contract.go 80.43% 10 Missing and 8 partials ⚠️
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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@weiihann weiihann requested review from kirugan and IronGauntlets and removed request for kirugan October 17, 2024 10:17
@pnowosie pnowosie self-requested a review October 28, 2024 12:52
commit f5dc02c
Author: Kirill <paltsev.kir@gmail.com>
Date:   Thu Oct 17 11:15:15 2024 +0400

    Small restructure of plugin logic (#2221)

commit b43c46f
Author: Rian Hughes <rian.hughes@physics.ox.ac.uk>
Date:   Wed Oct 16 15:42:07 2024 +0300

    Support plugins (#2051)

    Co-authored-by: rian <rian@physics.ox.ac.uk>
    Co-authored-by: LordGhostX <solomonghost9@gmail.com>

commit ca006de
Author: Kirill <paltsev.kir@gmail.com>
Date:   Wed Oct 16 16:14:16 2024 +0400

    Replace UnmarshalJSON() with UnmarshalText() for transaction statuses (#2220)

    * Replace UnmarshalJSON() with UnmarshalText() for transaction statuses

    UnmarshalText avoids issues with forgetting quotes in JSON, making it simpler for parsing plain text values.

fix merge conflicts
Copy link
Contributor

@pnowosie pnowosie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

state.go was hard to review I guess because of the method re-ordering and diff looks weird.
Only some minor comments

}

if oldValue != nil {
if err = cb(&key, oldValue); err != nil {
if oldVal != nil && logChanges {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checks the 2nd cond only if we care of logging

Suggested change
if oldVal != nil && logChanges {
if logChanges && oldVal != nil {

@@ -1,134 +0,0 @@
package core
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have the functionality to read contract's historical data now?
I saw we log old values but can we read?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Ok they're implemented in state,go

assert.Equal(t, replacedVal, nonce)

require.NoError(t, state.Revert(2, nonceStateUpdate))
nonce, sErr = state.ContractNonce(new(felt.Felt).Set(&su1FirstDeployedAddress))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to pass a cloned value instead of the original (su1FirstDeployedAddress)
It's a bit hard to read 🤔
Also new(felt.Felt).SetUint64(n) 👇 bellow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants