Skip to content

Commit

Permalink
offchain - unit testing improvements (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkouv authored Sep 18, 2023
1 parent d75130d commit 5fac764
Show file tree
Hide file tree
Showing 27 changed files with 2,578 additions and 1,617 deletions.
28 changes: 28 additions & 0 deletions core/services/ocr2/plugins/ccip/abihelpers/abi_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import (
"fmt"
"math"
"math/big"
"math/rand"
"testing"

"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/commit_store"
"github.com/smartcontractkit/chainlink/v2/core/utils"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
"github.com/test-go/testify/require"
Expand Down Expand Up @@ -58,6 +62,30 @@ func TestProofFlagToBits(t *testing.T) {
}
}

func TestCommitReportEncoding(t *testing.T) {
report := commit_store.CommitStoreCommitReport{
PriceUpdates: commit_store.InternalPriceUpdates{
TokenPriceUpdates: []commit_store.InternalTokenPriceUpdate{
{
SourceToken: utils.RandomAddress(),
UsdPerToken: big.NewInt(9e18),
},
},
DestChainSelector: rand.Uint64(),
UsdPerUnitGas: big.NewInt(2000e9),
},
MerkleRoot: [32]byte{123},
Interval: commit_store.CommitStoreInterval{Min: 1, Max: 10},
}

encodedReport, err := EncodeCommitReport(report)
require.NoError(t, err)

decodedReport, err := DecodeCommitReport(encodedReport)
require.NoError(t, err)
require.Equal(t, report, decodedReport)
}

func TestExecutionReportEncoding(t *testing.T) {
// Note could consider some fancier testing here (fuzz/property)
// but I think that would essentially be testing geth's abi library
Expand Down
6 changes: 3 additions & 3 deletions core/services/ocr2/plugins/ccip/commit_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
libocr2 "github.com/smartcontractkit/libocr/offchainreporting2plus"

relaylogger "github.com/smartcontractkit/chainlink-relay/pkg/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipevents"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/hashlib"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/oraclelib"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/pricegetter"
Expand Down Expand Up @@ -106,8 +106,8 @@ func NewCommitServices(lggr logger.Logger, jb job.Job, chainSet evm.LegacyChainC
lggr: commitLggr,
sourceLP: sourceChain.LogPoller(),
destLP: destChain.LogPoller(),
sourceEvents: ccipevents.NewLogPollerClient(sourceChain.LogPoller(), commitLggr, sourceChain.Client()),
destEvents: ccipevents.NewLogPollerClient(destChain.LogPoller(), commitLggr, destChain.Client()),
sourceReader: ccipdata.NewLogPollerReader(sourceChain.LogPoller(), commitLggr, sourceChain.Client()),
destReader: ccipdata.NewLogPollerReader(destChain.LogPoller(), commitLggr, destChain.Client()),
offRamp: offRamp,
onRampAddress: onRamp.Address(),
priceGetter: priceGetterObject,
Expand Down
12 changes: 5 additions & 7 deletions core/services/ocr2/plugins/ccip/commit_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/commit_store"
mock_contracts "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/mocks"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/testhelpers"
)

func TestGetCommitPluginFilterNamesFromSpec(t *testing.T) {
Expand Down Expand Up @@ -87,13 +88,10 @@ func TestGetCommitPluginFilterNames(t *testing.T) {
onRampAddr := common.HexToAddress("0xdafea492d9c6733ae3d56b7ed1adb60692c98bc2")
priceRegAddr := common.HexToAddress("0xdafea492d9c6733ae3d56b7ed1adb60692c98bc3")
offRampAddr := common.HexToAddress("0xDAFeA492D9c6733Ae3D56b7eD1AdB60692C98BC4")
mockCommitStore := mock_contracts.NewCommitStoreInterface(t)
mockCommitStore.On("GetStaticConfig", mock.Anything).Return(commit_store.CommitStoreStaticConfig{
OnRamp: onRampAddr,
}, nil)
mockCommitStore.On("GetDynamicConfig", mock.Anything).Return(commit_store.CommitStoreDynamicConfig{
PriceRegistry: priceRegAddr,
}, nil)

mockCommitStore, _ := testhelpers.NewFakeCommitStore(t, 1)
mockCommitStore.SetStaticConfig(commit_store.CommitStoreStaticConfig{OnRamp: onRampAddr})
mockCommitStore.SetDynamicConfig(commit_store.CommitStoreDynamicConfig{PriceRegistry: priceRegAddr})

srcLP := mocklp.NewLogPoller(t)
dstLP := mocklp.NewLogPoller(t)
Expand Down
14 changes: 7 additions & 7 deletions core/services/ocr2/plugins/ccip/commit_reporting_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/abihelpers"
ccipconfig "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/config"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/pricegetter"

"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/cache"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipevents"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/hashlib"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/merklemulti"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
Expand Down Expand Up @@ -56,8 +56,8 @@ type update struct {
type CommitPluginConfig struct {
lggr logger.Logger
sourceLP, destLP logpoller.LogPoller
sourceEvents ccipevents.Client
destEvents ccipevents.Client
sourceReader ccipdata.Reader
destReader ccipdata.Reader
offRamp evm_2_evm_offramp.EVM2EVMOffRampInterface
onRampAddress common.Address
commitStore commit_store.CommitStoreInterface
Expand Down Expand Up @@ -238,7 +238,7 @@ func (r *CommitReportingPlugin) calculateMinMaxSequenceNumbers(ctx context.Conte
return 0, 0, err
}

msgRequests, err := r.config.sourceEvents.GetSendRequestsGteSeqNum(ctx, r.config.onRampAddress, nextInflightMin, r.config.checkFinalityTags, int(r.offchainConfig.SourceFinalityDepth))
msgRequests, err := r.config.sourceReader.GetSendRequestsGteSeqNum(ctx, r.config.onRampAddress, nextInflightMin, r.config.checkFinalityTags, int(r.offchainConfig.SourceFinalityDepth))
if err != nil {
return 0, 0, err
}
Expand Down Expand Up @@ -367,7 +367,7 @@ func calculateUsdPer1e18TokenAmount(price *big.Int, decimals uint8) *big.Int {
// Gets the latest token price updates based on logs within the heartbeat
// The updates returned by this function are guaranteed to not contain nil values.
func (r *CommitReportingPlugin) getLatestTokenPriceUpdates(ctx context.Context, now time.Time, checkInflight bool) (map[common.Address]update, error) {
tokenPriceUpdates, err := r.config.destEvents.GetTokenPriceUpdatesCreatedAfter(
tokenPriceUpdates, err := r.config.destReader.GetTokenPriceUpdatesCreatedAfter(
ctx,
r.destPriceRegistry.Address(),
now.Add(-r.offchainConfig.FeeUpdateHeartBeat.Duration()),
Expand Down Expand Up @@ -423,7 +423,7 @@ func (r *CommitReportingPlugin) getLatestGasPriceUpdate(ctx context.Context, now
}

// If there are no price updates inflight, check latest prices onchain
gasPriceUpdates, err := r.config.destEvents.GetGasPriceUpdatesCreatedAfter(
gasPriceUpdates, err := r.config.destReader.GetGasPriceUpdatesCreatedAfter(
ctx,
r.destPriceRegistry.Address(),
r.config.sourceChainSelector,
Expand Down Expand Up @@ -657,7 +657,7 @@ func (r *CommitReportingPlugin) buildReport(ctx context.Context, lggr logger.Log

// Logs are guaranteed to be in order of seq num, since these are finalized logs only
// and the contract's seq num is auto-incrementing.
sendRequests, err := r.config.sourceEvents.GetSendRequestsBetweenSeqNums(
sendRequests, err := r.config.sourceReader.GetSendRequestsBetweenSeqNums(
ctx,
r.config.onRampAddress,
interval.Min,
Expand Down
Loading

0 comments on commit 5fac764

Please sign in to comment.