Skip to content

Commit

Permalink
Merge PR cosmos#4760: update x/auth to match module spec
Browse files Browse the repository at this point in the history
  • Loading branch information
fedekunze authored and alexanderbez committed Jul 29, 2019
1 parent 24b9e84 commit fb3e0af
Show file tree
Hide file tree
Showing 21 changed files with 603 additions and 554 deletions.
1 change: 1 addition & 0 deletions .pending/improvements/modules/_4760-update-auth
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4760 update `x/auth` to match module spec
1 change: 1 addition & 0 deletions client/cmd_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// nolint: misspell
package client_test

import (
Expand Down
105 changes: 63 additions & 42 deletions x/auth/alias.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// nolint
// autogenerated code using github.com/rigelrozanski/multitool
// aliases generated for the following subdirectories:
// ALIASGEN: github.com/cosmos/cosmos-sdk/x/auth/ante
// ALIASGEN: github.com/cosmos/cosmos-sdk/x/auth/keeper
// ALIASGEN: github.com/cosmos/cosmos-sdk/x/auth/types
package auth

import (
"github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/auth/exported"
"github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)

Expand All @@ -25,33 +29,46 @@ const (

var (
// functions aliases
NewBaseAccount = types.NewBaseAccount
ProtoBaseAccount = types.ProtoBaseAccount
NewBaseAccountWithAddress = types.NewBaseAccountWithAddress
NewBaseVestingAccount = types.NewBaseVestingAccount
NewContinuousVestingAccountRaw = types.NewContinuousVestingAccountRaw
NewContinuousVestingAccount = types.NewContinuousVestingAccount
NewDelayedVestingAccountRaw = types.NewDelayedVestingAccountRaw
NewDelayedVestingAccount = types.NewDelayedVestingAccount
RegisterCodec = types.RegisterCodec
NewGenesisState = types.NewGenesisState
DefaultGenesisState = types.DefaultGenesisState
ValidateGenesis = types.ValidateGenesis
AddressStoreKey = types.AddressStoreKey
NewParams = types.NewParams
ParamKeyTable = types.ParamKeyTable
DefaultParams = types.DefaultParams
NewQueryAccountParams = types.NewQueryAccountParams
NewStdTx = types.NewStdTx
CountSubKeys = types.CountSubKeys
NewStdFee = types.NewStdFee
StdSignBytes = types.StdSignBytes
DefaultTxDecoder = types.DefaultTxDecoder
DefaultTxEncoder = types.DefaultTxEncoder
NewTxBuilder = types.NewTxBuilder
NewTxBuilderFromCLI = types.NewTxBuilderFromCLI
MakeSignature = types.MakeSignature
NewAccountRetriever = types.NewAccountRetriever
NewAnteHandler = ante.NewAnteHandler
GetSignerAcc = ante.GetSignerAcc
ValidateSigCount = ante.ValidateSigCount
ValidateMemo = ante.ValidateMemo
ProcessPubKey = ante.ProcessPubKey
DefaultSigVerificationGasConsumer = ante.DefaultSigVerificationGasConsumer
DeductFees = ante.DeductFees
EnsureSufficientMempoolFees = ante.EnsureSufficientMempoolFees
SetGasMeter = ante.SetGasMeter
GetSignBytes = ante.GetSignBytes
NewAccountKeeper = keeper.NewAccountKeeper
NewDummySupplyKeeper = keeper.NewDummySupplyKeeper
NewQuerier = keeper.NewQuerier
NewBaseAccount = types.NewBaseAccount
ProtoBaseAccount = types.ProtoBaseAccount
NewBaseAccountWithAddress = types.NewBaseAccountWithAddress
NewBaseVestingAccount = types.NewBaseVestingAccount
NewContinuousVestingAccountRaw = types.NewContinuousVestingAccountRaw
NewContinuousVestingAccount = types.NewContinuousVestingAccount
NewDelayedVestingAccountRaw = types.NewDelayedVestingAccountRaw
NewDelayedVestingAccount = types.NewDelayedVestingAccount
NewAccountRetriever = types.NewAccountRetriever
RegisterCodec = types.RegisterCodec
NewGenesisState = types.NewGenesisState
DefaultGenesisState = types.DefaultGenesisState
ValidateGenesis = types.ValidateGenesis
AddressStoreKey = types.AddressStoreKey
NewParams = types.NewParams
ParamKeyTable = types.ParamKeyTable
DefaultParams = types.DefaultParams
NewQueryAccountParams = types.NewQueryAccountParams
NewStdTx = types.NewStdTx
CountSubKeys = types.CountSubKeys
NewStdFee = types.NewStdFee
StdSignBytes = types.StdSignBytes
DefaultTxDecoder = types.DefaultTxDecoder
DefaultTxEncoder = types.DefaultTxEncoder
NewTxBuilder = types.NewTxBuilder
NewTxBuilderFromCLI = types.NewTxBuilderFromCLI
MakeSignature = types.MakeSignature

// variable aliases
ModuleCdc = types.ModuleCdc
Expand All @@ -65,19 +82,23 @@ var (
)

type (
Account = exported.Account
VestingAccount = exported.VestingAccount
BaseAccount = types.BaseAccount
BaseVestingAccount = types.BaseVestingAccount
ContinuousVestingAccount = types.ContinuousVestingAccount
DelayedVestingAccount = types.DelayedVestingAccount
GenesisState = types.GenesisState
Params = types.Params
QueryAccountParams = types.QueryAccountParams
StdSignMsg = types.StdSignMsg
StdTx = types.StdTx
StdFee = types.StdFee
StdSignDoc = types.StdSignDoc
StdSignature = types.StdSignature
TxBuilder = types.TxBuilder
SignatureVerificationGasConsumer = ante.SignatureVerificationGasConsumer
Account = exported.Account
VestingAccount = exported.VestingAccount
AccountKeeper = keeper.AccountKeeper
BaseAccount = types.BaseAccount
BaseVestingAccount = types.BaseVestingAccount
ContinuousVestingAccount = types.ContinuousVestingAccount
DelayedVestingAccount = types.DelayedVestingAccount
NodeQuerier = types.NodeQuerier
AccountRetriever = types.AccountRetriever
GenesisState = types.GenesisState
Params = types.Params
QueryAccountParams = types.QueryAccountParams
StdSignMsg = types.StdSignMsg
StdTx = types.StdTx
StdFee = types.StdFee
StdSignDoc = types.StdSignDoc
StdSignature = types.StdSignature
TxBuilder = types.TxBuilder
)
47 changes: 24 additions & 23 deletions x/auth/ante.go → x/auth/ante/ante.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package auth
package ante

import (
"bytes"
Expand All @@ -13,6 +13,8 @@ import (

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/exported"
"github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)

Expand All @@ -30,12 +32,12 @@ func init() {

// SignatureVerificationGasConsumer is the type of function that is used to both consume gas when verifying signatures
// and also to accept or reject different types of PubKey's. This is where apps can define their own PubKey
type SignatureVerificationGasConsumer = func(meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params Params) sdk.Result
type SignatureVerificationGasConsumer = func(meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params types.Params) sdk.Result

// NewAnteHandler returns an AnteHandler that checks and increments sequence
// numbers, checks signatures & account numbers, and deducts fees from the first
// signer.
func NewAnteHandler(ak AccountKeeper, supplyKeeper types.SupplyKeeper, sigGasConsumer SignatureVerificationGasConsumer) sdk.AnteHandler {
func NewAnteHandler(ak keeper.AccountKeeper, supplyKeeper types.SupplyKeeper, sigGasConsumer SignatureVerificationGasConsumer) sdk.AnteHandler {
return func(
ctx sdk.Context, tx sdk.Tx, simulate bool,
) (newCtx sdk.Context, res sdk.Result, abort bool) {
Expand All @@ -45,7 +47,7 @@ func NewAnteHandler(ak AccountKeeper, supplyKeeper types.SupplyKeeper, sigGasCon
}

// all transactions must be of type auth.StdTx
stdTx, ok := tx.(StdTx)
stdTx, ok := tx.(types.StdTx)
if !ok {
// Set a gas meter with limit 0 as to prevent an infinite gas meter attack
// during runTx.
Expand Down Expand Up @@ -107,7 +109,7 @@ func NewAnteHandler(ak AccountKeeper, supplyKeeper types.SupplyKeeper, sigGasCon
// stdSigs contains the sequence number, account number, and signatures.
// When simulating, this would just be a 0-length slice.
signerAddrs := stdTx.GetSigners()
signerAccs := make([]Account, len(signerAddrs))
signerAccs := make([]exported.Account, len(signerAddrs))
isGenesis := ctx.BlockHeight() == 0

// fetch first signer, who's going to pay the fees
Expand Down Expand Up @@ -150,14 +152,13 @@ func NewAnteHandler(ak AccountKeeper, supplyKeeper types.SupplyKeeper, sigGasCon
ak.SetAccount(newCtx, signerAccs[i])
}

// TODO: tx tags (?)
return newCtx, sdk.Result{GasWanted: stdTx.Fee.Gas}, false // continue...
}
}

// GetSignerAcc returns an account for a given address that is expected to sign
// a transaction.
func GetSignerAcc(ctx sdk.Context, ak AccountKeeper, addr sdk.AccAddress) (Account, sdk.Result) {
func GetSignerAcc(ctx sdk.Context, ak keeper.AccountKeeper, addr sdk.AccAddress) (exported.Account, sdk.Result) {
if acc := ak.GetAccount(ctx, addr); acc != nil {
return acc, sdk.Result{}
}
Expand All @@ -166,12 +167,12 @@ func GetSignerAcc(ctx sdk.Context, ak AccountKeeper, addr sdk.AccAddress) (Accou

// ValidateSigCount validates that the transaction has a valid cumulative total
// amount of signatures.
func ValidateSigCount(stdTx StdTx, params Params) sdk.Result {
func ValidateSigCount(stdTx types.StdTx, params types.Params) sdk.Result {
stdSigs := stdTx.GetSignatures()

sigCount := 0
for i := 0; i < len(stdSigs); i++ {
sigCount += CountSubKeys(stdSigs[i].PubKey)
sigCount += types.CountSubKeys(stdSigs[i].PubKey)
if uint64(sigCount) > params.TxSigLimit {
return sdk.ErrTooManySignatures(
fmt.Sprintf("signatures: %d, limit: %d", sigCount, params.TxSigLimit),
Expand All @@ -183,7 +184,7 @@ func ValidateSigCount(stdTx StdTx, params Params) sdk.Result {
}

// ValidateMemo validates the memo size.
func ValidateMemo(stdTx StdTx, params Params) sdk.Result {
func ValidateMemo(stdTx types.StdTx, params types.Params) sdk.Result {
memoLength := len(stdTx.GetMemo())
if uint64(memoLength) > params.MaxMemoCharacters {
return sdk.ErrMemoTooLarge(
Expand All @@ -200,9 +201,9 @@ func ValidateMemo(stdTx StdTx, params Params) sdk.Result {
// verify the signature and increment the sequence. If the account doesn't have
// a pubkey, set it.
func processSig(
ctx sdk.Context, acc Account, sig StdSignature, signBytes []byte, simulate bool, params Params,
ctx sdk.Context, acc exported.Account, sig types.StdSignature, signBytes []byte, simulate bool, params types.Params,
sigGasConsumer SignatureVerificationGasConsumer,
) (updatedAcc Account, res sdk.Result) {
) (updatedAcc exported.Account, res sdk.Result) {

pubKey, res := ProcessPubKey(acc, sig, simulate)
if !res.IsOK() {
Expand Down Expand Up @@ -237,13 +238,13 @@ func processSig(
return acc, res
}

func consumeSimSigGas(gasmeter sdk.GasMeter, pubkey crypto.PubKey, sig StdSignature, params Params) {
simSig := StdSignature{PubKey: pubkey}
func consumeSimSigGas(gasmeter sdk.GasMeter, pubkey crypto.PubKey, sig types.StdSignature, params types.Params) {
simSig := types.StdSignature{PubKey: pubkey}
if len(sig.Signature) == 0 {
simSig.Signature = simSecp256k1Sig[:]
}

sigBz := ModuleCdc.MustMarshalBinaryLengthPrefixed(simSig)
sigBz := types.ModuleCdc.MustMarshalBinaryLengthPrefixed(simSig)
cost := sdk.Gas(len(sigBz) + 6)

// If the pubkey is a multi-signature pubkey, then we estimate for the maximum
Expand All @@ -258,8 +259,8 @@ func consumeSimSigGas(gasmeter sdk.GasMeter, pubkey crypto.PubKey, sig StdSignat
// ProcessPubKey verifies that the given account address matches that of the
// StdSignature. In addition, it will set the public key of the account if it
// has not been set.
func ProcessPubKey(acc Account, sig StdSignature, simulate bool) (crypto.PubKey, sdk.Result) {
// If pubkey is not known for account, set it from the StdSignature.
func ProcessPubKey(acc exported.Account, sig types.StdSignature, simulate bool) (crypto.PubKey, sdk.Result) {
// If pubkey is not known for account, set it from the types.StdSignature.
pubKey := acc.GetPubKey()
if simulate {
// In simulate mode the transaction comes with no signatures, thus if the
Expand Down Expand Up @@ -292,7 +293,7 @@ func ProcessPubKey(acc Account, sig StdSignature, simulate bool) (crypto.PubKey,
// for signature verification based upon the public key type. The cost is fetched from the given params and is matched
// by the concrete type.
func DefaultSigVerificationGasConsumer(
meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params Params,
meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params types.Params,
) sdk.Result {
switch pubkey := pubkey.(type) {
case ed25519.PubKeyEd25519:
Expand All @@ -317,7 +318,7 @@ func DefaultSigVerificationGasConsumer(

func consumeMultisignatureVerificationGas(meter sdk.GasMeter,
sig multisig.Multisignature, pubkey multisig.PubKeyMultisigThreshold,
params Params) {
params types.Params) {

size := sig.BitArray.Size()
sigIndex := 0
Expand All @@ -333,7 +334,7 @@ func consumeMultisignatureVerificationGas(meter sdk.GasMeter,
//
// NOTE: We could use the CoinKeeper (in addition to the AccountKeeper, because
// the CoinKeeper doesn't give us accounts), but it seems easier to do this.
func DeductFees(supplyKeeper types.SupplyKeeper, ctx sdk.Context, acc Account, fees sdk.Coins) sdk.Result {
func DeductFees(supplyKeeper types.SupplyKeeper, ctx sdk.Context, acc exported.Account, fees sdk.Coins) sdk.Result {
blockTime := ctx.BlockHeader().Time
coins := acc.GetCoins()

Expand Down Expand Up @@ -372,7 +373,7 @@ func DeductFees(supplyKeeper types.SupplyKeeper, ctx sdk.Context, acc Account, f
//
// Contract: This should only be called during CheckTx as it cannot be part of
// consensus.
func EnsureSufficientMempoolFees(ctx sdk.Context, stdFee StdFee) sdk.Result {
func EnsureSufficientMempoolFees(ctx sdk.Context, stdFee types.StdFee) sdk.Result {
minGasPrices := ctx.MinGasPrices()
if !minGasPrices.IsZero() {
requiredFees := make(sdk.Coins, len(minGasPrices))
Expand Down Expand Up @@ -410,13 +411,13 @@ func SetGasMeter(simulate bool, ctx sdk.Context, gasLimit uint64) sdk.Context {

// GetSignBytes returns a slice of bytes to sign over for a given transaction
// and an account.
func GetSignBytes(chainID string, stdTx StdTx, acc Account, genesis bool) []byte {
func GetSignBytes(chainID string, stdTx types.StdTx, acc exported.Account, genesis bool) []byte {
var accNum uint64
if !genesis {
accNum = acc.GetAccountNumber()
}

return StdSignBytes(
return types.StdSignBytes(
chainID, accNum, acc.GetSequence(), stdTx.Fee, stdTx.Msgs, stdTx.Memo,
)
}
Loading

0 comments on commit fb3e0af

Please sign in to comment.