Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daniil/rewrite-functional-iterators-into-for-loop-statement #1954

Merged
merged 16 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading