Skip to content

Commit

Permalink
Move common code to test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
asoliman92 committed Jun 25, 2024
1 parent 739bc7d commit caab3dc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,15 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/smartcontractkit/chainlink-common/pkg/codec"
types2 "github.com/smartcontractkit/chainlink-common/pkg/types"
query2 "github.com/smartcontractkit/chainlink-common/pkg/types/query"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest"
logger2 "github.com/smartcontractkit/chainlink/v2/core/logger"
helpers "github.com/smartcontractkit/chainlink/v2/core/services/ocr3/plugins/ccip_integration_tests"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/types"
"github.com/stretchr/testify/assert"
)

const chainID = 1337

func TestChainReader(t *testing.T) {
ctx := testutils.Context(t)
lggr := logger2.NullLogger
d := helpers.SetupTest[Chainreader](t, ctx, DeployChainreader, NewChainreader)
db := pgtest.NewSqlxDB(t)
lpOpts := logpoller.Opts{
PollPeriod: time.Millisecond,
FinalityDepth: 0,
BackfillBatchSize: 10,
RpcBatchSize: 10,
KeepFinalizedBlocksDepth: 100000,
}
cl := client.NewSimulatedBackendClient(t, d.SimulatedBE, big.NewInt(chainID))
lp := logpoller.NewLogPoller(logpoller.NewORM(big.NewInt(chainID), db, lggr), cl, lggr, lpOpts)
assert.NoError(t, lp.Start(ctx))

const (
ContractNameAlias = "myCoolContract"

Expand Down Expand Up @@ -90,24 +68,8 @@ func TestChainReader(t *testing.T) {
},
}

cr, err := evm.NewChainReaderService(ctx, lggr, lp, cl, cfg)
assert.NoError(t, err)
err = cr.Bind(ctx, []types2.BoundContract{
{
Address: d.ContractAddr.String(),
Name: ContractNameAlias,
Pending: false,
},
})
assert.NoError(t, err)

err = cr.Start(ctx)
assert.NoError(t, err)
for {
if err := cr.Ready(); err == nil {
break
}
}
d := helpers.SetupChainReaderTest[Chainreader](t, ctx, DeployChainreader, NewChainreader, cfg)
cr := *d.ChainReader

emitEvents(t, d, ctx) // Calls the contract to emit events

Expand All @@ -119,14 +81,14 @@ func TestChainReader(t *testing.T) {

t.Run("simple contract read", func(t *testing.T) {
var cnt big.Int
err = cr.GetLatestValue(ctx, ContractNameAlias, FnAliasGetCount, map[string]interface{}{}, &cnt)
err := cr.GetLatestValue(ctx, ContractNameAlias, FnAliasGetCount, map[string]interface{}{}, &cnt)
assert.NoError(t, err)
assert.Equal(t, int64(10), cnt.Int64())
})

t.Run("read array", func(t *testing.T) {
var nums []big.Int
err = cr.GetLatestValue(ctx, ContractNameAlias, FnAliasGetNumbers, map[string]interface{}{}, &nums)
err := cr.GetLatestValue(ctx, ContractNameAlias, FnAliasGetNumbers, map[string]interface{}{}, &nums)
assert.NoError(t, err)
assert.Len(t, nums, 10)
for i := 1; i <= 10; i++ {
Expand All @@ -139,7 +101,8 @@ func TestChainReader(t *testing.T) {
NameField string
Age *big.Int // WARN: specifying a wrong data type e.g. int instead of *big.Int fails silently with a default value of 0
}{}
err = cr.GetLatestValue(ctx, ContractNameAlias, FnAliasGetPerson, map[string]interface{}{}, &person)
err := cr.GetLatestValue(ctx, ContractNameAlias, FnAliasGetPerson, map[string]interface{}{}, &person)
assert.NoError(t, err)
assert.Equal(t, "Dim", person.NameField)
assert.Equal(t, int64(18), person.Age.Int64())
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,16 @@ package home_chain
import (
"context"
_ "embed"
"math/big"
"testing"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"

types2 "github.com/smartcontractkit/chainlink-common/pkg/types"
"github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
capcfg "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/ccip_capability_configuration"
helpers "github.com/smartcontractkit/chainlink/v2/core/services/ocr3/plugins/ccip_integration_tests"

"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest"
logger2 "github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/types"
"github.com/stretchr/testify/assert"
)
Expand All @@ -36,7 +28,6 @@ func TestHomeChainReader(t *testing.T) {
deployFunc := func(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *capcfg.CCIPCapabilityConfiguration, error) {
return capcfg.DeployCCIPCapabilityConfiguration(auth, backend, common.Address{})
}
d := helpers.SetupTest[capcfg.CCIPCapabilityConfiguration](t, context.Background(), deployFunc, capcfg.NewCCIPCapabilityConfiguration)
// Initialize chainReader
cfg := evmtypes.ChainReaderConfig{
Contracts: map[string]evmtypes.ChainContractReader{
Expand All @@ -56,49 +47,18 @@ func TestHomeChainReader(t *testing.T) {
},
}

lggr := logger2.NullLogger
db := pgtest.NewSqlxDB(t)
lpOpts := logpoller.Opts{
PollPeriod: time.Millisecond,
FinalityDepth: 1,
BackfillBatchSize: 1,
RpcBatchSize: 1,
KeepFinalizedBlocksDepth: 10000,
}
cl := client.NewSimulatedBackendClient(t, d.SimulatedBE, big.NewInt(chainID))
lp := logpoller.NewLogPoller(logpoller.NewORM(big.NewInt(chainID), db, lggr), cl, lggr, lpOpts)
assert.NoError(t, lp.Start(context.Background()))

cr, err := evm.NewChainReaderService(context.Background(), lggr, lp, cl, cfg)
assert.NoError(t, err)
err = cr.Bind(context.Background(), []types2.BoundContract{
{
Address: d.ContractAddr.String(),
Name: "CCIPCapabilityConfiguration",
Pending: false,
},
})
assert.NoError(t, err)

err = cr.Start(context.Background())
assert.NoError(t, err)
for {
if err := cr.Ready(); err == nil {
break
}
}

d.SimulatedBE.Commit()
d := helpers.SetupChainReaderTest[capcfg.CCIPCapabilityConfiguration](t, context.Background(), deployFunc, capcfg.NewCCIPCapabilityConfiguration, cfg)
chainReader := *d.ChainReader

// Apply chain configs to the contract
inputConfig := setupConfigInfo()
_, err = d.Contract.ApplyChainConfigUpdates(d.Auth, nil, inputConfig)
_, err := d.Contract.ApplyChainConfigUpdates(d.Auth, nil, inputConfig)
d.SimulatedBE.Commit()
assert.NoError(t, err)

// Now read the contract using chain reader
var allConfigs []capcfg.CCIPCapabilityConfigurationChainConfigInfo
err = cr.GetLatestValue(
err = chainReader.GetLatestValue(
context.Background(),
"CCIPCapabilityConfiguration",
"getAllChainConfigs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ import (
"context"
"math/big"
"testing"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
types2 "github.com/smartcontractkit/chainlink-common/pkg/types"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest"
logger2 "github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/types"
"github.com/stretchr/testify/assert"
)

Expand All @@ -21,6 +29,8 @@ type TestSetupData[T any] struct {
Contract *T
SimulatedBE *backends.SimulatedBackend
Auth *bind.TransactOpts
ChainReader *evm.ChainReaderService
ChainID int
}

type DeployFunc[T any] func(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *T, error)
Expand All @@ -29,7 +39,7 @@ type DeployFuncWithCapabilities[T any] func(auth *bind.TransactOpts, backend bin

type NewFunc[T any] func(address common.Address, backend bind.ContractBackend) (*T, error)

func SetupTest[T any](t *testing.T, _ context.Context, deployFunc DeployFunc[T], newFunc NewFunc[T]) *TestSetupData[T] {
func SetupChainReaderTest[T any](t *testing.T, _ context.Context, deployFunc DeployFunc[T], newFunc NewFunc[T], chainReaderConfig evmtypes.ChainReaderConfig) *TestSetupData[T] {
// Generate a new key pair for the simulated account
privateKey, err := crypto.GenerateKey()
assert.NoError(t, err)
Expand All @@ -54,10 +64,46 @@ func SetupTest[T any](t *testing.T, _ context.Context, deployFunc DeployFunc[T],
contract, err := newFunc(address, simulatedBackend)
assert.NoError(t, err)

lggr := logger2.NullLogger
db := pgtest.NewSqlxDB(t)
lpOpts := logpoller.Opts{
PollPeriod: time.Millisecond,
FinalityDepth: 1,
BackfillBatchSize: 1,
RpcBatchSize: 1,
KeepFinalizedBlocksDepth: 10000,
}
cl := client.NewSimulatedBackendClient(t, simulatedBackend, big.NewInt(chainID))
lp := logpoller.NewLogPoller(logpoller.NewORM(big.NewInt(chainID), db, lggr), cl, lggr, lpOpts)
assert.NoError(t, lp.Start(context.Background()))

cr, err := evm.NewChainReaderService(context.Background(), lggr, lp, cl, chainReaderConfig)
assert.NoError(t, err)
err = cr.Bind(context.Background(), []types2.BoundContract{
{
Address: address.String(),
Name: "CCIPCapabilityConfiguration",
Pending: false,
},
})
assert.NoError(t, err)

err = cr.Start(context.Background())
assert.NoError(t, err)
for {
if err := cr.Ready(); err == nil {
break
}
}

simulatedBackend.Commit()

return &TestSetupData[T]{
ContractAddr: address,
Contract: contract,
SimulatedBE: simulatedBackend,
Auth: auth,
ChainReader: &cr,
ChainID: chainID,
}
}

0 comments on commit caab3dc

Please sign in to comment.