From 1561c368f47766f0ec9eaeca922e1ec13b6045ea Mon Sep 17 00:00:00 2001 From: Domino Valdano <2644901+reductionista@users.noreply.github.com> Date: Fri, 20 Oct 2023 14:18:49 -0700 Subject: [PATCH] Add some tests --- pkg/loop/internal/pb/median.pb.go | 5 +-- pkg/loop/internal/pb/median_grpc.pb.go | 1 + pkg/loop/internal/pb/relayer.pb.go | 5 +-- pkg/loop/internal/pb/relayer_grpc.pb.go | 1 + pkg/loop/internal/pb/reporting.pb.go | 5 +-- pkg/loop/internal/pb/reporting_grpc.pb.go | 1 + pkg/loop/internal/test/config.go | 31 +++++++++++++++++++ pkg/loop/internal/test/median.go | 4 +++ pkg/loop/internal/test/plugin_provider.go | 6 ++++ pkg/loop/internal/test/test.go | 10 ++++++ ...nreporting2_monitoring_median_config.pb.go | 5 +-- ...eporting2_monitoring_offchain_config.pb.go | 5 +-- .../mercury/mercury_config.pb.go | 5 +-- .../mercury/v1/mercury_observation_v1.pb.go | 5 +-- .../mercury/v2/mercury_observation_v2.pb.go | 5 +-- .../mercury/v3/mercury_observation_v3.pb.go | 5 +-- 16 files changed, 81 insertions(+), 18 deletions(-) diff --git a/pkg/loop/internal/pb/median.pb.go b/pkg/loop/internal/pb/median.pb.go index 6e2b22af00..33bcb1d533 100644 --- a/pkg/loop/internal/pb/median.pb.go +++ b/pkg/loop/internal/pb/median.pb.go @@ -7,12 +7,13 @@ package pb import ( + reflect "reflect" + sync "sync" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" emptypb "google.golang.org/protobuf/types/known/emptypb" timestamppb "google.golang.org/protobuf/types/known/timestamppb" - reflect "reflect" - sync "sync" ) const ( diff --git a/pkg/loop/internal/pb/median_grpc.pb.go b/pkg/loop/internal/pb/median_grpc.pb.go index 3260a2faec..7fff6c84a9 100644 --- a/pkg/loop/internal/pb/median_grpc.pb.go +++ b/pkg/loop/internal/pb/median_grpc.pb.go @@ -8,6 +8,7 @@ package pb import ( context "context" + grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" diff --git a/pkg/loop/internal/pb/relayer.pb.go b/pkg/loop/internal/pb/relayer.pb.go index 53984a19bc..e30d4fe9f2 100644 --- a/pkg/loop/internal/pb/relayer.pb.go +++ b/pkg/loop/internal/pb/relayer.pb.go @@ -7,11 +7,12 @@ package pb import ( + reflect "reflect" + sync "sync" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" emptypb "google.golang.org/protobuf/types/known/emptypb" - reflect "reflect" - sync "sync" ) const ( diff --git a/pkg/loop/internal/pb/relayer_grpc.pb.go b/pkg/loop/internal/pb/relayer_grpc.pb.go index fd503d6b6d..3e19c43ec7 100644 --- a/pkg/loop/internal/pb/relayer_grpc.pb.go +++ b/pkg/loop/internal/pb/relayer_grpc.pb.go @@ -8,6 +8,7 @@ package pb import ( context "context" + grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" diff --git a/pkg/loop/internal/pb/reporting.pb.go b/pkg/loop/internal/pb/reporting.pb.go index 6ee101c4b5..6be06085ca 100644 --- a/pkg/loop/internal/pb/reporting.pb.go +++ b/pkg/loop/internal/pb/reporting.pb.go @@ -7,11 +7,12 @@ package pb import ( + reflect "reflect" + sync "sync" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" emptypb "google.golang.org/protobuf/types/known/emptypb" - reflect "reflect" - sync "sync" ) const ( diff --git a/pkg/loop/internal/pb/reporting_grpc.pb.go b/pkg/loop/internal/pb/reporting_grpc.pb.go index 34835ae37d..d50ff114f0 100644 --- a/pkg/loop/internal/pb/reporting_grpc.pb.go +++ b/pkg/loop/internal/pb/reporting_grpc.pb.go @@ -8,6 +8,7 @@ package pb import ( context "context" + grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" diff --git a/pkg/loop/internal/test/config.go b/pkg/loop/internal/test/config.go index fa01e8446d..6b30873585 100644 --- a/pkg/loop/internal/test/config.go +++ b/pkg/loop/internal/test/config.go @@ -3,11 +3,16 @@ package test import ( "bytes" "context" + "encoding/json" "fmt" + "math/big" + "time" libocr "github.com/smartcontractkit/libocr/offchainreporting2plus/types" "github.com/stretchr/testify/assert" + + "github.com/smartcontractkit/chainlink-relay/pkg/types" ) type staticConfigProvider struct{} @@ -85,3 +90,29 @@ func (s staticContractTransmitter) LatestConfigDigestAndEpoch(ctx context.Contex func (s staticContractTransmitter) FromAccount() (libocr.Account, error) { return account, nil } + +type staticChainReader struct{} + +func (c staticChainReader) GetLatestValue(ctx context.Context, bc types.BoundContract, method string, params, returnVal any) ([]byte, error) { + if !assert.ObjectsAreEqual(bc, boundContract) { + return nil, fmt.Errorf("expected report context %v but got %v", boundContract, bc) + } + if method != medianContractGenericMethod { + return nil, fmt.Errorf("expected generic contract method %v but got %v", medianContractGenericMethod, method) + } + if !assert.ObjectsAreEqual(params, getLatestValueParams) { + return nil, fmt.Errorf("expected params %v but got %v", params, getLatestValueParams) + } + + retStruct := struct { + Epoch uint32 + Round uint8 + LatestAnswer *big.Int + LatestTimestamp time.Time + }{epoch, round, latestAnswer, latestTimestamp} + ret, err := json.Marshal(retStruct) + if err != nil { + return nil, fmt.Errorf("Failed constructing static return data from GetLatestValue") + } + return ret, nil +} diff --git a/pkg/loop/internal/test/median.go b/pkg/loop/internal/test/median.go index 925309a2c3..01a09c16c1 100644 --- a/pkg/loop/internal/test/median.go +++ b/pkg/loop/internal/test/median.go @@ -305,6 +305,10 @@ func (s StaticMedianProvider) OnchainConfigCodec() median.OnchainConfigCodec { return staticOnchainConfigCodec{} } +func (s StaticMedianProvider) ChainReader() types.ChainReader { + return staticChainReader{} +} + type staticReportCodec struct{} func (s staticReportCodec) BuildReport(os []median.ParsedAttributedObservation) (libocr.Report, error) { diff --git a/pkg/loop/internal/test/plugin_provider.go b/pkg/loop/internal/test/plugin_provider.go index c9878832f3..a00eb13108 100644 --- a/pkg/loop/internal/test/plugin_provider.go +++ b/pkg/loop/internal/test/plugin_provider.go @@ -4,6 +4,8 @@ import ( "context" libocr "github.com/smartcontractkit/libocr/offchainreporting2plus/types" + + "github.com/smartcontractkit/chainlink-relay/pkg/types" ) type StaticPluginProvider struct{} @@ -29,3 +31,7 @@ func (s StaticPluginProvider) ContractConfigTracker() libocr.ContractConfigTrack func (s StaticPluginProvider) ContractTransmitter() libocr.ContractTransmitter { return staticContractTransmitter{} } + +func (s StaticPluginProvider) ChainReader() types.ChainReader { + return staticChainReader{} +} diff --git a/pkg/loop/internal/test/test.go b/pkg/loop/internal/test/test.go index 3e814e9f6d..6a8e06e3b7 100644 --- a/pkg/loop/internal/test/test.go +++ b/pkg/loop/internal/test/test.go @@ -125,4 +125,14 @@ URL = 'https://test.url' signed = []byte{13: 37} sigs = []libocr.AttributedOnchainSignature{{Signature: []byte{9: 8, 7: 6}, Signer: commontypes.OracleID(54)}} value = big.NewInt(999) + boundContract = types.BoundContract{ + Name: "my median contract", + Address: "0xBbf078A8849D74653e36E6DBBdC7e1a35E657C26", + Pending: false, + } + medianContractGenericMethod = "LatestTransmissionDetails" + getLatestValueParams = struct { + Param1 string + Param2 string + }{"value1", "value2"} ) diff --git a/pkg/monitoring/pb/offchainreporting2_monitoring_median_config.pb.go b/pkg/monitoring/pb/offchainreporting2_monitoring_median_config.pb.go index b8f817b6eb..45cab20556 100644 --- a/pkg/monitoring/pb/offchainreporting2_monitoring_median_config.pb.go +++ b/pkg/monitoring/pb/offchainreporting2_monitoring_median_config.pb.go @@ -7,10 +7,11 @@ package pb import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" ) const ( diff --git a/pkg/monitoring/pb/offchainreporting2_monitoring_offchain_config.pb.go b/pkg/monitoring/pb/offchainreporting2_monitoring_offchain_config.pb.go index b85d143092..344e8e6813 100644 --- a/pkg/monitoring/pb/offchainreporting2_monitoring_offchain_config.pb.go +++ b/pkg/monitoring/pb/offchainreporting2_monitoring_offchain_config.pb.go @@ -7,10 +7,11 @@ package pb import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" ) const ( diff --git a/pkg/reportingplugins/mercury/mercury_config.pb.go b/pkg/reportingplugins/mercury/mercury_config.pb.go index 042fd9d1ea..829ffb9bd1 100644 --- a/pkg/reportingplugins/mercury/mercury_config.pb.go +++ b/pkg/reportingplugins/mercury/mercury_config.pb.go @@ -7,10 +7,11 @@ package mercury import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" ) const ( diff --git a/pkg/reportingplugins/mercury/v1/mercury_observation_v1.pb.go b/pkg/reportingplugins/mercury/v1/mercury_observation_v1.pb.go index 4d67fa4a69..817084efbb 100644 --- a/pkg/reportingplugins/mercury/v1/mercury_observation_v1.pb.go +++ b/pkg/reportingplugins/mercury/v1/mercury_observation_v1.pb.go @@ -7,10 +7,11 @@ package mercury_v1 import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" ) const ( diff --git a/pkg/reportingplugins/mercury/v2/mercury_observation_v2.pb.go b/pkg/reportingplugins/mercury/v2/mercury_observation_v2.pb.go index f78189f81d..32f1270b9b 100644 --- a/pkg/reportingplugins/mercury/v2/mercury_observation_v2.pb.go +++ b/pkg/reportingplugins/mercury/v2/mercury_observation_v2.pb.go @@ -7,10 +7,11 @@ package mercury_v2 import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" ) const ( diff --git a/pkg/reportingplugins/mercury/v3/mercury_observation_v3.pb.go b/pkg/reportingplugins/mercury/v3/mercury_observation_v3.pb.go index 5b01fe4788..9902247f24 100644 --- a/pkg/reportingplugins/mercury/v3/mercury_observation_v3.pb.go +++ b/pkg/reportingplugins/mercury/v3/mercury_observation_v3.pb.go @@ -7,10 +7,11 @@ package mercury_v3 import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" ) const (