Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions pkg/loop/internal/core/services/oraclefactory3_1/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package oraclefactory3_1

import (
"context"
"fmt"

"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/durationpb"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/loop/internal/core/services/oracle"
reportingplugin "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/core/services/reportingplugin/ocr3_1"
"github.com/smartcontractkit/chainlink-common/pkg/loop/internal/goplugin"
"github.com/smartcontractkit/chainlink-common/pkg/loop/internal/net"
ocr3pb "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/pb/ocr3"
ocr3_1pb "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/pb/ocr3_1"
oraclefactorypb "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/pb/oraclefactory"
ocr3relayer "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/relayer/pluginprovider/ocr3"
"github.com/smartcontractkit/chainlink-common/pkg/types/core"
)

var _ core.OracleFactory = (*client)(nil)

type client struct {
broker *net.BrokerExt
grpc oraclefactorypb.OracleFactoryClient
log logger.Logger
resources []net.Resource
serviceClient *goplugin.ServiceClient
}

func NewClient(log logger.Logger, b *net.BrokerExt, conn grpc.ClientConnInterface) *client {
b = b.WithName("OracleFactoryClient")
return &client{
log: log,
broker: b,
serviceClient: goplugin.NewServiceClient(b, conn),
grpc: oraclefactorypb.NewOracleFactoryClient(conn)}
}

func (c *client) NewOracle(ctx context.Context, oracleArgs core.OracleArgs) (core.Oracle, error) {
var resources []net.Resource

serviceName := "ReportingPluginFactoryServer"
reportingPluginFactoryServerID, reportingPluginFactoryServerRes, err := c.broker.ServeNew(
serviceName, func(gs *grpc.Server) {
ocr3_1pb.RegisterReportingPluginFactoryServer(gs, reportingplugin.NewReportingPluginFactoryServer(
oracleArgs.ReportingPluginFactoryService,
c.broker,
))
},
)
if err != nil {
return nil, fmt.Errorf("failed to serve new %s: %w", serviceName, err)
}
resources = append(resources, reportingPluginFactoryServerRes)

serviceName = "ContractTransmitterServer"
contractTransmitterServerID, contractTransmitterServerRes, err := c.broker.ServeNew(
serviceName, func(gs *grpc.Server) {
ocr3pb.RegisterContractTransmitterServer(gs, ocr3relayer.NewContractTransmitterServer(
oracleArgs.ContractTransmitter,
))
},
)
if err != nil {
c.broker.CloseAll(resources...)
return nil, fmt.Errorf("failed to serve new %s: %w", serviceName, err)
}
resources = append(resources, contractTransmitterServerRes)

newOracleRequest := oraclefactorypb.NewOracleRequest{
LocalConfig: &oraclefactorypb.LocalConfig{
BlockchainTimeout: durationpb.New(oracleArgs.LocalConfig.BlockchainTimeout),
ContractConfigConfirmations: uint32(oracleArgs.LocalConfig.ContractConfigConfirmations),
SkipContractConfigConfirmations: oracleArgs.LocalConfig.SkipContractConfigConfirmations,
ContractConfigTrackerPollInterval: durationpb.New(oracleArgs.LocalConfig.ContractConfigTrackerPollInterval),
ContractTransmitterTransmitTimeout: durationpb.New(oracleArgs.LocalConfig.ContractTransmitterTransmitTimeout),
DatabaseTimeout: durationpb.New(oracleArgs.LocalConfig.DatabaseTimeout),
MinOcr2MaxDurationQuery: durationpb.New(oracleArgs.LocalConfig.MinOCR2MaxDurationQuery),
ContractConfigLoadTimeout: durationpb.New(oracleArgs.LocalConfig.ContractConfigLoadTimeout),
DefaultMaxDurationInitialization: durationpb.New(oracleArgs.LocalConfig.DefaultMaxDurationInitialization),
DevelopmentMode: oracleArgs.LocalConfig.DevelopmentMode,
},
ReportingPluginFactoryServiceId: reportingPluginFactoryServerID,
ContractTransmitterId: contractTransmitterServerID,
}

newOracleReply, err := c.grpc.NewOracle(ctx, &newOracleRequest)
if err != nil {
c.broker.CloseAll(resources...)
return nil, fmt.Errorf("error getting new oracle: %w", err)
}

oracleClientConn, err := c.broker.Dial(newOracleReply.OracleId)
if err != nil {
c.broker.CloseAll(resources...)
return nil, fmt.Errorf("error dialing reporting plugin factory service: %w", err)
}
resources = append(resources, net.Resource{
Closer: oracleClientConn,
Name: "OracleClientConn",
})

c.resources = append(c.resources, resources...)
return oracle.NewClient(oracleClientConn), nil
}
114 changes: 114 additions & 0 deletions pkg/loop/internal/core/services/oraclefactory3_1/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package oraclefactory3_1

