Skip to content

Commit

Permalink
Add chain_reader.go with ChainReader type defs, add ChainReader() to …
Browse files Browse the repository at this point in the history
…PluginProvider

Also add loop definitions for ChainReader Service, update medianProviderClient,
 and regenerate protobuf files
  • Loading branch information
reductionista committed Oct 18, 2023
1 parent 65aceb8 commit 98f7b4b
Show file tree
Hide file tree
Showing 9 changed files with 1,198 additions and 214 deletions.
63 changes: 63 additions & 0 deletions pkg/loop/internal/chain_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package internal

import (
"context"
"encoding/json"

"github.com/smartcontractkit/chainlink-relay/pkg/loop/internal/pb"
"github.com/smartcontractkit/chainlink-relay/pkg/types"
)

var _ types.ChainReader = (*chainReaderClient)(nil)

type chainReaderClient struct {
*brokerExt
grpc pb.ChainReaderClient
}

func (c *chainReaderClient) GetLatestValue(ctx context.Context, bc types.BoundContract, method string, params, retValues any) ([]byte, error) {
boundContract := pb.BoundContract{Name: bc.Name, Address: bc.Address, Pending: bc.Pending}
jsonParams, err := json.Marshal(params)
if err != nil {
return nil, err
}
jsonRetValues, err := json.Marshal(retValues)
if err != nil {
return nil, err
}

reply, err := c.grpc.GetLatestValue(ctx, &pb.GetLatestValueRequest{Bc: &boundContract, Method: method, Params: jsonParams, ReturnValues: jsonRetValues})
if err != nil {
return nil, err
}
return reply.RetValues, nil
}

var _ pb.ChainReaderServer = (*chainReaderServer)(nil)

type chainReaderServer struct {
pb.UnimplementedChainReaderServer
impl types.ChainReader
}

func (c *chainReaderServer) GetLatestValue(ctx context.Context, request *pb.GetLatestValueRequest) (*pb.GetLatestValueReply, error) {
var bc types.BoundContract
bc.Name = request.Bc.Name[:]
bc.Address = request.Bc.Address[:]
bc.Pending = request.Bc.Pending
retValues, err := c.impl.GetLatestValue(ctx, bc, request.Method, request.Params, request.ReturnValues)
if err != nil {
return nil, err
}
return &pb.GetLatestValueReply{RetValues: retValues}, nil
}

func (c *chainReaderServer) RegisterEventFilter(ctx context.Context, in *pb.RegisterEventFilterRequest) (*pb.RegisterEventFilterReply, error) {
return nil, nil
}
func (c *chainReaderServer) UnregisterEventFilter(ctx context.Context, in *pb.UnregisterEventFilterRequest) (*pb.RegisterEventFilterReply, error) {
return nil, nil
}
func (c *chainReaderServer) QueryEvents(ctx context.Context, in *pb.QueryEventsRequest) (*pb.QueryEventsReply, error) {
return nil, nil
}
9 changes: 8 additions & 1 deletion pkg/loop/internal/median.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (m *PluginMedianClient) NewMedianFactory(ctx context.Context, provider type
pb.RegisterOffchainConfigDigesterServer(s, &offchainConfigDigesterServer{impl: provider.OffchainConfigDigester()})
pb.RegisterContractConfigTrackerServer(s, &contractConfigTrackerServer{impl: provider.ContractConfigTracker()})
pb.RegisterContractTransmitterServer(s, &contractTransmitterServer{impl: provider.ContractTransmitter()})
pb.RegisterChainReaderServer(s, &chainReaderServer{impl: provider.ChainReader()})
pb.RegisterReportCodecServer(s, &reportCodecServer{impl: provider.ReportCodec()})
pb.RegisterMedianContractServer(s, &medianContractServer{impl: provider.MedianContract()})
pb.RegisterOnchainConfigCodecServer(s, &onchainConfigCodecServer{impl: provider.OnchainConfigCodec()})
Expand Down Expand Up @@ -172,8 +173,9 @@ var (
type medianProviderClient struct {
*pluginProviderClient
reportCodec median.ReportCodec
medianContract median.MedianContract
onchainConfigCodec median.OnchainConfigCodec
medianContract median.MedianContract
chainReader types.ChainReader
}

func (m *medianProviderClient) ClientConn() grpc.ClientConnInterface { return m.cc }
Expand All @@ -183,6 +185,7 @@ func newMedianProviderClient(b *brokerExt, cc grpc.ClientConnInterface) *medianP
m.reportCodec = &reportCodecClient{b, pb.NewReportCodecClient(m.cc)}
m.medianContract = &medianContractClient{pb.NewMedianContractClient(m.cc)}
m.onchainConfigCodec = &onchainConfigCodecClient{b, pb.NewOnchainConfigCodecClient(m.cc)}
m.chainReader = &chainReaderClient{b, pb.NewChainReaderClient(m.cc)}
return m
}

Expand All @@ -198,6 +201,10 @@ func (m *medianProviderClient) OnchainConfigCodec() median.OnchainConfigCodec {
return m.onchainConfigCodec
}

func (m *medianProviderClient) ChainReader() types.ChainReader {
return m.chainReader
}

var _ median.ReportCodec = (*reportCodecClient)(nil)

type reportCodecClient struct {
Expand Down
Loading

0 comments on commit 98f7b4b

Please sign in to comment.