diff --git a/go/upgrade/migrations/consensus_max_allowances.go b/go/upgrade/migrations/consensus_max_allowances.go new file mode 100644 index 00000000000..56981ed112e --- /dev/null +++ b/go/upgrade/migrations/consensus_max_allowances.go @@ -0,0 +1,54 @@ +package migrations + +import ( + "fmt" + + abciAPI "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api" + stakingState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/staking/state" +) + +const ( + // ConsensusMaxAllowancesHandler is the name of the upgrade that sets the + // staking max allowances consensus parameter to 16. + ConsensusMaxAllowancesHandler = "consensus-staking-max-allowances-16" +) + +var ( + _ Handler = (*maxAllowancesHandler)(nil) +) + +type maxAllowancesHandler struct { +} + +func (th *maxAllowancesHandler) StartupUpgrade(ctx *Context) error { + return nil +} + +func (th *maxAllowancesHandler) ConsensusUpgrade(ctx *Context, privateCtx interface{}) error { + abciCtx := privateCtx.(*abciAPI.Context) + switch abciCtx.Mode() { + case abciAPI.ContextBeginBlock: + // Nothing to do during begin block. + case abciAPI.ContextEndBlock: + // Update a consensus parameter during EndBlock. + state := stakingState.NewMutableState(abciCtx.State()) + + params, err := state.ConsensusParameters(abciCtx) + if err != nil { + return fmt.Errorf("unable to load staking consensus parameters: %w", err) + } + + params.MaxAllowances = 16 + + if err = state.SetConsensusParameters(abciCtx, params); err != nil { + return fmt.Errorf("failed to update staking consensus parameters: %w", err) + } + default: + return fmt.Errorf("upgrade handler called in unexpected context: %s", abciCtx.Mode()) + } + return nil +} + +func init() { + Register(ConsensusMaxAllowancesHandler, &maxAllowancesHandler{}) +}