From 3df9cc0bda9fb7e351a4137f9a12a6c93448da2c Mon Sep 17 00:00:00 2001 From: thomaspanf Date: Mon, 30 Sep 2024 23:05:57 -0700 Subject: [PATCH] Handle a case where maxPriorityFee > maxFee, consolidate fields --- .../node/auto-init-voting-power.go | 63 +++++++++---------- rocketpool-daemon/node/node.go | 28 +++++++++ 2 files changed, 58 insertions(+), 33 deletions(-) diff --git a/rocketpool-daemon/node/auto-init-voting-power.go b/rocketpool-daemon/node/auto-init-voting-power.go index cfeb19ddd..6441e6f06 100644 --- a/rocketpool-daemon/node/auto-init-voting-power.go +++ b/rocketpool-daemon/node/auto-init-voting-power.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log/slog" - "math/big" "github.com/ethereum/go-ethereum/common" "github.com/rocket-pool/node-manager-core/beacon" @@ -22,17 +21,16 @@ import ( ) type AutoInitVotingPower struct { - ctx context.Context - sp *services.ServiceProvider - logger *slog.Logger - cfg *config.SmartNodeConfig - w *wallet.Wallet - rp *rocketpool.RocketPool - bc beacon.IBeaconClient - gasThreshold float64 - maxFee *big.Int - maxPriorityFee *big.Int - nodeAddress common.Address + ctx context.Context + sp *services.ServiceProvider + logger *slog.Logger + cfg *config.SmartNodeConfig + w *wallet.Wallet + rp *rocketpool.RocketPool + bc beacon.IBeaconClient + gasThreshold float64 + gasSettings *GasSettings + nodeAddress common.Address } // Auto Initialize Vote Power @@ -40,18 +38,23 @@ func NewAutoInitVotingPower(ctx context.Context, sp *services.ServiceProvider, l cfg := sp.GetConfig() log := logger.With(slog.String(keys.TaskKey, "Auto Initialize Vote Power")) maxFee, maxPriorityFee := getAutoTxInfo(cfg, log) - return &AutoInitVotingPower{ - ctx: ctx, - sp: sp, - logger: log, - cfg: cfg, - w: sp.GetWallet(), - rp: sp.GetRocketPool(), - bc: sp.GetBeaconClient(), - gasThreshold: cfg.AutoInitVPThreshold.Value, + + gasSettings := &GasSettings{ maxFee: maxFee, maxPriorityFee: maxPriorityFee, } + + return &AutoInitVotingPower{ + ctx: ctx, + sp: sp, + logger: log, + cfg: cfg, + w: sp.GetWallet(), + rp: sp.GetRocketPool(), + bc: sp.GetBeaconClient(), + gasThreshold: cfg.AutoInitVPThreshold.Value, + gasSettings: gasSettings, + } } func (t *AutoInitVotingPower) Run(state *state.NetworkState) error { @@ -73,7 +76,7 @@ func (t *AutoInitVotingPower) Run(state *state.NetworkState) error { votingInitialized := node.IsVotingInitialized.Get() // Create the tx and submit if voting isn't initialized - if !votingInitialized { + if votingInitialized { txSubmission, err := t.createInitializeVotingTx() if err != nil { return fmt.Errorf("error preparing submission to initialize voting for node %s: %w", t.nodeAddress.Hex(), err) @@ -101,7 +104,7 @@ func (t *AutoInitVotingPower) createInitializeVotingTx() (*eth.TransactionSubmis return nil, fmt.Errorf("error creating node %s binding: %w", t.nodeAddress.Hex(), err) } // Get the tx info - txInfo, err := node.InitializeVoting(opts) + txInfo, err := node.StakeRpl(eth.EthToWei(1), opts) if err != nil { return nil, fmt.Errorf("error estimating the gas required to initialize voting for node %s: %w", t.nodeAddress.Hex(), err) } @@ -124,21 +127,15 @@ func (t *AutoInitVotingPower) initializeVotingPower(submission *eth.TransactionS } // Get the max fee - maxFee := t.maxFee - if maxFee == nil || maxFee.Uint64() == 0 { - maxFee, err = gas.GetMaxFeeWeiForDaemon(t.logger) + if t.gasSettings.maxFee == nil || t.gasSettings.maxFee.Uint64() == 0 { + t.gasSettings.maxFee, err = gas.GetMaxFeeWeiForDaemon(t.logger) if err != nil { return err } } - // Lower the priority fee when the suggested maxfee is lower than the user requested priority fee - if maxFee.Cmp(t.maxPriorityFee) < 0 { - t.maxPriorityFee = new(big.Int).Div(maxFee, big.NewInt(2)) - } - - opts.GasFeeCap = maxFee - opts.GasTipCap = t.maxPriorityFee + // Set GasFeeCap and GasTipCap + t.gasSettings.ApplyTo(opts) // Print the gas info if !gas.PrintAndCheckGasInfo(submission.TxInfo.SimulationResult, true, t.gasThreshold, t.logger, opts.GasFeeCap, 0) { diff --git a/rocketpool-daemon/node/node.go b/rocketpool-daemon/node/node.go index fc69cdf2e..a1414d512 100644 --- a/rocketpool-daemon/node/node.go +++ b/rocketpool-daemon/node/node.go @@ -89,6 +89,11 @@ type TaskLoop struct { secondsDelta float64 } +type GasSettings struct { + maxFee *big.Int + maxPriorityFee *big.Int +} + func NewTaskLoop(sp *services.ServiceProvider, wg *sync.WaitGroup) *TaskLoop { logger := sp.GetTasksLogger() ctx := logger.CreateContextWithLogger(sp.GetBaseContext()) @@ -480,3 +485,26 @@ func calculateTotalEffectiveStakeForNetwork(state *state.NetworkState) *big.Int } return total } + +// Applies GasFeeCap and GasTipCap to opts and handles a case where the user-inputted maxPriorityFee is greater than the oracle based maxFee +// If so, maxPriorityFee is appplied to opts as the min(maxPriorityFee, 25% of the oracle based maxFee) +func (g *GasSettings) ApplyTo(opts *bind.TransactOpts) *bind.TransactOpts { + opts.GasFeeCap = g.maxFee + + // If maxPriorityFee < maxFee, apply maxPriorityFee to opts + if g.maxPriorityFee.Cmp(g.maxFee) < 0 { + opts.GasTipCap = g.maxPriorityFee + return opts + } + quarterMaxFee := new(big.Int).Div(g.maxFee, big.NewInt(4)) + + // Otherwise apply maxPriorityFee to opts as min(priorityFee, 25% of the oracle based maxFee) + if g.maxPriorityFee.Cmp(quarterMaxFee) < 0 { + opts.GasTipCap = g.maxPriorityFee + } else { + opts.GasTipCap = quarterMaxFee + } + + return opts + +}