Skip to content

Commit

Permalink
refactor of contractBase to expose the contrac interface directly and…
Browse files Browse the repository at this point in the history
… be able to create mocks
  • Loading branch information
joanestebanr committed Jul 30, 2024
1 parent 9c0aaa2 commit f187b47
Show file tree
Hide file tree
Showing 22 changed files with 1,066 additions and 96 deletions.
4 changes: 2 additions & 2 deletions etherman/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.Banana.RollupManager.Contract().VerifyBatchesTrustedAggregator(
tx, err := etherMan.Contracts.Banana.RollupManager.VerifyBatchesTrustedAggregator(
&opts,
etherMan.RollupID,
pendStateNum,
Expand All @@ -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.Banana.RollupManager.Contract().GetRollupSequencedBatches(&bind.CallOpts{Pending: false}, etherman.RollupID, batchNumber)
rollupData, err := etherman.Contracts.Banana.RollupManager.GetRollupSequencedBatches(&bind.CallOpts{Pending: false}, etherman.RollupID, batchNumber)
if err != nil {
return common.Hash{}, err
}
Expand Down
47 changes: 25 additions & 22 deletions etherman/contracts/base.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
package contracts

import (
"reflect"

"github.com/0xPolygon/cdk/log"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
)

type ContractBase[T any] struct {
contract *T
type ContractBase struct {
address common.Address
contractName NameType
version VersionType
}

type contractConstructorFunc[T any] func(address common.Address, backend bind.ContractBackend) (*T, error)
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 {
log.Errorf("failed to bind contract %s at address %s. Err:%w", name, address.String(), err)
return nil, err
}

return &ContractBase[T]{
contract: contractBind,
func NewContractBase(address common.Address, backend bind.ContractBackend,
name NameType, version VersionType) *ContractBase {
return &ContractBase{
address: address,
contractName: name,
version: version,
}, nil
}

func (e *ContractBase[T]) Contract() *T {
return e.contract
}
}

func (e *ContractBase[T]) Address() common.Address {
func (e *ContractBase) Address() common.Address {
return e.address
}

func (e *ContractBase[T]) Name() string {
func (e *ContractBase) Name() string {
return string(e.contractName)
}

func (e *ContractBase[T]) Version() string {
func (e *ContractBase) Version() string {
return string(e.version)
}

func (e *ContractBase[T]) String() string {
func (e *ContractBase) String() string {
return e.Version() + "/" + e.Name() + "@" + e.Address().String()
}

func NewContractMagic[C any, T any](constructor contractConstructorFunc[T], address common.Address, backend bind.ContractBackend, name NameType, version VersionType) (*C, 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
}
tmp := new(C)
values := reflect.ValueOf(tmp).Elem()
values.FieldByIndex([]int{0}).Set(reflect.ValueOf(contractBind))
values.FieldByIndex([]int{1}).Set(reflect.ValueOf(NewContractBase(address, backend, name, version)))
return tmp, nil
}
16 changes: 16 additions & 0 deletions etherman/contracts/base_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package contracts_test

import (
"testing"

"github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonzkevmglobalexitrootv2"
"github.com/0xPolygon/cdk/etherman/contracts"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)

func TestNewContractMagic(t *testing.T) {
ger, err := contracts.NewContractMagic[contracts.GlobalExitRootBananaType](polygonzkevmglobalexitrootv2.NewPolygonzkevmglobalexitrootv2, common.Address{}, nil, contracts.ContractNameGlobalExitRoot, contracts.VersionBanana)
require.NoError(t, err)
require.NotNil(t, ger)
}
24 changes: 18 additions & 6 deletions etherman/contracts/contracts_banana.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
)

type GlobalExitRootBananaType = ContractBase[polygonzkevmglobalexitrootv2.Polygonzkevmglobalexitrootv2]
type RollupBananaType = ContractBase[polygonvalidiumetrog.Polygonvalidiumetrog]
type RollupManagerBananaType = ContractBase[polygonrollupmanager.Polygonrollupmanager]
type GlobalExitRootBananaType struct {
*polygonzkevmglobalexitrootv2.Polygonzkevmglobalexitrootv2
*ContractBase
}

type RollupManagerBananaType struct {
*polygonrollupmanager.Polygonrollupmanager
*ContractBase
}

type RollupBananaType struct {
*polygonvalidiumetrog.Polygonvalidiumetrog
*ContractBase
}

type ContractsBanana struct {
GlobalExitRoot GlobalExitRootBananaType
Expand All @@ -19,16 +30,17 @@ type ContractsBanana struct {
}

func NewContractsBanana(cfg config.L1Config, backend bind.ContractBackend) (*ContractsBanana, error) {
ger, err := NewContractBase(polygonzkevmglobalexitrootv2.NewPolygonzkevmglobalexitrootv2, cfg.GlobalExitRootManagerAddr, backend, ContractNameGlobalExitRoot, VersionBanana)

ger, err := NewContractMagic[GlobalExitRootBananaType](polygonzkevmglobalexitrootv2.NewPolygonzkevmglobalexitrootv2, cfg.GlobalExitRootManagerAddr, backend, ContractNameGlobalExitRoot, VersionBanana)
if err != nil {
return nil, err
}
rollup, err := NewContractBase(polygonvalidiumetrog.NewPolygonvalidiumetrog, cfg.ZkEVMAddr, backend, ContractNameRollup, VersionBanana)
rollup, err := NewContractMagic[RollupBananaType](polygonvalidiumetrog.NewPolygonvalidiumetrog, cfg.ZkEVMAddr, backend, ContractNameRollup, VersionBanana)
if err != nil {
return nil, err
}

rollupManager, err := NewContractBase(polygonrollupmanager.NewPolygonrollupmanager, cfg.RollupManagerAddr, backend, ContractNameRollupManager, VersionBanana)
rollupManager, err := NewContractMagic[RollupManagerBananaType](polygonrollupmanager.NewPolygonrollupmanager, cfg.RollupManagerAddr, backend, ContractNameRollupManager, VersionBanana)
if err != nil {
return nil, err
}
Expand Down
23 changes: 17 additions & 6 deletions etherman/contracts/contracts_elderberry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
)

type GlobalExitRootElderberryType = ContractBase[polygonzkevmglobalexitrootv2.Polygonzkevmglobalexitrootv2]
type RollupElderberryType = ContractBase[polygonvalidiumetrog.Polygonvalidiumetrog]
type RollupManagerElderberryType = ContractBase[polygonrollupmanager.Polygonrollupmanager]
type GlobalExitRootElderberryType struct {
*polygonzkevmglobalexitrootv2.Polygonzkevmglobalexitrootv2
*ContractBase
}

type RollupElderberryType struct {
*polygonvalidiumetrog.Polygonvalidiumetrog
*ContractBase
}

type RollupManagerElderberryType struct {
*polygonrollupmanager.Polygonrollupmanager
*ContractBase
}

type ContractsElderberry struct {
GlobalExitRoot GlobalExitRootElderberryType
Expand All @@ -19,16 +30,16 @@ type ContractsElderberry struct {
}

func NewContractsElderberry(cfg config.L1Config, backend bind.ContractBackend) (*ContractsElderberry, error) {
ger, err := NewContractBase(polygonzkevmglobalexitrootv2.NewPolygonzkevmglobalexitrootv2, cfg.GlobalExitRootManagerAddr, backend, ContractNameGlobalExitRoot, VersionElderberry)
ger, err := NewContractMagic[GlobalExitRootElderberryType](polygonzkevmglobalexitrootv2.NewPolygonzkevmglobalexitrootv2, cfg.GlobalExitRootManagerAddr, backend, ContractNameGlobalExitRoot, VersionElderberry)
if err != nil {
return nil, err
}
rollup, err := NewContractBase(polygonvalidiumetrog.NewPolygonvalidiumetrog, cfg.ZkEVMAddr, backend, ContractNameRollup, VersionElderberry)
rollup, err := NewContractMagic[RollupElderberryType](polygonvalidiumetrog.NewPolygonvalidiumetrog, cfg.ZkEVMAddr, backend, ContractNameRollup, VersionElderberry)
if err != nil {
return nil, err
}

rollupManager, err := NewContractBase(polygonrollupmanager.NewPolygonrollupmanager, cfg.RollupManagerAddr, backend, ContractNameRollupManager, VersionElderberry)
rollupManager, err := NewContractMagic[RollupManagerElderberryType](polygonrollupmanager.NewPolygonrollupmanager, cfg.RollupManagerAddr, backend, ContractNameRollupManager, VersionElderberry)
if err != nil {
return nil, err
}
Expand Down
22 changes: 11 additions & 11 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func NewClient(cfg config.Config, l1Config config.L1Config, commonConfig cdkcomm
}
log.Info(contracts.String())
// Get RollupID
rollupID, err := contracts.Banana.RollupManager.Contract().RollupAddressToID(&bind.CallOpts{Pending: false}, l1Config.ZkEVMAddr)
rollupID, err := contracts.Banana.RollupManager.RollupAddressToID(&bind.CallOpts{Pending: false}, l1Config.ZkEVMAddr)
if err != nil {
log.Errorf("error getting rollupID from %s : %+v", contracts.Banana.RollupManager.String(), err)
return nil, err
Expand All @@ -114,7 +114,7 @@ func NewClient(cfg config.Config, l1Config config.L1Config, commonConfig cdkcomm
}

if commonConfig.IsValidiumMode {
dapAddr, err := contracts.Banana.Rollup.Contract().DataAvailabilityProtocol(&bind.CallOpts{Pending: false})
dapAddr, err := contracts.Banana.Rollup.DataAvailabilityProtocol(&bind.CallOpts{Pending: false})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -148,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.Banana.RollupManager.Contract().GetBatchFee(&bind.CallOpts{Pending: false})
f, err := etherMan.Contracts.Banana.RollupManager.GetBatchFee(&bind.CallOpts{Pending: false})
if err != nil {
return nil, err
}
Expand All @@ -158,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.Banana.Rollup.Contract().TrustedSequencer(&bind.CallOpts{Pending: false})
return etherMan.Contracts.Banana.Rollup.TrustedSequencer(&bind.CallOpts{Pending: false})
}

// HeaderByNumber returns a block header from the current canonical chain. If number is
Expand All @@ -181,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.Banana.RollupManager.Contract().RollupIDToRollupData(&bind.CallOpts{Pending: false}, etherMan.RollupID)
rollupData, err := etherMan.Contracts.Banana.RollupManager.RollupIDToRollupData(&bind.CallOpts{Pending: false}, etherMan.RollupID)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -223,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.Banana.RollupManager.Contract().RollupIDToRollupData(&bind.CallOpts{Pending: false}, etherMan.RollupID)
rollupData, err := etherMan.Contracts.Banana.RollupManager.RollupIDToRollupData(&bind.CallOpts{Pending: false}, etherMan.RollupID)
if err != nil {
return 0, err
}
Expand All @@ -242,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.Banana.Rollup.Contract().TrustedSequencerURL(&bind.CallOpts{Pending: false})
return etherMan.Contracts.Banana.Rollup.TrustedSequencerURL(&bind.CallOpts{Pending: false})
}

// GetL2ChainID returns L2 Chain ID
func (etherMan *Client) GetL2ChainID() (uint64, error) {
rollupData, err := etherMan.Contracts.Banana.RollupManager.Contract().RollupIDToRollupData(&bind.CallOpts{Pending: false}, etherMan.RollupID)
rollupData, err := etherMan.Contracts.Banana.RollupManager.RollupIDToRollupData(&bind.CallOpts{Pending: false}, etherMan.RollupID)
log.Debug("chainID read from rollupManager: ", rollupData.ChainID)
if err != nil {
log.Debug("error from rollupManager: ", err)
Expand Down Expand Up @@ -397,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.Banana.Rollup.Contract().DataAvailabilityProtocol(&bind.CallOpts{Pending: false})
return etherMan.Contracts.Banana.Rollup.DataAvailabilityProtocol(&bind.CallOpts{Pending: false})
}

// GetDAProtocolName returns the name of the data availability protocol
Expand All @@ -407,7 +407,7 @@ 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.Banana.Rollup.Contract().LastAccInputHash(&bind.CallOpts{Pending: false})
return etherMan.Contracts.Banana.Rollup.LastAccInputHash(&bind.CallOpts{Pending: false})
}

// GetL1InfoRoot gets the L1 info root from the SC
Expand All @@ -419,7 +419,7 @@ func (etherMan *Client) GetL1InfoRoot(indexL1InfoRoot uint32) (common.Hash, erro
)

if indexL1InfoRoot > 0 {
lastL1InfoTreeRoot, err = etherMan.Contracts.Banana.GlobalExitRoot.Contract().L1InfoRootMap(&bind.CallOpts{Pending: false}, indexL1InfoRoot)
lastL1InfoTreeRoot, err = etherMan.Contracts.Banana.GlobalExitRoot.L1InfoRootMap(&bind.CallOpts{Pending: false}, indexL1InfoRoot)
if err != nil {
log.Errorf("error calling SC globalexitroot L1InfoLeafMap: %v", err)
}
Expand Down
27 changes: 17 additions & 10 deletions sequencesender/txbuilder/banana_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,30 @@ import (

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"
"github.com/ethereum/go-ethereum/common"
)

type TxBuilderBananaBase struct {
rollupContract contracts.RollupBananaType
globalExitRootContract contracts.GlobalExitRootBananaType
type rollupBananaBaseContractor interface {
LastAccInputHash(opts *bind.CallOpts) ([32]byte, error)
}

opts bind.TransactOpts
type globalExitRootBananaContractor interface {
L1InfoRootMap(opts *bind.CallOpts, index uint32) ([32]byte, error)
String() string
}

type TxBuilderBananaBase struct {
rollupContract rollupBananaBaseContractor
globalExitRootContract globalExitRootBananaContractor
opts bind.TransactOpts
}

func NewTxBuilderBananaBase(rollupContract contracts.RollupBananaType,
gerContract contracts.GlobalExitRootBananaType,
func NewTxBuilderBananaBase(rollupContract rollupBananaBaseContractor,
gerContract globalExitRootBananaContractor,
opts bind.TransactOpts) *TxBuilderBananaBase {
return &TxBuilderBananaBase{
rollupContract: rollupContract,
Expand Down Expand Up @@ -100,7 +107,7 @@ func (t *TxBuilderBananaBase) NewSequence(batches []seqsendertypes.Batch, coinba

sequence.L1InfoRoot = l1InfoRoot

accInputHash, err := t.rollupContract.Contract().LastAccInputHash(&bind.CallOpts{Pending: false})
accInputHash, err := t.rollupContract.LastAccInputHash(&bind.CallOpts{Pending: false})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -138,9 +145,9 @@ func (t *TxBuilderBananaBase) getL1InfoRoot(indexL1InfoRoot uint32) (common.Hash
)

if indexL1InfoRoot > 0 {
lastL1InfoTreeRoot, err = t.globalExitRootContract.Contract().L1InfoRootMap(&bind.CallOpts{Pending: false}, indexL1InfoRoot)
lastL1InfoTreeRoot, err = t.globalExitRootContract.L1InfoRootMap(&bind.CallOpts{Pending: false}, indexL1InfoRoot)
if err != nil {
log.Errorf("error calling SC globalexitroot L1InfoLeafMap: %v", err)
log.Errorf("error calling SC globalexitroot L1InfoLeafMap (%s) Err: %w", t.globalExitRootContract.String(), err)
}
}

Expand Down
Loading

0 comments on commit f187b47

Please sign in to comment.