From b1a399b254e390efdbf1c94b3324080f894a305d Mon Sep 17 00:00:00 2001 From: Arnau Date: Tue, 30 Jul 2024 18:34:14 +0200 Subject: [PATCH] fix UTs --- aggoracle/chaingersender/evm.go | 13 +++++---- aggoracle/e2e_test.go | 3 +- aggoracle/oracle.go | 49 +++++++++++++++++++-------------- l1infotreesync/processor.go | 17 ++++++++---- localbridgesync/processor.go | 8 +++--- 5 files changed, 53 insertions(+), 37 deletions(-) diff --git a/aggoracle/chaingersender/evm.go b/aggoracle/chaingersender/evm.go index 3a4060a4..93ce347c 100644 --- a/aggoracle/chaingersender/evm.go +++ b/aggoracle/chaingersender/evm.go @@ -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 } @@ -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) diff --git a/aggoracle/e2e_test.go b/aggoracle/e2e_test.go index ad1ff9c3..3e09c906 100644 --- a/aggoracle/e2e_test.go +++ b/aggoracle/e2e_test.go @@ -3,6 +3,7 @@ package aggoracle_test import ( "context" "errors" + "fmt" "math/big" "strconv" "testing" @@ -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[:]))) } } diff --git a/aggoracle/oracle.go b/aggoracle/oracle.go index 175801a6..49d14b7e 100644 --- a/aggoracle/oracle.go +++ b/aggoracle/oracle.go @@ -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) } @@ -31,7 +23,7 @@ type ChainSender interface { type AggOracle struct { ticker *time.Ticker - l1Client EthClienter + l1Client ethereum.ChainReader l1Info L1InfoTreer chainSender ChainSender blockFinality *big.Int @@ -39,7 +31,7 @@ type AggOracle struct { func New( chainSender ChainSender, - l1Client EthClienter, + l1Client ethereum.ChainReader, l1InfoTreeSyncer L1InfoTreer, blockFinalityType etherman.BlockNumberFinality, waitPeriodNextGER time.Duration, @@ -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 } @@ -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 } diff --git a/l1infotreesync/processor.go b/l1infotreesync/processor.go index 73c48d86..9b444d43 100644 --- a/l1infotreesync/processor.go +++ b/l1infotreesync/processor.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "github.com/0xPolygon/cdk/common" "github.com/0xPolygon/cdk/l1infotree" @@ -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") ) @@ -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 @@ -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 diff --git a/localbridgesync/processor.go b/localbridgesync/processor.go index 4a45c663..5d644a9a 100644 --- a/localbridgesync/processor.go +++ b/localbridgesync/processor.go @@ -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 } @@ -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() @@ -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 } @@ -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) }