Skip to content

Commit

Permalink
Rewrite iterator functions with new for loop statement (#1954)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkushinDaniil authored Aug 19, 2024
1 parent 50bc46d commit 0ac7e87
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 119 deletions.
4 changes: 3 additions & 1 deletion p2p/starknet/client.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package starknet

// TODO: remove this nolint when the issue is fixed https://github.com/daixiang0/gci/issues/209
//nolint:gci
import (
"context"
"errors"
"io"
"iter"
"time"

"github.com/NethermindEth/juno/p2p/starknet/spec"
"github.com/NethermindEth/juno/utils"
"github.com/NethermindEth/juno/utils/iter"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/protocol"
"google.golang.org/protobuf/encoding/protodelim"
Expand Down
4 changes: 3 additions & 1 deletion p2p/starknet/handlers.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
//go:generate protoc --go_out=./ --proto_path=./ --go_opt=Mp2p/proto/transaction.proto=./spec --go_opt=Mp2p/proto/state.proto=./spec --go_opt=Mp2p/proto/snapshot.proto=./spec --go_opt=Mp2p/proto/receipt.proto=./spec --go_opt=Mp2p/proto/mempool.proto=./spec --go_opt=Mp2p/proto/event.proto=./spec --go_opt=Mp2p/proto/block.proto=./spec --go_opt=Mp2p/proto/common.proto=./spec p2p/proto/transaction.proto p2p/proto/state.proto p2p/proto/snapshot.proto p2p/proto/common.proto p2p/proto/block.proto p2p/proto/event.proto p2p/proto/receipt.proto
package starknet

// TODO: remove this nolint when the issue is fixed https://github.com/daixiang0/gci/issues/209
//nolint:gci
import (
"bytes"
"context"
"fmt"
"iter"
"sync"

"github.com/NethermindEth/juno/adapters/core2p2p"
Expand All @@ -14,7 +17,6 @@ import (
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/p2p/starknet/spec"
"github.com/NethermindEth/juno/utils"
"github.com/NethermindEth/juno/utils/iter"
"github.com/libp2p/go-libp2p/core/network"
"google.golang.org/protobuf/encoding/protodelim"
"google.golang.org/protobuf/proto"
Expand Down
57 changes: 29 additions & 28 deletions p2p/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,26 +409,25 @@ func (s *syncService) genHeadersAndSigs(ctx context.Context, blockNumber uint64)
go func() {
defer close(headersAndSigCh)

headersIt(func(res *spec.BlockHeadersResponse) bool {
loop:
for res := range headersIt {
headerAndSig := specBlockHeaderAndSigs{}
switch v := res.HeaderMessage.(type) {
case *spec.BlockHeadersResponse_Header:
headerAndSig.header = v.Header
case *spec.BlockHeadersResponse_Fin:
return false
break loop
default:
s.log.Warnw("Unexpected HeaderMessage from getBlockHeaders", "v", v)
return false
break loop
}

select {
case <-ctx.Done():
return false
break
case headersAndSigCh <- headerAndSig:
}

return true
})
}
}()

return headersAndSigCh, nil
Expand All @@ -455,18 +454,18 @@ func (s *syncService) genClasses(ctx context.Context, blockNumber uint64) (<-cha
defer close(classesCh)

var classes []*spec.Class
classesIt(func(res *spec.ClassesResponse) bool {
loop:
for res := range classesIt {
switch v := res.ClassMessage.(type) {
case *spec.ClassesResponse_Class:
classes = append(classes, v.Class)
return true
case *spec.ClassesResponse_Fin:
return false
break loop
default:
s.log.Warnw("Unexpected ClassMessage from getClasses", "v", v)
return false
break loop
}
})
}

select {
case <-ctx.Done():
Expand Down Expand Up @@ -501,21 +500,21 @@ func (s *syncService) genStateDiffs(ctx context.Context, blockNumber uint64) (<-
defer close(stateDiffsCh)

var contractDiffs []*spec.ContractDiff
stateDiffsIt(func(res *spec.StateDiffsResponse) bool {

loop:
for res := range stateDiffsIt {
switch v := res.StateDiffMessage.(type) {
case *spec.StateDiffsResponse_ContractDiff:
contractDiffs = append(contractDiffs, v.ContractDiff)
return true
case *spec.StateDiffsResponse_DeclaredClass:
s.log.Warnw("Unimplemented message StateDiffsResponse_DeclaredClass")
return true
case *spec.StateDiffsResponse_Fin:
return false
break loop
default:
s.log.Warnw("Unexpected ClassMessage from getStateDiffs", "v", v)
return false
break loop
}
})
}

select {
case <-ctx.Done():
Expand Down Expand Up @@ -549,18 +548,19 @@ func (s *syncService) genEvents(ctx context.Context, blockNumber uint64) (<-chan
defer close(eventsCh)

var events []*spec.Event
eventsIt(func(res *spec.EventsResponse) bool {

loop:
for res := range eventsIt {
switch v := res.EventMessage.(type) {
case *spec.EventsResponse_Event:
events = append(events, v.Event)
return true
case *spec.EventsResponse_Fin:
return false
break loop
default:
s.log.Warnw("Unexpected EventMessage from getEvents", "v", v)
return false
break loop
}
})
}

select {
case <-ctx.Done():
Expand Down Expand Up @@ -598,20 +598,21 @@ func (s *syncService) genTransactions(ctx context.Context, blockNumber uint64) (
transactions []*spec.Transaction
receipts []*spec.Receipt
)
txsIt(func(res *spec.TransactionsResponse) bool {

loop:
for res := range txsIt {
switch v := res.TransactionMessage.(type) {
case *spec.TransactionsResponse_TransactionWithReceipt:
txWithReceipt := v.TransactionWithReceipt
transactions = append(transactions, txWithReceipt.Transaction)
receipts = append(receipts, txWithReceipt.Receipt)
return true
case *spec.TransactionsResponse_Fin:
return false
break loop
default:
s.log.Warnw("Unexpected TransactionMessage from getTransactions", "v", v)
return false
break loop
}
})
}

s.log.Debugw("Transactions length", "len", len(transactions))
spexTxs := specTxWithReceipts{
Expand Down
34 changes: 0 additions & 34 deletions utils/iter/pull.go

This file was deleted.

49 changes: 0 additions & 49 deletions utils/iter/pull_test.go

This file was deleted.

6 changes: 0 additions & 6 deletions utils/iter/seq.go

This file was deleted.

0 comments on commit 0ac7e87

Please sign in to comment.