Skip to content

Commit

Permalink
[KS-55] Minor fixes to Engine and Write target (#12135)
Browse files Browse the repository at this point in the history
1. Fix default gas limit in the Write target
2. Use mocked IDs with exactly 32 bytes
3. Pass observations as a list (of size 1)
4. More logging
  • Loading branch information
bolekk authored Feb 22, 2024
1 parent b60acb4 commit c147896
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
16 changes: 10 additions & 6 deletions core/capabilities/targets/write_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import (
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
"github.com/smartcontractkit/chainlink/v2/core/chains/legacyevm"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/keystone/generated/forwarder"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/pipeline"
)

var forwardABI = evmtypes.MustGetABI(forwarder.KeystoneForwarderMetaData.ABI)

func InitializeWrite(registry commontypes.CapabilitiesRegistry, legacyEVMChains legacyevm.LegacyChainContainer) error {
func InitializeWrite(registry commontypes.CapabilitiesRegistry, legacyEVMChains legacyevm.LegacyChainContainer, lggr logger.Logger) error {
for _, chain := range legacyEVMChains.Slice() {
capability := NewEvmWrite(chain)
capability := NewEvmWrite(chain, lggr)
if err := registry.Add(context.TODO(), capability); err != nil {
return err
}
Expand All @@ -41,12 +42,15 @@ var (
_ capabilities.ActionCapability = &EvmWrite{}
)

const defaultGasLimit = 200000

type EvmWrite struct {
chain legacyevm.Chain
capabilities.CapabilityInfo
lggr logger.Logger
}

func NewEvmWrite(chain legacyevm.Chain) *EvmWrite {
func NewEvmWrite(chain legacyevm.Chain, lggr logger.Logger) *EvmWrite {
// generate ID based on chain selector
name := fmt.Sprintf("write_%v", chain.ID())
chainName, err := chainselectors.NameFromChainId(chain.ID().Uint64())
Expand All @@ -64,6 +68,7 @@ func NewEvmWrite(chain legacyevm.Chain) *EvmWrite {
return &EvmWrite{
chain,
info,
lggr.Named("EvmWrite"),
}
}

Expand Down Expand Up @@ -153,6 +158,7 @@ func encodePayload(args []any, rawSelector string) ([]byte, error) {
}

func (cap *EvmWrite) Execute(ctx context.Context, callback chan<- capabilities.CapabilityResponse, request capabilities.CapabilityRequest) error {
cap.lggr.Debugw("Execute", "request", request)
// TODO: idempotency

// TODO: extract into ChainWriter?
Expand Down Expand Up @@ -184,8 +190,6 @@ func (cap *EvmWrite) Execute(ctx context.Context, callback chan<- capabilities.C

// TODO: validate encoded report is prefixed with workflowID and executionID that match the request meta

// unlimited gas in the MVP demo
gasLimit := 0
// No signature validation in the MVP demo
signatures := [][]byte{}

Expand All @@ -208,7 +212,7 @@ func (cap *EvmWrite) Execute(ctx context.Context, callback chan<- capabilities.C
FromAddress: config.FromAddress().Address(),
ToAddress: config.ForwarderAddress().Address(),
EncodedPayload: calldata,
FeeLimit: uint32(gasLimit),
FeeLimit: uint32(defaultGasLimit),
Meta: txMeta,
Strategy: strategy,
Checker: checker,
Expand Down
3 changes: 2 additions & 1 deletion core/capabilities/targets/write_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey"

Expand Down Expand Up @@ -45,7 +46,7 @@ func TestEvmWrite(t *testing.T) {
evmcfg := evmtest.NewChainScopedConfig(t, cfg)
chain.On("Config").Return(evmcfg)

capability := targets.NewEvmWrite(chain)
capability := targets.NewEvmWrite(chain, logger.TestLogger(t))
ctx := testutils.Context(t)

config, err := values.NewMap(map[string]any{
Expand Down
2 changes: 1 addition & 1 deletion core/services/workflows/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (d *Delegate) ServicesForSpec(spec job.Job) ([]job.ServiceCtx, error) {

func NewDelegate(logger logger.Logger, registry types.CapabilitiesRegistry, legacyEVMChains legacyevm.LegacyChainContainer) *Delegate {
// NOTE: we temporarily do registration inside NewDelegate, this will be moved out of job specs in the future
_ = targets.InitializeWrite(registry, legacyEVMChains)
_ = targets.InitializeWrite(registry, legacyEVMChains, logger)

return &Delegate{logger: logger, registry: registry}
}
Expand Down
39 changes: 26 additions & 13 deletions core/services/workflows/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import (
)

const (
mockedWorkflowID = "aaaaaaaa-f4d1-422f-a4b2-8ce0a1075f0a"
mockedExecutionID = "bbbbbbbb-f4d1-422f-a4b2-8ce0a1075f0a"
mockedTriggerID = "cccccccc-5cac-4071-be62-0152dd9adb0f"
// NOTE: max 32 bytes per ID - consider enforcing exactly 32 bytes?
mockedWorkflowID = "aaaaaaaaaa0000000000000000000000"
mockedExecutionID = "bbbbbbbbbb0000000000000000000000"
mockedTriggerID = "cccccccccc0000000000000000000000"
)

type Engine struct {
Expand Down Expand Up @@ -143,13 +144,20 @@ func (e *Engine) handleExecution(ctx context.Context, event capabilities.Capabil
if err != nil {
return err
}
if len(results.Underlying) == 0 {
return fmt.Errorf("consensus returned no reports")
}
if len(results.Underlying) > 1 {
e.logger.Debugw("consensus returned more than one report")
}

_, err = e.handleTarget(ctx, results)
// we're expecting exactly one report
_, err = e.handleTarget(ctx, results.Underlying[0])
return err
}

func (e *Engine) handleTarget(ctx context.Context, resp *values.List) (*values.List, error) {

func (e *Engine) handleTarget(ctx context.Context, resp values.Value) (*values.List, error) {
e.logger.Debugw("handle target")
inputs := map[string]values.Value{
"report": resp,
}
Expand All @@ -158,23 +166,28 @@ func (e *Engine) handleTarget(ctx context.Context, resp *values.List) (*values.L
Inputs: &values.Map{Underlying: inputs},
Config: e.targetConfig,
Metadata: capabilities.RequestMetadata{
WorkflowID: mockedWorkflowID,
WorkflowID: mockedWorkflowID,
WorkflowExecutionID: mockedExecutionID,
},
}
return capabilities.ExecuteSync(ctx, e.target, tr)
}

func (e *Engine) handleConsensus(ctx context.Context, resp capabilities.CapabilityResponse) (*values.List, error) {
e.logger.Debugw("running consensus", "resp", resp)
inputs := map[string]values.Value{
"observations": resp.Value,
}
func (e *Engine) handleConsensus(ctx context.Context, event capabilities.CapabilityResponse) (*values.List, error) {
e.logger.Debugw("running consensus", "event", event)
cr := capabilities.CapabilityRequest{
Metadata: capabilities.RequestMetadata{
WorkflowID: mockedWorkflowID,
WorkflowExecutionID: mockedExecutionID,
},
Inputs: &values.Map{Underlying: inputs},
Inputs: &values.Map{
Underlying: map[string]values.Value{
// each node provides a single observation - outputs of mercury trigger
"observations": &values.List{
Underlying: []values.Value{event.Value},
},
},
},
Config: e.consensusConfig,
}
return capabilities.ExecuteSync(ctx, e.consensus, cr)
Expand Down

0 comments on commit c147896

Please sign in to comment.