From 0784a13b25368ec80ba621094477102839f2069b Mon Sep 17 00:00:00 2001 From: Silas Lenihan <32529249+silaslenihan@users.noreply.github.com> Date: Thu, 26 Sep 2024 14:01:10 -0400 Subject: [PATCH] Updated TestStruct to enable advanced querying (#798) * Updated TestStruct to enable advanced querying * linting fixes * Update pkg/codec/encodings/type_codec_test.go Co-authored-by: Clement * Update pkg/codec/encodings/type_codec_test.go Co-authored-by: Clement * Fixed codec tests --------- Co-authored-by: Clement --- pkg/codec/encodings/type_codec_test.go | 46 +++++++---- .../contractreader/contract_reader_test.go | 3 - .../codec_interface_fuzz_tests.go | 11 ++- .../interfacetests/codec_interface_tests.go | 67 +++++++++------- pkg/types/interfacetests/utils.go | 76 ++++++++++++------- 5 files changed, 129 insertions(+), 74 deletions(-) diff --git a/pkg/codec/encodings/type_codec_test.go b/pkg/codec/encodings/type_codec_test.go index f4a689d07..d7762fe61 100644 --- a/pkg/codec/encodings/type_codec_test.go +++ b/pkg/codec/encodings/type_codec_test.go @@ -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) } @@ -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) @@ -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 diff --git a/pkg/loop/internal/relayer/pluginprovider/contractreader/contract_reader_test.go b/pkg/loop/internal/relayer/pluginprovider/contractreader/contract_reader_test.go index a226e1f0f..0b1defdaa 100644 --- a/pkg/loop/internal/relayer/pluginprovider/contractreader/contract_reader_test.go +++ b/pkg/loop/internal/relayer/pluginprovider/contractreader/contract_reader_test.go @@ -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-- { @@ -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) { @@ -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 { diff --git a/pkg/types/interfacetests/codec_interface_fuzz_tests.go b/pkg/types/interfacetests/codec_interface_fuzz_tests.go index e5f65490c..2b8e2957a 100644 --- a/pkg/types/interfacetests/codec_interface_fuzz_tests.go +++ b/pkg/types/interfacetests/codec_interface_fuzz_tests.go @@ -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) diff --git a/pkg/types/interfacetests/codec_interface_tests.go b/pkg/types/interfacetests/codec_interface_tests.go index 79e53dcc1..3f3c125d2 100644 --- a/pkg/types/interfacetests/codec_interface_tests.go +++ b/pkg/types/interfacetests/codec_interface_tests.go @@ -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) @@ -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, @@ -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) @@ -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) @@ -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)) }, diff --git a/pkg/types/interfacetests/utils.go b/pkg/types/interfacetests/utils.go index 2903c2e04..a9f009eb8 100644 --- a/pkg/types/interfacetests/utils.go +++ b/pkg/types/interfacetests/utils.go @@ -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 { @@ -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 { @@ -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), + }, + }, } }