Skip to content

Commit

Permalink
Add hash validation in go
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkushinDaniil committed Jan 8, 2025
1 parent 5d9c10f commit 2eeb323
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions p2p/sync/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/NethermindEth/juno/p2p/gen"
"github.com/NethermindEth/juno/utils"
"github.com/bufbuild/protovalidate-go"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/protocol"
"google.golang.org/protobuf/encoding/protodelim"
Expand Down Expand Up @@ -74,6 +75,14 @@ func requestAndReceiveStream[ReqT proto.Message, ResT proto.Message](ctx context
return nil, err
}

validator, err := protovalidate.New(
protovalidate.WithDisableLazy(true),
protovalidate.WithMessages(&gen.Hash{}),
)
if err != nil {
return nil, err
}

return func(yield func(ResT) bool) {
defer func() {
closeErr := stream.Close()
Expand All @@ -93,6 +102,11 @@ func requestAndReceiveStream[ReqT proto.Message, ResT proto.Message](ctx context
break
}

if err := validator.Validate(res); err != nil {
log.Errorw("Error while validating response", "err", err)
break
}

if !yield(res.(ResT)) {
break
}
Expand Down

0 comments on commit 2eeb323

Please sign in to comment.