Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
joanestebanr committed Jul 1, 2024
1 parent 5ec3939 commit 9f1919f
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 60 deletions.
54 changes: 31 additions & 23 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/0xPolygon/cdk/state/pgstatestorage"
ethtxman "github.com/0xPolygonHermez/zkevm-ethtx-manager/etherman"
"github.com/0xPolygonHermez/zkevm-ethtx-manager/etherman/etherscan"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -113,6 +114,18 @@ func createAggregator(ctx context.Context, c config.Config, runMigrations bool)
return aggregator
}

func registerValidium(selector *txbuilder.TxBuilderRouter, cfg config.Config,
ethman *etherman.Client, contracts *etherman2.Contracts,
sender *bind.TransactOpts) {
da, err := newDataAvailability(cfg, ethman)
if err != nil {
log.Fatal(err)
}
eth2elderberry := etherman2seqsender.NewEth2Elderberry(contracts.RollupElderberry)
txbuilder := txbuilder.NewBuildSequenceBatchesTxValidium(da, eth2elderberry, cfg.SequenceSender.L2Coinbase, sender)
selector.Register("elderberry", txbuilder)
}

func createSequenceSender(cfg config.Config) *sequencesender.SequenceSender {
ethman, err := etherman.NewClient(etherman.Config{
EthermanConfig: ethtxman.Config{
Expand All @@ -135,35 +148,30 @@ func createSequenceSender(cfg config.Config) *sequencesender.SequenceSender {
if err != nil {
log.Fatal(err)
}

eth2, err := etherman2.NewEtherman2Builder().
ChainReader(cfg.Etherman.URL).
AuthStore(nil).
Build(context.Background())
if err != nil {
log.Fatal(err)
}
err = eth2.AddOrReplaceAuth(*auth)
if err != nil {
log.Fatal(err)
}
cfg.SequenceSender.SenderAddress = auth.From

da, err := newDataAvailability(cfg, ethman)
/*
This code is just an example, but not needed
eth2, err := etherman2.NewEtherman2Builder().
ChainReader(cfg.Etherman.URL).
AuthStore(nil).
Build(context.Background())
if err != nil {
log.Fatal(err)
}
*/
contracts, err := etherman2.NewEtherman2ContractBuilder(cfg.Etherman.URL).
AddRollupElderberry(cfg.NetworkConfig.L1Config.ZkEVMAddr).
Build()
if err != nil {
log.Fatal(err)
}
txBuilder := txbuilder.NewTxBuilderSelector(txbuilder.NewSelectorPerForkID())

txBuilderSelector := txbuilder.NewTxBuilderSelector(txbuilder.NewSelectorPerForkID())
if cfg.SequenceSender.IsValidiumMode {
eth2c := etherman2seqsender.NewEth2Elderberry(nil)
err := eth2c.LoadContract(cfg.NetworkConfig.L1Config.ZkEVMAddr, ethman.EthClient)
if err != nil {
log.Fatal(err)
}
txBV := txbuilder.NewBuildSequenceBatchesTxValidium(da, eth2c, cfg.SequenceSender.L2Coinbase, auth)
txBuilder.Register("elderberry", txBV)
registerValidium(txBuilderSelector, cfg, ethman, contracts, auth)
}
seqSender, err := sequencesender.New(cfg.SequenceSender, ethman, da)

seqSender, err := sequencesender.New(cfg.SequenceSender, ethman, txBuilderSelector)
if err != nil {
log.Fatal(err)
}
Expand Down
52 changes: 52 additions & 0 deletions etherman2/builder_contracts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package etherman2

import (
"log"

internal_contracts "github.com/0xPolygon/cdk/etherman2/internal/contracts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)

type Etherman2ContractBuilder struct {
build struct {
result *Contracts
}
params struct {
rpcURL string
RollupElderberry *common.Address
}
}

func NewEtherman2ContractBuilder(rpcURL string) *Etherman2ContractBuilder {
res := &Etherman2ContractBuilder{}
res.params.rpcURL = rpcURL
return res
}

func (e *Etherman2ContractBuilder) AddRollupElderberry(address common.Address) *Etherman2ContractBuilder {
if e.build.result != nil {
log.Fatal("Cannot add contracts after build")
}
e.params.RollupElderberry = &address
return e
}

func (e *Etherman2ContractBuilder) Build() (*Contracts, error) {
if e.build.result != nil {
return e.build.result, nil
}
l1Client, err := ethclient.Dial(e.params.rpcURL)
if err != nil {
return nil, err
}
e.build.result = &Contracts{}
if e.params.RollupElderberry != nil {
rollupElderberry, err := internal_contracts.NewContractRollupElderberry(*e.params.RollupElderberry, l1Client)
if err != nil {
return nil, err
}
e.build.result.RollupElderberry = rollupElderberry
}
return e.build.result, nil
}
8 changes: 5 additions & 3 deletions etherman2/builder.go → etherman2/builder_eth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

type etherman2Impl struct {
*internal.Etherman2AuthStore
*internal.Etherman2ChainReader
Etherman2ChainReader
Etherman2AuthStorerRW
}

/*
Expand All @@ -31,6 +31,7 @@ type Etherman2Builder struct {
enabled bool
forcedChainID *uint64
}

result etherman2Impl
}

Expand Down Expand Up @@ -63,6 +64,7 @@ func (e *Etherman2Builder) Build(ctx context.Context) (*etherman2Impl, error) {
return nil, err
}
}

return &e.result, nil
}

Expand All @@ -89,7 +91,7 @@ func (e *Etherman2Builder) newChainReader() error {
func (e *Etherman2Builder) newAuthStore(ctx context.Context) error {
if e.authStore.enabled {
chainID, err := e.getChainID(ctx)
e.result.Etherman2AuthStore, err = internal.NewEtherman2L1Auth(chainID)
e.result.Etherman2AuthStorerRW, err = internal.NewEtherman2L1Auth(chainID)
if err != nil {
return err
}
Expand Down
6 changes: 1 addition & 5 deletions etherman2/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ type Etherman2AuthStorerRW interface {
AddOrReplaceAuth(auth bind.TransactOpts) error
}

type Etherman2ContractLoader[T any] interface {
LoadContract(address common.Address, backend bind.ContractBackend, funcConstructor func(common.Address, bind.ContractBackend) (T, error)) (T, error)
}

type Etherman2 interface {
type Etherman2Composite interface {
Etherman2ChainReader
Etherman2AuthStorerRW
}
24 changes: 24 additions & 0 deletions etherman2/interface_contracts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package etherman2

import (
"github.com/0xPolygon/cdk-contracts-tooling/contracts/elderberry/polygonvalidiumetrog"
"github.com/ethereum/go-ethereum/common"
)

type Etherman2Contract[T any] interface {
GetContract() *T
GetAddress() common.Address
}

type Etherman2ContractRollupCommon interface {
TrustedSequencer() (common.Address, error)
}

type Etherman2ContractRollupElderberry interface {
Etherman2Contract[polygonvalidiumetrog.Polygonvalidiumetrog]
Etherman2ContractRollupCommon
}

type Contracts struct {
RollupElderberry Etherman2ContractRollupElderberry
}
33 changes: 33 additions & 0 deletions etherman2/internal/contracts/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package internal_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
}

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) (*ContractBase[T], error) {
contractBind, err := constructor(address, backend)
if err != nil {
return nil, err
}

return &ContractBase[T]{
contractBind: contractBind,
address: address,
}, nil
}

func (e *ContractBase[T]) GetContract() *T {
return e.contractBind
}

func (e *ContractBase[T]) GetAddress() common.Address {
return e.address
}
26 changes: 26 additions & 0 deletions etherman2/internal/contracts/elderberry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package internal_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) {
//ZkEVM, err := polygonvalidiumetrog.NewPolygonvalidiumetrog(address, backend)
base, err := NewContractBase(polygonvalidiumetrog.NewPolygonvalidiumetrog, address, backend)
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})
}
21 changes: 6 additions & 15 deletions sequencesender/etherman2seqsender/eth2elderberry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,15 @@ import (
)

type Eth2Elderberry struct {
ZkEVM *polygonvalidiumetrog.Polygonvalidiumetrog
ZkEVM etherman2.Etherman2ContractRollupElderberry
}

func NewEth2Elderberry(zkEVM *polygonvalidiumetrog.Polygonvalidiumetrog) *Eth2Elderberry {
func NewEth2Elderberry(zkEVM etherman2.Etherman2ContractRollupElderberry) *Eth2Elderberry {
return &Eth2Elderberry{
ZkEVM: zkEVM,
}
}

func (etherMan *Eth2Elderberry) LoadContract(address common.Address, backend bind.ContractBackend) error {
var err error
etherMan.ZkEVM, err = polygonvalidiumetrog.NewPolygonvalidiumetrog(address, backend)
if err != nil {
return err
}
return nil
}

// func (etherMan *Eth2Elderberry) BuildSequenceBatchesTxZKEVM(opts bind.TransactOpts, sequences []ethmanTypes.Sequence, maxSequenceTimestamp uint64, lastSequencedBatchNumber uint64, l2Coinbase common.Address) (*types.Transaction, error) {
func (etherMan *Eth2Elderberry) BuildSequenceBatchesTxZKEVM(opts *bind.TransactOpts, params BuildSequenceBatchesParams) (*types.Transaction, error) {
batches := make([]polygonvalidiumetrog.PolygonRollupBaseEtrogBatchData, len(params.Sequences))
Expand All @@ -45,8 +36,8 @@ func (etherMan *Eth2Elderberry) BuildSequenceBatchesTxZKEVM(opts *bind.TransactO
ForcedBlockHashL1: seq.PrevBlockHash,
}
}

tx, err := etherMan.ZkEVM.SequenceBatches(opts, batches, params.MaxSequenceTimestamp, params.LastSequencedBatchNumber, params.L2Coinbase)
ZkEVM := etherMan.ZkEVM.GetContract()
tx, err := ZkEVM.SequenceBatches(opts, batches, params.MaxSequenceTimestamp, params.LastSequencedBatchNumber, params.L2Coinbase)
if err != nil {
etherMan.warningMessage(batches, params.L2Coinbase, opts)
if parsedErr, ok := etherman2.TryParseError(err); ok {
Expand All @@ -72,8 +63,8 @@ func (etherMan *Eth2Elderberry) BuildSequenceBatchesTxValidium(opts *bind.Transa
ForcedBlockHashL1: seq.PrevBlockHash,
}
}

tx, err := etherMan.ZkEVM.SequenceBatchesValidium(opts, batches, params.MaxSequenceTimestamp,
ZkEVM := etherMan.ZkEVM.GetContract()
tx, err := ZkEVM.SequenceBatchesValidium(opts, batches, params.MaxSequenceTimestamp,
params.LastSequencedBatchNumber, params.L2Coinbase, dataAvailabilityMessage)
if err != nil {
if parsedErr, ok := etherman2.TryParseError(err); ok {
Expand Down
20 changes: 6 additions & 14 deletions sequencesender/sequencesender.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"sync"
"time"

"github.com/0xPolygon/cdk/dataavailability"
"github.com/0xPolygon/cdk/etherman"
"github.com/0xPolygon/cdk/etherman/types"
"github.com/0xPolygon/cdk/log"
Expand Down Expand Up @@ -54,7 +53,6 @@ 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
}

Expand Down Expand Up @@ -84,12 +82,7 @@ type ethTxAdditionalData struct {
}

// New inits sequence sender
func New(cfg Config, etherman *etherman.Client, da *dataavailability.DataAvailability) (*SequenceSender, error) {
txBuilder := txbuilder.NewTxBuilderSelector(txbuilder.NewSelectorPerForkID())
if cfg.IsValidiumMode{
txBuilder.Register("elderberry", txbuilder.NewBuildSequenceBatchesTxValidium(da,
)
}
func New(cfg Config, etherman *etherman.Client, txBuilder txbuilder.TxBuilder) (*SequenceSender, error) {

// Create sequencesender
s := SequenceSender{
Expand All @@ -101,8 +94,8 @@ func New(cfg Config, etherman *etherman.Client, da *dataavailability.DataAvailab
validStream: false,
latestStreamBatch: 0,
seqSendingStopped: false,
da: da,
txBuilder: txBuilder,

txBuilder: txBuilder,
}

// Restore pending sent sequences
Expand Down Expand Up @@ -554,8 +547,6 @@ func (s *SequenceSender) buildSendTx(ctx context.Context, sequences []types.Sequ
return s.txBuilder.BuildSequenceBatchesTx(ctx, sequences, lastSequence, firstSequence)
}



// sendTx adds transaction to the ethTxManager to send it to L1
func (s *SequenceSender) sendTx(ctx context.Context, resend bool, txOldHash *common.Hash, to *common.Address, fromBatch uint64, toBatch uint64, data []byte) error {
// Params if new tx to send or resend a previous tx
Expand Down Expand Up @@ -631,6 +622,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() ([]types.Sequence, error) {
ctx := context.Background()
// Add sequences until too big for a single L1 tx or last batch is reached
s.mutexSequence.Lock()
defer s.mutexSequence.Unlock()
Expand Down Expand Up @@ -681,7 +673,7 @@ func (s *SequenceSender) getSequencesToSend() ([]types.Sequence, error) {

// Check if can be sent
//tx, err := s.etherman.BuildSequenceBatchesTx(s.cfg.SenderAddress, sequences, lastSequence.LastL2BLockTimestamp, firstSequence.BatchNumber-1, s.cfg.L2Coinbase, nil)
tx, err := s.buildSendTx(s.ctx, s.cfg.SenderAddress, sequences, lastSequence.LastL2BLockTimestamp, firstSequence.BatchNumber-1, s.cfg.L2Coinbase, nil)
tx, err := s.buildSendTx(ctx, sequences, lastSequence, firstSequence)
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
Expand All @@ -694,7 +686,7 @@ func (s *SequenceSender) getSequencesToSend() ([]types.Sequence, error) {
// Handling the error gracefully, re-processing the sequence as a sanity check
lastSequence = sequences[len(sequences)-1]
//_, err = s.etherman.BuildSequenceBatchesTx(s.cfg.SenderAddress, sequences, lastSequence.LastL2BLockTimestamp, firstSequence.BatchNumber-1, s.cfg.L2Coinbase, nil)
_, err= s.buildSendTx(s.ctx, s.cfg.SenderAddress, sequences, lastSequence.LastL2BLockTimestamp, firstSequence.BatchNumber-1, s.cfg.L2Coinbase, nil)
_, err = s.buildSendTx(ctx, sequences, lastSequence, firstSequence)
return sequences, err
}
return sequences, err
Expand Down

0 comments on commit 9f1919f

Please sign in to comment.