Skip to content

Commit

Permalink
refactor: rely only on single L1 bridge syncer in the e2e, cleanup lo…
Browse files Browse the repository at this point in the history
…gs, handle context cancellation
  • Loading branch information
Stefan-Ethernal committed Dec 18, 2024
1 parent 788b2da commit f54b164
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 110 deletions.
42 changes: 29 additions & 13 deletions bridgesync/bridgesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

const (
bridgeSyncL1 = "bridgesyncl1"
bridgeSyncL2 = "bridgesyncl2"
bridgeSyncL1 = "L1"
bridgeSyncL2 = "L2"
downloadBufferSize = 1000
)

Expand Down Expand Up @@ -107,14 +107,14 @@ func newBridgeSync(
rd ReorgDetector,
ethClient EthClienter,
initialBlock uint64,
l1OrL2ID string,
layerID string,
waitForNewBlocksPeriod time.Duration,
retryAfterErrorPeriod time.Duration,
maxRetryAttemptsAfterError int,
originNetwork uint32,
syncFullClaims bool,
) (*BridgeSync, error) {
logger := log.WithFields("bridge-syncer", l1OrL2ID)
logger := log.WithFields("bridge-syncer", layerID)
processor, err := newProcessor(dbPath, logger)
if err != nil {
return nil, err
Expand Down Expand Up @@ -143,7 +143,7 @@ func newBridgeSync(
return nil, err
}
downloader, err := sync.NewEVMDownloader(
l1OrL2ID,
layerID,
ethClient,
syncBlockChunkSize,
blockFinalityType,
Expand All @@ -156,17 +156,33 @@ func newBridgeSync(
return nil, err
}

driver, err := sync.NewEVMDriver(rd, processor, downloader, l1OrL2ID, downloadBufferSize, rh)
driver, err := sync.NewEVMDriver(rd, processor, downloader, layerID, downloadBufferSize, rh)
if err != nil {
return nil, err
}
logger.Infof("BridgeSyncer [%s] created: dbPath: %s initialBlock: %d bridgeAddr: %s, syncFullClaims: %d,"+
" maxRetryAttemptsAfterError: %d, retryAfterErrorPeriod: %s,"+
" syncBlockChunkSize: %d, blockFinalityType: %s, waitForNewBlocksPeriod: %s",
l1OrL2ID,
dbPath, initialBlock, bridge.String(), syncFullClaims,
maxRetryAttemptsAfterError, retryAfterErrorPeriod.String(),
syncBlockChunkSize, blockFinalityType, waitForNewBlocksPeriod.String())

logger.Infof(
"BridgeSyncer [%s] created:\n"+
" dbPath: %s\n"+
" initialBlock: %d\n"+
" bridgeAddr: %s\n"+
" syncFullClaims: %t\n"+
" maxRetryAttemptsAfterError: %d\n"+
" retryAfterErrorPeriod: %s\n"+
" syncBlockChunkSize: %d\n"+
" blockFinalityType: %s\n"+
" waitForNewBlocksPeriod: %s",
layerID,
dbPath,
initialBlock,
bridge.String(),
syncFullClaims,
maxRetryAttemptsAfterError,
retryAfterErrorPeriod.String(),
syncBlockChunkSize,
blockFinalityType,
waitForNewBlocksPeriod.String(),
)

return &BridgeSync{
processor: processor,
Expand Down
2 changes: 1 addition & 1 deletion bridgesync/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/0xPolygon/cdk-contracts-tooling/contracts/etrog/polygonzkevmbridge"
"github.com/0xPolygon/cdk-contracts-tooling/contracts/etrog/polygonzkevmbridgev2"
"github.com/0xPolygon/cdk-contracts-tooling/contracts/l2-sovereign-chain-paris/polygonzkevmbridgev2"
rpcTypes "github.com/0xPolygon/cdk-rpc/types"
"github.com/0xPolygon/cdk/db"
"github.com/0xPolygon/cdk/sync"
Expand Down
31 changes: 19 additions & 12 deletions claimsponsor/claimsponsor.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,22 @@ func newClaimSponsor(
}

func (c *ClaimSponsor) Start(ctx context.Context) {
var (
attempts int
)
attempts := 0

for {
err := c.claim(ctx)
if err != nil {
attempts++
c.logger.Error(err)
c.rh.Handle("claimsponsor main loop", attempts)
} else {
attempts = 0
select {
case <-ctx.Done():
return

default:
err := c.claim(ctx)
if err != nil {
attempts++
c.logger.Error(err)
c.rh.Handle("claimsponsor main loop", attempts)
} else {
attempts = 0
}
}
}
}
Expand Down Expand Up @@ -207,12 +212,14 @@ func (c *ClaimSponsor) updateClaimStatus(globalIndex *big.Int, status ClaimStatu
}

func (c *ClaimSponsor) waitTxToBeSuccessOrFail(ctx context.Context, txID string) (ClaimStatus, error) {
t := time.NewTicker(c.waitTxToBeMinedPeriod)
ticker := time.NewTicker(c.waitTxToBeMinedPeriod)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return "", errors.New("context cancelled")
case <-t.C:
case <-ticker.C:
status, err := c.sender.claimStatus(ctx, txID)
if err != nil {
return "", err
Expand Down
50 changes: 24 additions & 26 deletions claimsponsor/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/0xPolygon/cdk/bridgesync"
"github.com/0xPolygon/cdk/claimsponsor"
"github.com/0xPolygon/cdk/etherman"
"github.com/0xPolygon/cdk/log"
"github.com/0xPolygon/cdk/test/helpers"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand All @@ -23,11 +22,6 @@ func TestE2EL1toEVML2(t *testing.T) {
// start other needed components
ctx := context.Background()
env := helpers.NewE2EEnvWithEVML2(t)
dbPathBridgeSyncL1 := path.Join(t.TempDir(), "claimsponsorTestE2EL1toEVML2_bs.sqlite")
testClient := helpers.TestClient{ClientRenamed: env.L1Client.Client()}
bridgeSyncL1, err := bridgesync.NewL1(ctx, dbPathBridgeSyncL1, env.BridgeL1Addr, 10, etherman.LatestBlock, env.ReorgDetectorL1, testClient, 0, time.Millisecond*10, 0, 0, 1, false)
require.NoError(t, err)
go bridgeSyncL1.Start(ctx)

// start claim sponsor
dbPathClaimSponsor := path.Join(t.TempDir(), "claimsponsorTestE2EL1toEVML2_cs.sqlite")
Expand All @@ -46,44 +40,48 @@ func TestE2EL1toEVML2(t *testing.T) {
go claimer.Start(ctx)

// test
for i := 0; i < 3; i++ {
for i := uint32(0); i < 3; i++ {
// Send bridges to L2, wait for GER to be injected on L2
amount := big.NewInt(int64(i) + 1)
amount := new(big.Int).SetUint64(uint64(i) + 1)
env.AuthL1.Value = amount
_, err := env.BridgeL1Contract.BridgeAsset(env.AuthL1, env.NetworkIDL2, env.AuthL2.From, amount, common.Address{}, true, nil)
require.NoError(t, err)
env.L1Client.Commit()
time.Sleep(time.Millisecond * 300)

expectedGER, err := env.GERL1Contract.GetLastGlobalExitRoot(&bind.CallOpts{Pending: false})
require.NoError(t, err)
isInjected, err := env.AggOracleSender.IsGERInjected(expectedGER)
require.NoError(t, err)
require.True(t, isInjected, fmt.Sprintf("iteration %d, GER: %s", i, common.Bytes2Hex(expectedGER[:])))

// Build MP using bridgeSyncL1 & env.L1InfoTreeSync
info, err := env.L1InfoTreeSync.GetInfoByIndex(ctx, uint32(i))
info, err := env.L1InfoTreeSync.GetInfoByIndex(ctx, i)
require.NoError(t, err)
localProof, err := bridgeSyncL1.GetProof(ctx, uint32(i), info.MainnetExitRoot)

localProof, err := env.BridgeL1Sync.GetProof(ctx, i, info.MainnetExitRoot)
require.NoError(t, err)

rollupProof, err := env.L1InfoTreeSync.GetRollupExitTreeMerkleProof(ctx, 0, common.Hash{})
require.NoError(t, err)

// Request to sponsor claim
globalIndex := bridgesync.GenerateGlobalIndex(true, 0, uint32(i))
err = claimer.AddClaimToQueue(&claimsponsor.Claim{
LeafType: 0,
ProofLocalExitRoot: localProof,
ProofRollupExitRoot: rollupProof,
GlobalIndex: globalIndex,
MainnetExitRoot: info.MainnetExitRoot,
RollupExitRoot: info.RollupExitRoot,
OriginNetwork: 0,
OriginTokenAddress: common.Address{},
DestinationNetwork: env.NetworkIDL2,
DestinationAddress: env.AuthL2.From,
Amount: amount,
Metadata: nil,
})
globalIndex := bridgesync.GenerateGlobalIndex(true, 0, i)
err = claimer.AddClaimToQueue(
&claimsponsor.Claim{
LeafType: claimsponsor.LeafTypeAsset,
ProofLocalExitRoot: localProof,
ProofRollupExitRoot: rollupProof,
GlobalIndex: globalIndex,
MainnetExitRoot: info.MainnetExitRoot,
RollupExitRoot: info.RollupExitRoot,
OriginNetwork: 0,
OriginTokenAddress: common.Address{},
DestinationNetwork: env.NetworkIDL2,
DestinationAddress: env.AuthL2.From,
Amount: amount,
Metadata: nil,
})
require.NoError(t, err)

// Wait until success
Expand All @@ -103,7 +101,7 @@ func TestE2EL1toEVML2(t *testing.T) {
require.True(t, succeed)

// Check on contract that is claimed
isClaimed, err := env.BridgeL2Contract.IsClaimed(&bind.CallOpts{Pending: false}, uint32(i), 0)
isClaimed, err := env.BridgeL2Contract.IsClaimed(&bind.CallOpts{Pending: false}, i, 0)
require.NoError(t, err)
require.True(t, isClaimed)
}
Expand Down
49 changes: 22 additions & 27 deletions claimsponsor/evmclaimsponsor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"math/big"
"time"

"github.com/0xPolygon/cdk-contracts-tooling/contracts/etrog/polygonzkevmbridgev2"
"github.com/0xPolygon/cdk-contracts-tooling/contracts/l2-sovereign-chain-paris/polygonzkevmbridgev2"
configTypes "github.com/0xPolygon/cdk/config/types"
"github.com/0xPolygon/cdk/log"
"github.com/0xPolygon/zkevm-ethtx-manager/ethtxmanager"
Expand All @@ -22,8 +22,9 @@ const (
// LeafTypeAsset represents a bridge asset
LeafTypeAsset uint8 = 0
// LeafTypeMessage represents a bridge message
LeafTypeMessage uint8 = 1
gasTooHighErrTemplate = "Claim tx estimated to consume more gas than the maximum allowed by the service. " +
LeafTypeMessage uint8 = 1

gasTooHighErrTemplate = "Claim tx estimated to consume more gas than the maximum allowed by the service. " +
"Estimated %d, maximum allowed: %d"
)

Expand All @@ -41,15 +42,13 @@ type EthTxManager interface {
}

type EVMClaimSponsor struct {
*ClaimSponsor
l2Client EthClienter
bridgeABI *abi.ABI
bridgeAddr common.Address
bridgeContract *polygonzkevmbridgev2.Polygonzkevmbridgev2
ethTxManager EthTxManager
sender common.Address
gasOffest uint64
maxGas uint64
l2Client EthClienter
bridgeABI *abi.ABI
bridgeAddr common.Address
ethTxManager EthTxManager
sender common.Address
gasOffest uint64
maxGas uint64
}

type EVMClaimSponsorConfig struct {
Expand Down Expand Up @@ -83,7 +82,7 @@ func NewEVMClaimSponsor(
logger *log.Logger,
dbPath string,
l2Client EthClienter,
bridge common.Address,
bridgeAddr common.Address,
sender common.Address,
maxGas, gasOffset uint64,
ethTxManager EthTxManager,
Expand All @@ -92,24 +91,21 @@ func NewEVMClaimSponsor(
waitTxToBeMinedPeriod time.Duration,
waitOnEmptyQueue time.Duration,
) (*ClaimSponsor, error) {
contract, err := polygonzkevmbridgev2.NewPolygonzkevmbridgev2(bridge, l2Client)
if err != nil {
return nil, err
}
abi, err := polygonzkevmbridgev2.Polygonzkevmbridgev2MetaData.GetAbi()
if err != nil {
return nil, err
}

evmSponsor := &EVMClaimSponsor{
l2Client: l2Client,
bridgeABI: abi,
bridgeAddr: bridge,
bridgeContract: contract,
sender: sender,
gasOffest: gasOffset,
maxGas: maxGas,
ethTxManager: ethTxManager,
l2Client: l2Client,
bridgeABI: abi,
bridgeAddr: bridgeAddr,
sender: sender,
gasOffest: gasOffset,
maxGas: maxGas,
ethTxManager: ethTxManager,
}

baseSponsor, err := newClaimSponsor(
logger,
dbPath,
Expand All @@ -122,7 +118,6 @@ func NewEVMClaimSponsor(
if err != nil {
return nil, err
}
evmSponsor.ClaimSponsor = baseSponsor

return baseSponsor, nil
}
Expand Down Expand Up @@ -152,7 +147,7 @@ func (c *EVMClaimSponsor) sendClaim(ctx context.Context, claim *Claim) (string,
if err != nil {
return "", err
}
id, err := c.ethTxManager.Add(ctx, &c.bridgeAddr, big.NewInt(0), data, c.gasOffest, nil)
id, err := c.ethTxManager.Add(ctx, &c.bridgeAddr, common.Big0, data, c.gasOffest, nil)
if err != nil {
return "", err
}
Expand Down
9 changes: 4 additions & 5 deletions lastgersync/evmdownloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,10 @@ func (d *downloader) getGERsFromIndex(ctx context.Context, fromL1InfoTreeIndex u
if err != nil {
return nil, fmt.Errorf("error calling GetInfoByIndex: %w", err)
}
gers = append(gers,
Event{
L1InfoTreeIndex: i,
GlobalExitRoot: info.GlobalExitRoot,
})
gers = append(gers, Event{
L1InfoTreeIndex: i,
GlobalExitRoot: info.GlobalExitRoot,
})
}

return gers, nil
Expand Down
1 change: 1 addition & 0 deletions test/helpers/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func NewE2EEnvWithEVML2(t *testing.T) *AggoracleWithEVMChainEnv {
sender, l2Client, gerL2Contract, gerL2Addr,
bridgeL2Contract, bridgeL2Addr, authL2,
ethTxManMockL2, bridgeL2Sync, rdL2 := L2SetupEVM(t)

oracle, err := aggoracle.New(
log.GetDefaultLogger(), sender,
l1Client.Client(), syncer,
Expand Down
Loading

0 comments on commit f54b164

Please sign in to comment.