forked from node101-io/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: implement staking decorator to prevent unregistered accounts to become validators #23
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
43ef639
feat: custom decorators implemented for mina sig verification
korayakpinar cc86301
test: added new tests and refactored the older tests for ante package
korayakpinar 812a5ea
fix: solve merge conflicts and update mina verifier tests with the ne…
yusufozmis 149f23d
feat: implement staking decorator to ensure validators are registered…
yusufozmis f5a7e50
chore: add staking decorator to ante decorators
yusufozmis b35c2fd
test: add tests for staking decorator
yusufozmis 3124791
fix: initialize keyregistry before the genutil module, to ensure vali…
yusufozmis ef0dd06
Merge remote-tracking branch 'origin/development' into origin/staking…
yusufozmis 33fea74
chore: remove duplicates
yusufozmis 60638fc
chore: remove duplicate ante handler register
yusufozmis de3d69c
chore: minor lint problem fix
yusufozmis 63a1b01
refactor: run the staking decorator before the increment sequence dec…
yusufozmis e0c7167
fix: use verify bytes in the mina verifier instead of verify strings
yusufozmis e4088c1
refactor: read network id from app.toml instead of defining a constant
yusufozmis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package ante | ||
|
|
||
| import ( | ||
| errorsmod "cosmossdk.io/errors" | ||
| "cosmossdk.io/log" | ||
| storetypes "cosmossdk.io/store/types" | ||
| txsigning "cosmossdk.io/x/tx/signing" | ||
|
|
||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
| sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
| signing "github.com/cosmos/cosmos-sdk/types/tx/signing" | ||
| authante "github.com/cosmos/cosmos-sdk/x/auth/ante" | ||
| authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
| keyregistrykeeper "github.com/node101-io/pulsar-chain/x/keyregistry/keeper" | ||
| ) | ||
|
|
||
| // HandlerOptions are the options required for constructing the app ante handler. | ||
| type HandlerOptions struct { | ||
| AccountKeeper authante.AccountKeeper | ||
| BankKeeper authtypes.BankKeeper | ||
| ExtensionOptionChecker authante.ExtensionOptionChecker | ||
| FeegrantKeeper authante.FeegrantKeeper | ||
| SignModeHandler *txsigning.HandlerMap | ||
| SigGasConsumer func(meter storetypes.GasMeter, sig signing.SignatureV2, params authtypes.Params) error | ||
| TxFeeChecker authante.TxFeeChecker | ||
| SigVerifyOptions []authante.SigVerificationDecoratorOption | ||
| KeyregistryKeeper *keyregistrykeeper.Keeper | ||
| MinaNetworkID string | ||
| Logger log.Logger | ||
| } | ||
|
|
||
| // NewAnteHandler returns the chain ante handler. | ||
| func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { | ||
| if options.AccountKeeper == nil { | ||
| return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "account keeper is required for ante builder") | ||
| } | ||
|
|
||
| if options.BankKeeper == nil { | ||
| return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "bank keeper is required for ante builder") | ||
| } | ||
|
|
||
| if options.SignModeHandler == nil { | ||
| return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder") | ||
| } | ||
|
|
||
| if options.KeyregistryKeeper == nil { | ||
| return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "keyregistry keeper is required for ante builder") | ||
| } | ||
|
|
||
| if options.MinaNetworkID == "" { | ||
| return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "mina network ID is required for ante builder") | ||
| } | ||
|
|
||
| if options.Logger == nil { | ||
| return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "logger is required for ante builder") | ||
| } | ||
|
|
||
| cosmosSetPubKey := authante.NewSetPubKeyDecorator(options.AccountKeeper) | ||
| cosmosValidateSigCount := authante.NewValidateSigCountDecorator(options.AccountKeeper) | ||
| cosmosSigGasConsume := authante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer) | ||
| cosmosSigVerify := authante.NewSigVerificationDecorator( | ||
| options.AccountKeeper, | ||
| options.SignModeHandler, | ||
| options.SigVerifyOptions..., | ||
| ) | ||
|
|
||
| minaVerifier := NewMinaVerifier( | ||
| options.KeyregistryKeeper, | ||
| options.AccountKeeper, | ||
| options.SignModeHandler, | ||
| options.MinaNetworkID, | ||
| options.Logger, | ||
| ) | ||
|
|
||
| anteDecorators := []sdk.AnteDecorator{ | ||
| authante.NewSetUpContextDecorator(), | ||
| authante.NewExtensionOptionsDecorator(NewTxAuthExtensionOptionChecker(options.ExtensionOptionChecker)), | ||
| authante.NewValidateBasicDecorator(), | ||
| authante.NewTxTimeoutHeightDecorator(), | ||
| authante.NewValidateMemoDecorator(options.AccountKeeper), | ||
| authante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper), | ||
| authante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker), | ||
| NewTxAuthModeDecorator(), | ||
| NewRoutedSetPubKeyDecorator(cosmosSetPubKey), | ||
| NewRoutedValidateSigCountDecorator(options.AccountKeeper, cosmosValidateSigCount), | ||
| NewRoutedSigGasConsumeDecorator(options.AccountKeeper, cosmosSigGasConsume), | ||
| NewRoutedSigVerificationDecorator(cosmosSigVerify, minaVerifier), | ||
| NewStakingDecorator(options.KeyregistryKeeper), | ||
| authante.NewIncrementSequenceDecorator(options.AccountKeeper), | ||
| } | ||
|
|
||
| return sdk.ChainAnteDecorators(anteDecorators...), nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| package ante_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "cosmossdk.io/core/address" | ||
| "cosmossdk.io/log" | ||
| txsigning "cosmossdk.io/x/tx/signing" | ||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
| authante "github.com/cosmos/cosmos-sdk/x/auth/ante" | ||
| authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| appante "github.com/node101-io/pulsar-chain/app/ante" | ||
| keyregistrykeeper "github.com/node101-io/pulsar-chain/x/keyregistry/keeper" | ||
| ) | ||
|
|
||
| type stubAccountKeeper struct{} | ||
|
|
||
| func (stubAccountKeeper) GetParams(context.Context) authtypes.Params { | ||
| return authtypes.Params{} | ||
| } | ||
|
|
||
| func (stubAccountKeeper) GetAccount(context.Context, sdk.AccAddress) sdk.AccountI { | ||
| return nil | ||
| } | ||
|
|
||
| func (stubAccountKeeper) SetAccount(context.Context, sdk.AccountI) {} | ||
|
|
||
| func (stubAccountKeeper) GetModuleAddress(string) sdk.AccAddress { | ||
| return nil | ||
| } | ||
|
|
||
| func (stubAccountKeeper) AddressCodec() address.Codec { | ||
| return nil | ||
| } | ||
|
|
||
| func (stubAccountKeeper) UnorderedTransactionsEnabled() bool { | ||
| return false | ||
| } | ||
|
|
||
| func (stubAccountKeeper) RemoveExpiredUnorderedNonces(sdk.Context) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (stubAccountKeeper) TryAddUnorderedNonce(sdk.Context, []byte, time.Time) error { | ||
| return nil | ||
| } | ||
|
|
||
| type stubBankKeeper struct{} | ||
|
|
||
| func (stubBankKeeper) IsSendEnabledCoins(context.Context, ...sdk.Coin) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (stubBankKeeper) SendCoins(context.Context, sdk.AccAddress, sdk.AccAddress, sdk.Coins) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (stubBankKeeper) SendCoinsFromAccountToModule(context.Context, sdk.AccAddress, string, sdk.Coins) error { | ||
| return nil | ||
| } | ||
|
|
||
| var ( | ||
| _ authante.AccountKeeper = stubAccountKeeper{} | ||
| _ authtypes.BankKeeper = stubBankKeeper{} | ||
| ) | ||
|
|
||
| var stubKeyregistryKeeper = &keyregistrykeeper.Keeper{} | ||
|
|
||
| // A fully populated HandlerOptions should construct a usable ante chain. | ||
| // This is the baseline success case that all of the stricter validation tests compare against. | ||
| func TestNewAnteHandler(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| anteHandler, err := appante.NewAnteHandler(appante.HandlerOptions{ | ||
| AccountKeeper: stubAccountKeeper{}, | ||
| BankKeeper: stubBankKeeper{}, | ||
| SignModeHandler: &txsigning.HandlerMap{}, | ||
| KeyregistryKeeper: stubKeyregistryKeeper, | ||
| MinaNetworkID: appante.DefaultMinaNetworkID, | ||
| Logger: log.NewNopLogger(), | ||
| }) | ||
|
|
||
| require.NoError(t, err) | ||
| require.NotNil(t, anteHandler) | ||
| } | ||
|
|
||
| // AccountKeeper is required because the auth ante stack depends on account state almost everywhere. | ||
| // Failing at construction time is safer than letting a partially wired handler reach runtime. | ||
| func TestNewAnteHandlerRequiresAccountKeeper(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| anteHandler, err := appante.NewAnteHandler(appante.HandlerOptions{ | ||
| BankKeeper: stubBankKeeper{}, | ||
| SignModeHandler: &txsigning.HandlerMap{}, | ||
| KeyregistryKeeper: stubKeyregistryKeeper, | ||
| MinaNetworkID: appante.DefaultMinaNetworkID, | ||
| Logger: log.NewNopLogger(), | ||
| }) | ||
|
|
||
| require.ErrorContains(t, err, "account keeper is required for ante builder") | ||
| require.Nil(t, anteHandler) | ||
| } | ||
|
|
||
| // BankKeeper is mandatory for fee deduction in the default auth decorators. | ||
| // This test ensures the constructor rejects missing fee-transfer dependencies immediately. | ||
| func TestNewAnteHandlerRequiresBankKeeper(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| anteHandler, err := appante.NewAnteHandler(appante.HandlerOptions{ | ||
| AccountKeeper: stubAccountKeeper{}, | ||
| SignModeHandler: &txsigning.HandlerMap{}, | ||
| KeyregistryKeeper: stubKeyregistryKeeper, | ||
| MinaNetworkID: appante.DefaultMinaNetworkID, | ||
| Logger: log.NewNopLogger(), | ||
| }) | ||
|
|
||
| require.ErrorContains(t, err, "bank keeper is required for ante builder") | ||
| require.Nil(t, anteHandler) | ||
| } | ||
|
|
||
| // SignModeHandler is needed by both the Cosmos and Mina signature verification paths. | ||
| // Missing it would make sign-byte generation impossible later in the ante chain. | ||
| func TestNewAnteHandlerRequiresSignModeHandler(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| anteHandler, err := appante.NewAnteHandler(appante.HandlerOptions{ | ||
| AccountKeeper: stubAccountKeeper{}, | ||
| BankKeeper: stubBankKeeper{}, | ||
| KeyregistryKeeper: stubKeyregistryKeeper, | ||
| MinaNetworkID: appante.DefaultMinaNetworkID, | ||
| Logger: log.NewNopLogger(), | ||
| }) | ||
|
|
||
| require.ErrorContains(t, err, "sign mode handler is required for ante builder") | ||
| require.Nil(t, anteHandler) | ||
| } | ||
|
|
||
| // KeyregistryKeeper is part of the custom verifier contract for Mina-authenticated txs. | ||
| // The constructor should refuse to build an ante handler that can never resolve Mina signers. | ||
| func TestNewAnteHandlerRequiresKeyregistryKeeper(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| anteHandler, err := appante.NewAnteHandler(appante.HandlerOptions{ | ||
| AccountKeeper: stubAccountKeeper{}, | ||
| BankKeeper: stubBankKeeper{}, | ||
| SignModeHandler: &txsigning.HandlerMap{}, | ||
| MinaNetworkID: appante.DefaultMinaNetworkID, | ||
| Logger: log.NewNopLogger(), | ||
| }) | ||
|
|
||
| require.ErrorContains(t, err, "keyregistry keeper is required for ante builder") | ||
| require.Nil(t, anteHandler) | ||
| } | ||
|
|
||
| // Mina network selection affects how Mina signatures are verified on-chain. | ||
| // This test guards against silently constructing a verifier with an empty network ID. | ||
| func TestNewAnteHandlerRequiresMinaNetworkID(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| anteHandler, err := appante.NewAnteHandler(appante.HandlerOptions{ | ||
| AccountKeeper: stubAccountKeeper{}, | ||
| BankKeeper: stubBankKeeper{}, | ||
| SignModeHandler: &txsigning.HandlerMap{}, | ||
| KeyregistryKeeper: stubKeyregistryKeeper, | ||
| Logger: log.NewNopLogger(), | ||
| }) | ||
|
|
||
| require.ErrorContains(t, err, "mina network ID is required for ante builder") | ||
| require.Nil(t, anteHandler) | ||
| } | ||
|
|
||
| // The custom verifier emits debug information when verification is skipped. | ||
| // Requiring a logger up front avoids nil logger surprises during ante execution. | ||
| func TestNewAnteHandlerRequiresLogger(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| anteHandler, err := appante.NewAnteHandler(appante.HandlerOptions{ | ||
| AccountKeeper: stubAccountKeeper{}, | ||
| BankKeeper: stubBankKeeper{}, | ||
| SignModeHandler: &txsigning.HandlerMap{}, | ||
| KeyregistryKeeper: stubKeyregistryKeeper, | ||
| MinaNetworkID: appante.DefaultMinaNetworkID, | ||
| }) | ||
|
|
||
| require.ErrorContains(t, err, "logger is required for ante builder") | ||
| require.Nil(t, anteHandler) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.