Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
test(all): add more tests (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha committed Oct 30, 2023
1 parent 93b9ecf commit 9e9c7f3
Show file tree
Hide file tree
Showing 26 changed files with 576 additions and 161 deletions.
54 changes: 54 additions & 0 deletions bindings/encoding/input_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package encoding

import (
"context"
"math/big"
"math/rand"
"os"
"testing"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/require"
"github.com/taikoxyz/taiko-client/bindings"
)

func TestEncodeEvidence(t *testing.T) {
Expand Down Expand Up @@ -42,6 +48,18 @@ func TestEncodeProverAssignment(t *testing.T) {
require.NotNil(t, encoded)
}

func TestEncodeProverAssignmentPayload(t *testing.T) {
encoded, err := EncodeProverAssignmentPayload(
common.BytesToHash(randomBytes(32)),
common.BytesToAddress(randomBytes(20)),
120,
[]TierFee{{Tier: 0, Fee: common.Big1}},
)

require.Nil(t, err)
require.NotNil(t, encoded)
}

func TestUnpackTxListBytes(t *testing.T) {
_, err := UnpackTxListBytes(randomBytes(1024))
require.NotNil(t, err)
Expand All @@ -53,4 +71,40 @@ func TestUnpackTxListBytes(t *testing.T) {
),
)
require.ErrorContains(t, err, "no method with id")

cli, err := ethclient.Dial(os.Getenv("L1_NODE_WS_ENDPOINT"))
require.Nil(t, err)

chainID, err := cli.ChainID(context.Background())
require.Nil(t, err)

taikoL1, err := bindings.NewTaikoL1Client(
common.HexToAddress(os.Getenv("TAIKO_L1_ADDRESS")),
cli,
)
require.Nil(t, err)

l1ProposerPrivKey, err := crypto.ToECDSA(common.Hex2Bytes(os.Getenv("L1_PROPOSER_PRIVATE_KEY")))
require.Nil(t, err)

opts, err := bind.NewKeyedTransactorWithChainID(l1ProposerPrivKey, chainID)
require.Nil(t, err)

opts.NoSend = true
opts.GasLimit = randomHash().Big().Uint64()

txListBytes := randomBytes(1024)

tx, err := taikoL1.ProposeBlock(
opts,
randomHash(),
[32]byte(randomHash().Bytes()),
randomBytes(32),
txListBytes,
)
require.Nil(t, err)

b, err := UnpackTxListBytes(tx.Data())
require.Nil(t, err)
require.Equal(t, txListBytes, b)
}
29 changes: 7 additions & 22 deletions driver/chain_syncer/beaconsync/progress_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

var (
syncProgressCheckInterval = 10 * time.Second
syncProgressCheckInterval = 12 * time.Second
)

// SyncProgressTracker is responsible for tracking the L2 execution engine's sync progress, after
Expand All @@ -24,10 +24,9 @@ type SyncProgressTracker struct {
client *rpc.EthClient

// Meta data
triggered bool
lastSyncedVerifiedBlockID *big.Int
lastSyncedVerifiedBlockHeight *big.Int
lastSyncedVerifiedBlockHash common.Hash
triggered bool
lastSyncedVerifiedBlockID *big.Int
lastSyncedVerifiedBlockHash common.Hash

// Out-of-sync check related
lastSyncProgress *ethereum.SyncProgress
Expand Down Expand Up @@ -94,12 +93,11 @@ func (t *SyncProgressTracker) track(ctx context.Context) {
return
}

if new(big.Int).SetUint64(headHeight).Cmp(t.lastSyncedVerifiedBlockHeight) >= 0 {
if new(big.Int).SetUint64(headHeight).Cmp(t.lastSyncedVerifiedBlockID) >= 0 {
t.lastProgressedTime = time.Now()
log.Info("L2 execution engine has finished the P2P sync work, all verified blocks synced, "+
"will switch to insert pending blocks one by one",
"lastSyncedVerifiedBlockID", t.lastSyncedVerifiedBlockID,
"lastSyncedVerifiedBlockHeight", t.lastSyncedVerifiedBlockHeight,
"lastSyncedVerifiedBlockHash", t.lastSyncedVerifiedBlockHash,
)
return
Expand Down Expand Up @@ -134,19 +132,18 @@ func (t *SyncProgressTracker) track(ctx context.Context) {
}

// UpdateMeta updates the inner beacon sync meta data.
func (t *SyncProgressTracker) UpdateMeta(id, height *big.Int, blockHash common.Hash) {
func (t *SyncProgressTracker) UpdateMeta(id *big.Int, blockHash common.Hash) {
t.mutex.Lock()
defer t.mutex.Unlock()

log.Debug("Update sync progress tracker meta", "id", id, "height", height, "hash", blockHash)
log.Debug("Update sync progress tracker meta", "id", id, "hash", blockHash)

if !t.triggered {
t.lastProgressedTime = time.Now()
}

t.triggered = true
t.lastSyncedVerifiedBlockID = id
t.lastSyncedVerifiedBlockHeight = height
t.lastSyncedVerifiedBlockHash = blockHash
}

Expand Down Expand Up @@ -203,18 +200,6 @@ func (t *SyncProgressTracker) LastSyncedVerifiedBlockID() *big.Int {
return new(big.Int).Set(t.lastSyncedVerifiedBlockID)
}

// LastSyncedVerifiedBlockHeight returns tracker.lastSyncedVerifiedBlockHeight.
func (t *SyncProgressTracker) LastSyncedVerifiedBlockHeight() *big.Int {
t.mutex.RLock()
defer t.mutex.RUnlock()

if t.lastSyncedVerifiedBlockHeight == nil {
return nil
}

return new(big.Int).Set(t.lastSyncedVerifiedBlockHeight)
}

// LastSyncedVerifiedBlockHash returns tracker.lastSyncedVerifiedBlockHash.
func (t *SyncProgressTracker) LastSyncedVerifiedBlockHash() common.Hash {
t.mutex.RLock()
Expand Down
8 changes: 1 addition & 7 deletions driver/chain_syncer/beaconsync/progress_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *BeaconSyncProgressTrackerTestSuite) TestTrack() {

// Triggered
ctx, cancel = context.WithCancel(context.Background())
s.t.UpdateMeta(common.Big256, common.Big256, testutils.RandomHash())
s.t.UpdateMeta(common.Big256, testutils.RandomHash())
go s.t.Track(ctx)
time.Sleep(syncProgressCheckInterval + 5*time.Second)
cancel()
Expand Down Expand Up @@ -90,12 +90,6 @@ func (s *BeaconSyncProgressTrackerTestSuite) TestLastSyncedVerifiedBlockID() {
s.Equal(common.Big1.Uint64(), s.t.LastSyncedVerifiedBlockID().Uint64())
}

func (s *BeaconSyncProgressTrackerTestSuite) TestLastSyncedVerifiedBlockHeight() {
s.Nil(s.t.LastSyncedVerifiedBlockHeight())
s.t.lastSyncedVerifiedBlockHeight = common.Big1
s.Equal(common.Big1.Uint64(), s.t.LastSyncedVerifiedBlockHeight().Uint64())
}

func (s *BeaconSyncProgressTrackerTestSuite) TestLastSyncedVerifiedBlockHash() {
s.Equal(
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
Expand Down
4 changes: 1 addition & 3 deletions driver/chain_syncer/beaconsync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (s *Syncer) TriggerBeaconSync() error {
if s.progressTracker.lastSyncProgress == nil {
log.Info(
"Syncing beacon headers, please check L2 execution engine logs for progress",
"currentSyncHead", s.progressTracker.LastSyncedVerifiedBlockHeight(),
"currentSyncHead", s.progressTracker.LastSyncedVerifiedBlockID(),
"newBlockID", blockID,
)
}
Expand Down Expand Up @@ -80,14 +80,12 @@ func (s *Syncer) TriggerBeaconSync() error {
// Update sync status.
s.progressTracker.UpdateMeta(
blockID,
new(big.Int).SetUint64(latestVerifiedHeadPayload.Number),
latestVerifiedHeadPayload.BlockHash,
)

log.Info(
"⛓️ Beacon sync triggered",
"newHeadID", blockID,
"newHeadHeight", s.progressTracker.LastSyncedVerifiedBlockHeight(),
"newHeadHash", s.progressTracker.LastSyncedVerifiedBlockHash(),
)

Expand Down
16 changes: 4 additions & 12 deletions driver/chain_syncer/chain_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,13 @@ func (s *L2ChainSyncer) Sync(l1End *types.Header) error {
"lastSyncedVerifiedBlockHash", s.progressTracker.LastSyncedVerifiedBlockHash(),
)

heightOrID := &state.HeightOrID{Height: l2Head.Number}

// If the L2 execution engine has synced to latest verified block.
if l2Head.Hash() == s.progressTracker.LastSyncedVerifiedBlockHash() {
heightOrID.ID = s.progressTracker.LastSyncedVerifiedBlockID()
}

// Reset the L1Current cursor.
_, blockID, err := s.state.ResetL1Current(s.ctx, heightOrID)
if err != nil {
if _, err := s.state.ResetL1Current(s.ctx, l2Head.Number); err != nil {
return err
}

// Reset to the latest L2 execution engine's chain status.
s.progressTracker.UpdateMeta(blockID, heightOrID.Height, l2Head.Hash())
s.progressTracker.UpdateMeta(l2Head.Number, l2Head.Hash())
}

// Insert the proposed block one by one.
Expand All @@ -139,8 +131,8 @@ func (s *L2ChainSyncer) AheadOfProtocolVerifiedHead() bool {
return false
}

if s.progressTracker.LastSyncedVerifiedBlockHeight() != nil {
return s.state.GetL2Head().Number.Uint64() >= s.progressTracker.LastSyncedVerifiedBlockHeight().Uint64()
if s.progressTracker.LastSyncedVerifiedBlockID() != nil {
return s.state.GetL2Head().Number.Uint64() >= s.progressTracker.LastSyncedVerifiedBlockID().Uint64()
}

return true
Expand Down
4 changes: 1 addition & 3 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/suite"
"github.com/taikoxyz/taiko-client/bindings/encoding"
"github.com/taikoxyz/taiko-client/driver/state"
"github.com/taikoxyz/taiko-client/pkg/jwt"
"github.com/taikoxyz/taiko-client/proposer"
"github.com/taikoxyz/taiko-client/testutils"
Expand Down Expand Up @@ -308,8 +307,7 @@ func (s *DriverTestSuite) TestL1Current() {
// propose and insert a block
testutils.ProposeAndInsertEmptyBlocks(&s.ClientTestSuite, s.p, s.d.ChainSyncer().CalldataSyncer())
// reset L1 current with increased height
_, id, err := s.d.state.ResetL1Current(s.d.ctx, &state.HeightOrID{ID: common.Big1})
s.Equal(common.Big1, id)
_, err := s.d.state.ResetL1Current(s.d.ctx, common.Big1)
s.Nil(err)
}

Expand Down
80 changes: 14 additions & 66 deletions driver/state/l1_current.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,72 +31,21 @@ func (s *State) SetL1Current(h *types.Header) {
// BlockProposed event with given blockID / blockHash.
func (s *State) ResetL1Current(
ctx context.Context,
heightOrID *HeightOrID,
) (*bindings.TaikoL1ClientBlockProposed, *big.Int, error) {
if !heightOrID.NotEmpty() {
return nil, nil, fmt.Errorf("empty input %v", heightOrID)
blockID *big.Int,
) (*bindings.TaikoL1ClientBlockProposed, error) {
if blockID == nil {
return nil, fmt.Errorf("empty block ID")
}

log.Info("Reset L1 current cursor", "heightOrID", heightOrID)
log.Info("Reset L1 current cursor", "blockID", blockID)

var (
err error
)

if (heightOrID.ID != nil && heightOrID.ID.Cmp(common.Big0) == 0) ||
(heightOrID.Height != nil && heightOrID.Height.Cmp(common.Big0) == 0) {
if blockID.Cmp(common.Big0) == 0 {
l1Current, err := s.rpc.L1.HeaderByNumber(ctx, s.GenesisL1Height)
if err != nil {
return nil, nil, err
return nil, err
}
s.SetL1Current(l1Current)
return nil, common.Big0, nil
}

// Need to find the block ID at first, before filtering the BlockProposed events.
if heightOrID.ID == nil {
header, err := s.rpc.L2.HeaderByNumber(context.Background(), heightOrID.Height)
if err != nil {
return nil, nil, err
}
targetHash := header.Hash()

iter, err := eventIterator.NewTransitionProvedIterator(
ctx,
&eventIterator.TransitionProvenIteratorConfig{
Client: s.rpc.L1,
TaikoL1: s.rpc.TaikoL1,
StartHeight: s.GenesisL1Height,
EndHeight: s.GetL1Head().Number,
FilterQuery: []*big.Int{},
Reverse: true,
OnTransitionProved: func(
ctx context.Context,
e *bindings.TaikoL1ClientTransitionProved,
end eventIterator.EndTransitionProvedEventIterFunc,
) error {
log.Debug("Filtered TransitionProved event", "ID", e.BlockId, "hash", common.Hash(e.BlockHash))
if e.BlockHash == targetHash {
heightOrID.ID = e.BlockId
end()
}

return nil
},
},
)

if err != nil {
return nil, nil, err
}

if err := iter.Iter(); err != nil {
return nil, nil, err
}

if heightOrID.ID == nil {
return nil, nil, fmt.Errorf("TransitionProved event not found, hash: %s", targetHash)
}
return nil, nil
}

var event *bindings.TaikoL1ClientBlockProposed
Expand All @@ -107,7 +56,7 @@ func (s *State) ResetL1Current(
TaikoL1: s.rpc.TaikoL1,
StartHeight: s.GenesisL1Height,
EndHeight: s.GetL1Head().Number,
FilterQuery: []*big.Int{heightOrID.ID},
FilterQuery: []*big.Int{blockID},
Reverse: true,
OnBlockProposedEvent: func(
ctx context.Context,
Expand All @@ -120,26 +69,25 @@ func (s *State) ResetL1Current(
},
},
)

if err != nil {
return nil, nil, err
return nil, err
}

if err := iter.Iter(); err != nil {
return nil, nil, err
return nil, err
}

if event == nil {
return nil, nil, fmt.Errorf("BlockProposed event not found, blockID: %s", heightOrID.ID)
return nil, fmt.Errorf("BlockProposed event not found, blockID: %s", blockID)
}

l1Current, err := s.rpc.L1.HeaderByNumber(ctx, new(big.Int).SetUint64(event.Raw.BlockNumber))
if err != nil {
return nil, nil, err
return nil, err
}
s.SetL1Current(l1Current)

log.Info("Reset L1 current cursor", "height", s.GetL1Current().Number)

return event, heightOrID.ID, nil
return event, nil
}
Loading

0 comments on commit 9e9c7f3

Please sign in to comment.