Skip to content

Commit

Permalink
add UTs for new methods in the l1infotreesync processor
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaubennassar committed Sep 10, 2024
1 parent 26b1df5 commit a4f3c0b
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 25 deletions.
3 changes: 2 additions & 1 deletion aggoracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"
"time"

"github.com/0xPolygon/cdk/db"
"github.com/0xPolygon/cdk/etherman"
"github.com/0xPolygon/cdk/l1infotreesync"
"github.com/0xPolygon/cdk/log"
Expand Down Expand Up @@ -63,7 +64,7 @@ func (a *AggOracle) Start(ctx context.Context) {
if err != nil {
if err == l1infotreesync.ErrBlockNotProcessed {
log.Debugf("syncer is not ready for the block %d", blockNumToFetch)
} else if err == l1infotreesync.ErrNotFound {
} else if err == db.ErrNotFound {
blockNumToFetch = 0
log.Debugf("syncer has not found any GER until block %d", blockNumToFetch)
} else {
Expand Down
12 changes: 12 additions & 0 deletions db/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package db

import (
"database/sql"
"errors"

_ "github.com/mattn/go-sqlite3"
)

var (
ErrNotFound = errors.New("not found")
)

// NewSQLiteDB creates a new SQLite DB
func NewSQLiteDB(dbPath string) (*sql.DB, error) {
initMeddler()
Expand All @@ -21,3 +26,10 @@ func NewSQLiteDB(dbPath string) (*sql.DB, error) {
`)
return db, err
}

func ReturnErrNotFound(err error) error {
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFound
}
return err
}
31 changes: 28 additions & 3 deletions l1infotreesync/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func newSimulatedClient(auth *bind.TransactOpts) (

func TestE2E(t *testing.T) {
ctx := context.Background()
dbPath := path.Join(t.TempDir(), "file::memory:?cache=shared")
dbPath := "file::memory:?cache=shared"
privateKey, err := crypto.GenerateKey()
require.NoError(t, err)
auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(1337))
Expand Down Expand Up @@ -118,18 +118,43 @@ func TestE2E(t *testing.T) {
tx, err := verifySC.VerifyBatches(auth, rollupID, 0, newLocalExitRoot, common.Hash{}, i%2 != 0)
require.NoError(t, err)
client.Commit()
// Let the processor catch up
time.Sleep(time.Millisecond * 100)
receipt, err := client.Client().TransactionReceipt(ctx, tx.Hash())
require.NoError(t, err)
require.Equal(t, receipt.Status, types.ReceiptStatusSuccessful)
require.True(t, len(receipt.Logs) == 1+i%2+i%2)

// Let the processor catch
processorUpdated := false
for i := 0; i < 30; i++ {
lpb, err := syncer.GetLastProcessedBlock(ctx)
require.NoError(t, err)
if receipt.BlockNumber.Uint64() == lpb {
processorUpdated = true
break
}
time.Sleep(time.Millisecond * 10)
}
require.True(t, processorUpdated)

// Assert rollup exit root
expectedRollupExitRoot, err := verifySC.GetRollupExitRoot(&bind.CallOpts{Pending: false})
require.NoError(t, err)
actualRollupExitRoot, err := syncer.GetLastRollupExitRoot(ctx)
require.NoError(t, err)
require.Equal(t, common.Hash(expectedRollupExitRoot), actualRollupExitRoot.Hash, fmt.Sprintf("rollupID: %d, i: %d", rollupID, i))

// Assert verify batches
expectedVerify := l1infotreesync.VerifyBatches{
BlockNumber: receipt.BlockNumber.Uint64(),
BlockPosition: uint64(i%2 + i%2),
RollupID: rollupID,
ExitRoot: newLocalExitRoot,
Aggregator: auth.From,
RollupExitRoot: expectedRollupExitRoot,
}
actualVerify, err := syncer.GetLastVerifiedBatches(rollupID)
require.NoError(t, err)
require.Equal(t, expectedVerify, *actualVerify)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions l1infotreesync/migrations/l1infotreesync0001.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ CREATE TABLE l1info_leaf (
CREATE TABLE verify_batches (
block_num INTEGER NOT NULL REFERENCES block(num) ON DELETE CASCADE,
block_pos INTEGER NOT NULL,
rollup_id INTEGER NOT NULL,
batch_num INTEGER NOT NULL,
state_root VARCHAR NOT NULL,
exit_root VARCHAR NOT NULL,
Expand Down
46 changes: 26 additions & 20 deletions l1infotreesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

var (
ErrBlockNotProcessed = errors.New("given block(s) have not been processed yet")
ErrNotFound = errors.New("not found")
ErrNoBlock0 = errors.New("blockNum must be greater than 0")
)

Expand All @@ -43,15 +42,15 @@ type UpdateL1InfoTree struct {
// VerifyBatches representation of the VerifyBatches and VerifyBatchesTrustedAggregator events
type VerifyBatches struct {
BlockNumber uint64 `meddler:"block_num"`
BlockPosition uint64 `meddler:"block_num"`
RollupID uint32 `meddler:"block_pos"`
BlockPosition uint64 `meddler:"block_pos"`
RollupID uint32 `meddler:"rollup_id"`
NumBatch uint64 `meddler:"batch_num"`
StateRoot ethCommon.Hash `meddler:"state_root,hash"`
ExitRoot ethCommon.Hash `meddler:"exit_root,hash"`
Aggregator ethCommon.Address `meddler:"aggregator,address"`

// Not provided by downloader
RollupExitRoot ethCommon.Hash `meddler:"exit_root,hash"`
RollupExitRoot ethCommon.Hash `meddler:"rollup_exit_root,hash"`
}

type InitL1InfoRootMap struct {
Expand Down Expand Up @@ -150,7 +149,7 @@ func (p *processor) GetLatestInfoUntilBlock(ctx context.Context, blockNum uint64
)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNotFound
return nil, db.ErrNotFound
}
return nil, err
}
Expand Down Expand Up @@ -241,7 +240,7 @@ func (p *processor) ProcessBlock(ctx context.Context, b sync.Block) error {
var initialL1InfoIndex uint32
var l1InfoLeavesAdded uint32
lastIndex, err := p.getLastIndex(tx)
if err == ErrNotFound {
if err == db.ErrNotFound {
initialL1InfoIndex = 0
err = nil
} else if err != nil {
Expand Down Expand Up @@ -312,75 +311,82 @@ func (p *processor) getLastIndex(tx db.DBer) (uint32, error) {
row := tx.QueryRow("SELECT position FROM l1info_leaf ORDER BY block_num DESC, block_pos DESC LIMIT 1;")
err := row.Scan(&lastProcessedIndex)
if errors.Is(err, sql.ErrNoRows) {
return 0, ErrNotFound
return 0, db.ErrNotFound
}
return lastProcessedIndex, err
}

func (p *processor) GetLastVerifiedBatches(rollupID uint32) (*VerifyBatches, error) {
verified := &VerifyBatches{}
return verified, meddler.QueryRow(p.db, verified, `
SELECT * FROM verified_batches
err := meddler.QueryRow(p.db, verified, `
SELECT * FROM verify_batches
WHERE rollup_id = $1
ORDER BY block_num DESC, block_pos DESC
LIMIT 1;
`, rollupID)
return verified, db.ReturnErrNotFound(err)
}

func (p *processor) GetFirstVerifiedBatches(rollupID uint32) (*VerifyBatches, error) {
verified := &VerifyBatches{}
return verified, meddler.QueryRow(p.db, verified, `
SELECT * FROM verified_batches
err := meddler.QueryRow(p.db, verified, `
SELECT * FROM verify_batches
WHERE rollup_id = $1
ORDER BY block_num ASC, block_pos ASC
LIMIT 1;
`, rollupID)
return verified, db.ReturnErrNotFound(err)
}

func (p *processor) GetFirstVerifiedBatchesAfterBlock(rollupID uint32, blockNum uint64) (*VerifyBatches, error) {
verified := &VerifyBatches{}
return verified, meddler.QueryRow(p.db, verified, `
SELECT * FROM verified_batches
err := meddler.QueryRow(p.db, verified, `
SELECT * FROM verify_batches
WHERE rollup_id = $1 AND block_num >= $2
ORDER BY block_num ASC, block_pos ASC
LIMIT 1;
`, rollupID, blockNum)
return verified, db.ReturnErrNotFound(err)
}

func (p *processor) GetFirstL1InfoWithRollupExitRoot(rollupExitRoot ethCommon.Hash) (*L1InfoTreeLeaf, error) {
info := &L1InfoTreeLeaf{}
return info, meddler.QueryRow(p.db, info, `
err := meddler.QueryRow(p.db, info, `
SELECT * FROM l1info_leaf
WHERE rollup_exit_root = $1
ORDER BY block_num ASC, block_pos ASC
LIMIT 1;
`, rollupExitRoot)
`, rollupExitRoot.Hex())
return info, db.ReturnErrNotFound(err)
}

func (p *processor) GetLastInfo() (*L1InfoTreeLeaf, error) {
info := &L1InfoTreeLeaf{}
return info, meddler.QueryRow(p.db, info, `
err := meddler.QueryRow(p.db, info, `
SELECT * FROM l1info_leaf
ORDER BY block_num ASC, block_pos ASC
ORDER BY block_num DESC, block_pos DESC
LIMIT 1;
`)
return info, db.ReturnErrNotFound(err)
}

func (p *processor) GetFirstInfo() (*L1InfoTreeLeaf, error) {
info := &L1InfoTreeLeaf{}
return info, meddler.QueryRow(p.db, info, `
err := meddler.QueryRow(p.db, info, `
SELECT * FROM l1info_leaf
ORDER BY block_num DESC, block_pos DESC
ORDER BY block_num ASC, block_pos ASC
LIMIT 1;
`)
return info, db.ReturnErrNotFound(err)
}

func (p *processor) GetFirstInfoAfterBlock(blockNum uint64) (*L1InfoTreeLeaf, error) {
info := &L1InfoTreeLeaf{}
return info, meddler.QueryRow(p.db, info, `
err := meddler.QueryRow(p.db, info, `
SELECT * FROM l1info_leaf
WHERE block_num >= $1
ORDER BY block_num ASC, block_pos ASC
LIMIT 1;
`, blockNum)
return info, db.ReturnErrNotFound(err)
}
Loading

0 comments on commit a4f3c0b

Please sign in to comment.