Skip to content

Commit

Permalink
Merge branch 'main' into cl33-16
Browse files Browse the repository at this point in the history
  • Loading branch information
b-gopalswami authored Dec 20, 2024
2 parents a0c340f + 95fc747 commit a7784c6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
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

0 comments on commit a7784c6

Please sign in to comment.