-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
94799ea
commit 2536fc6
Showing
3 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |