Skip to content

Commit 8ecc863

Browse files
committed
chore: cleanups
1 parent 9534e34 commit 8ecc863

8 files changed

Lines changed: 133 additions & 70 deletions

File tree

node/node.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ func New(cfg *Config, version string, logLevel *log.Level) (*Node, error) {
209209
opts = append(opts, blockchain.WithListener(makeBlockchainMetrics()))
210210
}
211211
if cfg.NewState {
212-
opts = append(opts, blockchain.WithNewState(true))
212+
opts = append(opts, blockchain.WithNewState(
213+
cfg.NewState,
214+
))
213215
}
214216
chain := blockchain.New(database, &cfg.Network, opts...)
215217

rpc/v10/storage.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,6 @@ func (h *Handler) isBlockSupported(blockID *BlockID, chainHeight uint64) *jsonrp
305305
}
306306

307307
func getClassProof(tr core.Trie, classes []felt.Felt) ([]*HashToNode, error) {
308-
// TODO(maksym): remove after trie2 integration. RPC packages shouldn't
309-
// care about which trie implementation is being used and the output format should be the same
310308
switch t := tr.(type) {
311309
case *trie.Trie:
312310
classProof := trie.NewProofNodeSet()
@@ -323,7 +321,7 @@ func getClassProof(tr core.Trie, classes []felt.Felt) ([]*HashToNode, error) {
323321
return nil, err
324322
}
325323
}
326-
return adaptTrieProofNodes(classProof), nil
324+
return adaptTrieProofNodes(classProof)
327325
default:
328326
return nil, fmt.Errorf("unknown trie type: %T", tr)
329327
}
@@ -334,8 +332,6 @@ func getContractProof(
334332
state core.StateReader,
335333
contracts []felt.Felt,
336334
) (*ContractProof, error) {
337-
// TODO(maksym): remove after trie2 integration. RPC packages shouldn't
338-
// care about which trie implementation is being used and the output format should be the same
339335
switch t := tr.(type) {
340336
case *trie.Trie:
341337
return getContractProofWithDeprecatedTrie(t, state, contracts)
@@ -386,8 +382,13 @@ func getContractProofWithTrie(
386382
return nil, err
387383
}
388384

385+
nodes, err := adaptTrieProofNodes(contractProof)
386+
if err != nil {
387+
return nil, err
388+
}
389+
389390
return &ContractProof{
390-
Nodes: adaptTrieProofNodes(contractProof),
391+
Nodes: nodes,
391392
LeavesData: contractLeavesData,
392393
}, nil
393394
}
@@ -399,7 +400,8 @@ func buildContractLeavesData(
399400
contractLeavesData := make([]*LeafData, len(contracts))
400401
for i, contract := range contracts {
401402
classHash, err := state.ContractClassHash(&contract)
402-
if err != nil { // contract does not exist, skip getting leaf data
403+
if err != nil {
404+
// contract does not exist, skip getting leaf data
403405
if errors.Is(err, db.ErrKeyNotFound) {
404406
continue
405407
}
@@ -457,7 +459,11 @@ func getContractStorageProof(
457459
return nil, err
458460
}
459461
}
460-
contractStorageRes[i] = adaptTrieProofNodes(contractStorageProof)
462+
nodes, err := adaptTrieProofNodes(contractStorageProof)
463+
if err != nil {
464+
return nil, err
465+
}
466+
contractStorageRes[i] = nodes
461467
default:
462468
return nil, fmt.Errorf("unknown trie type: %T", contractStorageTrie)
463469
}
@@ -496,24 +502,37 @@ func adaptDeprecatedTrieProofNodes(proof *trie.ProofNodeSet) []*HashToNode {
496502
return nodes
497503
}
498504

499-
func adaptTrieProofNodes(proof *trie2.ProofNodeSet) []*HashToNode {
505+
func adaptTrieProofNodes(proof *trie2.ProofNodeSet) ([]*HashToNode, error) {
500506
nodes := make([]*HashToNode, proof.Size())
501507
nodeList := proof.List()
502508
for i, hash := range proof.Keys() {
503509
var node Node
504510

505511
switch n := nodeList[i].(type) {
506512
case *trienode.BinaryNode:
513+
leftChild, err := nodeFelt(n.Children[0])
514+
if err != nil {
515+
return nil, err
516+
}
517+
rightChild, err := nodeFelt(n.Children[1])
518+
if err != nil {
519+
return nil, err
520+
}
507521
node = &BinaryNode{
508-
Left: nodeFelt(n.Children[0]),
509-
Right: nodeFelt(n.Children[1]),
522+
Left: &leftChild,
523+
Right: &rightChild,
510524
}
511525
case *trienode.EdgeNode:
512526
pathFelt := n.Path.Felt()
527+
child, err := nodeFelt(n.Child)
528+
if err != nil {
529+
return nil, err
530+
}
531+
513532
node = &EdgeNode{
514533
Path: pathFelt.String(),
515534
Length: int(n.Path.Len()),
516-
Child: nodeFelt(n.Child),
535+
Child: &child,
517536
}
518537
}
519538

@@ -523,17 +542,17 @@ func adaptTrieProofNodes(proof *trie2.ProofNodeSet) []*HashToNode {
523542
}
524543
}
525544

526-
return nodes
545+
return nodes, nil
527546
}
528547

529-
func nodeFelt(n trienode.Node) *felt.Felt {
548+
func nodeFelt(n trienode.Node) (felt.Felt, error) {
530549
switch n := n.(type) {
531550
case *trienode.HashNode:
532-
return (*felt.Felt)(n)
551+
return felt.Felt(*n), nil
533552
case *trienode.ValueNode:
534-
return (*felt.Felt)(n)
553+
return felt.Felt(*n), nil
535554
default:
536-
panic(fmt.Sprintf("unknown node type: %T", n))
555+
return felt.Felt{}, fmt.Errorf("unknown node type: %T", n)
537556
}
538557
}
539558

rpc/v10/storage_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ func TestStorageProof(t *testing.T) {
573573
var trieRoot felt.Felt
574574

575575
if !statetestutils.UseNewState() {
576-
tempTrie := emptyTrie(t)
576+
tempTrie := emptyDeprecatedTrie(t)
577577
_, _ = tempTrie.Put(key, value)
578578
_, _ = tempTrie.Put(key2, value2)
579579
_ = tempTrie.Commit()
@@ -862,7 +862,7 @@ func TestStorageProof(t *testing.T) {
862862

863863
// Build a separate storage trie with different contents so its root differs from
864864
// the contracts trie root.
865-
contractStorageTrie := emptyTrie(t)
865+
contractStorageTrie := emptyDeprecatedTrie(t)
866866
storageKey := felt.NewFromUint64[felt.Felt](99)
867867
storageVal := felt.NewFromUint64[felt.Felt](999)
868868
_, _ = contractStorageTrie.Put(storageKey, storageVal)
@@ -896,7 +896,7 @@ func TestStorageProof(t *testing.T) {
896896
Return(headBlock.Header, nil)
897897

898898
contract := felt.NewFromUint64[felt.Felt](0xdead)
899-
mockState.EXPECT().ContractStorageTrie(contract).Return(emptyCommonTrie(t), nil).Times(1)
899+
mockState.EXPECT().ContractStorageTrie(contract).Return(emptyTrie(t), nil).Times(1)
900900

901901
storageKeys := []rpc.StorageKeys{{Contract: contract, Keys: []felt.Felt{*key}}}
902902
proof, rpcErr := handler.StorageProof(&blockLatest, nil, nil, storageKeys)
@@ -1573,7 +1573,7 @@ func verifyIf(
15731573
require.Equal(t, leaf, *value)
15741574
}
15751575

1576-
func emptyTrie(t *testing.T) *trie.Trie {
1576+
func emptyDeprecatedTrie(t *testing.T) *trie.Trie {
15771577
memdb := memory.New()
15781578
txn := memdb.NewIndexedBatch()
15791579

@@ -1582,13 +1582,13 @@ func emptyTrie(t *testing.T) *trie.Trie {
15821582
return tempTrie
15831583
}
15841584

1585-
func emptyCommonTrie(t *testing.T) core.Trie {
1585+
func emptyTrie(t *testing.T) core.Trie {
15861586
if statetestutils.UseNewState() {
15871587
tempTrie, err := trie2.NewEmptyPedersen()
15881588
require.NoError(t, err)
15891589
return tempTrie
15901590
}
1591-
return emptyTrie(t)
1591+
return emptyDeprecatedTrie(t)
15921592
}
15931593

15941594
func verifyGlobalStateRoot(t *testing.T, globalStateRoot, classRoot, storageRoot *felt.Felt) {

rpc/v8/storage.go

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func getClassProof(tr core.Trie, classes []felt.Felt) ([]*HashToNode, error) {
226226
return nil, err
227227
}
228228
}
229-
return adaptTrieProofNodes(classProof), nil
229+
return adaptTrieProofNodes(classProof)
230230
default:
231231
return nil, fmt.Errorf("unknown trie type: %T", tr)
232232
}
@@ -255,7 +255,8 @@ func buildContractLeavesData(
255255

256256
for i, contract := range contracts {
257257
classHash, err := state.ContractClassHash(&contract)
258-
if err != nil { // contract does not exist, skip getting leaf data
258+
if err != nil {
259+
// contract does not exist, skip getting leaf data
259260
if errors.Is(err, db.ErrKeyNotFound) {
260261
continue
261262
}
@@ -329,8 +330,13 @@ func getContractProofWithTrie(
329330
return nil, err
330331
}
331332

333+
nodes, err := adaptTrieProofNodes(contractProof)
334+
if err != nil {
335+
return nil, err
336+
}
337+
332338
return &ContractProof{
333-
Nodes: adaptTrieProofNodes(contractProof),
339+
Nodes: nodes,
334340
LeavesData: contractLeavesData,
335341
}, nil
336342
}
@@ -362,7 +368,13 @@ func getContractStorageProof(
362368
return nil, err
363369
}
364370
}
365-
contractStorageRes[i] = adaptTrieProofNodes(contractStorageProof)
371+
372+
nodes, err := adaptTrieProofNodes(contractStorageProof)
373+
if err != nil {
374+
return nil, err
375+
}
376+
377+
contractStorageRes[i] = nodes
366378
default:
367379
return nil, fmt.Errorf("unknown trie type: %T", contractStorageTrie)
368380
}
@@ -401,24 +413,36 @@ func adaptDeprecatedTrieProofNodes(proof *trie.ProofNodeSet) []*HashToNode {
401413
return nodes
402414
}
403415

404-
func adaptTrieProofNodes(proof *trie2.ProofNodeSet) []*HashToNode {
416+
func adaptTrieProofNodes(proof *trie2.ProofNodeSet) ([]*HashToNode, error) {
405417
nodes := make([]*HashToNode, proof.Size())
406418
nodeList := proof.List()
407419
for i, hash := range proof.Keys() {
408420
var node Node
409421

410422
switch n := nodeList[i].(type) {
411423
case *trienode.BinaryNode:
424+
leftChild, err := nodeFelt(n.Children[0])
425+
if err != nil {
426+
return nil, err
427+
}
428+
rightChild, err := nodeFelt(n.Children[1])
429+
if err != nil {
430+
return nil, err
431+
}
412432
node = &BinaryNode{
413-
Left: nodeFelt(n.Children[0]),
414-
Right: nodeFelt(n.Children[1]),
433+
Left: &leftChild,
434+
Right: &rightChild,
415435
}
416436
case *trienode.EdgeNode:
417437
pathFelt := n.Path.Felt()
438+
child, err := nodeFelt(n.Child)
439+
if err != nil {
440+
return nil, err
441+
}
418442
node = &EdgeNode{
419443
Path: pathFelt.String(),
420444
Length: int(n.Path.Len()),
421-
Child: nodeFelt(n.Child),
445+
Child: &child,
422446
}
423447
}
424448

@@ -428,17 +452,17 @@ func adaptTrieProofNodes(proof *trie2.ProofNodeSet) []*HashToNode {
428452
}
429453
}
430454

431-
return nodes
455+
return nodes, nil
432456
}
433457

434-
func nodeFelt(n trienode.Node) *felt.Felt {
458+
func nodeFelt(n trienode.Node) (felt.Felt, error) {
435459
switch n := n.(type) {
436460
case *trienode.HashNode:
437-
return (*felt.Felt)(n)
461+
return felt.Felt(*n), nil
438462
case *trienode.ValueNode:
439-
return (*felt.Felt)(n)
463+
return felt.Felt(*n), nil
440464
default:
441-
panic(fmt.Sprintf("unknown node type: %T", n))
465+
return felt.Zero, fmt.Errorf("unknown node type: %T", n)
442466
}
443467
}
444468

rpc/v8/storage_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func TestStorageProof(t *testing.T) {
221221
trieRoot := felt.Zero
222222

223223
if !statetestutils.UseNewState() {
224-
tempTrie := emptyTrie(t)
224+
tempTrie := emptyDeprecatedTrie(t)
225225
_, _ = tempTrie.Put(key, value)
226226
_, _ = tempTrie.Put(key2, value2)
227227
_ = tempTrie.Commit()
@@ -486,7 +486,7 @@ func TestStorageProof(t *testing.T) {
486486
Return(headBlock.Header, nil)
487487

488488
// Build a separate storage trie with different contents so its root differs from the contracts trie root.
489-
contractStorageTrie := emptyTrie(t)
489+
contractStorageTrie := emptyDeprecatedTrie(t)
490490
storageKey := felt.NewFromUint64[felt.Felt](99)
491491
storageVal := felt.NewFromUint64[felt.Felt](999)
492492
_, _ = contractStorageTrie.Put(storageKey, storageVal)
@@ -519,7 +519,7 @@ func TestStorageProof(t *testing.T) {
519519
mockReader.EXPECT().BlockHeaderByNumber(blockNumber).
520520
Return(headBlock.Header, nil)
521521
contract := felt.NewUnsafeFromString[felt.Felt]("0xdead")
522-
mockState.EXPECT().ContractStorageTrie(contract).Return(emptyCommonTrie(t), nil).Times(1)
522+
mockState.EXPECT().ContractStorageTrie(contract).Return(emptyTrie(t), nil).Times(1)
523523

524524
storageKeys := []rpc.StorageKeys{{Contract: contract, Keys: []felt.Felt{*key}}}
525525
proof, rpcErr := handler.StorageProof(&blockLatest, nil, nil, storageKeys)
@@ -984,7 +984,7 @@ func verifyIf(
984984
require.Equal(t, leaf, *value)
985985
}
986986

987-
func emptyTrie(t *testing.T) *trie.Trie {
987+
func emptyDeprecatedTrie(t *testing.T) *trie.Trie {
988988
memdb := memory.New()
989989
txn := memdb.NewIndexedBatch()
990990

@@ -993,13 +993,13 @@ func emptyTrie(t *testing.T) *trie.Trie {
993993
return tempTrie
994994
}
995995

996-
func emptyCommonTrie(t *testing.T) core.Trie {
996+
func emptyTrie(t *testing.T) core.Trie {
997997
if statetestutils.UseNewState() {
998998
tempTrie, err := trie2.NewEmptyPedersen()
999999
require.NoError(t, err)
10001000
return tempTrie
10011001
}
1002-
return emptyTrie(t)
1002+
return emptyDeprecatedTrie(t)
10031003
}
10041004

10051005
func verifyGlobalStateRoot(t *testing.T, globalStateRoot, classRoot, storageRoot *felt.Felt) {

0 commit comments

Comments
 (0)