Skip to content

Commit

Permalink
PRT-142 Fix issue where provider cannot extract the consumer address (#…
Browse files Browse the repository at this point in the history
…195)

* PRT-142 make copy of request for proof

* Create shallowCopy not reflect

* add unitest to verify
  • Loading branch information
0xAleksaOpacic authored Dec 22, 2022
1 parent 94799ea commit 2536fc6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
5 changes: 4 additions & 1 deletion relayer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions x/pairing/types/relay_extensions.go
Original file line number Diff line number Diff line change
@@ -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
}
61 changes: 61 additions & 0 deletions x/pairing/types/relay_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}

0 comments on commit 2536fc6

Please sign in to comment.