Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validator wallet refactor for bold #2804

Merged
merged 7 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions arbnode/dataposter/data_poster.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ func (p *DataPoster) feeAndTipCaps(ctx context.Context, nonce uint64, gasLimit u
return newBaseFeeCap, newTipCap, newBlobFeeCap, nil
}

func (p *DataPoster) PostSimpleTransactionAutoNonce(ctx context.Context, to common.Address, calldata []byte, gasLimit uint64, value *big.Int) (*types.Transaction, error) {
func (p *DataPoster) PostSimpleTransaction(ctx context.Context, to common.Address, calldata []byte, gasLimit uint64, value *big.Int) (*types.Transaction, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
nonce, _, _, _, err := p.getNextNonceAndMaybeMeta(ctx, 1)
Expand All @@ -722,10 +722,6 @@ func (p *DataPoster) PostSimpleTransactionAutoNonce(ctx context.Context, to comm
return p.postTransactionWithMutex(ctx, time.Now(), nonce, nil, to, calldata, gasLimit, value, nil, nil)
}

func (p *DataPoster) PostSimpleTransaction(ctx context.Context, nonce uint64, to common.Address, calldata []byte, gasLimit uint64, value *big.Int) (*types.Transaction, error) {
return p.PostTransaction(ctx, time.Now(), nonce, nil, to, calldata, gasLimit, value, nil, nil)
}

func (p *DataPoster) PostTransaction(ctx context.Context, dataCreatedAt time.Time, nonce uint64, meta []byte, to common.Address, calldata []byte, gasLimit uint64, value *big.Int, kzgBlobs []kzg4844.Blob, accessList types.AccessList) (*types.Transaction, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
Expand Down
6 changes: 3 additions & 3 deletions staker/bold/bold_staker.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023, Offchain Labs, Inc.
// For license information, see https://github.com/offchainlabs/bold/blob/main/LICENSE
// Copyright 2023-2024, Offchain Labs, Inc.
// For license information, see https://github.com/offchainlabs/nitro/blob/main/LICENSE
package bold

import (
Expand Down Expand Up @@ -361,7 +361,7 @@ func newBOLDChallengeManager(
if err != nil {
return nil, fmt.Errorf("could not get challenge manager: %w", err)
}
assertionChain, err := solimpl.NewAssertionChain(ctx, rollupAddress, chalManager, txOpts, client, solimpl.NewDataPosterTransactor(dataPoster))
assertionChain, err := solimpl.NewAssertionChain(ctx, rollupAddress, chalManager, txOpts, client, NewDataPosterTransactor(dataPoster))
if err != nil {
return nil, fmt.Errorf("could not create assertion chain: %w", err)
}
Expand Down
44 changes: 44 additions & 0 deletions staker/bold/data_poster_transactor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2023-2024, Offchain Labs, Inc.
// For license information, see https://github.com/offchainlabs/nitro/blob/main/LICENSE
package bold

import (
"context"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"

solimpl "github.com/offchainlabs/bold/chain-abstraction/sol-implementation"
"github.com/offchainlabs/nitro/arbnode/dataposter"
)

// DataPosterTransactor is a wrapper around a DataPoster that implements the Transactor interface.
type DataPosterTransactor struct {
fifo *solimpl.FIFO
*dataposter.DataPoster
}

func NewDataPosterTransactor(dataPoster *dataposter.DataPoster) *DataPosterTransactor {
return &DataPosterTransactor{
fifo: solimpl.NewFIFO(1000),
DataPoster: dataPoster,
}
}

func (d *DataPosterTransactor) SendTransaction(ctx context.Context, fn func(opts *bind.TransactOpts) (*types.Transaction, error), opts *bind.TransactOpts, gas uint64) (*types.Transaction, error) {
// Try to acquire lock and if it fails, wait for a bit and try again.
for !d.fifo.Lock() {
select {
case <-time.After(100 * time.Millisecond):
case <-ctx.Done():
return nil, ctx.Err()
}
}
defer d.fifo.Unlock()
tx, err := fn(opts)
if err != nil {
return nil, err
}
return d.PostSimpleTransaction(ctx, *tx.To(), tx.Data(), gas, tx.Value())
}
19 changes: 5 additions & 14 deletions staker/legacy/fast_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewFastConfirmSafe(
gasRefunder: gasRefunder,
l1Reader: l1Reader,
}
safe, err := contractsgen.NewSafe(fastConfirmSafeAddress, builder)
safe, err := contractsgen.NewSafe(fastConfirmSafeAddress, wallet.L1Client())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -127,11 +127,7 @@ func (f *FastConfirmSafe) tryFastConfirmation(ctx context.Context, blockHash com
}

log.Info("Approving Safe tx hash to fast confirm", "safeHash", safeTxHash, "nodeHash", nodeHash)
auth, err := f.builder.Auth(ctx)
if err != nil {
return err
}
_, err = f.safe.ApproveHash(auth, safeTxHash)
_, err = f.safe.ApproveHash(f.builder.Auth(ctx), safeTxHash)
if err != nil {
return err
}
Expand Down Expand Up @@ -160,7 +156,7 @@ func (f *FastConfirmSafe) tryFastConfirmation(ctx context.Context, blockHash com
}

func (f *FastConfirmSafe) flushTransactions(ctx context.Context) error {
arbTx, err := f.wallet.ExecuteTransactions(ctx, f.builder, f.gasRefunder)
arbTx, err := f.builder.ExecuteTransactions(ctx)
if err != nil {
return err
}
Expand All @@ -172,7 +168,6 @@ func (f *FastConfirmSafe) flushTransactions(ctx context.Context) error {
return fmt.Errorf("error waiting for tx receipt: %w", err)
}
}
f.builder.ClearTransactions()
return nil
}

Expand Down Expand Up @@ -229,13 +224,9 @@ func (f *FastConfirmSafe) checkApprovedHashAndExecTransaction(ctx context.Contex
}
}
if approvedHashCount >= f.threshold {
auth, err := f.builder.Auth(ctx)
if err != nil {
return false, err
}
log.Info("Executing Safe tx to fast confirm", "safeHash", safeTxHash)
_, err = f.safe.ExecTransaction(
auth,
_, err := f.safe.ExecTransaction(
f.builder.Auth(ctx),
f.wallet.RollupAddress(),
big.NewInt(0),
fastConfirmCallData,
Expand Down
17 changes: 5 additions & 12 deletions staker/legacy/l1_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,17 @@ func NewL1Validator(
client *ethclient.Client,
wallet ValidatorWalletInterface,
validatorUtilsAddress common.Address,
gasRefunder common.Address,
callOpts bind.CallOpts,
inboxTracker staker.InboxTrackerInterface,
txStreamer staker.TransactionStreamerInterface,
blockValidator *staker.BlockValidator,
) (*L1Validator, error) {
builder, err := txbuilder.NewBuilder(wallet)
builder, err := txbuilder.NewBuilder(wallet, gasRefunder)
if err != nil {
return nil, err
}
rollup, err := staker.NewRollupWatcher(wallet.RollupAddress(), builder, callOpts)
rollup, err := staker.NewRollupWatcher(wallet.RollupAddress(), wallet.L1Client(), callOpts)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -159,11 +160,7 @@ func (v *L1Validator) resolveNextNode(ctx context.Context, info *staker.StakerIn
return false, nil
}
log.Warn("rejecting node", "node", unresolvedNodeIndex)
auth, err := v.builder.Auth(ctx)
if err != nil {
return false, err
}
_, err = v.rollup.RejectNextNode(auth, *addr)
_, err = v.rollup.RejectNextNode(v.builder.Auth(ctx), *addr)
return true, err
case CONFIRM_TYPE_VALID:
nodeInfo, err := v.rollup.LookupNode(ctx, unresolvedNodeIndex)
Expand All @@ -172,11 +169,7 @@ func (v *L1Validator) resolveNextNode(ctx context.Context, info *staker.StakerIn
}
afterGs := nodeInfo.AfterState().GlobalState
log.Info("confirming node", "node", unresolvedNodeIndex)
auth, err := v.builder.Auth(ctx)
if err != nil {
return false, err
}
_, err = v.rollup.ConfirmNextNode(auth, afterGs.BlockHash, afterGs.SendRoot)
_, err = v.rollup.ConfirmNextNode(v.builder.Auth(ctx), afterGs.BlockHash, afterGs.SendRoot)
if err != nil {
return false, err
}
Expand Down
67 changes: 16 additions & 51 deletions staker/legacy/staker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/offchainlabs/nitro/cmd/genericconf"
"github.com/offchainlabs/nitro/solgen/go/rollupgen"
"github.com/offchainlabs/nitro/staker"
"github.com/offchainlabs/nitro/staker/txbuilder"
"github.com/offchainlabs/nitro/util"
"github.com/offchainlabs/nitro/util/arbmath"
"github.com/offchainlabs/nitro/util/headerreader"
Expand Down Expand Up @@ -292,7 +291,7 @@ type ValidatorWalletInterface interface {
ChallengeManagerAddress() common.Address
L1Client() *ethclient.Client
TestTransactions(context.Context, []*types.Transaction) error
ExecuteTransactions(context.Context, *txbuilder.Builder, common.Address) (*types.Transaction, error)
ExecuteTransactions(context.Context, []*types.Transaction, common.Address) (*types.Transaction, error)
TimeoutChallenges(context.Context, []uint64) (*types.Transaction, error)
CanBatchTxs() bool
AuthIfEoa() *bind.TransactOpts
Expand All @@ -318,7 +317,7 @@ func NewStaker(
return nil, err
}
client := l1Reader.Client()
val, err := NewL1Validator(client, wallet, validatorUtilsAddress, callOpts,
val, err := NewL1Validator(client, wallet, validatorUtilsAddress, config().GasRefunder(), callOpts,
statelessBlockValidator.InboxTracker(), statelessBlockValidator.InboxStreamer(), blockValidator)
if err != nil {
return nil, err
Expand Down Expand Up @@ -452,12 +451,9 @@ func (s *Staker) tryFastConfirmation(ctx context.Context, blockHash common.Hash,
if s.fastConfirmSafe != nil {
return s.fastConfirmSafe.tryFastConfirmation(ctx, blockHash, sendRoot, nodeHash)
}
auth, err := s.builder.Auth(ctx)
if err != nil {
return err
}
auth := s.builder.Auth(ctx)
log.Info("Fast confirming node with wallet", "wallet", auth.From, "nodeHash", nodeHash)
_, err = s.rollup.FastConfirmNextNode(auth, blockHash, sendRoot, nodeHash)
_, err := s.rollup.FastConfirmNextNode(auth, blockHash, sendRoot, nodeHash)
return err
}

Expand Down Expand Up @@ -823,7 +819,7 @@ func (s *Staker) Act(ctx context.Context) (*types.Transaction, error) {
}
if s.builder.BuildingTransactionCount() > 0 {
// Try to fast confirm previous nodes before working on new ones
return s.wallet.ExecuteTransactions(ctx, s.builder, cfg.GasRefunder())
return s.builder.ExecuteTransactions(ctx)
}
}
}
Expand Down Expand Up @@ -893,24 +889,17 @@ func (s *Staker) Act(ctx context.Context) (*types.Transaction, error) {
stakeIsUnwanted := effectiveStrategy < StakeLatestStrategy
if stakeIsTooOutdated || stakeIsUnwanted {
// Note: we must have an address if rawInfo != nil
auth, err := s.builder.Auth(ctx)
if err != nil {
return nil, err
}
auth := s.builder.Auth(ctx)
_, err = s.rollup.ReturnOldDeposit(auth, walletAddressOrZero)
if err != nil {
return nil, fmt.Errorf("error returning old deposit (from our staker %v): %w", walletAddressOrZero, err)
}
auth, err = s.builder.Auth(ctx)
if err != nil {
return nil, err
}
_, err = s.rollup.WithdrawStakerFunds(auth)
if err != nil {
return nil, fmt.Errorf("error withdrawing staker funds from our staker %v: %w", walletAddressOrZero, err)
}
log.Info("removing old stake and withdrawing funds")
return s.wallet.ExecuteTransactions(ctx, s.builder, cfg.GasRefunder())
return s.builder.ExecuteTransactions(ctx)
}
}

Expand All @@ -920,11 +909,7 @@ func (s *Staker) Act(ctx context.Context) (*types.Transaction, error) {
return nil, fmt.Errorf("error checking withdrawable funds of our staker %v: %w", walletAddressOrZero, err)
}
if withdrawable.Sign() > 0 {
auth, err := s.builder.Auth(ctx)
if err != nil {
return nil, err
}
_, err = s.rollup.WithdrawStakerFunds(auth)
_, err = s.rollup.WithdrawStakerFunds(s.builder.Auth(ctx))
if err != nil {
return nil, fmt.Errorf("error withdrawing our staker %v funds: %w", walletAddressOrZero, err)
}
Expand Down Expand Up @@ -964,7 +949,7 @@ func (s *Staker) Act(ctx context.Context) (*types.Transaction, error) {
if info.StakerInfo == nil && info.StakeExists {
log.Info("staking to execute transactions")
}
return s.wallet.ExecuteTransactions(ctx, s.builder, cfg.GasRefunder())
return s.builder.ExecuteTransactions(ctx)
}

func (s *Staker) handleConflict(ctx context.Context, info *staker.StakerInfo) error {
Expand All @@ -983,8 +968,8 @@ func (s *Staker) handleConflict(ctx context.Context, info *staker.StakerInfo) er

newChallengeManager, err := NewChallengeManager(
ctx,
s.builder,
s.builder.BuilderAuth(),
s.client,
s.builder.Auth(context.TODO()),
eljobe marked this conversation as resolved.
Show resolved Hide resolved
*s.builder.WalletAddress(),
s.wallet.ChallengeManagerAddress(),
*info.CurrentChallenge,
Expand Down Expand Up @@ -1042,11 +1027,7 @@ func (s *Staker) advanceStake(ctx context.Context, info *OurStakerInfo, effectiv

// We'll return early if we already have a stake
if info.StakeExists {
auth, err := s.builder.Auth(ctx)
if err != nil {
return err
}
_, err = s.rollup.StakeOnNewNode(auth, action.assertion.AsSolidityStruct(), action.hash, action.prevInboxMaxCount)
_, err = s.rollup.StakeOnNewNode(s.builder.Auth(ctx), action.assertion.AsSolidityStruct(), action.hash, action.prevInboxMaxCount)
if err != nil {
return fmt.Errorf("error staking on new node: %w", err)
}
Expand All @@ -1058,12 +1039,8 @@ func (s *Staker) advanceStake(ctx context.Context, info *OurStakerInfo, effectiv
if err != nil {
return fmt.Errorf("error getting current required stake: %w", err)
}
auth, err := s.builder.AuthWithAmount(ctx, stakeAmount)
if err != nil {
return err
}
_, err = s.rollup.NewStakeOnNewNode(
auth,
s.builder.AuthWithAmount(ctx, stakeAmount),
action.assertion.AsSolidityStruct(),
action.hash,
action.prevInboxMaxCount,
Expand Down Expand Up @@ -1096,11 +1073,7 @@ func (s *Staker) advanceStake(ctx context.Context, info *OurStakerInfo, effectiv
log.Info("staking on existing node", "node", action.number)
// We'll return early if we already havea stake
if info.StakeExists {
auth, err := s.builder.Auth(ctx)
if err != nil {
return err
}
_, err = s.rollup.StakeOnExistingNode(auth, action.number, action.hash)
_, err = s.rollup.StakeOnExistingNode(s.builder.Auth(ctx), action.number, action.hash)
if err != nil {
return fmt.Errorf("error staking on existing node: %w", err)
}
Expand All @@ -1112,12 +1085,8 @@ func (s *Staker) advanceStake(ctx context.Context, info *OurStakerInfo, effectiv
if err != nil {
return fmt.Errorf("error getting current required stake: %w", err)
}
auth, err := s.builder.AuthWithAmount(ctx, stakeAmount)
if err != nil {
return err
}
_, err = s.rollup.NewStakeOnExistingNode(
auth,
s.builder.AuthWithAmount(ctx, stakeAmount),
action.number,
action.hash,
)
Expand Down Expand Up @@ -1193,12 +1162,8 @@ func (s *Staker) createConflict(ctx context.Context, info *staker.StakerInfo) er
return fmt.Errorf("error looking up node %v: %w", conflictInfo.Node2, err)
}
log.Warn("creating challenge", "node1", conflictInfo.Node1, "node2", conflictInfo.Node2, "otherStaker", staker)
auth, err := s.builder.Auth(ctx)
if err != nil {
return err
}
_, err = s.rollup.CreateChallenge(
auth,
s.builder.Auth(ctx),
[2]common.Address{staker1, staker2},
[2]uint64{conflictInfo.Node1, conflictInfo.Node2},
node1Info.MachineStatuses(),
Expand Down
8 changes: 2 additions & 6 deletions staker/multi_protocol/multi_protocol_staker.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,15 @@ func (m *MultiProtocolStaker) setupBoldStaker(
ctx context.Context,
rollupAddress common.Address,
) (*boldstaker.BOLDStaker, error) {
txBuilder, err := txbuilder.NewBuilder(m.wallet)
if err != nil {
return nil, err
}
auth, err := txBuilder.Auth(ctx)
txBuilder, err := txbuilder.NewBuilder(m.wallet, m.legacyConfig().GasRefunder())
if err != nil {
return nil, err
}
boldStaker, err := boldstaker.NewBOLDStaker(
ctx,
rollupAddress,
*m.getCallOpts(ctx),
auth,
txBuilder.SingleTxAuth(),
m.l1Reader,
m.blockValidator,
m.statelessBlockValidator,
Expand Down
Loading
Loading