From 2536fc6e9bce3d7c052525e77c88e192cf3a8242 Mon Sep 17 00:00:00 2001 From: OpacicAleksa Date: Thu, 22 Dec 2022 16:16:54 +0100 Subject: [PATCH] PRT-142 Fix issue where provider cannot extract the consumer address (#195) * PRT-142 make copy of request for proof * Create shallowCopy not reflect * add unitest to verify --- relayer/server.go | 5 ++- x/pairing/types/relay_extensions.go | 13 ++++++ x/pairing/types/relay_test.go | 61 +++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 x/pairing/types/relay_extensions.go create mode 100644 x/pairing/types/relay_test.go diff --git a/relayer/server.go b/relayer/server.go index c222bb28b5..a0f300aa07 100644 --- a/relayer/server.go +++ b/relayer/server.go @@ -699,7 +699,10 @@ func (s *relayServer) initRelay(ctx context.Context, request *pairingtypes.Relay } relaySession.Lock.Lock() - relaySession.Proof = request + + // Make a shallow copy of relay request and save it as session proof + relaySession.Proof = request.ShallowCopy() + relaySession.Lock.Unlock() } if userSessions == nil { diff --git a/x/pairing/types/relay_extensions.go b/x/pairing/types/relay_extensions.go new file mode 100644 index 0000000000..4c0e695bea --- /dev/null +++ b/x/pairing/types/relay_extensions.go @@ -0,0 +1,13 @@ +package types + +// ShallowCopy makes a shallow copy of the relay request, and returns it +// A shallow copy includes the values of all fields in the original struct, +// but any nested values (such as slices, maps, and pointers) are shared between the original and the copy. +func (m *RelayRequest) ShallowCopy() *RelayRequest { + if m == nil { + return nil + } + + requestCopy := *m + return &requestCopy +} diff --git a/x/pairing/types/relay_test.go b/x/pairing/types/relay_test.go new file mode 100644 index 0000000000..cce73e2a43 --- /dev/null +++ b/x/pairing/types/relay_test.go @@ -0,0 +1,61 @@ +package types + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +// getDummyRequest creates dummy request used in tests +func createDummyRequest(requestedBlock int64, dataReliability *VRFData) *RelayRequest { + return &RelayRequest{ + ChainID: "testID", + Data: []byte("Dummy data"), + RequestBlock: requestedBlock, + DataReliability: dataReliability, + } +} + +// TestRelayShallowCopy tests shallow copy method of relay request +func TestRelayShallowCopy(t *testing.T) { + t.Parallel() + t.Run( + "Check if copy object has same data as original", + func(t *testing.T) { + t.Parallel() + + dataReliability := &VRFData{ + Differentiator: true, + } + + request := createDummyRequest(-2, dataReliability) + copy := request.ShallowCopy() + + assert.Equal(t, request, copy) + }) + t.Run( + "Only nested values should be shared", + func(t *testing.T) { + t.Parallel() + + dataReliability := &VRFData{ + Differentiator: true, + } + + requestedBlock := int64(-2) + + request := createDummyRequest(requestedBlock, dataReliability) + copy := request.ShallowCopy() + + // Change RequestBlock + copy.RequestBlock = 1000 + + // Check that Requested block has not changed in the original + assert.Equal(t, request.RequestBlock, requestedBlock) + + // Change shared dataReliability + dataReliability.Differentiator = false + + // DataReliability should be changed on both objects + assert.Equal(t, request.DataReliability, copy.DataReliability) + }) +}