Skip to content

Commit

Permalink
fix some tests with real timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 committed Jul 9, 2024
1 parent 30005e4 commit 96fd9f3
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 40 deletions.
14 changes: 7 additions & 7 deletions core/chains/evm/gas/block_history_estimator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) {
elems[1].Result = &b42
})

head := evmtypes.NewHead(big.NewInt(44), b44.Hash, b43.Hash, uint64(time.Now().Unix()), ubig.New(testutils.FixtureChainID))
head := evmtypes.NewHead(big.NewInt(44), b44.Hash, b43.Hash, ubig.New(testutils.FixtureChainID))
err = bhe.FetchBlocks(tests.Context(t), &head)
require.NoError(t, err)

Expand Down Expand Up @@ -513,8 +513,8 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) {
elems[1].Result = &b2
})

head2 := evmtypes.NewHead(big.NewInt(2), b2.Hash, b1.Hash, uint64(time.Now().Unix()), ubig.New(testutils.FixtureChainID))
head3 := evmtypes.NewHead(big.NewInt(3), b3.Hash, b2.Hash, uint64(time.Now().Unix()), ubig.New(testutils.FixtureChainID))
head2 := evmtypes.NewHead(big.NewInt(2), b2.Hash, b1.Hash, ubig.New(testutils.FixtureChainID))
head3 := evmtypes.NewHead(big.NewInt(3), b3.Hash, b2.Hash, ubig.New(testutils.FixtureChainID))
head3.Parent = &head2
err := bhe.FetchBlocks(tests.Context(t), &head3)
require.NoError(t, err)
Expand Down Expand Up @@ -568,8 +568,8 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) {
gas.SetRollingBlockHistory(bhe, blocks)

// RE-ORG, head2 and head3 have different hash than saved b2 and b3
head2 := evmtypes.NewHead(big.NewInt(2), utils.NewHash(), b1.Hash, uint64(time.Now().Unix()), ubig.New(testutils.FixtureChainID))
head3 := evmtypes.NewHead(big.NewInt(3), utils.NewHash(), head2.Hash, uint64(time.Now().Unix()), ubig.New(testutils.FixtureChainID))
head2 := evmtypes.NewHead(big.NewInt(2), utils.NewHash(), b1.Hash, ubig.New(testutils.FixtureChainID))
head3 := evmtypes.NewHead(big.NewInt(3), utils.NewHash(), head2.Hash, ubig.New(testutils.FixtureChainID))
head3.Parent = &head2

ethClient.On("BatchCallContext", mock.Anything, mock.MatchedBy(func(b []rpc.BatchElem) bool {
Expand Down Expand Up @@ -641,8 +641,8 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) {
gas.SetRollingBlockHistory(bhe, blocks)

// head2 and head3 have identical hash to saved blocks
head2 := evmtypes.NewHead(big.NewInt(2), b2.Hash, b1.Hash, uint64(time.Now().Unix()), ubig.New(testutils.FixtureChainID))
head3 := evmtypes.NewHead(big.NewInt(3), b3.Hash, head2.Hash, uint64(time.Now().Unix()), ubig.New(testutils.FixtureChainID))
head2 := evmtypes.NewHead(big.NewInt(2), b2.Hash, b1.Hash, ubig.New(testutils.FixtureChainID))
head3 := evmtypes.NewHead(big.NewInt(3), b3.Hash, head2.Hash, ubig.New(testutils.FixtureChainID))
head3.Parent = &head2

err := bhe.FetchBlocks(tests.Context(t), &head3)
Expand Down
2 changes: 1 addition & 1 deletion core/chains/evm/headtracker/head_saver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestHeadSaver_Load(t *testing.T) {
// H2Uncle
//
newHead := func(num int, parent common.Hash) *evmtypes.Head {
h := evmtypes.NewHead(big.NewInt(int64(num)), utils.NewHash(), parent, uint64(time.Now().Unix()), ubig.NewI(0))
h := evmtypes.NewHead(big.NewInt(int64(num)), utils.NewHash(), parent, ubig.NewI(0))
return &h
}
h0 := newHead(0, utils.NewHash())
Expand Down
20 changes: 12 additions & 8 deletions core/chains/evm/headtracker/head_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ func TestHeadTracker_SwitchesToLongestChainWithHeadSamplingEnabled(t *testing.T)
c := ht.headSaver.Chain(h.Hash)
require.NotNil(t, c)
assert.Equal(t, c.ParentHash, h.ParentHash)
assert.Equal(t, c.Timestamp.Unix(), h.Timestamp.UTC().Unix())
assert.Equal(t, c.Timestamp.Unix(), h.Timestamp.Unix())
assert.Equal(t, c.Number, h.Number)
}
}
Expand Down Expand Up @@ -791,7 +791,7 @@ func TestHeadTracker_SwitchesToLongestChainWithHeadSamplingDisabled(t *testing.T
c := ht.headSaver.Chain(h.Hash)
require.NotNil(t, c)
assert.Equal(t, c.ParentHash, h.ParentHash)
assert.Equal(t, c.Timestamp.Unix(), h.Timestamp.UTC().Unix())
assert.Equal(t, c.Timestamp.Unix(), h.Timestamp.Unix())
assert.Equal(t, c.Number, h.Number)
}
}
Expand All @@ -816,7 +816,8 @@ func TestHeadTracker_Backfill(t *testing.T) {
ParentHash: common.BigToHash(big.NewInt(0)),
Time: now,
}
head0 := evmtypes.NewHead(gethHead0.Number, utils.NewHash(), gethHead0.ParentHash, gethHead0.Time, ubig.New(testutils.FixtureChainID))
head0 := evmtypes.NewHead(gethHead0.Number, utils.NewHash(), gethHead0.ParentHash, ubig.New(testutils.FixtureChainID))
head0.Timestamp = time.Unix(int64(gethHead0.Time), 0)

h1 := *testutils.Head(1)
h1.ParentHash = head0.Hash
Expand All @@ -826,7 +827,8 @@ func TestHeadTracker_Backfill(t *testing.T) {
ParentHash: utils.NewHash(),
Time: now,
}
head8 := evmtypes.NewHead(gethHead8.Number, utils.NewHash(), gethHead8.ParentHash, gethHead8.Time, ubig.New(testutils.FixtureChainID))
head8 := evmtypes.NewHead(gethHead8.Number, utils.NewHash(), gethHead8.ParentHash, ubig.New(testutils.FixtureChainID))
head8.Timestamp = time.Unix(int64(gethHead8.Time), 0)

h9 := *testutils.Head(9)
h9.ParentHash = head8.Hash
Expand All @@ -836,7 +838,8 @@ func TestHeadTracker_Backfill(t *testing.T) {
ParentHash: h9.Hash,
Time: now,
}
head10 := evmtypes.NewHead(gethHead10.Number, utils.NewHash(), gethHead10.ParentHash, gethHead10.Time, ubig.New(testutils.FixtureChainID))
head10 := evmtypes.NewHead(gethHead10.Number, utils.NewHash(), gethHead10.ParentHash, ubig.New(testutils.FixtureChainID))
head10.Timestamp = time.Unix(int64(gethHead10.Time), 0)

h11 := *testutils.Head(11)
h11.ParentHash = head10.Hash
Expand Down Expand Up @@ -1418,7 +1421,7 @@ func (hb *headBuffer) Append(head *evmtypes.Head) {
Hash: head.Hash,
ParentHash: head.ParentHash,
Parent: head.Parent,
Timestamp: time.Unix(int64(len(hb.Heads)), 0),
Timestamp: head.Timestamp,
EVMChainID: head.EVMChainID,
}
hb.Heads = append(hb.Heads, cloned)
Expand All @@ -1438,11 +1441,12 @@ func (b *blocks) Head(number uint64) *evmtypes.Head {
func NewBlocks(t *testing.T, numHashes int) *blocks {
hashes := make([]common.Hash, 0)
heads := make(map[int64]*evmtypes.Head)
now := time.Now()
for i := int64(0); i < int64(numHashes); i++ {
hash := testutils.NewHash()
hashes = append(hashes, hash)

heads[i] = &evmtypes.Head{Hash: hash, Number: i, Timestamp: time.Unix(i, 0), EVMChainID: ubig.New(testutils.FixtureChainID)}
heads[i] = &evmtypes.Head{Hash: hash, Number: i, Timestamp: now.Add(time.Duration(i) * time.Second), EVMChainID: ubig.New(testutils.FixtureChainID)}
if i > 0 {
parent := heads[i-1]
heads[i].Parent = parent
Expand Down Expand Up @@ -1489,7 +1493,7 @@ func (b *blocks) NewHead(number uint64) *evmtypes.Head {
Hash: testutils.NewHash(),
ParentHash: parent.Hash,
Parent: parent,
Timestamp: time.Unix(parent.Number+1, 0),
Timestamp: parent.Timestamp.Add(1),
EVMChainID: ubig.New(testutils.FixtureChainID),
}
return head
Expand Down
7 changes: 3 additions & 4 deletions core/chains/evm/headtracker/heads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package headtracker_test
import (
"math/big"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -79,11 +78,11 @@ func TestHeads_AddHeads(t *testing.T) {
var parentHash common.Hash
for i := 0; i < 5; i++ {
hash := utils.NewHash()
h := evmtypes.NewHead(big.NewInt(int64(i)), hash, parentHash, uint64(time.Now().Unix()), ubig.NewI(0))
h := evmtypes.NewHead(big.NewInt(int64(i)), hash, parentHash, ubig.NewI(0))
testHeads = append(testHeads, &h)
if i == 2 {
// uncled block
h := evmtypes.NewHead(big.NewInt(int64(i)), uncleHash, parentHash, uint64(time.Now().Unix()), ubig.NewI(0))
h := evmtypes.NewHead(big.NewInt(int64(i)), uncleHash, parentHash, ubig.NewI(0))
testHeads = append(testHeads, &h)
}
parentHash = hash
Expand Down Expand Up @@ -115,7 +114,7 @@ func TestHeads_MarkFinalized(t *testing.T) {
// H1Uncle H2Uncle
//
newHead := func(num int, parent common.Hash) *evmtypes.Head {
h := evmtypes.NewHead(big.NewInt(int64(num)), utils.NewHash(), parent, uint64(time.Now().Unix()), ubig.NewI(0))
h := evmtypes.NewHead(big.NewInt(int64(num)), utils.NewHash(), parent, ubig.NewI(0))
return &h
}
h0 := newHead(0, utils.NewHash())
Expand Down
2 changes: 1 addition & 1 deletion core/chains/evm/logpoller/log_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ func (lp *logPoller) fillRemainingBlocksFromRPC(
BlockHash: head.Hash,
BlockNumber: head.Number,
BlockTimestamp: head.Timestamp,
CreatedAt: head.Timestamp,
CreatedAt: head.CreatedAt,
}
}
return logPollerBlocks, nil
Expand Down
9 changes: 4 additions & 5 deletions core/chains/evm/testutils/evmtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,15 @@ func randomBytes(n int) []byte {
// Head given the value convert it into an Head
func Head(val interface{}) *evmtypes.Head {
var h evmtypes.Head
time := uint64(0)
switch t := val.(type) {
case int:
h = evmtypes.NewHead(big.NewInt(int64(t)), evmutils.NewHash(), evmutils.NewHash(), time, ubig.New(FixtureChainID))
h = evmtypes.NewHead(big.NewInt(int64(t)), evmutils.NewHash(), evmutils.NewHash(), ubig.New(FixtureChainID))
case uint64:
h = evmtypes.NewHead(big.NewInt(int64(t)), evmutils.NewHash(), evmutils.NewHash(), time, ubig.New(FixtureChainID))
h = evmtypes.NewHead(big.NewInt(int64(t)), evmutils.NewHash(), evmutils.NewHash(), ubig.New(FixtureChainID))
case int64:
h = evmtypes.NewHead(big.NewInt(t), evmutils.NewHash(), evmutils.NewHash(), time, ubig.New(FixtureChainID))
h = evmtypes.NewHead(big.NewInt(t), evmutils.NewHash(), evmutils.NewHash(), ubig.New(FixtureChainID))
case *big.Int:
h = evmtypes.NewHead(t, evmutils.NewHash(), evmutils.NewHash(), time, ubig.New(FixtureChainID))
h = evmtypes.NewHead(t, evmutils.NewHash(), evmutils.NewHash(), ubig.New(FixtureChainID))
default:
panic(fmt.Sprintf("Could not convert %v of type %T to Head", val, val))
}
Expand Down
4 changes: 2 additions & 2 deletions core/chains/evm/types/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ var _ commontypes.Head[common.Hash] = &Head{}
var _ htrktypes.Head[common.Hash, *big.Int] = &Head{}

// NewHead returns a Head instance.
func NewHead(number *big.Int, blockHash common.Hash, parentHash common.Hash, timestamp uint64, chainID *ubig.Big) Head {
func NewHead(number *big.Int, blockHash common.Hash, parentHash common.Hash, chainID *ubig.Big) Head {
return Head{
Number: number.Int64(),
Hash: blockHash,
ParentHash: parentHash,
Timestamp: time.Unix(int64(timestamp), 0),
Timestamp: time.Now(),
EVMChainID: chainID,
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/chains/evm/types/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestHead_NewHead(t *testing.T) {
}
for _, test := range tests {
t.Run(test.want, func(t *testing.T) {
num := evmtypes.NewHead(test.input, utils.NewHash(), utils.NewHash(), 0, nil)
num := evmtypes.NewHead(test.input, utils.NewHash(), utils.NewHash(), nil)
assert.Equal(t, test.want, fmt.Sprintf("%x", num.ToInt()))
})
}
Expand Down
15 changes: 7 additions & 8 deletions core/internal/cltest/cltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -992,16 +992,15 @@ func AssertEthTxAttemptCountStays(t testing.TB, txStore txmgr.TestEvmTxStore, wa
// Head given the value convert it into a Head
func Head(val interface{}) *evmtypes.Head {
var h evmtypes.Head
time := uint64(0)
switch t := val.(type) {
case int:
h = evmtypes.NewHead(big.NewInt(int64(t)), evmutils.NewHash(), evmutils.NewHash(), time, ubig.New(&FixtureChainID))
h = evmtypes.NewHead(big.NewInt(int64(t)), evmutils.NewHash(), evmutils.NewHash(), ubig.New(&FixtureChainID))
case uint64:
h = evmtypes.NewHead(big.NewInt(int64(t)), evmutils.NewHash(), evmutils.NewHash(), time, ubig.New(&FixtureChainID))
h = evmtypes.NewHead(big.NewInt(int64(t)), evmutils.NewHash(), evmutils.NewHash(), ubig.New(&FixtureChainID))
case int64:
h = evmtypes.NewHead(big.NewInt(t), evmutils.NewHash(), evmutils.NewHash(), time, ubig.New(&FixtureChainID))
h = evmtypes.NewHead(big.NewInt(t), evmutils.NewHash(), evmutils.NewHash(), ubig.New(&FixtureChainID))
case *big.Int:
h = evmtypes.NewHead(t, evmutils.NewHash(), evmutils.NewHash(), time, ubig.New(&FixtureChainID))
h = evmtypes.NewHead(t, evmutils.NewHash(), evmutils.NewHash(), ubig.New(&FixtureChainID))
default:
panic(fmt.Sprintf("Could not convert %v of type %T to Head", val, val))
}
Expand All @@ -1010,8 +1009,7 @@ func Head(val interface{}) *evmtypes.Head {

func HeadWithHash(n int64, hash common.Hash) *evmtypes.Head {
var h evmtypes.Head

Check failure on line 1011 in core/internal/cltest/cltest.go

View workflow job for this annotation

GitHub Actions / lint

S1021: should merge variable declaration with assignment on next line (gosimple)
time := uint64(0)
h = evmtypes.NewHead(big.NewInt(n), hash, evmutils.NewHash(), time, ubig.New(&FixtureChainID))
h = evmtypes.NewHead(big.NewInt(n), hash, evmutils.NewHash(), ubig.New(&FixtureChainID))
return &h
}

Expand Down Expand Up @@ -1392,11 +1390,12 @@ func (b *Blocks) slice(i, j int) (heads []*evmtypes.Head) {
func NewBlocks(t *testing.T, numHashes int) *Blocks {
hashes := make([]common.Hash, 0)
heads := make(map[int64]*evmtypes.Head)
now := time.Now()
for i := int64(0); i < int64(numHashes); i++ {
hash := evmutils.NewHash()
hashes = append(hashes, hash)

heads[i] = &evmtypes.Head{Hash: hash, Number: i, Timestamp: time.Unix(i, 0), EVMChainID: ubig.New(&FixtureChainID)}
heads[i] = &evmtypes.Head{Hash: hash, Number: i, Timestamp: now.Add(time.Duration(i) * time.Second), EVMChainID: ubig.New(&FixtureChainID)}
if i > 0 {
parent := heads[i-1]
heads[i].Parent = parent
Expand Down
2 changes: 1 addition & 1 deletion core/internal/cltest/factories.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func MustGenerateRandomKeyState(_ testing.TB) ethkey.State {
}

func MustInsertHead(t *testing.T, ds sqlutil.DataSource, number int64) evmtypes.Head {
h := evmtypes.NewHead(big.NewInt(number), evmutils.NewHash(), evmutils.NewHash(), 0, ubig.New(&FixtureChainID))
h := evmtypes.NewHead(big.NewInt(number), evmutils.NewHash(), evmutils.NewHash(), ubig.New(&FixtureChainID))
horm := headtracker.NewORM(FixtureChainID, ds)

err := horm.IdempotentInsertHead(testutils.Context(t), &h)
Expand Down
2 changes: 1 addition & 1 deletion core/services/keeper/upkeep_executer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
)

func newHead() evmtypes.Head {
return evmtypes.NewHead(big.NewInt(20), utils.NewHash(), utils.NewHash(), 1000, ubig.NewI(0))
return evmtypes.NewHead(big.NewInt(20), utils.NewHash(), utils.NewHash(), ubig.NewI(0))
}

func mockEstimator(t *testing.T) gas.EvmFeeEstimator {
Expand Down
1 change: 0 additions & 1 deletion core/services/vrf/v2/integration_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,6 @@ func TestVRFV2Integration_SingleConsumer_AlwaysRevertingCallback_StillFulfilled(
}

func TestVRFV2Integration_ConsumerProxy_HappyPath(t *testing.T) {
t.Skip("TODO FIXME")
ownerKey := cltest.MustGenerateRandomKey(t)
uni := newVRFCoordinatorV2Universe(t, ownerKey, 0)
testConsumerProxyHappyPath(
Expand Down

0 comments on commit 96fd9f3

Please sign in to comment.