Skip to content

Commit

Permalink
Fix chainreader & config poller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reductionista committed May 15, 2024
1 parent 5bd0199 commit 81aa9b7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 32 deletions.
2 changes: 1 addition & 1 deletion core/chains/evm/client/simulated_backend_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ func (c *SimulatedBackendClient) EstimateGas(ctx context.Context, call ethereum.

// SuggestGasPrice recommends a gas price.
func (c *SimulatedBackendClient) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
panic("unimplemented")
return c.client.SuggestGasPrice(ctx)
}

// BatchCallContext makes a batch rpc call.
Expand Down
2 changes: 0 additions & 2 deletions core/chains/evm/logpoller/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ func SetupTH(t testing.TB, opts logpoller.Opts) TestHarness {
chainID := testutils.NewRandomEVMChainID()
chainID2 := testutils.NewRandomEVMChainID()
db := pgtest.NewSqlxDB(t)
//dataDir, err := os.MkdirTemp("", "simgethdata")
//require.NoError(t, err)

o := logpoller.NewORM(chainID, db, lggr)
o2 := logpoller.NewORM(chainID2, db, lggr)
Expand Down
7 changes: 4 additions & 3 deletions core/chains/evm/logpoller/log_poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func Test_BackupLogPoller(t *testing.T) {
require.ErrorIs(t, err, ethereum.NotFound)

currentBlockNumber := th.PollAndSaveLogs(ctx, 1)
assert.Equal(t, int64(35), currentBlockNumber)
assert.Equal(t, int64(1), currentBlockNumber)

// flush out cached block 34 by reading logs from first 32 blocks
//query := ethereum.FilterQuery{
Expand Down Expand Up @@ -380,7 +380,7 @@ func Test_BackupLogPoller(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, 0, len(logs))
th.Backend.Commit()
//markBlockAsFinalized(t, th, 35)
finalizeThroughBlock(t, th, 35)

// Run ordinary poller + backup poller at least once more
th.LogPoller.PollAndSaveLogs(ctx, currentBlockNumber+1)
Expand Down Expand Up @@ -1760,7 +1760,7 @@ func Test_PollAndSavePersistsFinalityInBlocks(t *testing.T) {
}

if tt.useFinalityTag {
finalizeThroughBlock(t, th, int64(tt.expectedFinalizedBlock))
finalizeThroughBlock(t, th, tt.expectedFinalizedBlock)
}

th.PollAndSaveLogs(ctx, 1)
Expand Down Expand Up @@ -1944,6 +1944,7 @@ func finalizeThroughBlock(t *testing.T, th TestHarness, blockNumber int64) {
require.Len(t, currentBlock, 32)
}
h, err = th.Client.HeaderByNumber(ctx, nil)
require.NoError(t, err)
require.GreaterOrEqual(t, h.Number.Int64(), targetBlockNumber)
}

Expand Down
25 changes: 12 additions & 13 deletions core/services/relay/evm/chain_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ import (

"github.com/ethereum/go-ethereum/accounts/abi"
"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/common/hexutil"
"github.com/ethereum/go-ethereum/core"
evmtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient/simulated"
"github.com/smartcontractkit/libocr/commontypes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -63,7 +62,7 @@ func TestChainReaderGetLatestValue(t *testing.T) {
anyString := "foo"
tx, err := it.evmTest.TriggerEventWithDynamicTopic(it.auth, anyString)
require.NoError(t, err)
it.sim.Commit()
it.backend.Commit()
it.incNonce()
it.awaitTx(t, tx)

Expand Down Expand Up @@ -113,7 +112,7 @@ func triggerFourTopics(t *testing.T, it *chainReaderInterfaceTester, i1, i2, i3
tx, err := it.evmTest.ChainReaderTesterTransactor.TriggerWithFourTopics(it.auth, i1, i2, i3)
require.NoError(t, err)
require.NoError(t, err)
it.sim.Commit()
it.backend.Commit()
it.incNonce()
it.awaitTx(t, tx)
}
Expand All @@ -124,7 +123,7 @@ type chainReaderInterfaceTester struct {
address2 string
chainConfig types.ChainReaderConfig
auth *bind.TransactOpts
sim *backends.SimulatedBackend
backend *simulated.Backend
pk *ecdsa.PrivateKey
evmTest *chain_reader_tester.ChainReaderTester
cr evm.ChainReaderService
Expand Down Expand Up @@ -238,7 +237,7 @@ func (it *chainReaderInterfaceTester) Setup(t *testing.T) {
},
},
}
it.client = client.NewSimulatedBackendClient(t, it.sim, big.NewInt(1337))
it.client = client.NewSimulatedBackendClient(t, it.backend, big.NewInt(1337))
it.deployNewContracts(t)
}

Expand Down Expand Up @@ -308,7 +307,7 @@ func (it *chainReaderInterfaceTester) sendTxWithTestStruct(t *testing.T, testStr
midToInternalType(testStruct.NestedStruct),
)
require.NoError(t, err)
it.sim.Commit()
it.backend.Commit()
it.incNonce()
it.awaitTx(t, tx)
}
Expand Down Expand Up @@ -337,8 +336,8 @@ func (it *chainReaderInterfaceTester) setupChainNoClient(t require.TestingT) {
it.auth, err = bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(1337))
require.NoError(t, err)

it.sim = backends.NewSimulatedBackend(core.GenesisAlloc{it.auth.From: {Balance: big.NewInt(math.MaxInt64)}}, commonGasLimitOnEvms*5000)
it.sim.Commit()
it.backend = simulated.NewBackend(evmtypes.GenesisAlloc{it.auth.From: {Balance: big.NewInt(math.MaxInt64)}}, simulated.WithBlockGasLimit(commonGasLimitOnEvms*5000))
it.backend.Commit()
}

