Skip to content

Commit

Permalink
Update TestSpawner_CreateJobDeleteJob (#13390)
Browse files Browse the repository at this point in the history
* Unregister filters for both Keepers 2.1 & 2.0 jobs

Also: fix log msg typo

* Replace ocr2vrf job with ocr2keeper job in TestSpawner_CreateJobDeleteJob test

* Append filter names instead of overwriting
  • Loading branch information
reductionista authored Jun 5, 2024
1 parent 9a7a348 commit 59c74da
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 59 deletions.
78 changes: 29 additions & 49 deletions core/services/job/helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package job_test

import (
"crypto/rand"
"encoding/hex"
"fmt"
"math/big"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/google/uuid"
"github.com/hashicorp/consul/sdk/freeport"
"github.com/lib/pq"
"github.com/pelletier/go-toml"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -49,31 +48,30 @@ observationSource = """
%s
"""
`
ocr2vrfJobSpecTemplate = `
type = "offchainreporting2"
schemaVersion = 1
name = "ocr2 vrf spec"
maxTaskDuration = "10s"
contractID = "%s"
ocrKeyBundleID = "%s"
relay = "evm"
pluginType = "ocr2vrf"
transmitterID = "%s"
forwardingAllowed = %t

ocr2Keeper21JobSpecTemplate = `
type = "offchainreporting2"
pluginType = "ocr2automation"
relay = "evm"
name = "ocr2keeper"
schemaVersion = 1
contractID = "%s"
contractConfigTrackerPollInterval = "15s"
ocrKeyBundleID = "%s"
transmitterID = "%s"
p2pv2Bootstrappers = [
"%s"
]
[relayConfig]
chainID = %d
fromBlock = %d
sendingKeys = [%s]
chainID = %d
[pluginConfig]
dkgEncryptionPublicKey = "%s"
dkgSigningPublicKey = "%s"
dkgKeyID = "%s"
dkgContractAddress = "%s"
vrfCoordinatorAddress = "%s"
linkEthFeedAddress = "%s"
maxServiceWorkers = 100
cacheEvictionInterval = "1s"
mercuryCredentialName = "%s"
contractVersion = "v2.1"
useBufferV1 = %v
`
voterTurnoutDataSourceTemplate = `
// data source 1
Expand Down Expand Up @@ -268,38 +266,20 @@ func makeOCRJobSpecFromToml(t *testing.T, jobSpecToml string) *job.Job {
return &jb
}

func makeOCR2VRFJobSpec(t testing.TB, ks keystore.Master, transmitter common.Address, chainID *big.Int, fromBlock uint64) *job.Job {
func makeOCR2Keeper21JobSpec(t testing.TB, ks keystore.Master, transmitter common.Address, chainID *big.Int) *job.Job {
t.Helper()
ctx := testutils.Context(t)

useForwarders := false
_, beacon := cltest.MustInsertRandomKey(t, ks.Eth())
_, coordinator := cltest.MustInsertRandomKey(t, ks.Eth())
_, feed := cltest.MustInsertRandomKey(t, ks.Eth())
_, dkg := cltest.MustInsertRandomKey(t, ks.Eth())
sendingKeys := fmt.Sprintf(`"%s"`, transmitter)
bootstrapNodePort := freeport.GetOne(t)
bootstrapPeerID := "peerId"

kb, _ := ks.OCR2().Create(ctx, chaintype.EVM)
_, registry := cltest.MustInsertRandomKey(t, ks.Eth())

vrfKey := make([]byte, 32)
_, err := rand.Read(vrfKey)
require.NoError(t, err)
ocr2Keeper21Job := fmt.Sprintf(ocr2Keeper21JobSpecTemplate, registry.String(), kb.ID(), transmitter,
fmt.Sprintf("%s127.0.0.1:%d", bootstrapPeerID, bootstrapNodePort), chainID, "mercury cred", false)

ocr2vrfJob := fmt.Sprintf(ocr2vrfJobSpecTemplate,
beacon.String(),
kb.ID(),
"",
useForwarders,
chainID,
fromBlock,
sendingKeys,
ks.DKGEncrypt(),
ks.DKGSign(),
hex.EncodeToString(vrfKey[:]),
dkg.String(),
coordinator.String(),
feed.String(),
)
jobSpec := makeOCR2JobSpecFromToml(t, ocr2vrfJob)
jobSpec := makeOCR2JobSpecFromToml(t, ocr2Keeper21Job)

return jobSpec
}
Expand Down
20 changes: 11 additions & 9 deletions core/services/job/spawner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/smartcontractkit/chainlink-common/pkg/types"
"github.com/smartcontractkit/chainlink-common/pkg/utils"
"github.com/smartcontractkit/chainlink-common/pkg/utils/mailbox/mailboxtest"

"github.com/smartcontractkit/chainlink/v2/core/capabilities"

"github.com/smartcontractkit/chainlink/v2/core/bridges"
Expand Down Expand Up @@ -291,7 +292,8 @@ func TestSpawner_CreateJobDeleteJob(t *testing.T) {
relayExtenders := evmtest.NewChainRelayExtenders(t, testopts)
assert.Equal(t, relayExtenders.Len(), 1)
legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders)
chain := evmtest.MustGetDefaultChain(t, legacyChains)
chain, err := legacyChains.Get("0")
require.NoError(t, err)

evmRelayer, err := evmrelayer.NewRelayer(lggr, chain, evmrelayer.RelayerOpts{
DS: db,
Expand All @@ -305,7 +307,7 @@ func TestSpawner_CreateJobDeleteJob(t *testing.T) {
r: evmRelayer,
}

jobOCR2VRF := makeOCR2VRFJobSpec(t, keyStore, address, chain.ID(), 2)
jobOCR2Keeper := makeOCR2Keeper21JobSpec(t, keyStore, address, chain.ID())

orm := NewTestORM(t, db, pipeline.NewORM(db, lggr, config.JobPipeline().MaxSuccessfulRuns()), bridges.NewORM(db), keyStore)
mailMon := servicetest.Run(t, mailboxtest.NewMonitor(t))
Expand All @@ -315,26 +317,26 @@ func TestSpawner_CreateJobDeleteJob(t *testing.T) {

d := ocr2.NewDelegate(nil, orm, nil, nil, nil, nil, nil, monitoringEndpoint, legacyChains, lggr, ocr2DelegateConfig,
keyStore.OCR2(), keyStore.DKGSign(), keyStore.DKGEncrypt(), ethKeyStore, testRelayGetter, mailMon, capabilities.NewRegistry(lggr))
delegateOCR2 := &delegate{jobOCR2VRF.Type, []job.ServiceCtx{}, 0, nil, d}
delegateOCR2 := &delegate{jobOCR2Keeper.Type, []job.ServiceCtx{}, 0, nil, d}

spawner := job.NewSpawner(orm, config.Database(), noopChecker{}, map[job.Type]job.Delegate{
jobOCR2VRF.Type: delegateOCR2,
jobOCR2Keeper.Type: delegateOCR2,
}, lggr, nil)

ctx := testutils.Context(t)
err = spawner.CreateJob(ctx, nil, jobOCR2VRF)
err = spawner.CreateJob(ctx, nil, jobOCR2Keeper)
require.NoError(t, err)
jobSpecID := jobOCR2VRF.ID
delegateOCR2.jobID = jobOCR2VRF.ID
jobSpecID := jobOCR2Keeper.ID
delegateOCR2.jobID = jobOCR2Keeper.ID

lp.On("UnregisterFilter", mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) {
lggr.Debugf("Got here, with args %v", args)
lggr.Debugf("UnregisterFilter called with args %v", args)
})

err = spawner.DeleteJob(ctx, nil, jobSpecID)
require.NoError(t, err)

lp.AssertNumberOfCalls(t, "UnregisterFilter", 3)
lp.AssertNumberOfCalls(t, "UnregisterFilter", 6)

lp.On("Close").Return(nil).Once()
spawner.Close()
Expand Down
9 changes: 8 additions & 1 deletion core/services/ocr2/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func (d *Delegate) cleanupEVM(ctx context.Context, jb job.Job, relayID types.Rel
spec := jb.OCR2OracleSpec
chain, err := d.legacyChains.Get(relayID.ChainID)
if err != nil {
d.lggr.Error("cleanupEVM: failed to chain get chain %s", "err", relayID.ChainID, err)
d.lggr.Errorw("cleanupEVM: failed to get chain id", "chainId", relayID.ChainID, "err", err)
return nil
}
lp := chain.LogPoller()
Expand All @@ -316,10 +316,17 @@ func (d *Delegate) cleanupEVM(ctx context.Context, jb job.Job, relayID types.Rel
d.lggr.Errorw("failed to derive ocr2vrf filter names from spec", "err", err, "spec", spec)
}
case types.OCR2Keeper:
// Not worth the effort to validate and parse the job spec config to figure out whether this is v2.0 or v2.1,
// simpler and faster to just Unregister them both
filters, err = ocr2keeper.FilterNamesFromSpec20(spec)
if err != nil {
d.lggr.Errorw("failed to derive ocr2keeper filter names from spec", "err", err, "spec", spec)
}
filters21, err2 := ocr2keeper.FilterNamesFromSpec21(spec)
if err2 != nil {
d.lggr.Errorw("failed to derive ocr2keeper filter names from spec", "err", err, "spec", spec)
}
filters = append(filters, filters21...)
default:
return nil
}
Expand Down

0 comments on commit 59c74da

Please sign in to comment.