Skip to content

Commit

Permalink
[CORE-538] Update to Comsos 0.50.1
Browse files Browse the repository at this point in the history
  • Loading branch information
lcwik committed Dec 15, 2023
1 parent eba7f81 commit 657e07a
Show file tree
Hide file tree
Showing 450 changed files with 4,056 additions and 4,133 deletions.
2 changes: 2 additions & 0 deletions proto/dydxprotocol/sending/transfer.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ message MsgDepositToSubaccount {
// MsgWithdrawFromSubaccount represents a single transfer from an
// `x/subaccounts` subaccount to an `x/bank` account.
message MsgWithdrawFromSubaccount {
option (cosmos.msg.v1.signer) = "sender";

// The sender subaccount ID.
dydxprotocol.subaccounts.SubaccountId sender = 2
[ (gogoproto.nullable) = false ];
Expand Down
4 changes: 2 additions & 2 deletions protocol/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ update-swagger-docs: statik
.PHONY: update-swagger-docs

###############################################################################
### Sample Prengenesis ###
### Sample Pregenesis ###
###############################################################################

# Run at `./protocol` directory.
Expand All @@ -421,7 +421,7 @@ update-sample-pregenesis:

check-sample-pregenesis-up-to-date:
$(MAKE) localnet-build
@docker build . -t check-sample-pregenesis -f scripts/genesis/Dockerfile --no-cache
@docker build . -t check-sample-pregenesis -f scripts/genesis/Dockerfile --no-cache
@docker run -v $(CURDIR):/workspace check-sample-pregenesis

.PHONY: update-sample-pregenesis check-sample-pregenesis-up-to-date
6 changes: 3 additions & 3 deletions protocol/app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
}

anteDecorators := newAnteDecoratorChain(options)
anteDecorators := NewAnteDecoratorChain(options)

// TODO(STAB-24): This change can be reverted to using ChainAnteDecorators again once
// https://github.com/cosmos/cosmos-sdk/pull/16076 is merged, released, and we pick-up the SDK version containing
Expand All @@ -65,8 +65,8 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
return anteHandlers[0], nil
}