func (it *chainReaderInterfaceTester) deployNewContracts(t *testing.T) {
Expand All @@ -348,18 +347,18 @@ func (it *chainReaderInterfaceTester) deployNewContracts(t *testing.T) {

func (it *chainReaderInterfaceTester) deployNewContract(t *testing.T) string {
ctx := testutils.Context(t)
gasPrice, err := it.sim.SuggestGasPrice(ctx)
gasPrice, err := it.client.SuggestGasPrice(ctx)
require.NoError(t, err)
it.auth.GasPrice = gasPrice

// 105528 was in the error: gas too low: have 0, want 105528
// Not sure if there's a better way to get it.
it.auth.GasLimit = 10552800

address, tx, ts, err := chain_reader_tester.DeployChainReaderTester(it.auth, it.sim)
address, tx, ts, err := chain_reader_tester.DeployChainReaderTester(it.auth, it.client)

require.NoError(t, err)
it.sim.Commit()
it.backend.Commit()
if it.evmTest == nil {
it.evmTest = ts
}
Expand All @@ -370,7 +369,7 @@ func (it *chainReaderInterfaceTester) deployNewContract(t *testing.T) string {

func (it *chainReaderInterfaceTester) awaitTx(t *testing.T, tx *evmtypes.Transaction) {
ctx := testutils.Context(t)
receipt, err := it.sim.TransactionReceipt(ctx, tx.Hash())
receipt, err := it.client.TransactionReceipt(ctx, tx.Hash())
require.NoError(t, err)
require.Equal(t, evmtypes.ReceiptStatusSuccessful, receipt.Status)
}
Expand Down
29 changes: 16 additions & 13 deletions core/services/relay/evm/config_poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

"github.com/ethereum/go-ethereum"
"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"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/ethclient/simulated"
"github.com/onsi/gomega"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -52,7 +52,8 @@ func TestConfigPoller(t *testing.T) {
var configStoreContractAddr common.Address
var configStoreContract *ocrconfigurationstoreevmsimple.OCRConfigurationStoreEVMSimple
var user *bind.TransactOpts
var b *backends.SimulatedBackend
var b *simulated.Backend
var ec simulated.Client
var linkTokenAddress common.Address
var accessAddress common.Address
ctx := testutils.Context(t)
Expand All @@ -64,16 +65,18 @@ func TestConfigPoller(t *testing.T) {
require.NoError(t, err)
user, err = bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
require.NoError(t, err)
b = backends.NewSimulatedBackend(core.GenesisAlloc{
b = simulated.NewBackend(types.GenesisAlloc{
user.From: {Balance: big.NewInt(1000000000000000000)}},
5*ethconfig.Defaults.Miner.GasCeil)
linkTokenAddress, _, _, err = link_token_interface.DeployLinkToken(user, b)
simulated.WithBlockGasLimit(5*ethconfig.Defaults.Miner.GasCeil))
require.NotNil(t, b)
ec = b.Client()
linkTokenAddress, _, _, err = link_token_interface.DeployLinkToken(user, ec)
require.NoError(t, err)
accessAddress, _, _, err = testoffchainaggregator2.DeploySimpleWriteAccessController(user, b)
accessAddress, _, _, err = testoffchainaggregator2.DeploySimpleWriteAccessController(user, ec)
require.NoError(t, err, "failed to deploy test access controller contract")
ocrAddress, _, ocrContract, err = ocr2aggregator.DeployOCR2Aggregator(
user,
b,
ec,
linkTokenAddress,
big.NewInt(0),
big.NewInt(10),
Expand All @@ -83,7 +86,7 @@ func TestConfigPoller(t *testing.T) {
"TEST",
)
require.NoError(t, err)
configStoreContractAddr, _, configStoreContract, err = ocrconfigurationstoreevmsimple.DeployOCRConfigurationStoreEVMSimple(user, b)
configStoreContractAddr, _, configStoreContract, err = ocrconfigurationstoreevmsimple.DeployOCRConfigurationStoreEVMSimple(user, ec)
require.NoError(t, err)
b.Commit()

Expand Down Expand Up @@ -133,7 +136,7 @@ func TestConfigPoller(t *testing.T) {
DeltaC: 10,
}, ocrContract, user)
b.Commit()
latest, err := b.BlockByNumber(testutils.Context(t), nil)
latest, err := ec.BlockByNumber(testutils.Context(t), nil)
require.NoError(t, err)
// Ensure we capture this config set log.
require.NoError(t, lp.Replay(testutils.Context(t), latest.Number().Int64()-1))
Expand Down Expand Up @@ -164,7 +167,7 @@ func TestConfigPoller(t *testing.T) {
var err error
ocrAddress, _, ocrContract, err = ocr2aggregator.DeployOCR2Aggregator(
user,
b,
ec,
linkTokenAddress,
big.NewInt(0),
big.NewInt(10),
Expand Down Expand Up @@ -207,7 +210,7 @@ func TestConfigPoller(t *testing.T) {
changedInBlock, configDigest, err := cp.LatestConfigDetails(testutils.Context(t))
require.NoError(t, err)

latest, err := b.BlockByNumber(testutils.Context(t), nil)
latest, err := ec.BlockByNumber(testutils.Context(t), nil)
require.NoError(t, err)

onchainDetails, err := ocrContract.LatestConfigDetails(nil)
Expand Down Expand Up @@ -239,7 +242,7 @@ func TestConfigPoller(t *testing.T) {
// deploy it again to reset to empty config
ocrAddress, _, ocrContract, err = ocr2aggregator.DeployOCR2Aggregator(
user,
b,
ec,
linkTokenAddress,
big.NewInt(0),
big.NewInt(10),
Expand Down

0 comments on commit 81aa9b7

Please sign in to comment.