diff --git a/adapters/p2p2core/felt.go b/adapters/p2p2core/felt.go index 60a25065e..0d200fcf1 100644 --- a/adapters/p2p2core/felt.go +++ b/adapters/p2p2core/felt.go @@ -7,7 +7,6 @@ import ( "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/p2p/starknet/spec" "github.com/ethereum/go-ethereum/common" - "reflect" ) func AdaptHash(h *spec.Hash) *felt.Felt { diff --git a/core/trie/snap_support.go b/core/trie/snap_support.go index 6caf6b834..7c2ae7636 100644 --- a/core/trie/snap_support.go +++ b/core/trie/snap_support.go @@ -175,25 +175,24 @@ func buildKeys(currentKey Key, currentNode *felt.Felt, proofMap map[felt.Felt]Pr return nil } - if proofNode.Edge != nil { - chKey := currentKey.Append(proofNode.Edge.Path) - ch := proofNode.Edge.Child + switch node := proofNode.(type) { + case *Edge: + chKey := currentKey.Append(node.Path) + ch := node.Child err := buildKeys(chKey, ch, proofMap, keys, depth+1) if err != nil { return err } - } else { - binary := proofNode.Binary - + case *Binary: chKey := currentKey.AppendBit(false) - ch := binary.LeftHash + ch := node.LeftHash err := buildKeys(chKey, ch, proofMap, keys, depth+1) if err != nil { return err } chKey = currentKey.AppendBit(true) - ch = binary.RightHash + ch = node.RightHash err = buildKeys(chKey, ch, proofMap, keys, depth+1) if err != nil { return err diff --git a/db/pebble/batch.go b/db/pebble/batch.go index 60747d6a8..5646f341c 100644 --- a/db/pebble/batch.go +++ b/db/pebble/batch.go @@ -14,14 +14,15 @@ var _ db.Transaction = (*batch)(nil) type batch struct { batch *pebble.Batch - lock *sync.Mutex + dbLock *sync.Mutex + rwlock sync.RWMutex listener db.EventListener } -func NewBatch(dbBatch *pebble.Batch, lock *sync.Mutex, listener db.EventListener) *batch { +func NewBatch(dbBatch *pebble.Batch, dbLock *sync.Mutex, listener db.EventListener) *batch { return &batch{ batch: dbBatch, - lock: lock, + dbLock: dbLock, listener: listener, } } @@ -34,8 +35,8 @@ func (b *batch) Discard() error { err := b.batch.Close() b.batch = nil - b.lock.Unlock() - b.lock = nil + b.dbLock.Unlock() + b.dbLock = nil return err } @@ -53,6 +54,9 @@ func (b *batch) Commit() error { // Set : see db.Transaction.Set func (b *batch) Set(key, val []byte) error { + b.rwlock.Lock() + defer b.rwlock.Unlock() + start := time.Now() if len(key) == 0 { return errors.New("empty key") @@ -69,6 +73,9 @@ func (b *batch) Set(key, val []byte) error { // Delete : see db.Transaction.Delete func (b *batch) Delete(key []byte) error { + b.rwlock.Lock() + defer b.rwlock.Unlock() + if b.batch == nil { return ErrDiscardedTransaction } @@ -81,6 +88,9 @@ func (b *batch) Delete(key []byte) error { // Get : see db.Transaction.Get func (b *batch) Get(key []byte, cb func([]byte) error) error { + b.rwlock.RLock() + defer b.rwlock.RUnlock() + if b.batch == nil { return ErrDiscardedTransaction } diff --git a/db/pebble/db.go b/db/pebble/db.go index d3b930335..079bbd064 100644 --- a/db/pebble/db.go +++ b/db/pebble/db.go @@ -94,7 +94,6 @@ func (d *DB) NewTransaction(update bool) (db.Transaction, error) { d.wMutex.Lock() return NewBatch(d.pebble.NewIndexedBatch(), d.wMutex, d.listener), nil } - txn.rwlock = &sync.RWMutex{} return NewSnapshot(d.pebble.NewSnapshot(), d.listener), nil } diff --git a/p2p/p2p.go b/p2p/p2p.go index 00730316f..c5dfca89a 100644 --- a/p2p/p2p.go +++ b/p2p/p2p.go @@ -48,11 +48,9 @@ type Service struct { topics map[string]*pubsub.Topic topicsLock sync.RWMutex - downloader *Downloader - synchroniser *syncService + downloader *Downloader + synchroniser *SyncService gossipTracer *gossipTracer - snapSyncher service.Service - //snapServer *snapServer feederNode bool database db.DB diff --git a/p2p/snap_server.go b/p2p/snap_server.go index bb698fb68..33612d04e 100644 --- a/p2p/snap_server.go +++ b/p2p/snap_server.go @@ -1,6 +1,7 @@ package p2p import ( + "iter" "math/big" "github.com/NethermindEth/juno/utils" @@ -13,7 +14,6 @@ import ( "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/core/trie" "github.com/NethermindEth/juno/p2p/starknet/spec" - "github.com/NethermindEth/juno/utils/iter" "github.com/ethereum/go-ethereum/log" ) @@ -426,26 +426,24 @@ func Core2P2pProof(proofs []trie.ProofNode) *spec.PatriciaRangeProof { nodes := make([]*spec.PatriciaNode, len(proofs)) for i := range proofs { - if proofs[i].Binary != nil { - binary := proofs[i].Binary + switch node := proofs[i].(type) { + case *trie.Edge: + pathFelt := node.Path.Felt() nodes[i] = &spec.PatriciaNode{ - Node: &spec.PatriciaNode_Binary_{ - Binary: &spec.PatriciaNode_Binary{ - Left: core2p2p.AdaptFelt(binary.LeftHash), - Right: core2p2p.AdaptFelt(binary.RightHash), + Node: &spec.PatriciaNode_Edge_{ + Edge: &spec.PatriciaNode_Edge{ + Length: uint32(node.Path.Len()), + Path: core2p2p.AdaptFelt(&pathFelt), + Value: core2p2p.AdaptFelt(node.Child), }, }, } - } - if proofs[i].Edge != nil { - edge := proofs[i].Edge - pathfeld := edge.Path.Felt() + case *trie.Binary: nodes[i] = &spec.PatriciaNode{ - Node: &spec.PatriciaNode_Edge_{ - Edge: &spec.PatriciaNode_Edge{ - Length: uint32(edge.Path.Len()), - Path: core2p2p.AdaptFelt(&pathfeld), - Value: core2p2p.AdaptFelt(edge.Child), + Node: &spec.PatriciaNode_Binary_{ + Binary: &spec.PatriciaNode_Binary{ + Left: core2p2p.AdaptFelt(node.LeftHash), + Right: core2p2p.AdaptFelt(node.RightHash), }, }, } diff --git a/p2p/snap_syncer.go b/p2p/snap_syncer.go index 5494bca19..44231742a 100644 --- a/p2p/snap_syncer.go +++ b/p2p/snap_syncer.go @@ -1230,21 +1230,17 @@ func P2pProofToTrieProofs(proof *spec.PatriciaRangeProof) []trie.ProofNode { for i, node := range proof.Nodes { if node.GetBinary() != nil { binary := node.GetBinary() - proofs[i] = trie.ProofNode{ - Binary: &trie.Binary{ - LeftHash: p2p2core.AdaptFelt(binary.Left), - RightHash: p2p2core.AdaptFelt(binary.Right), - }, + proofs[i] = &trie.Binary{ + LeftHash: p2p2core.AdaptFelt(binary.Left), + RightHash: p2p2core.AdaptFelt(binary.Right), } } else { edge := node.GetEdge() // TODO. What if edge is nil too? key := trie.NewKey(uint8(edge.Length), edge.Path.Elements) - proofs[i] = trie.ProofNode{ - Edge: &trie.Edge{ - Child: p2p2core.AdaptFelt(edge.Value), - Path: &key, - }, + proofs[i] = &trie.Edge{ + Child: p2p2core.AdaptFelt(edge.Value), + Path: &key, } } } diff --git a/p2p/starknet/snap_provider.go b/p2p/starknet/snap_provider.go index 8832a9f6a..59ede0d19 100644 --- a/p2p/starknet/snap_provider.go +++ b/p2p/starknet/snap_provider.go @@ -1,8 +1,9 @@ package starknet import ( + "iter" + "github.com/NethermindEth/juno/p2p/starknet/spec" - "github.com/NethermindEth/juno/utils/iter" "google.golang.org/protobuf/proto" ) diff --git a/p2p/sync.go b/p2p/sync.go index 37856ec04..1c11ba64e 100644 --- a/p2p/sync.go +++ b/p2p/sync.go @@ -87,7 +87,7 @@ func (s *SyncService) Start(ctx context.Context) { } } -func (s *syncService) getNextHeight() (int, error) { +func (s *SyncService) getNextHeight() (int, error) { curHeight, err := s.blockchain.Height() if err == nil { return int(curHeight) + 1, nil @@ -97,7 +97,7 @@ func (s *syncService) getNextHeight() (int, error) { return 0, err } -func (s *syncService) processBlock(ctx context.Context, blockNumber uint64) error { +func (s *SyncService) processBlock(ctx context.Context, blockNumber uint64) error { headersAndSigsCh, err := s.genHeadersAndSigs(ctx, blockNumber) if err != nil { return fmt.Errorf("failed to get block headers parts: %w", err) @@ -270,7 +270,7 @@ func (s *SyncService) processSpecBlockParts( return orderedBlockBodiesCh } -//nolint:gocyclo,funlen +//nolint:gocyclo func (s *SyncService) adaptAndSanityCheckBlock(ctx context.Context, header *spec.SignedBlockHeader, contractDiffs []*spec.ContractDiff, classes []*spec.Class, txs []*spec.Transaction, receipts []*spec.Receipt, events []*spec.Event, prevBlockRoot *felt.Felt, ) <-chan blockBody { @@ -631,7 +631,7 @@ func (s *SyncService) genTransactions(ctx context.Context, blockNumber uint64) ( return txsCh, nil } -func (s *syncService) randomPeer() peer.ID { +func (s *SyncService) randomPeer() peer.ID { store := s.host.Peerstore() // todo do not request same block from all peers peers := utils.Filter(store.Peers(), func(peerID peer.ID) bool {