Skip to content

Commit

Permalink
code cleanup on contract reader (#753)
Browse files Browse the repository at this point in the history
* code cleanup on contract reader

* run make generate
  • Loading branch information
EasterTheBunny authored Sep 9, 2024
1 parent 3c6df3a commit 5d42fb7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 26 deletions.
4 changes: 2 additions & 2 deletions pkg/loop/internal/pb/chain_reader.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/loop/internal/pb/chain_reader.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/types/chain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5d42fb7

Please sign in to comment.