import (
"context"
"fmt"

"google.golang.org/grpc"

"github.com/smartcontractkit/libocr/offchainreporting2plus/types"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
oraclesrv "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/core/services/oracle"
"github.com/smartcontractkit/chainlink-common/pkg/loop/internal/core/services/reportingplugin/ocr3"
"github.com/smartcontractkit/chainlink-common/pkg/loop/internal/net"
oraclepb "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/pb/oracle"
oraclefactorypb "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/pb/oraclefactory"
ocr3relayer "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/relayer/pluginprovider/ocr3"
"github.com/smartcontractkit/chainlink-common/pkg/types/core"
)

var _ oraclefactorypb.OracleFactoryServer = (*server)(nil)

type server struct {
oraclefactorypb.UnimplementedOracleFactoryServer

broker *net.BrokerExt
impl core.OracleFactory
log logger.Logger
resources net.Resources

Name string
}

func NewServer(log logger.Logger, impl core.OracleFactory, broker *net.BrokerExt) (*server, net.Resource) {
name := "OracleFactoryServer3_1"
newServer := &server{
log: log,
impl: impl,
broker: broker.WithName(name),
resources: make(net.Resources, 0),
}

return newServer, net.Resource{
Name: name,
Closer: newServer,
}
}

func (s *server) Close() error {
s.broker.CloseAll(s.resources...)
return nil
}

func (s *server) NewOracle(ctx context.Context, req *oraclefactorypb.NewOracleRequest) (*oraclefactorypb.NewOracleReply, error) {
var resources []net.Resource

serviceName := "ReportingPluginFactory3_1"
reportingPluginFactoryServiceConn, err := s.broker.Dial(req.ReportingPluginFactoryServiceId)
if err != nil {
return nil, fmt.Errorf("error dialing %s service: %w", serviceName, err)
}
resources = append(resources, net.Resource{
Closer: reportingPluginFactoryServiceConn,
Name: serviceName,
})

serviceName = "ContractTransmitter"
contractTransmitterConn, err := s.broker.Dial(req.ContractTransmitterId)
if err != nil {
return nil, fmt.Errorf("error dialing %s service: %w", serviceName, err)
}
resources = append(resources, net.Resource{
Closer: contractTransmitterConn,
Name: serviceName,
})

args := core.OracleArgs{
LocalConfig: types.LocalConfig{
BlockchainTimeout: req.LocalConfig.BlockchainTimeout.AsDuration(),
ContractConfigConfirmations: uint16(req.LocalConfig.ContractConfigConfirmations), // #nosec G115
SkipContractConfigConfirmations: req.LocalConfig.SkipContractConfigConfirmations,
ContractConfigTrackerPollInterval: req.LocalConfig.ContractConfigTrackerPollInterval.AsDuration(),
ContractTransmitterTransmitTimeout: req.LocalConfig.ContractTransmitterTransmitTimeout.AsDuration(),
DatabaseTimeout: req.LocalConfig.DatabaseTimeout.AsDuration(),
ContractConfigLoadTimeout: req.LocalConfig.ContractConfigLoadTimeout.AsDuration(),
DefaultMaxDurationInitialization: req.LocalConfig.DefaultMaxDurationInitialization.AsDuration(),
MinOCR2MaxDurationQuery: req.LocalConfig.MinOcr2MaxDurationQuery.AsDuration(),
DevelopmentMode: req.LocalConfig.DevelopmentMode,
},
ReportingPluginFactoryService: ocr3.NewReportingPluginFactoryClient(
s.broker,
reportingPluginFactoryServiceConn,
),
ContractTransmitter: ocr3relayer.NewContractTransmitterClient(s.broker, contractTransmitterConn),
}

oracle, err := s.impl.NewOracle(ctx, args)
if err != nil {
return nil, fmt.Errorf("NewOracle call failed: %w", err)
}

oracleServer, oracleServerRes := oraclesrv.NewServer(s.log, oracle, s.broker)
resources = append(resources, oracleServerRes)
oracleID, oracleRes, err := s.broker.ServeNew("Oracle", func(gs *grpc.Server) {
oraclepb.RegisterOracleServer(gs, oracleServer)
})
if err != nil {
s.broker.CloseAll(resources...)
return nil, fmt.Errorf("failed to serve new oracle: %w", err)
}
resources = append(resources, oracleRes)
s.resources = append(s.resources, resources...)
return &oraclefactorypb.NewOracleReply{OracleId: oracleID}, nil
}
Loading
Loading