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

Update BoLD Submodule and Add Newly Supported Flags to BoLD Staker #2835

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
88 changes: 78 additions & 10 deletions staker/bold/bold_staker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"math/big"
"strings"
"time"

flag "github.com/spf13/pflag"
Expand Down Expand Up @@ -57,16 +58,21 @@ type BoldConfig struct {
// How often to scan for newly created assertions onchain.
AssertionScanningInterval time.Duration `koanf:"assertion-scanning-interval"`
// How often to confirm assertions onchain.
AssertionConfirmingInterval time.Duration `koanf:"assertion-confirming-interval"`
API bool `koanf:"api"`
APIHost string `koanf:"api-host"`
APIPort uint16 `koanf:"api-port"`
APIDBPath string `koanf:"api-db-path"`
TrackChallengeParentAssertionHashes []string `koanf:"track-challenge-parent-assertion-hashes"`
CheckStakerSwitchInterval time.Duration `koanf:"check-staker-switch-interval"`
StateProviderConfig StateProviderConfig `koanf:"state-provider-config"`
StartValidationFromStaked bool `koanf:"start-validation-from-staked"`
AssertionConfirmingInterval time.Duration `koanf:"assertion-confirming-interval"`
API bool `koanf:"api"`
APIHost string `koanf:"api-host"`
APIPort uint16 `koanf:"api-port"`
APIDBPath string `koanf:"api-db-path"`
TrackChallengeParentAssertionHashes []string `koanf:"track-challenge-parent-assertion-hashes"`
CheckStakerSwitchInterval time.Duration `koanf:"check-staker-switch-interval"`
StateProviderConfig StateProviderConfig `koanf:"state-provider-config"`
StartValidationFromStaked bool `koanf:"start-validation-from-staked"`
AutoDeposit bool `koanf:"auto-deposit"`
AutoIncreaseAllowance bool `koanf:"auto-increase-allowance"`
DelegatedStaking DelegatedStakingConfig `koanf:"delegated-staking"`
RPCBlockNumber string `koanf:"rpc-block-number"`
strategy legacystaker.StakerStrategy
blockNum rpc.BlockNumber
}

func (c *BoldConfig) Validate() error {
Expand All @@ -75,9 +81,31 @@ func (c *BoldConfig) Validate() error {
return err
}
c.strategy = strategy
var blockNum rpc.BlockNumber
switch strings.ToLower(c.RPCBlockNumber) {
case "safe":
blockNum = rpc.SafeBlockNumber
case "finalized":
blockNum = rpc.FinalizedBlockNumber
case "latest":
blockNum = rpc.LatestBlockNumber
default:
return fmt.Errorf("unknown rpc block number \"%v\", expected either latest, safe, or finalized", c.RPCBlockNumber)
}
c.blockNum = blockNum
return nil
}

type DelegatedStakingConfig struct {
Enable bool `koanf:"enable"`
CustomWithdrawalAddress string `koanf:"custom-withdrawal-address"`
}

var DefaultDelegatedStakingConfig = DelegatedStakingConfig{
Enable: false,
CustomWithdrawalAddress: "",
}

