Skip to content

Commit

Permalink
[WIP] Linter
Browse files Browse the repository at this point in the history
  • Loading branch information
pnowosie committed Aug 14, 2024
1 parent 90b7397 commit 806f194
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 172 deletions.
7 changes: 0 additions & 7 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,13 +829,6 @@ func (b *Blockchain) HeadStateFreakingState() (*core.State, StateCloser, error)
return nil, nil, err
}

/*
_, err = chainHeight(txn)
if err != nil {
return nil, nil, utils.RunAndWrapOnError(txn.Discard, err)
}
*/

return core.NewState(txn), txn.Discard, nil
}

Expand Down
9 changes: 6 additions & 3 deletions blockchain/snap_server_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/NethermindEth/juno/db"
)

const MaxSnapshots = 128

type snapshotRecord struct {
stateRoot *felt.Felt
contractsRoot *felt.Felt
Expand Down Expand Up @@ -108,7 +110,7 @@ func (b *Blockchain) seedSnapshot() error {
return err
}

defer srCloser()
defer func() { _ = srCloser() }()

state := stateR.(*core.State)
contractsRoot, classRoot, err := state.StateAndClassRoot()
Expand Down Expand Up @@ -139,7 +141,7 @@ func (b *Blockchain) seedSnapshot() error {

// TODO: Reorgs
b.snapshots = append(b.snapshots, &dbsnap)
if len(b.snapshots) > 128 {
if len(b.snapshots) > MaxSnapshots {
toremove := b.snapshots[0]
err = toremove.closer()
if err != nil {
Expand All @@ -156,6 +158,7 @@ func (b *Blockchain) seedSnapshot() error {

func (b *Blockchain) Close() {
for _, snapshot := range b.snapshots {
snapshot.closer()
// ignore the errors here as it's most likely called on shutdown
_ = snapshot.closer()
}
}
5 changes: 0 additions & 5 deletions core/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ type IterableStorage interface {
IterateLeaf(startKey *Key, consumer func(key, value *felt.Felt) (bool, error)) (bool, error)
}

var usingIterableStorage = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "juno_trie_iterable_storage",
Help: "Time in address get",
}, []string{"iterable"})

type HashFunc func(*felt.Felt, *felt.Felt) *felt.Felt

// Trie is a dense Merkle Patricia Trie (i.e., all internal nodes have two children).
Expand Down
110 changes: 57 additions & 53 deletions sync/snap_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ type snapServer struct {

var _ SnapServerBlockchain = &blockchain.Blockchain{}

const maxNodePerRequest = 1024 * 1024 // I just want it to process faster
const (
_1024 = 1024
maxNodePerRequest = _1024 * _1024 // I just want it to process faster
)

func determineMaxNodes(specifiedMaxNodes uint64) uint64 {
if specifiedMaxNodes == 0 {
return 1024 * 16
return _1024 * 16
}

if specifiedMaxNodes < maxNodePerRequest {
Expand Down Expand Up @@ -94,7 +98,7 @@ func (b *snapServer) GetClassRange(ctx context.Context, request *spec.ClassRange
yield(nil, err)
return
}
defer classCloser()
defer func() { _ = classCloser() }()

startAddr := p2p2core.AdaptHash(request.Start)
limitAddr := p2p2core.AdaptHash(request.End)
Expand All @@ -108,10 +112,11 @@ func (b *snapServer) GetClassRange(ctx context.Context, request *spec.ClassRange
}

classkeys := []*felt.Felt{}
proofs, finished, err := iterateWithLimit(ctrie, startAddr, limitAddr, determineMaxNodes(uint64(request.ChunksPerProof)), func(key, value *felt.Felt) error {
classkeys = append(classkeys, key)
return nil
})
proofs, finished, err := iterateWithLimit(ctrie, startAddr, limitAddr, determineMaxNodes(uint64(request.ChunksPerProof)),
func(key, value *felt.Felt) error {
classkeys = append(classkeys, key)
return nil
})

coreclasses, err := b.blockchain.GetClasses(classkeys)
if err != nil {
Expand Down Expand Up @@ -145,7 +150,7 @@ func (b *snapServer) GetClassRange(ctx context.Context, request *spec.ClassRange
startAddr = classkeys[len(classkeys)-1]
}

// will this send a `Fin` as in https://github.com/starknet-io/starknet-p2p-specs/blob/e335372d39b728372c0ff393bef78763deeb3fcb/p2p/proto/snapshot.proto#L77
// TODO: not needed? - just stop the loop
yield(nil, nil)
}
}
Expand All @@ -171,43 +176,44 @@ func (b *snapServer) GetContractRange(ctx context.Context, request *spec.Contrac
yield(nil, err)
return
}
defer scloser()
defer func() { _ = scloser() }()

startAddr := p2p2core.AdaptAddress(request.Start)
limitAddr := p2p2core.AdaptAddress(request.End)
states := []*spec.ContractState{}

for {
proofs, finished, err := iterateWithLimit(strie, startAddr, limitAddr, determineMaxNodes(uint64(request.ChunksPerProof)), func(key, value *felt.Felt) error {
classHash, err := s.ContractClassHash(key)
if err != nil {
return err
}

nonce, err := s.ContractNonce(key)
if err != nil {
return err
}

ctr, err := s.StorageTrieForAddr(key)
if err != nil {
return err
}

croot, err := ctr.Root()
if err != nil {
return err
}

startAddr = key
states = append(states, &spec.ContractState{
Address: core2p2p.AdaptAddress(key),
Class: core2p2p.AdaptHash(classHash),
Storage: core2p2p.AdaptHash(croot),
Nonce: nonce.Uint64(),
proofs, finished, err := iterateWithLimit(strie, startAddr, limitAddr, determineMaxNodes(uint64(request.ChunksPerProof)),
func(key, value *felt.Felt) error {
classHash, err := s.ContractClassHash(key)
if err != nil {
return err
}

nonce, err := s.ContractNonce(key)
if err != nil {
return err
}

ctr, err := s.StorageTrieForAddr(key)
if err != nil {
return err
}

croot, err := ctr.Root()
if err != nil {
return err
}

startAddr = key
states = append(states, &spec.ContractState{
Address: core2p2p.AdaptAddress(key),
Class: core2p2p.AdaptHash(classHash),
Storage: core2p2p.AdaptHash(croot),
Nonce: nonce.Uint64(),
})
return nil
})
return nil
})
if err != nil {
yield(nil, err)
return
Expand Down Expand Up @@ -260,15 +266,16 @@ func (b *snapServer) GetStorageRange(ctx context.Context, request *StorageRangeR
return
}

handled, err := b.handleStorageRangeRequest(ctx, strie, query, request.ChunkPerProof, contractLimit, func(values []*spec.ContractStoredValue, proofs []trie.ProofNode) {
yield(&StorageRangeStreamingResult{
ContractsRoot: contractRoot,
ClassesRoot: classRoot,
StorageAddr: p2p2core.AdaptAddress(query.Address),
Range: values,
RangeProof: Core2P2pProof(proofs),
}, nil)
})
handled, err := b.handleStorageRangeRequest(ctx, strie, query, request.ChunkPerProof, contractLimit,
func(values []*spec.ContractStoredValue, proofs []trie.ProofNode) {
yield(&StorageRangeStreamingResult{
ContractsRoot: contractRoot,
ClassesRoot: classRoot,
StorageAddr: p2p2core.AdaptAddress(query.Address),
Range: values,
RangeProof: Core2P2pProof(proofs),
}, nil)
})
if err != nil {
yield(nil, err)
return
Expand Down Expand Up @@ -299,7 +306,7 @@ func (b *snapServer) GetClasses(ctx context.Context, felts []*felt.Felt) ([]*spe

func (b *snapServer) handleStorageRangeRequest(
ctx context.Context,
trie *trie.Trie,
stTrie *trie.Trie,
request *spec.StorageRangeQuery,
maxChunkPerProof uint64,
nodeLimit uint64,
Expand All @@ -325,7 +332,7 @@ func (b *snapServer) handleStorageRangeRequest(
limit = nodeLimit
}

proofs, finish, err := iterateWithLimit(trie, startAddr, endAddr, limit, func(key, value *felt.Felt) error {
proofs, finish, err := iterateWithLimit(stTrie, startAddr, endAddr, limit, func(key, value *felt.Felt) error {
response = append(response, &spec.ContractStoredValue{
Key: core2p2p.AdaptFelt(key),
Value: core2p2p.AdaptFelt(value),
Expand All @@ -345,11 +352,8 @@ func (b *snapServer) handleStorageRangeRequest(
}

yield(response, proofs)
if finished {
return totalSent, nil
}

totalSent += totalSent
totalSent += int64(len(response))
nodeLimit -= limit

asBint := startAddr.BigInt(big.NewInt(0))
Expand Down
8 changes: 0 additions & 8 deletions sync/snap_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ func TestClassRange(t *testing.T) {
d, _ = pebble.NewWithOptions("/Users/pnowosie/juno/snapshots/juno-sepolia", 128000000, 128, false)
defer func() { _ = d.Close() }()
bc := blockchain.New(d, &utils.Sepolia) // Needed because class loader need encoder to be registered
_, err := utils.NewZapLogger(utils.DEBUG, false)
assert.NoError(t, err)

b, err := bc.Head()
assert.NoError(t, err)
Expand Down Expand Up @@ -73,9 +71,6 @@ func TestContractRange(t *testing.T) {
defer func() { _ = d.Close() }()
bc := blockchain.New(d, &utils.Sepolia) // Needed because class loader need encoder to be registered

_, err := utils.NewZapLogger(utils.DEBUG, false)
assert.NoError(t, err)

b, err := bc.Head()
assert.NoError(t, err)

Expand Down Expand Up @@ -150,9 +145,6 @@ func TestContractStorageRange(t *testing.T) {
defer func() { _ = d.Close() }()
bc := blockchain.New(d, &utils.Sepolia) // Needed because class loader need encoder to be registered

_, err := utils.NewZapLogger(utils.DEBUG, false)
assert.NoError(t, err)

b, err := bc.Head()
assert.NoError(t, err)

Expand Down
Loading

0 comments on commit 806f194

Please sign in to comment.