From 5d42fb7622b71d61f634ddccd8f85ba12caa3f32 Mon Sep 17 00:00:00 2001 From: Awbrey Hughlett Date: Mon, 9 Sep 2024 17:34:13 -0500 Subject: [PATCH] code cleanup on contract reader (#753) * code cleanup on contract reader * run make generate --- pkg/loop/internal/pb/chain_reader.pb.go | 4 +-- pkg/loop/internal/pb/chain_reader.proto | 4 +-- .../chainreader/chain_reader.go | 36 +++++++++---------- pkg/types/chain_reader.go | 4 +-- 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/pkg/loop/internal/pb/chain_reader.pb.go b/pkg/loop/internal/pb/chain_reader.pb.go index b58e0f1c5..c84bb9fd3 100644 --- a/pkg/loop/internal/pb/chain_reader.pb.go +++ b/pkg/loop/internal/pb/chain_reader.pb.go @@ -489,7 +489,7 @@ func (x *QueryKeyRequest) GetLimitAndSort() *LimitAndSort { return nil } -// BindRequest has arguments for [github.com/smartcontractkit/chainlink-common/pkg/types.ChainReader.Bind/Unbind]. +// BindRequest has arguments for [github.com/smartcontractkit/chainlink-common/pkg/types.ChainReader.Bind]. type BindRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -537,7 +537,7 @@ func (x *BindRequest) GetBindings() []*BoundContract { return nil } -// UnbindRequest has arguments for [github.com/smartcontractkit/chainlink-common/pkg/types.ChainReader.Bind/Unbind]. +// UnbindRequest has arguments for [github.com/smartcontractkit/chainlink-common/pkg/types.ChainReader.Unbind]. type UnbindRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache diff --git a/pkg/loop/internal/pb/chain_reader.proto b/pkg/loop/internal/pb/chain_reader.proto index cf7a1d448..6a30c9185 100644 --- a/pkg/loop/internal/pb/chain_reader.proto +++ b/pkg/loop/internal/pb/chain_reader.proto @@ -34,12 +34,12 @@ message QueryKeyRequest { LimitAndSort limit_and_sort = 3; } -// BindRequest has arguments for [github.com/smartcontractkit/chainlink-common/pkg/types.ChainReader.Bind/Unbind]. +// BindRequest has arguments for [github.com/smartcontractkit/chainlink-common/pkg/types.ChainReader.Bind]. message BindRequest { repeated BoundContract bindings = 1; } -// UnbindRequest has arguments for [github.com/smartcontractkit/chainlink-common/pkg/types.ChainReader.Bind/Unbind]. +// UnbindRequest has arguments for [github.com/smartcontractkit/chainlink-common/pkg/types.ChainReader.Unbind]. message UnbindRequest { repeated BoundContract bindings = 1; } diff --git a/pkg/loop/internal/relayer/pluginprovider/chainreader/chain_reader.go b/pkg/loop/internal/relayer/pluginprovider/chainreader/chain_reader.go index 9bd2cfe54..d9ddf67c9 100644 --- a/pkg/loop/internal/relayer/pluginprovider/chainreader/chain_reader.go +++ b/pkg/loop/internal/relayer/pluginprovider/chainreader/chain_reader.go @@ -360,9 +360,7 @@ func (c *Server) Unbind(ctx context.Context, bindings *pb.UnbindRequest) (*empty func getContractEncodedType(readIdentifier string, possibleTypeProvider any, forEncoding bool) (any, error) { if ctp, ok := possibleTypeProvider.(types.ContractTypeProvider); ok { - val, err := ctp.CreateContractType(readIdentifier, forEncoding) - - return val, err + return ctp.CreateContractType(readIdentifier, forEncoding) } return &map[string]any{}, nil @@ -410,29 +408,27 @@ func newPbBatchGetLatestValuesReply(result types.BatchGetLatestValuesResult, enc func convertBatchGetLatestValuesRequestToProto(request types.BatchGetLatestValuesRequest, encodeWith EncodingVersion) (*pb.BatchGetLatestValuesRequest, error) { requests := make([]*pb.ContractBatch, len(request)) - var i int + var requestIdx int for binding, nextBatch := range request { - reads := make([]*pb.BatchRead, len(nextBatch)) + requests[requestIdx] = &pb.ContractBatch{ + Contract: convertBoundContractToProto(binding), + Reads: make([]*pb.BatchRead, len(nextBatch)), + } - for idx, batchCall := range nextBatch { + for readIdx, batchCall := range nextBatch { versionedParams, err := EncodeVersionedBytes(batchCall.Params, encodeWith) if err != nil { return nil, err } - reads[idx] = &pb.BatchRead{ + requests[requestIdx].Reads[readIdx] = &pb.BatchRead{ ReadName: batchCall.ReadName, Params: versionedParams, } } - requests[i] = &pb.ContractBatch{ - Contract: convertBoundContractToProto(binding), - Reads: reads, - } - - i++ + requestIdx++ } return &pb.BatchGetLatestValuesRequest{Requests: requests}, nil @@ -643,21 +639,21 @@ func convertBatchGetLatestValuesRequestFromProto(pbRequest *pb.BatchGetLatestVal } request := make(types.BatchGetLatestValuesRequest) - for _, batch := range pbRequest.Requests { - binding := convertBoundContractFromProto(batch.Contract) + for _, pbBatch := range pbRequest.Requests { + binding := convertBoundContractFromProto(pbBatch.Contract) if _, ok := request[binding]; !ok { - request[binding] = make([]types.BatchRead, len(batch.Reads)) + request[binding] = make([]types.BatchRead, len(pbBatch.Reads)) } - for idx, readCall := range batch.Reads { - call := types.BatchRead{ReadName: readCall.ReadName} - params, err := getContractEncodedType(binding.ReadIdentifier(readCall.ReadName), impl, true) + for idx, pbReadCall := range pbBatch.Reads { + call := types.BatchRead{ReadName: pbReadCall.ReadName} + params, err := getContractEncodedType(binding.ReadIdentifier(pbReadCall.ReadName), impl, true) if err != nil { return nil, err } - if err = DecodeVersionedBytes(params, readCall.Params); err != nil { + if err = DecodeVersionedBytes(params, pbReadCall.Params); err != nil { return nil, err } diff --git a/pkg/types/chain_reader.go b/pkg/types/chain_reader.go index 94bbe66da..0fd39d1e6 100644 --- a/pkg/types/chain_reader.go +++ b/pkg/types/chain_reader.go @@ -113,8 +113,8 @@ type BoundContract struct { Name string } -func (bc BoundContract) ReadIdentifier(method string) string { - return bc.Address + "-" + bc.Name + "-" + method +func (bc BoundContract) ReadIdentifier(readName string) string { + return bc.Address + "-" + bc.Name + "-" + readName } func (bc BoundContract) String() string {