Skip to content

Commit 51ef3ef

Browse files
committed
wip
1 parent 6efadaa commit 51ef3ef

13 files changed

Lines changed: 4726 additions & 0 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package oraclefactory3_1
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"google.golang.org/grpc"
8+
9+
"github.com/smartcontractkit/libocr/offchainreporting2plus/types"
10+
11+
"github.com/smartcontractkit/chainlink-common/pkg/logger"
12+
oraclesrv "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/core/services/oracle"
13+
"github.com/smartcontractkit/chainlink-common/pkg/loop/internal/core/services/reportingplugin/ocr3"
14+
"github.com/smartcontractkit/chainlink-common/pkg/loop/internal/net"
15+
oraclepb "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/pb/oracle"
16+
oraclefactorypb "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/pb/oraclefactory"
17+
ocr3relayer "github.com/smartcontractkit/chainlink-common/pkg/loop/internal/relayer/pluginprovider/ocr3"
18+
"github.com/smartcontractkit/chainlink-common/pkg/types/core"
19+
)
20+
21+
var _ oraclefactorypb.OracleFactoryServer = (*server)(nil)
22+
23+
type server struct {
24+
oraclefactorypb.UnimplementedOracleFactoryServer
25+
26+
broker *net.BrokerExt
27+
impl core.OracleFactory
28+
log logger.Logger
29+
resources net.Resources
30+
31+
Name string
32+
}
33+
34+
func NewServer(log logger.Logger, impl core.OracleFactory, broker *net.BrokerExt) (*server, net.Resource) {
35+
name := "OracleFactoryServer3_1"
36+
newServer := &server{
37+
log: log,
38+
impl: impl,
39+
broker: broker.WithName(name),
40+
resources: make(net.Resources, 0),
41+
}
42+
43+
return newServer, net.Resource{
44+
Name: name,
45+
Closer: newServer,
46+
}
47+
}
48+
49+
func (s *server) Close() error {
50+
s.broker.CloseAll(s.resources...)
51+
return nil
52+
}
53+
54+
func (s *server) NewOracle(ctx context.Context, req *oraclefactorypb.NewOracleRequest) (*oraclefactorypb.NewOracleReply, error) {
55+
var resources []net.Resource
56+
57+
serviceName := "ReportingPluginFactory3_1"
58+
reportingPluginFactoryServiceConn, err := s.broker.Dial(req.ReportingPluginFactoryServiceId)
59+
if err != nil {
60+
return nil, fmt.Errorf("error dialing %s service: %w", serviceName, err)
61+
}
62+
resources = append(resources, net.Resource{
63+
Closer: reportingPluginFactoryServiceConn,
64+
Name: serviceName,
65+
})
66+
67+
serviceName = "ContractTransmitter"
68+
contractTransmitterConn, err := s.broker.Dial(req.ContractTransmitterId)
69+
if err != nil {
70+
return nil, fmt.Errorf("error dialing %s service: %w", serviceName, err)
71+
}
72+
resources = append(resources, net.Resource{
73+
Closer: contractTransmitterConn,
74+
Name: serviceName,
75+
})
76+
77+
args := core.OracleArgs{
78+
LocalConfig: types.LocalConfig{
79+
BlockchainTimeout: req.LocalConfig.BlockchainTimeout.AsDuration(),
80+
ContractConfigConfirmations: uint16(req.LocalConfig.ContractConfigConfirmations), // #nosec G115
81+
SkipContractConfigConfirmations: req.LocalConfig.SkipContractConfigConfirmations,
82+
ContractConfigTrackerPollInterval: req.LocalConfig.ContractConfigTrackerPollInterval.AsDuration(),
83+
ContractTransmitterTransmitTimeout: req.LocalConfig.ContractTransmitterTransmitTimeout.AsDuration(),
84+
DatabaseTimeout: req.LocalConfig.DatabaseTimeout.AsDuration(),
85+
ContractConfigLoadTimeout: req.LocalConfig.ContractConfigLoadTimeout.AsDuration(),
86+
DefaultMaxDurationInitialization: req.LocalConfig.DefaultMaxDurationInitialization.AsDuration(),
87+
MinOCR2MaxDurationQuery: req.LocalConfig.MinOcr2MaxDurationQuery.AsDuration(),
88+
DevelopmentMode: req.LocalConfig.DevelopmentMode,
89+
},
90+
ReportingPluginFactoryService: ocr3.NewReportingPluginFactoryClient(
91+
s.broker,
92+
reportingPluginFactoryServiceConn,
93+
),
94+
ContractTransmitter: ocr3relayer.NewContractTransmitterClient(s.broker, contractTransmitterConn),
95+
}
96+
97+
oracle, err := s.impl.NewOracle(ctx, args)
98+
if err != nil {
99+
return nil, fmt.Errorf("NewOracle call failed: %w", err)
100+
}
101+
102+
oracleServer, oracleServerRes := oraclesrv.NewServer(s.log, oracle, s.broker)
103+
resources = append(resources, oracleServerRes)
104+
oracleID, oracleRes, err := s.broker.ServeNew("Oracle", func(gs *grpc.Server) {
105+
oraclepb.RegisterOracleServer(gs, oracleServer)
106+
})
107+
if err != nil {
108+
s.broker.CloseAll(resources...)
109+
return nil, fmt.Errorf("failed to serve new oracle: %w", err)
110+
}
111+
resources = append(resources, oracleRes)
112+
s.resources = append(s.resources, resources...)
113+
return &oraclefactorypb.NewOracleReply{OracleId: oracleID}, nil
114+
}

0 commit comments

Comments
 (0)