Skip to content

Commit

Permalink
fix UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaubennassar committed Jul 30, 2024
1 parent b90342f commit b1a399b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 37 deletions.
13 changes: 7 additions & 6 deletions aggoracle/chaingersender/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func NewEVMChainGERSender(
func (c *EVMChainGERSender) IsGERAlreadyInjected(ger common.Hash) (bool, error) {
timestamp, err := c.gerContract.GlobalExitRootMap(&bind.CallOpts{Pending: false}, ger)
if err != nil {
return false, err
return false, fmt.Errorf("error calling gerContract.GlobalExitRootMap: %w", err)
}
return timestamp.Cmp(big.NewInt(0)) != 0, nil
}
Expand All @@ -95,19 +95,20 @@ func (c *EVMChainGERSender) UpdateGERWaitUntilMined(ctx context.Context, ger com
}
for {
time.Sleep(c.waitPeriodMonitorTx)
log.Debugf("waiting for tx %s to be mined", id.Hex())
res, err := c.ethTxMan.Result(ctx, id)
if err != nil {
log.Error("error calling ethTxMan.Result: ", err)
}
switch res.Status {
case ethtxmanager.MonitoredTxStatusCreated:
case ethtxmanager.MonitoredTxStatusSent:
case ethtxmanager.MonitoredTxStatusCreated,
ethtxmanager.MonitoredTxStatusSent:
continue
case ethtxmanager.MonitoredTxStatusFailed:
return fmt.Errorf("tx %s failed", res.ID)
case ethtxmanager.MonitoredTxStatusMined:
case ethtxmanager.MonitoredTxStatusSafe:
case ethtxmanager.MonitoredTxStatusFinalized:
case ethtxmanager.MonitoredTxStatusMined,
ethtxmanager.MonitoredTxStatusSafe,
ethtxmanager.MonitoredTxStatusFinalized:
return nil
default:
log.Error("unexpected tx status: ", res.Status)
Expand Down
3 changes: 2 additions & 1 deletion aggoracle/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aggoracle_test
import (
"context"
"errors"
"fmt"
"math/big"
"strconv"
"testing"
Expand Down Expand Up @@ -208,6 +209,6 @@ func runTest(
require.NoError(t, err)
isInjected, err := sender.IsGERAlreadyInjected(expectedGER)
require.NoError(t, err)
require.True(t, isInjected)
require.True(t, isInjected, fmt.Sprintf("iteration %d, GER: %s", i, common.Bytes2Hex(expectedGER[:])))
}
}
49 changes: 28 additions & 21 deletions aggoracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@ import (
"github.com/0xPolygon/cdk/l1infotreesync"
"github.com/0xPolygon/cdk/log"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
)

type EthClienter interface {
ethereum.LogFilterer
ethereum.BlockNumberReader
ethereum.ChainReader
bind.ContractBackend
}

type L1InfoTreer interface {
GetLatestInfoUntilBlock(ctx context.Context, blockNum uint64) (*l1infotreesync.L1InfoTreeLeaf, error)
}
Expand All @@ -31,15 +23,15 @@ type ChainSender interface {

type AggOracle struct {
ticker *time.Ticker
l1Client EthClienter
l1Client ethereum.ChainReader
l1Info L1InfoTreer
chainSender ChainSender
blockFinality *big.Int
}

func New(
chainSender ChainSender,
l1Client EthClienter,
l1Client ethereum.ChainReader,
l1InfoTreeSyncer L1InfoTreer,
blockFinalityType etherman.BlockNumberFinality,
waitPeriodNextGER time.Duration,
Expand All @@ -59,15 +51,23 @@ func New(
}

func (a *AggOracle) Start(ctx context.Context) {
var (
blockNumToFetch uint64
gerToInject common.Hash
err error
)
for {
select {
case <-a.ticker.C:
gerToInject, err := a.getLastFinalisedGER(ctx)
blockNumToFetch, gerToInject, err = a.getLastFinalisedGER(ctx, blockNumToFetch)
if err != nil {
if err == l1infotreesync.ErrBlockNotProcessed || err == l1infotreesync.ErrNotFound {
log.Debugf("syncer is not ready: %v", err)
if err == l1infotreesync.ErrBlockNotProcessed {
log.Debugf("syncer is not ready for the block %d", blockNumToFetch)
} else if err == l1infotreesync.ErrNotFound {
blockNumToFetch = 0
log.Debugf("syncer has not found any GER until block %d", blockNumToFetch)
} else {
log.Error("error calling isGERAlreadyInjected: ", err)
log.Error("error calling getLastFinalisedGER: ", err)
}
continue
}
Expand All @@ -90,14 +90,21 @@ func (a *AggOracle) Start(ctx context.Context) {
}
}

func (a *AggOracle) getLastFinalisedGER(ctx context.Context) (common.Hash, error) {
header, err := a.l1Client.HeaderByNumber(ctx, a.blockFinality)
if err != nil {
return common.Hash{}, err
// getLastFinalisedGER tries to return a finalised GER:
// If blockNumToFetch != 0: it will try to fetch it until the given block
// Else it will ask the L1 client for the latest finalised block and use that
// If it fails to get the GER from the syncer, it will retunr the block number that used to query
func (a *AggOracle) getLastFinalisedGER(ctx context.Context, blockNumToFetch uint64) (uint64, common.Hash, error) {
if blockNumToFetch == 0 {
header, err := a.l1Client.HeaderByNumber(ctx, a.blockFinality)
if err != nil {
return 0, common.Hash{}, err
}
blockNumToFetch = header.Number.Uint64()
}
info, err := a.l1Info.GetLatestInfoUntilBlock(ctx, header.Number.Uint64())
info, err := a.l1Info.GetLatestInfoUntilBlock(ctx, blockNumToFetch)
if err != nil {
return common.Hash{}, err
return blockNumToFetch, common.Hash{}, err
}
return info.GlobalExitRoot, nil
return 0, info.GlobalExitRoot, nil
}
17 changes: 12 additions & 5 deletions l1infotreesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"

"github.com/0xPolygon/cdk/common"
"github.com/0xPolygon/cdk/l1infotree"
Expand Down Expand Up @@ -47,6 +48,7 @@ const (
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")
lastBlockKey = []byte("lb")
)

Expand Down Expand Up @@ -208,6 +210,9 @@ func (p *processor) GetInfoByRoot(ctx context.Context, root ethCommon.Hash) (*L1
// GetLatestInfoUntilBlock returns the most recent L1InfoTreeLeaf that occurred before or at blockNum.
// If the blockNum has not been processed yet the error ErrBlockNotProcessed will be returned
func (p *processor) GetLatestInfoUntilBlock(ctx context.Context, blockNum uint64) (*L1InfoTreeLeaf, error) {
if blockNum == 0 {
return nil, ErrNoBlock0
}
tx, err := p.db.BeginRo(ctx)
if err != nil {
return nil, err
Expand All @@ -222,15 +227,17 @@ func (p *processor) GetLatestInfoUntilBlock(ctx context.Context, blockNum uint64
}
iter, err := tx.RangeDescend(blockTable, common.Uint64ToBytes(blockNum), common.Uint64ToBytes(0), 1)
if err != nil {
return nil, err
}
if !iter.HasNext() {
return nil, ErrNotFound
return nil, fmt.Errorf(
"error calling RangeDescend(blockTable, %d, 0, 1): %w", blockNum, err,
)
}
_, v, err := iter.Next()
k, v, err := iter.Next()
if err != nil {
return nil, err
}
if k == nil {
return nil, ErrNotFound
}
blk := blockWithLeafs{}
if err := json.Unmarshal(v, &blk); err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions localbridgesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (p *processor) GetClaimsAndBridges(
}
defer c.Close()

for k, v, err := c.Seek(common.Uint64To2Bytes(fromBlock)); k != nil; k, v, err = c.Next() {
for k, v, err := c.Seek(common.Uint64ToBytes(fromBlock)); k != nil; k, v, err = c.Next() {
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func (p *processor) Reorg(firstReorgedBlock uint64) error {
return err
}
defer c.Close()
firstKey := common.Uint64To2Bytes(firstReorgedBlock)
firstKey := common.Uint64ToBytes(firstReorgedBlock)
for k, _, err := c.Seek(firstKey); k != nil; k, _, err = c.Next() {
if err != nil {
tx.Rollback()
Expand Down Expand Up @@ -176,7 +176,7 @@ func (p *processor) ProcessBlock(block sync.Block) error {
tx.Rollback()
return err
}
if err := tx.Put(eventsTable, common.Uint64To2Bytes(block.Num), value); err != nil {
if err := tx.Put(eventsTable, common.Uint64ToBytes(block.Num), value); err != nil {
tx.Rollback()
return err
}
Expand All @@ -189,6 +189,6 @@ func (p *processor) ProcessBlock(block sync.Block) error {
}

func (p *processor) updateLastProcessedBlock(tx kv.RwTx, blockNum uint64) error {
blockNumBytes := common.Uint64To2Bytes(blockNum)
blockNumBytes := common.Uint64ToBytes(blockNum)
return tx.Put(lastBlockTable, lastBlokcKey, blockNumBytes)
}

0 comments on commit b1a399b

Please sign in to comment.