Skip to content

Commit

Permalink
refactor(workflow): removes indexing of secrets URL in contract
Browse files Browse the repository at this point in the history
  • Loading branch information
MStreet3 committed Nov 10, 2024
1 parent c2eee53 commit e181a48
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 146 deletions.
2 changes: 1 addition & 1 deletion contracts/src/v0.8/workflow/dev/WorkflowRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ contract WorkflowRegistry is DONAccessControl, Ownable2StepMsgSender, ITypeAndVe
uint32 indexed donID,
string workflowName
);
event WorkflowForceUpdateSecretsRequestedV1(string indexed secretsURL, address indexed owner, string workflowName);
event WorkflowForceUpdateSecretsRequestedV1(string secretsURL, address indexed owner, string workflowName);
event RegistryLockedV1(address lockedBy);
event RegistryUnlockedV1(address unlockedBy);

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
GETH_VERSION: 1.13.8
workflow_registry_wrapper: ../../../contracts/solc/v0.8.24/WorkflowRegistry/WorkflowRegistry.abi ../../../contracts/solc/v0.8.24/WorkflowRegistry/WorkflowRegistry.bin 052d00d20224a7068b9d6735a3a250c18048d0eb835a5ec59d2e544507e3c7be
workflow_registry_wrapper: ../../../contracts/solc/v0.8.24/WorkflowRegistry/WorkflowRegistry.abi ../../../contracts/solc/v0.8.24/WorkflowRegistry/WorkflowRegistry.bin e142b10b4b61b8c97dcc679822c9ca7fd5663e3f3d164fddb1f59c420b224bdf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/test-go/testify/assert"

