|
| 1 | +package oraclefactory3_1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "google.golang.org/grpc" |
| 8 | + "google.golang.org/protobuf/types/known/durationpb" |
| 9 | + |
| 10 | + "github.com/smartcontractkit/chainlink-common/pkg/logger" |
| 11 | + "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/core/services/oracle" |
| 12 | + reportingplugin "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/core/services/reportingplugin/ocr3_1" |
| 13 | + "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/goplugin" |
| 14 | + "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/net" |
| 15 | + ocr3pb "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/pb/ocr3" |
| 16 | + ocr3_1pb "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/pb/ocr3_1" |
| 17 | + oraclefactorypb "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/pb/oraclefactory" |
| 18 | + ocr3relayer "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/relayer/pluginprovider/ocr3" |
| 19 | + "github.com/smartcontractkit/chainlink-common/pkg/types/core" |
| 20 | +) |
| 21 | + |
| 22 | +var _ core.OracleFactory = (*client)(nil) |
| 23 | + |
| 24 | +type client struct { |
| 25 | + broker *net.BrokerExt |
| 26 | + grpc oraclefactorypb.OracleFactoryClient |
| 27 | + log logger.Logger |
| 28 | + resources []net.Resource |
| 29 | + serviceClient *goplugin.ServiceClient |
| 30 | +} |
| 31 | + |
| 32 | +func NewClient(log logger.Logger, b *net.BrokerExt, conn grpc.ClientConnInterface) *client { |
| 33 | + b = b.WithName("OracleFactoryClient") |
| 34 | + return &client{ |
| 35 | + log: log, |
| 36 | + broker: b, |
| 37 | + serviceClient: goplugin.NewServiceClient(b, conn), |
| 38 | + grpc: oraclefactorypb.NewOracleFactoryClient(conn)} |
| 39 | +} |
| 40 | + |
| 41 | +func (c *client) NewOracle(ctx context.Context, oracleArgs core.OracleArgs) (core.Oracle, error) { |
| 42 | + var resources []net.Resource |
| 43 | + |
| 44 | + serviceName := "ReportingPluginFactoryServer" |
| 45 | + reportingPluginFactoryServerID, reportingPluginFactoryServerRes, err := c.broker.ServeNew( |
| 46 | + serviceName, func(gs *grpc.Server) { |
| 47 | + ocr3_1pb.RegisterReportingPluginFactoryServer(gs, reportingplugin.NewReportingPluginFactoryServer( |
| 48 | + oracleArgs.ReportingPluginFactoryService, |
| 49 | + c.broker, |
| 50 | + )) |
| 51 | + }, |
| 52 | + ) |
| 53 | + if err != nil { |
| 54 | + return nil, fmt.Errorf("failed to serve new %s: %w", serviceName, err) |
| 55 | + } |
| 56 | + resources = append(resources, reportingPluginFactoryServerRes) |
| 57 | + |
| 58 | + serviceName = "ContractTransmitterServer" |
| 59 | + contractTransmitterServerID, contractTransmitterServerRes, err := c.broker.ServeNew( |
| 60 | + serviceName, func(gs *grpc.Server) { |
| 61 | + ocr3pb.RegisterContractTransmitterServer(gs, ocr3relayer.NewContractTransmitterServer( |
| 62 | + oracleArgs.ContractTransmitter, |
| 63 | + )) |
| 64 | + }, |
| 65 | + ) |
| 66 | + if err != nil { |
| 67 | + c.broker.CloseAll(resources...) |
| 68 | + return nil, fmt.Errorf("failed to serve new %s: %w", serviceName, err) |
| 69 | + } |
| 70 | + resources = append(resources, contractTransmitterServerRes) |
| 71 | + |
| 72 | + newOracleRequest := oraclefactorypb.NewOracleRequest{ |
| 73 | + LocalConfig: &oraclefactorypb.LocalConfig{ |
| 74 | + BlockchainTimeout: durationpb.New(oracleArgs.LocalConfig.BlockchainTimeout), |
| 75 | + ContractConfigConfirmations: uint32(oracleArgs.LocalConfig.ContractConfigConfirmations), |
| 76 | + SkipContractConfigConfirmations: oracleArgs.LocalConfig.SkipContractConfigConfirmations, |
| 77 | + ContractConfigTrackerPollInterval: durationpb.New(oracleArgs.LocalConfig.ContractConfigTrackerPollInterval), |
| 78 | + ContractTransmitterTransmitTimeout: durationpb.New(oracleArgs.LocalConfig.ContractTransmitterTransmitTimeout), |
| 79 | + DatabaseTimeout: durationpb.New(oracleArgs.LocalConfig.DatabaseTimeout), |
| 80 | + MinOcr2MaxDurationQuery: durationpb.New(oracleArgs.LocalConfig.MinOCR2MaxDurationQuery), |
| 81 | + ContractConfigLoadTimeout: durationpb.New(oracleArgs.LocalConfig.ContractConfigLoadTimeout), |
| 82 | + DefaultMaxDurationInitialization: durationpb.New(oracleArgs.LocalConfig.DefaultMaxDurationInitialization), |
| 83 | + DevelopmentMode: oracleArgs.LocalConfig.DevelopmentMode, |
| 84 | + }, |
| 85 | + ReportingPluginFactoryServiceId: reportingPluginFactoryServerID, |
| 86 | + ContractTransmitterId: contractTransmitterServerID, |
| 87 | + } |
| 88 | + |
| 89 | + newOracleReply, err := c.grpc.NewOracle(ctx, &newOracleRequest) |
| 90 | + if err != nil { |
| 91 | + c.broker.CloseAll(resources...) |
| 92 | + return nil, fmt.Errorf("error getting new oracle: %w", err) |
| 93 | + } |
| 94 | + |
| 95 | + oracleClientConn, err := c.broker.Dial(newOracleReply.OracleId) |
| 96 | + if err != nil { |
| 97 | + c.broker.CloseAll(resources...) |
| 98 | + return nil, fmt.Errorf("error dialing reporting plugin factory service: %w", err) |
| 99 | + } |
| 100 | + resources = append(resources, net.Resource{ |
| 101 | + Closer: oracleClientConn, |
| 102 | + Name: "OracleClientConn", |
| 103 | + }) |
| 104 | + |
| 105 | + c.resources = append(c.resources, resources...) |
| 106 | + return oracle.NewClient(oracleClientConn), nil |
| 107 | +} |
0 commit comments