Skip to content

Commit

Permalink
Fixing broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-sekara committed Sep 29, 2023
1 parent f057105 commit b6a7395
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/services/ocr2/plugins/ccip/testhelpers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/store/models"
)

const PermissionLessExecutionThresholdSeconds = 20 * 60
var PermissionLessExecutionThresholdSeconds = uint32(FirstBlockAge.Seconds())

func (c *CCIPContracts) CreateDefaultCommitOnchainConfig(t *testing.T) []byte {
config, err := abihelpers.EncodeAbiStruct(ccipconfig.CommitOnchainConfig{
Expand Down
13 changes: 13 additions & 0 deletions core/services/ocr2/plugins/ccip/testhelpers/simulated_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"math/big"
"testing"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
Expand All @@ -17,6 +18,9 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/services/keystore"
)

// FirstBlockAge is used to compute first block's timestamp in SimulatedBackend (time.Now() - FirstBlockAge)
const FirstBlockAge = 24 * time.Hour

func SetupChain(t *testing.T) (*backends.SimulatedBackend, *bind.TransactOpts) {
key, err := crypto.GenerateKey()
require.NoError(t, err)
Expand All @@ -25,6 +29,15 @@ func SetupChain(t *testing.T) (*backends.SimulatedBackend, *bind.TransactOpts) {
chain := backends.NewSimulatedBackend(core.GenesisAlloc{
user.From: {Balance: new(big.Int).Mul(big.NewInt(1000), big.NewInt(1e18))}},
ethconfig.Defaults.Miner.GasCeil)
// CCIP relies on block timestamps, but SimulatedBackend uses by default clock starting from 1970-01-01
// This trick is used to move the clock closer to the current time. We set first block to be X hours ago.
// Tests create plenty of transactions so this number can't be too low, every new block mined will tick the clock,
// if you mine more than "X hours" transactions, SimulatedBackend will panic because generated timestamps will be in the future.
// IMPORTANT: Any adjustments to FirstBlockAge will automatically update PermissionLessExecutionThresholdSeconds in tests
blockTime := time.UnixMilli(int64(chain.Blockchain().CurrentHeader().Time))
err = chain.AdjustTime(time.Since(blockTime) - FirstBlockAge)
require.NoError(t, err)
chain.Commit()
return chain, user
}

Expand Down

0 comments on commit b6a7395

Please sign in to comment.