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

No panic on new request id #397

Merged
merged 4 commits into from
Dec 20, 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
17 changes: 12 additions & 5 deletions commit/merkleroot/rmn/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

mapset "github.com/deckarep/golang-set/v2"
"golang.org/x/exp/maps"
rand2 "golang.org/x/exp/rand"
"google.golang.org/protobuf/proto"

chainsel "github.com/smartcontractkit/chain-selectors"
Expand Down Expand Up @@ -364,7 +365,7 @@ func (c *controller) sendObservationRequests(
}

req := &rmnpb.Request{
RequestId: newRequestID(),
RequestId: newRequestID(c.lggr),
Request: &rmnpb.Request_ObservationRequest{
ObservationRequest: &rmnpb.ObservationRequest{
LaneDest: destChain,
Expand Down Expand Up @@ -810,7 +811,7 @@ func (c *controller) sendReportSignatureRequest(
}

req := &rmnpb.Request{
RequestId: newRequestID(),
RequestId: newRequestID(c.lggr),
Request: &rmnpb.Request_ReportSignatureRequest{
ReportSignatureRequest: reportSigReq,
},
Expand Down Expand Up @@ -908,7 +909,7 @@ func (c *controller) listenForRmnReportSignatures(
continue
}
req := &rmnpb.Request{
RequestId: newRequestID(),
RequestId: newRequestID(c.lggr),
Request: &rmnpb.Request_ReportSignatureRequest{
ReportSignatureRequest: reportSigReq,
},
Expand Down Expand Up @@ -1072,11 +1073,17 @@ func randomShuffle[T any](s []T) []T {
return ret
}

func newRequestID() uint64 {
// newRequestID generates a new unique request ID.
func newRequestID(lggr logger.Logger) uint64 {
b := make([]byte, 8)
_, err := crand.Read(b)
if err != nil {
panic(err)
// fallback to time-based id in the very rare scenario that the random number generator fails
lggr.Warnw("failed to generate random request id, falling back to golang.org/x/exp/rand",
"err", err,
)
rand2.Seed(uint64(time.Now().UnixNano()))
return rand2.Uint64()
}
randomUint64 := binary.LittleEndian.Uint64(b)
return randomUint64
Expand Down
10 changes: 10 additions & 0 deletions commit/merkleroot/rmn/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,16 @@ func Test_controller_validateSignedObservationResponse(t *testing.T) {
}
}

func Test_newRequestID(t *testing.T) {
ids := map[uint64]struct{}{}
for i := 0; i < 1000; i++ {
id := newRequestID(logger.Test(t))
_, ok := ids[id]
assert.False(t, ok)
ids[id] = struct{}{}
}
}

func (ts *testSetup) waitForObservationRequestsToBeSent(
rmnClient *mockPeerClient,
homeF int,
Expand Down
Loading