Skip to content

Commit 925371e

Browse files
authored
refactor(core): new deprecatedstate pkg (#3561)
* refactor(core): copy deprecated state code into new deprecatedstate pkg * refactor(core): remove deprecated state code from core, use new deprecatedstate pkg instead * refactor(core): centralize common contract function in core/deprecatedstate pkgs * ci: fix linter * refactor(core): rename redudant NewDeprecated**State to NewState * refactor(core): create pending pkg * Revert "refactor(core): create pending pkg" This reverts commit adc6811. * refactor(core): rename NewState to New in deprecatedstate package * refactor(deprecatedstate,core): remove redundant functions and call accessors directly * refactor(deprecatedstate): rename NewStateHistory to NewHistory
1 parent 4924bc8 commit 925371e

16 files changed

Lines changed: 302 additions & 259 deletions

File tree

blockchain/statebackend/deprecated.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55

66
"github.com/NethermindEth/juno/core"
7+
"github.com/NethermindEth/juno/core/deprecatedstate"
78
"github.com/NethermindEth/juno/core/felt"
89
"github.com/NethermindEth/juno/db"
910
"github.com/NethermindEth/juno/db/memory"
@@ -28,7 +29,7 @@ func (b *deprecatedStateBackend) StateCommitment() (felt.Felt, error) {
2829
if err != nil {
2930
return felt.Felt{}, err
3031
}
31-
return core.NewDeprecatedState(txn).Commitment(header.ProtocolVersion)
32+
return deprecatedstate.New(txn).Commitment(header.ProtocolVersion)
3233
}
3334

3435
func (b *deprecatedStateBackend) HeadState() (core.StateReader, StateCloser, error) {
@@ -46,7 +47,7 @@ func (b *deprecatedStateBackend) HeadState() (core.StateReader, StateCloser, err
4647
return nil, nil, err
4748
}
4849

49-
return core.NewDeprecatedState(txn), NoopStateCloser, nil
50+
return deprecatedstate.New(txn), NoopStateCloser, nil
5051
}
5152

5253
func (b *deprecatedStateBackend) StateAtBlockNumber(
@@ -61,8 +62,8 @@ func (b *deprecatedStateBackend) StateAtBlockNumber(
6162
return nil, nil, err
6263
}
6364

64-
return core.NewDeprecatedStateHistory(
65-
core.NewDeprecatedState(txn),
65+
return deprecatedstate.NewHistory(
66+
deprecatedstate.New(txn),
6667
blockNumber,
6768
), NoopStateCloser, nil
6869
}
@@ -74,7 +75,7 @@ func (b *deprecatedStateBackend) StateAtBlockHash(
7475
if blockHash.IsZero() {
7576
memDB := memory.New()
7677
txn := memDB.NewIndexedBatch()
77-
return core.NewDeprecatedState(txn), NoopStateCloser, nil
78+
return deprecatedstate.New(txn), NoopStateCloser, nil
7879
}
7980

8081
txn := b.database.NewIndexedBatch() //nolint:staticcheck // indexedBatch used by old state
@@ -83,8 +84,8 @@ func (b *deprecatedStateBackend) StateAtBlockHash(
8384
return nil, nil, err
8485
}
8586

86-
return core.NewDeprecatedStateHistory(
87-
core.NewDeprecatedState(txn),
87+
return deprecatedstate.NewHistory(
88+
deprecatedstate.New(txn),
8889
header.Number,
8990
), NoopStateCloser, nil
9091
}
@@ -100,7 +101,12 @@ func (b *deprecatedStateBackend) Store(
100101
if err := verifyBlockSuccession(txn, block); err != nil {
101102
return err
102103
}
103-
err := core.NewDeprecatedState(txn).Update(block.Header, stateUpdate, newClasses, false)
104+
err := deprecatedstate.New(txn).Update(
105+
block.Header,
106+
stateUpdate,
107+
newClasses,
108+
false,
109+
)
104110
if err != nil {
105111
return err
106112
}
@@ -142,7 +148,7 @@ func (b *deprecatedStateBackend) RevertHead() error {
142148
return err
143149
}
144150

145-
if err = core.NewDeprecatedState(txn).Revert(header, stateUpdate); err != nil {
151+
if err = deprecatedstate.New(txn).Revert(header, stateUpdate); err != nil {
146152
return err
147153
}
148154

@@ -167,8 +173,8 @@ func (b *deprecatedStateBackend) GetReverseStateDiff() (core.StateDiff, error) {
167173
return core.StateDiff{}, err
168174
}
169175

170-
reverseDiff, err := core.
171-
NewDeprecatedState(txn).
176+
reverseDiff, err := deprecatedstate.
177+
New(txn).
172178
GetReverseStateDiff(blockNum, stateUpdate.StateDiff)
173179
if err != nil {
174180
return core.StateDiff{}, err
@@ -186,7 +192,7 @@ func (b *deprecatedStateBackend) Simulate(
186192
txn := b.database.NewIndexedBatch()
187193
defer txn.Close()
188194

189-
err := updateStateRoots(core.NewDeprecatedState(txn), block, stateUpdate, newClasses)
195+
err := updateStateRoots(deprecatedstate.New(txn), block, stateUpdate, newClasses)
190196
if err != nil {
191197
return SimulateResult{}, err
192198
}
@@ -224,7 +230,7 @@ func (b *deprecatedStateBackend) Finalise(
224230
) error {
225231
//nolint:staticcheck,nolintlint // used by old state
226232
err := b.database.Update(func(txn db.IndexedBatch) error {
227-
err := updateStateRoots(core.NewDeprecatedState(txn), block, stateUpdate, newClasses)
233+
err := updateStateRoots(deprecatedstate.New(txn), block, stateUpdate, newClasses)
228234
if err != nil {
229235
return err
230236
}

core/block.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import (
1313
"github.com/sourcegraph/conc"
1414
)
1515

16+
type L1Head struct {
17+
BlockNumber uint64
18+
BlockHash *felt.Felt
19+
StateRoot *felt.Felt
20+
}
21+
1622
type Header struct {
1723
// The hash of this block
1824
Hash *felt.Felt
Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package core
1+
package deprecatedstate
22

33
import (
44
"errors"
55

6-
"github.com/NethermindEth/juno/core/crypto"
6+
"github.com/NethermindEth/juno/core"
77
"github.com/NethermindEth/juno/core/felt"
88
"github.com/NethermindEth/juno/core/trie"
99
"github.com/NethermindEth/juno/db"
@@ -19,6 +19,8 @@ var (
1919

2020
// NewContractUpdater creates an updater for the contract instance at the given address.
2121
// Deploy should be called for contracts that were just deployed to the network.
22+
//
23+
//nolint:staticcheck // Necessary for old state
2224
func NewContractUpdater(addr *felt.Felt, txn db.IndexedBatch) (*ContractUpdater, error) {
2325
contractDeployed, err := deployed(addr, txn)
2426
if err != nil {
@@ -36,6 +38,8 @@ func NewContractUpdater(addr *felt.Felt, txn db.IndexedBatch) (*ContractUpdater,
3638
}
3739

3840
// DeployContract sets up the database for a new contract.
41+
//
42+
//nolint:staticcheck // Necessary for old state
3943
func DeployContract(addr, classHash *felt.Felt, txn db.IndexedBatch) (*ContractUpdater, error) {
4044
contractDeployed, err := deployed(addr, txn)
4145
if err != nil {
@@ -64,28 +68,9 @@ func DeployContract(addr, classHash *felt.Felt, txn db.IndexedBatch) (*ContractU
6468
return c, nil
6569
}
6670

67-
// ContractAddress computes the address of a Starknet contract.
68-
func ContractAddress(
69-
callerAddress,
70-
classHash,
71-
salt *felt.Felt,
72-
constructorCallData []*felt.Felt,
73-
) felt.Felt {
74-
prefix := felt.FromBytes[felt.Felt]([]byte("STARKNET_CONTRACT_ADDRESS"))
75-
callDataHash := crypto.PedersenArray(constructorCallData...)
76-
77-
// https://docs.starknet.io/architecture-and-concepts/smart-contracts/contract-address/
78-
return crypto.PedersenArray(
79-
&prefix,
80-
callerAddress,
81-
salt,
82-
classHash,
83-
&callDataHash,
84-
)
85-
}
86-
71+
//nolint:staticcheck // Necessary for old state
8772
func deployed(addr *felt.Felt, txn db.IndexedBatch) (bool, error) {
88-
_, err := ContractClassHash(addr, txn)
73+
_, err := core.GetContractClassHash(txn, addr)
8974
if errors.Is(err, db.ErrKeyNotFound) {
9075
return false, nil
9176
}
@@ -100,6 +85,7 @@ type ContractUpdater struct {
10085
// Address that this contract instance is deployed to
10186
Address *felt.Felt
10287
// txn to access the database
88+
//nolint:staticcheck // Necessary for old state
10389
txn db.IndexedBatch
10490
}
10591

@@ -118,19 +104,15 @@ func (c *ContractUpdater) Purge() error {
118104
return nil
119105
}
120106

121-
// ContractNonce returns the amount transactions sent from this contract.
122-
// Only account contracts can have a non-zero nonce.
123-
func ContractNonce(addr *felt.Felt, txn db.KeyValueReader) (felt.Felt, error) {
124-
return GetContractNonce(txn, addr)
125-
}
126-
127107
// UpdateNonce updates the nonce value in the database.
128108
func (c *ContractUpdater) UpdateNonce(nonce *felt.Felt) error {
129109
nonceKey := db.ContractNonceKey(c.Address)
130110
return c.txn.Put(nonceKey, nonce.Marshal())
131111
}
132112

133113
// ContractRoot returns the root of the contract storage.
114+
//
115+
//nolint:staticcheck // Necessary for old state
134116
func ContractRoot(addr *felt.Felt, txn db.IndexedBatch) (felt.Felt, error) {
135117
cStorage, err := storage(addr, txn)
136118
if err != nil {
@@ -164,6 +146,7 @@ func (c *ContractUpdater) UpdateStorage(diff map[felt.Felt]*felt.Felt, cb OnValu
164146
return cStorage.Commit()
165147
}
166148

149+
//nolint:staticcheck // Necessary for old state
167150
func ContractStorage(addr, key *felt.Felt, txn db.IndexedBatch) (felt.Felt, error) {
168151
cStorage, err := storage(addr, txn)
169152
if err != nil {
@@ -172,11 +155,7 @@ func ContractStorage(addr, key *felt.Felt, txn db.IndexedBatch) (felt.Felt, erro
172155
return cStorage.Get(key)
173156
}
174157

175-
// ContractClassHash returns hash of the class that the contract at the given address instantiates.
176-
func ContractClassHash(addr *felt.Felt, txn db.KeyValueReader) (felt.Felt, error) {
177-
return GetContractClassHash(txn, addr)
178-
}
179-
158+
//nolint:staticcheck // Necessary for old state
180159
func setClassHash(txn db.IndexedBatch, addr, classHash *felt.Felt) error {
181160
classHashKey := db.ContractClassHashKey(addr)
182161
return txn.Put(classHashKey, classHash.Marshal())
@@ -189,6 +168,8 @@ func (c *ContractUpdater) Replace(classHash *felt.Felt) error {
189168

190169
// storage returns the [core.Trie] that represents the
191170
// storage of the contract.
171+
//
172+
//nolint:staticcheck // Necessary for old state
192173
func storage(addr *felt.Felt, txn db.IndexedBatch) (*trie.Trie, error) {
193174
addrBytes := addr.Marshal()
194175
return trie.NewTriePedersen(txn, db.ContractStorage.Key(addrBytes), ContractStorageTrieHeight)
Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package core
1+
package deprecatedstate
22

33
import (
44
"testing"
@@ -9,44 +9,6 @@ import (
99
"github.com/stretchr/testify/require"
1010
)
1111

12-
func TestContractAddress(t *testing.T) {
13-
tests := []struct {
14-
callerAddress *felt.Felt
15-
classHash *felt.Felt
16-
salt *felt.Felt
17-
constructorCallData []*felt.Felt
18-
want *felt.Felt
19-
}{
20-
{
21-
// https://alpha-mainnet.starknet.io/feeder_gateway/get_transaction?transactionHash=0x6486c6303dba2f364c684a2e9609211c5b8e417e767f37b527cda51e776e6f0
22-
callerAddress: felt.NewUnsafeFromString[felt.Felt]("0x0000000000000000000000000000000000000000"),
23-
classHash: felt.NewUnsafeFromString[felt.Felt](
24-
"0x46f844ea1a3b3668f81d38b5c1bd55e816e0373802aefe732138628f0133486",
25-
),
26-
salt: felt.NewUnsafeFromString[felt.Felt](
27-
"0x74dc2fe193daf1abd8241b63329c1123214842b96ad7fd003d25512598a956b",
28-
),
29-
constructorCallData: []*felt.Felt{
30-
felt.NewUnsafeFromString[felt.Felt]("0x6d706cfbac9b8262d601c38251c5fbe0497c3a96cc91a92b08d91b61d9e70c4"),
31-
felt.NewUnsafeFromString[felt.Felt]("0x79dc0da7c54b95f10aa182ad0a46400db63156920adb65eca2654c0945a463"),
32-
felt.NewUnsafeFromString[felt.Felt]("0x2"),
33-
felt.NewUnsafeFromString[felt.Felt]("0x6658165b4984816ab189568637bedec5aa0a18305909c7f5726e4a16e3afef6"),
34-
felt.NewUnsafeFromString[felt.Felt]("0x6b648b36b074a91eee55730f5f5e075ec19c0a8f9ffb0903cefeee93b6ff328"),
35-
},
36-
want: felt.NewUnsafeFromString[felt.Felt]("0x3ec215c6c9028ff671b46a2a9814970ea23ed3c4bcc3838c6d1dcbf395263c3"),
37-
},
38-
}
39-
40-
for _, tt := range tests {
41-
t.Run("Address", func(t *testing.T) {
42-
address := ContractAddress(tt.callerAddress, tt.classHash, tt.salt, tt.constructorCallData)
43-
if !address.Equal(tt.want) {
44-
t.Errorf("wrong address: got %s, want %s", address.String(), tt.want.String())
45-
}
46-
})
47-
}
48-
}
49-
5012
func TestNewContractUpdater(t *testing.T) {
5113
testDB := memory.New()
5214
txn := testDB.NewIndexedBatch()

0 commit comments

Comments
 (0)