Skip to content

Commit

Permalink
Rename wallet structs to reduce verbosity at call sites
Browse files Browse the repository at this point in the history
  • Loading branch information
anodar committed Sep 18, 2023
1 parent fa69b19 commit 921ca50
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 67 deletions.
4 changes: 2 additions & 2 deletions arbnode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,15 +827,15 @@ func createNodeImpl(
tmpAddress := common.HexToAddress(config.Staker.ContractWalletAddress)
existingWalletAddress = &tmpAddress
}
wallet, err = validatorwallet.NewContractValidatorWallet(dp, existingWalletAddress, deployInfo.ValidatorWalletCreator, deployInfo.Rollup, l1Reader, txOptsValidator, int64(deployInfo.DeployedAt), func(common.Address) {}, getExtraGas)
wallet, err = validatorwallet.NewContract(dp, existingWalletAddress, deployInfo.ValidatorWalletCreator, deployInfo.Rollup, l1Reader, txOptsValidator, int64(deployInfo.DeployedAt), func(common.Address) {}, getExtraGas)
if err != nil {
return nil, err
}
} else {
if len(config.Staker.ContractWalletAddress) > 0 {
return nil, errors.New("validator contract wallet specified but flag to use a smart contract wallet was not specified")
}
wallet, err = validatorwallet.NewEoaValidatorWallet(dp, deployInfo.Rollup, l1client, txOptsValidator, getExtraGas)
wallet, err = validatorwallet.NewEOA(dp, deployInfo.Rollup, l1client, txOptsValidator, getExtraGas)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions staker/l1_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type L1Validator struct {
rollupAddress common.Address
validatorUtils *rollupgen.ValidatorUtils
client arbutil.L1Interface
builder *txbuilder.ValidatorTxBuilder
builder *txbuilder.Builder
wallet ValidatorWalletInterface
callOpts bind.CallOpts

Expand All @@ -67,7 +67,7 @@ func NewL1Validator(
txStreamer TransactionStreamerInterface,
blockValidator *BlockValidator,
) (*L1Validator, error) {
builder, err := txbuilder.NewValidatorTxBuilder(wallet)
builder, err := txbuilder.NewBuilder(wallet)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion staker/staker.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ type ValidatorWalletInterface interface {
ChallengeManagerAddress() common.Address
L1Client() arbutil.L1Interface
TestTransactions(context.Context, []*types.Transaction) error
ExecuteTransactions(context.Context, *txbuilder.ValidatorTxBuilder, common.Address) (*types.Transaction, error)
ExecuteTransactions(context.Context, *txbuilder.Builder, common.Address) (*types.Transaction, error)
TimeoutChallenges(context.Context, []uint64) (*types.Transaction, error)
CanBatchTxs() bool
AuthIfEoa() *bind.TransactOpts
Expand Down
28 changes: 14 additions & 14 deletions staker/txbuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ type ValidatorWalletInterface interface {
Address() *common.Address
L1Client() arbutil.L1Interface
TestTransactions(context.Context, []*types.Transaction) error
ExecuteTransactions(context.Context, *ValidatorTxBuilder, common.Address) (*types.Transaction, error)
ExecuteTransactions(context.Context, *Builder, common.Address) (*types.Transaction, error)
AuthIfEoa() *bind.TransactOpts
}

// ValidatorTxBuilder combines any transactions sent to it via SendTransaction into one batch,
// Builder combines any transactions sent to it via SendTransaction into one batch,
// which is then sent to the validator wallet.
// This lets the validator make multiple atomic transactions.
// This inherits from an eth client so it can be used as an L1Interface,
// where it transparently intercepts calls to SendTransaction and queues them for the next batch.
type ValidatorTxBuilder struct {
type Builder struct {
arbutil.L1Interface
transactions []*types.Transaction
builderAuth *bind.TransactOpts
isAuthFake bool
wallet ValidatorWalletInterface
}

func NewValidatorTxBuilder(wallet ValidatorWalletInterface) (*ValidatorTxBuilder, error) {
func NewBuilder(wallet ValidatorWalletInterface) (*Builder, error) {
randKey, err := crypto.GenerateKey()
if err != nil {
return nil, err
Expand All @@ -52,30 +52,30 @@ func NewValidatorTxBuilder(wallet ValidatorWalletInterface) (*ValidatorTxBuilder
}
isAuthFake = true
}
return &ValidatorTxBuilder{
return &Builder{
builderAuth: builderAuth,
wallet: wallet,
L1Interface: wallet.L1Client(),
isAuthFake: isAuthFake,
}, nil
}

func (b *ValidatorTxBuilder) BuildingTransactionCount() int {
func (b *Builder) BuildingTransactionCount() int {
return len(b.transactions)
}

func (b *ValidatorTxBuilder) ClearTransactions() {
func (b *Builder) ClearTransactions() {
b.transactions = nil
}

func (b *ValidatorTxBuilder) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
func (b *Builder) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
if len(b.transactions) == 0 && !b.isAuthFake {
return b.L1Interface.EstimateGas(ctx, call)
}
return 0, nil
}

func (b *ValidatorTxBuilder) SendTransaction(ctx context.Context, tx *types.Transaction) error {
func (b *Builder) SendTransaction(ctx context.Context, tx *types.Transaction) error {
b.transactions = append(b.transactions, tx)
err := b.wallet.TestTransactions(ctx, b.transactions)
if err != nil {
Expand All @@ -89,7 +89,7 @@ func (b *ValidatorTxBuilder) SendTransaction(ctx context.Context, tx *types.Tran
// While this is not currently required, it's recommended not to reuse the returned auth for multiple transactions,
// as for an EOA this has the nonce in it. However, the EOA wwallet currently will only publish the first created tx,
// which is why that doesn't really matter.
func (b *ValidatorTxBuilder) AuthWithAmount(ctx context.Context, amount *big.Int) (*bind.TransactOpts, error) {
func (b *Builder) AuthWithAmount(ctx context.Context, amount *big.Int) (*bind.TransactOpts, error) {
nonce, err := b.NonceAt(ctx, b.builderAuth.From, nil)
if err != nil {
return nil, err
Expand All @@ -107,20 +107,20 @@ func (b *ValidatorTxBuilder) AuthWithAmount(ctx context.Context, amount *big.Int

// Auth is the same as AuthWithAmount with a 0 amount specified.
// See AuthWithAmount docs for important details.
func (b *ValidatorTxBuilder) Auth(ctx context.Context) (*bind.TransactOpts, error) {
func (b *Builder) Auth(ctx context.Context) (*bind.TransactOpts, error) {
return b.AuthWithAmount(ctx, common.Big0)
}

func (b *ValidatorTxBuilder) Transactions() []*types.Transaction {
func (b *Builder) Transactions() []*types.Transaction {
return b.transactions
}

// Auth is the same as AuthWithAmount with a 0 amount specified.
// See AuthWithAmount docs for important details.
func (b *ValidatorTxBuilder) BuilderAuth() *bind.TransactOpts {
func (b *Builder) BuilderAuth() *bind.TransactOpts {
return b.builderAuth
}

func (b *ValidatorTxBuilder) WalletAddress() *common.Address {
func (b *Builder) WalletAddress() *common.Address {
return b.wallet.Address()
}
52 changes: 26 additions & 26 deletions staker/validatorwallet/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func init() {
walletCreatedID = parsedValidatorWalletCreator.Events["WalletCreated"].ID
}

type ContractValidatorWallet struct {
type Contract struct {
con *rollupgen.ValidatorWallet
address atomic.Pointer[common.Address]
onWalletCreated func(common.Address)
Expand All @@ -59,8 +59,8 @@ type ContractValidatorWallet struct {
getExtraGas func() uint64
}

func NewContractValidatorWallet(dp *dataposter.DataPoster, address *common.Address, walletFactoryAddr, rollupAddress common.Address, l1Reader *headerreader.HeaderReader, auth *bind.TransactOpts, rollupFromBlock int64, onWalletCreated func(common.Address),
getExtraGas func() uint64) (*ContractValidatorWallet, error) {
func NewContract(dp *dataposter.DataPoster, address *common.Address, walletFactoryAddr, rollupAddress common.Address, l1Reader *headerreader.HeaderReader, auth *bind.TransactOpts, rollupFromBlock int64, onWalletCreated func(common.Address),
getExtraGas func() uint64) (*Contract, error) {
var con *rollupgen.ValidatorWallet
if address != nil {
var err error
Expand All @@ -73,7 +73,7 @@ func NewContractValidatorWallet(dp *dataposter.DataPoster, address *common.Addre
if err != nil {
return nil, err
}
wallet := &ContractValidatorWallet{
wallet := &Contract{
con: con,
onWalletCreated: onWalletCreated,
l1Reader: l1Reader,
Expand All @@ -90,7 +90,7 @@ func NewContractValidatorWallet(dp *dataposter.DataPoster, address *common.Addre
return wallet, nil
}

func (v *ContractValidatorWallet) validateWallet(ctx context.Context) error {
func (v *Contract) validateWallet(ctx context.Context) error {
if v.con == nil || v.auth == nil {
return nil
}
Expand All @@ -109,7 +109,7 @@ func (v *ContractValidatorWallet) validateWallet(ctx context.Context) error {
return nil
}

func (v *ContractValidatorWallet) Initialize(ctx context.Context) error {
func (v *Contract) Initialize(ctx context.Context) error {
err := v.populateWallet(ctx, false)
if err != nil {
return err
Expand All @@ -124,35 +124,35 @@ func (v *ContractValidatorWallet) Initialize(ctx context.Context) error {
}

// May be the nil if the wallet hasn't been deployed yet
func (v *ContractValidatorWallet) Address() *common.Address {
func (v *Contract) Address() *common.Address {
return v.address.Load()
}

// May be zero if the wallet hasn't been deployed yet
func (v *ContractValidatorWallet) AddressOrZero() common.Address {
func (v *Contract) AddressOrZero() common.Address {
addr := v.address.Load()
if addr == nil {
return common.Address{}
}
return *addr
}

func (v *ContractValidatorWallet) TxSenderAddress() *common.Address {
func (v *Contract) TxSenderAddress() *common.Address {
if v.auth == nil {
return nil
}
return &v.auth.From
}

func (v *ContractValidatorWallet) From() common.Address {
func (v *Contract) From() common.Address {
if v.auth == nil {
return common.Address{}
}
return v.auth.From
}

// nil value == 0 value
func (v *ContractValidatorWallet) getAuth(ctx context.Context, value *big.Int) (*bind.TransactOpts, error) {
func (v *Contract) getAuth(ctx context.Context, value *big.Int) (*bind.TransactOpts, error) {
newAuth := *v.auth
newAuth.Context = ctx
newAuth.Value = value
Expand All @@ -164,7 +164,7 @@ func (v *ContractValidatorWallet) getAuth(ctx context.Context, value *big.Int) (
return &newAuth, nil
}

func (v *ContractValidatorWallet) executeTransaction(ctx context.Context, tx *types.Transaction, gasRefunder common.Address) (*types.Transaction, error) {
func (v *Contract) executeTransaction(ctx context.Context, tx *types.Transaction, gasRefunder common.Address) (*types.Transaction, error) {
auth, err := v.getAuth(ctx, tx.Value())
if err != nil {
return nil, err
Expand All @@ -180,7 +180,7 @@ func (v *ContractValidatorWallet) executeTransaction(ctx context.Context, tx *ty
return v.dataPoster.PostTransaction(ctx, time.Now(), auth.Nonce.Uint64(), nil, *v.Address(), data, gas, auth.Value)
}

func (v *ContractValidatorWallet) populateWallet(ctx context.Context, createIfMissing bool) error {
func (v *Contract) populateWallet(ctx context.Context, createIfMissing bool) error {
if v.con != nil {
return nil
}
Expand Down Expand Up @@ -235,7 +235,7 @@ func combineTxes(txes []*types.Transaction) ([][]byte, []common.Address, []*big.
}

// Not thread safe! Don't call this from multiple threads at the same time.
func (v *ContractValidatorWallet) ExecuteTransactions(ctx context.Context, builder *txbuilder.ValidatorTxBuilder, gasRefunder common.Address) (*types.Transaction, error) {
func (v *Contract) ExecuteTransactions(ctx context.Context, builder *txbuilder.Builder, gasRefunder common.Address) (*types.Transaction, error) {
txes := builder.Transactions()
if len(txes) == 0 {
return nil, nil
Expand Down Expand Up @@ -296,7 +296,7 @@ func (v *ContractValidatorWallet) ExecuteTransactions(ctx context.Context, build
return arbTx, nil
}

func (v *ContractValidatorWallet) estimateGas(ctx context.Context, value *big.Int, data []byte) (uint64, error) {
func (v *Contract) estimateGas(ctx context.Context, value *big.Int, data []byte) (uint64, error) {
h, err := v.l1Reader.LastHeader(ctx)
if err != nil {
return 0, fmt.Errorf("getting the last header: %w", err)
Expand Down Expand Up @@ -325,7 +325,7 @@ func (v *ContractValidatorWallet) estimateGas(ctx context.Context, value *big.In
return g + v.getExtraGas(), nil
}

func (v *ContractValidatorWallet) TimeoutChallenges(ctx context.Context, challenges []uint64) (*types.Transaction, error) {
func (v *Contract) TimeoutChallenges(ctx context.Context, challenges []uint64) (*types.Transaction, error) {
auth, err := v.getAuth(ctx, nil)
if err != nil {
return nil, err
Expand All @@ -342,26 +342,26 @@ func (v *ContractValidatorWallet) TimeoutChallenges(ctx context.Context, challen
}

// gasForTxData returns auth.GasLimit if it's nonzero, otherwise returns estimate.
func (v *ContractValidatorWallet) gasForTxData(ctx context.Context, auth *bind.TransactOpts, data []byte) (uint64, error) {
func (v *Contract) gasForTxData(ctx context.Context, auth *bind.TransactOpts, data []byte) (uint64, error) {
if auth.GasLimit != 0 {
return auth.GasLimit, nil
}
return v.estimateGas(ctx, auth.Value, data)
}

func (v *ContractValidatorWallet) L1Client() arbutil.L1Interface {
func (v *Contract) L1Client() arbutil.L1Interface {
return v.l1Reader.Client()
}

func (v *ContractValidatorWallet) RollupAddress() common.Address {
func (v *Contract) RollupAddress() common.Address {
return v.rollupAddress
}

func (v *ContractValidatorWallet) ChallengeManagerAddress() common.Address {
func (v *Contract) ChallengeManagerAddress() common.Address {
return v.challengeManagerAddress
}

func (v *ContractValidatorWallet) TestTransactions(ctx context.Context, txs []*types.Transaction) error {
func (v *Contract) TestTransactions(ctx context.Context, txs []*types.Transaction) error {
if v.Address() == nil {
return nil
}
Expand All @@ -380,23 +380,23 @@ func (v *ContractValidatorWallet) TestTransactions(ctx context.Context, txs []*t
return err
}

func (v *ContractValidatorWallet) CanBatchTxs() bool {
func (v *Contract) CanBatchTxs() bool {
return true
}

func (v *ContractValidatorWallet) AuthIfEoa() *bind.TransactOpts {
func (v *Contract) AuthIfEoa() *bind.TransactOpts {
return nil
}

func (w *ContractValidatorWallet) Start(ctx context.Context) {
func (w *Contract) Start(ctx context.Context) {
w.dataPoster.Start(ctx)
}

func (b *ContractValidatorWallet) StopAndWait() {
func (b *Contract) StopAndWait() {
b.dataPoster.StopAndWait()
}

func (b *ContractValidatorWallet) DataPoster() *dataposter.DataPoster {
func (b *Contract) DataPoster() *dataposter.DataPoster {
return b.dataPoster
}

Expand Down
Loading

0 comments on commit 921ca50

Please sign in to comment.