-
Notifications
You must be signed in to change notification settings - Fork 28
fix: use deterministic proto marshalling in SetResponse and RegisterTrigger #1960
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import ( | |
| ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2/types" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "google.golang.org/protobuf/proto" | ||
| "google.golang.org/protobuf/types/known/anypb" | ||
| "google.golang.org/protobuf/types/known/emptypb" | ||
|
|
||
|
|
@@ -168,6 +169,39 @@ func TestSetResponse(t *testing.T) { | |
| assert.Nil(t, resp.Payload) | ||
| assert.Nil(t, resp.OCRAttestation) | ||
| }) | ||
|
|
||
| t.Run("deterministic marshalling with map fields", func(t *testing.T) { | ||
| // Proto messages with map fields can serialize in different orders | ||
| // because Go map iteration order is randomized. SetResponse must | ||
| // produce identical bytes across calls so that distributed nodes | ||
| // reach quorum on the same hash. | ||
| msg := &pb.CapabilityConfig{ | ||
| MethodConfigs: map[string]*pb.CapabilityMethodConfig{ | ||
| "alpha": {}, | ||
| "bravo": {}, | ||
| "charlie": {}, | ||
| "delta": {}, | ||
| "echo": {}, | ||
| }, | ||
| } | ||
|
|
||
| var firstBytes []byte | ||
| for i := 0; i < 100; i++ { | ||
| resp := capabilities.CapabilityResponse{} | ||
| err := capabilities.SetResponse(&resp, true, msg, nil) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, resp.Payload) | ||
|
|
||
| b, err := proto.Marshal(resp.Payload) | ||
| require.NoError(t, err) | ||
|
|
||
| if firstBytes == nil { | ||
| firstBytes = b | ||
| } else { | ||
| assert.Equal(t, firstBytes, b, "SetResponse produced non-deterministic bytes on iteration %d", i) | ||
| } | ||
| } | ||
|
Comment on lines
+173
to
+203
|
||
| }) | ||
| } | ||
|
|
||
| func TestExecute(t *testing.T) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use deterministic marshaling for CapabilityResponse as a whole, which should propagate to the nested any proto: https://github.com/smartcontractkit/chainlink-common/blob/main/pkg/capabilities/pb/capabilities_helpers.go#L35
I think this test is not representative and the problem doesn't exist. Am I wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't get a repro using this particular bit of nondeterminism anyway. Otherwise there are threads for the stuff I was able to repro.