Skip to content

Commit c79a235

Browse files
committed
fix(state): unify the errors with the old state for backward compatibility
1 parent b48ef4b commit c79a235

11 files changed

Lines changed: 34 additions & 166 deletions

File tree

core/deprecatedstate/state_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestUpdate(t *testing.T) {
5252

5353
t.Run("error when state current root doesn't match state update's old root",
5454
func(t *testing.T) {
55-
oldRoot := new(felt.Felt).SetBytes([]byte("some old root"))
55+
oldRoot := felt.NewFromBytes[felt.Felt]([]byte("some old root"))
5656
su := &core.StateUpdate{
5757
OldRoot: oldRoot,
5858
}
@@ -65,7 +65,7 @@ func TestUpdate(t *testing.T) {
6565
})
6666

6767
t.Run("error when state new root doesn't match state update's new root", func(t *testing.T) {
68-
newRoot := new(felt.Felt).SetBytes([]byte("some new root"))
68+
newRoot := felt.NewFromBytes[felt.Felt]([]byte("some new root"))
6969
su := &core.StateUpdate{
7070
NewRoot: newRoot,
7171
OldRoot: su0.NewRoot,
@@ -116,7 +116,7 @@ func TestUpdate(t *testing.T) {
116116
scValue := felt.NewUnsafeFromString[felt.Felt](
117117
"0x10979c6b0b36b03be36739a21cc43a51076545ce6d3397f1b45c7e286474ad5",
118118
)
119-
scAddr := new(felt.Felt).SetUint64(1)
119+
scAddr := &felt.One
120120

121121
su4 := &core.StateUpdate{
122122
OldRoot: su3.NewRoot,
@@ -249,7 +249,7 @@ func TestNonce(t *testing.T) {
249249
})
250250

251251
t.Run("update contract nonce", func(t *testing.T) {
252-
expectedNonce := new(felt.Felt).SetUint64(1)
252+
expectedNonce := &felt.One
253253
su = &core.StateUpdate{
254254
NewRoot: felt.NewUnsafeFromString[felt.Felt](
255255
"0x6210642ffd49f64617fc9e5c0bbe53a6a92769e2996eb312a42d2bdb7f2afc1",

core/state/accessors.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package state
22

33
import (
4-
"errors"
54
"fmt"
65

76
"github.com/NethermindEth/juno/core"
@@ -19,21 +18,10 @@ func GetStateObject(r db.KeyValueReader, state *State, addr *felt.Felt) (*stateO
1918
return &obj, nil
2019
}
2120

22-
// Gets a contract instance from the database. If it doesn't exist returns a contract not deployed error
23-
func GetContract(r db.KeyValueReader, addr *felt.Felt) (stateContract, error) {
24-
contract, err := getContract(r, addr)
25-
if err != nil {
26-
if errors.Is(err, db.ErrKeyNotFound) {
27-
return stateContract{}, ErrContractNotDeployed
28-
}
29-
return stateContract{}, err
30-
}
31-
32-
return contract, nil
33-
}
34-
3521
// Gets a contract instance from the database.
36-
func getContract(r db.KeyValueReader, addr *felt.Felt) (stateContract, error) {
22+
// Returns db.ErrKeyNotFound when the contract does not exist, for backward compatibility.
23+
// TODO: return more precise error (e.g. ErrContractNotDeployed) once deprecated state is removed.
24+
func GetContract(r db.KeyValueReader, addr *felt.Felt) (stateContract, error) {
3725
key := db.ContractKey(addr)
3826
var contract stateContract
3927
if err := r.Get(key, func(val []byte) error {

core/state/errors.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
)
66

77
var (
8-
ErrContractNotDeployed = errors.New("contract not deployed")
98
ErrContractAlreadyDeployed = errors.New("contract already deployed")
109
ErrNoHistoryValue = errors.New("no history value found")
1110
ErrHistoricalTrieNotSupported = errors.New("cannot support historical trie")

core/state/factory/state_factory.go

Lines changed: 0 additions & 68 deletions
This file was deleted.

core/state/factory/state_factory_test.go

Lines changed: 0 additions & 66 deletions
This file was deleted.

core/state/history.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ func (s *stateHistory) checkDeployed(addr *felt.Felt) error {
7676
}
7777

7878
if !isDeployed {
79-
// TODO(weiihann): previously this was db.ErrKeyNotFound
80-
// remember to handle it in the rpc
81-
return ErrContractNotDeployed
79+
// TODO: return a dedicated error like ErrContractNotFound
80+
// once deprecated state is removed. Using db.ErrKeyNotFound
81+
// for backward compatibility.
82+
return db.ErrKeyNotFound
8283
}
8384

8485
return nil

core/state/history_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func TestStateHistoryContractOperations(t *testing.T) {
122122
t.Run("NonExistentContract", func(t *testing.T) {
123123
nonExistentAddr := new(felt.Felt).SetUint64(999)
124124
_, err := historyBlock0.ContractClassHash(nonExistentAddr)
125-
assert.ErrorIs(t, err, ErrContractNotDeployed)
125+
assert.ErrorIs(t, err, db.ErrKeyNotFound)
126126
})
127127
}
128128

core/state/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ func (s *State) updateContractStorage(blockNum uint64, storage map[felt.Felt]map
497497
for addr, storage := range storage {
498498
obj, err := s.getStateObject(&addr)
499499
if err != nil {
500-
if _, ok := noClassContracts[addr]; ok && errors.Is(err, ErrContractNotDeployed) {
500+
if _, ok := noClassContracts[addr]; ok && errors.Is(err, db.ErrKeyNotFound) {
501501
contract := newContractDeployed(noClassContractsClassHash, blockNum)
502502
newObj := newStateObject(s, &addr, &contract)
503503
obj = &newObj

core/state/state_reader.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (s *StateReader) ContractStorageLastUpdatedBlock(
9292
func (s *StateReader) ContractDeployedAt(addr *felt.Felt, blockNum uint64) (bool, error) {
9393
contract, err := GetContract(s.db.disk, addr)
9494
if err != nil {
95-
if errors.Is(err, ErrContractNotDeployed) {
95+
if errors.Is(err, db.ErrKeyNotFound) {
9696
return false, nil
9797
}
9898
return false, err
@@ -272,7 +272,17 @@ func (s *StateReader) valueAt(prefix []byte, blockNum uint64, cb func(val []byte
272272

273273
seekKey := binary.BigEndian.AppendUint64(prefix, blockNum)
274274
if !it.Seek(seekKey) {
275-
return ErrNoHistoryValue
275+
// All existing keys (if any) are before seekKey.
276+
// The last stored value is the correct answer for this blockNum.
277+
if !it.Prev() {
278+
// Iterator is truly empty for this prefix — no history exists.
279+
return ErrNoHistoryValue
280+
}
281+
val, err := it.Value()
282+
if err != nil {
283+
return err
284+
}
285+
return cb(val)
276286
}
277287

278288
key := it.Key()

core/state/state_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestUpdate(t *testing.T) {
6363
scValue := felt.NewUnsafeFromString[felt.Felt](
6464
"0x10979c6b0b36b03be36739a21cc43a51076545ce6d3397f1b45c7e286474ad5",
6565
)
66-
scAddr := felt.NewFromUint64[felt.Felt](1)
66+
scAddr := &felt.One
6767

6868
stateUpdates := getStateUpdates(t)
6969

@@ -175,7 +175,7 @@ func TestUpdate(t *testing.T) {
175175
state, err := New(stateUpdates[4].NewRoot, stateDB, batch)
176176
require.NoError(t, err)
177177
err = state.Update(&core.Header{Number: block5}, su5, nil, false)
178-
require.ErrorIs(t, err, ErrContractNotDeployed)
178+
require.ErrorIs(t, err, db.ErrKeyNotFound)
179179
})
180180
}
181181

@@ -306,7 +306,9 @@ func TestRevert(t *testing.T) {
306306
require.NoError(t, state.Revert(&core.Header{Number: block2}, storageStateUpdate))
307307
require.NoError(t, batch.Write())
308308

309-
storage, sErr := state.ContractStorage(&su1FirstDeployedAddress, replacedVal)
309+
reader, err = NewStateReader(storageStateUpdate.OldRoot, stateDB)
310+
require.NoError(t, err)
311+
storage, sErr := reader.ContractStorage(&su1FirstDeployedAddress, replacedVal)
310312
require.NoError(t, sErr)
311313
assert.Equal(t, felt.Zero, storage)
312314
})

0 commit comments

Comments
 (0)