Skip to content

Commit

Permalink
Handle a case where maxPriorityFee > maxFee, consolidate fields
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaspanf committed Oct 1, 2024
1 parent dbc99e7 commit 3df9cc0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 33 deletions.
63 changes: 30 additions & 33 deletions rocketpool-daemon/node/auto-init-voting-power.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -22,36 +21,40 @@ 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
func NewAutoInitVotingPower(ctx context.Context, sp *services.ServiceProvider, logger *log.Logger) *AutoInitVotingPower {
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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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) {
Expand Down
28 changes: 28 additions & 0 deletions rocketpool-daemon/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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

}

0 comments on commit 3df9cc0

Please sign in to comment.