"github.com/smartcontractkit/chainlink-common/pkg/services/servicetest"
Expand All @@ -34,10 +33,10 @@ func Test_SecretsWorker(t *testing.T) {

giveTicker = signalers.MakeTicker(ctx.Done(), 500*time.Millisecond)
giveSecretsURL = "https://original-url.com"
giveHash = crypto.Keccak256Hash([]byte(giveSecretsURL))
donID = uint32(1)
giveWorkflow = RegisterWorkflowCMD{
Name: "test-wf",
DonID: uint32(1),
DonID: donID,
Status: uint8(0),
SecretsURL: giveSecretsURL,
}
Expand Down Expand Up @@ -94,17 +93,17 @@ func Test_SecretsWorker(t *testing.T) {
giveCfg.StartBlockNum = uint64(0)

// Seed the DB
_, err = orm.Update(ctx, giveSecretsURL, giveContents)
gotID, err := orm.Update(ctx, giveSecretsURL, giveContents)
require.NoError(t, err)

gotSecretsURL, err := orm.GetSecretsURL(ctx, giveHash.Hex())
gotSecretsURL, err := orm.GetSecretsURLByID(ctx, gotID)
assert.NoError(t, err)
assert.Equal(t, giveSecretsURL, gotSecretsURL)

// verify the DB
gotArtifact, err := orm.GetArtifactByHash(ctx, giveHash.Hex())
contents, err := orm.GetContents(ctx, giveSecretsURL)
assert.NoError(t, err)
assert.Equal(t, gotArtifact.Contents, giveContents)
assert.Equal(t, contents, giveContents)

worker := syncer.NewWorkflowRegistry(
lggr,
Expand All @@ -116,7 +115,7 @@ func Test_SecretsWorker(t *testing.T) {
)

// generate a log event
updateAuthorizedAddress(t, backendTH, wfRegistryC, []common.Address{backendTH.ContractsOwner.From}, true)
updateAuthorizedAddress(t, backendTH, wfRegistryC, donID, []common.Address{backendTH.ContractsOwner.From}, true)
updateAllowedDONs(t, backendTH, wfRegistryC, []uint32{1}, true)
registerWorkflow(t, backendTH, wfRegistryC, giveWorkflow)

Expand All @@ -132,22 +131,23 @@ func Test_SecretsWorker(t *testing.T) {

// Require the secrets contents to eventually be updated
require.Eventually(t, func() bool {
secrets, err := orm.GetArtifactByHash(ctx, giveHash.Hex())
secrets, err := orm.GetContents(ctx, giveSecretsURL)
lggr.Debugf("got secrets %v", secrets)
require.NoError(t, err)
return secrets.Contents == wantContents
return secrets == wantContents
}, 5*time.Second, time.Second)
}

func updateAuthorizedAddress(
t *testing.T,
th *testutils.EVMBackendTH,
wfRegC *workflow_registry_wrapper.WorkflowRegistry,
donID uint32,
addresses []common.Address,
allowed bool,
) {
t.Helper()
_, err := wfRegC.UpdateAuthorizedAddresses(th.ContractsOwner, addresses, allowed)
_, err := wfRegC.UpdateDONPermissions(th.ContractsOwner, donID, addresses, allowed)
require.NoError(t, err, "failed to update authorised addresses")
th.Backend.Commit()
th.Backend.Commit()
Expand Down
15 changes: 4 additions & 11 deletions core/services/workflows/syncer/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,12 @@ func (h *forceUpdateSecretsHandler) Handle(
ctx context.Context,
event WorkflowRegistryEvent,
) error {
hash, err := getURLHash(event)
url, err := getSecretsURL(event)
if err != nil {
h.lggr.Errorf("failed to get URL hash", err)
return err
}

// Fetch the secrets url from ORM
url, err := h.orm.GetSecretsURL(ctx, hash)
if err != nil {
h.lggr.Errorf("failed to get secrets URL for hash %x", hash)
return err
}

// Fetch the contents of the secrets file from the url via the fetcher
secrets, err := h.fetcher(ctx, url)
if err != nil {
Expand All @@ -80,10 +73,10 @@ func (h *forceUpdateSecretsHandler) Handle(
return nil
}

func getURLHash(event WorkflowRegistryEvent) (string, error) {
hash, ok := event.Data["SecretsURL"].([]byte)
func getSecretsURL(event WorkflowRegistryEvent) (string, error) {
url, ok := event.Data["SecretsURL"].(string)
if !ok {
return "", fmt.Errorf("failed to fetch secrets hash from event : %+v", event.Data)
}
return string(hash), nil
return url, nil
}
42 changes: 21 additions & 21 deletions core/services/workflows/syncer/handler_mock.go

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

33 changes: 12 additions & 21 deletions core/services/workflows/syncer/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/capabilities/triggers/logevent/logeventcap"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/capabilities/workflows/common"
"github.com/smartcontractkit/chainlink/v2/core/services/workflows/syncer/mocks"
"github.com/smartcontractkit/chainlink/v2/core/utils/matches"

Expand All @@ -29,17 +28,14 @@ func Test_Handler(t *testing.T) {
mockORM := mocks.NewORM(t)
ctx := testutils.Context(t)
giveURL := "https://original-url.com"
hash := common.Keccak256Hash([]byte(giveURL))

giveEvent := WorkflowRegistryEvent{
Output: logeventcap.Output{
Data: map[string]any{
"SecretsURL": []byte(hash),
"SecretsURL": giveURL,
},
},
}

mockORM.EXPECT().GetSecretsURL(matches.AnyContext, hash).Return(giveURL, nil)
fetcher := func(_ context.Context, _ string) ([]byte, error) {
return []byte("contents"), nil
}
Expand All @@ -49,30 +45,26 @@ func Test_Handler(t *testing.T) {
require.NoError(t, err)
})

t.Run("fails to get secrets", func(t *testing.T) {
t.Run("fails to get secrets url", func(t *testing.T) {
mockORM := mocks.NewORM(t)
ctx := testutils.Context(t)
giveURL := "http://example.com"
hash := common.Keccak256Hash([]byte(giveURL))
mockORM.EXPECT().GetSecretsURL(matches.AnyContext, hash).Return("", assert.AnError)
h := newForceUpdateSecretsHandler(lggr, mockORM, nil)
err := h.Handle(ctx, WorkflowRegistryEvent{
Output: logeventcap.Output{
Data: map[string]any{
"SecretsURL": []byte(hash),
"SecretsURL": assert.AnError,
},
},
})
require.Error(t, err)
require.ErrorIs(t, err, assert.AnError)
require.ErrorContains(t, err, assert.AnError.Error())
})

t.Run("fails to fetch contents", func(t *testing.T) {
mockORM := mocks.NewORM(t)
ctx := testutils.Context(t)
giveURL := "http://example.com"
hash := common.Keccak256Hash([]byte(giveURL))
mockORM.EXPECT().GetSecretsURL(matches.AnyContext, hash).Return("", assert.AnError)

fetcher := func(_ context.Context, _ string) ([]byte, error) {
return nil, assert.AnError
}
Expand All @@ -81,7 +73,7 @@ func Test_Handler(t *testing.T) {
err := h.Handle(ctx, WorkflowRegistryEvent{
Output: logeventcap.Output{
Data: map[string]any{
"SecretsURL": []byte(hash),
"SecretsURL": giveURL,
},
},
})
Expand All @@ -93,8 +85,7 @@ func Test_Handler(t *testing.T) {
mockORM := mocks.NewORM(t)
ctx := testutils.Context(t)
giveURL := "http://example.com"
hash := common.Keccak256Hash([]byte(giveURL))
mockORM.EXPECT().GetSecretsURL(matches.AnyContext, hash).Return(giveURL, nil)

fetcher := func(_ context.Context, _ string) ([]byte, error) {
return []byte("contents"), nil
}
Expand All @@ -103,7 +94,7 @@ func Test_Handler(t *testing.T) {
err := h.Handle(ctx, WorkflowRegistryEvent{
Output: logeventcap.Output{
Data: map[string]any{
"SecretsURL": []byte(hash),
"SecretsURL": giveURL,
},
},
})
Expand All @@ -114,16 +105,16 @@ func Test_Handler(t *testing.T) {

func Test_getURLHash(t *testing.T) {
giveURL := "http://example.com"
hash := common.Keccak256Hash([]byte(giveURL))

giveEvent := WorkflowRegistryEvent{
Output: logeventcap.Output{
Data: map[string]any{
"SecretsURL": []byte(hash),
"SecretsURL": giveURL,
},
},
}
gotHash, err := getURLHash(giveEvent)
gotURL, err := getSecretsURL(giveEvent)
require.NoError(t, err)

assert.Equal(t, hash, gotHash)
assert.Equal(t, giveURL, gotURL)
}
Loading

0 comments on commit e181a48

Please sign in to comment.