From 467f15f15c6cae45f2c29c94cb11c81ea6129eeb Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Wed, 17 Jul 2024 10:06:44 +0200 Subject: [PATCH 01/51] wip --- cmd/run.go | 43 +++- common/config.go | 8 + config/config.go | 4 + config/default.go | 4 + etherman/aggregator.go | 6 +- etherman/contracts.go | 138 ++++++++++++ etherman/contracts/banana_daprotocol.go | 21 ++ etherman/contracts/banana_globalexitroot.go | 59 +++++ etherman/contracts/banana_rollup.go | 38 ++++ etherman/contracts/banana_rollupmanager.go | 69 ++++++ etherman/contracts/base.go | 50 +++++ etherman/contracts/elderberry_daprotocol.go | 21 ++ .../contracts/elderberry_globalexitroot.go | 30 +++ etherman/contracts/elderberry_rollup.go | 38 ++++ .../contracts/elderberry_rollupmanager.go | 69 ++++++ etherman/contracts/interfaces.go | 79 +++++++ etherman/contracts/router_rollup.go | 6 + etherman/errors.go | 2 +- etherman/errors_test.go | 6 +- etherman/etherman.go | 211 ++---------------- go.sum | 2 - sequencesender/seqsendertypes/batch.go | 45 ++++ sequencesender/sequencesender.go | 177 +++++++-------- sequencesender/sequencesender_test.go | 7 + sequencesender/txbuilder/banana_base.go | 111 +++++++++ sequencesender/txbuilder/banana_types.go | 132 +++++++++++ sequencesender/txbuilder/banana_validium.go | 100 +++++++++ sequencesender/txbuilder/banana_zkevm.go | 79 +++++++ sequencesender/txbuilder/elderberry_types.go | 55 +++++ sequencesender/txbuilder/elderberry_zkevm.go | 105 +++++++++ sequencesender/txbuilder/interface.go | 17 ++ 31 files changed, 1431 insertions(+), 301 deletions(-) create mode 100644 common/config.go create mode 100644 etherman/contracts.go create mode 100644 etherman/contracts/banana_daprotocol.go create mode 100644 etherman/contracts/banana_globalexitroot.go create mode 100644 etherman/contracts/banana_rollup.go create mode 100644 etherman/contracts/banana_rollupmanager.go create mode 100644 etherman/contracts/base.go create mode 100644 etherman/contracts/elderberry_daprotocol.go create mode 100644 etherman/contracts/elderberry_globalexitroot.go create mode 100644 etherman/contracts/elderberry_rollup.go create mode 100644 etherman/contracts/elderberry_rollupmanager.go create mode 100644 etherman/contracts/interfaces.go create mode 100644 etherman/contracts/router_rollup.go create mode 100644 sequencesender/seqsendertypes/batch.go create mode 100644 sequencesender/txbuilder/banana_base.go create mode 100644 sequencesender/txbuilder/banana_types.go create mode 100644 sequencesender/txbuilder/banana_validium.go create mode 100644 sequencesender/txbuilder/banana_zkevm.go create mode 100644 sequencesender/txbuilder/elderberry_types.go create mode 100644 sequencesender/txbuilder/elderberry_zkevm.go create mode 100644 sequencesender/txbuilder/interface.go diff --git a/cmd/run.go b/cmd/run.go index db1090bb..983a5158 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -16,8 +16,10 @@ import ( "github.com/0xPolygon/cdk/dataavailability" "github.com/0xPolygon/cdk/dataavailability/datacommittee" "github.com/0xPolygon/cdk/etherman" + "github.com/0xPolygon/cdk/etherman/contracts" "github.com/0xPolygon/cdk/log" "github.com/0xPolygon/cdk/sequencesender" + "github.com/0xPolygon/cdk/sequencesender/txbuilder" "github.com/0xPolygon/cdk/state" "github.com/0xPolygon/cdk/state/pgstatestorage" ethtxman "github.com/0xPolygonHermez/zkevm-ethtx-manager/etherman" @@ -123,7 +125,7 @@ func createSequenceSender(cfg config.Config) *sequencesender.SequenceSender { HTTPHeaders: cfg.SequenceSender.EthTxManager.Etherman.HTTPHeaders, }, IsValidiumMode: cfg.SequenceSender.IsValidiumMode, - }, cfg.NetworkConfig.L1Config) + }, cfg.NetworkConfig.L1Config, cfg.Common) if err != nil { log.Fatal(err) } @@ -134,12 +136,11 @@ func createSequenceSender(cfg config.Config) *sequencesender.SequenceSender { } cfg.SequenceSender.SenderAddress = auth.From - da, err := newDataAvailability(cfg, ethman) + txBuilder, err := newTxBuilder(cfg, ethman) if err != nil { log.Fatal(err) } - - seqSender, err := sequencesender.New(cfg.SequenceSender, ethman, da) + seqSender, err := sequencesender.New(cfg.SequenceSender, ethman, txBuilder) if err != nil { log.Fatal(err) } @@ -147,6 +148,38 @@ func createSequenceSender(cfg config.Config) *sequencesender.SequenceSender { return seqSender } +func newTxBuilder(cfg config.Config, ethman *etherman.Client) (txbuilder.TxBuilder, error) { + auth, _, err := ethman.LoadAuthFromKeyStore(cfg.SequenceSender.PrivateKey.Path, cfg.SequenceSender.PrivateKey.Password) + if err != nil { + log.Fatal(err) + } + da, err := newDataAvailability(cfg, ethman) + if err != nil { + log.Fatal(err) + } + var txBuilder txbuilder.TxBuilder + + switch contracts.VersionType(cfg.Common.ContractVersions) { + case contracts.VersionBanana: + if cfg.Common.IsValidiumMode { + txBuilder = txbuilder.NewTxBuilderBananaValidium(*ethman.Contracts.Banana.ZkEVM, da, *auth, auth.From) + } else { + txBuilder = txbuilder.NewTxBuilderBananaZKEVM(*ethman.Contracts.Banana.ZkEVM, *auth, auth.From) + } + case contracts.VersionElderberry: + if cfg.Common.IsValidiumMode { + err = fmt.Errorf("Elderberry+Validium not implemented yet") + //txBuilder = txbuilder.NewTxBuilderElderberryValidium(*ethman.Contracts.Elderberry.ZkEVM, da, *auth, auth.From) + } else { + txBuilder = txbuilder.NewTxBuilderElderberryZKEVM(*ethman.Contracts.Elderberry.ZkEVM, *auth, auth.From) + } + default: + err = fmt.Errorf("unknown contract version: %s", cfg.Common.ContractVersions) + } + + return txBuilder, err +} + func newDataAvailability(c config.Config, etherman *etherman.Client) (*dataavailability.DataAvailability, error) { if !c.SequenceSender.IsValidiumMode { return nil, nil @@ -212,7 +245,7 @@ func newEtherman(c config.Config) (*etherman.Client, error) { config := etherman.Config{ URL: c.Aggregator.EthTxManager.Etherman.URL, } - return etherman.NewClient(config, c.NetworkConfig.L1Config) + return etherman.NewClient(config, c.NetworkConfig.L1Config, c.Common) } func logVersion() { diff --git a/common/config.go b/common/config.go new file mode 100644 index 00000000..0fdf14d4 --- /dev/null +++ b/common/config.go @@ -0,0 +1,8 @@ +package common + +type Config struct { + // IsValidiumMode has the value true if the sequence sender is running in validium mode. + IsValidiumMode bool `mapstructure:"IsValidiumMode"` + // Contract Versions: elderberry, banana + ContractVersions string `mapstructure:"ContractVersions"` +} diff --git a/config/config.go b/config/config.go index c38da5e6..e18dff10 100644 --- a/config/config.go +++ b/config/config.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/0xPolygon/cdk/aggregator" + "github.com/0xPolygon/cdk/common" "github.com/0xPolygon/cdk/etherman" "github.com/0xPolygon/cdk/log" "github.com/0xPolygon/cdk/sequencesender" @@ -66,6 +67,9 @@ type Config struct { NetworkConfig NetworkConfig // Configuration of the sequence sender service SequenceSender sequencesender.Config + + // Common Config that affects all the services + Common common.Config } // Default parses the default configuration values. diff --git a/config/default.go b/config/default.go index f8dce471..e8ee755e 100644 --- a/config/default.go +++ b/config/default.go @@ -5,6 +5,10 @@ const DefaultValues = ` ForkUpgradeBatchNumber = 0 ForkUpgradeNewForkId = 0 +[Common] +IsValidiumMode = false +ContractVersions = "banana" + [Log] Environment = "development" # "production" or "development" Level = "info" diff --git a/etherman/aggregator.go b/etherman/aggregator.go index a85f4f8b..bf0f326f 100644 --- a/etherman/aggregator.go +++ b/etherman/aggregator.go @@ -41,7 +41,7 @@ func (etherMan *Client) BuildTrustedVerifyBatchesTxData(lastVerifiedBatch, newVe const pendStateNum = 0 // TODO hardcoded for now until we implement the pending state feature - tx, err := etherMan.RollupManager.VerifyBatchesTrustedAggregator( + tx, err := etherMan.Contracts.RollupManager(nil).VerifyBatchesTrustedAggregator( &opts, etherMan.RollupID, pendStateNum, @@ -53,7 +53,7 @@ func (etherMan *Client) BuildTrustedVerifyBatchesTxData(lastVerifiedBatch, newVe proof, ) if err != nil { - if parsedErr, ok := tryParseError(err); ok { + if parsedErr, ok := TryParseError(err); ok { err = parsedErr } return nil, nil, err @@ -64,7 +64,7 @@ func (etherMan *Client) BuildTrustedVerifyBatchesTxData(lastVerifiedBatch, newVe // GetBatchAccInputHash gets the batch accumulated input hash from the ethereum func (etherman *Client) GetBatchAccInputHash(ctx context.Context, batchNumber uint64) (common.Hash, error) { - rollupData, err := etherman.RollupManager.GetRollupSequencedBatches(&bind.CallOpts{Pending: false}, etherman.RollupID, batchNumber) + rollupData, err := etherman.Contracts.RollupManager(nil).GetRollupSequencedBatches(etherman.RollupID, batchNumber) if err != nil { return common.Hash{}, err } diff --git a/etherman/contracts.go b/etherman/contracts.go new file mode 100644 index 00000000..54315cf5 --- /dev/null +++ b/etherman/contracts.go @@ -0,0 +1,138 @@ +package etherman + +import ( + "github.com/0xPolygon/cdk/etherman/contracts" + "github.com/0xPolygon/cdk/log" + "github.com/ethereum/go-ethereum/accounts/abi/bind" +) + +type ContractsBanana struct { + //DAProtocol *ContractDAProtocolBanana + GlobalExitRoot *contracts.ContractGlobalExitRootBanana + RollupManager *contracts.ContractRollupManangerBanana + ZkEVM *contracts.ContractRollupBanana +} + +func newContractsBanana(cfg L1Config, backend bind.ContractBackend) (*ContractsBanana, error) { + + globalExitRoot, err := contracts.NewContractGlobalExitRootBanana(cfg.GlobalExitRootManagerAddr, backend) + if err != nil { + return nil, err + } + + rollupManager, err := contracts.NewContractRollupManangerBanana(cfg.RollupManagerAddr, backend) + if err != nil { + return nil, err + } + + zkEVM, err := contracts.NewContractRollupBanana(cfg.ZkEVMAddr, backend) + if err != nil { + return nil, err + } + + return &ContractsBanana{ + GlobalExitRoot: globalExitRoot, + RollupManager: rollupManager, + ZkEVM: zkEVM, + }, nil +} + +type ContractsElderberry struct { + + //DAProtocol *ContractDAProtocolBanana + RollupManager *contracts.ContractRollupManangerElderberry + GlobalExitRoot *contracts.ContractGlobalExitRootElderberry + ZkEVM *contracts.ContractRollupElderberry +} + +func newContractsElderberry(cfg L1Config, backend bind.ContractBackend) (*ContractsElderberry, error) { + + globalExitRoot, err := contracts.NewContractGlobalExitRootElderberry(cfg.GlobalExitRootManagerAddr, backend) + if err != nil { + return nil, err + } + rollupManager, err := contracts.NewContractRollupManangerElderberry(cfg.RollupManagerAddr, backend) + if err != nil { + return nil, err + } + + zkEVM, err := contracts.NewContractRollupElderberry(cfg.ZkEVMAddr, backend) + if err != nil { + return nil, err + } + + return &ContractsElderberry{ + GlobalExitRoot: globalExitRoot, + RollupManager: rollupManager, + ZkEVM: zkEVM, + }, nil +} + +type Contracts struct { + Banana ContractsBanana + Elderberry ContractsElderberry + contractVersion contracts.VersionType +} + +func NewContracts(cfg L1Config, backend bind.ContractBackend, defaultVersion contracts.VersionType) (*Contracts, error) { + banana, err := newContractsBanana(cfg, backend) + if err != nil { + return nil, err + } + + elderberry, err := newContractsElderberry(cfg, backend) + if err != nil { + return nil, err + } + + return &Contracts{ + Banana: *banana, + Elderberry: *elderberry, + contractVersion: defaultVersion, + }, nil +} + +func (c *Contracts) RollupManager(specificVersion *contracts.VersionType) contracts.RollupManagerContractor { + useVersion := c.contractVersion + if specificVersion != nil { + useVersion = *specificVersion + } + if useVersion == contracts.VersionBanana { + return c.Banana.RollupManager + } else if useVersion == contracts.VersionElderberry { + return c.Elderberry.RollupManager + } else { + log.Errorf("RollupManager unknown version %s", useVersion) + return nil + } +} + +func (c *Contracts) GlobalExitRoot(specificVersion *contracts.VersionType) contracts.GlobalExitRootContractor { + useVersion := c.contractVersion + if specificVersion != nil { + useVersion = *specificVersion + } + if useVersion == contracts.VersionBanana { + return c.Banana.GlobalExitRoot + } else if useVersion == contracts.VersionElderberry { + return c.Elderberry.GlobalExitRoot + } else { + log.Errorf("RollupManager unknown version %s", useVersion) + return nil + } +} + +func (c *Contracts) ZkEVM(specificVersion *contracts.VersionType) contracts.RollupContractor { + useVersion := c.contractVersion + if specificVersion != nil { + useVersion = *specificVersion + } + if useVersion == contracts.VersionBanana { + return c.Banana.ZkEVM + } else if useVersion == contracts.VersionElderberry { + return c.Elderberry.ZkEVM + } else { + log.Errorf("ZkEVM unknown version %s", useVersion) + return nil + } +} diff --git a/etherman/contracts/banana_daprotocol.go b/etherman/contracts/banana_daprotocol.go new file mode 100644 index 00000000..03255dfb --- /dev/null +++ b/etherman/contracts/banana_daprotocol.go @@ -0,0 +1,21 @@ +package contracts + +import ( + "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/idataavailabilityprotocol" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" +) + +type ContractDAProtocolBanana struct { + *ContractBase[idataavailabilityprotocol.Idataavailabilityprotocol] +} + +func NewContractDAProtocolBanana(address common.Address, backend bind.ContractBackend) (*ContractDAProtocolBanana, error) { + base, err := NewContractBase(idataavailabilityprotocol.NewIdataavailabilityprotocol, address, backend, ContractNameDAProtocol, VersionBanana) + if err != nil { + return nil, err + } + return &ContractDAProtocolBanana{ + ContractBase: base, + }, nil +} diff --git a/etherman/contracts/banana_globalexitroot.go b/etherman/contracts/banana_globalexitroot.go new file mode 100644 index 00000000..09eb7523 --- /dev/null +++ b/etherman/contracts/banana_globalexitroot.go @@ -0,0 +1,59 @@ +package contracts + +import ( + "fmt" + + "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonzkevmglobalexitrootv2" + "github.com/0xPolygon/cdk/log" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" +) + +type ContractGlobalExitRootBanana struct { + *ContractBase[polygonzkevmglobalexitrootv2.Polygonzkevmglobalexitrootv2] +} + +func NewContractGlobalExitRootBanana(address common.Address, backend bind.ContractBackend) (*ContractGlobalExitRootBanana, error) { + base, err := NewContractBase(polygonzkevmglobalexitrootv2.NewPolygonzkevmglobalexitrootv2, address, backend, ContractNameGlobalExitRoot, VersionBanana) + if err != nil { + return nil, err + } + return &ContractGlobalExitRootBanana{ + ContractBase: base, + }, nil +} + +func (e *ContractGlobalExitRootBanana) GetL1InfoRoot(indexL1InfoRoot uint32) (common.Hash, error) { + // Get lastL1InfoTreeRoot (if index==0 then root=0, no call is needed) + var ( + lastL1InfoTreeRoot common.Hash + err error + ) + + if indexL1InfoRoot > 0 { + lastL1InfoTreeRoot, err = e.GetContract().L1InfoRootMap(&bind.CallOpts{Pending: false}, indexL1InfoRoot) + if err != nil { + log.Errorf("error calling SC globalexitroot L1InfoLeafMap: %v", err) + } + } + + return lastL1InfoTreeRoot, err +} + +func (e *ContractGlobalExitRootBanana) L1InfoIndexToRoot(indexLeaf uint32) (common.Hash, error) { + var ( + lastL1InfoTreeRoot common.Hash + err error + ) + + if indexLeaf > 0 { + lastL1InfoTreeRoot, err = e.GetContract().L1InfoRootMap(&bind.CallOpts{Pending: false}, indexLeaf) + if err != nil { + errC := fmt.Errorf("error calling SC globalexitroot L1InfoIndexToRoot(%d) err: %w", indexLeaf, err) + log.Errorf("%v", errC) + return common.Hash{}, errC + } + } + + return lastL1InfoTreeRoot, err +} diff --git a/etherman/contracts/banana_rollup.go b/etherman/contracts/banana_rollup.go new file mode 100644 index 00000000..663426cd --- /dev/null +++ b/etherman/contracts/banana_rollup.go @@ -0,0 +1,38 @@ +package contracts + +import ( + "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonvalidiumetrog" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" +) + +type ContractRollupBanana struct { + *ContractBase[polygonvalidiumetrog.Polygonvalidiumetrog] +} + +func NewContractRollupBanana(address common.Address, backend bind.ContractBackend) (*ContractRollupBanana, error) { + base, err := NewContractBase(polygonvalidiumetrog.NewPolygonvalidiumetrog, address, backend, ContractNameRollup, VersionBanana) + if err != nil { + return nil, err + } + return &ContractRollupBanana{ + ContractBase: base, + }, nil +} + +func (e *ContractRollupBanana) TrustedSequencer() (common.Address, error) { + return e.GetContract().TrustedSequencer(&bind.CallOpts{Pending: false}) +} + +// LastAccInputHash gets the last acc input hash from the SC +func (e *ContractRollupBanana) LastAccInputHash() (common.Hash, error) { + return e.GetContract().LastAccInputHash(&bind.CallOpts{Pending: false}) +} + +func (e *ContractRollupBanana) DataAvailabilityProtocol() (common.Address, error) { + return e.GetContract().DataAvailabilityProtocol(&bind.CallOpts{Pending: false}) +} + +func (e *ContractRollupBanana) TrustedSequencerURL() (string, error) { + return e.GetContract().TrustedSequencerURL(&bind.CallOpts{Pending: false}) +} diff --git a/etherman/contracts/banana_rollupmanager.go b/etherman/contracts/banana_rollupmanager.go new file mode 100644 index 00000000..85fdaaea --- /dev/null +++ b/etherman/contracts/banana_rollupmanager.go @@ -0,0 +1,69 @@ +package contracts + +import ( + "math/big" + + "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonrollupmanager" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" +) + +type ContractRollupManangerBanana struct { + *ContractBase[polygonrollupmanager.Polygonrollupmanager] +} + +func NewContractRollupManangerBanana(address common.Address, backend bind.ContractBackend) (*ContractRollupManangerBanana, error) { + base, err := NewContractBase(polygonrollupmanager.NewPolygonrollupmanager, address, backend, ContractNameRollupManager, VersionBanana) + if err != nil { + return nil, err + } + return &ContractRollupManangerBanana{ + ContractBase: base, + }, nil +} + +func (e *ContractRollupManangerBanana) RollupAddressToID(rollupAddress common.Address) (uint32, error) { + return e.GetContract().RollupAddressToID(&bind.CallOpts{Pending: false}, rollupAddress) +} + +func (e *ContractRollupManangerBanana) RollupIDToRollupData(rollupID uint32) (*RollupData, error) { + rollupData, err := e.GetContract().RollupIDToRollupData(&bind.CallOpts{Pending: false}, rollupID) + if err != nil { + return nil, err + } + return &RollupData{ + RollupContract: rollupData.RollupContract, + ChainID: rollupData.ChainID, + Verifier: rollupData.Verifier, + ForkID: rollupData.ForkID, + LastLocalExitRoot: rollupData.LastLocalExitRoot, + LastBatchSequenced: rollupData.LastBatchSequenced, + LastVerifiedBatch: rollupData.LastVerifiedBatch, + LastPendingState: rollupData.LastPendingState, + LastPendingStateConsolidated: rollupData.LastPendingStateConsolidated, + LastVerifiedBatchBeforeUpgrade: rollupData.LastVerifiedBatchBeforeUpgrade, + RollupTypeID: rollupData.RollupTypeID, + RollupCompatibilityID: rollupData.RollupCompatibilityID, + }, nil +} + +func (e *ContractRollupManangerBanana) GetBatchFee() (*big.Int, error) { + return e.GetContract().GetBatchFee(&bind.CallOpts{Pending: false}) +} + +func (e *ContractRollupManangerBanana) VerifyBatchesTrustedAggregator(opts *bind.TransactOpts, rollupID uint32, pendingStateNum uint64, initNumBatch uint64, finalNewBatch uint64, newLocalExitRoot [32]byte, newStateRoot [32]byte, beneficiary common.Address, proof [24][32]byte) (*types.Transaction, error) { + return e.GetContract().VerifyBatchesTrustedAggregator(opts, rollupID, pendingStateNum, initNumBatch, finalNewBatch, newLocalExitRoot, newStateRoot, beneficiary, proof) +} + +func (e *ContractRollupManangerBanana) GetRollupSequencedBatches(rollupID uint32, batchNum uint64) (StateVariablesSequencedBatchData, error) { + res, err := e.GetContract().GetRollupSequencedBatches(&bind.CallOpts{Pending: false}, rollupID, batchNum) + if err != nil { + return StateVariablesSequencedBatchData{}, err + } + return StateVariablesSequencedBatchData{ + AccInputHash: res.AccInputHash, + SequencedTimestamp: res.SequencedTimestamp, + PreviousLastBatchSequenced: res.PreviousLastBatchSequenced, + }, nil +} diff --git a/etherman/contracts/base.go b/etherman/contracts/base.go new file mode 100644 index 00000000..666bf5ff --- /dev/null +++ b/etherman/contracts/base.go @@ -0,0 +1,50 @@ +package contracts + +import ( + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" +) + +type ContractBase[T any] struct { + contractBind *T + address common.Address + contractName NameType + version VersionType +} + +type contractConstructorFunc[T any] func(address common.Address, backend bind.ContractBackend) (*T, error) + +func NewContractBase[T any](constructor contractConstructorFunc[T], address common.Address, backend bind.ContractBackend, + name NameType, version VersionType) (*ContractBase[T], error) { + contractBind, err := constructor(address, backend) + if err != nil { + return nil, err + } + + return &ContractBase[T]{ + contractBind: contractBind, + address: address, + contractName: name, + version: version, + }, nil +} + +func (e *ContractBase[T]) GetContract() *T { + return e.contractBind +} + +func (e *ContractBase[T]) GetAddress() common.Address { + return e.address +} + +func (e *ContractBase[T]) GetName() string { + return string(e.contractName) +} + +func (e *ContractBase[T]) GetVersion() string { + return string(e.version) +} + +func (e *ContractBase[T]) String() string { + return e.GetVersion() + "/" + e.GetName() + "@" + e.address.String() +} diff --git a/etherman/contracts/elderberry_daprotocol.go b/etherman/contracts/elderberry_daprotocol.go new file mode 100644 index 00000000..94b2fa36 --- /dev/null +++ b/etherman/contracts/elderberry_daprotocol.go @@ -0,0 +1,21 @@ +package contracts + +import ( + "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry/idataavailabilityprotocol" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" +) + +type ContractDAProtocolElderberry struct { + *ContractBase[idataavailabilityprotocol.Idataavailabilityprotocol] +} + +func NewContractDAProtocolElderberry(address common.Address, backend bind.ContractBackend) (*ContractDAProtocolElderberry, error) { + base, err := NewContractBase(idataavailabilityprotocol.NewIdataavailabilityprotocol, address, backend, ContractNameDAProtocol, VersionElderberry) + if err != nil { + return nil, err + } + return &ContractDAProtocolElderberry{ + ContractBase: base, + }, nil +} diff --git a/etherman/contracts/elderberry_globalexitroot.go b/etherman/contracts/elderberry_globalexitroot.go new file mode 100644 index 00000000..1dc21c0a --- /dev/null +++ b/etherman/contracts/elderberry_globalexitroot.go @@ -0,0 +1,30 @@ +package contracts + +import ( + "fmt" + + "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry/polygonzkevmglobalexitrootv2" + "github.com/0xPolygon/cdk/log" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" +) + +type ContractGlobalExitRootElderberry struct { + *ContractBase[polygonzkevmglobalexitrootv2.Polygonzkevmglobalexitrootv2] +} + +func NewContractGlobalExitRootElderberry(address common.Address, backend bind.ContractBackend) (*ContractGlobalExitRootElderberry, error) { + base, err := NewContractBase(polygonzkevmglobalexitrootv2.NewPolygonzkevmglobalexitrootv2, address, backend, ContractNameGlobalExitRoot, VersionElderberry) + if err != nil { + return nil, err + } + return &ContractGlobalExitRootElderberry{ + ContractBase: base, + }, nil +} + +func (e *ContractGlobalExitRootElderberry) L1InfoIndexToRoot(indexLeaf uint32) (common.Hash, error) { + errC := fmt.Errorf("Contract %s doesn't implement L1InfoIndexToRoot. Err:%w", e.String(), ErrNotImplemented) + log.Errorf("%v", errC) + return common.Hash{}, errC +} diff --git a/etherman/contracts/elderberry_rollup.go b/etherman/contracts/elderberry_rollup.go new file mode 100644 index 00000000..b071aac7 --- /dev/null +++ b/etherman/contracts/elderberry_rollup.go @@ -0,0 +1,38 @@ +package contracts + +import ( + "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry/polygonvalidiumetrog" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" +) + +type ContractRollupElderberry struct { + *ContractBase[polygonvalidiumetrog.Polygonvalidiumetrog] +} + +func NewContractRollupElderberry(address common.Address, backend bind.ContractBackend) (*ContractRollupElderberry, error) { + base, err := NewContractBase(polygonvalidiumetrog.NewPolygonvalidiumetrog, address, backend, ContractNameRollup, VersionElderberry) + if err != nil { + return nil, err + } + return &ContractRollupElderberry{ + ContractBase: base, + }, nil +} + +func (e *ContractRollupElderberry) TrustedSequencer() (common.Address, error) { + return e.GetContract().TrustedSequencer(&bind.CallOpts{Pending: false}) +} + +// LastAccInputHash gets the last acc input hash from the SC +func (e *ContractRollupElderberry) LastAccInputHash() (common.Hash, error) { + return e.GetContract().LastAccInputHash(&bind.CallOpts{Pending: false}) +} + +func (e *ContractRollupElderberry) DataAvailabilityProtocol() (common.Address, error) { + return e.GetContract().DataAvailabilityProtocol(&bind.CallOpts{Pending: false}) +} + +func (e *ContractRollupElderberry) TrustedSequencerURL() (string, error) { + return e.GetContract().TrustedSequencerURL(&bind.CallOpts{Pending: false}) +} diff --git a/etherman/contracts/elderberry_rollupmanager.go b/etherman/contracts/elderberry_rollupmanager.go new file mode 100644 index 00000000..c50f8a75 --- /dev/null +++ b/etherman/contracts/elderberry_rollupmanager.go @@ -0,0 +1,69 @@ +package contracts + +import ( + "math/big" + + "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry/polygonrollupmanager" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" +) + +type ContractRollupManangerElderberry struct { + *ContractBase[polygonrollupmanager.Polygonrollupmanager] +} + +func NewContractRollupManangerElderberry(address common.Address, backend bind.ContractBackend) (*ContractRollupManangerElderberry, error) { + base, err := NewContractBase(polygonrollupmanager.NewPolygonrollupmanager, address, backend, ContractNameRollupManager, VersionElderberry) + if err != nil { + return nil, err + } + return &ContractRollupManangerElderberry{ + ContractBase: base, + }, nil +} + +func (e *ContractRollupManangerElderberry) RollupAddressToID(rollupAddress common.Address) (uint32, error) { + return e.GetContract().RollupAddressToID(&bind.CallOpts{Pending: false}, rollupAddress) +} + +func (e *ContractRollupManangerElderberry) RollupIDToRollupData(rollupID uint32) (*RollupData, error) { + rollupData, err := e.GetContract().RollupIDToRollupData(&bind.CallOpts{Pending: false}, rollupID) + if err != nil { + return nil, err + } + return &RollupData{ + RollupContract: rollupData.RollupContract, + ChainID: rollupData.ChainID, + Verifier: rollupData.Verifier, + ForkID: rollupData.ForkID, + LastLocalExitRoot: rollupData.LastLocalExitRoot, + LastBatchSequenced: rollupData.LastBatchSequenced, + LastVerifiedBatch: rollupData.LastVerifiedBatch, + LastPendingState: rollupData.LastPendingState, + LastPendingStateConsolidated: rollupData.LastPendingStateConsolidated, + LastVerifiedBatchBeforeUpgrade: rollupData.LastVerifiedBatchBeforeUpgrade, + RollupTypeID: rollupData.RollupTypeID, + RollupCompatibilityID: rollupData.RollupCompatibilityID, + }, nil +} + +func (e *ContractRollupManangerElderberry) GetBatchFee() (*big.Int, error) { + return e.GetContract().GetBatchFee(&bind.CallOpts{Pending: false}) +} + +func (e *ContractRollupManangerElderberry) VerifyBatchesTrustedAggregator(opts *bind.TransactOpts, rollupID uint32, pendingStateNum uint64, initNumBatch uint64, finalNewBatch uint64, newLocalExitRoot [32]byte, newStateRoot [32]byte, beneficiary common.Address, proof [24][32]byte) (*types.Transaction, error) { + return e.GetContract().VerifyBatchesTrustedAggregator(opts, rollupID, pendingStateNum, initNumBatch, finalNewBatch, newLocalExitRoot, newStateRoot, beneficiary, proof) +} + +func (e *ContractRollupManangerElderberry) GetRollupSequencedBatches(rollupID uint32, batchNum uint64) (StateVariablesSequencedBatchData, error) { + res, err := e.GetContract().GetRollupSequencedBatches(&bind.CallOpts{Pending: false}, rollupID, batchNum) + if err != nil { + return StateVariablesSequencedBatchData{}, err + } + return StateVariablesSequencedBatchData{ + AccInputHash: res.AccInputHash, + SequencedTimestamp: res.SequencedTimestamp, + PreviousLastBatchSequenced: res.PreviousLastBatchSequenced, + }, nil +} diff --git a/etherman/contracts/interfaces.go b/etherman/contracts/interfaces.go new file mode 100644 index 00000000..a07f1b12 --- /dev/null +++ b/etherman/contracts/interfaces.go @@ -0,0 +1,79 @@ +package contracts + +import ( + "errors" + "math/big" + + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" +) + +type NameType string + +const ( + ContractNameDAProtocol NameType = "daprotocol" + ContractNameRollupManager NameType = "rollupmanager" + ContractNameGlobalExitRoot NameType = "globalexitroot" + ContractNameRollup NameType = "rollup" +) + +var ( + ErrNotImplemented = errors.New("not implemented") +) + +type VersionType string + +const ( + VersionBanana VersionType = "banana" + VersionElderberry VersionType = "elderberry" +) + +type RollupContractor interface { + TrustedSequencer() (common.Address, error) + TrustedSequencerURL() (string, error) + LastAccInputHash() (common.Hash, error) + DataAvailabilityProtocol() (common.Address, error) +} + +type GlobalExitRootContractor interface { + // L1InfoIndexToRoot: get the root for a specific leaf index + L1InfoIndexToRoot(indexLeaf uint32) (common.Hash, error) +} + +type BaseContractor interface { + GetName() string + GetVersion() string + GetAddress() common.Address + String() string +} + +type RollupData struct { + RollupContract common.Address + ChainID uint64 + Verifier common.Address + ForkID uint64 + LastLocalExitRoot [32]byte + LastBatchSequenced uint64 + LastVerifiedBatch uint64 + LastPendingState uint64 + LastPendingStateConsolidated uint64 + LastVerifiedBatchBeforeUpgrade uint64 + RollupTypeID uint64 + RollupCompatibilityID uint8 +} + +type StateVariablesSequencedBatchData struct { + AccInputHash [32]byte + SequencedTimestamp uint64 + PreviousLastBatchSequenced uint64 +} + +type RollupManagerContractor interface { + BaseContractor + RollupAddressToID(rollupAddress common.Address) (uint32, error) + GetBatchFee() (*big.Int, error) + RollupIDToRollupData(rollupID uint32) (*RollupData, error) + VerifyBatchesTrustedAggregator(opts *bind.TransactOpts, rollupID uint32, pendingStateNum uint64, initNumBatch uint64, finalNewBatch uint64, newLocalExitRoot [32]byte, newStateRoot [32]byte, beneficiary common.Address, proof [24][32]byte) (*types.Transaction, error) + GetRollupSequencedBatches(rollupID uint32, batchNum uint64) (StateVariablesSequencedBatchData, error) +} diff --git a/etherman/contracts/router_rollup.go b/etherman/contracts/router_rollup.go new file mode 100644 index 00000000..5f3be656 --- /dev/null +++ b/etherman/contracts/router_rollup.go @@ -0,0 +1,6 @@ +package contracts + +type ContractRollupRouter struct { + banana *ContractGlobalExitRootBanana + elderberry *ContractGlobalExitRootElderberry +} diff --git a/etherman/errors.go b/etherman/errors.go index 38a4e47d..bb7123fb 100644 --- a/etherman/errors.go +++ b/etherman/errors.go @@ -39,7 +39,7 @@ var ( } ) -func tryParseError(err error) (error, bool) { +func TryParseError(err error) (error, bool) { parsedError, exists := errorsCache[err.Error()] if !exists { for errStr, actualErr := range errorsCache { diff --git a/etherman/errors_test.go b/etherman/errors_test.go index 186768e6..cfc02ccc 100644 --- a/etherman/errors_test.go +++ b/etherman/errors_test.go @@ -11,7 +11,7 @@ func TestTryParseWithExactMatch(t *testing.T) { expected := ErrTimestampMustBeInsideRange smartContractErr := expected - actualErr, ok := tryParseError(smartContractErr) + actualErr, ok := TryParseError(smartContractErr) assert.ErrorIs(t, actualErr, expected) assert.True(t, ok) @@ -21,7 +21,7 @@ func TestTryParseWithContains(t *testing.T) { expected := ErrTimestampMustBeInsideRange smartContractErr := fmt.Errorf(" execution reverted: ProofOfEfficiency::sequenceBatches: %s", expected) - actualErr, ok := tryParseError(smartContractErr) + actualErr, ok := TryParseError(smartContractErr) assert.ErrorIs(t, actualErr, expected) assert.True(t, ok) @@ -30,7 +30,7 @@ func TestTryParseWithContains(t *testing.T) { func TestTryParseWithNonExistingErr(t *testing.T) { smartContractErr := fmt.Errorf("some non-existing err") - actualErr, ok := tryParseError(smartContractErr) + actualErr, ok := TryParseError(smartContractErr) assert.Nil(t, actualErr) assert.False(t, ok) diff --git a/etherman/etherman.go b/etherman/etherman.go index 000f08ed..72ab56d7 100644 --- a/etherman/etherman.go +++ b/etherman/etherman.go @@ -11,16 +11,15 @@ import ( "time" "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/idataavailabilityprotocol" - "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonrollupmanager" - "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonvalidiumetrog" - "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonzkevmglobalexitrootv2" + "github.com/0xPolygon/cdk-contracts-tooling/contracts/etrog/polygonzkevmglobalexitrootv2" + cdkcommon "github.com/0xPolygon/cdk/common" + "github.com/0xPolygon/cdk/etherman/contracts" "github.com/0xPolygon/cdk/log" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/rpc" ) @@ -78,14 +77,11 @@ type L1Config struct { // Client is a simple implementation of EtherMan. type Client struct { EthClient ethereumClient - ZkEVM *polygonvalidiumetrog.Polygonvalidiumetrog - RollupManager *polygonrollupmanager.Polygonrollupmanager DAProtocol *idataavailabilityprotocol.Idataavailabilityprotocol GlobalExitRoot *polygonzkevmglobalexitrootv2.Polygonzkevmglobalexitrootv2 - // Pol *pol.Pol - SCAddresses []common.Address - RollupID uint32 + Contracts *Contracts + RollupID uint32 l1Cfg L1Config cfg Config @@ -93,7 +89,7 @@ type Client struct { } // NewClient creates a new etherman. -func NewClient(cfg Config, l1Config L1Config) (*Client, error) { +func NewClient(cfg Config, l1Config L1Config, commonConfig cdkcommon.Config) (*Client, error) { // Connect to ethereum node ethClient, err := ethclient.Dial(cfg.EthermanConfig.URL) if err != nil { @@ -101,46 +97,36 @@ func NewClient(cfg Config, l1Config L1Config) (*Client, error) { return nil, err } - // Create smc clients - zkevm, err := polygonvalidiumetrog.NewPolygonvalidiumetrog(l1Config.ZkEVMAddr, ethClient) - if err != nil { - return nil, err - } - - rollupManager, err := polygonrollupmanager.NewPolygonrollupmanager(l1Config.RollupManagerAddr, ethClient) - if err != nil { - return nil, err - } - globalExitRoot, err := polygonzkevmglobalexitrootv2.NewPolygonzkevmglobalexitrootv2(l1Config.GlobalExitRootManagerAddr, ethClient) if err != nil { return nil, err } - var scAddresses []common.Address - scAddresses = append(scAddresses, l1Config.ZkEVMAddr, l1Config.RollupManagerAddr, l1Config.GlobalExitRootManagerAddr) + contracts, err := NewContracts(l1Config, ethClient, contracts.VersionType(commonConfig.ContractVersions)) // Get RollupID - rollupID, err := rollupManager.RollupAddressToID(&bind.CallOpts{Pending: false}, l1Config.ZkEVMAddr) + rollupID, err := contracts.RollupManager(nil).RollupAddressToID(l1Config.ZkEVMAddr) if err != nil { return nil, err } + if rollupID == 0 { + return nil, errors.New("rollupID is 0, is not a valid value. Check that rollup Address is correct " + l1Config.ZkEVMAddr.String()) + } log.Debug("rollupID: ", rollupID) client := &Client{ EthClient: ethClient, - ZkEVM: zkevm, - RollupManager: rollupManager, + Contracts: contracts, GlobalExitRoot: globalExitRoot, - SCAddresses: scAddresses, RollupID: rollupID, l1Cfg: l1Config, cfg: cfg, auth: map[common.Address]bind.TransactOpts{}, } + cfg.IsValidiumMode = commonConfig.IsValidiumMode - if cfg.IsValidiumMode { - dapAddr, err := zkevm.DataAvailabilityProtocol(&bind.CallOpts{Pending: false}) + if commonConfig.IsValidiumMode { + dapAddr, err := contracts.ZkEVM(nil).DataAvailabilityProtocol() if err != nil { return nil, err } @@ -172,145 +158,9 @@ func (etherMan *Client) WaitTxToBeMined(ctx context.Context, tx *types.Transacti return true, nil } -// BuildSequenceBatchesTx builds a tx to be sent to the PoE SC method SequenceBatches. -func (etherMan *Client) BuildSequenceBatchesTx(sender common.Address, sequence SequenceBanana, - dataAvailabilityMessage []byte) (*types.Transaction, error) { - opts, err := etherMan.getAuthByAddress(sender) - if err == ErrNotFound { - return nil, fmt.Errorf("failed to build sequence batches, err: %w", ErrPrivateKeyNotFound) - } - - opts.NoSend = true - - // force nonce, gas limit and gas price to avoid querying it from the chain - opts.Nonce = big.NewInt(1) - opts.GasLimit = uint64(1) - opts.GasPrice = big.NewInt(1) - - return etherMan.sequenceBatches(opts, sequence, dataAvailabilityMessage) -} - -func (etherMan *Client) sequenceBatches(opts bind.TransactOpts, sequence SequenceBanana, dataAvailabilityMessage []byte) (tx *types.Transaction, err error) { - if etherMan.cfg.IsValidiumMode { - return etherMan.sequenceBatchesValidium(opts, sequence, dataAvailabilityMessage) - } - - return etherMan.sequenceBatchesRollup(opts, sequence) -} - -func (etherMan *Client) sequenceBatchesRollup(opts bind.TransactOpts, sequence SequenceBanana) (*types.Transaction, error) { - batches := make([]polygonvalidiumetrog.PolygonRollupBaseEtrogBatchData, len(sequence.Batches)) - for i, batch := range sequence.Batches { - var ger common.Hash - if batch.ForcedBatchTimestamp > 0 { - ger = batch.ForcedGlobalExitRoot - } - - batches[i] = polygonvalidiumetrog.PolygonRollupBaseEtrogBatchData{ - Transactions: batch.L2Data, - ForcedGlobalExitRoot: ger, - ForcedTimestamp: batch.ForcedBatchTimestamp, - ForcedBlockHashL1: batch.ForcedBlockHashL1, - } - } - - tx, err := etherMan.ZkEVM.SequenceBatches(&opts, batches, sequence.IndexL1InfoRoot, sequence.MaxSequenceTimestamp, sequence.AccInputHash, sequence.L2Coinbase) - if err != nil { - log.Debugf("Batches to send: %+v", batches) - log.Debug("l2CoinBase: ", sequence.L2Coinbase) - log.Debug("Sequencer address: ", opts.From) - a, err2 := polygonvalidiumetrog.PolygonvalidiumetrogMetaData.GetAbi() - if err2 != nil { - log.Error("error getting abi. Error: ", err2) - } - input, err3 := a.Pack("sequenceBatches", batches, sequence.IndexL1InfoRoot, sequence.MaxSequenceTimestamp, sequence.AccInputHash, sequence.L2Coinbase) - if err3 != nil { - log.Error("error packing call. Error: ", err3) - } - ctx := context.Background() - var b string - block, err4 := etherMan.EthClient.BlockByNumber(ctx, nil) - if err4 != nil { - log.Error("error getting blockNumber. Error: ", err4) - b = "latest" - } else { - b = fmt.Sprintf("%x", block.Number()) - } - log.Warnf(`Use the next command to debug it manually. - curl --location --request POST 'http://localhost:8545' \ - --header 'Content-Type: application/json' \ - --data-raw '{ - "jsonrpc": "2.0", - "method": "eth_call", - "params": [{"from": "%s","to":"%s","data":"0x%s"},"0x%s"], - "id": 1 - }'`, opts.From, etherMan.l1Cfg.ZkEVMAddr, common.Bytes2Hex(input), b) - if parsedErr, ok := tryParseError(err); ok { - err = parsedErr - } - } - - return tx, err -} - -func (etherMan *Client) sequenceBatchesValidium(opts bind.TransactOpts, sequence SequenceBanana, dataAvailabilityMessage []byte) (*types.Transaction, error) { - batches := make([]polygonvalidiumetrog.PolygonValidiumEtrogValidiumBatchData, len(sequence.Batches)) - for i, batch := range sequence.Batches { - var ger common.Hash - if batch.ForcedBatchTimestamp > 0 { - ger = batch.ForcedGlobalExitRoot - } - - batches[i] = polygonvalidiumetrog.PolygonValidiumEtrogValidiumBatchData{ - TransactionsHash: crypto.Keccak256Hash(batch.L2Data), - ForcedGlobalExitRoot: ger, - ForcedTimestamp: batch.ForcedBatchTimestamp, - ForcedBlockHashL1: batch.ForcedBlockHashL1, - } - } - - tx, err := etherMan.ZkEVM.SequenceBatchesValidium(&opts, batches, sequence.IndexL1InfoRoot, sequence.MaxSequenceTimestamp, sequence.AccInputHash, sequence.L2Coinbase, dataAvailabilityMessage) - if err != nil { - log.Debugf("Batches to send: %+v", batches) - log.Debug("l2CoinBase: ", sequence.L2Coinbase) - log.Debug("Sequencer address: ", opts.From) - a, err2 := polygonvalidiumetrog.PolygonvalidiumetrogMetaData.GetAbi() - if err2 != nil { - log.Error("error getting abi. Error: ", err2) - } - input, err3 := a.Pack("sequenceBatchesValidium", batches, sequence.IndexL1InfoRoot, sequence.MaxSequenceTimestamp, sequence.AccInputHash, sequence.L2Coinbase, dataAvailabilityMessage) - if err3 != nil { - log.Error("error packing call. Error: ", err3) - } - ctx := context.Background() - var b string - block, err4 := etherMan.EthClient.BlockByNumber(ctx, nil) - if err4 != nil { - log.Error("error getting blockNumber. Error: ", err4) - b = "latest" - } else { - b = fmt.Sprintf("%x", block.Number()) - } - log.Warnf(`Use the next command to debug it manually. - curl --location --request POST 'http://localhost:8545' \ - --header 'Content-Type: application/json' \ - --data-raw '{ - "jsonrpc": "2.0", - "method": "eth_call", - "params": [{"from": "%s","to":"%s","data":"0x%s"},"0x%s"], - "id": 1 - }'`, opts.From, etherMan.l1Cfg.ZkEVMAddr, common.Bytes2Hex(input), b) - if parsedErr, ok := tryParseError(err); ok { - err = parsedErr - } - } - - return tx, err -} - // GetSendSequenceFee get super/trusted sequencer fee func (etherMan *Client) GetSendSequenceFee(numBatches uint64) (*big.Int, error) { - f, err := etherMan.RollupManager.GetBatchFee(&bind.CallOpts{Pending: false}) + f, err := etherMan.Contracts.RollupManager(nil).GetBatchFee() if err != nil { return nil, err } @@ -320,7 +170,7 @@ func (etherMan *Client) GetSendSequenceFee(numBatches uint64) (*big.Int, error) // TrustedSequencer gets trusted sequencer address func (etherMan *Client) TrustedSequencer() (common.Address, error) { - return etherMan.ZkEVM.TrustedSequencer(&bind.CallOpts{Pending: false}) + return etherMan.Contracts.ZkEVM(nil).TrustedSequencer() } // HeaderByNumber returns a block header from the current canonical chain. If number is @@ -343,7 +193,7 @@ func (etherMan *Client) EthBlockByNumber(ctx context.Context, blockNumber uint64 // GetLatestBatchNumber function allows to retrieve the latest proposed batch in the smc func (etherMan *Client) GetLatestBatchNumber() (uint64, error) { - rollupData, err := etherMan.RollupManager.RollupIDToRollupData(&bind.CallOpts{Pending: false}, etherMan.RollupID) + rollupData, err := etherMan.Contracts.RollupManager(nil).RollupIDToRollupData(etherMan.RollupID) if err != nil { return 0, err } @@ -385,7 +235,7 @@ func (etherMan *Client) GetLatestBlockTimestamp(ctx context.Context) (uint64, er // GetLatestVerifiedBatchNum gets latest verified batch from ethereum func (etherMan *Client) GetLatestVerifiedBatchNum() (uint64, error) { - rollupData, err := etherMan.RollupManager.RollupIDToRollupData(&bind.CallOpts{Pending: false}, etherMan.RollupID) + rollupData, err := etherMan.Contracts.RollupManager(nil).RollupIDToRollupData(etherMan.RollupID) if err != nil { return 0, err } @@ -404,12 +254,12 @@ func (etherMan *Client) GetTxReceipt(ctx context.Context, txHash common.Hash) (* // GetTrustedSequencerURL Gets the trusted sequencer url from rollup smc func (etherMan *Client) GetTrustedSequencerURL() (string, error) { - return etherMan.ZkEVM.TrustedSequencerURL(&bind.CallOpts{Pending: false}) + return etherMan.Contracts.ZkEVM(nil).TrustedSequencerURL() } // GetL2ChainID returns L2 Chain ID func (etherMan *Client) GetL2ChainID() (uint64, error) { - rollupData, err := etherMan.RollupManager.RollupIDToRollupData(&bind.CallOpts{Pending: false}, etherMan.RollupID) + rollupData, err := etherMan.Contracts.RollupManager(nil).RollupIDToRollupData(etherMan.RollupID) log.Debug("chainID read from rollupManager: ", rollupData.ChainID) if err != nil { log.Debug("error from rollupManager: ", err) @@ -559,7 +409,7 @@ func (etherMan *Client) GetLatestBlockHeader(ctx context.Context) (*types.Header // GetDAProtocolAddr returns the address of the data availability protocol func (etherMan *Client) GetDAProtocolAddr() (common.Address, error) { - return etherMan.ZkEVM.DataAvailabilityProtocol(&bind.CallOpts{Pending: false}) + return etherMan.Contracts.ZkEVM(nil).DataAvailabilityProtocol() } // GetDAProtocolName returns the name of the data availability protocol @@ -569,23 +419,10 @@ func (etherMan *Client) GetDAProtocolName() (string, error) { // LastAccInputHash gets the last acc input hash from the SC func (etherMan *Client) LastAccInputHash() (common.Hash, error) { - return etherMan.ZkEVM.LastAccInputHash(&bind.CallOpts{Pending: false}) + return etherMan.Contracts.ZkEVM(nil).LastAccInputHash() } // GetL1InfoRoot gets the L1 info root from the SC func (etherMan *Client) GetL1InfoRoot(indexL1InfoRoot uint32) (common.Hash, error) { - // Get lastL1InfoTreeRoot (if index==0 then root=0, no call is needed) - var ( - lastL1InfoTreeRoot common.Hash - err error - ) - - if indexL1InfoRoot > 0 { - lastL1InfoTreeRoot, err = etherMan.GlobalExitRoot.L1InfoRootMap(&bind.CallOpts{Pending: false}, indexL1InfoRoot) - if err != nil { - log.Errorf("error calling SC globalexitroot L1InfoLeafMap: %v", err) - } - } - - return lastL1InfoTreeRoot, err + return etherMan.Contracts.GlobalExitRoot(nil).L1InfoIndexToRoot(indexL1InfoRoot) } diff --git a/go.sum b/go.sum index 1b18704e..d74af758 100644 --- a/go.sum +++ b/go.sum @@ -39,8 +39,6 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/0xPolygon/cdk-contracts-tooling v0.0.0-20240627125553-80db9d8a41b5 h1:bezOoVvpQvGvf4aCZQpMPFJGKXhhvktpApr+TsD8rdk= github.com/0xPolygon/cdk-contracts-tooling v0.0.0-20240627125553-80db9d8a41b5/go.mod h1:mFlcEjsm2YBBsu8atHJ3zyVnwM+Z/fMXpVmIJge+WVU= -github.com/0xPolygon/cdk-data-availability v0.0.7 h1:i5v2I8uEgHSZ5BjnJs3Dsy1XpKdSvfZbON9D16gSCUg= -github.com/0xPolygon/cdk-data-availability v0.0.7/go.mod h1:qF+xt2gYwBab8XPh3fr6pnNUMEJq/32rmJN0D630hmY= github.com/0xPolygon/cdk-data-availability v0.0.8-0.20240620143831-55a2ce665c77 h1:ijHKNgkzyaepqknBEVGJy9GADTUimoR/wagvEP134l8= github.com/0xPolygon/cdk-data-availability v0.0.8-0.20240620143831-55a2ce665c77/go.mod h1:3XkZ0zn0GsvAT01MPQMmukF534CVSFmtrcoK3F/BK6Q= github.com/0xPolygon/cdk-data-availability v0.0.8-0.20240712072318-72ae67613cbf h1:VWxVYeDhDURGp8pHR4kq3jwoXRazVHsQLMMIqYsO+Fw= diff --git a/sequencesender/seqsendertypes/batch.go b/sequencesender/seqsendertypes/batch.go new file mode 100644 index 00000000..5d998b1c --- /dev/null +++ b/sequencesender/seqsendertypes/batch.go @@ -0,0 +1,45 @@ +package seqsendertypes + +import ( + "github.com/ethereum/go-ethereum/common" +) + +type Batch interface { + //underlyingType *ethmantypes.Batch + DeepCopy() Batch + LastCoinbase() common.Address + ForcedBatchTimestamp() uint64 + ForcedGlobalExitRoot() common.Hash + ForcedBlockHashL1() common.Hash + L2Data() []byte + LastL2BLockTimestamp() uint64 + BatchNumber() uint64 + GlobalExitRoot() common.Hash + L1InfoTreeIndex() uint32 + + String() string + + // WRITE + SetL2Data(data []byte) + SetLastCoinbase(address common.Address) + SetLastL2BLockTimestamp(ts uint64) + SetL1InfoTreeIndex(index uint32) +} + +type Sequence interface { + IndexL1InfoRoot() uint32 + MaxSequenceTimestamp() uint64 + L1InfoRoot() common.Hash + Batches() []Batch + FirstBatch() Batch + LastBatch() Batch + Len() int + + L2Coinbase() common.Address + + String() string + // WRITE + //SetL1InfoRoot(hash common.Hash) + //SetOldAccInputHash(hash common.Hash) + //SetAccInputHash(hash common.Hash) +} diff --git a/sequencesender/sequencesender.go b/sequencesender/sequencesender.go index 6c283caf..39775c6c 100644 --- a/sequencesender/sequencesender.go +++ b/sequencesender/sequencesender.go @@ -12,10 +12,10 @@ import ( "sync" "time" - cdkcommon "github.com/0xPolygon/cdk/common" - "github.com/0xPolygon/cdk/dataavailability" "github.com/0xPolygon/cdk/etherman" "github.com/0xPolygon/cdk/log" + "github.com/0xPolygon/cdk/sequencesender/seqsendertypes" + "github.com/0xPolygon/cdk/sequencesender/txbuilder" "github.com/0xPolygon/cdk/state" "github.com/0xPolygon/cdk/state/datastream" "github.com/0xPolygonHermez/zkevm-data-streamer/datastreamer" @@ -52,12 +52,12 @@ type SequenceSender struct { latestStreamBatch uint64 // Latest batch received by the streaming seqSendingStopped bool // If there is a critical error streamClient *datastreamer.StreamClient - da *dataavailability.DataAvailability + TxBuilder txbuilder.TxBuilder } type sequenceData struct { batchClosed bool - batch *etherman.Batch + batch seqsendertypes.Batch batchRaw *state.BatchRawV2 } @@ -81,7 +81,7 @@ type ethTxAdditionalData struct { } // New inits sequence sender -func New(cfg Config, etherman *etherman.Client, da *dataavailability.DataAvailability) (*SequenceSender, error) { +func New(cfg Config, etherman *etherman.Client, txBuilder txbuilder.TxBuilder) (*SequenceSender, error) { // Create sequencesender s := SequenceSender{ cfg: cfg, @@ -92,8 +92,9 @@ func New(cfg Config, etherman *etherman.Client, da *dataavailability.DataAvailab validStream: false, latestStreamBatch: 0, seqSendingStopped: false, - da: da, + TxBuilder: txBuilder, } + log.Infof("Seq_sender: %s", txBuilder.String()) // Restore pending sent sequences err := s.loadSentSequencesTransactions() @@ -471,13 +472,16 @@ func (s *SequenceSender) tryToSendSequence(ctx context.Context) { } // Send sequences to L1 - sequenceCount := sequence.Len() - firstSequence := sequence.Batches[0] - lastSequence := sequence.Batches[sequenceCount-1] - lastL2BlockTimestamp := lastSequence.LastL2BLockTimestamp + //sequenceCount := sequence.Len() + //firstSequence := sequence.Batches[0] + firstSequence := sequence.FirstBatch() + //lastSequence := sequence.Batches[sequenceCount-1] + lastSequence := sequence.LastBatch() + lastL2BlockTimestamp := lastSequence.LastL2BLockTimestamp() log.Infof("[SeqSender] sending sequences to L1. From batch %d to batch %d", firstSequence.BatchNumber, lastSequence.BatchNumber) - printSequenceBatches(sequence) + //printSequenceBatches(sequence) + log.Infof(sequence.String()) // Wait until last L1 block timestamp is L1BlockTimestampMargin seconds above the timestamp of the last L2 block in the sequence timeMargin := int64(s.cfg.L1BlockTimestampMargin.Seconds()) @@ -493,7 +497,7 @@ func (s *SequenceSender) tryToSendSequence(ctx context.Context) { if !elapsed { log.Infof("[SeqSender] waiting at least %d seconds to send sequences, time difference between last L1 block %d (ts: %d) and last L2 block %d (ts: %d) in the sequence is lower than %d seconds", - waitTime, lastL1BlockHeader.Number, lastL1BlockHeader.Time, lastSequence.BatchNumber, lastL2BlockTimestamp, timeMargin) + waitTime, lastL1BlockHeader.Number, lastL1BlockHeader.Time, lastSequence.BatchNumber(), lastL2BlockTimestamp, timeMargin) time.Sleep(time.Duration(waitTime) * time.Second) } else { log.Infof("[SeqSender] continuing, time difference between last L1 block %d (ts: %d) and last L2 block %d (ts: %d) in the sequence is greater than %d seconds", @@ -522,27 +526,35 @@ func (s *SequenceSender) tryToSendSequence(ctx context.Context) { // Send sequences to L1 log.Infof("[SeqSender] sending sequences to L1. From batch %d to batch %d", firstSequence.BatchNumber, lastSequence.BatchNumber) - printSequenceBatches(sequence) + //printSequenceBatches(sequence) + log.Infof(sequence.String()) - // Post sequences to DA backend - var dataAvailabilityMessage []byte - if s.cfg.IsValidiumMode { - dataAvailabilityMessage, err = s.da.PostSequence(ctx, *sequence) + /* + // Post sequences to DA backend + var dataAvailabilityMessage []byte + if s.cfg.IsValidiumMode { + dataAvailabilityMessage, err = s.da.PostSequence(ctx, *sequence) + if err != nil { + log.Error("error posting sequences to the data availability protocol: ", err) + return + } + } + + // Build sequence data + tx, err := s.etherman.BuildSequenceBatchesTx(s.cfg.SenderAddress, *sequence, dataAvailabilityMessage) if err != nil { - log.Error("error posting sequences to the data availability protocol: ", err) + log.Errorf("[SeqSender] error estimating new sequenceBatches to add to ethtxmanager: ", err) return } - } - - // Build sequence data - tx, err := s.etherman.BuildSequenceBatchesTx(s.cfg.SenderAddress, *sequence, dataAvailabilityMessage) + */ + tx, err := s.TxBuilder.BuildSequenceBatchesTx(ctx, s.cfg.SenderAddress, sequence) if err != nil { - log.Errorf("[SeqSender] error estimating new sequenceBatches to add to ethtxmanager: ", err) + log.Errorf("[SeqSender] error building sequenceBatches tx: %v", err) return } // Add sequence tx - err = s.sendTx(ctx, false, nil, tx.To(), firstSequence.BatchNumber, lastSequence.BatchNumber, tx.Data()) + err = s.sendTx(ctx, false, nil, tx.To(), firstSequence.BatchNumber(), lastSequence.BatchNumber(), tx.Data()) if err != nil { return } @@ -625,12 +637,12 @@ func (s *SequenceSender) sendTx(ctx context.Context, resend bool, txOldHash *com } // getSequencesToSend generates sequences to be sent to L1. Empty array means there are no sequences to send or it's not worth sending -func (s *SequenceSender) getSequencesToSend() (*etherman.SequenceBanana, error) { +func (s *SequenceSender) getSequencesToSend() (seqsendertypes.Sequence, error) { // Add sequences until too big for a single L1 tx or last batch is reached s.mutexSequence.Lock() defer s.mutexSequence.Unlock() var prevCoinbase common.Address - sequenceBatches := make([]etherman.Batch, 0) + sequenceBatches := make([]seqsendertypes.Batch, 0) for i := 0; i < len(s.sequenceList); i++ { batchNumber := s.sequenceList[i] if batchNumber <= s.latestVirtualBatch || batchNumber <= s.latestSentToL1Batch { @@ -650,14 +662,15 @@ func (s *SequenceSender) getSequencesToSend() (*etherman.SequenceBanana, error) } // New potential batch to add to the sequence - batch := *s.sequenceData[batchNumber].batch + batch := s.sequenceData[batchNumber].batch.DeepCopy() // If the coinbase changes, the sequence ends here - if len(sequenceBatches) > 0 && batch.LastCoinbase != prevCoinbase { + if len(sequenceBatches) > 0 && batch.LastCoinbase() != prevCoinbase { log.Infof("[SeqSender] batch with different coinbase (batch %v, sequence %v), sequence will be sent to this point", prevCoinbase, batch.LastCoinbase) - return s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) + //return s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) + return s.TxBuilder.NewSequence(sequenceBatches, s.cfg.L2Coinbase) } - prevCoinbase = batch.LastCoinbase + prevCoinbase = batch.LastCoinbase() // Add new sequence batch sequenceBatches = append(sequenceBatches, batch) @@ -668,16 +681,21 @@ func (s *SequenceSender) getSequencesToSend() (*etherman.SequenceBanana, error) "[SeqSender] sequence should be sent to L1, because MaxBatchesForL1 (%d) has been reached", s.cfg.MaxBatchesForL1, ) - return s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) + + //return s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) + return s.TxBuilder.NewSequence(sequenceBatches, s.cfg.L2Coinbase) } } else { - sequence, err := s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) + //sequence, err := s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) + sequence, err := s.TxBuilder.NewSequence(sequenceBatches, s.cfg.L2Coinbase) if err != nil { return nil, err } // Check if can be sent - tx, err := s.etherman.BuildSequenceBatchesTx(s.cfg.SenderAddress, *sequence, nil) + // TODO: pass real CTX + ctx := context.Background() + tx, err := s.TxBuilder.BuildSequenceBatchesTx(ctx, s.cfg.SenderAddress, sequence) if err == nil && tx.Size() > s.cfg.MaxTxSizeForL1 { log.Infof("[SeqSender] oversized Data on TX oldHash %s (txSize %d > %d)", tx.Hash(), tx.Size(), s.cfg.MaxTxSizeForL1) err = ErrOversizedData @@ -685,15 +703,17 @@ func (s *SequenceSender) getSequencesToSend() (*etherman.SequenceBanana, error) if err != nil { log.Infof("[SeqSender] handling estimate gas send sequence error: %v", err) - sequenceBatches, err = s.handleEstimateGasSendSequenceErr(sequence.Batches, batchNumber, err) + sequenceBatches, err = s.handleEstimateGasSendSequenceErr(sequence.Batches(), batchNumber, err) if sequenceBatches != nil { // Handling the error gracefully, re-processing the sequence as a sanity check - sequence, err = s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) + //sequence, err = s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) + sequence, err = s.TxBuilder.NewSequence(sequenceBatches, s.cfg.L2Coinbase) if err != nil { return nil, err } - _, err = s.etherman.BuildSequenceBatchesTx(s.cfg.SenderAddress, *sequence, nil) + //_, err = s.etherman.BuildSequenceBatchesTx(s.cfg.SenderAddress, *sequence, nil) + _, err = s.TxBuilder.BuildSequenceBatchesTx(ctx, s.cfg.SenderAddress, sequence) return sequence, err } @@ -704,7 +724,8 @@ func (s *SequenceSender) getSequencesToSend() (*etherman.SequenceBanana, error) // Check if the current batch is the last before a change to a new forkid, in this case we need to close and send the sequence to L1 if (s.cfg.ForkUpgradeBatchNumber != 0) && (batchNumber == (s.cfg.ForkUpgradeBatchNumber)) { log.Infof("[SeqSender] sequence should be sent to L1, as we have reached the batch %d from which a new forkid is applied (upgrade)", s.cfg.ForkUpgradeBatchNumber) - return s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) + //return s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) + return s.TxBuilder.NewSequence(sequenceBatches, s.cfg.L2Coinbase) } } @@ -716,7 +737,8 @@ func (s *SequenceSender) getSequencesToSend() (*etherman.SequenceBanana, error) if s.latestVirtualTime.Before(time.Now().Add(-s.cfg.LastBatchVirtualizationTimeMaxWaitPeriod.Duration)) { log.Infof("[SeqSender] sequence should be sent, too much time without sending anything to L1") - return s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) + //return s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) + return s.TxBuilder.NewSequence(sequenceBatches, s.cfg.L2Coinbase) } log.Infof("[SeqSender] not enough time has passed since last batch was virtualized and the sequence could be bigger") @@ -724,7 +746,7 @@ func (s *SequenceSender) getSequencesToSend() (*etherman.SequenceBanana, error) } // handleEstimateGasSendSequenceErr handles an error on the estimate gas. Results: (nil,nil)=requires waiting, (nil,error)=no handled gracefully, (seq,nil) handled gracefully -func (s *SequenceSender) handleEstimateGasSendSequenceErr(sequenceBatches []etherman.Batch, currentBatchNumToSequence uint64, err error) ([]etherman.Batch, error) { +func (s *SequenceSender) handleEstimateGasSendSequenceErr(sequenceBatches []seqsendertypes.Batch, currentBatchNumToSequence uint64, err error) ([]seqsendertypes.Batch, error) { // Insufficient allowance if errors.Is(err, etherman.ErrInsufficientAllowance) { return nil, err @@ -928,13 +950,12 @@ func (s *SequenceSender) closeSequenceBatch() error { data := s.sequenceData[s.wipBatch] if data != nil { data.batchClosed = true - - var err error - data.batch.L2Data, err = state.EncodeBatchV2(data.batchRaw) + l2Data, err := state.EncodeBatchV2(data.batchRaw) if err != nil { log.Errorf("[SeqSender] error closing and encoding the batch %d: %v", s.wipBatch, err) return err } + data.batch.SetL2Data(l2Data) } s.mutexSequence.Unlock() @@ -952,14 +973,7 @@ func (s *SequenceSender) addNewSequenceBatch(l2Block *datastream.L2Block) { s.logFatalf("[SeqSender] new batch number (%d) is lower than the current one (%d)", l2Block.BatchNumber, s.wipBatch) } - // Create batch - batch := etherman.Batch{ - LastL2BLockTimestamp: l2Block.Timestamp, - BatchNumber: l2Block.BatchNumber, - L1InfoTreeIndex: l2Block.L1InfotreeIndex, - LastCoinbase: common.BytesToAddress(l2Block.Coinbase), - GlobalExitRoot: common.BytesToHash(l2Block.GlobalExitRoot), - } + batch := s.TxBuilder.NewBatchFromL2Block(l2Block) // Add to the list s.sequenceList = append(s.sequenceList, l2Block.BatchNumber) @@ -968,7 +982,7 @@ func (s *SequenceSender) addNewSequenceBatch(l2Block *datastream.L2Block) { batchRaw := state.BatchRawV2{} data := sequenceData{ batchClosed: false, - batch: &batch, + batch: batch, batchRaw: &batchRaw, } s.sequenceData[l2Block.BatchNumber] = &data @@ -987,7 +1001,7 @@ func (s *SequenceSender) addInfoSequenceBatchStart(batch *datastream.BatchStart) data := s.sequenceData[s.wipBatch] if data != nil { wipBatch := data.batch - if wipBatch.BatchNumber+1 != batch.Number { + if wipBatch.BatchNumber()+1 != batch.Number { s.logFatalf("[SeqSender] batch start number (%d) does not match the current consecutive one (%d)", batch.Number, wipBatch.BatchNumber) } } @@ -1003,7 +1017,7 @@ func (s *SequenceSender) addInfoSequenceBatchEnd(batch *datastream.BatchEnd) { data := s.sequenceData[s.wipBatch] if data != nil { wipBatch := data.batch - if wipBatch.BatchNumber == batch.Number { + if wipBatch.BatchNumber() == batch.Number { // wipBatch.StateRoot = common.BytesToHash(batch) TODO: check if this is needed } else { s.logFatalf("[SeqSender] batch end number (%d) does not match the current one (%d)", batch.Number, wipBatch.BatchNumber) @@ -1022,14 +1036,16 @@ func (s *SequenceSender) addNewBatchL2Block(l2Block *datastream.L2Block) { data := s.sequenceData[s.wipBatch] if data != nil { wipBatchRaw := data.batchRaw - data.batch.LastL2BLockTimestamp = l2Block.Timestamp + //data.batch.LastL2BLockTimestamp = l2Block.Timestamp + data.batch.SetLastL2BLockTimestamp(l2Block.Timestamp) // Sanity check: should be the same coinbase within the batch - if common.BytesToAddress(l2Block.Coinbase) != data.batch.LastCoinbase { + if common.BytesToAddress(l2Block.Coinbase) != data.batch.LastCoinbase() { s.logFatalf("[SeqSender] coinbase changed within the batch! (Previous %v, Current %v)", data.batch.LastCoinbase, common.BytesToAddress(l2Block.Coinbase)) } - data.batch.LastCoinbase = common.BytesToAddress(l2Block.Coinbase) - data.batch.L1InfoTreeIndex = l2Block.L1InfotreeIndex - + //data.batch.LastCoinbase = common.BytesToAddress(l2Block.Coinbase) + data.batch.SetLastCoinbase(common.BytesToAddress(l2Block.Coinbase)) + //data.batch.L1InfoTreeIndex = l2Block.L1InfotreeIndex + data.batch.SetL1InfoTreeIndex(l2Block.L1InfotreeIndex) // New L2 block raw newBlockRaw := state.L2BlockRaw{} @@ -1037,7 +1053,9 @@ func (s *SequenceSender) addNewBatchL2Block(l2Block *datastream.L2Block) { wipBatchRaw.Blocks = append(wipBatchRaw.Blocks, newBlockRaw) // Update batch timestamp - data.batch.LastL2BLockTimestamp = l2Block.Timestamp + //data.batch.LastL2BLockTimestamp = l2Block.Timestamp + // TODO: Duplicated assignation + data.batch.SetLastL2BLockTimestamp(l2Block.Timestamp) // Get current L2 block _, blockRaw := s.getWipL2Block() @@ -1148,47 +1166,6 @@ func (s *SequenceSender) logFatalf(template string, args ...interface{}) { } } -// newSequenceBanana creates a new sequence to be sent to L1 -func (s *SequenceSender) newSequenceBanana(batches []etherman.Batch, coinbase common.Address) (*etherman.SequenceBanana, error) { - sequence := etherman.NewSequenceBanana(batches, coinbase) - - l1InfoRoot, err := s.etherman.GetL1InfoRoot(sequence.IndexL1InfoRoot) - if err != nil { - return nil, err - } - - sequence.L1InfoRoot = l1InfoRoot - - accInputHash, err := s.etherman.LastAccInputHash() - if err != nil { - return nil, err - } - - oldAccInputHash := common.BytesToHash(accInputHash.Bytes()) //copy it - - for _, batch := range sequence.Batches { - infoRootHash := sequence.L1InfoRoot - timestamp := sequence.MaxSequenceTimestamp - blockHash := common.Hash{} - - if batch.ForcedBatchTimestamp > 0 { - infoRootHash = batch.ForcedGlobalExitRoot - timestamp = batch.ForcedBatchTimestamp - blockHash = batch.ForcedBlockHashL1 - } - - accInputHash, err = cdkcommon.CalculateAccInputHash(accInputHash, batch.L2Data, infoRootHash, timestamp, batch.LastCoinbase, blockHash) - if err != nil { - return nil, err - } - } - - sequence.OldAccInputHash = oldAccInputHash - sequence.AccInputHash = accInputHash - - return sequence, nil -} - // printSequenceBatches prints data from slice of type sequence batches func printSequenceBatches(sequence *etherman.SequenceBanana) { for i, b := range sequence.Batches { diff --git a/sequencesender/sequencesender_test.go b/sequencesender/sequencesender_test.go index ca0d33e7..1c58f976 100644 --- a/sequencesender/sequencesender_test.go +++ b/sequencesender/sequencesender_test.go @@ -63,3 +63,10 @@ func TestStreamTx(t *testing.T) { printBatch(decodedBatch, true, true) } + +func TestKK(t *testing.T) { + sut := &SequenceSender{} + sequences, err := sut.getSequencesToSend() + require.NoError(t, err) + require.NotNil(t, sequences) +} diff --git a/sequencesender/txbuilder/banana_base.go b/sequencesender/txbuilder/banana_base.go new file mode 100644 index 00000000..5fd98947 --- /dev/null +++ b/sequencesender/txbuilder/banana_base.go @@ -0,0 +1,111 @@ +package txbuilder + +import ( + cdkcommon "github.com/0xPolygon/cdk/common" + "github.com/0xPolygon/cdk/etherman" + "github.com/0xPolygon/cdk/etherman/contracts" + "github.com/0xPolygon/cdk/sequencesender/seqsendertypes" + "github.com/0xPolygon/cdk/state/datastream" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" +) + +type TxBuilderBananaBase struct { + zkevm contracts.ContractRollupBanana + globalExitRootContract contracts.ContractGlobalExitRootBanana + opts bind.TransactOpts + SenderAddress common.Address +} + +func convertToSequenceBanana(sequences seqsendertypes.Sequence) etherman.SequenceBanana { + // TODO: Fill + return etherman.SequenceBanana{} +} + +func convertToEthermanBatch(batch seqsendertypes.Batch) (etherman.Batch, error) { + return etherman.Batch{ + L2Data: batch.L2Data(), + LastCoinbase: batch.LastCoinbase(), + ForcedGlobalExitRoot: batch.ForcedGlobalExitRoot(), + ForcedBlockHashL1: batch.ForcedBlockHashL1(), + ForcedBatchTimestamp: batch.ForcedBatchTimestamp(), + BatchNumber: batch.BatchNumber(), + L1InfoTreeIndex: batch.L1InfoTreeIndex(), + LastL2BLockTimestamp: batch.LastL2BLockTimestamp(), + GlobalExitRoot: batch.GlobalExitRoot(), + }, nil + // cast, ok := batch.(*BananaBatch) + // if !ok { + // log.Error("Batch is not a BananaBatch") + // return etherman.Batch{}, fmt.Errorf("Batch is not a BananaBatch") + // } + // return cast.Batch, nil +} + +func convertToEthermanBatches(batch []seqsendertypes.Batch) ([]etherman.Batch, error) { + result := make([]etherman.Batch, len(batch)) + for i, b := range batch { + var err error + result[i], err = convertToEthermanBatch(b) + if err != nil { + return nil, err + } + } + + return result, nil +} + +func (t *TxBuilderBananaBase) NewBatchFromL2Block(l2Block *datastream.L2Block) seqsendertypes.Batch { + batch := ðerman.Batch{ + LastL2BLockTimestamp: l2Block.Timestamp, + BatchNumber: l2Block.BatchNumber, + L1InfoTreeIndex: l2Block.L1InfotreeIndex, + LastCoinbase: common.BytesToAddress(l2Block.Coinbase), + GlobalExitRoot: common.BytesToHash(l2Block.GlobalExitRoot), + } + return NewBananaBatch(batch) +} + +func (t *TxBuilderBananaBase) NewSequence(batches []seqsendertypes.Batch, coinbase common.Address) (seqsendertypes.Sequence, error) { + ethBatches, err := convertToEthermanBatches(batches) + if err != nil { + return nil, err + } + sequence := etherman.NewSequenceBanana(ethBatches, coinbase) + + l1InfoRoot, err := t.globalExitRootContract.GetL1InfoRoot(sequence.IndexL1InfoRoot) + if err != nil { + return nil, err + } + + sequence.L1InfoRoot = l1InfoRoot + + accInputHash, err := t.zkevm.LastAccInputHash() + if err != nil { + return nil, err + } + + oldAccInputHash := common.BytesToHash(accInputHash.Bytes()) //copy it + + for _, batch := range sequence.Batches { + infoRootHash := sequence.L1InfoRoot + timestamp := sequence.MaxSequenceTimestamp + blockHash := common.Hash{} + + if batch.ForcedBatchTimestamp > 0 { + infoRootHash = batch.ForcedGlobalExitRoot + timestamp = batch.ForcedBatchTimestamp + blockHash = batch.ForcedBlockHashL1 + } + + accInputHash, err = cdkcommon.CalculateAccInputHash(accInputHash, batch.L2Data, infoRootHash, timestamp, batch.LastCoinbase, blockHash) + if err != nil { + return nil, err + } + } + + sequence.OldAccInputHash = oldAccInputHash + sequence.AccInputHash = accInputHash + res := NewBananaSequence(*sequence) + return res, nil +} diff --git a/sequencesender/txbuilder/banana_types.go b/sequencesender/txbuilder/banana_types.go new file mode 100644 index 00000000..f44a753f --- /dev/null +++ b/sequencesender/txbuilder/banana_types.go @@ -0,0 +1,132 @@ +package txbuilder + +import ( + "fmt" + + "github.com/0xPolygon/cdk/etherman" + "github.com/0xPolygon/cdk/sequencesender/seqsendertypes" + "github.com/ethereum/go-ethereum/common" +) + +type BananaBatch struct { + etherman.Batch +} + +type BananaSequence struct { + etherman.SequenceBanana +} + +func NewBananaBatch(batch *etherman.Batch) *BananaBatch { + return &BananaBatch{*batch} +} + +func NewBananaSequence(ult etherman.SequenceBanana) *BananaSequence { + return &BananaSequence{ult} +} + +func (b *BananaSequence) IndexL1InfoRoot() uint32 { + return b.SequenceBanana.IndexL1InfoRoot +} + +func (b *BananaSequence) MaxSequenceTimestamp() uint64 { + return b.SequenceBanana.MaxSequenceTimestamp +} + +func (b *BananaSequence) L1InfoRoot() common.Hash { + return b.SequenceBanana.L1InfoRoot +} + +func (b *BananaSequence) Batches() []seqsendertypes.Batch { + res := make([]seqsendertypes.Batch, len(b.SequenceBanana.Batches)) + for i, batch := range b.SequenceBanana.Batches { + res[i] = &BananaBatch{batch} + } + return res +} + +func (b *BananaSequence) FirstBatch() seqsendertypes.Batch { + return &BananaBatch{b.SequenceBanana.Batches[0]} +} + +func (b *BananaSequence) LastBatch() seqsendertypes.Batch { + return &BananaBatch{b.SequenceBanana.Batches[b.Len()-1]} +} + +func (b *BananaSequence) Len() int { + return len(b.SequenceBanana.Batches) +} + +func (b *BananaSequence) String() string { + res := fmt.Sprintf("Seq/Banana: L2Coinbase: %s, OldAccInputHash: %x, AccInputHash: %x, L1InfoRoot: %x, MaxSequenceTimestamp: %d, IndexL1InfoRoot: %d", + b.L2Coinbase().String(), b.OldAccInputHash.String(), b.AccInputHash.String(), b.L1InfoRoot().String(), b.MaxSequenceTimestamp(), b.IndexL1InfoRoot()) + + for i, batch := range b.Batches() { + res += fmt.Sprintf("\n\tBatch %d: %s", i, batch.String()) + } + return res +} + +func (b *BananaSequence) L2Coinbase() common.Address { + return b.SequenceBanana.L2Coinbase +} + +func (b *BananaBatch) LastCoinbase() common.Address { + return b.Batch.LastCoinbase +} + +func (b *BananaBatch) ForcedBatchTimestamp() uint64 { + return b.Batch.ForcedBatchTimestamp +} + +func (b *BananaBatch) ForcedGlobalExitRoot() common.Hash { + return b.Batch.ForcedGlobalExitRoot +} + +func (b *BananaBatch) ForcedBlockHashL1() common.Hash { + return b.Batch.ForcedBlockHashL1 +} + +func (b *BananaBatch) L2Data() []byte { + return b.Batch.L2Data +} + +func (b *BananaBatch) LastL2BLockTimestamp() uint64 { + return b.Batch.LastL2BLockTimestamp +} + +func (b *BananaBatch) BatchNumber() uint64 { + return b.Batch.BatchNumber +} + +func (b BananaBatch) DeepCopy() seqsendertypes.Batch { + return &BananaBatch{b.Batch} + +} + +func (b *BananaBatch) SetL2Data(data []byte) { + b.Batch.L2Data = data + +} +func (b *BananaBatch) SetLastCoinbase(address common.Address) { + b.Batch.LastCoinbase = address +} +func (b *BananaBatch) SetLastL2BLockTimestamp(ts uint64) { + b.Batch.LastL2BLockTimestamp = ts +} +func (b *BananaBatch) SetL1InfoTreeIndex(index uint32) { + b.Batch.L1InfoTreeIndex = index +} + +func (b *BananaBatch) GlobalExitRoot() common.Hash { + return b.Batch.GlobalExitRoot +} + +func (b *BananaBatch) L1InfoTreeIndex() uint32 { + return b.Batch.L1InfoTreeIndex +} + +func (b *BananaBatch) String() string { + return fmt.Sprintf("Batch/Banana: LastCoinbase: %s, ForcedBatchTimestamp: %d, ForcedGlobalExitRoot: %x, ForcedBlockHashL1: %x, L2Data: %x, LastL2BLockTimestamp: %d, BatchNumber: %d, GlobalExitRoot: %x, L1InfoTreeIndex: %d", + b.LastCoinbase().String(), b.ForcedBatchTimestamp(), b.ForcedGlobalExitRoot().String(), b.ForcedBlockHashL1().String(), b.L2Data(), b.LastL2BLockTimestamp(), b.BatchNumber(), b.GlobalExitRoot().String(), b.L1InfoTreeIndex(), + ) +} diff --git a/sequencesender/txbuilder/banana_validium.go b/sequencesender/txbuilder/banana_validium.go new file mode 100644 index 00000000..e7af822d --- /dev/null +++ b/sequencesender/txbuilder/banana_validium.go @@ -0,0 +1,100 @@ +package txbuilder + +import ( + "context" + "math/big" + + "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonvalidiumetrog" + "github.com/0xPolygon/cdk/dataavailability" + "github.com/0xPolygon/cdk/etherman" + "github.com/0xPolygon/cdk/etherman/contracts" + "github.com/0xPolygon/cdk/log" + "github.com/0xPolygon/cdk/sequencesender/seqsendertypes" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + ethtypes "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" +) + +type TxBuilderBananaValidium struct { + TxBuilderBananaBase + da dataavailability.SequenceSender +} + +func NewTxBuilderBananaValidium(zkevm contracts.ContractRollupBanana, da dataavailability.SequenceSender, opts bind.TransactOpts, sender common.Address) *TxBuilderBananaValidium { + return &TxBuilderBananaValidium{ + TxBuilderBananaBase: TxBuilderBananaBase{ + zkevm: zkevm, + opts: opts, + SenderAddress: sender, + }, + da: da, + } +} + +func (t *TxBuilderBananaValidium) BuildSequenceBatchesTx(ctx context.Context, sender common.Address, sequences seqsendertypes.Sequence) (*ethtypes.Transaction, error) { + // TODO: param sender + // Post sequences to DA backend + var dataAvailabilityMessage []byte + var err error + ethseq := convertToSequenceBanana(sequences) + + dataAvailabilityMessage, err = t.da.PostSequence(ctx, ethseq) + if err != nil { + log.Error("error posting sequences to the data availability protocol: ", err) + return nil, err + } + + // Build sequence data + tx, err := t.internalBuildSequenceBatchesTx(t.SenderAddress, ethseq, dataAvailabilityMessage) + if err != nil { + log.Errorf("[SeqSender] error estimating new sequenceBatches to add to ethtxmanager: ", err) + return nil, err + } + return tx, nil +} + +// BuildSequenceBatchesTx builds a tx to be sent to the PoE SC method SequenceBatches. +func (t *TxBuilderBananaValidium) internalBuildSequenceBatchesTx(sender common.Address, sequence etherman.SequenceBanana, + dataAvailabilityMessage []byte) (*ethtypes.Transaction, error) { + newopts := t.opts + newopts.NoSend = true + + // force nonce, gas limit and gas price to avoid querying it from the chain + newopts.Nonce = big.NewInt(1) + newopts.GasLimit = uint64(1) + newopts.GasPrice = big.NewInt(1) + + return t.sequenceBatchesValidium(newopts, sequence, dataAvailabilityMessage) +} + +func (t *TxBuilderBananaValidium) sequenceBatchesValidium(opts bind.TransactOpts, sequence etherman.SequenceBanana, dataAvailabilityMessage []byte) (*ethtypes.Transaction, error) { + batches := make([]polygonvalidiumetrog.PolygonValidiumEtrogValidiumBatchData, len(sequence.Batches)) + for i, batch := range sequence.Batches { + var ger common.Hash + if batch.ForcedBatchTimestamp > 0 { + ger = batch.ForcedGlobalExitRoot + } + + batches[i] = polygonvalidiumetrog.PolygonValidiumEtrogValidiumBatchData{ + TransactionsHash: crypto.Keccak256Hash(batch.L2Data), + ForcedGlobalExitRoot: ger, + ForcedTimestamp: batch.ForcedBatchTimestamp, + ForcedBlockHashL1: batch.ForcedBlockHashL1, + } + } + + tx, err := t.zkevm.GetContract().SequenceBatchesValidium(&opts, batches, sequence.IndexL1InfoRoot, sequence.MaxSequenceTimestamp, sequence.AccInputHash, sequence.L2Coinbase, dataAvailabilityMessage) + if err != nil { + log.Debugf("Batches to send: %+v", batches) + log.Debug("l2CoinBase: ", sequence.L2Coinbase) + log.Debug("Sequencer address: ", opts.From) + + } + + return tx, err +} + +func (t *TxBuilderBananaValidium) String() string { + return "Banana/Validium" +} diff --git a/sequencesender/txbuilder/banana_zkevm.go b/sequencesender/txbuilder/banana_zkevm.go new file mode 100644 index 00000000..f7148cf7 --- /dev/null +++ b/sequencesender/txbuilder/banana_zkevm.go @@ -0,0 +1,79 @@ +package txbuilder + +import ( + "context" + "math/big" + + "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonvalidiumetrog" + "github.com/0xPolygon/cdk/etherman" + "github.com/0xPolygon/cdk/etherman/contracts" + "github.com/0xPolygon/cdk/log" + "github.com/0xPolygon/cdk/sequencesender/seqsendertypes" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + ethtypes "github.com/ethereum/go-ethereum/core/types" +) + +type TxBuilderBananaZKEVM struct { + TxBuilderBananaBase +} + +func NewTxBuilderBananaZKEVM(zkevm contracts.ContractRollupBanana, opts bind.TransactOpts, sender common.Address) *TxBuilderBananaZKEVM { + return &TxBuilderBananaZKEVM{ + TxBuilderBananaBase: TxBuilderBananaBase{ + zkevm: zkevm, + opts: opts, + SenderAddress: sender, + }, + } +} + +func (t *TxBuilderBananaZKEVM) BuildSequenceBatchesTx(ctx context.Context, sender common.Address, sequences seqsendertypes.Sequence) (*ethtypes.Transaction, error) { + var err error + ethseq := convertToSequenceBanana(sequences) + newopts := t.opts + newopts.NoSend = true + + // force nonce, gas limit and gas price to avoid querying it from the chain + newopts.Nonce = big.NewInt(1) + newopts.GasLimit = uint64(1) + newopts.GasPrice = big.NewInt(1) + // Build sequence data + tx, err := t.sequenceBatchesRollup(newopts, ethseq) + if err != nil { + log.Errorf("[SeqSender] error estimating new sequenceBatches to add to ethtxmanager: ", err) + return nil, err + } + return tx, nil +} + +func (t *TxBuilderBananaZKEVM) sequenceBatchesRollup(opts bind.TransactOpts, sequence etherman.SequenceBanana) (*ethtypes.Transaction, error) { + batches := make([]polygonvalidiumetrog.PolygonRollupBaseEtrogBatchData, len(sequence.Batches)) + for i, batch := range sequence.Batches { + var ger common.Hash + if batch.ForcedBatchTimestamp > 0 { + ger = batch.ForcedGlobalExitRoot + } + + batches[i] = polygonvalidiumetrog.PolygonRollupBaseEtrogBatchData{ + Transactions: batch.L2Data, + ForcedGlobalExitRoot: ger, + ForcedTimestamp: batch.ForcedBatchTimestamp, + ForcedBlockHashL1: batch.ForcedBlockHashL1, + } + } + + tx, err := t.zkevm.GetContract().SequenceBatches(&opts, batches, sequence.IndexL1InfoRoot, sequence.MaxSequenceTimestamp, sequence.AccInputHash, sequence.L2Coinbase) + if err != nil { + log.Debugf("Batches to send: %+v", batches) + log.Debug("l2CoinBase: ", sequence.L2Coinbase) + log.Debug("Sequencer address: ", opts.From) + + } + + return tx, err +} + +func (t *TxBuilderBananaZKEVM) String() string { + return "Banana/ZKEVM" +} diff --git a/sequencesender/txbuilder/elderberry_types.go b/sequencesender/txbuilder/elderberry_types.go new file mode 100644 index 00000000..fc57bc18 --- /dev/null +++ b/sequencesender/txbuilder/elderberry_types.go @@ -0,0 +1,55 @@ +package txbuilder + +import ( + "fmt" + "log" + + "github.com/0xPolygon/cdk/sequencesender/seqsendertypes" + "github.com/ethereum/go-ethereum/common" +) + +type ElderberrySequence struct { + l2Coinbase common.Address + batches []seqsendertypes.Batch +} + +func (b *ElderberrySequence) IndexL1InfoRoot() uint32 { + log.Fatal("Elderberry Sequence does not have IndexL1InfoRoot") + return 0 +} + +func (b *ElderberrySequence) MaxSequenceTimestamp() uint64 { + return b.LastBatch().LastL2BLockTimestamp() +} + +func (b *ElderberrySequence) L1InfoRoot() common.Hash { + log.Fatal("Elderberry Sequence does not have L1InfoRoot") + return common.Hash{} +} + +func (b *ElderberrySequence) Batches() []seqsendertypes.Batch { + return b.batches +} + +func (b *ElderberrySequence) FirstBatch() seqsendertypes.Batch { + return b.batches[0] +} + +func (b *ElderberrySequence) LastBatch() seqsendertypes.Batch { + return b.batches[b.Len()-1] +} + +func (b *ElderberrySequence) Len() int { + return len(b.batches) +} + +func (b *ElderberrySequence) L2Coinbase() common.Address { + return b.l2Coinbase +} +func (b *ElderberrySequence) String() string { + res := fmt.Sprintf("Seq/Elderberry: L2Coinbase: %s, Batches: %d", b.l2Coinbase.String(), len(b.batches)) + for i, batch := range b.Batches() { + res += fmt.Sprintf("\n\tBatch %d: %s", i, batch.String()) + } + return res +} diff --git a/sequencesender/txbuilder/elderberry_zkevm.go b/sequencesender/txbuilder/elderberry_zkevm.go new file mode 100644 index 00000000..d561e59f --- /dev/null +++ b/sequencesender/txbuilder/elderberry_zkevm.go @@ -0,0 +1,105 @@ +package txbuilder + +import ( + "context" + "math/big" + + "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry/polygonvalidiumetrog" + "github.com/0xPolygon/cdk/etherman" + "github.com/0xPolygon/cdk/etherman/contracts" + "github.com/0xPolygon/cdk/log" + "github.com/0xPolygon/cdk/sequencesender/seqsendertypes" + "github.com/0xPolygon/cdk/state/datastream" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + ethtypes "github.com/ethereum/go-ethereum/core/types" +) + +type TxBuilderElderberryZKEVM struct { + opts bind.TransactOpts + zkevm contracts.ContractRollupElderberry +} + +func NewTxBuilderElderberryZKEVM(zkevm contracts.ContractRollupElderberry, opts bind.TransactOpts, sender common.Address) *TxBuilderElderberryZKEVM { + return &TxBuilderElderberryZKEVM{ + opts: opts, + zkevm: zkevm, + } +} + +func (t *TxBuilderElderberryZKEVM) NewSequence(batches []seqsendertypes.Batch, coinbase common.Address) (seqsendertypes.Sequence, error) { + seq := ElderberrySequence{ + l2Coinbase: coinbase, + batches: batches, + } + return &seq, nil +} + +func (t *TxBuilderElderberryZKEVM) NewBatchFromL2Block(l2Block *datastream.L2Block) seqsendertypes.Batch { + batch := ðerman.Batch{ + LastL2BLockTimestamp: l2Block.Timestamp, + BatchNumber: l2Block.BatchNumber, + L1InfoTreeIndex: l2Block.L1InfotreeIndex, + LastCoinbase: common.BytesToAddress(l2Block.Coinbase), + GlobalExitRoot: common.BytesToHash(l2Block.GlobalExitRoot), + } + return NewBananaBatch(batch) +} + +func (t *TxBuilderElderberryZKEVM) BuildSequenceBatchesTx(ctx context.Context, sender common.Address, sequences seqsendertypes.Sequence) (*ethtypes.Transaction, error) { + + newopts := t.opts + newopts.NoSend = true + + // force nonce, gas limit and gas price to avoid querying it from the chain + newopts.Nonce = big.NewInt(1) + newopts.GasLimit = uint64(1) + newopts.GasPrice = big.NewInt(1) + + return t.sequenceBatchesRollup(newopts, sequences) +} + +func (t *TxBuilderElderberryZKEVM) sequenceBatchesRollup(opts bind.TransactOpts, sequences seqsendertypes.Sequence) (*types.Transaction, error) { + batches := make([]polygonvalidiumetrog.PolygonRollupBaseEtrogBatchData, sequences.Len()) + for i, seq := range sequences.Batches() { + var ger common.Hash + if seq.ForcedBatchTimestamp() > 0 { + ger = seq.GlobalExitRoot() + } + + batches[i] = polygonvalidiumetrog.PolygonRollupBaseEtrogBatchData{ + Transactions: seq.L2Data(), + ForcedGlobalExitRoot: ger, + ForcedTimestamp: uint64(seq.ForcedBatchTimestamp()), + // TODO: Check that is ok to use ForcedBlockHashL1 instead PrevBlockHash + ForcedBlockHashL1: seq.ForcedBlockHashL1(), + } + } + lastSequencedBatchNumber := getLastSequencedBatchNumber(sequences) + ZkEVM := t.zkevm.GetContract() + tx, err := ZkEVM.SequenceBatches(&opts, batches, sequences.MaxSequenceTimestamp(), lastSequencedBatchNumber, sequences.L2Coinbase()) + if err != nil { + t.warningMessage(batches, sequences.L2Coinbase(), &opts) + if parsedErr, ok := etherman.TryParseError(err); ok { + err = parsedErr + } + } + + return tx, err +} + +func (t *TxBuilderElderberryZKEVM) warningMessage(batches []polygonvalidiumetrog.PolygonRollupBaseEtrogBatchData, l2Coinbase common.Address, opts *bind.TransactOpts) { + log.Warnf("Sequencer address: ", opts.From, "l2CoinBase: ", l2Coinbase, " Batches to send: %+v", batches) +} + +func getLastSequencedBatchNumber(sequences seqsendertypes.Sequence) uint64 { + if sequences.Len() == 0 { + return 0 + } + return sequences.FirstBatch().BatchNumber() - 1 +} + +func (t *TxBuilderElderberryZKEVM) String() string { + return "Elderberry/ZKEVM" +} diff --git a/sequencesender/txbuilder/interface.go b/sequencesender/txbuilder/interface.go new file mode 100644 index 00000000..c7b93be8 --- /dev/null +++ b/sequencesender/txbuilder/interface.go @@ -0,0 +1,17 @@ +package txbuilder + +import ( + "context" + + "github.com/0xPolygon/cdk/sequencesender/seqsendertypes" + "github.com/0xPolygon/cdk/state/datastream" + "github.com/ethereum/go-ethereum/common" + ethtypes "github.com/ethereum/go-ethereum/core/types" +) + +type TxBuilder interface { + BuildSequenceBatchesTx(ctx context.Context, sender common.Address, sequences seqsendertypes.Sequence) (*ethtypes.Transaction, error) + NewSequence(batches []seqsendertypes.Batch, coinbase common.Address) (seqsendertypes.Sequence, error) + NewBatchFromL2Block(l2Block *datastream.L2Block) seqsendertypes.Batch + String() string +} From 684aef546b245936f878c93b3641474eee9c5654 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Wed, 17 Jul 2024 12:03:08 +0200 Subject: [PATCH 02/51] remove IsValidium from seqSender code --- cmd/run.go | 9 +- config/default.go | 1 - etherman/config.go | 3 - etherman/etherman.go | 1 - sequencesender/config.go | 2 - sequencesender/sequencesender.go | 108 ++---------------- sequencesender/txbuilder/banana_validium.go | 12 +- sequencesender/txbuilder/banana_zkevm.go | 10 +- sequencesender/txbuilder/elderberry_zkevm.go | 14 ++- sequencesender/txbuilder/interface.go | 5 + .../txbuilder/validium_cond_num_batches.go | 24 ++++ .../txbuilder/zkevm_cond_max_size.go | 83 ++++++++++++++ test/config/test.config.toml | 5 +- 13 files changed, 158 insertions(+), 119 deletions(-) create mode 100644 sequencesender/txbuilder/validium_cond_num_batches.go create mode 100644 sequencesender/txbuilder/zkevm_cond_max_size.go diff --git a/cmd/run.go b/cmd/run.go index 983a5158..71e5a448 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -124,7 +124,6 @@ func createSequenceSender(cfg config.Config) *sequencesender.SequenceSender { }, HTTPHeaders: cfg.SequenceSender.EthTxManager.Etherman.HTTPHeaders, }, - IsValidiumMode: cfg.SequenceSender.IsValidiumMode, }, cfg.NetworkConfig.L1Config, cfg.Common) if err != nil { log.Fatal(err) @@ -162,16 +161,16 @@ func newTxBuilder(cfg config.Config, ethman *etherman.Client) (txbuilder.TxBuild switch contracts.VersionType(cfg.Common.ContractVersions) { case contracts.VersionBanana: if cfg.Common.IsValidiumMode { - txBuilder = txbuilder.NewTxBuilderBananaValidium(*ethman.Contracts.Banana.ZkEVM, da, *auth, auth.From) + txBuilder = txbuilder.NewTxBuilderBananaValidium(*ethman.Contracts.Banana.ZkEVM, da, *auth, auth.From, cfg.SequenceSender.MaxBatchesForL1) } else { - txBuilder = txbuilder.NewTxBuilderBananaZKEVM(*ethman.Contracts.Banana.ZkEVM, *auth, auth.From) + txBuilder = txbuilder.NewTxBuilderBananaZKEVM(*ethman.Contracts.Banana.ZkEVM, *auth, auth.From, cfg.SequenceSender.MaxTxSizeForL1) } case contracts.VersionElderberry: if cfg.Common.IsValidiumMode { err = fmt.Errorf("Elderberry+Validium not implemented yet") //txBuilder = txbuilder.NewTxBuilderElderberryValidium(*ethman.Contracts.Elderberry.ZkEVM, da, *auth, auth.From) } else { - txBuilder = txbuilder.NewTxBuilderElderberryZKEVM(*ethman.Contracts.Elderberry.ZkEVM, *auth, auth.From) + txBuilder = txbuilder.NewTxBuilderElderberryZKEVM(*ethman.Contracts.Elderberry.ZkEVM, *auth, auth.From, cfg.SequenceSender.MaxTxSizeForL1) } default: err = fmt.Errorf("unknown contract version: %s", cfg.Common.ContractVersions) @@ -181,7 +180,7 @@ func newTxBuilder(cfg config.Config, ethman *etherman.Client) (txbuilder.TxBuild } func newDataAvailability(c config.Config, etherman *etherman.Client) (*dataavailability.DataAvailability, error) { - if !c.SequenceSender.IsValidiumMode { + if !c.Common.IsValidiumMode { return nil, nil } diff --git a/config/default.go b/config/default.go index e8ee755e..7b12e6f3 100644 --- a/config/default.go +++ b/config/default.go @@ -15,7 +15,6 @@ Level = "info" Outputs = ["stderr"] [SequenceSender] -IsValidiumMode = false WaitPeriodSendSequence = "15s" LastBatchVirtualizationTimeMaxWaitPeriod = "10s" L1BlockTimestampMargin = "30s" diff --git a/etherman/config.go b/etherman/config.go index 15904d91..1c50cd70 100644 --- a/etherman/config.go +++ b/etherman/config.go @@ -9,9 +9,6 @@ type Config struct { EthermanConfig etherman.Config - // IsValidiumMode is a flag to indicate if the sequence sender is running in validium mode - IsValidiumMode bool - // ForkIDChunkSize is the max interval for each call to L1 provider to get the forkIDs ForkIDChunkSize uint64 `mapstructure:"ForkIDChunkSize"` } diff --git a/etherman/etherman.go b/etherman/etherman.go index 72ab56d7..2c877463 100644 --- a/etherman/etherman.go +++ b/etherman/etherman.go @@ -123,7 +123,6 @@ func NewClient(cfg Config, l1Config L1Config, commonConfig cdkcommon.Config) (*C cfg: cfg, auth: map[common.Address]bind.TransactOpts{}, } - cfg.IsValidiumMode = commonConfig.IsValidiumMode if commonConfig.IsValidiumMode { dapAddr, err := contracts.ZkEVM(nil).DataAvailabilityProtocol() diff --git a/sequencesender/config.go b/sequencesender/config.go index 58fa01d8..9b80e404 100644 --- a/sequencesender/config.go +++ b/sequencesender/config.go @@ -9,8 +9,6 @@ import ( // Config represents the configuration of a sequence sender type Config struct { - // IsValidiumMode has the value true if the sequence sender is running in validium mode. - IsValidiumMode bool `mapstructure:"IsValidiumMode"` // WaitPeriodSendSequence is the time the sequencer waits until // trying to send a sequence to L1 WaitPeriodSendSequence types.Duration `mapstructure:"WaitPeriodSendSequence"` diff --git a/sequencesender/sequencesender.go b/sequencesender/sequencesender.go index 39775c6c..7bc0adf6 100644 --- a/sequencesender/sequencesender.go +++ b/sequencesender/sequencesender.go @@ -25,11 +25,6 @@ import ( "google.golang.org/protobuf/proto" ) -var ( - // ErrOversizedData when transaction input data is greater than a limit (DOS protection) - ErrOversizedData = errors.New("oversized data") -) - // SequenceSender represents a sequence sender type SequenceSender struct { cfg Config @@ -463,7 +458,7 @@ func (s *SequenceSender) tryToSendSequence(ctx context.Context) { // Check if should send sequence to L1 log.Infof("[SeqSender] getting sequences to send") - sequence, err := s.getSequencesToSend() + sequence, err := s.getSequencesToSend(ctx) if err != nil || sequence == nil || sequence.Len() == 0 { if err != nil { log.Errorf("[SeqSender] error getting sequences: %v", err) @@ -526,27 +521,8 @@ func (s *SequenceSender) tryToSendSequence(ctx context.Context) { // Send sequences to L1 log.Infof("[SeqSender] sending sequences to L1. From batch %d to batch %d", firstSequence.BatchNumber, lastSequence.BatchNumber) - //printSequenceBatches(sequence) log.Infof(sequence.String()) - /* - // Post sequences to DA backend - var dataAvailabilityMessage []byte - if s.cfg.IsValidiumMode { - dataAvailabilityMessage, err = s.da.PostSequence(ctx, *sequence) - if err != nil { - log.Error("error posting sequences to the data availability protocol: ", err) - return - } - } - - // Build sequence data - tx, err := s.etherman.BuildSequenceBatchesTx(s.cfg.SenderAddress, *sequence, dataAvailabilityMessage) - if err != nil { - log.Errorf("[SeqSender] error estimating new sequenceBatches to add to ethtxmanager: ", err) - return - } - */ tx, err := s.TxBuilder.BuildSequenceBatchesTx(ctx, s.cfg.SenderAddress, sequence) if err != nil { log.Errorf("[SeqSender] error building sequenceBatches tx: %v", err) @@ -637,7 +613,7 @@ func (s *SequenceSender) sendTx(ctx context.Context, resend bool, txOldHash *com } // getSequencesToSend generates sequences to be sent to L1. Empty array means there are no sequences to send or it's not worth sending -func (s *SequenceSender) getSequencesToSend() (seqsendertypes.Sequence, error) { +func (s *SequenceSender) getSequencesToSend(ctx context.Context) (seqsendertypes.Sequence, error) { // Add sequences until too big for a single L1 tx or last batch is reached s.mutexSequence.Lock() defer s.mutexSequence.Unlock() @@ -675,50 +651,12 @@ func (s *SequenceSender) getSequencesToSend() (seqsendertypes.Sequence, error) { // Add new sequence batch sequenceBatches = append(sequenceBatches, batch) - if s.cfg.IsValidiumMode { - if len(sequenceBatches) == int(s.cfg.MaxBatchesForL1) { - log.Infof( - "[SeqSender] sequence should be sent to L1, because MaxBatchesForL1 (%d) has been reached", - s.cfg.MaxBatchesForL1, - ) - - //return s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) - return s.TxBuilder.NewSequence(sequenceBatches, s.cfg.L2Coinbase) - } - } else { - //sequence, err := s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) - sequence, err := s.TxBuilder.NewSequence(sequenceBatches, s.cfg.L2Coinbase) - if err != nil { - return nil, err - } - - // Check if can be sent - // TODO: pass real CTX - ctx := context.Background() - tx, err := s.TxBuilder.BuildSequenceBatchesTx(ctx, s.cfg.SenderAddress, sequence) - if err == nil && tx.Size() > s.cfg.MaxTxSizeForL1 { - log.Infof("[SeqSender] oversized Data on TX oldHash %s (txSize %d > %d)", tx.Hash(), tx.Size(), s.cfg.MaxTxSizeForL1) - err = ErrOversizedData - } - - if err != nil { - log.Infof("[SeqSender] handling estimate gas send sequence error: %v", err) - sequenceBatches, err = s.handleEstimateGasSendSequenceErr(sequence.Batches(), batchNumber, err) - if sequenceBatches != nil { - // Handling the error gracefully, re-processing the sequence as a sanity check - //sequence, err = s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) - sequence, err = s.TxBuilder.NewSequence(sequenceBatches, s.cfg.L2Coinbase) - if err != nil { - return nil, err - } - - //_, err = s.etherman.BuildSequenceBatchesTx(s.cfg.SenderAddress, *sequence, nil) - _, err = s.TxBuilder.BuildSequenceBatchesTx(ctx, s.cfg.SenderAddress, sequence) - return sequence, err - } - - return sequence, err - } + newSeq, err := s.TxBuilder.NewSequenceIfWorthToSend(ctx, sequenceBatches, s.cfg.L2Coinbase, batchNumber) + if err != nil { + return nil, err + } + if newSeq != nil { + return newSeq, nil } // Check if the current batch is the last before a change to a new forkid, in this case we need to close and send the sequence to L1 @@ -745,36 +683,6 @@ func (s *SequenceSender) getSequencesToSend() (seqsendertypes.Sequence, error) { return nil, nil } -// handleEstimateGasSendSequenceErr handles an error on the estimate gas. Results: (nil,nil)=requires waiting, (nil,error)=no handled gracefully, (seq,nil) handled gracefully -func (s *SequenceSender) handleEstimateGasSendSequenceErr(sequenceBatches []seqsendertypes.Batch, currentBatchNumToSequence uint64, err error) ([]seqsendertypes.Batch, error) { - // Insufficient allowance - if errors.Is(err, etherman.ErrInsufficientAllowance) { - return nil, err - } - if isDataForEthTxTooBig(err) { - // Remove the latest item and send the sequences - log.Infof("Done building sequences, selected batches to %d. Batch %d caused the L1 tx to be too big: %v", currentBatchNumToSequence-1, currentBatchNumToSequence, err) - } else { - // Remove the latest item and send the sequences - log.Infof("Done building sequences, selected batches to %d. Batch %d excluded due to unknown error: %v", currentBatchNumToSequence, currentBatchNumToSequence+1, err) - } - - if len(sequenceBatches) > 1 { - sequenceBatches = sequenceBatches[:len(sequenceBatches)-1] - } else { - sequenceBatches = nil - } - - return sequenceBatches, nil -} - -// isDataForEthTxTooBig checks if tx oversize error -func isDataForEthTxTooBig(err error) bool { - return errors.Is(err, etherman.ErrGasRequiredExceedsAllowance) || - errors.Is(err, ErrOversizedData) || - errors.Is(err, etherman.ErrContentLengthTooLarge) -} - // loadSentSequencesTransactions loads the file into the memory structure func (s *SequenceSender) loadSentSequencesTransactions() error { // Check if file exists diff --git a/sequencesender/txbuilder/banana_validium.go b/sequencesender/txbuilder/banana_validium.go index e7af822d..4eda6e56 100644 --- a/sequencesender/txbuilder/banana_validium.go +++ b/sequencesender/txbuilder/banana_validium.go @@ -18,10 +18,11 @@ import ( type TxBuilderBananaValidium struct { TxBuilderBananaBase - da dataavailability.SequenceSender + da dataavailability.SequenceSender + condNewSeq CondNewSequence } -func NewTxBuilderBananaValidium(zkevm contracts.ContractRollupBanana, da dataavailability.SequenceSender, opts bind.TransactOpts, sender common.Address) *TxBuilderBananaValidium { +func NewTxBuilderBananaValidium(zkevm contracts.ContractRollupBanana, da dataavailability.SequenceSender, opts bind.TransactOpts, sender common.Address, maxBatchesForL1 uint64) *TxBuilderBananaValidium { return &TxBuilderBananaValidium{ TxBuilderBananaBase: TxBuilderBananaBase{ zkevm: zkevm, @@ -29,9 +30,16 @@ func NewTxBuilderBananaValidium(zkevm contracts.ContractRollupBanana, da dataava SenderAddress: sender, }, da: da, + condNewSeq: &NewSequenceConditionalNumBatches{ + maxBatchesForL1: maxBatchesForL1, + }, } } +func (t *TxBuilderBananaValidium) NewSequenceIfWorthToSend(ctx context.Context, sequenceBatches []seqsendertypes.Batch, l2Coinbase common.Address, batchNumber uint64) (seqsendertypes.Sequence, error) { + return t.condNewSeq.NewSequenceIfWorthToSend(ctx, t, sequenceBatches, t.SenderAddress, l2Coinbase, batchNumber) +} + func (t *TxBuilderBananaValidium) BuildSequenceBatchesTx(ctx context.Context, sender common.Address, sequences seqsendertypes.Sequence) (*ethtypes.Transaction, error) { // TODO: param sender // Post sequences to DA backend diff --git a/sequencesender/txbuilder/banana_zkevm.go b/sequencesender/txbuilder/banana_zkevm.go index f7148cf7..0201be24 100644 --- a/sequencesender/txbuilder/banana_zkevm.go +++ b/sequencesender/txbuilder/banana_zkevm.go @@ -16,18 +16,26 @@ import ( type TxBuilderBananaZKEVM struct { TxBuilderBananaBase + condNewSeq CondNewSequence } -func NewTxBuilderBananaZKEVM(zkevm contracts.ContractRollupBanana, opts bind.TransactOpts, sender common.Address) *TxBuilderBananaZKEVM { +func NewTxBuilderBananaZKEVM(zkevm contracts.ContractRollupBanana, opts bind.TransactOpts, sender common.Address, maxTxSizeForL1 uint64) *TxBuilderBananaZKEVM { return &TxBuilderBananaZKEVM{ TxBuilderBananaBase: TxBuilderBananaBase{ zkevm: zkevm, opts: opts, SenderAddress: sender, }, + condNewSeq: &NewSequenceConditionalMaxSize{ + maxTxSizeForL1: maxTxSizeForL1, + }, } } +func (t *TxBuilderBananaZKEVM) NewSequenceIfWorthToSend(ctx context.Context, sequenceBatches []seqsendertypes.Batch, l2Coinbase common.Address, batchNumber uint64) (seqsendertypes.Sequence, error) { + return t.condNewSeq.NewSequenceIfWorthToSend(ctx, t, sequenceBatches, t.SenderAddress, l2Coinbase, batchNumber) +} + func (t *TxBuilderBananaZKEVM) BuildSequenceBatchesTx(ctx context.Context, sender common.Address, sequences seqsendertypes.Sequence) (*ethtypes.Transaction, error) { var err error ethseq := convertToSequenceBanana(sequences) diff --git a/sequencesender/txbuilder/elderberry_zkevm.go b/sequencesender/txbuilder/elderberry_zkevm.go index d561e59f..6d0ef012 100644 --- a/sequencesender/txbuilder/elderberry_zkevm.go +++ b/sequencesender/txbuilder/elderberry_zkevm.go @@ -17,14 +17,18 @@ import ( ) type TxBuilderElderberryZKEVM struct { - opts bind.TransactOpts - zkevm contracts.ContractRollupElderberry + opts bind.TransactOpts + zkevm contracts.ContractRollupElderberry + condNewSeq CondNewSequence } -func NewTxBuilderElderberryZKEVM(zkevm contracts.ContractRollupElderberry, opts bind.TransactOpts, sender common.Address) *TxBuilderElderberryZKEVM { +func NewTxBuilderElderberryZKEVM(zkevm contracts.ContractRollupElderberry, opts bind.TransactOpts, sender common.Address, maxTxSizeForL1 uint64) *TxBuilderElderberryZKEVM { return &TxBuilderElderberryZKEVM{ opts: opts, zkevm: zkevm, + condNewSeq: &NewSequenceConditionalMaxSize{ + maxTxSizeForL1: maxTxSizeForL1, + }, } } @@ -36,6 +40,10 @@ func (t *TxBuilderElderberryZKEVM) NewSequence(batches []seqsendertypes.Batch, c return &seq, nil } +func (t *TxBuilderElderberryZKEVM) NewSequenceIfWorthToSend(ctx context.Context, sequenceBatches []seqsendertypes.Batch, l2Coinbase common.Address, batchNumber uint64) (seqsendertypes.Sequence, error) { + return t.condNewSeq.NewSequenceIfWorthToSend(ctx, t, sequenceBatches, t.opts.From, l2Coinbase, batchNumber) +} + func (t *TxBuilderElderberryZKEVM) NewBatchFromL2Block(l2Block *datastream.L2Block) seqsendertypes.Batch { batch := ðerman.Batch{ LastL2BLockTimestamp: l2Block.Timestamp, diff --git a/sequencesender/txbuilder/interface.go b/sequencesender/txbuilder/interface.go index c7b93be8..8f3b4661 100644 --- a/sequencesender/txbuilder/interface.go +++ b/sequencesender/txbuilder/interface.go @@ -12,6 +12,11 @@ import ( type TxBuilder interface { BuildSequenceBatchesTx(ctx context.Context, sender common.Address, sequences seqsendertypes.Sequence) (*ethtypes.Transaction, error) NewSequence(batches []seqsendertypes.Batch, coinbase common.Address) (seqsendertypes.Sequence, error) + NewSequenceIfWorthToSend(ctx context.Context, sequenceBatches []seqsendertypes.Batch, l2Coinbase common.Address, batchNumber uint64) (seqsendertypes.Sequence, error) NewBatchFromL2Block(l2Block *datastream.L2Block) seqsendertypes.Batch String() string } + +type CondNewSequence interface { + NewSequenceIfWorthToSend(ctx context.Context, txBuilder TxBuilder, sequenceBatches []seqsendertypes.Batch, senderAddress, l2Coinbase common.Address, batchNumber uint64) (seqsendertypes.Sequence, error) +} diff --git a/sequencesender/txbuilder/validium_cond_num_batches.go b/sequencesender/txbuilder/validium_cond_num_batches.go new file mode 100644 index 00000000..a80fed1c --- /dev/null +++ b/sequencesender/txbuilder/validium_cond_num_batches.go @@ -0,0 +1,24 @@ +package txbuilder + +import ( + "context" + + "github.com/0xPolygon/cdk/log" + "github.com/0xPolygon/cdk/sequencesender/seqsendertypes" + "github.com/ethereum/go-ethereum/common" +) + +type NewSequenceConditionalNumBatches struct { + maxBatchesForL1 uint64 // cfg.MaxBatchesForL1 +} + +func (c *NewSequenceConditionalNumBatches) NewSequenceIfWorthToSend(ctx context.Context, txBuilder TxBuilder, sequenceBatches []seqsendertypes.Batch, senderAddress, l2Coinbase common.Address, batchNumber uint64) (seqsendertypes.Sequence, error) { + if len(sequenceBatches) >= int(c.maxBatchesForL1) { + log.Infof( + "[SeqSender] sequence should be sent to L1, because MaxBatchesForL1 (%d) has been reached", + c.maxBatchesForL1, + ) + return txBuilder.NewSequence(sequenceBatches, l2Coinbase) + } + return nil, nil +} diff --git a/sequencesender/txbuilder/zkevm_cond_max_size.go b/sequencesender/txbuilder/zkevm_cond_max_size.go new file mode 100644 index 00000000..4a41f3d6 --- /dev/null +++ b/sequencesender/txbuilder/zkevm_cond_max_size.go @@ -0,0 +1,83 @@ +package txbuilder + +import ( + "context" + "errors" + + "github.com/0xPolygon/cdk/etherman" + "github.com/0xPolygon/cdk/log" + "github.com/0xPolygon/cdk/sequencesender/seqsendertypes" + "github.com/ethereum/go-ethereum/common" +) + +var ( + // ErrOversizedData when transaction input data is greater than a limit (DOS protection) + ErrOversizedData = errors.New("oversized data") +) + +type NewSequenceConditionalMaxSize struct { + maxTxSizeForL1 uint64 // cfg.MaxTxSizeForL1 +} + +func (c *NewSequenceConditionalMaxSize) NewSequenceIfWorthToSend(ctx context.Context, txBuilder TxBuilder, sequenceBatches []seqsendertypes.Batch, senderAddress, l2Coinbase common.Address, batchNumber uint64) (seqsendertypes.Sequence, error) { + sequence, err := txBuilder.NewSequence(sequenceBatches, l2Coinbase) + if err != nil { + return nil, err + } + + // Check if can be sent + tx, err := txBuilder.BuildSequenceBatchesTx(ctx, senderAddress, sequence) + if err == nil && tx.Size() > c.maxTxSizeForL1 { + log.Infof("[SeqSender] oversized Data on TX oldHash %s (txSize %d > %d)", tx.Hash(), tx.Size(), c.maxTxSizeForL1) + err = ErrOversizedData + } + + if err != nil { + log.Infof("[SeqSender] handling estimate gas send sequence error: %v", err) + sequenceBatches, err = handleEstimateGasSendSequenceErr(sequence.Batches(), batchNumber, err) + if sequenceBatches != nil { + // Handling the error gracefully, re-processing the sequence as a sanity check + //sequence, err = s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) + sequence, err = txBuilder.NewSequence(sequenceBatches, l2Coinbase) + if err != nil { + return nil, err + } + + _, err = txBuilder.BuildSequenceBatchesTx(ctx, senderAddress, sequence) + return sequence, err + } + + return sequence, err + } + return nil, nil +} + +// handleEstimateGasSendSequenceErr handles an error on the estimate gas. Results: (nil,nil)=requires waiting, (nil,error)=no handled gracefully, (seq,nil) handled gracefully +func handleEstimateGasSendSequenceErr(sequenceBatches []seqsendertypes.Batch, currentBatchNumToSequence uint64, err error) ([]seqsendertypes.Batch, error) { + // Insufficient allowance + if errors.Is(err, etherman.ErrInsufficientAllowance) { + return nil, err + } + if isDataForEthTxTooBig(err) { + // Remove the latest item and send the sequences + log.Infof("Done building sequences, selected batches to %d. Batch %d caused the L1 tx to be too big: %v", currentBatchNumToSequence-1, currentBatchNumToSequence, err) + } else { + // Remove the latest item and send the sequences + log.Infof("Done building sequences, selected batches to %d. Batch %d excluded due to unknown error: %v", currentBatchNumToSequence, currentBatchNumToSequence+1, err) + } + + if len(sequenceBatches) > 1 { + sequenceBatches = sequenceBatches[:len(sequenceBatches)-1] + } else { + sequenceBatches = nil + } + + return sequenceBatches, nil +} + +// isDataForEthTxTooBig checks if tx oversize error +func isDataForEthTxTooBig(err error) bool { + return errors.Is(err, etherman.ErrGasRequiredExceedsAllowance) || + errors.Is(err, ErrOversizedData) || + errors.Is(err, etherman.ErrContentLengthTooLarge) +} diff --git a/test/config/test.config.toml b/test/config/test.config.toml index f4b55677..9cc1ef0b 100644 --- a/test/config/test.config.toml +++ b/test/config/test.config.toml @@ -1,5 +1,8 @@ -[SequenceSender] +[Common] IsValidiumMode = false +ContractVersions = "banana" + +[SequenceSender] WaitPeriodSendSequence = "15s" LastBatchVirtualizationTimeMaxWaitPeriod = "10s" L1BlockTimestampMargin = "30s" From d82fbc47e746f3392af264efe461bafe08ebdba9 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Wed, 17 Jul 2024 12:06:07 +0200 Subject: [PATCH 03/51] remove unused file --- etherman/contracts/router_rollup.go | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 etherman/contracts/router_rollup.go diff --git a/etherman/contracts/router_rollup.go b/etherman/contracts/router_rollup.go deleted file mode 100644 index 5f3be656..00000000 --- a/etherman/contracts/router_rollup.go +++ /dev/null @@ -1,6 +0,0 @@ -package contracts - -type ContractRollupRouter struct { - banana *ContractGlobalExitRootBanana - elderberry *ContractGlobalExitRootElderberry -} From 83b2418edef2481026d755317343506405b375d0 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Wed, 17 Jul 2024 12:08:11 +0200 Subject: [PATCH 04/51] renamed --- sequencesender/seqsendertypes/{batch.go => types.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sequencesender/seqsendertypes/{batch.go => types.go} (100%) diff --git a/sequencesender/seqsendertypes/batch.go b/sequencesender/seqsendertypes/types.go similarity index 100% rename from sequencesender/seqsendertypes/batch.go rename to sequencesender/seqsendertypes/types.go From 43d8bcd9ccf33972068b5fed15fee590c8e83d6f Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Wed, 17 Jul 2024 15:36:37 +0200 Subject: [PATCH 05/51] wip --- etherman/contracts/interfaces.go | 5 +++++ sequencesender/sequencesender.go | 4 ---- sequencesender/sequencesender_test.go | 3 ++- sequencesender/txbuilder/banana_base.go | 21 ++++++++++++++++++--- sequencesender/txbuilder/banana_validium.go | 6 +++++- sequencesender/txbuilder/banana_zkevm.go | 6 +++++- 6 files changed, 35 insertions(+), 10 deletions(-) diff --git a/etherman/contracts/interfaces.go b/etherman/contracts/interfaces.go index a07f1b12..60ac67d1 100644 --- a/etherman/contracts/interfaces.go +++ b/etherman/contracts/interfaces.go @@ -2,6 +2,7 @@ package contracts import ( "errors" + "fmt" "math/big" "github.com/ethereum/go-ethereum/accounts/abi/bind" @@ -63,6 +64,10 @@ type RollupData struct { RollupCompatibilityID uint8 } +func (r *RollupData) String() string { + return fmt.Sprintf("RollupContract: %s, ChainID: %d, Verifier: %s, ForkID: %d, LastLocalExitRoot: %x, LastBatchSequenced: %d, LastVerifiedBatch: %d, LastPendingState: %d, LastPendingStateConsolidated: %d, LastVerifiedBatchBeforeUpgrade: %d, RollupTypeID: %d, RollupCompatibilityID: %d", r.RollupContract.String(), r.ChainID, r.Verifier.String(), r.ForkID, r.LastLocalExitRoot, r.LastBatchSequenced, r.LastVerifiedBatch, r.LastPendingState, r.LastPendingStateConsolidated, r.LastVerifiedBatchBeforeUpgrade, r.RollupTypeID, r.RollupCompatibilityID) +} + type StateVariablesSequencedBatchData struct { AccInputHash [32]byte SequencedTimestamp uint64 diff --git a/sequencesender/sequencesender.go b/sequencesender/sequencesender.go index 7bc0adf6..809ec086 100644 --- a/sequencesender/sequencesender.go +++ b/sequencesender/sequencesender.go @@ -467,15 +467,11 @@ func (s *SequenceSender) tryToSendSequence(ctx context.Context) { } // Send sequences to L1 - //sequenceCount := sequence.Len() - //firstSequence := sequence.Batches[0] firstSequence := sequence.FirstBatch() - //lastSequence := sequence.Batches[sequenceCount-1] lastSequence := sequence.LastBatch() lastL2BlockTimestamp := lastSequence.LastL2BLockTimestamp() log.Infof("[SeqSender] sending sequences to L1. From batch %d to batch %d", firstSequence.BatchNumber, lastSequence.BatchNumber) - //printSequenceBatches(sequence) log.Infof(sequence.String()) // Wait until last L1 block timestamp is L1BlockTimestampMargin seconds above the timestamp of the last L2 block in the sequence diff --git a/sequencesender/sequencesender_test.go b/sequencesender/sequencesender_test.go index 1c58f976..6df35b30 100644 --- a/sequencesender/sequencesender_test.go +++ b/sequencesender/sequencesender_test.go @@ -1,6 +1,7 @@ package sequencesender import ( + "context" "testing" "github.com/0xPolygon/cdk/log" @@ -66,7 +67,7 @@ func TestStreamTx(t *testing.T) { func TestKK(t *testing.T) { sut := &SequenceSender{} - sequences, err := sut.getSequencesToSend() + sequences, err := sut.getSequencesToSend(context.TODO()) require.NoError(t, err) require.NotNil(t, sequences) } diff --git a/sequencesender/txbuilder/banana_base.go b/sequencesender/txbuilder/banana_base.go index 5fd98947..5d6276c4 100644 --- a/sequencesender/txbuilder/banana_base.go +++ b/sequencesender/txbuilder/banana_base.go @@ -1,9 +1,12 @@ package txbuilder import ( + "fmt" + cdkcommon "github.com/0xPolygon/cdk/common" "github.com/0xPolygon/cdk/etherman" "github.com/0xPolygon/cdk/etherman/contracts" + "github.com/0xPolygon/cdk/log" "github.com/0xPolygon/cdk/sequencesender/seqsendertypes" "github.com/0xPolygon/cdk/state/datastream" "github.com/ethereum/go-ethereum/accounts/abi/bind" @@ -17,9 +20,21 @@ type TxBuilderBananaBase struct { SenderAddress common.Address } -func convertToSequenceBanana(sequences seqsendertypes.Sequence) etherman.SequenceBanana { - // TODO: Fill - return etherman.SequenceBanana{} +func convertToSequenceBanana(sequences seqsendertypes.Sequence) (etherman.SequenceBanana, error) { + seqEth, ok := sequences.(*BananaSequence) + if !ok { + log.Error("sequences is not a BananaSequence") + return etherman.SequenceBanana{}, fmt.Errorf("sequences is not a BananaSequence") + } + seqEth.SequenceBanana.Batches = make([]etherman.Batch, len(sequences.Batches())) + for _, batch := range sequences.Batches() { + ethBatch, err := convertToEthermanBatch(batch) + if err != nil { + return etherman.SequenceBanana{}, err + } + seqEth.SequenceBanana.Batches = append(seqEth.SequenceBanana.Batches, ethBatch) + } + return seqEth.SequenceBanana, nil } func convertToEthermanBatch(batch seqsendertypes.Batch) (etherman.Batch, error) { diff --git a/sequencesender/txbuilder/banana_validium.go b/sequencesender/txbuilder/banana_validium.go index 4eda6e56..fd844ec4 100644 --- a/sequencesender/txbuilder/banana_validium.go +++ b/sequencesender/txbuilder/banana_validium.go @@ -45,7 +45,11 @@ func (t *TxBuilderBananaValidium) BuildSequenceBatchesTx(ctx context.Context, se // Post sequences to DA backend var dataAvailabilityMessage []byte var err error - ethseq := convertToSequenceBanana(sequences) + ethseq, err := convertToSequenceBanana(sequences) + if err != nil { + log.Error("error converting sequences to etherman: ", err) + return nil, err + } dataAvailabilityMessage, err = t.da.PostSequence(ctx, ethseq) if err != nil { diff --git a/sequencesender/txbuilder/banana_zkevm.go b/sequencesender/txbuilder/banana_zkevm.go index 0201be24..9bf4d6a1 100644 --- a/sequencesender/txbuilder/banana_zkevm.go +++ b/sequencesender/txbuilder/banana_zkevm.go @@ -38,7 +38,11 @@ func (t *TxBuilderBananaZKEVM) NewSequenceIfWorthToSend(ctx context.Context, seq func (t *TxBuilderBananaZKEVM) BuildSequenceBatchesTx(ctx context.Context, sender common.Address, sequences seqsendertypes.Sequence) (*ethtypes.Transaction, error) { var err error - ethseq := convertToSequenceBanana(sequences) + ethseq, err := convertToSequenceBanana(sequences) + if err != nil { + log.Error("error converting sequences to etherman: ", err) + return nil, err + } newopts := t.opts newopts.NoSend = true From 187efc6df0c65645d1d0a70fd144f5f05df4c226 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 18 Jul 2024 12:11:57 +0200 Subject: [PATCH 06/51] add errors --- etherman/contracts.go | 14 ++++++++++++++ etherman/contracts/base.go | 2 ++ etherman/etherman.go | 27 ++++++++++++--------------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/etherman/contracts.go b/etherman/contracts.go index 54315cf5..1506e36f 100644 --- a/etherman/contracts.go +++ b/etherman/contracts.go @@ -13,6 +13,11 @@ type ContractsBanana struct { ZkEVM *contracts.ContractRollupBanana } +func (c *ContractsBanana) String() string { + return "RollupManager: " + c.RollupManager.String() + "\nGlobalExitRoot: " + c.GlobalExitRoot.String() + "\nZkEVM: " + c.ZkEVM.String() + +} + func newContractsBanana(cfg L1Config, backend bind.ContractBackend) (*ContractsBanana, error) { globalExitRoot, err := contracts.NewContractGlobalExitRootBanana(cfg.GlobalExitRootManagerAddr, backend) @@ -45,6 +50,11 @@ type ContractsElderberry struct { ZkEVM *contracts.ContractRollupElderberry } +func (c *ContractsElderberry) String() string { + return "RollupManager: " + c.RollupManager.String() + "\nGlobalExitRoot: " + c.GlobalExitRoot.String() + "\nZkEVM: " + c.ZkEVM.String() + +} + func newContractsElderberry(cfg L1Config, backend bind.ContractBackend) (*ContractsElderberry, error) { globalExitRoot, err := contracts.NewContractGlobalExitRootElderberry(cfg.GlobalExitRootManagerAddr, backend) @@ -74,6 +84,10 @@ type Contracts struct { contractVersion contracts.VersionType } +func (c *Contracts) String() string { + return "default_contract_version: " + string(c.contractVersion) + "\n Banana: \n" + c.Banana.String() + "\n Elderberry:\n" + c.Elderberry.String() +} + func NewContracts(cfg L1Config, backend bind.ContractBackend, defaultVersion contracts.VersionType) (*Contracts, error) { banana, err := newContractsBanana(cfg, backend) if err != nil { diff --git a/etherman/contracts/base.go b/etherman/contracts/base.go index 666bf5ff..39b9230a 100644 --- a/etherman/contracts/base.go +++ b/etherman/contracts/base.go @@ -1,6 +1,7 @@ package contracts import ( + "github.com/0xPolygon/cdk/log" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" ) @@ -18,6 +19,7 @@ func NewContractBase[T any](constructor contractConstructorFunc[T], address comm name NameType, version VersionType) (*ContractBase[T], error) { contractBind, err := constructor(address, backend) if err != nil { + log.Errorf("failed to bind contract %s at address %s. Err:%w", name, address.String(), err) return nil, err } diff --git a/etherman/etherman.go b/etherman/etherman.go index 2c877463..1364206d 100644 --- a/etherman/etherman.go +++ b/etherman/etherman.go @@ -11,7 +11,6 @@ import ( "time" "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/idataavailabilityprotocol" - "github.com/0xPolygon/cdk-contracts-tooling/contracts/etrog/polygonzkevmglobalexitrootv2" cdkcommon "github.com/0xPolygon/cdk/common" "github.com/0xPolygon/cdk/etherman/contracts" "github.com/0xPolygon/cdk/log" @@ -76,9 +75,8 @@ type L1Config struct { // Client is a simple implementation of EtherMan. type Client struct { - EthClient ethereumClient - DAProtocol *idataavailabilityprotocol.Idataavailabilityprotocol - GlobalExitRoot *polygonzkevmglobalexitrootv2.Polygonzkevmglobalexitrootv2 + EthClient ethereumClient + DAProtocol *idataavailabilityprotocol.Idataavailabilityprotocol Contracts *Contracts RollupID uint32 @@ -97,16 +95,15 @@ func NewClient(cfg Config, l1Config L1Config, commonConfig cdkcommon.Config) (*C return nil, err } - globalExitRoot, err := polygonzkevmglobalexitrootv2.NewPolygonzkevmglobalexitrootv2(l1Config.GlobalExitRootManagerAddr, ethClient) + contracts, err := NewContracts(l1Config, ethClient, contracts.VersionType(commonConfig.ContractVersions)) if err != nil { return nil, err } - - contracts, err := NewContracts(l1Config, ethClient, contracts.VersionType(commonConfig.ContractVersions)) - + log.Info(contracts.String()) // Get RollupID rollupID, err := contracts.RollupManager(nil).RollupAddressToID(l1Config.ZkEVMAddr) if err != nil { + log.Errorf("error getting rollupID from %s : %+v", contracts.RollupManager(nil).String(), err) return nil, err } if rollupID == 0 { @@ -115,13 +112,13 @@ func NewClient(cfg Config, l1Config L1Config, commonConfig cdkcommon.Config) (*C log.Debug("rollupID: ", rollupID) client := &Client{ - EthClient: ethClient, - Contracts: contracts, - GlobalExitRoot: globalExitRoot, - RollupID: rollupID, - l1Cfg: l1Config, - cfg: cfg, - auth: map[common.Address]bind.TransactOpts{}, + EthClient: ethClient, + Contracts: contracts, + + RollupID: rollupID, + l1Cfg: l1Config, + cfg: cfg, + auth: map[common.Address]bind.TransactOpts{}, } if commonConfig.IsValidiumMode { From 9925aa82c4a2cf922c5c57fd68dd90ecaf1b7747 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Fri, 19 Jul 2024 17:02:09 +0200 Subject: [PATCH 07/51] revert multi-fork for etherman and remove wrappers for contracts --- cmd/run.go | 11 +- config/config.go | 4 +- config/network.go | 6 +- etherman/aggregator.go | 4 +- etherman/config.go | 14 -- etherman/config/config.go | 31 ++++ etherman/contracts.go | 152 ------------------ etherman/contracts/banana_daprotocol.go | 21 --- etherman/contracts/banana_globalexitroot.go | 59 ------- etherman/contracts/banana_rollup.go | 38 ----- etherman/contracts/banana_rollupmanager.go | 69 -------- etherman/contracts/base.go | 16 +- etherman/contracts/contracts.go | 32 ++++ etherman/contracts/contracts_banana.go | 46 ++++++ etherman/contracts/contracts_elderberry.go | 46 ++++++ etherman/contracts/elderberry_daprotocol.go | 21 --- .../contracts/elderberry_globalexitroot.go | 30 ---- etherman/contracts/elderberry_rollup.go | 38 ----- .../contracts/elderberry_rollupmanager.go | 69 -------- etherman/contracts/interfaces.go | 6 +- etherman/etherman.go | 71 ++++---- sequencesender/sequencesender.go | 2 +- sequencesender/txbuilder/banana_base.go | 45 +++++- sequencesender/txbuilder/banana_validium.go | 13 +- sequencesender/txbuilder/banana_zkevm.go | 10 +- sequencesender/txbuilder/elderberry_zkevm.go | 14 +- 26 files changed, 271 insertions(+), 597 deletions(-) delete mode 100644 etherman/config.go create mode 100644 etherman/config/config.go delete mode 100644 etherman/contracts.go delete mode 100644 etherman/contracts/banana_daprotocol.go delete mode 100644 etherman/contracts/banana_globalexitroot.go delete mode 100644 etherman/contracts/banana_rollup.go delete mode 100644 etherman/contracts/banana_rollupmanager.go create mode 100644 etherman/contracts/contracts.go create mode 100644 etherman/contracts/contracts_banana.go create mode 100644 etherman/contracts/contracts_elderberry.go delete mode 100644 etherman/contracts/elderberry_daprotocol.go delete mode 100644 etherman/contracts/elderberry_globalexitroot.go delete mode 100644 etherman/contracts/elderberry_rollup.go delete mode 100644 etherman/contracts/elderberry_rollupmanager.go diff --git a/cmd/run.go b/cmd/run.go index 71e5a448..1bd65330 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -16,6 +16,7 @@ import ( "github.com/0xPolygon/cdk/dataavailability" "github.com/0xPolygon/cdk/dataavailability/datacommittee" "github.com/0xPolygon/cdk/etherman" + ethermanconfig "github.com/0xPolygon/cdk/etherman/config" "github.com/0xPolygon/cdk/etherman/contracts" "github.com/0xPolygon/cdk/log" "github.com/0xPolygon/cdk/sequencesender" @@ -113,7 +114,7 @@ func createAggregator(ctx context.Context, c config.Config, runMigrations bool) } func createSequenceSender(cfg config.Config) *sequencesender.SequenceSender { - ethman, err := etherman.NewClient(etherman.Config{ + ethman, err := etherman.NewClient(ethermanconfig.Config{ EthermanConfig: ethtxman.Config{ URL: cfg.SequenceSender.EthTxManager.Etherman.URL, MultiGasProvider: cfg.SequenceSender.EthTxManager.Etherman.MultiGasProvider, @@ -161,16 +162,16 @@ func newTxBuilder(cfg config.Config, ethman *etherman.Client) (txbuilder.TxBuild switch contracts.VersionType(cfg.Common.ContractVersions) { case contracts.VersionBanana: if cfg.Common.IsValidiumMode { - txBuilder = txbuilder.NewTxBuilderBananaValidium(*ethman.Contracts.Banana.ZkEVM, da, *auth, auth.From, cfg.SequenceSender.MaxBatchesForL1) + txBuilder = txbuilder.NewTxBuilderBananaValidium(ethman.Contracts.Banana.Rollup, ethman.Contracts.Banana.GlobalExitRoot, da, *auth, auth.From, cfg.SequenceSender.MaxBatchesForL1) } else { - txBuilder = txbuilder.NewTxBuilderBananaZKEVM(*ethman.Contracts.Banana.ZkEVM, *auth, auth.From, cfg.SequenceSender.MaxTxSizeForL1) + txBuilder = txbuilder.NewTxBuilderBananaZKEVM(ethman.Contracts.Banana.Rollup, ethman.Contracts.Banana.GlobalExitRoot, *auth, auth.From, cfg.SequenceSender.MaxTxSizeForL1) } case contracts.VersionElderberry: if cfg.Common.IsValidiumMode { err = fmt.Errorf("Elderberry+Validium not implemented yet") //txBuilder = txbuilder.NewTxBuilderElderberryValidium(*ethman.Contracts.Elderberry.ZkEVM, da, *auth, auth.From) } else { - txBuilder = txbuilder.NewTxBuilderElderberryZKEVM(*ethman.Contracts.Elderberry.ZkEVM, *auth, auth.From, cfg.SequenceSender.MaxTxSizeForL1) + txBuilder = txbuilder.NewTxBuilderElderberryZKEVM(ethman.Contracts.Elderberry.Rollup, *auth, auth.From, cfg.SequenceSender.MaxTxSizeForL1) } default: err = fmt.Errorf("unknown contract version: %s", cfg.Common.ContractVersions) @@ -241,7 +242,7 @@ func runMigrations(c db.Config, name string) { } func newEtherman(c config.Config) (*etherman.Client, error) { - config := etherman.Config{ + config := ethermanconfig.Config{ URL: c.Aggregator.EthTxManager.Etherman.URL, } return etherman.NewClient(config, c.NetworkConfig.L1Config, c.Common) diff --git a/config/config.go b/config/config.go index e18dff10..02ede0fb 100644 --- a/config/config.go +++ b/config/config.go @@ -7,7 +7,7 @@ import ( "github.com/0xPolygon/cdk/aggregator" "github.com/0xPolygon/cdk/common" - "github.com/0xPolygon/cdk/etherman" + ethermanconfig "github.com/0xPolygon/cdk/etherman/config" "github.com/0xPolygon/cdk/log" "github.com/0xPolygon/cdk/sequencesender" "github.com/0xPolygonHermez/zkevm-ethtx-manager/ethtxmanager" @@ -56,7 +56,7 @@ You could find some examples: */ type Config struct { // Configuration of the etherman (client for access L1) - Etherman etherman.Config + Etherman ethermanconfig.Config // Configuration for ethereum transaction manager EthTxManager ethtxmanager.Config // Configuration of the aggregator diff --git a/config/network.go b/config/network.go index cbdef441..ed3b34d4 100644 --- a/config/network.go +++ b/config/network.go @@ -7,7 +7,7 @@ import ( "io" "os" - "github.com/0xPolygon/cdk/etherman" + ethermanconfig "github.com/0xPolygon/cdk/etherman/config" "github.com/0xPolygon/cdk/log" "github.com/0xPolygon/cdk/state" "github.com/ethereum/go-ethereum/common" @@ -17,7 +17,7 @@ import ( // NetworkConfig is the configuration struct for the different environments type NetworkConfig struct { // L1: Configuration related to L1 - L1Config etherman.L1Config `json:"l1Config"` + L1Config ethermanconfig.L1Config `json:"l1Config"` // L1: Genesis of the rollup, first block number and root Genesis state.Genesis } @@ -48,7 +48,7 @@ type GenesisFromJSON struct { // L2: List of states contracts used to populate merkle tree at initial state Genesis []genesisAccountFromJSON `json:"genesis"` // L1: configuration of the network - L1Config etherman.L1Config + L1Config ethermanconfig.L1Config } type genesisAccountFromJSON struct { diff --git a/etherman/aggregator.go b/etherman/aggregator.go index bf0f326f..ed8d3575 100644 --- a/etherman/aggregator.go +++ b/etherman/aggregator.go @@ -41,7 +41,7 @@ func (etherMan *Client) BuildTrustedVerifyBatchesTxData(lastVerifiedBatch, newVe const pendStateNum = 0 // TODO hardcoded for now until we implement the pending state feature - tx, err := etherMan.Contracts.RollupManager(nil).VerifyBatchesTrustedAggregator( + tx, err := etherMan.Contracts.Banana.RollupManager.Contract().VerifyBatchesTrustedAggregator( &opts, etherMan.RollupID, pendStateNum, @@ -64,7 +64,7 @@ func (etherMan *Client) BuildTrustedVerifyBatchesTxData(lastVerifiedBatch, newVe // GetBatchAccInputHash gets the batch accumulated input hash from the ethereum func (etherman *Client) GetBatchAccInputHash(ctx context.Context, batchNumber uint64) (common.Hash, error) { - rollupData, err := etherman.Contracts.RollupManager(nil).GetRollupSequencedBatches(etherman.RollupID, batchNumber) + rollupData, err := etherman.Contracts.Banana.RollupManager.Contract().GetRollupSequencedBatches(&bind.CallOpts{Pending: false}, etherman.RollupID, batchNumber) if err != nil { return common.Hash{}, err } diff --git a/etherman/config.go b/etherman/config.go deleted file mode 100644 index 1c50cd70..00000000 --- a/etherman/config.go +++ /dev/null @@ -1,14 +0,0 @@ -package etherman - -import "github.com/0xPolygonHermez/zkevm-ethtx-manager/etherman" - -// Config represents the configuration of the etherman -type Config struct { - // URL is the URL of the Ethereum node for L1 - URL string `mapstructure:"URL"` - - EthermanConfig etherman.Config - - // ForkIDChunkSize is the max interval for each call to L1 provider to get the forkIDs - ForkIDChunkSize uint64 `mapstructure:"ForkIDChunkSize"` -} diff --git a/etherman/config/config.go b/etherman/config/config.go new file mode 100644 index 00000000..c9208ee4 --- /dev/null +++ b/etherman/config/config.go @@ -0,0 +1,31 @@ +package config + +import ( + "github.com/0xPolygonHermez/zkevm-ethtx-manager/etherman" + "github.com/ethereum/go-ethereum/common" +) + +// Config represents the configuration of the etherman +type Config struct { + // URL is the URL of the Ethereum node for L1 + URL string `mapstructure:"URL"` + + EthermanConfig etherman.Config + + // ForkIDChunkSize is the max interval for each call to L1 provider to get the forkIDs + ForkIDChunkSize uint64 `mapstructure:"ForkIDChunkSize"` +} + +// L1Config represents the configuration of the network used in L1 +type L1Config struct { + // Chain ID of the L1 network + L1ChainID uint64 `json:"chainId"` + // ZkEVMAddr Address of the L1 contract polygonZkEVMAddress + ZkEVMAddr common.Address `json:"polygonZkEVMAddress"` + // RollupManagerAddr Address of the L1 contract + RollupManagerAddr common.Address `json:"polygonRollupManagerAddress"` + // PolAddr Address of the L1 Pol token Contract + PolAddr common.Address `json:"polTokenAddress"` + // GlobalExitRootManagerAddr Address of the L1 GlobalExitRootManager contract + GlobalExitRootManagerAddr common.Address `json:"polygonZkEVMGlobalExitRootAddress"` +} diff --git a/etherman/contracts.go b/etherman/contracts.go deleted file mode 100644 index 1506e36f..00000000 --- a/etherman/contracts.go +++ /dev/null @@ -1,152 +0,0 @@ -package etherman - -import ( - "github.com/0xPolygon/cdk/etherman/contracts" - "github.com/0xPolygon/cdk/log" - "github.com/ethereum/go-ethereum/accounts/abi/bind" -) - -type ContractsBanana struct { - //DAProtocol *ContractDAProtocolBanana - GlobalExitRoot *contracts.ContractGlobalExitRootBanana - RollupManager *contracts.ContractRollupManangerBanana - ZkEVM *contracts.ContractRollupBanana -} - -func (c *ContractsBanana) String() string { - return "RollupManager: " + c.RollupManager.String() + "\nGlobalExitRoot: " + c.GlobalExitRoot.String() + "\nZkEVM: " + c.ZkEVM.String() - -} - -func newContractsBanana(cfg L1Config, backend bind.ContractBackend) (*ContractsBanana, error) { - - globalExitRoot, err := contracts.NewContractGlobalExitRootBanana(cfg.GlobalExitRootManagerAddr, backend) - if err != nil { - return nil, err - } - - rollupManager, err := contracts.NewContractRollupManangerBanana(cfg.RollupManagerAddr, backend) - if err != nil { - return nil, err - } - - zkEVM, err := contracts.NewContractRollupBanana(cfg.ZkEVMAddr, backend) - if err != nil { - return nil, err - } - - return &ContractsBanana{ - GlobalExitRoot: globalExitRoot, - RollupManager: rollupManager, - ZkEVM: zkEVM, - }, nil -} - -type ContractsElderberry struct { - - //DAProtocol *ContractDAProtocolBanana - RollupManager *contracts.ContractRollupManangerElderberry - GlobalExitRoot *contracts.ContractGlobalExitRootElderberry - ZkEVM *contracts.ContractRollupElderberry -} - -func (c *ContractsElderberry) String() string { - return "RollupManager: " + c.RollupManager.String() + "\nGlobalExitRoot: " + c.GlobalExitRoot.String() + "\nZkEVM: " + c.ZkEVM.String() - -} - -func newContractsElderberry(cfg L1Config, backend bind.ContractBackend) (*ContractsElderberry, error) { - - globalExitRoot, err := contracts.NewContractGlobalExitRootElderberry(cfg.GlobalExitRootManagerAddr, backend) - if err != nil { - return nil, err - } - rollupManager, err := contracts.NewContractRollupManangerElderberry(cfg.RollupManagerAddr, backend) - if err != nil { - return nil, err - } - - zkEVM, err := contracts.NewContractRollupElderberry(cfg.ZkEVMAddr, backend) - if err != nil { - return nil, err - } - - return &ContractsElderberry{ - GlobalExitRoot: globalExitRoot, - RollupManager: rollupManager, - ZkEVM: zkEVM, - }, nil -} - -type Contracts struct { - Banana ContractsBanana - Elderberry ContractsElderberry - contractVersion contracts.VersionType -} - -func (c *Contracts) String() string { - return "default_contract_version: " + string(c.contractVersion) + "\n Banana: \n" + c.Banana.String() + "\n Elderberry:\n" + c.Elderberry.String() -} - -func NewContracts(cfg L1Config, backend bind.ContractBackend, defaultVersion contracts.VersionType) (*Contracts, error) { - banana, err := newContractsBanana(cfg, backend) - if err != nil { - return nil, err - } - - elderberry, err := newContractsElderberry(cfg, backend) - if err != nil { - return nil, err - } - - return &Contracts{ - Banana: *banana, - Elderberry: *elderberry, - contractVersion: defaultVersion, - }, nil -} - -func (c *Contracts) RollupManager(specificVersion *contracts.VersionType) contracts.RollupManagerContractor { - useVersion := c.contractVersion - if specificVersion != nil { - useVersion = *specificVersion - } - if useVersion == contracts.VersionBanana { - return c.Banana.RollupManager - } else if useVersion == contracts.VersionElderberry { - return c.Elderberry.RollupManager - } else { - log.Errorf("RollupManager unknown version %s", useVersion) - return nil - } -} - -func (c *Contracts) GlobalExitRoot(specificVersion *contracts.VersionType) contracts.GlobalExitRootContractor { - useVersion := c.contractVersion - if specificVersion != nil { - useVersion = *specificVersion - } - if useVersion == contracts.VersionBanana { - return c.Banana.GlobalExitRoot - } else if useVersion == contracts.VersionElderberry { - return c.Elderberry.GlobalExitRoot - } else { - log.Errorf("RollupManager unknown version %s", useVersion) - return nil - } -} - -func (c *Contracts) ZkEVM(specificVersion *contracts.VersionType) contracts.RollupContractor { - useVersion := c.contractVersion - if specificVersion != nil { - useVersion = *specificVersion - } - if useVersion == contracts.VersionBanana { - return c.Banana.ZkEVM - } else if useVersion == contracts.VersionElderberry { - return c.Elderberry.ZkEVM - } else { - log.Errorf("ZkEVM unknown version %s", useVersion) - return nil - } -} diff --git a/etherman/contracts/banana_daprotocol.go b/etherman/contracts/banana_daprotocol.go deleted file mode 100644 index 03255dfb..00000000 --- a/etherman/contracts/banana_daprotocol.go +++ /dev/null @@ -1,21 +0,0 @@ -package contracts - -import ( - "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/idataavailabilityprotocol" - "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/common" -) - -type ContractDAProtocolBanana struct { - *ContractBase[idataavailabilityprotocol.Idataavailabilityprotocol] -} - -func NewContractDAProtocolBanana(address common.Address, backend bind.ContractBackend) (*ContractDAProtocolBanana, error) { - base, err := NewContractBase(idataavailabilityprotocol.NewIdataavailabilityprotocol, address, backend, ContractNameDAProtocol, VersionBanana) - if err != nil { - return nil, err - } - return &ContractDAProtocolBanana{ - ContractBase: base, - }, nil -} diff --git a/etherman/contracts/banana_globalexitroot.go b/etherman/contracts/banana_globalexitroot.go deleted file mode 100644 index 09eb7523..00000000 --- a/etherman/contracts/banana_globalexitroot.go +++ /dev/null @@ -1,59 +0,0 @@ -package contracts - -import ( - "fmt" - - "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonzkevmglobalexitrootv2" - "github.com/0xPolygon/cdk/log" - "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/common" -) - -type ContractGlobalExitRootBanana struct { - *ContractBase[polygonzkevmglobalexitrootv2.Polygonzkevmglobalexitrootv2] -} - -func NewContractGlobalExitRootBanana(address common.Address, backend bind.ContractBackend) (*ContractGlobalExitRootBanana, error) { - base, err := NewContractBase(polygonzkevmglobalexitrootv2.NewPolygonzkevmglobalexitrootv2, address, backend, ContractNameGlobalExitRoot, VersionBanana) - if err != nil { - return nil, err - } - return &ContractGlobalExitRootBanana{ - ContractBase: base, - }, nil -} - -func (e *ContractGlobalExitRootBanana) GetL1InfoRoot(indexL1InfoRoot uint32) (common.Hash, error) { - // Get lastL1InfoTreeRoot (if index==0 then root=0, no call is needed) - var ( - lastL1InfoTreeRoot common.Hash - err error - ) - - if indexL1InfoRoot > 0 { - lastL1InfoTreeRoot, err = e.GetContract().L1InfoRootMap(&bind.CallOpts{Pending: false}, indexL1InfoRoot) - if err != nil { - log.Errorf("error calling SC globalexitroot L1InfoLeafMap: %v", err) - } - } - - return lastL1InfoTreeRoot, err -} - -func (e *ContractGlobalExitRootBanana) L1InfoIndexToRoot(indexLeaf uint32) (common.Hash, error) { - var ( - lastL1InfoTreeRoot common.Hash - err error - ) - - if indexLeaf > 0 { - lastL1InfoTreeRoot, err = e.GetContract().L1InfoRootMap(&bind.CallOpts{Pending: false}, indexLeaf) - if err != nil { - errC := fmt.Errorf("error calling SC globalexitroot L1InfoIndexToRoot(%d) err: %w", indexLeaf, err) - log.Errorf("%v", errC) - return common.Hash{}, errC - } - } - - return lastL1InfoTreeRoot, err -} diff --git a/etherman/contracts/banana_rollup.go b/etherman/contracts/banana_rollup.go deleted file mode 100644 index 663426cd..00000000 --- a/etherman/contracts/banana_rollup.go +++ /dev/null @@ -1,38 +0,0 @@ -package contracts - -import ( - "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonvalidiumetrog" - "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/common" -) - -type ContractRollupBanana struct { - *ContractBase[polygonvalidiumetrog.Polygonvalidiumetrog] -} - -func NewContractRollupBanana(address common.Address, backend bind.ContractBackend) (*ContractRollupBanana, error) { - base, err := NewContractBase(polygonvalidiumetrog.NewPolygonvalidiumetrog, address, backend, ContractNameRollup, VersionBanana) - if err != nil { - return nil, err - } - return &ContractRollupBanana{ - ContractBase: base, - }, nil -} - -func (e *ContractRollupBanana) TrustedSequencer() (common.Address, error) { - return e.GetContract().TrustedSequencer(&bind.CallOpts{Pending: false}) -} - -// LastAccInputHash gets the last acc input hash from the SC -func (e *ContractRollupBanana) LastAccInputHash() (common.Hash, error) { - return e.GetContract().LastAccInputHash(&bind.CallOpts{Pending: false}) -} - -func (e *ContractRollupBanana) DataAvailabilityProtocol() (common.Address, error) { - return e.GetContract().DataAvailabilityProtocol(&bind.CallOpts{Pending: false}) -} - -func (e *ContractRollupBanana) TrustedSequencerURL() (string, error) { - return e.GetContract().TrustedSequencerURL(&bind.CallOpts{Pending: false}) -} diff --git a/etherman/contracts/banana_rollupmanager.go b/etherman/contracts/banana_rollupmanager.go deleted file mode 100644 index 85fdaaea..00000000 --- a/etherman/contracts/banana_rollupmanager.go +++ /dev/null @@ -1,69 +0,0 @@ -package contracts - -import ( - "math/big" - - "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonrollupmanager" - "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" -) - -type ContractRollupManangerBanana struct { - *ContractBase[polygonrollupmanager.Polygonrollupmanager] -} - -func NewContractRollupManangerBanana(address common.Address, backend bind.ContractBackend) (*ContractRollupManangerBanana, error) { - base, err := NewContractBase(polygonrollupmanager.NewPolygonrollupmanager, address, backend, ContractNameRollupManager, VersionBanana) - if err != nil { - return nil, err - } - return &ContractRollupManangerBanana{ - ContractBase: base, - }, nil -} - -func (e *ContractRollupManangerBanana) RollupAddressToID(rollupAddress common.Address) (uint32, error) { - return e.GetContract().RollupAddressToID(&bind.CallOpts{Pending: false}, rollupAddress) -} - -func (e *ContractRollupManangerBanana) RollupIDToRollupData(rollupID uint32) (*RollupData, error) { - rollupData, err := e.GetContract().RollupIDToRollupData(&bind.CallOpts{Pending: false}, rollupID) - if err != nil { - return nil, err - } - return &RollupData{ - RollupContract: rollupData.RollupContract, - ChainID: rollupData.ChainID, - Verifier: rollupData.Verifier, - ForkID: rollupData.ForkID, - LastLocalExitRoot: rollupData.LastLocalExitRoot, - LastBatchSequenced: rollupData.LastBatchSequenced, - LastVerifiedBatch: rollupData.LastVerifiedBatch, - LastPendingState: rollupData.LastPendingState, - LastPendingStateConsolidated: rollupData.LastPendingStateConsolidated, - LastVerifiedBatchBeforeUpgrade: rollupData.LastVerifiedBatchBeforeUpgrade, - RollupTypeID: rollupData.RollupTypeID, - RollupCompatibilityID: rollupData.RollupCompatibilityID, - }, nil -} - -func (e *ContractRollupManangerBanana) GetBatchFee() (*big.Int, error) { - return e.GetContract().GetBatchFee(&bind.CallOpts{Pending: false}) -} - -func (e *ContractRollupManangerBanana) VerifyBatchesTrustedAggregator(opts *bind.TransactOpts, rollupID uint32, pendingStateNum uint64, initNumBatch uint64, finalNewBatch uint64, newLocalExitRoot [32]byte, newStateRoot [32]byte, beneficiary common.Address, proof [24][32]byte) (*types.Transaction, error) { - return e.GetContract().VerifyBatchesTrustedAggregator(opts, rollupID, pendingStateNum, initNumBatch, finalNewBatch, newLocalExitRoot, newStateRoot, beneficiary, proof) -} - -func (e *ContractRollupManangerBanana) GetRollupSequencedBatches(rollupID uint32, batchNum uint64) (StateVariablesSequencedBatchData, error) { - res, err := e.GetContract().GetRollupSequencedBatches(&bind.CallOpts{Pending: false}, rollupID, batchNum) - if err != nil { - return StateVariablesSequencedBatchData{}, err - } - return StateVariablesSequencedBatchData{ - AccInputHash: res.AccInputHash, - SequencedTimestamp: res.SequencedTimestamp, - PreviousLastBatchSequenced: res.PreviousLastBatchSequenced, - }, nil -} diff --git a/etherman/contracts/base.go b/etherman/contracts/base.go index 39b9230a..ebe17711 100644 --- a/etherman/contracts/base.go +++ b/etherman/contracts/base.go @@ -7,7 +7,7 @@ import ( ) type ContractBase[T any] struct { - contractBind *T + contract *T address common.Address contractName NameType version VersionType @@ -24,29 +24,29 @@ func NewContractBase[T any](constructor contractConstructorFunc[T], address comm } return &ContractBase[T]{ - contractBind: contractBind, + contract: contractBind, address: address, contractName: name, version: version, }, nil } -func (e *ContractBase[T]) GetContract() *T { - return e.contractBind +func (e *ContractBase[T]) Contract() *T { + return e.contract } -func (e *ContractBase[T]) GetAddress() common.Address { +func (e *ContractBase[T]) Address() common.Address { return e.address } -func (e *ContractBase[T]) GetName() string { +func (e *ContractBase[T]) Name() string { return string(e.contractName) } -func (e *ContractBase[T]) GetVersion() string { +func (e *ContractBase[T]) Version() string { return string(e.version) } func (e *ContractBase[T]) String() string { - return e.GetVersion() + "/" + e.GetName() + "@" + e.address.String() + return e.Version() + "/" + e.Name() + "@" + e.Address().String() } diff --git a/etherman/contracts/contracts.go b/etherman/contracts/contracts.go new file mode 100644 index 00000000..77aff05b --- /dev/null +++ b/etherman/contracts/contracts.go @@ -0,0 +1,32 @@ +package contracts + +import ( + "github.com/0xPolygon/cdk/etherman/config" + "github.com/ethereum/go-ethereum/accounts/abi/bind" +) + +type Contracts struct { + Banana ContractsBanana + Elderberry ContractsElderberry +} + +func NewContracts(cfg config.L1Config, backend bind.ContractBackend) (*Contracts, error) { + banana, err := NewContractsBanana(cfg, backend) + if err != nil { + return nil, err + } + + elderberry, err := NewContractsElderberry(cfg, backend) + if err != nil { + return nil, err + } + + return &Contracts{ + Banana: *banana, + Elderberry: *elderberry, + }, nil +} + +func (c *Contracts) String() string { + return " Banana: \n" + c.Banana.String() + "\n Elderberry:\n" + c.Elderberry.String() +} diff --git a/etherman/contracts/contracts_banana.go b/etherman/contracts/contracts_banana.go new file mode 100644 index 00000000..d01e9782 --- /dev/null +++ b/etherman/contracts/contracts_banana.go @@ -0,0 +1,46 @@ +package contracts + +import ( + "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonrollupmanager" + "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonvalidiumetrog" + "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonzkevmglobalexitrootv2" + "github.com/0xPolygon/cdk/etherman/config" + "github.com/ethereum/go-ethereum/accounts/abi/bind" +) + +type GlobalExitRootBananaType = ContractBase[polygonzkevmglobalexitrootv2.Polygonzkevmglobalexitrootv2] +type RollupBananaType = ContractBase[polygonvalidiumetrog.Polygonvalidiumetrog] +type RollupManagerBananaType = ContractBase[polygonrollupmanager.Polygonrollupmanager] + +type ContractsBanana struct { + GlobalExitRoot GlobalExitRootBananaType + Rollup RollupBananaType + RollupManager RollupManagerBananaType +} + +func NewContractsBanana(cfg config.L1Config, backend bind.ContractBackend) (*ContractsBanana, error) { + ger, err := NewContractBase(polygonzkevmglobalexitrootv2.NewPolygonzkevmglobalexitrootv2, cfg.GlobalExitRootManagerAddr, backend, ContractNameGlobalExitRoot, VersionBanana) + if err != nil { + return nil, err + } + rollup, err := NewContractBase(polygonvalidiumetrog.NewPolygonvalidiumetrog, cfg.ZkEVMAddr, backend, ContractNameRollup, VersionBanana) + if err != nil { + return nil, err + } + + rollupManager, err := NewContractBase(polygonrollupmanager.NewPolygonrollupmanager, cfg.RollupManagerAddr, backend, ContractNameRollupManager, VersionBanana) + if err != nil { + return nil, err + } + + return &ContractsBanana{ + GlobalExitRoot: *ger, + Rollup: *rollup, + RollupManager: *rollupManager, + }, nil +} + +func (c *ContractsBanana) String() string { + return "RollupManager: " + c.RollupManager.String() + "\nGlobalExitRoot: " + c.GlobalExitRoot.String() + "\nRollup: " + c.Rollup.String() + +} diff --git a/etherman/contracts/contracts_elderberry.go b/etherman/contracts/contracts_elderberry.go new file mode 100644 index 00000000..4127ff11 --- /dev/null +++ b/etherman/contracts/contracts_elderberry.go @@ -0,0 +1,46 @@ +package contracts + +import ( + "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry/polygonrollupmanager" + "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry/polygonvalidiumetrog" + "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry/polygonzkevmglobalexitrootv2" + "github.com/0xPolygon/cdk/etherman/config" + "github.com/ethereum/go-ethereum/accounts/abi/bind" +) + +type GlobalExitRootElderberryType = ContractBase[polygonzkevmglobalexitrootv2.Polygonzkevmglobalexitrootv2] +type RollupElderberryType = ContractBase[polygonvalidiumetrog.Polygonvalidiumetrog] +type RollupManagerElderberryType = ContractBase[polygonrollupmanager.Polygonrollupmanager] + +type ContractsElderberry struct { + GlobalExitRoot GlobalExitRootElderberryType + Rollup RollupElderberryType + RollupManager RollupManagerElderberryType +} + +func NewContractsElderberry(cfg config.L1Config, backend bind.ContractBackend) (*ContractsElderberry, error) { + ger, err := NewContractBase(polygonzkevmglobalexitrootv2.NewPolygonzkevmglobalexitrootv2, cfg.GlobalExitRootManagerAddr, backend, ContractNameGlobalExitRoot, VersionElderberry) + if err != nil { + return nil, err + } + rollup, err := NewContractBase(polygonvalidiumetrog.NewPolygonvalidiumetrog, cfg.ZkEVMAddr, backend, ContractNameRollup, VersionElderberry) + if err != nil { + return nil, err + } + + rollupManager, err := NewContractBase(polygonrollupmanager.NewPolygonrollupmanager, cfg.RollupManagerAddr, backend, ContractNameRollupManager, VersionElderberry) + if err != nil { + return nil, err + } + + return &ContractsElderberry{ + GlobalExitRoot: *ger, + Rollup: *rollup, + RollupManager: *rollupManager, + }, nil +} + +func (c *ContractsElderberry) String() string { + return "RollupManager: " + c.RollupManager.String() + "\nGlobalExitRoot: " + c.GlobalExitRoot.String() + "\nRollup: " + c.Rollup.String() + +} diff --git a/etherman/contracts/elderberry_daprotocol.go b/etherman/contracts/elderberry_daprotocol.go deleted file mode 100644 index 94b2fa36..00000000 --- a/etherman/contracts/elderberry_daprotocol.go +++ /dev/null @@ -1,21 +0,0 @@ -package contracts - -import ( - "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry/idataavailabilityprotocol" - "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/common" -) - -type ContractDAProtocolElderberry struct { - *ContractBase[idataavailabilityprotocol.Idataavailabilityprotocol] -} - -func NewContractDAProtocolElderberry(address common.Address, backend bind.ContractBackend) (*ContractDAProtocolElderberry, error) { - base, err := NewContractBase(idataavailabilityprotocol.NewIdataavailabilityprotocol, address, backend, ContractNameDAProtocol, VersionElderberry) - if err != nil { - return nil, err - } - return &ContractDAProtocolElderberry{ - ContractBase: base, - }, nil -} diff --git a/etherman/contracts/elderberry_globalexitroot.go b/etherman/contracts/elderberry_globalexitroot.go deleted file mode 100644 index 1dc21c0a..00000000 --- a/etherman/contracts/elderberry_globalexitroot.go +++ /dev/null @@ -1,30 +0,0 @@ -package contracts - -import ( - "fmt" - - "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry/polygonzkevmglobalexitrootv2" - "github.com/0xPolygon/cdk/log" - "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/common" -) - -type ContractGlobalExitRootElderberry struct { - *ContractBase[polygonzkevmglobalexitrootv2.Polygonzkevmglobalexitrootv2] -} - -func NewContractGlobalExitRootElderberry(address common.Address, backend bind.ContractBackend) (*ContractGlobalExitRootElderberry, error) { - base, err := NewContractBase(polygonzkevmglobalexitrootv2.NewPolygonzkevmglobalexitrootv2, address, backend, ContractNameGlobalExitRoot, VersionElderberry) - if err != nil { - return nil, err - } - return &ContractGlobalExitRootElderberry{ - ContractBase: base, - }, nil -} - -func (e *ContractGlobalExitRootElderberry) L1InfoIndexToRoot(indexLeaf uint32) (common.Hash, error) { - errC := fmt.Errorf("Contract %s doesn't implement L1InfoIndexToRoot. Err:%w", e.String(), ErrNotImplemented) - log.Errorf("%v", errC) - return common.Hash{}, errC -} diff --git a/etherman/contracts/elderberry_rollup.go b/etherman/contracts/elderberry_rollup.go deleted file mode 100644 index b071aac7..00000000 --- a/etherman/contracts/elderberry_rollup.go +++ /dev/null @@ -1,38 +0,0 @@ -package contracts - -import ( - "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry/polygonvalidiumetrog" - "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/common" -) - -type ContractRollupElderberry struct { - *ContractBase[polygonvalidiumetrog.Polygonvalidiumetrog] -} - -func NewContractRollupElderberry(address common.Address, backend bind.ContractBackend) (*ContractRollupElderberry, error) { - base, err := NewContractBase(polygonvalidiumetrog.NewPolygonvalidiumetrog, address, backend, ContractNameRollup, VersionElderberry) - if err != nil { - return nil, err - } - return &ContractRollupElderberry{ - ContractBase: base, - }, nil -} - -func (e *ContractRollupElderberry) TrustedSequencer() (common.Address, error) { - return e.GetContract().TrustedSequencer(&bind.CallOpts{Pending: false}) -} - -// LastAccInputHash gets the last acc input hash from the SC -func (e *ContractRollupElderberry) LastAccInputHash() (common.Hash, error) { - return e.GetContract().LastAccInputHash(&bind.CallOpts{Pending: false}) -} - -func (e *ContractRollupElderberry) DataAvailabilityProtocol() (common.Address, error) { - return e.GetContract().DataAvailabilityProtocol(&bind.CallOpts{Pending: false}) -} - -func (e *ContractRollupElderberry) TrustedSequencerURL() (string, error) { - return e.GetContract().TrustedSequencerURL(&bind.CallOpts{Pending: false}) -} diff --git a/etherman/contracts/elderberry_rollupmanager.go b/etherman/contracts/elderberry_rollupmanager.go deleted file mode 100644 index c50f8a75..00000000 --- a/etherman/contracts/elderberry_rollupmanager.go +++ /dev/null @@ -1,69 +0,0 @@ -package contracts - -import ( - "math/big" - - "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry/polygonrollupmanager" - "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" -) - -type ContractRollupManangerElderberry struct { - *ContractBase[polygonrollupmanager.Polygonrollupmanager] -} - -func NewContractRollupManangerElderberry(address common.Address, backend bind.ContractBackend) (*ContractRollupManangerElderberry, error) { - base, err := NewContractBase(polygonrollupmanager.NewPolygonrollupmanager, address, backend, ContractNameRollupManager, VersionElderberry) - if err != nil { - return nil, err - } - return &ContractRollupManangerElderberry{ - ContractBase: base, - }, nil -} - -func (e *ContractRollupManangerElderberry) RollupAddressToID(rollupAddress common.Address) (uint32, error) { - return e.GetContract().RollupAddressToID(&bind.CallOpts{Pending: false}, rollupAddress) -} - -func (e *ContractRollupManangerElderberry) RollupIDToRollupData(rollupID uint32) (*RollupData, error) { - rollupData, err := e.GetContract().RollupIDToRollupData(&bind.CallOpts{Pending: false}, rollupID) - if err != nil { - return nil, err - } - return &RollupData{ - RollupContract: rollupData.RollupContract, - ChainID: rollupData.ChainID, - Verifier: rollupData.Verifier, - ForkID: rollupData.ForkID, - LastLocalExitRoot: rollupData.LastLocalExitRoot, - LastBatchSequenced: rollupData.LastBatchSequenced, - LastVerifiedBatch: rollupData.LastVerifiedBatch, - LastPendingState: rollupData.LastPendingState, - LastPendingStateConsolidated: rollupData.LastPendingStateConsolidated, - LastVerifiedBatchBeforeUpgrade: rollupData.LastVerifiedBatchBeforeUpgrade, - RollupTypeID: rollupData.RollupTypeID, - RollupCompatibilityID: rollupData.RollupCompatibilityID, - }, nil -} - -func (e *ContractRollupManangerElderberry) GetBatchFee() (*big.Int, error) { - return e.GetContract().GetBatchFee(&bind.CallOpts{Pending: false}) -} - -func (e *ContractRollupManangerElderberry) VerifyBatchesTrustedAggregator(opts *bind.TransactOpts, rollupID uint32, pendingStateNum uint64, initNumBatch uint64, finalNewBatch uint64, newLocalExitRoot [32]byte, newStateRoot [32]byte, beneficiary common.Address, proof [24][32]byte) (*types.Transaction, error) { - return e.GetContract().VerifyBatchesTrustedAggregator(opts, rollupID, pendingStateNum, initNumBatch, finalNewBatch, newLocalExitRoot, newStateRoot, beneficiary, proof) -} - -func (e *ContractRollupManangerElderberry) GetRollupSequencedBatches(rollupID uint32, batchNum uint64) (StateVariablesSequencedBatchData, error) { - res, err := e.GetContract().GetRollupSequencedBatches(&bind.CallOpts{Pending: false}, rollupID, batchNum) - if err != nil { - return StateVariablesSequencedBatchData{}, err - } - return StateVariablesSequencedBatchData{ - AccInputHash: res.AccInputHash, - SequencedTimestamp: res.SequencedTimestamp, - PreviousLastBatchSequenced: res.PreviousLastBatchSequenced, - }, nil -} diff --git a/etherman/contracts/interfaces.go b/etherman/contracts/interfaces.go index 60ac67d1..87061874 100644 --- a/etherman/contracts/interfaces.go +++ b/etherman/contracts/interfaces.go @@ -43,9 +43,9 @@ type GlobalExitRootContractor interface { } type BaseContractor interface { - GetName() string - GetVersion() string - GetAddress() common.Address + Name() string + Version() string + Address() common.Address String() string } diff --git a/etherman/etherman.go b/etherman/etherman.go index 1364206d..e47477c8 100644 --- a/etherman/etherman.go +++ b/etherman/etherman.go @@ -12,6 +12,7 @@ import ( "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/idataavailabilityprotocol" cdkcommon "github.com/0xPolygon/cdk/common" + "github.com/0xPolygon/cdk/etherman/config" "github.com/0xPolygon/cdk/etherman/contracts" "github.com/0xPolygon/cdk/log" "github.com/ethereum/go-ethereum" @@ -59,57 +60,48 @@ type ethereumClient interface { bind.DeployBackend } -// L1Config represents the configuration of the network used in L1 -type L1Config struct { - // Chain ID of the L1 network - L1ChainID uint64 `json:"chainId"` - // ZkEVMAddr Address of the L1 contract polygonZkEVMAddress - ZkEVMAddr common.Address `json:"polygonZkEVMAddress"` - // RollupManagerAddr Address of the L1 contract - RollupManagerAddr common.Address `json:"polygonRollupManagerAddress"` - // PolAddr Address of the L1 Pol token Contract - PolAddr common.Address `json:"polTokenAddress"` - // GlobalExitRootManagerAddr Address of the L1 GlobalExitRootManager contract - GlobalExitRootManagerAddr common.Address `json:"polygonZkEVMGlobalExitRootAddress"` -} - // Client is a simple implementation of EtherMan. type Client struct { EthClient ethereumClient DAProtocol *idataavailabilityprotocol.Idataavailabilityprotocol - Contracts *Contracts + Contracts *contracts.Contracts RollupID uint32 - l1Cfg L1Config - cfg Config + l1Cfg config.L1Config + cfg config.Config auth map[common.Address]bind.TransactOpts // empty in case of read-only client } // NewClient creates a new etherman. -func NewClient(cfg Config, l1Config L1Config, commonConfig cdkcommon.Config) (*Client, error) { +func NewClient(cfg config.Config, l1Config config.L1Config, commonConfig cdkcommon.Config) (*Client, error) { // Connect to ethereum node ethClient, err := ethclient.Dial(cfg.EthermanConfig.URL) if err != nil { log.Errorf("error connecting to %s: %+v", cfg.EthermanConfig.URL, err) return nil, err } - - contracts, err := NewContracts(l1Config, ethClient, contracts.VersionType(commonConfig.ContractVersions)) + L1chainID, err := ethClient.ChainID(context.Background()) + if err != nil { + log.Errorf("error getting L1chainID from %s: %+v", cfg.EthermanConfig.URL, err) + return nil, err + } + log.Infof("L1ChainID: %d", L1chainID.Uint64()) + contracts, err := contracts.NewContracts(l1Config, ethClient) if err != nil { return nil, err } log.Info(contracts.String()) // Get RollupID - rollupID, err := contracts.RollupManager(nil).RollupAddressToID(l1Config.ZkEVMAddr) + rollupID, err := contracts.Banana.RollupManager.Contract().RollupAddressToID(&bind.CallOpts{Pending: false}, l1Config.ZkEVMAddr) if err != nil { - log.Errorf("error getting rollupID from %s : %+v", contracts.RollupManager(nil).String(), err) + log.Errorf("error getting rollupID from %s : %+v", contracts.Banana.RollupManager.String(), err) return nil, err } if rollupID == 0 { return nil, errors.New("rollupID is 0, is not a valid value. Check that rollup Address is correct " + l1Config.ZkEVMAddr.String()) } - log.Debug("rollupID: ", rollupID) + log.Infof("rollupID: %d (obtenied from SMC: %s )", rollupID, contracts.Banana.RollupManager.String()) client := &Client{ EthClient: ethClient, @@ -122,7 +114,7 @@ func NewClient(cfg Config, l1Config L1Config, commonConfig cdkcommon.Config) (*C } if commonConfig.IsValidiumMode { - dapAddr, err := contracts.ZkEVM(nil).DataAvailabilityProtocol() + dapAddr, err := contracts.Banana.Rollup.Contract().DataAvailabilityProtocol(&bind.CallOpts{Pending: false}) if err != nil { return nil, err } @@ -156,7 +148,7 @@ func (etherMan *Client) WaitTxToBeMined(ctx context.Context, tx *types.Transacti // GetSendSequenceFee get super/trusted sequencer fee func (etherMan *Client) GetSendSequenceFee(numBatches uint64) (*big.Int, error) { - f, err := etherMan.Contracts.RollupManager(nil).GetBatchFee() + f, err := etherMan.Contracts.Banana.RollupManager.Contract().GetBatchFee(&bind.CallOpts{Pending: false}) if err != nil { return nil, err } @@ -166,7 +158,7 @@ func (etherMan *Client) GetSendSequenceFee(numBatches uint64) (*big.Int, error) // TrustedSequencer gets trusted sequencer address func (etherMan *Client) TrustedSequencer() (common.Address, error) { - return etherMan.Contracts.ZkEVM(nil).TrustedSequencer() + return etherMan.Contracts.Banana.Rollup.Contract().TrustedSequencer(&bind.CallOpts{Pending: false}) } // HeaderByNumber returns a block header from the current canonical chain. If number is @@ -189,7 +181,7 @@ func (etherMan *Client) EthBlockByNumber(ctx context.Context, blockNumber uint64 // GetLatestBatchNumber function allows to retrieve the latest proposed batch in the smc func (etherMan *Client) GetLatestBatchNumber() (uint64, error) { - rollupData, err := etherMan.Contracts.RollupManager(nil).RollupIDToRollupData(etherMan.RollupID) + rollupData, err := etherMan.Contracts.Banana.RollupManager.Contract().RollupIDToRollupData(&bind.CallOpts{Pending: false}, etherMan.RollupID) if err != nil { return 0, err } @@ -231,7 +223,7 @@ func (etherMan *Client) GetLatestBlockTimestamp(ctx context.Context) (uint64, er // GetLatestVerifiedBatchNum gets latest verified batch from ethereum func (etherMan *Client) GetLatestVerifiedBatchNum() (uint64, error) { - rollupData, err := etherMan.Contracts.RollupManager(nil).RollupIDToRollupData(etherMan.RollupID) + rollupData, err := etherMan.Contracts.Banana.RollupManager.Contract().RollupIDToRollupData(&bind.CallOpts{Pending: false}, etherMan.RollupID) if err != nil { return 0, err } @@ -250,12 +242,12 @@ func (etherMan *Client) GetTxReceipt(ctx context.Context, txHash common.Hash) (* // GetTrustedSequencerURL Gets the trusted sequencer url from rollup smc func (etherMan *Client) GetTrustedSequencerURL() (string, error) { - return etherMan.Contracts.ZkEVM(nil).TrustedSequencerURL() + return etherMan.Contracts.Banana.Rollup.Contract().TrustedSequencerURL(&bind.CallOpts{Pending: false}) } // GetL2ChainID returns L2 Chain ID func (etherMan *Client) GetL2ChainID() (uint64, error) { - rollupData, err := etherMan.Contracts.RollupManager(nil).RollupIDToRollupData(etherMan.RollupID) + rollupData, err := etherMan.Contracts.Banana.RollupManager.Contract().RollupIDToRollupData(&bind.CallOpts{Pending: false}, etherMan.RollupID) log.Debug("chainID read from rollupManager: ", rollupData.ChainID) if err != nil { log.Debug("error from rollupManager: ", err) @@ -405,7 +397,7 @@ func (etherMan *Client) GetLatestBlockHeader(ctx context.Context) (*types.Header // GetDAProtocolAddr returns the address of the data availability protocol func (etherMan *Client) GetDAProtocolAddr() (common.Address, error) { - return etherMan.Contracts.ZkEVM(nil).DataAvailabilityProtocol() + return etherMan.Contracts.Banana.Rollup.Contract().DataAvailabilityProtocol(&bind.CallOpts{Pending: false}) } // GetDAProtocolName returns the name of the data availability protocol @@ -415,10 +407,23 @@ func (etherMan *Client) GetDAProtocolName() (string, error) { // LastAccInputHash gets the last acc input hash from the SC func (etherMan *Client) LastAccInputHash() (common.Hash, error) { - return etherMan.Contracts.ZkEVM(nil).LastAccInputHash() + return etherMan.Contracts.Banana.Rollup.Contract().LastAccInputHash(&bind.CallOpts{Pending: false}) } // GetL1InfoRoot gets the L1 info root from the SC func (etherMan *Client) GetL1InfoRoot(indexL1InfoRoot uint32) (common.Hash, error) { - return etherMan.Contracts.GlobalExitRoot(nil).L1InfoIndexToRoot(indexL1InfoRoot) + // Get lastL1InfoTreeRoot (if index==0 then root=0, no call is needed) + var ( + lastL1InfoTreeRoot common.Hash + err error + ) + + if indexL1InfoRoot > 0 { + lastL1InfoTreeRoot, err = etherMan.Contracts.Banana.GlobalExitRoot.Contract().L1InfoRootMap(&bind.CallOpts{Pending: false}, indexL1InfoRoot) + if err != nil { + log.Errorf("error calling SC globalexitroot L1InfoLeafMap: %v", err) + } + } + + return lastL1InfoTreeRoot, err } diff --git a/sequencesender/sequencesender.go b/sequencesender/sequencesender.go index 809ec086..70b5fc24 100644 --- a/sequencesender/sequencesender.go +++ b/sequencesender/sequencesender.go @@ -471,7 +471,7 @@ func (s *SequenceSender) tryToSendSequence(ctx context.Context) { lastSequence := sequence.LastBatch() lastL2BlockTimestamp := lastSequence.LastL2BLockTimestamp() - log.Infof("[SeqSender] sending sequences to L1. From batch %d to batch %d", firstSequence.BatchNumber, lastSequence.BatchNumber) + log.Infof("[SeqSender] sending sequences to L1. From batch %d to batch %d", firstSequence.BatchNumber(), lastSequence.BatchNumber()) log.Infof(sequence.String()) // Wait until last L1 block timestamp is L1BlockTimestampMargin seconds above the timestamp of the last L2 block in the sequence diff --git a/sequencesender/txbuilder/banana_base.go b/sequencesender/txbuilder/banana_base.go index 5d6276c4..babce9cc 100644 --- a/sequencesender/txbuilder/banana_base.go +++ b/sequencesender/txbuilder/banana_base.go @@ -14,10 +14,24 @@ import ( ) type TxBuilderBananaBase struct { - zkevm contracts.ContractRollupBanana - globalExitRootContract contracts.ContractGlobalExitRootBanana - opts bind.TransactOpts - SenderAddress common.Address + rollupContract contracts.RollupBananaType + globalExitRootContract contracts.GlobalExitRootBananaType + + opts bind.TransactOpts + SenderAddress common.Address +} + +func NewTxBuilderBananaBase(rollupContract contracts.RollupBananaType, + gerContract contracts.GlobalExitRootBananaType, + opts bind.TransactOpts, + sender common.Address) *TxBuilderBananaBase { + return &TxBuilderBananaBase{ + rollupContract: rollupContract, + globalExitRootContract: gerContract, + opts: opts, + SenderAddress: sender, + } + } func convertToSequenceBanana(sequences seqsendertypes.Sequence) (etherman.SequenceBanana, error) { @@ -88,19 +102,19 @@ func (t *TxBuilderBananaBase) NewSequence(batches []seqsendertypes.Batch, coinba } sequence := etherman.NewSequenceBanana(ethBatches, coinbase) - l1InfoRoot, err := t.globalExitRootContract.GetL1InfoRoot(sequence.IndexL1InfoRoot) + l1InfoRoot, err := t.getL1InfoRoot(sequence.IndexL1InfoRoot) if err != nil { return nil, err } sequence.L1InfoRoot = l1InfoRoot - accInputHash, err := t.zkevm.LastAccInputHash() + accInputHash, err := t.rollupContract.Contract().LastAccInputHash(&bind.CallOpts{Pending: false}) if err != nil { return nil, err } - oldAccInputHash := common.BytesToHash(accInputHash.Bytes()) //copy it + oldAccInputHash := common.BytesToHash(accInputHash[:]) //copy it for _, batch := range sequence.Batches { infoRootHash := sequence.L1InfoRoot @@ -124,3 +138,20 @@ func (t *TxBuilderBananaBase) NewSequence(batches []seqsendertypes.Batch, coinba res := NewBananaSequence(*sequence) return res, nil } + +func (t *TxBuilderBananaBase) getL1InfoRoot(indexL1InfoRoot uint32) (common.Hash, error) { + // Get lastL1InfoTreeRoot (if index==0 then root=0, no call is needed) + var ( + lastL1InfoTreeRoot common.Hash + err error + ) + + if indexL1InfoRoot > 0 { + lastL1InfoTreeRoot, err = t.globalExitRootContract.Contract().L1InfoRootMap(&bind.CallOpts{Pending: false}, indexL1InfoRoot) + if err != nil { + log.Errorf("error calling SC globalexitroot L1InfoLeafMap: %v", err) + } + } + + return lastL1InfoTreeRoot, err +} diff --git a/sequencesender/txbuilder/banana_validium.go b/sequencesender/txbuilder/banana_validium.go index fd844ec4..08d1750e 100644 --- a/sequencesender/txbuilder/banana_validium.go +++ b/sequencesender/txbuilder/banana_validium.go @@ -22,14 +22,11 @@ type TxBuilderBananaValidium struct { condNewSeq CondNewSequence } -func NewTxBuilderBananaValidium(zkevm contracts.ContractRollupBanana, da dataavailability.SequenceSender, opts bind.TransactOpts, sender common.Address, maxBatchesForL1 uint64) *TxBuilderBananaValidium { +func NewTxBuilderBananaValidium(rollupContract contracts.RollupBananaType, gerContract contracts.GlobalExitRootBananaType, + da dataavailability.SequenceSender, opts bind.TransactOpts, sender common.Address, maxBatchesForL1 uint64) *TxBuilderBananaValidium { return &TxBuilderBananaValidium{ - TxBuilderBananaBase: TxBuilderBananaBase{ - zkevm: zkevm, - opts: opts, - SenderAddress: sender, - }, - da: da, + TxBuilderBananaBase: *NewTxBuilderBananaBase(rollupContract, gerContract, opts, sender), + da: da, condNewSeq: &NewSequenceConditionalNumBatches{ maxBatchesForL1: maxBatchesForL1, }, @@ -96,7 +93,7 @@ func (t *TxBuilderBananaValidium) sequenceBatchesValidium(opts bind.TransactOpts } } - tx, err := t.zkevm.GetContract().SequenceBatchesValidium(&opts, batches, sequence.IndexL1InfoRoot, sequence.MaxSequenceTimestamp, sequence.AccInputHash, sequence.L2Coinbase, dataAvailabilityMessage) + tx, err := t.rollupContract.Contract().SequenceBatchesValidium(&opts, batches, sequence.IndexL1InfoRoot, sequence.MaxSequenceTimestamp, sequence.AccInputHash, sequence.L2Coinbase, dataAvailabilityMessage) if err != nil { log.Debugf("Batches to send: %+v", batches) log.Debug("l2CoinBase: ", sequence.L2Coinbase) diff --git a/sequencesender/txbuilder/banana_zkevm.go b/sequencesender/txbuilder/banana_zkevm.go index 9bf4d6a1..dbb679fb 100644 --- a/sequencesender/txbuilder/banana_zkevm.go +++ b/sequencesender/txbuilder/banana_zkevm.go @@ -19,13 +19,9 @@ type TxBuilderBananaZKEVM struct { condNewSeq CondNewSequence } -func NewTxBuilderBananaZKEVM(zkevm contracts.ContractRollupBanana, opts bind.TransactOpts, sender common.Address, maxTxSizeForL1 uint64) *TxBuilderBananaZKEVM { +func NewTxBuilderBananaZKEVM(rollupContract contracts.RollupBananaType, gerContract contracts.GlobalExitRootBananaType, opts bind.TransactOpts, sender common.Address, maxTxSizeForL1 uint64) *TxBuilderBananaZKEVM { return &TxBuilderBananaZKEVM{ - TxBuilderBananaBase: TxBuilderBananaBase{ - zkevm: zkevm, - opts: opts, - SenderAddress: sender, - }, + TxBuilderBananaBase: *NewTxBuilderBananaBase(rollupContract, gerContract, opts, sender), condNewSeq: &NewSequenceConditionalMaxSize{ maxTxSizeForL1: maxTxSizeForL1, }, @@ -75,7 +71,7 @@ func (t *TxBuilderBananaZKEVM) sequenceBatchesRollup(opts bind.TransactOpts, seq } } - tx, err := t.zkevm.GetContract().SequenceBatches(&opts, batches, sequence.IndexL1InfoRoot, sequence.MaxSequenceTimestamp, sequence.AccInputHash, sequence.L2Coinbase) + tx, err := t.rollupContract.Contract().SequenceBatches(&opts, batches, sequence.IndexL1InfoRoot, sequence.MaxSequenceTimestamp, sequence.AccInputHash, sequence.L2Coinbase) if err != nil { log.Debugf("Batches to send: %+v", batches) log.Debug("l2CoinBase: ", sequence.L2Coinbase) diff --git a/sequencesender/txbuilder/elderberry_zkevm.go b/sequencesender/txbuilder/elderberry_zkevm.go index 6d0ef012..31a59a56 100644 --- a/sequencesender/txbuilder/elderberry_zkevm.go +++ b/sequencesender/txbuilder/elderberry_zkevm.go @@ -17,15 +17,15 @@ import ( ) type TxBuilderElderberryZKEVM struct { - opts bind.TransactOpts - zkevm contracts.ContractRollupElderberry - condNewSeq CondNewSequence + opts bind.TransactOpts + rollupContract contracts.RollupElderberryType + condNewSeq CondNewSequence } -func NewTxBuilderElderberryZKEVM(zkevm contracts.ContractRollupElderberry, opts bind.TransactOpts, sender common.Address, maxTxSizeForL1 uint64) *TxBuilderElderberryZKEVM { +func NewTxBuilderElderberryZKEVM(zkevm contracts.RollupElderberryType, opts bind.TransactOpts, sender common.Address, maxTxSizeForL1 uint64) *TxBuilderElderberryZKEVM { return &TxBuilderElderberryZKEVM{ - opts: opts, - zkevm: zkevm, + opts: opts, + rollupContract: zkevm, condNewSeq: &NewSequenceConditionalMaxSize{ maxTxSizeForL1: maxTxSizeForL1, }, @@ -85,7 +85,7 @@ func (t *TxBuilderElderberryZKEVM) sequenceBatchesRollup(opts bind.TransactOpts, } } lastSequencedBatchNumber := getLastSequencedBatchNumber(sequences) - ZkEVM := t.zkevm.GetContract() + ZkEVM := t.rollupContract.Contract() tx, err := ZkEVM.SequenceBatches(&opts, batches, sequences.MaxSequenceTimestamp(), lastSequencedBatchNumber, sequences.L2Coinbase()) if err != nil { t.warningMessage(batches, sequences.L2Coinbase(), &opts) From eca9e9de1db4660f84ff80c3b44334627bed3fff Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Mon, 22 Jul 2024 14:56:30 +0200 Subject: [PATCH 08/51] add elderberry validium --- .../datacommittee/datacommittee.go | 65 +++++++++++-- dataavailability/interfaces.go | 6 ++ go.mod | 2 +- go.sum | 12 +-- sequencesender/txbuilder/elderberry_base.go | 52 +++++++++++ .../txbuilder/elderberry_validium.go | 91 +++++++++++++++++++ sequencesender/txbuilder/elderberry_zkevm.go | 34 +------ 7 files changed, 216 insertions(+), 46 deletions(-) create mode 100644 sequencesender/txbuilder/elderberry_base.go create mode 100644 sequencesender/txbuilder/elderberry_validium.go diff --git a/dataavailability/datacommittee/datacommittee.go b/dataavailability/datacommittee/datacommittee.go index c9be97fc..c29cd622 100644 --- a/dataavailability/datacommittee/datacommittee.go +++ b/dataavailability/datacommittee/datacommittee.go @@ -153,6 +153,37 @@ type signatureMsg struct { err error } +func (s *Backend) PostSequenceElderberry(ctx context.Context, batchesData [][]byte) ([]byte, error) { + // Get current committee + committee, err := s.getCurrentDataCommittee() + if err != nil { + return nil, err + } + + // Authenticate as trusted sequencer by signing the sequence + sequence := make(daTypes.Sequence, 0, len(batchesData)) + for _, batchData := range batchesData { + sequence = append(sequence, batchData) + } + signedSequence, err := sequence.Sign(s.privKey) + if err != nil { + return nil, err + } + + // Request signatures to all members in parallel + ch := make(chan signatureMsg, len(committee.Members)) + signatureCtx, cancelSignatureCollection := context.WithCancel(ctx) + for _, member := range committee.Members { + signedSequenceElderberry := daTypes.SignedSequence{ + Sequence: sequence, + Signature: signedSequence, + } + go requestSignatureFromMember(signatureCtx, &signedSequenceElderberry, + func(c client.Client) ([]byte, error) { return c.SignSequence(ctx, signedSequenceElderberry) }, member, ch) + } + return s.collectSignatures(committee, ch, cancelSignatureCollection) +} + func (s *Backend) PostSequence(ctx context.Context, sequence etherman.SequenceBanana) ([]byte, error) { // Get current committee committee, err := s.getCurrentDataCommittee() @@ -187,13 +218,22 @@ func (s *Backend) PostSequence(ctx context.Context, sequence etherman.SequenceBa ch := make(chan signatureMsg, len(committee.Members)) signatureCtx, cancelSignatureCollection := context.WithCancel(ctx) for _, member := range committee.Members { - go requestSignatureFromMember(signatureCtx, daTypes.SignedSequenceBanana{ + signedSequenceBanana := daTypes.SignedSequenceBanana{ Sequence: sequenceBanana, Signature: signature, - }, member, ch) + } + go requestSignatureFromMember(signatureCtx, + &signedSequenceBanana, + func(c client.Client) ([]byte, error) { return c.SignSequenceBanana(ctx, signedSequenceBanana) }, + member, ch) } + return s.collectSignatures(committee, ch, cancelSignatureCollection) +} + +func (*Backend) collectSignatures(committee *DataCommittee, ch chan signatureMsg, cancelSignatureCollection context.CancelFunc) ([]byte, error) { // Collect signatures + // Stop requesting as soon as we have N valid signatures var ( msgs = make(signatureMsgs, 0, len(committee.Members)) collectedSignatures uint64 @@ -215,13 +255,24 @@ func (s *Backend) PostSequence(ctx context.Context, sequence etherman.SequenceBa msgs = append(msgs, msg) } - // Stop requesting as soon as we have N valid signatures cancelSignatureCollection() return buildSignaturesAndAddrs(msgs, committee.Members), nil } -func requestSignatureFromMember(ctx context.Context, signedSequence daTypes.SignedSequenceBanana, member DataCommitteeMember, ch chan signatureMsg) { +type funcSignType func(c client.Client) ([]byte, error) + +type SignedInterface interface { + SetSignature([]byte) + Signer() (common.Address, error) +} + +// funcSetSignatureType: is not possible to define a SetSignature function because +// the type daTypes.SequenceBanana and daTypes.Sequence belong to different packages +// So a future refactor is define a common interface for both +func requestSignatureFromMember(ctx context.Context, signedSequence daTypes.SignedSequenceInterface, + funcSign funcSignType, + member DataCommitteeMember, ch chan signatureMsg) { select { case <-ctx.Done(): return @@ -231,7 +282,9 @@ func requestSignatureFromMember(ctx context.Context, signedSequence daTypes.Sign // request c := client.New(member.URL) log.Infof("sending request to sign the sequence to %s at %s", member.Addr.Hex(), member.URL) - signature, err := c.SignSequenceBanana(ctx, signedSequence) + //signature, err := c.SignSequenceBanana(ctx, signedSequence) + signature, err := funcSign(c) + if err != nil { ch <- signatureMsg{ addr: member.Addr, @@ -240,7 +293,7 @@ func requestSignatureFromMember(ctx context.Context, signedSequence daTypes.Sign return } // verify returned signature - signedSequence.Signature = signature + signedSequence.SetSignature(signature) signer, err := signedSequence.Signer() if err != nil { ch <- signatureMsg{ diff --git a/dataavailability/interfaces.go b/dataavailability/interfaces.go index 54c5c26e..1ef21408 100644 --- a/dataavailability/interfaces.go +++ b/dataavailability/interfaces.go @@ -22,6 +22,12 @@ type SequenceSender interface { PostSequence(ctx context.Context, sequence etherman.SequenceBanana) ([]byte, error) } +type SequenceSenderElderberry interface { + // PostSequence sends the sequence data to the data availability backend, and returns the dataAvailabilityMessage + // as expected by the contract + PostSequenceElderberry(ctx context.Context, batchesData [][]byte) ([]byte, error) +} + // SequenceRetriever is used to retrieve batch data type SequenceRetriever interface { // GetSequence retrieves the sequence data from the data availability backend diff --git a/go.mod b/go.mod index 452ed304..4526a32f 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.22.4 require ( github.com/0xPolygon/cdk-contracts-tooling v0.0.0-20240627125553-80db9d8a41b5 - github.com/0xPolygon/cdk-data-availability v0.0.8-0.20240712072318-72ae67613cbf + github.com/0xPolygon/cdk-data-availability v0.0.8 github.com/0xPolygon/cdk-rpc v0.0.0-20240419104226-c0a62ba0f49d github.com/0xPolygonHermez/zkevm-data-streamer v0.2.2 github.com/0xPolygonHermez/zkevm-ethtx-manager v0.1.9 diff --git a/go.sum b/go.sum index d74af758..a452f39d 100644 --- a/go.sum +++ b/go.sum @@ -39,26 +39,18 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/0xPolygon/cdk-contracts-tooling v0.0.0-20240627125553-80db9d8a41b5 h1:bezOoVvpQvGvf4aCZQpMPFJGKXhhvktpApr+TsD8rdk= github.com/0xPolygon/cdk-contracts-tooling v0.0.0-20240627125553-80db9d8a41b5/go.mod h1:mFlcEjsm2YBBsu8atHJ3zyVnwM+Z/fMXpVmIJge+WVU= -github.com/0xPolygon/cdk-data-availability v0.0.8-0.20240620143831-55a2ce665c77 h1:ijHKNgkzyaepqknBEVGJy9GADTUimoR/wagvEP134l8= -github.com/0xPolygon/cdk-data-availability v0.0.8-0.20240620143831-55a2ce665c77/go.mod h1:3XkZ0zn0GsvAT01MPQMmukF534CVSFmtrcoK3F/BK6Q= -github.com/0xPolygon/cdk-data-availability v0.0.8-0.20240712072318-72ae67613cbf h1:VWxVYeDhDURGp8pHR4kq3jwoXRazVHsQLMMIqYsO+Fw= -github.com/0xPolygon/cdk-data-availability v0.0.8-0.20240712072318-72ae67613cbf/go.mod h1:3XkZ0zn0GsvAT01MPQMmukF534CVSFmtrcoK3F/BK6Q= +github.com/0xPolygon/cdk-data-availability v0.0.8 h1:bMmOYZ7Ei683y80ric3KzMPXtRGmchAmfjIRzghaHb4= +github.com/0xPolygon/cdk-data-availability v0.0.8/go.mod h1:3XkZ0zn0GsvAT01MPQMmukF534CVSFmtrcoK3F/BK6Q= github.com/0xPolygon/cdk-rpc v0.0.0-20240419104226-c0a62ba0f49d h1:sxh6hZ2jF/sxxj2jd5o1vuNNCZjYmn4aRG9SRlVaEFs= github.com/0xPolygon/cdk-rpc v0.0.0-20240419104226-c0a62ba0f49d/go.mod h1:2scWqMMufrQXu7TikDgQ3BsyaKoX8qP26D6E262vSOg= github.com/0xPolygonHermez/zkevm-data-streamer v0.2.2 h1:XRMTk+W6vtJVGVjuEznfWyNt7HkRkkuSmlN5Y6p60Sc= github.com/0xPolygonHermez/zkevm-data-streamer v0.2.2/go.mod h1:0QkAXcFa92mFJrCbN3UPUJGJYes851yEgYHLONnaosE= github.com/0xPolygonHermez/zkevm-ethtx-manager v0.1.9 h1:vrAezzwTNke6NroDAltGh1k2AJ6ibmZPBsG0bCltbRc= github.com/0xPolygonHermez/zkevm-ethtx-manager v0.1.9/go.mod h1:pRqfLQVM3nbzdhy3buqjAgcVyNDKAXOHqTSgkwiKpic= -github.com/0xPolygonHermez/zkevm-synchronizer-l1 v0.6.1 h1:pf0lvUJNl5/3AnpnFPvgaYTUoOydVUaqI0hyZGglI2E= -github.com/0xPolygonHermez/zkevm-synchronizer-l1 v0.6.1/go.mod h1:3L6m4ZYs2tTTNhxMtq1KHSclxr7RmlZ3eWtOe8lsYsM= -github.com/0xPolygonHermez/zkevm-synchronizer-l1 v0.6.3-0.20240712073237-fa4202239a02 h1:ordyO43rufvsseuo2Ossb3bDx6oApFzCOmduWWmzWLs= -github.com/0xPolygonHermez/zkevm-synchronizer-l1 v0.6.3-0.20240712073237-fa4202239a02/go.mod h1:/LHf8jPQeBYKABM1xUmN1dKaFVIJc9jMQDSGBDJ7CS0= github.com/0xPolygonHermez/zkevm-synchronizer-l1 v0.6.3-0.20240712085301-0310358abb59 h1:Qwh92vFEXnpmDggQaZA3648viEQfLdMnAw/WFSY+2i8= github.com/0xPolygonHermez/zkevm-synchronizer-l1 v0.6.3-0.20240712085301-0310358abb59/go.mod h1:/LHf8jPQeBYKABM1xUmN1dKaFVIJc9jMQDSGBDJ7CS0= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= -github.com/DATA-DOG/go-sqlmock v1.5.1 h1:FK6RCIUSfmbnI/imIICmboyQBkOckutaa6R5YYlLZyo= -github.com/DATA-DOG/go-sqlmock v1.5.1/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU= github.com/DataDog/zstd v1.5.2 h1:vUG4lAyuPCXO0TLbXvPv7EB7cNK1QV/luu55UHLrrn8= github.com/DataDog/zstd v1.5.2/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw= github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= diff --git a/sequencesender/txbuilder/elderberry_base.go b/sequencesender/txbuilder/elderberry_base.go new file mode 100644 index 00000000..79a92202 --- /dev/null +++ b/sequencesender/txbuilder/elderberry_base.go @@ -0,0 +1,52 @@ +package txbuilder + +import ( + "github.com/0xPolygon/cdk/etherman" + "github.com/0xPolygon/cdk/etherman/contracts" + "github.com/0xPolygon/cdk/sequencesender/seqsendertypes" + "github.com/0xPolygon/cdk/state/datastream" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" +) + +type TxBuilderElderberryBase struct { + rollupContract contracts.RollupElderberryType + opts bind.TransactOpts +} + +func NewTxBuilderElderberryBase(rollupContract contracts.RollupElderberryType, opts bind.TransactOpts) *TxBuilderElderberryBase { + return &TxBuilderElderberryBase{ + rollupContract: rollupContract, + opts: opts, + } +} + +func (t *TxBuilderElderberryBase) NewSequence(batches []seqsendertypes.Batch, coinbase common.Address) (seqsendertypes.Sequence, error) { + seq := ElderberrySequence{ + l2Coinbase: coinbase, + batches: batches, + } + return &seq, nil +} + +func (t *TxBuilderElderberryBase) NewBatchFromL2Block(l2Block *datastream.L2Block) seqsendertypes.Batch { + batch := ðerman.Batch{ + LastL2BLockTimestamp: l2Block.Timestamp, + BatchNumber: l2Block.BatchNumber, + L1InfoTreeIndex: l2Block.L1InfotreeIndex, + LastCoinbase: common.BytesToAddress(l2Block.Coinbase), + GlobalExitRoot: common.BytesToHash(l2Block.GlobalExitRoot), + } + return NewBananaBatch(batch) +} + +func getLastSequencedBatchNumber(sequences seqsendertypes.Sequence) uint64 { + if sequences.Len() == 0 { + return 0 + } + return sequences.FirstBatch().BatchNumber() - 1 +} + +func (t *TxBuilderElderberryZKEVM) String() string { + return "Elderberry/ZKEVM" +} diff --git a/sequencesender/txbuilder/elderberry_validium.go b/sequencesender/txbuilder/elderberry_validium.go new file mode 100644 index 00000000..d9d1acd2 --- /dev/null +++ b/sequencesender/txbuilder/elderberry_validium.go @@ -0,0 +1,91 @@ +package txbuilder + +import ( + "context" + + "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry/polygonvalidiumetrog" + "github.com/0xPolygon/cdk/dataavailability" + "github.com/0xPolygon/cdk/etherman" + "github.com/0xPolygon/cdk/etherman/contracts" + "github.com/0xPolygon/cdk/log" + "github.com/0xPolygon/cdk/sequencesender/seqsendertypes" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + ethtypes "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" +) + +type TxBuilderElderberryValidium struct { + TxBuilderElderberryBase + da dataavailability.SequenceSenderElderberry + condNewSeq CondNewSequence +} + +func NewTxBuilderElderberryValidium(zkevm contracts.RollupElderberryType, + da dataavailability.SequenceSenderElderberry, + opts bind.TransactOpts, sender common.Address, maxTxSizeForL1 uint64) *TxBuilderElderberryValidium { + return &TxBuilderElderberryValidium{ + da: da, + TxBuilderElderberryBase: *NewTxBuilderElderberryBase( + zkevm, opts, + ), + condNewSeq: &NewSequenceConditionalMaxSize{ + maxTxSizeForL1: maxTxSizeForL1, + }, + } +} +func (t *TxBuilderElderberryValidium) NewSequenceIfWorthToSend(ctx context.Context, sequenceBatches []seqsendertypes.Batch, l2Coinbase common.Address, batchNumber uint64) (seqsendertypes.Sequence, error) { + return t.condNewSeq.NewSequenceIfWorthToSend(ctx, t, sequenceBatches, t.opts.From, l2Coinbase, batchNumber) +} + +func (t *TxBuilderElderberryValidium) BuildSequenceBatchesTx(ctx context.Context, sender common.Address, sequences seqsendertypes.Sequence) (*ethtypes.Transaction, error) { + + batchesData := convertToBatchesData(sequences) + dataAvailabilityMessage, err := t.da.PostSequenceElderberry(ctx, batchesData) + if err != nil { + log.Error("error posting sequences to the data availability protocol: ", err) + return nil, err + } + return t.buildSequenceBatchesTxValidium(sequences, dataAvailabilityMessage) +} + +func (t *TxBuilderElderberryValidium) buildSequenceBatchesTxValidium( + sequences seqsendertypes.Sequence, dataAvailabilityMessage []byte) (*types.Transaction, error) { + batches := make([]polygonvalidiumetrog.PolygonValidiumEtrogValidiumBatchData, sequences.Len()) + for i, seq := range sequences.Batches() { + var ger common.Hash + if seq.ForcedBatchTimestamp() > 0 { + ger = seq.GlobalExitRoot() + } + batches[i] = polygonvalidiumetrog.PolygonValidiumEtrogValidiumBatchData{ + TransactionsHash: crypto.Keccak256Hash(seq.L2Data()), + ForcedGlobalExitRoot: ger, + ForcedTimestamp: uint64(seq.ForcedBatchTimestamp()), + ForcedBlockHashL1: seq.ForcedBlockHashL1(), + } + } + lastSequencedBatchNumber := getLastSequencedBatchNumber(sequences) + ZkEVM := t.rollupContract.Contract() + tx, err := ZkEVM.SequenceBatchesValidium(&t.opts, batches, sequences.MaxSequenceTimestamp(), + lastSequencedBatchNumber, sequences.L2Coinbase(), dataAvailabilityMessage) + if err != nil { + if parsedErr, ok := etherman.TryParseError(err); ok { + err = parsedErr + } + } + + return tx, err +} + +func (t *TxBuilderElderberryValidium) String() string { + return "Elderberry/Validium" +} + +func convertToBatchesData(sequences seqsendertypes.Sequence) [][]byte { + batches := make([][]byte, sequences.Len()) + for i, batch := range sequences.Batches() { + batches[i] = batch.L2Data() + } + return batches +} diff --git a/sequencesender/txbuilder/elderberry_zkevm.go b/sequencesender/txbuilder/elderberry_zkevm.go index 31a59a56..1603193b 100644 --- a/sequencesender/txbuilder/elderberry_zkevm.go +++ b/sequencesender/txbuilder/elderberry_zkevm.go @@ -9,7 +9,6 @@ import ( "github.com/0xPolygon/cdk/etherman/contracts" "github.com/0xPolygon/cdk/log" "github.com/0xPolygon/cdk/sequencesender/seqsendertypes" - "github.com/0xPolygon/cdk/state/datastream" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -17,44 +16,21 @@ import ( ) type TxBuilderElderberryZKEVM struct { - opts bind.TransactOpts - rollupContract contracts.RollupElderberryType - condNewSeq CondNewSequence + TxBuilderElderberryBase + condNewSeq CondNewSequence } func NewTxBuilderElderberryZKEVM(zkevm contracts.RollupElderberryType, opts bind.TransactOpts, sender common.Address, maxTxSizeForL1 uint64) *TxBuilderElderberryZKEVM { return &TxBuilderElderberryZKEVM{ - opts: opts, - rollupContract: zkevm, + TxBuilderElderberryBase: *NewTxBuilderElderberryBase( + zkevm, opts, + ), condNewSeq: &NewSequenceConditionalMaxSize{ maxTxSizeForL1: maxTxSizeForL1, }, } } -func (t *TxBuilderElderberryZKEVM) NewSequence(batches []seqsendertypes.Batch, coinbase common.Address) (seqsendertypes.Sequence, error) { - seq := ElderberrySequence{ - l2Coinbase: coinbase, - batches: batches, - } - return &seq, nil -} - -func (t *TxBuilderElderberryZKEVM) NewSequenceIfWorthToSend(ctx context.Context, sequenceBatches []seqsendertypes.Batch, l2Coinbase common.Address, batchNumber uint64) (seqsendertypes.Sequence, error) { - return t.condNewSeq.NewSequenceIfWorthToSend(ctx, t, sequenceBatches, t.opts.From, l2Coinbase, batchNumber) -} - -func (t *TxBuilderElderberryZKEVM) NewBatchFromL2Block(l2Block *datastream.L2Block) seqsendertypes.Batch { - batch := ðerman.Batch{ - LastL2BLockTimestamp: l2Block.Timestamp, - BatchNumber: l2Block.BatchNumber, - L1InfoTreeIndex: l2Block.L1InfotreeIndex, - LastCoinbase: common.BytesToAddress(l2Block.Coinbase), - GlobalExitRoot: common.BytesToHash(l2Block.GlobalExitRoot), - } - return NewBananaBatch(batch) -} - func (t *TxBuilderElderberryZKEVM) BuildSequenceBatchesTx(ctx context.Context, sender common.Address, sequences seqsendertypes.Sequence) (*ethtypes.Transaction, error) { newopts := t.opts From e18eb66a1aba57fe1210ea1b971864fb9e2d3976 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Mon, 22 Jul 2024 15:05:04 +0200 Subject: [PATCH 09/51] wip-elderberry validium --- cmd/run.go | 3 +-- dataavailability/dataavailability.go | 4 ++++ dataavailability/interfaces.go | 3 +++ sequencesender/txbuilder/elderberry_base.go | 4 ---- sequencesender/txbuilder/elderberry_validium.go | 6 +++--- sequencesender/txbuilder/elderberry_zkevm.go | 11 ++++------- 6 files changed, 15 insertions(+), 16 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index 1bd65330..6ce6bb36 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -168,8 +168,7 @@ func newTxBuilder(cfg config.Config, ethman *etherman.Client) (txbuilder.TxBuild } case contracts.VersionElderberry: if cfg.Common.IsValidiumMode { - err = fmt.Errorf("Elderberry+Validium not implemented yet") - //txBuilder = txbuilder.NewTxBuilderElderberryValidium(*ethman.Contracts.Elderberry.ZkEVM, da, *auth, auth.From) + txBuilder = txbuilder.NewTxBuilderElderberryValidium(ethman.Contracts.Elderberry.Rollup, da, *auth, auth.From, cfg.SequenceSender.MaxBatchesForL1) } else { txBuilder = txbuilder.NewTxBuilderElderberryZKEVM(ethman.Contracts.Elderberry.Rollup, *auth, auth.From, cfg.SequenceSender.MaxTxSizeForL1) } diff --git a/dataavailability/dataavailability.go b/dataavailability/dataavailability.go index 0e35c3d7..284f0b64 100644 --- a/dataavailability/dataavailability.go +++ b/dataavailability/dataavailability.go @@ -23,3 +23,7 @@ func New(backend DABackender) (*DataAvailability, error) { func (d *DataAvailability) PostSequence(ctx context.Context, sequenceBanana etherman.SequenceBanana) ([]byte, error) { return d.backend.PostSequence(ctx, sequenceBanana) } + +func (d *DataAvailability) PostSequenceElderberry(ctx context.Context, batchesData [][]byte) ([]byte, error) { + return d.backend.PostSequenceElderberry(ctx, batchesData) +} diff --git a/dataavailability/interfaces.go b/dataavailability/interfaces.go index 1ef21408..8292fd71 100644 --- a/dataavailability/interfaces.go +++ b/dataavailability/interfaces.go @@ -20,6 +20,9 @@ type SequenceSender interface { // PostSequence sends the sequence data to the data availability backend, and returns the dataAvailabilityMessage // as expected by the contract PostSequence(ctx context.Context, sequence etherman.SequenceBanana) ([]byte, error) + // PostSequenceElderberry sends the sequence data to the data availability backend, and returns the dataAvailabilityMessage + // as expected by the contract + PostSequenceElderberry(ctx context.Context, batchesData [][]byte) ([]byte, error) } type SequenceSenderElderberry interface { diff --git a/sequencesender/txbuilder/elderberry_base.go b/sequencesender/txbuilder/elderberry_base.go index 79a92202..f643a96c 100644 --- a/sequencesender/txbuilder/elderberry_base.go +++ b/sequencesender/txbuilder/elderberry_base.go @@ -46,7 +46,3 @@ func getLastSequencedBatchNumber(sequences seqsendertypes.Sequence) uint64 { } return sequences.FirstBatch().BatchNumber() - 1 } - -func (t *TxBuilderElderberryZKEVM) String() string { - return "Elderberry/ZKEVM" -} diff --git a/sequencesender/txbuilder/elderberry_validium.go b/sequencesender/txbuilder/elderberry_validium.go index d9d1acd2..3db17879 100644 --- a/sequencesender/txbuilder/elderberry_validium.go +++ b/sequencesender/txbuilder/elderberry_validium.go @@ -24,14 +24,14 @@ type TxBuilderElderberryValidium struct { func NewTxBuilderElderberryValidium(zkevm contracts.RollupElderberryType, da dataavailability.SequenceSenderElderberry, - opts bind.TransactOpts, sender common.Address, maxTxSizeForL1 uint64) *TxBuilderElderberryValidium { + opts bind.TransactOpts, sender common.Address, maxBatchesForL1 uint64) *TxBuilderElderberryValidium { return &TxBuilderElderberryValidium{ da: da, TxBuilderElderberryBase: *NewTxBuilderElderberryBase( zkevm, opts, ), - condNewSeq: &NewSequenceConditionalMaxSize{ - maxTxSizeForL1: maxTxSizeForL1, + condNewSeq: &NewSequenceConditionalNumBatches{ + maxBatchesForL1: maxBatchesForL1, }, } } diff --git a/sequencesender/txbuilder/elderberry_zkevm.go b/sequencesender/txbuilder/elderberry_zkevm.go index 1603193b..39979375 100644 --- a/sequencesender/txbuilder/elderberry_zkevm.go +++ b/sequencesender/txbuilder/elderberry_zkevm.go @@ -31,6 +31,10 @@ func NewTxBuilderElderberryZKEVM(zkevm contracts.RollupElderberryType, opts bind } } +func (t *TxBuilderElderberryZKEVM) NewSequenceIfWorthToSend(ctx context.Context, sequenceBatches []seqsendertypes.Batch, l2Coinbase common.Address, batchNumber uint64) (seqsendertypes.Sequence, error) { + return t.condNewSeq.NewSequenceIfWorthToSend(ctx, t, sequenceBatches, t.opts.From, l2Coinbase, batchNumber) +} + func (t *TxBuilderElderberryZKEVM) BuildSequenceBatchesTx(ctx context.Context, sender common.Address, sequences seqsendertypes.Sequence) (*ethtypes.Transaction, error) { newopts := t.opts @@ -77,13 +81,6 @@ func (t *TxBuilderElderberryZKEVM) warningMessage(batches []polygonvalidiumetrog log.Warnf("Sequencer address: ", opts.From, "l2CoinBase: ", l2Coinbase, " Batches to send: %+v", batches) } -func getLastSequencedBatchNumber(sequences seqsendertypes.Sequence) uint64 { - if sequences.Len() == 0 { - return 0 - } - return sequences.FirstBatch().BatchNumber() - 1 -} - func (t *TxBuilderElderberryZKEVM) String() string { return "Elderberry/ZKEVM" } From 6dd5417223180700acd83bbe5d4d168d767edfc9 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Mon, 22 Jul 2024 18:02:21 +0200 Subject: [PATCH 10/51] add translator to override DAC members URLs --- cmd/run.go | 5 ++ common/config.go | 5 +- .../datacommittee/datacommittee.go | 8 +++ sequencesender/txbuilder/banana_validium.go | 3 +- translator/config.go | 11 +++ translator/translator.go | 5 ++ translator/translator_impl.go | 69 +++++++++++++++++++ 7 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 translator/config.go create mode 100644 translator/translator.go create mode 100644 translator/translator_impl.go diff --git a/cmd/run.go b/cmd/run.go index 6ce6bb36..25f29654 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -23,6 +23,7 @@ import ( "github.com/0xPolygon/cdk/sequencesender/txbuilder" "github.com/0xPolygon/cdk/state" "github.com/0xPolygon/cdk/state/pgstatestorage" + "github.com/0xPolygon/cdk/translator" ethtxman "github.com/0xPolygonHermez/zkevm-ethtx-manager/etherman" "github.com/0xPolygonHermez/zkevm-ethtx-manager/etherman/etherscan" "github.com/jackc/pgx/v4/pgxpool" @@ -183,6 +184,9 @@ func newDataAvailability(c config.Config, etherman *etherman.Client) (*dataavail if !c.Common.IsValidiumMode { return nil, nil } + translator := translator.NewTranslatorImpl() + log.Infof("Translator rules: %v", c.Common.Translator) + translator.AddConfigRules(c.Common.Translator) // Backend specific config daProtocolName, err := etherman.GetDAProtocolName() @@ -210,6 +214,7 @@ func newDataAvailability(c config.Config, etherman *etherman.Client) (*dataavail dacAddr, pk, dataCommitteeClient.NewFactory(), + translator, ) if err != nil { return nil, err diff --git a/common/config.go b/common/config.go index 0fdf14d4..d8f2d1ce 100644 --- a/common/config.go +++ b/common/config.go @@ -1,8 +1,11 @@ package common +import "github.com/0xPolygon/cdk/translator" + type Config struct { // IsValidiumMode has the value true if the sequence sender is running in validium mode. IsValidiumMode bool `mapstructure:"IsValidiumMode"` // Contract Versions: elderberry, banana - ContractVersions string `mapstructure:"ContractVersions"` + ContractVersions string `mapstructure:"ContractVersions"` + Translator translator.Config `mapstructure:"Translator"` } diff --git a/dataavailability/datacommittee/datacommittee.go b/dataavailability/datacommittee/datacommittee.go index c29cd622..bfe9393a 100644 --- a/dataavailability/datacommittee/datacommittee.go +++ b/dataavailability/datacommittee/datacommittee.go @@ -14,6 +14,7 @@ import ( daTypes "github.com/0xPolygon/cdk-data-availability/types" "github.com/0xPolygon/cdk/etherman" "github.com/0xPolygon/cdk/log" + "github.com/0xPolygonHermez/zkevm-synchronizer-l1/translator" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" @@ -22,6 +23,7 @@ import ( ) const unexpectedHashTemplate = "missmatch on transaction data. Expected hash %s, actual hash: %s" +const translateContextName = "dataCommittee" // DataCommitteeMember represents a member of the Data Committee type DataCommitteeMember struct { @@ -45,6 +47,7 @@ type Backend struct { committeeMembers []DataCommitteeMember selectedCommitteeMember int ctx context.Context + Translator translator.Translator } // New creates an instance of Backend @@ -53,6 +56,7 @@ func New( dataCommitteeAddr common.Address, privKey *ecdsa.PrivateKey, dataCommitteeClientFactory client.Factory, + translator translator.Translator, ) (*Backend, error) { ethClient, err := ethclient.Dial(l1RPCURL) if err != nil { @@ -70,6 +74,7 @@ func New( privKey: privKey, dataCommitteeClientFactory: dataCommitteeClientFactory, ctx: context.Background(), + Translator: translator, }, nil } @@ -377,6 +382,9 @@ func (d *Backend) getCurrentDataCommitteeMembers() ([]DataCommitteeMember, error if err != nil { return nil, fmt.Errorf("error getting Members %d from L1 SC: %w", i, err) } + if d.Translator != nil { + member.Url = d.Translator.Translate(translateContextName, member.Url) + } members = append(members, DataCommitteeMember{ Addr: member.Addr, URL: member.Url, diff --git a/sequencesender/txbuilder/banana_validium.go b/sequencesender/txbuilder/banana_validium.go index 08d1750e..d926143a 100644 --- a/sequencesender/txbuilder/banana_validium.go +++ b/sequencesender/txbuilder/banana_validium.go @@ -22,7 +22,8 @@ type TxBuilderBananaValidium struct { condNewSeq CondNewSequence } -func NewTxBuilderBananaValidium(rollupContract contracts.RollupBananaType, gerContract contracts.GlobalExitRootBananaType, +func NewTxBuilderBananaValidium(rollupContract contracts.RollupBananaType, + gerContract contracts.GlobalExitRootBananaType, da dataavailability.SequenceSender, opts bind.TransactOpts, sender common.Address, maxBatchesForL1 uint64) *TxBuilderBananaValidium { return &TxBuilderBananaValidium{ TxBuilderBananaBase: *NewTxBuilderBananaBase(rollupContract, gerContract, opts, sender), diff --git a/translator/config.go b/translator/config.go new file mode 100644 index 00000000..f4a37039 --- /dev/null +++ b/translator/config.go @@ -0,0 +1,11 @@ +package translator + +type ConfigRuleFullMatch struct { + ContextName string `mapstructure:"ContextName"` + Old string `mapstructure:"Old"` + New string `mapstructure:"New"` +} + +type Config struct { + FullMatchRules []ConfigRuleFullMatch `mapstructure:"FullMatchRules"` +} diff --git a/translator/translator.go b/translator/translator.go new file mode 100644 index 00000000..ae6e595b --- /dev/null +++ b/translator/translator.go @@ -0,0 +1,5 @@ +package translator + +type Translator interface { + Translate(contextName string, data string) string +} diff --git a/translator/translator_impl.go b/translator/translator_impl.go new file mode 100644 index 00000000..92326460 --- /dev/null +++ b/translator/translator_impl.go @@ -0,0 +1,69 @@ +package translator + +import "github.com/0xPolygonHermez/zkevm-synchronizer-l1/log" + +type TranslatorFullMatchRule struct { + // If null match any context + ContextName *string + // If null match any data + FullMatchString string + NewString string +} + +func (t *TranslatorFullMatchRule) Match(contextName string, data string) bool { + if t.ContextName != nil && *t.ContextName != contextName { + return false + } + if t.FullMatchString != data { + return false + } + return true +} + +func (t *TranslatorFullMatchRule) Translate(contextName string, data string) string { + return t.NewString +} + +func NewTranslatorFullMatchRule(contextName *string, fullMatchString string, newString string) *TranslatorFullMatchRule { + return &TranslatorFullMatchRule{ + ContextName: contextName, + FullMatchString: fullMatchString, + NewString: newString, + } +} + +type TranslatorImpl struct { + FullMatchRules []TranslatorFullMatchRule +} + +func NewTranslatorImpl() *TranslatorImpl { + return &TranslatorImpl{ + FullMatchRules: []TranslatorFullMatchRule{}, + } +} + +func (t *TranslatorImpl) Translate(contextName string, data string) string { + for _, rule := range t.FullMatchRules { + if rule.Match(contextName, data) { + translated := rule.Translate(contextName, data) + log.Debugf("Translated (ctxName=%s) %s to %s", contextName, data, translated) + return translated + } + } + return data +} + +func (t *TranslatorImpl) AddRule(rule TranslatorFullMatchRule) { + t.FullMatchRules = append(t.FullMatchRules, rule) +} + +func (t *TranslatorImpl) AddConfigRules(cfg Config) { + for _, v := range cfg.FullMatchRules { + var contextName *string + if v.ContextName != "" { + contextName = &v.ContextName + } + rule := NewTranslatorFullMatchRule(contextName, v.Old, v.New) + t.AddRule(*rule) + } +} From c32bf7a7f312b1b8d2e67c2236196471c0870d8d Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Tue, 23 Jul 2024 11:13:53 +0200 Subject: [PATCH 11/51] add auto-config kurtosis environment --- test/config/test.kurtosis_template.toml | 122 ++++++++++++++++++++++++ test/scripts/config_kurtosis.sh | 34 +++++++ 2 files changed, 156 insertions(+) create mode 100644 test/config/test.kurtosis_template.toml create mode 100755 test/scripts/config_kurtosis.sh diff --git a/test/config/test.kurtosis_template.toml b/test/config/test.kurtosis_template.toml new file mode 100644 index 00000000..b4c02f25 --- /dev/null +++ b/test/config/test.kurtosis_template.toml @@ -0,0 +1,122 @@ +ForkUpgradeBatchNumber = 0 +ForkUpgradeNewForkId = 0 + +[Common] +IsValidiumMode = true +ContractVersions = "elderberry" +[Common.Translator] + FullMatchRules = [ + {Old="http://zkevm-dac-001:8484", New="http://127.0.0.1:${dac_port}"}, + ] + +[Log] +Environment = "development" # "production" or "development" +Level = "info" +Outputs = ["stderr"] + +[SequenceSender] +WaitPeriodSendSequence = "15s" +LastBatchVirtualizationTimeMaxWaitPeriod = "10s" +L1BlockTimestampMargin = "30s" +MaxTxSizeForL1 = 131072 +L2Coinbase = "${zkevm_l2_sequencer_address}" +#PrivateKey = {Path = "../test/sequencer.keystore", Password = "testonly"} +PrivateKey = {Path = "/tmp/seq_sender/sequencer.keystore", Password = "${zkevm_l2_keystore_password}"} + +SequencesTxFileName = "sequencesender.json" +GasOffset = 80000 +WaitPeriodPurgeTxFile = "15m" +MaxPendingTx = 1 + [SequenceSender.StreamClient] + Server = "127.0.0.1:${zkevm_data_streamer_port}" + [SequenceSender.EthTxManager] + FrequencyToMonitorTxs = "1s" + WaitTxToBeMined = "2m" + GetReceiptMaxTime = "250ms" + GetReceiptWaitInterval = "1s" + PrivateKeys = [ + {Path = "../test/sequencer.keystore", Password = "testonly"}, + ] + ForcedGas = 0 + GasPriceMarginFactor = 1 + MaxGasPriceLimit = 0 + PersistenceFilename = "ethtxmanager.json" + ReadPendingL1Txs = false + SafeStatusL1NumberOfBlocks = 0 + FinalizedStatusL1NumberOfBlocks = 0 + [SequenceSender.EthTxManager.Etherman] + URL = "http://127.0.0.1:${l1_rpc_port}" + MultiGasProvider = false + L1ChainID = 1337 +[Aggregator] +Host = "0.0.0.0" +Port = 50081 +RetryTime = "5s" +VerifyProofInterval = "10s" +TxProfitabilityCheckerType = "acceptall" +TxProfitabilityMinReward = "1.1" +ProofStatePollingInterval = "5s" +SenderAddress = "" +CleanupLockedProofsInterval = "2m" +GeneratingProofCleanupThreshold = "10m" +ForkId = 9 +GasOffset = 0 +WitnessURL = "localhost:8123" +UseL1BatchData = true +UseFullWitness = false +SettlementBackend = "l1" +AggLayerTxTimeout = "5m" +AggLayerURL = "" +SequencerPrivateKey = {} + [Aggregator.DB] + Name = "aggregator_db" + User = "aggregator_user" + Password = "aggregator_password" + Host = "cdk-aggregator-db" + Port = "5432" + EnableLog = false + MaxConns = 200 + [Aggregator.Log] + Environment = "development" # "production" or "development" + Level = "info" + Outputs = ["stderr"] + [Aggregator.StreamClient] + Server = "localhost:6900" + [Aggregator.EthTxManager] + FrequencyToMonitorTxs = "1s" + WaitTxToBeMined = "2m" + GetReceiptMaxTime = "250ms" + GetReceiptWaitInterval = "1s" + PrivateKeys = [ + {Path = "/pk/aggregator.keystore", Password = "testonly"}, + ] + ForcedGas = 0 + GasPriceMarginFactor = 1 + MaxGasPriceLimit = 0 + PersistenceFilename = "" + ReadPendingL1Txs = false + SafeStatusL1NumberOfBlocks = 0 + FinalizedStatusL1NumberOfBlocks = 0 + [Aggregator.EthTxManager.Etherman] + URL = "" + L1ChainID = 11155111 + HTTPHeaders = [] + [Aggregator.Synchronizer] + [Aggregator.Synchronizer.DB] + Name = "sync_db" + User = "sync_user" + Password = "sync_password" + Host = "cdk-l1-sync-db" + Port = "5432" + EnableLog = false + MaxConns = 10 + [Aggregator.Synchronizer.Synchronizer] + SyncInterval = "10s" + SyncChunkSize = 1000 + GenesisBlockNumber = 5511080 + SyncUpToBlock = "finalized" + BlockFinality = "finalized" + OverrideStorageCheck = false + [Aggregator.Synchronizer.Etherman] + [Aggregator.Synchronizer.Etherman.Validium] + Enabled = false diff --git a/test/scripts/config_kurtosis.sh b/test/scripts/config_kurtosis.sh new file mode 100755 index 00000000..0073f0d1 --- /dev/null +++ b/test/scripts/config_kurtosis.sh @@ -0,0 +1,34 @@ +#!/bin/bash +ENCLAVE=cdk-v1 +DEST=/tmp/seq_sender + +build_vars_file(){ + cat > $DEST/vars.json < $DEST/test.kurtosis.toml +#kurtosis files rendertemplate $ENCLAVE config/test.kurtosis_template.toml $DEST/vars.json config/test.kurtosis.toml \ No newline at end of file From b9e4e022f2d246f3de56d2c6de1c1343e56575e6 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Tue, 23 Jul 2024 15:37:17 +0200 Subject: [PATCH 12/51] fix bugs, fix kurtosis config --- cmd/run.go | 2 +- sequencesender/sequencesender.go | 3 ++- .../txbuilder/elderberry_validium.go | 18 +++++++++++++++--- test/config/test.kurtosis_template.toml | 4 ++-- test/scripts/config_kurtosis.sh | 2 +- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index 25f29654..e1d94bd1 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -128,7 +128,7 @@ func createSequenceSender(cfg config.Config) *sequencesender.SequenceSender { }, }, cfg.NetworkConfig.L1Config, cfg.Common) if err != nil { - log.Fatal(err) + log.Fatalf("Failed to connect create etherman. Err: %w, ", err) } auth, _, err := ethman.LoadAuthFromKeyStore(cfg.SequenceSender.PrivateKey.Path, cfg.SequenceSender.PrivateKey.Password) diff --git a/sequencesender/sequencesender.go b/sequencesender/sequencesender.go index 70b5fc24..78bcea2c 100644 --- a/sequencesender/sequencesender.go +++ b/sequencesender/sequencesender.go @@ -104,6 +104,7 @@ func New(cfg Config, etherman *etherman.Client, txBuilder txbuilder.TxBuilder) ( Level: cfg.Log.Level, Outputs: cfg.Log.Outputs, } + s.ethTxManager, err = ethtxmanager.New(cfg.EthTxManager) if err != nil { log.Fatalf("[SeqSender] error creating ethtxmanager client: %v", err) @@ -516,7 +517,7 @@ func (s *SequenceSender) tryToSendSequence(ctx context.Context) { } // Send sequences to L1 - log.Infof("[SeqSender] sending sequences to L1. From batch %d to batch %d", firstSequence.BatchNumber, lastSequence.BatchNumber) + log.Infof("[SeqSender] sending sequences to L1. From batch %d to batch %d", firstSequence.BatchNumber(), lastSequence.BatchNumber()) log.Infof(sequence.String()) tx, err := s.TxBuilder.BuildSequenceBatchesTx(ctx, s.cfg.SenderAddress, sequence) diff --git a/sequencesender/txbuilder/elderberry_validium.go b/sequencesender/txbuilder/elderberry_validium.go index 3db17879..a0369191 100644 --- a/sequencesender/txbuilder/elderberry_validium.go +++ b/sequencesender/txbuilder/elderberry_validium.go @@ -2,6 +2,8 @@ package txbuilder import ( "context" + "encoding/hex" + "math/big" "github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry/polygonvalidiumetrog" "github.com/0xPolygon/cdk/dataavailability" @@ -47,10 +49,18 @@ func (t *TxBuilderElderberryValidium) BuildSequenceBatchesTx(ctx context.Context log.Error("error posting sequences to the data availability protocol: ", err) return nil, err } - return t.buildSequenceBatchesTxValidium(sequences, dataAvailabilityMessage) + newopts := t.opts + newopts.NoSend = true + + // force nonce, gas limit and gas price to avoid querying it from the chain + newopts.Nonce = big.NewInt(1) + newopts.GasLimit = uint64(1) + newopts.GasPrice = big.NewInt(1) + + return t.buildSequenceBatchesTxValidium(&newopts, sequences, dataAvailabilityMessage) } -func (t *TxBuilderElderberryValidium) buildSequenceBatchesTxValidium( +func (t *TxBuilderElderberryValidium) buildSequenceBatchesTxValidium(opts *bind.TransactOpts, sequences seqsendertypes.Sequence, dataAvailabilityMessage []byte) (*types.Transaction, error) { batches := make([]polygonvalidiumetrog.PolygonValidiumEtrogValidiumBatchData, sequences.Len()) for i, seq := range sequences.Batches() { @@ -67,7 +77,9 @@ func (t *TxBuilderElderberryValidium) buildSequenceBatchesTxValidium( } lastSequencedBatchNumber := getLastSequencedBatchNumber(sequences) ZkEVM := t.rollupContract.Contract() - tx, err := ZkEVM.SequenceBatchesValidium(&t.opts, batches, sequences.MaxSequenceTimestamp(), + log.Infof("SequenceBatchesValidium(from=%s, len(batches)=%d, MaxSequenceTimestamp=%d, lastSequencedBatchNumber=%d, L2Coinbase=%s, dataAvailabilityMessage=%s)", + t.opts.From.String(), len(batches), sequences.MaxSequenceTimestamp(), lastSequencedBatchNumber, sequences.L2Coinbase().String(), hex.EncodeToString(dataAvailabilityMessage)) + tx, err := ZkEVM.SequenceBatchesValidium(opts, batches, sequences.MaxSequenceTimestamp(), lastSequencedBatchNumber, sequences.L2Coinbase(), dataAvailabilityMessage) if err != nil { if parsedErr, ok := etherman.TryParseError(err); ok { diff --git a/test/config/test.kurtosis_template.toml b/test/config/test.kurtosis_template.toml index b4c02f25..81dddbf5 100644 --- a/test/config/test.kurtosis_template.toml +++ b/test/config/test.kurtosis_template.toml @@ -35,7 +35,7 @@ MaxPendingTx = 1 GetReceiptMaxTime = "250ms" GetReceiptWaitInterval = "1s" PrivateKeys = [ - {Path = "../test/sequencer.keystore", Password = "testonly"}, + {Path = "/tmp/seq_sender/sequencer.keystore", Password = "${zkevm_l2_keystore_password}"}, ] ForcedGas = 0 GasPriceMarginFactor = 1 @@ -47,7 +47,7 @@ MaxPendingTx = 1 [SequenceSender.EthTxManager.Etherman] URL = "http://127.0.0.1:${l1_rpc_port}" MultiGasProvider = false - L1ChainID = 1337 + L1ChainID = ${zkevm_l1_chain_id} [Aggregator] Host = "0.0.0.0" Port = 50081 diff --git a/test/scripts/config_kurtosis.sh b/test/scripts/config_kurtosis.sh index 0073f0d1..b2066ea7 100755 --- a/test/scripts/config_kurtosis.sh +++ b/test/scripts/config_kurtosis.sh @@ -27,7 +27,7 @@ export zkevm_data_streamer_port=$(kurtosis enclave inspect $ENCLAVE | grep "data kurtosis files download $ENCLAVE zkevm-sequence-sender-config-artifact $DEST export zkevm_l2_sequencer_address=$(cat $DEST/config.toml |grep L2Coinbase | cut -f 2 -d "="| tr -d '"' | tr -d ' ') export zkevm_l2_keystore_password=$(cat $DEST/config.toml |grep -A1 L2Coinbase | tr ',' '\n' | grep Password | cut -f 2 -d '=' | tr -d '}' | tr -d '"' | tr -d ' ') - +export zkevm_l1_chain_id=$(cat $DEST/config.toml |grep L1ChainID | cut -f 2 -d '=') #build_vars_file envsubst < test/config/test.kurtosis_template.toml > $DEST/test.kurtosis.toml From 037e7053bc61ae9ae477e71bd1d8d1dca4900ed0 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Tue, 23 Jul 2024 15:45:52 +0200 Subject: [PATCH 13/51] maxbatchesforL1=0 -> disabled --- sequencesender/txbuilder/validium_cond_num_batches.go | 2 +- test/config/test.kurtosis_template.toml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/sequencesender/txbuilder/validium_cond_num_batches.go b/sequencesender/txbuilder/validium_cond_num_batches.go index a80fed1c..da663063 100644 --- a/sequencesender/txbuilder/validium_cond_num_batches.go +++ b/sequencesender/txbuilder/validium_cond_num_batches.go @@ -13,7 +13,7 @@ type NewSequenceConditionalNumBatches struct { } func (c *NewSequenceConditionalNumBatches) NewSequenceIfWorthToSend(ctx context.Context, txBuilder TxBuilder, sequenceBatches []seqsendertypes.Batch, senderAddress, l2Coinbase common.Address, batchNumber uint64) (seqsendertypes.Sequence, error) { - if len(sequenceBatches) >= int(c.maxBatchesForL1) { + if c.maxBatchesForL1 > 0 && len(sequenceBatches) >= int(c.maxBatchesForL1) { log.Infof( "[SeqSender] sequence should be sent to L1, because MaxBatchesForL1 (%d) has been reached", c.maxBatchesForL1, diff --git a/test/config/test.kurtosis_template.toml b/test/config/test.kurtosis_template.toml index 81dddbf5..b4ff3066 100644 --- a/test/config/test.kurtosis_template.toml +++ b/test/config/test.kurtosis_template.toml @@ -19,6 +19,7 @@ WaitPeriodSendSequence = "15s" LastBatchVirtualizationTimeMaxWaitPeriod = "10s" L1BlockTimestampMargin = "30s" MaxTxSizeForL1 = 131072 +MaxBatchesForL1 = 2 L2Coinbase = "${zkevm_l2_sequencer_address}" #PrivateKey = {Path = "../test/sequencer.keystore", Password = "testonly"} PrivateKey = {Path = "/tmp/seq_sender/sequencer.keystore", Password = "${zkevm_l2_keystore_password}"} From ff9407abd181211e7303302b854f127e4ef28fa0 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Wed, 24 Jul 2024 09:50:20 +0200 Subject: [PATCH 14/51] wip --- sequencesender/txbuilder/zkevm_cond_max_size.go | 1 + test/config/test.kurtosis_template.toml | 8 ++++---- test/scripts/config_kurtosis.sh | 9 +++++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/sequencesender/txbuilder/zkevm_cond_max_size.go b/sequencesender/txbuilder/zkevm_cond_max_size.go index 4a41f3d6..3218d015 100644 --- a/sequencesender/txbuilder/zkevm_cond_max_size.go +++ b/sequencesender/txbuilder/zkevm_cond_max_size.go @@ -49,6 +49,7 @@ func (c *NewSequenceConditionalMaxSize) NewSequenceIfWorthToSend(ctx context.Con return sequence, err } + log.Infof("[SeqSender] [MaxSize] current size:%d max_size:%d num_batches: %d", tx.Size(), c.maxTxSizeForL1, sequence.Len()) return nil, nil } diff --git a/test/config/test.kurtosis_template.toml b/test/config/test.kurtosis_template.toml index b4ff3066..25abf430 100644 --- a/test/config/test.kurtosis_template.toml +++ b/test/config/test.kurtosis_template.toml @@ -2,7 +2,7 @@ ForkUpgradeBatchNumber = 0 ForkUpgradeNewForkId = 0 [Common] -IsValidiumMode = true +IsValidiumMode = ${zkevm_is_validium} ContractVersions = "elderberry" [Common.Translator] FullMatchRules = [ @@ -48,7 +48,7 @@ MaxPendingTx = 1 [SequenceSender.EthTxManager.Etherman] URL = "http://127.0.0.1:${l1_rpc_port}" MultiGasProvider = false - L1ChainID = ${zkevm_l1_chain_id} + L1ChainID = ${l1_chain_id} [Aggregator] Host = "0.0.0.0" Port = 50081 @@ -100,7 +100,7 @@ SequencerPrivateKey = {} FinalizedStatusL1NumberOfBlocks = 0 [Aggregator.EthTxManager.Etherman] URL = "" - L1ChainID = 11155111 + L1ChainID = ${l1_chain_id} HTTPHeaders = [] [Aggregator.Synchronizer] [Aggregator.Synchronizer.DB] @@ -120,4 +120,4 @@ SequencerPrivateKey = {} OverrideStorageCheck = false [Aggregator.Synchronizer.Etherman] [Aggregator.Synchronizer.Etherman.Validium] - Enabled = false + Enabled = ${zkevm_is_validium} diff --git a/test/scripts/config_kurtosis.sh b/test/scripts/config_kurtosis.sh index b2066ea7..009c59ba 100755 --- a/test/scripts/config_kurtosis.sh +++ b/test/scripts/config_kurtosis.sh @@ -27,8 +27,13 @@ export zkevm_data_streamer_port=$(kurtosis enclave inspect $ENCLAVE | grep "data kurtosis files download $ENCLAVE zkevm-sequence-sender-config-artifact $DEST export zkevm_l2_sequencer_address=$(cat $DEST/config.toml |grep L2Coinbase | cut -f 2 -d "="| tr -d '"' | tr -d ' ') export zkevm_l2_keystore_password=$(cat $DEST/config.toml |grep -A1 L2Coinbase | tr ',' '\n' | grep Password | cut -f 2 -d '=' | tr -d '}' | tr -d '"' | tr -d ' ') -export zkevm_l1_chain_id=$(cat $DEST/config.toml |grep L1ChainID | cut -f 2 -d '=') +export l1_chain_id=$(cat $DEST/config.toml |grep L1ChainID | cut -f 2 -d '=') +export zkevm_is_validium=$(cat $DEST/config.toml |grep IsValidiumMode | cut -f 2 -d '=') #build_vars_file envsubst < test/config/test.kurtosis_template.toml > $DEST/test.kurtosis.toml -#kurtosis files rendertemplate $ENCLAVE config/test.kurtosis_template.toml $DEST/vars.json config/test.kurtosis.toml \ No newline at end of file + +echo "rember to stop sequence-sender:" +echo "-----------------------------------------------------------" +echo "kurtosis service stop cdk-v1 zkevm-node-sequence-sender-001" + From c0634c2714a86294a978b098d97ae7cefc6ba636 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Wed, 24 Jul 2024 10:35:14 +0200 Subject: [PATCH 15/51] fix script --- test/config/test.kurtosis_template.toml | 5 +- test/scripts/config_kurtosis.sh | 39 ----------- test/scripts/config_kurtosis_for_local_run.sh | 67 +++++++++++++++++++ 3 files changed, 69 insertions(+), 42 deletions(-) delete mode 100755 test/scripts/config_kurtosis.sh create mode 100755 test/scripts/config_kurtosis_for_local_run.sh diff --git a/test/config/test.kurtosis_template.toml b/test/config/test.kurtosis_template.toml index 25abf430..061a4838 100644 --- a/test/config/test.kurtosis_template.toml +++ b/test/config/test.kurtosis_template.toml @@ -21,8 +21,7 @@ L1BlockTimestampMargin = "30s" MaxTxSizeForL1 = 131072 MaxBatchesForL1 = 2 L2Coinbase = "${zkevm_l2_sequencer_address}" -#PrivateKey = {Path = "../test/sequencer.keystore", Password = "testonly"} -PrivateKey = {Path = "/tmp/seq_sender/sequencer.keystore", Password = "${zkevm_l2_keystore_password}"} +PrivateKey = {Path = "${sequencer_keystore_file}", Password = "${zkevm_l2_keystore_password}"} SequencesTxFileName = "sequencesender.json" GasOffset = 80000 @@ -36,7 +35,7 @@ MaxPendingTx = 1 GetReceiptMaxTime = "250ms" GetReceiptWaitInterval = "1s" PrivateKeys = [ - {Path = "/tmp/seq_sender/sequencer.keystore", Password = "${zkevm_l2_keystore_password}"}, + {Path = "${sequencer_keystore_file}", Password = "${zkevm_l2_keystore_password}"}, ] ForcedGas = 0 GasPriceMarginFactor = 1 diff --git a/test/scripts/config_kurtosis.sh b/test/scripts/config_kurtosis.sh deleted file mode 100755 index 009c59ba..00000000 --- a/test/scripts/config_kurtosis.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash -ENCLAVE=cdk-v1 -DEST=/tmp/seq_sender - -build_vars_file(){ - cat > $DEST/vars.json < $DEST/test.kurtosis.toml - -echo "rember to stop sequence-sender:" -echo "-----------------------------------------------------------" -echo "kurtosis service stop cdk-v1 zkevm-node-sequence-sender-001" - diff --git a/test/scripts/config_kurtosis_for_local_run.sh b/test/scripts/config_kurtosis_for_local_run.sh new file mode 100755 index 00000000..424858bc --- /dev/null +++ b/test/scripts/config_kurtosis_for_local_run.sh @@ -0,0 +1,67 @@ +#!/bin/bash +#Include common varaibles +source $(dirname $0)/env.sh + + + + +############################################################################### +# MAIN +############################################################################### +set -o pipefail # enable strict command pipe error detection + +if [ -z $TMP_CDK_FOLDER -o -z $ENCLAVE ]; then + echo "TMP_CDK_FOLDER or ENCLAVE is not set. Must be set on file env.sh" + exit 1 +fi +DEST=${TMP_CDK_FOLDER}/local_config + +[ ! -d ${DEST} ] && mkdir -p ${DEST} +rm $DEST/* +kurtosis files download cdk-v1 genesis $DEST +[ $? -ne 0 ] && echo "Error downloading genesis" && exit 1 +export genesis_file=$DEST/genesis.json +kurtosis files download cdk-v1 sequencer-keystore $DEST +[ $? -ne 0 ] && echo "Error downloading sequencer-keystore" && exit 1 +export sequencer_keystore_file=$DEST/sequencer.keystore + +l1_rpc_port=$(kurtosis port print $ENCLAVE el-1-geth-lighthouse rpc | cut -f 3 -d ":") +[ $? -ne 0 ] && echo "Error getting l1_rpc_port" && exit 1 || export l1_rpc_port && echo "l1_rpc_port=$l1_rpc_port" + +zkevm_data_streamer_port=$(kurtosis port print $ENCLAVE cdk-erigon-sequencer-001 data-streamer | cut -f 3 -d ":") +[ $? -ne 0 ] && echo "Error getting zkevm_data_streamer_port" && exit 1 || export zkevm_data_streamer_port && echo "zkevm_data_streamer_port=$zkevm_data_streamer_port" + +kurtosis files download $ENCLAVE zkevm-sequence-sender-config-artifact $DEST +export zkevm_l2_sequencer_address=$(cat $DEST/config.toml |grep L2Coinbase | cut -f 2 -d "="| tr -d '"' | tr -d ' ') +export zkevm_l2_keystore_password=$(cat $DEST/config.toml |grep -A1 L2Coinbase | tr ',' '\n' | grep Password | cut -f 2 -d '=' | tr -d '}' | tr -d '"' | tr -d ' ') +export l1_chain_id=$(cat $DEST/config.toml |grep L1ChainID | cut -f 2 -d '=') +export zkevm_is_validium=$(cat $DEST/config.toml |grep IsValidiumMode | cut -f 2 -d '=') + +if [ "$zkevm_is_validium" == "true" ]; then + dac_port=$(kurtosis port print $ENCLAVE zkevm-dac http-rpc | cut -f 3 -d ":") + [ $? -ne 0 ] && echo "Error getting dac_port" && exit 1 || export dac_port && echo "dac_port=$dac_port" +fi + +envsubst < test/config/test.kurtosis_template.toml > $DEST/test.kurtosis.toml + +echo "- start kurtosis" +echo " kurtosis clean --all; kurtosis run --enclave cdk-v1 --args-file cdk-erigon-sequencer-params.yml --image-download always ." +echo " " +echo "- Stop sequence-sender" +echo " kurtosis service stop cdk-v1 zkevm-node-sequence-sender-001" +echo " " +echo "- Add next configuration to vscode launch.json" +cat << EOF + { + "name": "run local_docker", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "cmd/", + "args":["run","-cfg","$DEST/test.kurtosis.toml", + "--components", "sequence-sender", + "--custom-network-file", "$DEST/local_config/genesis.json" + ] + }, +EOF + From fc950055e4729c0ca7f50189e31ae17340c73ad1 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Wed, 24 Jul 2024 14:24:36 +0200 Subject: [PATCH 16/51] add e2e test --- .github/test-e2e.yml | 40 +++++++++++++++++++++ Dockerfile | 2 +- test/Makefile | 23 ++++++++++++ test/config/test.kurtosis_template.toml | 1 + test/run-e2e-seq_sender.sh | 32 +++++++++++++++++ test/scripts/clone_kurtosis.sh | 31 ++++++++++++++++ test/scripts/env.sh | 5 +++ test/scripts/get_kurtosis_clone_folder.sh | 11 ++++++ test/scripts/kurtosis_prepare_params_yml.sh | 35 ++++++++++++++++++ 9 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 .github/test-e2e.yml create mode 100644 test/Makefile create mode 100755 test/run-e2e-seq_sender.sh create mode 100755 test/scripts/clone_kurtosis.sh create mode 100644 test/scripts/env.sh create mode 100755 test/scripts/get_kurtosis_clone_folder.sh create mode 100755 test/scripts/kurtosis_prepare_params_yml.sh diff --git a/.github/test-e2e.yml b/.github/test-e2e.yml new file mode 100644 index 00000000..06a695ad --- /dev/null +++ b/.github/test-e2e.yml @@ -0,0 +1,40 @@ +--- +name: Test e2e +on: + push: + branches: + - main + - master + - develop + - update-external-dependencies + - 'release/**' + pull_request: + +jobs: + test-e2e: + strategy: + fail-fast: false + matrix: + go-version: [ 1.21.x ] + goarch: [ "amd64" ] + e2e-group: [ "elderberry-validium", "elderberry-rollup" ] + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 + + - name: Install Go + uses: actions/setup-go@v3 + with: + go-version: ${{ matrix.go-version }} + env: + GOARCH: ${{ matrix.goarch }} + + - name: Build Docker + run: make build-docker + + + + - name: Test + run: make test-e2e-${{ matrix.e2e-group }} + working-directory: test diff --git a/Dockerfile b/Dockerfile index 19aef335..294dd305 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,6 +14,6 @@ RUN cd /src && make build # CONTAINER FOR RUNNING BINARY FROM alpine:3.18.4 COPY --from=build /src/dist/cdk /app/cdk -RUN apk update && apk add postgresql15-client +RUN mkdir /app/data && apk update && apk add postgresql15-client EXPOSE 8123 CMD ["/bin/sh", "-c", "/app/cdk run"] diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 00000000..43facfe5 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,23 @@ +.PHONY: test-e2e-elderberry-validium +test-e2e-elderberry-validium: stop ## Runs e2e tests checking elderberry/validium + ./run-e2e-seq_sender.sh cdk-validium + +.PHONY: test-e2e-elderberry-rollup +test-e2e-elderberry-rollup: stop ## Runs e2e tests checking elderberry/rollup + ./run-e2e-seq_sender.sh rollup + +.PHONY: stop +stop: + kurtosis clean --all + + +## Help display. +## Pulls comments from beside commands and prints a nicely formatted +## display with the commands and their usage information. +.DEFAULT_GOAL := help + +.PHONY: help +help: ## Prints this help + @grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \ + | sort \ + | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' diff --git a/test/config/test.kurtosis_template.toml b/test/config/test.kurtosis_template.toml index 061a4838..b2f7f68f 100644 --- a/test/config/test.kurtosis_template.toml +++ b/test/config/test.kurtosis_template.toml @@ -15,6 +15,7 @@ Level = "info" Outputs = ["stderr"] [SequenceSender] +IsValidiumMode={{.zkevm_is_validium}} WaitPeriodSendSequence = "15s" LastBatchVirtualizationTimeMaxWaitPeriod = "10s" L1BlockTimestampMargin = "30s" diff --git a/test/run-e2e-seq_sender.sh b/test/run-e2e-seq_sender.sh new file mode 100755 index 00000000..222a0131 --- /dev/null +++ b/test/run-e2e-seq_sender.sh @@ -0,0 +1,32 @@ +#!/bin/bash +source $(dirname $0)/scripts/env.sh +FORK=elderberry +DATA_AVAILABILITY_MODE=$1 +if [ -z $DATA_AVAILABILITY_MODE ]; then + echo "Missing DATA_AVAILABILITY_MODE: ['rollup', 'cdk-validium']" + exit 1 +fi +KURTOSIS_VERSION=jesteban/cdk-seq_sender +BASE_FOLDER=$(dirname $0) +KURTOSIS_FOLDER=$($BASE_FOLDER/scripts/get_kurtosis_clone_folder.sh $KURTOSIS_VERSION) +[ $? -ne 0 ] && echo "Error getting kurtosis folder" && exit 1 + +$BASE_FOLDER/scripts/clone_kurtosis.sh $KURTOSIS_VERSION "$KURTOSIS_FOLDER" +[ $? -ne 0 ] && echo "Error cloning kurtosis " && exit 1 + +docker images -q cdk:latest > /dev/null +if [ $? -ne 0 ] ; then + echo "Building cdk:latest" + pushd $BASE_FOLDER/.. + make build-docker + popd +else + echo "docker cdk:latest already exists" +fi + +$BASE_FOLDER/scripts/kurtosis_prepare_params_yml.sh "$KURTOSIS_FOLDER" "elderberry" "cdk-validium" +[ $? -ne 0 ] && echo "Error preparing params.yml" && exit 1 + +kurtosis clean --all +kurtosis run --enclave cdk-v1 --args-file $DEST_KURTOSIS_PARAMS_YML --image-download always $KURTOSIS_FOLDER +[ $? -ne 0 ] && echo "Error running kurtosis" && exit 1 \ No newline at end of file diff --git a/test/scripts/clone_kurtosis.sh b/test/scripts/clone_kurtosis.sh new file mode 100755 index 00000000..f4505e75 --- /dev/null +++ b/test/scripts/clone_kurtosis.sh @@ -0,0 +1,31 @@ +#!/bin/bash +source $(dirname $0)/env.sh + +usage() { + echo "Usage: $0 " + echo "Clones kurtosis-cdk version to folder " +} + +KURTOSIS_CDK_URL="git@github.com:0xPolygon/kurtosis-cdk.git" +KURTOSIS_VERSION=$1 +if [ -z $KURTOSIS_VERSION ]; then + echo "Missing param KURTOSIS_VERSION" + usage + exit 1 +fi +DEST_FOLDER="$2" +if [ -z $DEST_FOLDER ]; then + echo "Missing param Destination Folder" + usage + exit 1 +fi +if [ -d $DEST_FOLDER ]; then + echo "Folder $DEST_FOLDER already exists. No cloning needed" + pushd $DEST_FOLDER > /dev/null + echo "Pulling latest changes" + git pull + popd > /dev/null + exit 0 +fi +echo "Cloning kurtosis-cdk version $KURTOSIS_VERSION to folder $DEST_FOLDER" +git clone --depth=1 $KURTOSIS_CDK_URL -b "$KURTOSIS_VERSION" "$DEST_FOLDER" \ No newline at end of file diff --git a/test/scripts/env.sh b/test/scripts/env.sh new file mode 100644 index 00000000..bb814ff9 --- /dev/null +++ b/test/scripts/env.sh @@ -0,0 +1,5 @@ +#!/bin/bash +### Common variables +ENCLAVE=cdk-v1 +TMP_CDK_FOLDER=/tmp/cdk +DEST_KURTOSIS_PARAMS_YML=$TMP_CDK_FOLDER/e2e-params.yml \ No newline at end of file diff --git a/test/scripts/get_kurtosis_clone_folder.sh b/test/scripts/get_kurtosis_clone_folder.sh new file mode 100755 index 00000000..0a1946b5 --- /dev/null +++ b/test/scripts/get_kurtosis_clone_folder.sh @@ -0,0 +1,11 @@ +#!/bin/bash +source $(dirname $0)/env.sh + +KURTOSIS_VERSION=$1 +if [ -z $KURTOSIS_VERSION ]; then + echo "KURTOSIS_VERSION is not set. Must be set on file env.sh" + exit 1 +fi +KURTOSIS_VERSION_FOLDER_NAME=$(echo $KURTOSIS_VERSION | tr -c '[:alnum:]._-' '_') +DEST_FOLDER=$TMP_CDK_FOLDER/kurtosis-cdk-$KURTOSIS_VERSION_FOLDER_NAME +echo $DEST_FOLDER \ No newline at end of file diff --git a/test/scripts/kurtosis_prepare_params_yml.sh b/test/scripts/kurtosis_prepare_params_yml.sh new file mode 100755 index 00000000..3921deaa --- /dev/null +++ b/test/scripts/kurtosis_prepare_params_yml.sh @@ -0,0 +1,35 @@ +#!/bin/bash +source $(dirname $0)/env.sh + +if [ -z $DEST_KURTOSIS_PARAMS_YML ]; then + echo "DEST_KURTOSIS_PARAMS_YML is not set. Must be set on file env.sh" + exit 1 +fi + +KURTOSIS_FOLDER=$1 +if [ -z $KURTOSIS_FOLDER ]; then + echo "Missing param Kurtosis Folder" + exit 1 +fi + +FORK_NAME=$2 +if [ -z $FORK_NAME ]; then + echo "Missing param Fork Name" + exit 1 +fi +DATA_AVAILABILITY_MODE=$3 +if [ -z $DATA_AVAILABILITY_MODE ]; then + echo "Missing param Data Availability Mode : [rollup, cdk-validium]" + exit 1 +fi + + + + +cp $KURTOSIS_FOLDER/cdk-erigon-sequencer-params.yml $DEST_KURTOSIS_PARAMS_YML +yq -Y --in-place ".args.data_availability_mode = \"$DATA_AVAILABILITY_MODE\"" $DEST_KURTOSIS_PARAMS_YML +yq -Y --in-place ".args.zkevm_sequence_sender_image = \"cdk:latest\"" $DEST_KURTOSIS_PARAMS_YML +yq -Y --in-place ".args.sequencer_type = \"erigon\"" $DEST_KURTOSIS_PARAMS_YML +yq -Y --in-place ".args.deploy_cdk_erigon_node = true" $DEST_KURTOSIS_PARAMS_YML +yq -Y --in-place ".args.sequencer_type = \"erigon\"" $DEST_KURTOSIS_PARAMS_YML +yq -Y --in-place ".args.sequencer_sender_type = \"cdk\"" $DEST_KURTOSIS_PARAMS_YML \ No newline at end of file From 3f2c70943a51f07d1fcfc288a6d12c13c8ad60e0 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Wed, 24 Jul 2024 14:54:16 +0200 Subject: [PATCH 17/51] comments on PR --- etherman/contracts/interfaces.go | 59 --------------------------- sequencesender/sequencesender_test.go | 8 ---- 2 files changed, 67 deletions(-) diff --git a/etherman/contracts/interfaces.go b/etherman/contracts/interfaces.go index 87061874..e5bfe39c 100644 --- a/etherman/contracts/interfaces.go +++ b/etherman/contracts/interfaces.go @@ -2,12 +2,6 @@ package contracts import ( "errors" - "fmt" - "math/big" - - "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" ) type NameType string @@ -29,56 +23,3 @@ const ( VersionBanana VersionType = "banana" VersionElderberry VersionType = "elderberry" ) - -type RollupContractor interface { - TrustedSequencer() (common.Address, error) - TrustedSequencerURL() (string, error) - LastAccInputHash() (common.Hash, error) - DataAvailabilityProtocol() (common.Address, error) -} - -type GlobalExitRootContractor interface { - // L1InfoIndexToRoot: get the root for a specific leaf index - L1InfoIndexToRoot(indexLeaf uint32) (common.Hash, error) -} - -type BaseContractor interface { - Name() string - Version() string - Address() common.Address - String() string -} - -type RollupData struct { - RollupContract common.Address - ChainID uint64 - Verifier common.Address - ForkID uint64 - LastLocalExitRoot [32]byte - LastBatchSequenced uint64 - LastVerifiedBatch uint64 - LastPendingState uint64 - LastPendingStateConsolidated uint64 - LastVerifiedBatchBeforeUpgrade uint64 - RollupTypeID uint64 - RollupCompatibilityID uint8 -} - -func (r *RollupData) String() string { - return fmt.Sprintf("RollupContract: %s, ChainID: %d, Verifier: %s, ForkID: %d, LastLocalExitRoot: %x, LastBatchSequenced: %d, LastVerifiedBatch: %d, LastPendingState: %d, LastPendingStateConsolidated: %d, LastVerifiedBatchBeforeUpgrade: %d, RollupTypeID: %d, RollupCompatibilityID: %d", r.RollupContract.String(), r.ChainID, r.Verifier.String(), r.ForkID, r.LastLocalExitRoot, r.LastBatchSequenced, r.LastVerifiedBatch, r.LastPendingState, r.LastPendingStateConsolidated, r.LastVerifiedBatchBeforeUpgrade, r.RollupTypeID, r.RollupCompatibilityID) -} - -type StateVariablesSequencedBatchData struct { - AccInputHash [32]byte - SequencedTimestamp uint64 - PreviousLastBatchSequenced uint64 -} - -type RollupManagerContractor interface { - BaseContractor - RollupAddressToID(rollupAddress common.Address) (uint32, error) - GetBatchFee() (*big.Int, error) - RollupIDToRollupData(rollupID uint32) (*RollupData, error) - VerifyBatchesTrustedAggregator(opts *bind.TransactOpts, rollupID uint32, pendingStateNum uint64, initNumBatch uint64, finalNewBatch uint64, newLocalExitRoot [32]byte, newStateRoot [32]byte, beneficiary common.Address, proof [24][32]byte) (*types.Transaction, error) - GetRollupSequencedBatches(rollupID uint32, batchNum uint64) (StateVariablesSequencedBatchData, error) -} diff --git a/sequencesender/sequencesender_test.go b/sequencesender/sequencesender_test.go index 6df35b30..ca0d33e7 100644 --- a/sequencesender/sequencesender_test.go +++ b/sequencesender/sequencesender_test.go @@ -1,7 +1,6 @@ package sequencesender import ( - "context" "testing" "github.com/0xPolygon/cdk/log" @@ -64,10 +63,3 @@ func TestStreamTx(t *testing.T) { printBatch(decodedBatch, true, true) } - -func TestKK(t *testing.T) { - sut := &SequenceSender{} - sequences, err := sut.getSequencesToSend(context.TODO()) - require.NoError(t, err) - require.NotNil(t, sequences) -} From b0248374052d04aeaed3488a55298b1157bc83f5 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Wed, 24 Jul 2024 14:58:30 +0200 Subject: [PATCH 18/51] wip --- .github/test-e2e.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/test-e2e.yml b/.github/test-e2e.yml index 06a695ad..6740c11c 100644 --- a/.github/test-e2e.yml +++ b/.github/test-e2e.yml @@ -1,4 +1,3 @@ ---- name: Test e2e on: push: From 8b7ac7d095dbd7546e1386ca0337342f1656b502 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Wed, 24 Jul 2024 15:09:08 +0200 Subject: [PATCH 19/51] wip --- .github/test-e2e.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.github/test-e2e.yml b/.github/test-e2e.yml index 6740c11c..8e7a19c8 100644 --- a/.github/test-e2e.yml +++ b/.github/test-e2e.yml @@ -1,12 +1,6 @@ name: Test e2e on: push: - branches: - - main - - master - - develop - - update-external-dependencies - - 'release/**' pull_request: jobs: From a152f312afd367ae998ebd99a7341fe73b29dbc3 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Wed, 24 Jul 2024 15:10:30 +0200 Subject: [PATCH 20/51] wip --- .github/test-e2e.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/test-e2e.yml b/.github/test-e2e.yml index 8e7a19c8..d653e601 100644 --- a/.github/test-e2e.yml +++ b/.github/test-e2e.yml @@ -1,7 +1,5 @@ name: Test e2e -on: - push: - pull_request: +on: push jobs: test-e2e: From 55b6d3d2649f393e3558984b09861a80d7c82c4a Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Wed, 24 Jul 2024 15:11:56 +0200 Subject: [PATCH 21/51] wip --- .github/test-e2e.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/test-e2e.yml b/.github/test-e2e.yml index d653e601..aa30a21f 100644 --- a/.github/test-e2e.yml +++ b/.github/test-e2e.yml @@ -1,6 +1,6 @@ name: Test e2e on: push - + jobs: test-e2e: strategy: From 98eba4053a240a497cd3203fde9995a0c8ed9690 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Wed, 24 Jul 2024 15:31:35 +0200 Subject: [PATCH 22/51] add verify batches check --- test/run-e2e-seq_sender.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/run-e2e-seq_sender.sh b/test/run-e2e-seq_sender.sh index 222a0131..2d2f8d79 100755 --- a/test/run-e2e-seq_sender.sh +++ b/test/run-e2e-seq_sender.sh @@ -29,4 +29,6 @@ $BASE_FOLDER/scripts/kurtosis_prepare_params_yml.sh "$KURTOSIS_FOLDER" "elderber kurtosis clean --all kurtosis run --enclave cdk-v1 --args-file $DEST_KURTOSIS_PARAMS_YML --image-download always $KURTOSIS_FOLDER -[ $? -ne 0 ] && echo "Error running kurtosis" && exit 1 \ No newline at end of file +[ $? -ne 0 ] && echo "Error running kurtosis" && exit 1 +echo "Waiting 10 minutes to get some verified batch...." +$KURTOSIS_FOLDER/.github/actions/monitor-cdk-verified-batches/batch_verification_monitor.sh 1 600 \ No newline at end of file From 852d3394167006213845f1204c20d0bec5fa98bd Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 09:32:14 +0200 Subject: [PATCH 23/51] placeholder for testing GHaction --- .github/workflows/test-e2e.yml | 10 ++++++++++ .github/workflows/test-unittest.yml | 14 ++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 .github/workflows/test-e2e.yml create mode 100644 .github/workflows/test-unittest.yml diff --git a/.github/workflows/test-e2e.yml b/.github/workflows/test-e2e.yml new file mode 100644 index 00000000..42188c3e --- /dev/null +++ b/.github/workflows/test-e2e.yml @@ -0,0 +1,10 @@ +name: Test e2e +on: push + +jobs: + test-e2e: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 diff --git a/.github/workflows/test-unittest.yml b/.github/workflows/test-unittest.yml new file mode 100644 index 00000000..64550a77 --- /dev/null +++ b/.github/workflows/test-unittest.yml @@ -0,0 +1,14 @@ +name: Test Unittest +on: + push: + workflow_dispatch: {} + pull_request: + types: [opened, synchronize, reopened] + +jobs: + test-unittest: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 \ No newline at end of file From 953f6ffa4c04ca410447368480cc80aff3710b43 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 09:50:26 +0200 Subject: [PATCH 24/51] test --- .github/test-e2e.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/test-e2e.yml b/.github/test-e2e.yml index aa30a21f..33e6394b 100644 --- a/.github/test-e2e.yml +++ b/.github/test-e2e.yml @@ -1,6 +1,10 @@ name: Test e2e -on: push - +on: + push: + workflow_dispatch: {} + pull_request: + types: [opened, synchronize, reopened] + jobs: test-e2e: strategy: From 8e9482b510325cfb1a40b74fdcc133f1e6b83dd5 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 09:54:27 +0200 Subject: [PATCH 25/51] wip --- .github/test-e2e.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/test-e2e.yml b/.github/test-e2e.yml index 33e6394b..569c1ade 100644 --- a/.github/test-e2e.yml +++ b/.github/test-e2e.yml @@ -4,7 +4,7 @@ on: workflow_dispatch: {} pull_request: types: [opened, synchronize, reopened] - + jobs: test-e2e: strategy: @@ -27,7 +27,7 @@ jobs: - name: Build Docker run: make build-docker - + - name: Test From 85c38522471219b533c665c92fbda706ef5d7bf8 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 10:00:34 +0200 Subject: [PATCH 26/51] add workflow_dispatch --- .github/workflows/test-e2e.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test-e2e.yml b/.github/workflows/test-e2e.yml index 42188c3e..e4dbf3cb 100644 --- a/.github/workflows/test-e2e.yml +++ b/.github/workflows/test-e2e.yml @@ -1,5 +1,9 @@ name: Test e2e -on: push +on: + push: + workflow_dispatch: {} + pull_request: + types: [opened, synchronize, reopened] jobs: test-e2e: From 8b8cffc48ca848be32ec12fcdb9465b9f2dc7c0e Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 10:10:23 +0200 Subject: [PATCH 27/51] wip --- .github/test-e2e.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/test-e2e.yml b/.github/test-e2e.yml index 569c1ade..094177d6 100644 --- a/.github/test-e2e.yml +++ b/.github/test-e2e.yml @@ -1,6 +1,8 @@ name: Test e2e on: push: + branches: + - '**' workflow_dispatch: {} pull_request: types: [opened, synchronize, reopened] From e8901a342f7b7c288db45e60f073de4202a3e290 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 10:36:40 +0200 Subject: [PATCH 28/51] wip --- .github/test-e2e.yml | 37 ---------------------------------- .github/workflows/test-e2e.yml | 29 +++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 40 deletions(-) delete mode 100644 .github/test-e2e.yml diff --git a/.github/test-e2e.yml b/.github/test-e2e.yml deleted file mode 100644 index 094177d6..00000000 --- a/.github/test-e2e.yml +++ /dev/null @@ -1,37 +0,0 @@ -name: Test e2e -on: - push: - branches: - - '**' - workflow_dispatch: {} - pull_request: - types: [opened, synchronize, reopened] - -jobs: - test-e2e: - strategy: - fail-fast: false - matrix: - go-version: [ 1.21.x ] - goarch: [ "amd64" ] - e2e-group: [ "elderberry-validium", "elderberry-rollup" ] - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 - - - name: Install Go - uses: actions/setup-go@v3 - with: - go-version: ${{ matrix.go-version }} - env: - GOARCH: ${{ matrix.goarch }} - - - name: Build Docker - run: make build-docker - - - - - name: Test - run: make test-e2e-${{ matrix.e2e-group }} - working-directory: test diff --git a/.github/workflows/test-e2e.yml b/.github/workflows/test-e2e.yml index e4dbf3cb..094177d6 100644 --- a/.github/workflows/test-e2e.yml +++ b/.github/workflows/test-e2e.yml @@ -1,14 +1,37 @@ name: Test e2e on: push: + branches: + - '**' workflow_dispatch: {} pull_request: types: [opened, synchronize, reopened] - + jobs: test-e2e: + strategy: + fail-fast: false + matrix: + go-version: [ 1.21.x ] + goarch: [ "amd64" ] + e2e-group: [ "elderberry-validium", "elderberry-rollup" ] runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - name: Checkout code + uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 + + - name: Install Go + uses: actions/setup-go@v3 with: - fetch-depth: 0 + go-version: ${{ matrix.go-version }} + env: + GOARCH: ${{ matrix.goarch }} + + - name: Build Docker + run: make build-docker + + + + - name: Test + run: make test-e2e-${{ matrix.e2e-group }} + working-directory: test From b75e72c0b27d4571eabb5613d5b74124013a3ec0 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 10:56:25 +0200 Subject: [PATCH 29/51] wip --- .github/workflows/test-e2e.yml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/.github/workflows/test-e2e.yml b/.github/workflows/test-e2e.yml index 094177d6..79315ae0 100644 --- a/.github/workflows/test-e2e.yml +++ b/.github/workflows/test-e2e.yml @@ -30,6 +30,27 @@ jobs: - name: Build Docker run: make build-docker + # this is better to get the action in + - name: Install kurtosis + shell: bash + run: | + echo "deb [trusted=yes] https://apt.fury.io/kurtosis-tech/ /" | sudo tee /etc/apt/sources.list.d/kurtosis.list + sudo apt update + sudo apt install kurtosis-cli=0.90.1 + kurtosis version + + - name: Disable kurtosis analytics + shell: bash + run: kurtosis analytics disable + + - name: Install yq + shell: bash + run: | + pip3 install yq + yq --version + + - name: Install foundry + uses: foundry-rs/foundry-toolchain@v1 - name: Test From 4a413a13e31987592890c7d44d9da8c00c6507d1 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 11:05:59 +0200 Subject: [PATCH 30/51] wip --- .github/workflows/test-e2e.yml | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test-e2e.yml b/.github/workflows/test-e2e.yml index 79315ae0..e8639f8a 100644 --- a/.github/workflows/test-e2e.yml +++ b/.github/workflows/test-e2e.yml @@ -18,7 +18,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 + uses: actions/checkout@4 - name: Install Go uses: actions/setup-go@v3 @@ -52,6 +52,16 @@ jobs: - name: Install foundry uses: foundry-rs/foundry-toolchain@v1 + - name: checkout kurtosis-cdk + uses: actions/checkout@4 + with: + repository: git@github.com:0xPolygon/kurtosis-cdk.git + ref: jesteban/cdk-seq_sender + path: "ext/kurtosis-cdk" + + - name: Link kurtosis-cdk folder + shell: bash + run: mkdir -p /tmp/cdk/ && ln -s $GITHUB_WORKSPACE/ext/kurtosis-cdk /tmp/cdk/kurtosis-cdk-jesteban_cdk-seq_sender_ - name: Test run: make test-e2e-${{ matrix.e2e-group }} From 62f3fed35dce08e134bff05da168fac155346596 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 11:07:20 +0200 Subject: [PATCH 31/51] wip --- .github/workflows/test-e2e.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test-e2e.yml b/.github/workflows/test-e2e.yml index e8639f8a..f64d65c8 100644 --- a/.github/workflows/test-e2e.yml +++ b/.github/workflows/test-e2e.yml @@ -18,7 +18,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@4 + uses: actions/checkout@v4 - name: Install Go uses: actions/setup-go@v3 @@ -53,7 +53,7 @@ jobs: uses: foundry-rs/foundry-toolchain@v1 - name: checkout kurtosis-cdk - uses: actions/checkout@4 + uses: actions/checkout@v4 with: repository: git@github.com:0xPolygon/kurtosis-cdk.git ref: jesteban/cdk-seq_sender From b0a99e3dbe14610ab0ea722f559307bc77b22a8d Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 11:15:42 +0200 Subject: [PATCH 32/51] wip --- .github/workflows/test-e2e.yml | 2 +- .github/workflows/test-unittest.yml | 7 ++++++- Makefile | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test-e2e.yml b/.github/workflows/test-e2e.yml index f64d65c8..3db2d24f 100644 --- a/.github/workflows/test-e2e.yml +++ b/.github/workflows/test-e2e.yml @@ -55,7 +55,7 @@ jobs: - name: checkout kurtosis-cdk uses: actions/checkout@v4 with: - repository: git@github.com:0xPolygon/kurtosis-cdk.git + repository: https://github.com/0xPolygon/kurtosis-cdk.git ref: jesteban/cdk-seq_sender path: "ext/kurtosis-cdk" diff --git a/.github/workflows/test-unittest.yml b/.github/workflows/test-unittest.yml index 64550a77..235cd041 100644 --- a/.github/workflows/test-unittest.yml +++ b/.github/workflows/test-unittest.yml @@ -11,4 +11,9 @@ jobs: steps: - uses: actions/checkout@v4 with: - fetch-depth: 0 \ No newline at end of file + fetch-depth: 0 + - name: Install Go + uses: actions/setup-go@v3 + + - name: Build Docker + run: make test \ No newline at end of file diff --git a/Makefile b/Makefile index 89d32b1c..76e2dd08 100644 --- a/Makefile +++ b/Makefile @@ -82,7 +82,8 @@ stop: ## Stops all services .PHONY: test test: - go test -count=1 -short -race -p 1 -timeout 60s ./... + #go test -count=1 -short -race -p 1 -timeout 60s ./... + trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -short -race -p 1 -covermode=atomic -coverprofile=../coverage.out -coverpkg ./... -timeout 200s ./... .PHONY: install-linter install-linter: ## Installs the linter From 95f46fb453e7eff2fe6e0037448d2782cd9c52dc Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 11:18:45 +0200 Subject: [PATCH 33/51] wip --- .github/workflows/test-e2e.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test-e2e.yml b/.github/workflows/test-e2e.yml index 3db2d24f..0f3db036 100644 --- a/.github/workflows/test-e2e.yml +++ b/.github/workflows/test-e2e.yml @@ -55,7 +55,7 @@ jobs: - name: checkout kurtosis-cdk uses: actions/checkout@v4 with: - repository: https://github.com/0xPolygon/kurtosis-cdk.git + repository: 0xPolygon/kurtosis-cdk ref: jesteban/cdk-seq_sender path: "ext/kurtosis-cdk" From 9c28cc1b77bb7b7bdb1bb0d419fdaf7655c18a97 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 11:53:56 +0200 Subject: [PATCH 34/51] wip --- .github/workflows/test-unittest.yml | 2 +- test/run-e2e-seq_sender.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test-unittest.yml b/.github/workflows/test-unittest.yml index 235cd041..48a98f02 100644 --- a/.github/workflows/test-unittest.yml +++ b/.github/workflows/test-unittest.yml @@ -15,5 +15,5 @@ jobs: - name: Install Go uses: actions/setup-go@v3 - - name: Build Docker + - name: Launch unittest run: make test \ No newline at end of file diff --git a/test/run-e2e-seq_sender.sh b/test/run-e2e-seq_sender.sh index 2d2f8d79..8fe81590 100755 --- a/test/run-e2e-seq_sender.sh +++ b/test/run-e2e-seq_sender.sh @@ -29,6 +29,6 @@ $BASE_FOLDER/scripts/kurtosis_prepare_params_yml.sh "$KURTOSIS_FOLDER" "elderber kurtosis clean --all kurtosis run --enclave cdk-v1 --args-file $DEST_KURTOSIS_PARAMS_YML --image-download always $KURTOSIS_FOLDER -[ $? -ne 0 ] && echo "Error running kurtosis" && exit 1 +#[ $? -ne 0 ] && echo "Error running kurtosis" && exit 1 echo "Waiting 10 minutes to get some verified batch...." $KURTOSIS_FOLDER/.github/actions/monitor-cdk-verified-batches/batch_verification_monitor.sh 1 600 \ No newline at end of file From b2dd6a00c10a9c95acc069be70a6878d3fc9a897 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 12:37:12 +0200 Subject: [PATCH 35/51] disabled datacommitte test because PUSH0 is not supported by geth simulated --- dataavailability/datacommittee/datacommittee_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/dataavailability/datacommittee/datacommittee_test.go b/dataavailability/datacommittee/datacommittee_test.go index e7c27188..886b1f6c 100644 --- a/dataavailability/datacommittee/datacommittee_test.go +++ b/dataavailability/datacommittee/datacommittee_test.go @@ -16,6 +16,7 @@ import ( ) func TestUpdateDataCommitteeEvent(t *testing.T) { + t.Skip("This test is not working because the simulated backedn doesnt accept PUSH0, check: https://github.com/ethereum/go-ethereum/issues/28144#issuecomment-2247124776") // Set up testing environment dac, ethBackend, auth, da := newTestingEnv(t) From ab3a46257727bf690585b24c092d63e62d40f55b Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 12:38:38 +0200 Subject: [PATCH 36/51] wip --- .github/workflows/test-e2e.yml | 3 +-- .github/workflows/test-unittest.yml | 5 +++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test-e2e.yml b/.github/workflows/test-e2e.yml index 0f3db036..a8456ed9 100644 --- a/.github/workflows/test-e2e.yml +++ b/.github/workflows/test-e2e.yml @@ -4,8 +4,7 @@ on: branches: - '**' workflow_dispatch: {} - pull_request: - types: [opened, synchronize, reopened] + jobs: test-e2e: diff --git a/.github/workflows/test-unittest.yml b/.github/workflows/test-unittest.yml index 48a98f02..156a0144 100644 --- a/.github/workflows/test-unittest.yml +++ b/.github/workflows/test-unittest.yml @@ -1,9 +1,10 @@ name: Test Unittest on: push: + branches: + - '**' workflow_dispatch: {} - pull_request: - types: [opened, synchronize, reopened] + jobs: test-unittest: From 1fc99d77a6ceadbbb7523a3638494c287b195653 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 15:54:27 +0200 Subject: [PATCH 37/51] PR comments --- sequencesender/txbuilder/banana_base.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/sequencesender/txbuilder/banana_base.go b/sequencesender/txbuilder/banana_base.go index babce9cc..60c6c4a7 100644 --- a/sequencesender/txbuilder/banana_base.go +++ b/sequencesender/txbuilder/banana_base.go @@ -63,12 +63,6 @@ func convertToEthermanBatch(batch seqsendertypes.Batch) (etherman.Batch, error) LastL2BLockTimestamp: batch.LastL2BLockTimestamp(), GlobalExitRoot: batch.GlobalExitRoot(), }, nil - // cast, ok := batch.(*BananaBatch) - // if !ok { - // log.Error("Batch is not a BananaBatch") - // return etherman.Batch{}, fmt.Errorf("Batch is not a BananaBatch") - // } - // return cast.Batch, nil } func convertToEthermanBatches(batch []seqsendertypes.Batch) ([]etherman.Batch, error) { From 06aa68c0973ba17fa6fa311e995bf29d6cfe151f Mon Sep 17 00:00:00 2001 From: Joan Esteban <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 15:54:53 +0200 Subject: [PATCH 38/51] Update dataavailability/datacommittee/datacommittee.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Stefan Negovanović <93934272+Stefan-Ethernal@users.noreply.github.com> --- dataavailability/datacommittee/datacommittee.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dataavailability/datacommittee/datacommittee.go b/dataavailability/datacommittee/datacommittee.go index bfe9393a..c9542e33 100644 --- a/dataavailability/datacommittee/datacommittee.go +++ b/dataavailability/datacommittee/datacommittee.go @@ -22,8 +22,10 @@ import ( "golang.org/x/net/context" ) -const unexpectedHashTemplate = "missmatch on transaction data. Expected hash %s, actual hash: %s" -const translateContextName = "dataCommittee" +const ( + unexpectedHashTemplate = "missmatch on transaction data. Expected hash %s, actual hash: %s" + translateContextName = "dataCommittee" +) // DataCommitteeMember represents a member of the Data Committee type DataCommitteeMember struct { From af1640d3d16ccdea0145aa2f4e0eaac9c45c9e36 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 15:57:14 +0200 Subject: [PATCH 39/51] PR comments --- dataavailability/datacommittee/datacommittee.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dataavailability/datacommittee/datacommittee.go b/dataavailability/datacommittee/datacommittee.go index c9542e33..a817644a 100644 --- a/dataavailability/datacommittee/datacommittee.go +++ b/dataavailability/datacommittee/datacommittee.go @@ -23,8 +23,8 @@ import ( ) const ( - unexpectedHashTemplate = "missmatch on transaction data. Expected hash %s, actual hash: %s" - translateContextName = "dataCommittee" + unexpectedHashTemplate = "missmatch on transaction data. Expected hash %s, actual hash: %s" + translateContextName = "dataCommittee" ) // DataCommitteeMember represents a member of the Data Committee @@ -188,7 +188,7 @@ func (s *Backend) PostSequenceElderberry(ctx context.Context, batchesData [][]by go requestSignatureFromMember(signatureCtx, &signedSequenceElderberry, func(c client.Client) ([]byte, error) { return c.SignSequence(ctx, signedSequenceElderberry) }, member, ch) } - return s.collectSignatures(committee, ch, cancelSignatureCollection) + return collectSignatures(committee, ch, cancelSignatureCollection) } func (s *Backend) PostSequence(ctx context.Context, sequence etherman.SequenceBanana) ([]byte, error) { @@ -235,10 +235,10 @@ func (s *Backend) PostSequence(ctx context.Context, sequence etherman.SequenceBa member, ch) } - return s.collectSignatures(committee, ch, cancelSignatureCollection) + return collectSignatures(committee, ch, cancelSignatureCollection) } -func (*Backend) collectSignatures(committee *DataCommittee, ch chan signatureMsg, cancelSignatureCollection context.CancelFunc) ([]byte, error) { +func collectSignatures(committee *DataCommittee, ch chan signatureMsg, cancelSignatureCollection context.CancelFunc) ([]byte, error) { // Collect signatures // Stop requesting as soon as we have N valid signatures var ( From 4979588c6ecb1507e31773e3ec9ea76162b5ceb0 Mon Sep 17 00:00:00 2001 From: Joan Esteban <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 15:59:17 +0200 Subject: [PATCH 40/51] Update Makefile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Stefan Negovanović <93934272+Stefan-Ethernal@users.noreply.github.com> --- Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/Makefile b/Makefile index 76e2dd08..d092a0f3 100644 --- a/Makefile +++ b/Makefile @@ -82,7 +82,6 @@ stop: ## Stops all services .PHONY: test test: - #go test -count=1 -short -race -p 1 -timeout 60s ./... trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -short -race -p 1 -covermode=atomic -coverprofile=../coverage.out -coverpkg ./... -timeout 200s ./... .PHONY: install-linter From 19dab0c8434e5508a39e339779692ef001bf7114 Mon Sep 17 00:00:00 2001 From: Joan Esteban <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 15:59:29 +0200 Subject: [PATCH 41/51] Update dataavailability/datacommittee/datacommittee_test.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Stefan Negovanović <93934272+Stefan-Ethernal@users.noreply.github.com> --- dataavailability/datacommittee/datacommittee_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dataavailability/datacommittee/datacommittee_test.go b/dataavailability/datacommittee/datacommittee_test.go index 886b1f6c..4673a4b5 100644 --- a/dataavailability/datacommittee/datacommittee_test.go +++ b/dataavailability/datacommittee/datacommittee_test.go @@ -16,7 +16,7 @@ import ( ) func TestUpdateDataCommitteeEvent(t *testing.T) { - t.Skip("This test is not working because the simulated backedn doesnt accept PUSH0, check: https://github.com/ethereum/go-ethereum/issues/28144#issuecomment-2247124776") + t.Skip("This test is not working because the simulated backend doesnt accept PUSH0, check: https://github.com/ethereum/go-ethereum/issues/28144#issuecomment-2247124776") // Set up testing environment dac, ethBackend, auth, da := newTestingEnv(t) From f499b7f5137de8f62890003f1fb4d60aef0e6b8e Mon Sep 17 00:00:00 2001 From: Joan Esteban <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:01:57 +0200 Subject: [PATCH 42/51] Update test/scripts/config_kurtosis_for_local_run.sh MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Stefan Negovanović <93934272+Stefan-Ethernal@users.noreply.github.com> --- test/scripts/config_kurtosis_for_local_run.sh | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/scripts/config_kurtosis_for_local_run.sh b/test/scripts/config_kurtosis_for_local_run.sh index 424858bc..98cf369c 100755 --- a/test/scripts/config_kurtosis_for_local_run.sh +++ b/test/scripts/config_kurtosis_for_local_run.sh @@ -63,5 +63,13 @@ cat << EOF "--custom-network-file", "$DEST/local_config/genesis.json" ] }, -EOF + "type": "go", + "request": "launch", + "mode": "auto", + "program": "cmd/", + "args":["run","-cfg","$DEST/test.kurtosis.toml", + "--components", "sequence-sender", + "--custom-network-file", "$DEST/local_config/genesis.json" + ] + }, From f1085b6de405b2a9bef1d04bc89c7ed63e1b5d99 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:02:59 +0200 Subject: [PATCH 43/51] PR comments --- dataavailability/datacommittee/datacommittee.go | 10 +++++----- test/scripts/config_kurtosis_for_local_run.sh | 9 --------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/dataavailability/datacommittee/datacommittee.go b/dataavailability/datacommittee/datacommittee.go index a817644a..3613ecd5 100644 --- a/dataavailability/datacommittee/datacommittee.go +++ b/dataavailability/datacommittee/datacommittee.go @@ -160,9 +160,9 @@ type signatureMsg struct { err error } -func (s *Backend) PostSequenceElderberry(ctx context.Context, batchesData [][]byte) ([]byte, error) { +func (d *Backend) PostSequenceElderberry(ctx context.Context, batchesData [][]byte) ([]byte, error) { // Get current committee - committee, err := s.getCurrentDataCommittee() + committee, err := d.getCurrentDataCommittee() if err != nil { return nil, err } @@ -172,7 +172,7 @@ func (s *Backend) PostSequenceElderberry(ctx context.Context, batchesData [][]by for _, batchData := range batchesData { sequence = append(sequence, batchData) } - signedSequence, err := sequence.Sign(s.privKey) + signedSequence, err := sequence.Sign(d.privKey) if err != nil { return nil, err } @@ -191,9 +191,9 @@ func (s *Backend) PostSequenceElderberry(ctx context.Context, batchesData [][]by return collectSignatures(committee, ch, cancelSignatureCollection) } -func (s *Backend) PostSequence(ctx context.Context, sequence etherman.SequenceBanana) ([]byte, error) { +func (d *Backend) PostSequence(ctx context.Context, sequence etherman.SequenceBanana) ([]byte, error) { // Get current committee - committee, err := s.getCurrentDataCommittee() + committee, err := d.getCurrentDataCommittee() if err != nil { return nil, err } diff --git a/test/scripts/config_kurtosis_for_local_run.sh b/test/scripts/config_kurtosis_for_local_run.sh index 98cf369c..a96146f3 100755 --- a/test/scripts/config_kurtosis_for_local_run.sh +++ b/test/scripts/config_kurtosis_for_local_run.sh @@ -54,15 +54,6 @@ echo "- Add next configuration to vscode launch.json" cat << EOF { "name": "run local_docker", - "type": "go", - "request": "launch", - "mode": "auto", - "program": "cmd/", - "args":["run","-cfg","$DEST/test.kurtosis.toml", - "--components", "sequence-sender", - "--custom-network-file", "$DEST/local_config/genesis.json" - ] - }, "type": "go", "request": "launch", "mode": "auto", From fddefb1bab10294b847c91f0fbd3e620c6c15600 Mon Sep 17 00:00:00 2001 From: Joan Esteban <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:24:33 +0200 Subject: [PATCH 44/51] Update test/config/test.kurtosis_template.toml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Stefan Negovanović <93934272+Stefan-Ethernal@users.noreply.github.com> --- test/config/test.kurtosis_template.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/test/config/test.kurtosis_template.toml b/test/config/test.kurtosis_template.toml index b2f7f68f..061a4838 100644 --- a/test/config/test.kurtosis_template.toml +++ b/test/config/test.kurtosis_template.toml @@ -15,7 +15,6 @@ Level = "info" Outputs = ["stderr"] [SequenceSender] -IsValidiumMode={{.zkevm_is_validium}} WaitPeriodSendSequence = "15s" LastBatchVirtualizationTimeMaxWaitPeriod = "10s" L1BlockTimestampMargin = "30s" From 95799d6b29d72229136a780ad33a043430b25bbb Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:28:07 +0200 Subject: [PATCH 45/51] PR comments --- dataavailability/datacommittee/datacommittee.go | 2 +- sequencesender/sequencesender.go | 9 --------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/dataavailability/datacommittee/datacommittee.go b/dataavailability/datacommittee/datacommittee.go index 3613ecd5..daa29737 100644 --- a/dataavailability/datacommittee/datacommittee.go +++ b/dataavailability/datacommittee/datacommittee.go @@ -216,7 +216,7 @@ func (d *Backend) PostSequence(ctx context.Context, sequence etherman.SequenceBa MaxSequenceTimestamp: daTypes.ArgUint64(sequence.MaxSequenceTimestamp), } - signature, err := sequenceBanana.Sign(s.privKey) + signature, err := sequenceBanana.Sign(d.privKey) if err != nil { return nil, err } diff --git a/sequencesender/sequencesender.go b/sequencesender/sequencesender.go index 78bcea2c..21e2dd40 100644 --- a/sequencesender/sequencesender.go +++ b/sequencesender/sequencesender.go @@ -941,27 +941,18 @@ func (s *SequenceSender) addNewBatchL2Block(l2Block *datastream.L2Block) { data := s.sequenceData[s.wipBatch] if data != nil { wipBatchRaw := data.batchRaw - //data.batch.LastL2BLockTimestamp = l2Block.Timestamp data.batch.SetLastL2BLockTimestamp(l2Block.Timestamp) // Sanity check: should be the same coinbase within the batch if common.BytesToAddress(l2Block.Coinbase) != data.batch.LastCoinbase() { s.logFatalf("[SeqSender] coinbase changed within the batch! (Previous %v, Current %v)", data.batch.LastCoinbase, common.BytesToAddress(l2Block.Coinbase)) } - //data.batch.LastCoinbase = common.BytesToAddress(l2Block.Coinbase) data.batch.SetLastCoinbase(common.BytesToAddress(l2Block.Coinbase)) - //data.batch.L1InfoTreeIndex = l2Block.L1InfotreeIndex data.batch.SetL1InfoTreeIndex(l2Block.L1InfotreeIndex) // New L2 block raw newBlockRaw := state.L2BlockRaw{} // Add L2 block wipBatchRaw.Blocks = append(wipBatchRaw.Blocks, newBlockRaw) - - // Update batch timestamp - //data.batch.LastL2BLockTimestamp = l2Block.Timestamp - // TODO: Duplicated assignation - data.batch.SetLastL2BLockTimestamp(l2Block.Timestamp) - // Get current L2 block _, blockRaw := s.getWipL2Block() if blockRaw == nil { From f34765fab571e1e7642d72407aec2d3d28929441 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:32:15 +0200 Subject: [PATCH 46/51] PR comments --- sequencesender/sequencesender.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/sequencesender/sequencesender.go b/sequencesender/sequencesender.go index 21e2dd40..961b6f75 100644 --- a/sequencesender/sequencesender.go +++ b/sequencesender/sequencesender.go @@ -640,7 +640,6 @@ func (s *SequenceSender) getSequencesToSend(ctx context.Context) (seqsendertypes // If the coinbase changes, the sequence ends here if len(sequenceBatches) > 0 && batch.LastCoinbase() != prevCoinbase { log.Infof("[SeqSender] batch with different coinbase (batch %v, sequence %v), sequence will be sent to this point", prevCoinbase, batch.LastCoinbase) - //return s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) return s.TxBuilder.NewSequence(sequenceBatches, s.cfg.L2Coinbase) } prevCoinbase = batch.LastCoinbase() @@ -659,7 +658,6 @@ func (s *SequenceSender) getSequencesToSend(ctx context.Context) (seqsendertypes // Check if the current batch is the last before a change to a new forkid, in this case we need to close and send the sequence to L1 if (s.cfg.ForkUpgradeBatchNumber != 0) && (batchNumber == (s.cfg.ForkUpgradeBatchNumber)) { log.Infof("[SeqSender] sequence should be sent to L1, as we have reached the batch %d from which a new forkid is applied (upgrade)", s.cfg.ForkUpgradeBatchNumber) - //return s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) return s.TxBuilder.NewSequence(sequenceBatches, s.cfg.L2Coinbase) } } @@ -672,7 +670,6 @@ func (s *SequenceSender) getSequencesToSend(ctx context.Context) (seqsendertypes if s.latestVirtualTime.Before(time.Now().Add(-s.cfg.LastBatchVirtualizationTimeMaxWaitPeriod.Duration)) { log.Infof("[SeqSender] sequence should be sent, too much time without sending anything to L1") - //return s.newSequenceBanana(sequenceBatches, s.cfg.L2Coinbase) return s.TxBuilder.NewSequence(sequenceBatches, s.cfg.L2Coinbase) } From 14eb4fa7eb36688a1683a2709752dddc2bc4eb08 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:33:45 +0200 Subject: [PATCH 47/51] PR comments --- etherman/contracts/{interfaces.go => types.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename etherman/contracts/{interfaces.go => types.go} (100%) diff --git a/etherman/contracts/interfaces.go b/etherman/contracts/types.go similarity index 100% rename from etherman/contracts/interfaces.go rename to etherman/contracts/types.go From 8adc59ea36b212685a53086316835de6fb1e21b5 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:35:10 +0200 Subject: [PATCH 48/51] PR comments --- dataavailability/datacommittee/datacommittee.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dataavailability/datacommittee/datacommittee.go b/dataavailability/datacommittee/datacommittee.go index daa29737..1fd902e7 100644 --- a/dataavailability/datacommittee/datacommittee.go +++ b/dataavailability/datacommittee/datacommittee.go @@ -289,7 +289,7 @@ func requestSignatureFromMember(ctx context.Context, signedSequence daTypes.Sign // request c := client.New(member.URL) log.Infof("sending request to sign the sequence to %s at %s", member.Addr.Hex(), member.URL) - //signature, err := c.SignSequenceBanana(ctx, signedSequence) + //funcSign must call something like that c.SignSequenceBanana(ctx, signedSequence) signature, err := funcSign(c) if err != nil { From 9c2e6623ff4305e0fb4427c0d24523fdf44b2ed1 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:37:59 +0200 Subject: [PATCH 49/51] PR comments --- dataavailability/datacommittee/datacommittee.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/dataavailability/datacommittee/datacommittee.go b/dataavailability/datacommittee/datacommittee.go index 1fd902e7..54b3882f 100644 --- a/dataavailability/datacommittee/datacommittee.go +++ b/dataavailability/datacommittee/datacommittee.go @@ -269,11 +269,6 @@ func collectSignatures(committee *DataCommittee, ch chan signatureMsg, cancelSig type funcSignType func(c client.Client) ([]byte, error) -type SignedInterface interface { - SetSignature([]byte) - Signer() (common.Address, error) -} - // funcSetSignatureType: is not possible to define a SetSignature function because // the type daTypes.SequenceBanana and daTypes.Sequence belong to different packages // So a future refactor is define a common interface for both From e3c3a599915a9499f873ba6f0274309a598bfc78 Mon Sep 17 00:00:00 2001 From: Joan Esteban <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:38:44 +0200 Subject: [PATCH 50/51] Update cmd/run.go Co-authored-by: Goran Rojovic <100121253+goran-ethernal@users.noreply.github.com> --- cmd/run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/run.go b/cmd/run.go index e1d94bd1..15d24c29 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -128,7 +128,7 @@ func createSequenceSender(cfg config.Config) *sequencesender.SequenceSender { }, }, cfg.NetworkConfig.L1Config, cfg.Common) if err != nil { - log.Fatalf("Failed to connect create etherman. Err: %w, ", err) + log.Fatalf("Failed to create etherman. Err: %w, ", err) } auth, _, err := ethman.LoadAuthFromKeyStore(cfg.SequenceSender.PrivateKey.Path, cfg.SequenceSender.PrivateKey.Password) From 098b503815809275987496726cbdec8be45da2a2 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:40:10 +0200 Subject: [PATCH 51/51] PR comments --- translator/translator_impl.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/translator/translator_impl.go b/translator/translator_impl.go index 92326460..1e1a2a6a 100644 --- a/translator/translator_impl.go +++ b/translator/translator_impl.go @@ -14,10 +14,7 @@ func (t *TranslatorFullMatchRule) Match(contextName string, data string) bool { if t.ContextName != nil && *t.ContextName != contextName { return false } - if t.FullMatchString != data { - return false - } - return true + return t.FullMatchString == data } func (t *TranslatorFullMatchRule) Translate(contextName string, data string) string {