Skip to content

Commit

Permalink
fix tx validation vs execution
Browse files Browse the repository at this point in the history
  • Loading branch information
testinginprod committed Oct 17, 2024
1 parent 553e110 commit 9dc5acf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
11 changes: 10 additions & 1 deletion x/accounts/defaults/base/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"

"cosmossdk.io/core/transaction"
gogotypes "github.com/cosmos/gogoproto/types/any"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
Expand Down Expand Up @@ -43,6 +44,7 @@ func NewAccount(name string, handlerMap *signing.HandlerMap, options ...Option)
Sequence: collections.NewSequence(deps.SchemaBuilder, SequencePrefix, "sequence"),
addrCodec: deps.AddressCodec,
hs: deps.Environment.HeaderService,
ts: deps.Environment.TransactionService,
supportedPubKeys: map[string]pubKeyImpl{},
signingHandlers: handlerMap,
}
Expand All @@ -65,6 +67,7 @@ type Account struct {

addrCodec address.Codec
hs header.Service
ts transaction.Service

supportedPubKeys map[string]pubKeyImpl

Expand Down Expand Up @@ -106,7 +109,13 @@ func (a Account) Authenticate(ctx context.Context, msg *aa_interface_v1.MsgAuthe
}

gotSeq := msg.Tx.AuthInfo.SignerInfos[msg.SignerIndex].Sequence
if gotSeq != signerData.Sequence {

execMode := a.ts.ExecMode(ctx)
if execMode == transaction.ExecModeCheck {
if gotSeq < signerData.Sequence {
return nil, fmt.Errorf("sequence number must be higher than: %d, got: %d", signerData.Sequence, gotSeq)
}
} else if gotSeq != signerData.Sequence {
return nil, fmt.Errorf("unexpected sequence number, wanted: %d, got: %d", signerData.Sequence, gotSeq)
}

Expand Down
14 changes: 11 additions & 3 deletions x/auth/ante/sigverify.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,17 +306,25 @@ func (svd SigVerificationDecorator) consumeSignatureGas(

// verifySig will verify the signature of the provided signer account.
func (svd SigVerificationDecorator) verifySig(ctx context.Context, tx sdk.Tx, acc sdk.AccountI, sig signing.SignatureV2, newlyCreated bool) error {
if sig.Sequence != acc.GetSequence() {
execMode := svd.ak.GetEnvironment().TransactionService.ExecMode(ctx)
if execMode == transaction.ExecModeCheck {
if sig.Sequence < acc.GetSequence() {
return errorsmod.Wrapf(
sdkerrors.ErrWrongSequence,
"account sequence mismatch, expected higher than or equal to %d, got %d", acc.GetSequence(), sig.Sequence,
)
}
} else if sig.Sequence != acc.GetSequence() {
return errorsmod.Wrapf(
sdkerrors.ErrWrongSequence,
"account sequence mismatch, expected %d, got %d", acc.GetSequence(), sig.Sequence,
"account sequence mismatch: expected %d, got %d", acc.GetSequence(), sig.Sequence,
)
}

// we're in simulation mode, or in ReCheckTx, or context is not
// on sig verify tx, then we do not need to verify the signatures
// in the tx.
if svd.ak.GetEnvironment().TransactionService.ExecMode(ctx) == transaction.ExecModeSimulate ||
if execMode == transaction.ExecModeSimulate ||
isRecheckTx(ctx, svd.ak.GetEnvironment().TransactionService) ||
!isSigverifyTx(ctx) {
return nil
Expand Down

0 comments on commit 9dc5acf

Please sign in to comment.