Skip to content

Commit

Permalink
Updated TestStruct to enable advanced querying (#798)
Browse files Browse the repository at this point in the history
* Updated TestStruct to enable advanced querying

* linting fixes

* Update pkg/codec/encodings/type_codec_test.go

Co-authored-by: Clement <clement.erena78@gmail.com>

* Update pkg/codec/encodings/type_codec_test.go

Co-authored-by: Clement <clement.erena78@gmail.com>

* Fixed codec tests

---------

Co-authored-by: Clement <clement.erena78@gmail.com>
  • Loading branch information
silaslenihan and Atrax1 authored Sep 26, 2024
1 parent 84ed150 commit 0784a13
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 74 deletions.
46 changes: 33 additions & 13 deletions pkg/codec/encodings/type_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,14 @@ func (b *bigEndianInterfaceTester) encode(t *testing.T, bytes []byte, ts TestStr
bs, err := bigbigendian.SerializeSigned(4, ts.BigField)
require.NoError(t, err)
bytes = append(bytes, bs...)
bytes = append(bytes, ts.NestedStruct.FixedBytes[:]...)
bytes = rawbin.BigEndian.AppendUint64(bytes, uint64(ts.NestedStruct.Inner.I))
bytes = rawbin.BigEndian.AppendUint32(bytes, uint32(len(ts.NestedStruct.Inner.S)))
bytes = append(bytes, []byte(ts.NestedStruct.Inner.S)...)
bytes = append(bytes, ts.NestedDynamicStruct.FixedBytes[:]...)
bytes = rawbin.BigEndian.AppendUint64(bytes, uint64(ts.NestedDynamicStruct.Inner.I))
bytes = rawbin.BigEndian.AppendUint32(bytes, uint32(len(ts.NestedDynamicStruct.Inner.S)))
bytes = append(bytes, []byte(ts.NestedDynamicStruct.Inner.S)...)
bytes = append(bytes, ts.NestedStaticStruct.FixedBytes[:]...)
bytes = rawbin.BigEndian.AppendUint64(bytes, uint64(ts.NestedStaticStruct.Inner.I))
bytes = append(bytes, byte(len(ts.NestedStaticStruct.Inner.A)))
bytes = append(bytes, ts.NestedStaticStruct.Inner.A...)
if request.ExtraField {
bytes = append(bytes, 5)
}
Expand All @@ -196,26 +200,41 @@ func (b *bigEndianInterfaceTester) encode(t *testing.T, bytes []byte, ts TestStr
func newTestStructCodec(t *testing.T, builder encodings.Builder) encodings.TypeCodec {
sCodec, err := builder.String(math.MaxInt32)
require.NoError(t, err)
innerTestStruct, err := encodings.NewStructCodec([]encodings.NamedTypeCodec{

arr2, err := encodings.NewArray(2, builder.Uint8())
require.NoError(t, err)

size, err := builder.Int(1)
require.NoError(t, err)

acc, err := encodings.NewSlice(builder.Uint8(), size)
require.NoError(t, err)

innerDynamicTestStruct, err := encodings.NewStructCodec([]encodings.NamedTypeCodec{
{Name: "I", Codec: builder.Int64()},
{Name: "S", Codec: sCodec},
})
require.NoError(t, err)
arr2, err := encodings.NewArray(2, builder.Uint8())

innerStaticTestStruct, err := encodings.NewStructCodec([]encodings.NamedTypeCodec{
{Name: "I", Codec: builder.Int64()},
{Name: "A", Codec: acc},
})
require.NoError(t, err)

midCodec, err := encodings.NewStructCodec([]encodings.NamedTypeCodec{
midDynamicCodec, err := encodings.NewStructCodec([]encodings.NamedTypeCodec{
{Name: "FixedBytes", Codec: arr2},
{Name: "Inner", Codec: innerTestStruct},
{Name: "Inner", Codec: innerDynamicTestStruct},
})
require.NoError(t, err)

oIDs, err := encodings.NewArray(32, builder.OracleID())
midStaticCodec, err := encodings.NewStructCodec([]encodings.NamedTypeCodec{
{Name: "FixedBytes", Codec: arr2},
{Name: "Inner", Codec: innerStaticTestStruct},
})
require.NoError(t, err)

size, err := builder.Int(1)
require.NoError(t, err)
acc, err := encodings.NewSlice(builder.Uint8(), size)
oIDs, err := encodings.NewArray(32, builder.OracleID())
require.NoError(t, err)

accs, err := encodings.NewSlice(acc, size)
Expand All @@ -232,7 +251,8 @@ func newTestStructCodec(t *testing.T, builder encodings.Builder) encodings.TypeC
{Name: "Account", Codec: acc},
{Name: "Accounts", Codec: accs},
{Name: "BigField", Codec: bi},
{Name: "NestedStruct", Codec: midCodec},
{Name: "NestedDynamicStruct", Codec: midDynamicCodec},
{Name: "NestedStaticStruct", Codec: midStaticCodec},
})
require.NoError(t, err)
return ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ func (f *fakeContractReader) SetBatchLatestValues(batchCallEntry BatchCallEntry)
}

func (f *fakeContractReader) GetLatestValue(_ context.Context, readIdentifier string, confidenceLevel primitives.ConfidenceLevel, params, returnVal any) error {

if strings.HasSuffix(readIdentifier, MethodReturningAlterableUint64) {
r := returnVal.(*uint64)
for i := len(f.vals) - 1; i >= 0; i-- {
Expand Down Expand Up @@ -520,7 +519,6 @@ func (f *fakeContractReader) GetLatestValue(_ context.Context, readIdentifier st
}

return nil

}

func (f *fakeContractReader) BatchGetLatestValues(_ context.Context, request types.BatchGetLatestValuesRequest) (types.BatchGetLatestValuesResult, error) {
Expand Down Expand Up @@ -602,7 +600,6 @@ func (f *fakeContractReader) QueryKey(_ context.Context, _ types.BoundContract,
}
}
if len(filter.Expressions) == 0 || doAppend {

if isValueType {
value, err := values.Wrap(trigger.testStruct)
if err != nil {
Expand Down
11 changes: 9 additions & 2 deletions pkg/types/interfacetests/codec_interface_fuzz_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,20 @@ func RunCodecInterfaceFuzzTests(f *testing.F, tester CodecInterfaceTester) {
Account: tester.GetAccountBytes(accountSeed),
Accounts: [][]byte{tester.GetAccountBytes(accountsSeed + 1), tester.GetAccountBytes(accountsSeed + 2)},
BigField: big.NewInt(bigField),
NestedStruct: MidLevelTestStruct{
NestedDynamicStruct: MidLevelDynamicTestStruct{
FixedBytes: fb,
Inner: InnerTestStruct{
Inner: InnerDynamicTestStruct{
I: i,
S: s,
},
},
NestedStaticStruct: MidLevelStaticTestStruct{
FixedBytes: fb,
Inner: InnerStaticTestStruct{
I: i,
A: tester.GetAccountBytes(accountsSeed + 3),
},
},
}
codec := tester.GetCodec(t)
ctx := tests.Context(t)
Expand Down
67 changes: 39 additions & 28 deletions pkg/types/interfacetests/codec_interface_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ func RunCodecInterfaceTests(t *testing.T, tester CodecInterfaceTester) {
req := &EncodeRequest{TestStructs: []TestStruct{item}, TestOn: TestItemType}
resp := tester.EncodeFields(t, req)
compatibleItem := compatibleTestStruct{
Account: item.Account,
Accounts: item.Accounts,
BigField: item.BigField,
DifferentField: item.DifferentField,
Field: *item.Field,
NestedStruct: item.NestedStruct,
OracleID: item.OracleID,
OracleIDs: item.OracleIDs,
Account: item.Account,
Accounts: item.Accounts,
BigField: item.BigField,
DifferentField: item.DifferentField,
Field: *item.Field,
NestedDynamicStruct: item.NestedDynamicStruct,
NestedStaticStruct: item.NestedStaticStruct,
OracleID: item.OracleID,
OracleIDs: item.OracleIDs,
}

codec := tester.GetCodec(t)
Expand All @@ -98,12 +99,20 @@ func RunCodecInterfaceTests(t *testing.T, tester CodecInterfaceTester) {
"BigField": item.BigField,
"DifferentField": item.DifferentField,
"Field": item.Field,
"NestedStruct": map[string]any{
"NestedDynamicStruct": map[string]any{
// since we're testing compatibility, also use slice instead of array
"FixedBytes": item.NestedStruct.FixedBytes[:],
"FixedBytes": item.NestedDynamicStruct.FixedBytes[:],
"Inner": map[string]any{
"I": item.NestedStruct.Inner.I,
"S": item.NestedStruct.Inner.S,
"I": item.NestedDynamicStruct.Inner.I,
"S": item.NestedDynamicStruct.Inner.S,
},
},
"NestedStaticStruct": map[string]any{
// since we're testing compatibility, also use slice instead of array
"FixedBytes": item.NestedStaticStruct.FixedBytes[:],
"Inner": map[string]any{
"I": item.NestedStaticStruct.Inner.I,
"A": item.NestedStaticStruct.Inner.A,
},
},
"OracleID": item.OracleID,
Expand All @@ -126,13 +135,14 @@ func RunCodecInterfaceTests(t *testing.T, tester CodecInterfaceTester) {
ctx := tests.Context(t)
ts := CreateTestStruct[*testing.T](0, tester)
item := &TestStructMissingField{
DifferentField: ts.DifferentField,
OracleID: ts.OracleID,
OracleIDs: ts.OracleIDs,
Account: ts.Account,
Accounts: ts.Accounts,
BigField: ts.BigField,
NestedStruct: ts.NestedStruct,
DifferentField: ts.DifferentField,
OracleID: ts.OracleID,
OracleIDs: ts.OracleIDs,
Account: ts.Account,
Accounts: ts.Accounts,
BigField: ts.BigField,
NestedDynamicStruct: ts.NestedDynamicStruct,
NestedStaticStruct: ts.NestedStaticStruct,
}

codec := tester.GetCodec(t)
Expand Down Expand Up @@ -338,14 +348,15 @@ func RunCodecInterfaceTests(t *testing.T, tester CodecInterfaceTester) {
ctx := tests.Context(t)
cr := tester.GetCodec(t)
nilArgs := &TestStruct{
Field: nil,
DifferentField: "",
OracleID: 0,
OracleIDs: [32]commontypes.OracleID{},
Account: nil,
Accounts: nil,
BigField: nil,
NestedStruct: MidLevelTestStruct{},
Field: nil,
DifferentField: "",
OracleID: 0,
OracleIDs: [32]commontypes.OracleID{},
Account: nil,
Accounts: nil,
BigField: nil,
NestedDynamicStruct: MidLevelDynamicTestStruct{},
NestedStaticStruct: MidLevelStaticTestStruct{},
}
// Assure no panic, use _,_ to tell the compiler we don't care about the error
_, _ = cr.Encode(ctx, nilArgs, TestItemType)
Expand All @@ -356,7 +367,7 @@ func RunCodecInterfaceTests(t *testing.T, tester CodecInterfaceTester) {
test: func(t *testing.T) {
ctx := tests.Context(t)
cr := tester.GetCodec(t)
notTestStruct := &MidLevelTestStruct{}
notTestStruct := &MidLevelDynamicTestStruct{}
_, err := cr.Encode(ctx, notTestStruct, TestItemType)
assert.True(t, errors.Is(err, types.ErrInvalidType))
},
Expand Down
76 changes: 48 additions & 28 deletions pkg/types/interfacetests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,36 @@ func (e ExpectedGetLatestValueArgs) String() string {
e.ContractName, e.ReadName, e.ConfidenceLevel, e.Params, e.ReturnVal)
}

type InnerTestStruct struct {
type InnerDynamicTestStruct struct {
I int
S string
}

type MidLevelTestStruct struct {
type InnerStaticTestStruct struct {
I int
A []byte
}

type MidLevelDynamicTestStruct struct {
FixedBytes [2]byte
Inner InnerDynamicTestStruct
}

type MidLevelStaticTestStruct struct {
FixedBytes [2]byte
Inner InnerTestStruct
Inner InnerStaticTestStruct
}

type TestStruct struct {
Field *int32
OracleID commontypes.OracleID
OracleIDs [32]commontypes.OracleID
Account []byte
Accounts [][]byte
DifferentField string
BigField *big.Int
NestedStruct MidLevelTestStruct
Field *int32
OracleID commontypes.OracleID
OracleIDs [32]commontypes.OracleID
Account []byte
Accounts [][]byte
DifferentField string
BigField *big.Int
NestedDynamicStruct MidLevelDynamicTestStruct
NestedStaticStruct MidLevelStaticTestStruct
}

type TestStructWithExtraField struct {
Expand All @@ -159,25 +170,27 @@ type TestStructWithExtraField struct {
}

type TestStructMissingField struct {
DifferentField string
OracleID commontypes.OracleID
OracleIDs [32]commontypes.OracleID
Account []byte
Accounts [][]byte
BigField *big.Int
NestedStruct MidLevelTestStruct
DifferentField string
OracleID commontypes.OracleID
OracleIDs [32]commontypes.OracleID
Account []byte
Accounts [][]byte
BigField *big.Int
NestedDynamicStruct MidLevelDynamicTestStruct
NestedStaticStruct MidLevelStaticTestStruct
}

// compatibleTestStruct has fields in a different order
type compatibleTestStruct struct {
Account []byte
Accounts [][]byte
BigField *big.Int
DifferentField string
Field int32
NestedStruct MidLevelTestStruct
OracleID commontypes.OracleID
OracleIDs [32]commontypes.OracleID
Account []byte
Accounts [][]byte
BigField *big.Int
DifferentField string
Field int32
NestedDynamicStruct MidLevelDynamicTestStruct
NestedStaticStruct MidLevelStaticTestStruct
OracleID commontypes.OracleID
OracleIDs [32]commontypes.OracleID
}

type LatestParams struct {
Expand Down Expand Up @@ -207,13 +220,20 @@ func CreateTestStruct[T any](i int, tester BasicTester[T]) TestStruct {
Accounts: [][]byte{tester.GetAccountBytes(i + 4), tester.GetAccountBytes(i + 5)},
DifferentField: s,
BigField: big.NewInt(int64((i + 1) * (i + 2))),
NestedStruct: MidLevelTestStruct{
NestedDynamicStruct: MidLevelDynamicTestStruct{
FixedBytes: [2]byte{uint8(i), uint8(i + 1)},
Inner: InnerTestStruct{
Inner: InnerDynamicTestStruct{
I: i,
S: s,
},
},
NestedStaticStruct: MidLevelStaticTestStruct{
FixedBytes: [2]byte{uint8(i), uint8(i + 1)},
Inner: InnerStaticTestStruct{
I: i,
A: tester.GetAccountBytes(i + 6),
},
},
}
}

Expand Down

0 comments on commit 0784a13

Please sign in to comment.