// newAnteDecoratorChain returns a list of AnteDecorators in the expected application chain ordering
func newAnteDecoratorChain(options HandlerOptions) []sdk.AnteDecorator {
// NewAnteDecoratorChain returns a list of AnteDecorators in the expected application chain ordering
func NewAnteDecoratorChain(options HandlerOptions) []sdk.AnteDecorator {
return []sdk.AnteDecorator{
// Note: app-injected messages, and clob transactions don't require Gas fees.
libante.NewAppInjectedMsgAnteWrapper(
Expand Down
23 changes: 17 additions & 6 deletions protocol/app/ante/basic_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ante_test

import (
"github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
"testing"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
Expand All @@ -19,6 +21,8 @@ import (
// Forked from:
// https://github.com/cosmos/cosmos-sdk/blob/1e8e923d3174cdfdb42454a96c27251ad72b6504/x/auth/ante/basic.go#L18
func TestValidateBasic_AppInjectedMsgWrapper(t *testing.T) {
// The Ante suite and test setup from Cosmos SDK has "cosmos" hardcoded in it as the address codec which
// is why we are required to use it below.
tests := map[string]struct {
msgOne sdk.Msg
msgTwo sdk.Msg
Expand All @@ -39,28 +43,28 @@ func TestValidateBasic_AppInjectedMsgWrapper(t *testing.T) {
expectedErr: nil,
},
"valid ValidateBasic: single msg, NO AppInjected msg": {
msgOne: &testdata.TestMsg{Signers: []string{"meh"}},
msgOne: &testdata.TestMsg{Signers: []string{constants.AliceAccAddress.String()}},
txHasSignature: true, // this should allow ValidateBasic to pass.

expectedErr: nil,
},
"fails ValidateBasic: mult msgs, AppInjected msg": {
msgOne: &pricestypes.MsgUpdateMarketPrices{}, // AppInjected.
msgTwo: &testdata.TestMsg{Signers: []string{"meh"}},
msgTwo: &testdata.TestMsg{Signers: []string{constants.AliceAccAddress.String()}},
txHasSignature: true,

expectedErr: nil,
},
"valid: mult msgs, NO AppInjected msg": {
msgOne: &testdata.TestMsg{Signers: []string{"meh"}},
msgTwo: &testdata.TestMsg{Signers: []string{"meh"}},
msgOne: &testdata.TestMsg{Signers: []string{constants.AliceAccAddress.String()}},
msgTwo: &testdata.TestMsg{Signers: []string{constants.AliceAccAddress.String()}},
txHasSignature: true, // this should allow ValidateBasic to pass.

expectedErr: nil,
},
"skip ValidateBasic: recheck": {
msgOne: &pricestypes.MsgUpdateMarketPrices{}, // AppInjected.
msgTwo: &testdata.TestMsg{Signers: []string{"meh"}},
msgTwo: &testdata.TestMsg{Signers: []string{constants.AliceAccAddress.String()}},
isRecheck: true,
txHasSignature: false, // this should cause ValidateBasic to fail, but this is skipped.

Expand Down Expand Up @@ -98,7 +102,14 @@ func TestValidateBasic_AppInjectedMsgWrapper(t *testing.T) {
privs, accNums, accSeqs = []cryptotypes.PrivKey{}, []uint64{}, []uint64{}
}

tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.Ctx.ChainID())
tx, err := suite.CreateTestTx(
suite.Ctx,
privs,
accNums,
accSeqs,
suite.Ctx.ChainID(),
signing.SignMode_SIGN_MODE_DIRECT,
)
require.NoError(t, err)

if tc.isRecheck {
Expand Down
10 changes: 9 additions & 1 deletion protocol/app/ante/gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ante_test
import (
"cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app"
assets "github.com/dydxprotocol/v4-chain/protocol/x/assets/types"
Expand Down Expand Up @@ -120,7 +121,14 @@ func TestValidateMsgType_FreeInfiniteGasDecorator(t *testing.T) {
// Empty private key, so tx's signature should be empty.
privs, accNums, accSeqs := []cryptotypes.PrivKey{}, []uint64{}, []uint64{}

tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.Ctx.ChainID())
tx, err := suite.CreateTestTx(
suite.Ctx,
privs,
accNums,
accSeqs,
suite.Ctx.ChainID(),
signing.SignMode_SIGN_MODE_DIRECT,
)
require.NoError(t, err)

resultCtx, err := antehandler(suite.Ctx, tx, false)
Expand Down
10 changes: 9 additions & 1 deletion protocol/app/ante/msg_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ante_test
import (
errorsmod "cosmossdk.io/errors"
"fmt"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
"golang.org/x/exp/slices"
"testing"

Expand Down Expand Up @@ -484,7 +485,14 @@ func runTest(t *testing.T, name string, msgs []sdk.Msg, mode txMode, expectedErr
require.NoError(t, suite.TxBuilder.SetMsgs(msgs...))
// Empty private key, so tx's signature should be empty.
privs, accNums, accSeqs := []cryptotypes.PrivKey{}, []uint64{}, []uint64{}
tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.Ctx.ChainID())
tx, err := suite.CreateTestTx(
suite.Ctx,
privs,
accNums,
accSeqs,
suite.Ctx.ChainID(),
signing.SignMode_SIGN_MODE_DIRECT,
)
require.NoError(t, err)
suite.Ctx = getCtxWithTxMode(mode, suite)

Expand Down
58 changes: 37 additions & 21 deletions protocol/app/ante/sigverify.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,33 @@ package ante

import (
errorsmod "cosmossdk.io/errors"
txsigning "cosmossdk.io/x/tx/signing"
"fmt"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"google.golang.org/protobuf/types/known/anypb"

gometrics "github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
sdkante "github.com/cosmos/cosmos-sdk/x/auth/ante"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/dydxprotocol/v4-chain/protocol/lib/metrics"
gometrics "github.com/hashicorp/go-metrics"
)

// Verify all signatures for a tx and return an error if any are invalid. Note,
// SigVerificationDecorator verifies all signatures for a tx and return an error if any are invalid. Note,
// the SigVerificationDecorator will not check signatures on ReCheck.
//
// CONTRACT: Pubkeys are set in context for all signers before this decorator runs
// CONTRACT: Tx must implement SigVerifiableTx interface
type SigVerificationDecorator struct {
ak sdkante.AccountKeeper
signModeHandler authsigning.SignModeHandler
signModeHandler *txsigning.HandlerMap
}

func NewSigVerificationDecorator(
ak sdkante.AccountKeeper,
signModeHandler authsigning.SignModeHandler,
signModeHandler *txsigning.HandlerMap,
) SigVerificationDecorator {
return SigVerificationDecorator{
ak: ak,
Expand All @@ -39,7 +42,7 @@ func (svd SigVerificationDecorator) AnteHandle(
simulate bool,
next sdk.AnteHandler,
) (newCtx sdk.Context, err error) {
sigTx, ok := tx.(authsigning.SigVerifiableTx)
sigTx, ok := tx.(authsigning.Tx)
if !ok {
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "invalid transaction type")
}
Expand All @@ -51,14 +54,17 @@ func (svd SigVerificationDecorator) AnteHandle(
return ctx, err
}

signerAddrs := sigTx.GetSigners()
signers, err := sigTx.GetSigners()
if err != nil {
return ctx, err
}

// check that signer length and signature length are the same
if len(sigs) != len(signerAddrs) {
if len(sigs) != len(signers) {
err := errorsmod.Wrapf(
sdkerrors.ErrUnauthorized,
"invalid number of signer; expected: %d, got %d",
len(signerAddrs),
len(signers),
len(sigs),
)
return ctx, err
Expand All @@ -69,7 +75,7 @@ func (svd SigVerificationDecorator) AnteHandle(
skipSequenceValidation := ShouldSkipSequenceValidation(tx.GetMsgs())

for i, sig := range sigs {
acc, err := sdkante.GetSignerAcc(ctx, svd.ak, signerAddrs[i])
acc, err := sdkante.GetSignerAcc(ctx, svd.ak, signers[i])
if err != nil {
return ctx, err
}
Expand Down Expand Up @@ -109,23 +115,32 @@ func (svd SigVerificationDecorator) AnteHandle(
if !genesis {
accNum = acc.GetAccountNumber()
}
signerData := authsigning.SignerData{
Address: acc.GetAddress().String(),
ChainID: chainID,
AccountNumber: accNum,
Sequence: acc.GetSequence(),
PubKey: pubKey,
}

// no need to verify signatures on recheck tx
if !simulate && !ctx.IsReCheckTx() {
err := authsigning.VerifySignature(pubKey, signerData, sig.Data, svd.signModeHandler, tx)
anyPk, _ := codectypes.NewAnyWithValue(pubKey)

signerData := txsigning.SignerData{
Address: acc.GetAddress().String(),
ChainID: chainID,
AccountNumber: accNum,
Sequence: acc.GetSequence(),
PubKey: &anypb.Any{
TypeUrl: anyPk.TypeUrl,
Value: anyPk.Value,
},
}
adaptableTx, ok := tx.(authsigning.V2AdaptableTx)
if !ok {
return ctx, fmt.Errorf("expected tx to implement V2AdaptableTx, got %T", tx)
}
txData := adaptableTx.GetSigningTxData()
err = authsigning.VerifySignature(ctx, pubKey, signerData, sig.Data, svd.signModeHandler, txData)
if err != nil {
var errMsg string
if sdkante.OnlyLegacyAminoSigners(sig.Data) {
// If all signers are using SIGN_MODE_LEGACY_AMINO, we rely on VerifySignature to
// check account sequence number, and therefore communicate sequence number as a
// potential cause of error.
// If all signers are using SIGN_MODE_LEGACY_AMINO, we rely on VerifySignature to check account sequence number,
// and therefore communicate sequence number as a potential cause of error.
errMsg = fmt.Sprintf(
"signature verification failed; please verify account number (%d), sequence (%d)"+
" and chain-id (%s)",
Expand All @@ -135,9 +150,10 @@ func (svd SigVerificationDecorator) AnteHandle(
)
} else {
errMsg = fmt.Sprintf(
"signature verification failed; please verify account number (%d) and chain-id (%s)",
"signature verification failed; please verify account number (%d) and chain-id (%s): (%s)",
accNum,
chainID,
err.Error(),
)
}
return ctx, errorsmod.Wrap(sdkerrors.ErrUnauthorized, errMsg)
Expand Down
Loading

0 comments on commit 657e07a

Please sign in to comment.