type StateProviderConfig struct {
// A name identifier for the validator for cosmetic purposes.
ValidatorName string `koanf:"validator-name"`
Expand Down Expand Up @@ -106,6 +134,10 @@ var DefaultBoldConfig = BoldConfig{
CheckStakerSwitchInterval: time.Minute, // Every minute, check if the Nitro node staker should switch to using BOLD.
StateProviderConfig: DefaultStateProviderConfig,
StartValidationFromStaked: true,
AutoDeposit: true,
AutoIncreaseAllowance: true,
DelegatedStaking: DefaultDelegatedStakingConfig,
RPCBlockNumber: "finalized",
}

var BoldModes = map[legacystaker.StakerStrategy]boldtypes.Mode{
Expand All @@ -118,6 +150,7 @@ var BoldModes = map[legacystaker.StakerStrategy]boldtypes.Mode{
func BoldConfigAddOptions(prefix string, f *flag.FlagSet) {
f.Bool(prefix+".enable", DefaultBoldConfig.Enable, "enable bold challenge protocol")
f.String(prefix+".strategy", DefaultBoldConfig.Strategy, "define the bold validator staker strategy, either watchtower, defensive, stakeLatest, or makeNodes")
f.String(prefix+".rpc-block-number", DefaultBoldConfig.RPCBlockNumber, "define the block number to use for reading data onchain, either latest, safe, or finalized")
f.Duration(prefix+".assertion-posting-interval", DefaultBoldConfig.AssertionPostingInterval, "assertion posting interval")
f.Duration(prefix+".assertion-scanning-interval", DefaultBoldConfig.AssertionScanningInterval, "scan assertion interval")
f.Duration(prefix+".assertion-confirming-interval", DefaultBoldConfig.AssertionConfirmingInterval, "confirm assertion interval")
Expand All @@ -129,6 +162,9 @@ func BoldConfigAddOptions(prefix string, f *flag.FlagSet) {
f.StringSlice(prefix+".track-challenge-parent-assertion-hashes", DefaultBoldConfig.TrackChallengeParentAssertionHashes, "only track challenges/edges with these parent assertion hashes")
StateProviderConfigAddOptions(prefix+".state-provider-config", f)
f.Bool(prefix+".start-validation-from-staked", DefaultBoldConfig.StartValidationFromStaked, "assume staked nodes are valid")
f.Bool(prefix+".auto-deposit", DefaultBoldConfig.AutoDeposit, "auto-deposit stake token whenever making a move in BoLD that does not have enough stake token balance")
f.Bool(prefix+".auto-increase-allowance", DefaultBoldConfig.AutoIncreaseAllowance, "auto-increase spending allowance of the stake token by the rollup and challenge manager contracts")
DelegatedStakingConfigAddOptions(prefix+".delegated-staking", f)
}

func StateProviderConfigAddOptions(prefix string, f *flag.FlagSet) {
Expand All @@ -137,6 +173,11 @@ func StateProviderConfigAddOptions(prefix string, f *flag.FlagSet) {
f.String(prefix+".machine-leaves-cache-path", DefaultStateProviderConfig.MachineLeavesCachePath, "path to machine cache")
}

func DelegatedStakingConfigAddOptions(prefix string, f *flag.FlagSet) {
f.Bool(prefix+".enable", DefaultDelegatedStakingConfig.Enable, "enable delegated staking by having the validator call newStake on startup")
f.String(prefix+".custom-withdrawal-address", DefaultDelegatedStakingConfig.CustomWithdrawalAddress, "enable a custom withdrawal address for staking on the rollup contract, useful for delegated stakers")
}

type BOLDStaker struct {
stopwaiter.StopWaiter
config *BoldConfig
Expand Down Expand Up @@ -365,7 +406,25 @@ func newBOLDChallengeManager(
if err != nil {
return nil, fmt.Errorf("could not create challenge manager bindings: %w", err)
}
assertionChain, err := solimpl.NewAssertionChain(ctx, rollupAddress, chalManager, txOpts, client, NewDataPosterTransactor(dataPoster))
assertionChainOpts := []solimpl.Opt{
solimpl.WithRpcHeadBlockNumber(config.blockNum),
}
if config.DelegatedStaking.Enable && config.DelegatedStaking.CustomWithdrawalAddress != "" {
withdrawalAddr := common.HexToAddress(config.DelegatedStaking.CustomWithdrawalAddress)
assertionChainOpts = append(assertionChainOpts, solimpl.WithCustomWithdrawalAddress(withdrawalAddr))
}
if !config.AutoDeposit {
assertionChainOpts = append(assertionChainOpts, solimpl.WithoutAutoDeposit())
}
assertionChain, err := solimpl.NewAssertionChain(
ctx,
rollupAddress,
chalManager,
txOpts,
client,
NewDataPosterTransactor(dataPoster),
assertionChainOpts...,
)
if err != nil {
return nil, fmt.Errorf("could not create assertion chain: %w", err)
}
Expand Down Expand Up @@ -455,6 +514,15 @@ func newBOLDChallengeManager(
apiAddr := fmt.Sprintf("%s:%d", config.APIHost, config.APIPort)
stackOpts = append(stackOpts, challengemanager.StackWithAPIEnabled(apiAddr, apiDBPath))
}
if !config.AutoDeposit {
stackOpts = append(stackOpts, challengemanager.StackWithoutAutoDeposit())
}
if !config.AutoIncreaseAllowance {
stackOpts = append(stackOpts, challengemanager.StackWithoutAutoAllowanceApproval())
Copy link
Collaborator

@PlasmaPower PlasmaPower Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this code also for the assertion chain? Does the assertion chain not do auto allowance? Same thing for the delegated staking, maybe I'm just not sure what's the responsibility of the challenge manager vs assertion chain.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The challenge manager spins up something called an assertion manager, and it is the assertion manager that does the auto allowance approval, auto deposit, and delegated staking on startup. The assertion chain is an abstraction over the Go bindings to Solidity which also do auto-deposit each time it makes a move related to the stake token. Perhaps we should unify all those responsibilities under the assertion chain?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for now, makes sense and LGTM aside from the presumably accidental testnode change

}
if config.DelegatedStaking.Enable {
stackOpts = append(stackOpts, challengemanager.StackWithDelegatedStaking())
}

manager, err := challengemanager.NewChallengeStack(
assertionChain,
Expand Down
Loading