Skip to content

Commit

Permalink
[chore] Further values improvements (#382)
Browse files Browse the repository at this point in the history
- Make .Proto() on each value type private; to handle nils correctly,
Only the global Proto() function should be used.
- Make FromProto and its variants never return an error. If the values
  library was used to generate a proto (as opposed to generating it
  manually by importing the proto lib), this should be safe despite the
  panics added to the function to deserialize decimals.
- Also remove unused proto imports
  • Loading branch information
cedric-cordenier authored Mar 1, 2024
1 parent 7211450 commit 6e6e1dd
Show file tree
Hide file tree
Showing 19 changed files with 351 additions and 429 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestDataFeedsAggregator_Aggregate_TwoRounds(t *testing.T) {
require.Equal(t, 1.0, newState.FeedInfo[feedIDA.String()].Price)

// validate encodable outcome
val, err := values.FromMapValueProto(outcome.EncodableOutcome)
val := values.FromMapValueProto(outcome.EncodableOutcome)
require.NoError(t, err)
topLevelMap, err := val.Unwrap()
require.NoError(t, err)
Expand Down
14 changes: 2 additions & 12 deletions pkg/capabilities/consensus/ocr3/reporting_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,7 @@ func (r *reportingPlugin) Outcome(outctx ocr3types.OutcomeContext, query types.Q
m[weid] = make(map[ocrcommon.OracleID][]values.Value)
}

val, err := values.FromProto(rq.Observation)
if err != nil {
r.lggr.Errorw("could not unmarshal observation payload", "error", err, "payload", rq)
continue
}

m[weid][o.Observer] = append(m[weid][o.Observer], val)
m[weid][o.Observer] = append(m[weid][o.Observer], values.FromProto(rq.Observation))
}
}

Expand Down Expand Up @@ -214,18 +208,14 @@ func (r *reportingPlugin) Reports(seqNr uint64, outcome ocr3types.Outcome) ([]oc
r.lggr.Errorw("could not append IDs")
continue
}
mv, err := values.FromMapValueProto(outcome.EncodableOutcome)
if err != nil {
r.lggr.Errorw("could not convert outcome to value", "workflowID", id.WorkflowId)
continue
}

enc, err := r.r.getEncoder(id.WorkflowId)
if err != nil {
r.lggr.Errorw("could not retrieve encoder for workflow", "error", err, "workflowID", id.WorkflowId)
continue
}

mv := values.FromMapValueProto(outcome.EncodableOutcome)
report, err := enc.Encode(context.Background(), *mv)
if err != nil {
r.lggr.Errorw("could not encode report for workflow", "error", err, "workflowID", id.WorkflowId)
Expand Down
60 changes: 13 additions & 47 deletions pkg/loop/internal/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,8 @@ func (t *triggerExecutableServer) RegisterTrigger(ctx context.Context, request *
cr := request.CapabilityRequest
md := cr.Metadata

config, err := values.FromProto(cr.Config)
if err != nil {
connCancel()
return nil, err
}

inputs, err := values.FromProto(cr.Inputs)
if err != nil {
connCancel()
return nil, err
}
config := values.FromProto(cr.Config)
inputs := values.FromProto(cr.Inputs)

req := capabilities.CapabilityRequest{
Metadata: capabilities.RequestMetadata{
Expand All @@ -246,17 +237,10 @@ func (t *triggerExecutableServer) UnregisterTrigger(ctx context.Context, request
req := request.CapabilityRequest
md := req.Metadata

config, err := values.FromProto(req.Config)
if err != nil {
return nil, err
}

inputs, err := values.FromProto(req.Inputs)
if err != nil {
return nil, err
}
config := values.FromProto(req.Config)
inputs := values.FromProto(req.Inputs)

err = t.impl.UnregisterTrigger(ctx, capabilities.CapabilityRequest{
err := t.impl.UnregisterTrigger(ctx, capabilities.CapabilityRequest{
Metadata: capabilities.RequestMetadata{
WorkflowID: md.WorkflowId,
WorkflowExecutionID: md.WorkflowExecutionId,
Expand Down Expand Up @@ -347,12 +331,9 @@ func newCallbackExecutableServer(brokerExt *BrokerExt, impl capabilities.Callbac
var _ pb.CallbackExecutableServer = (*callbackExecutableServer)(nil)

func (c *callbackExecutableServer) RegisterToWorkflow(ctx context.Context, req *pb.RegisterToWorkflowRequest) (*emptypb.Empty, error) {
config, err := values.FromProto(req.Config)
if err != nil {
return nil, err
}
config := values.FromProto(req.Config)

err = c.impl.RegisterToWorkflow(ctx, capabilities.RegisterToWorkflowRequest{
err := c.impl.RegisterToWorkflow(ctx, capabilities.RegisterToWorkflowRequest{
Metadata: capabilities.RegistrationMetadata{
WorkflowID: req.Metadata.WorkflowId,
},
Expand All @@ -362,12 +343,9 @@ func (c *callbackExecutableServer) RegisterToWorkflow(ctx context.Context, req *
}

func (c *callbackExecutableServer) UnregisterFromWorkflow(ctx context.Context, req *pb.UnregisterFromWorkflowRequest) (*emptypb.Empty, error) {
config, err := values.FromProto(req.Config)
if err != nil {
return nil, err
}
config := values.FromProto(req.Config)

err = c.impl.UnregisterFromWorkflow(ctx, capabilities.UnregisterFromWorkflowRequest{
err := c.impl.UnregisterFromWorkflow(ctx, capabilities.UnregisterFromWorkflowRequest{
Metadata: capabilities.RegistrationMetadata{
WorkflowID: req.Metadata.WorkflowId,
},
Expand All @@ -390,17 +368,8 @@ func (c *callbackExecutableServer) Execute(ctx context.Context, req *pb.ExecuteR
cr := req.CapabilityRequest
md := cr.Metadata

config, err := values.FromProto(cr.Config)
if err != nil {
connCancel()
return nil, err
}

inputs, err := values.FromProto(cr.Inputs)
if err != nil {
connCancel()
return nil, err
}
config := values.FromProto(cr.Config)
inputs := values.FromProto(cr.Inputs)

r := capabilities.CapabilityRequest{
Metadata: capabilities.RequestMetadata{
Expand Down Expand Up @@ -535,12 +504,9 @@ func (c *callbackServer) SendResponse(ctx context.Context, req *pb.CapabilityRes
return nil, errors.New("cannot send response: the underlying channel has been closed")
}

val, err := values.FromProto(req.Value)
if err != nil {
return nil, err
}
val := values.FromProto(req.Value)

err = nil
var err error
if req.Error != "" {
err = errors.New(req.Error)
}
Expand Down
109 changes: 53 additions & 56 deletions pkg/loop/internal/pb/codec.pb.go

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

2 changes: 0 additions & 2 deletions pkg/loop/internal/pb/codec.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ option go_package = "github.com/smartcontractkit/chainlink-common/pkg/loop/inter

package loop;

import "google/protobuf/empty.proto";

service Codec {
rpc GetEncoding(GetEncodingRequest) returns (GetEncodingResponse);
rpc GetDecoding(GetDecodingRequest) returns (GetDecodingResponse);
Expand Down
Loading

0 comments on commit 6e6e1dd

Please sign in to comment.