Skip to content

Commit

Permalink
Apply Kava patches on v0.21.0 (#16)
Browse files Browse the repository at this point in the history
* apply patch for base account to eth account conversion on contract (#5)

Apply patch for base account to eth account conversion on contract

* register EthAccount with legacy amino codec in order to use EthAccounts (#8)

over json rpc

* Update cosmos-sdk to use Kava fork and and informal systems tendermint fork

* update type signature to match forked sdk (#10)

* Add EIP712 with multiple messages and migrations

Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>

* Update eip712 tests and params

Removal of unsupported test cases

* fix denoms_to_claim type

* Add tip to ConstructUntypedEIP712Data

* Revert "update type signature to match forked sdk (#10)"

This reverts commit 7c71098.

* Update cosmos fork to remove block height from account constructor

* Add v3 migrations

Includes upstream migrations 2-5

* Remove failing redundant migrations test

* Remove upstream v4 and v5 migrations

* Update migration docs

* Remove redundant v2 param types

* Remove min gas parsing in rosetta config

* Update Cosmos v0.46.11 and cometbft v0.34.27

* Update go.mod

* Rename v2 migration types to avoid duplicate proto registration

* Use tagged Cosmos sdk version v0.46.11-kava.1

---------

Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com>
Co-authored-by: DracoLi <draco@dracoli.com>
  • Loading branch information
4 people committed Mar 30, 2023
1 parent 768e307 commit d9a405f
Show file tree
Hide file tree
Showing 44 changed files with 4,039 additions and 3,489 deletions.
176 changes: 70 additions & 106 deletions app/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"math/big"
"strings"
"time"

sdkmath "cosmossdk.io/math"
kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
Expand All @@ -19,7 +18,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/authz"

sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -49,6 +47,69 @@ func (suite AnteTestSuite) TestAnteHandler() {

suite.app.EvmKeeper.SetBalance(suite.ctx, addr, big.NewInt(10000000000))

params := suite.app.EvmKeeper.GetParams(suite.ctx)
params.EIP712AllowedMsgs = []evmtypes.EIP712AllowedMsg{
{
MsgTypeUrl: "/cosmos.bank.v1beta1.MsgSend",
MsgValueTypeName: "MsgValueSend",
ValueTypes: []evmtypes.EIP712MsgAttrType{
{Name: "from_address", Type: "string"},
{Name: "to_address", Type: "string"},
{Name: "amount", Type: "Coin[]"},
},
},
{
MsgTypeUrl: "/cosmos.staking.v1beta1.MsgDelegate",
MsgValueTypeName: "MsgValueDelegate",
ValueTypes: []evmtypes.EIP712MsgAttrType{
{Name: "delegator_address", Type: "string"},
{Name: "validator_address", Type: "string"},
{Name: "amount", Type: "Coin"},
},
},
{
MsgTypeUrl: "/cosmos.staking.v1beta1.MsgCreateValidator",
MsgValueTypeName: "MsgValueCreateValidator",
ValueTypes: []evmtypes.EIP712MsgAttrType{
{Name: "description", Type: "Description"},
{Name: "commission", Type: "CommissionRates"},
{Name: "min_self_delegation", Type: "string"},
{Name: "delegator_address", Type: "string"},
{Name: "validator_address", Type: "string"},
{Name: "pubkey", Type: "Pubkey"},
{Name: "value", Type: "Coin"},
},
NestedTypes: []evmtypes.EIP712NestedMsgType{
{
Name: "Description",
Attrs: []evmtypes.EIP712MsgAttrType{
{Name: "moniker", Type: "string"},
{Name: "identity", Type: "string"},
{Name: "website", Type: "string"},
{Name: "security_contact", Type: "string"},
{Name: "details", Type: "string"},
},
},
{
Name: "CommissionRates",
Attrs: []evmtypes.EIP712MsgAttrType{
{Name: "rate", Type: "string"},
{Name: "max_rate", Type: "string"},
{Name: "max_change_rate", Type: "string"},
},
},
{
Name: "Pubkey",
Attrs: []evmtypes.EIP712MsgAttrType{
{Name: "type", Type: "string"},
{Name: "value", Type: "string"},
},
},
},
},
}
suite.app.EvmKeeper.SetParams(suite.ctx, params)

suite.app.FeeMarketKeeper.SetBaseFee(suite.ctx, big.NewInt(100))
}

Expand Down Expand Up @@ -350,135 +411,38 @@ func (suite AnteTestSuite) TestAnteHandler() {
}, false, false, true,
},
{
"success- DeliverTx EIP712 create validator (with blank fields)",
func() sdk.Tx {
from := acc.GetAddress()
coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewInt(20))
amount := sdk.NewCoins(coinAmount)
gas := uint64(200000)
txBuilder := suite.CreateTestEIP712MsgCreateValidator2(from, privKey, "ethermint_9000-1", gas, amount)
return txBuilder.GetTx()
}, false, false, true,
},
{
"success- DeliverTx EIP712 MsgSubmitProposal",
func() sdk.Tx {
from := acc.GetAddress()
coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewInt(20))
gasAmount := sdk.NewCoins(coinAmount)
gas := uint64(200000)
// reusing the gasAmount for deposit
deposit := sdk.NewCoins(coinAmount)
txBuilder := suite.CreateTestEIP712SubmitProposal(from, privKey, "ethermint_9000-1", gas, gasAmount, deposit)
return txBuilder.GetTx()
}, false, false, true,
},
{
"success- DeliverTx EIP712 MsgGrant",
func() sdk.Tx {
from := acc.GetAddress()
grantee := sdk.AccAddress("_______grantee______")
coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewInt(20))
gasAmount := sdk.NewCoins(coinAmount)
gas := uint64(200000)
blockTime := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC)
expiresAt := blockTime.Add(time.Hour)
msg, err := authz.NewMsgGrant(
from, grantee, &banktypes.SendAuthorization{SpendLimit: gasAmount}, &expiresAt,
)
suite.Require().NoError(err)
return suite.CreateTestEIP712SingleMessageTxBuilder(from, privKey, "ethermint_9000-1", gas, gasAmount, msg).GetTx()
}, false, false, true,
},

{
"success- DeliverTx EIP712 MsgGrantAllowance",
func() sdk.Tx {
from := acc.GetAddress()
coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewInt(20))
gasAmount := sdk.NewCoins(coinAmount)
gas := uint64(200000)
txBuilder := suite.CreateTestEIP712GrantAllowance(from, privKey, "ethermint_9000-1", gas, gasAmount)
return txBuilder.GetTx()
}, false, false, true,
},
{
"success- DeliverTx EIP712 edit validator",
func() sdk.Tx {
from := acc.GetAddress()
coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewInt(20))
amount := sdk.NewCoins(coinAmount)
gas := uint64(200000)
txBuilder := suite.CreateTestEIP712MsgEditValidator(from, privKey, "ethermint_9000-1", gas, amount)
return txBuilder.GetTx()
}, false, false, true,
},
{
"success- DeliverTx EIP712 submit evidence",
func() sdk.Tx {
from := acc.GetAddress()
coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewInt(20))
amount := sdk.NewCoins(coinAmount)
gas := uint64(200000)
txBuilder := suite.CreateTestEIP712MsgSubmitEvidence(from, privKey, "ethermint_9000-1", gas, amount)
return txBuilder.GetTx()
}, false, false, true,
},
{
"success- DeliverTx EIP712 submit proposal v1",
func() sdk.Tx {
from := acc.GetAddress()
coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewInt(20))
amount := sdk.NewCoins(coinAmount)
gas := uint64(200000)
txBuilder := suite.CreateTestEIP712SubmitProposalV1(from, privKey, "ethermint_9000-1", gas, amount)
return txBuilder.GetTx()
}, false, false, true,
},
{
"success- DeliverTx EIP712 MsgExec",
"success- DeliverTx EIP712 Multiple MsgSend",
func() sdk.Tx {
from := acc.GetAddress()
coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewInt(20))
amount := sdk.NewCoins(coinAmount)
gas := uint64(200000)
txBuilder := suite.CreateTestEIP712MsgExec(from, privKey, "ethermint_9000-1", gas, amount)
txBuilder := suite.CreateTestEIP712MultipleMsgSend(from, privKey, "ethermint_9000-1", gas, amount)
return txBuilder.GetTx()
}, false, false, true,
},
{
"success- DeliverTx EIP712 MsgVoteV1",
"fails - DeliverTx EIP712 Multiple Signers",
func() sdk.Tx {
from := acc.GetAddress()
coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewInt(20))
amount := sdk.NewCoins(coinAmount)
gas := uint64(200000)
txBuilder := suite.CreateTestEIP712MsgVoteV1(from, privKey, "ethermint_9000-1", gas, amount)
txBuilder := suite.CreateTestEIP712MultipleSignerMsgs(from, privKey, "ethermint_9000-1", gas, amount)
return txBuilder.GetTx()
}, false, false, true,
}, false, false, false,
},
{
"success- DeliverTx EIP712 Multiple MsgSend",
"success - DeliverTx EIP712 signed Cosmos Tx with multiple messages",
func() sdk.Tx {
from := acc.GetAddress()
coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewInt(20))
amount := sdk.NewCoins(coinAmount)
gas := uint64(200000)
txBuilder := suite.CreateTestEIP712MultipleMsgSend(from, privKey, "ethermint_9000-1", gas, amount)
txBuilder := suite.CreateTestEIP712TxBuilderMultipleMsgs(from, privKey, "ethermint_9000-1", gas, amount)
return txBuilder.GetTx()
}, false, false, true,
},
{
"fails - DeliverTx EIP712 Multiple Signers",
func() sdk.Tx {
from := acc.GetAddress()
coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewInt(20))
amount := sdk.NewCoins(coinAmount)
gas := uint64(200000)
txBuilder := suite.CreateTestEIP712MultipleSignerMsgs(from, privKey, "ethermint_9000-1", gas, amount)
return txBuilder.GetTx()
}, false, false, false,
},
{
"fails - DeliverTx EIP712 signed Cosmos Tx with wrong Chain ID",
func() sdk.Tx {
Expand Down
17 changes: 12 additions & 5 deletions app/ante/eip712.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func NewLegacyCosmosAnteHandlerEip712(options HandlerOptions) sdk.AnteHandler {
authante.NewValidateSigCountDecorator(options.AccountKeeper),
authante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer),
// Note: signature verification uses EIP instead of the cosmos signature validator
NewLegacyEip712SigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
NewLegacyEip712SigVerificationDecorator(options.AccountKeeper, options.SignModeHandler, options.EvmKeeper),
authante.NewIncrementSequenceDecorator(options.AccountKeeper),
ibcante.NewRedundantRelayDecorator(options.IBCKeeper),
NewGasWantedDecorator(options.EvmKeeper, options.FeeMarketKeeper),
Expand All @@ -81,16 +81,19 @@ func NewLegacyCosmosAnteHandlerEip712(options HandlerOptions) sdk.AnteHandler {
type LegacyEip712SigVerificationDecorator struct {
ak evmtypes.AccountKeeper
signModeHandler authsigning.SignModeHandler
evmKeeper EVMKeeper
}

// Deprecated: NewLegacyEip712SigVerificationDecorator creates a new LegacyEip712SigVerificationDecorator
func NewLegacyEip712SigVerificationDecorator(
ak evmtypes.AccountKeeper,
signModeHandler authsigning.SignModeHandler,
ek EVMKeeper,
) LegacyEip712SigVerificationDecorator {
return LegacyEip712SigVerificationDecorator{
ak: ak,
signModeHandler: signModeHandler,
evmKeeper: ek,
}
}

Expand Down Expand Up @@ -181,7 +184,9 @@ func (svd LegacyEip712SigVerificationDecorator) AnteHandle(ctx sdk.Context,
return next(ctx, tx, simulate)
}

if err := VerifySignature(pubKey, signerData, sig.Data, svd.signModeHandler, authSignTx); err != nil {
evmParams := svd.evmKeeper.GetParams(ctx)

if err := VerifySignature(pubKey, signerData, sig.Data, svd.signModeHandler, authSignTx, evmParams); err != nil {
errMsg := fmt.Errorf("signature verification failed; please verify account number (%d) and chain-id (%s): %w", accNum, chainID, err)
return ctx, errorsmod.Wrap(errortypes.ErrUnauthorized, errMsg.Error())
}
Expand All @@ -197,6 +202,7 @@ func VerifySignature(
sigData signing.SignatureData,
_ authsigning.SignModeHandler,
tx authsigning.Tx,
params evmtypes.Params,
) error {
switch data := sigData.(type) {
case *signing.SingleSignatureData:
Expand All @@ -217,7 +223,7 @@ func VerifySignature(
return errorsmod.Wrap(errortypes.ErrNoSignatures, "tx doesn't contain any msgs to verify signature")
}

txBytes := legacytx.StdSignBytes(
txBytes := eip712.ConstructUntypedEIP712Data(
signerData.ChainID,
signerData.AccountNumber,
signerData.Sequence,
Expand All @@ -226,7 +232,8 @@ func VerifySignature(
Amount: tx.GetFee(),
Gas: tx.GetGas(),
},
msgs, tx.GetMemo(), tx.GetTip(),
msgs, tx.GetMemo(),
tx.GetTip(),
)

signerChainID, err := ethermint.ParseChainID(signerData.ChainID)
Expand Down Expand Up @@ -264,7 +271,7 @@ func VerifySignature(
FeePayer: feePayer,
}

typedData, err := eip712.WrapTxToTypedData(ethermintCodec, extOpt.TypedDataChainID, msgs[0], txBytes, feeDelegation)
typedData, err := eip712.WrapTxToTypedData(extOpt.TypedDataChainID, msgs, txBytes, feeDelegation, params)
if err != nil {
return errorsmod.Wrap(err, "failed to create EIP-712 typed data from tx")
}
Expand Down
14 changes: 14 additions & 0 deletions app/ante/fee_market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ func (suite AnteTestSuite) TestGasWantedDecorator() {
from, fromPrivKey := tests.NewAddrKey()
to := tests.GenerateAddress()

params := suite.app.EvmKeeper.GetParams(suite.ctx)
params.EIP712AllowedMsgs = []evmtypes.EIP712AllowedMsg{
{
MsgTypeUrl: "/cosmos.bank.v1beta1.MsgSend",
MsgValueTypeName: "MsgValueSend",
ValueTypes: []evmtypes.EIP712MsgAttrType{
{Name: "from_address", Type: "string"},
{Name: "to_address", Type: "string"},
{Name: "amount", Type: "Coin[]"},
},
},
}
suite.app.EvmKeeper.SetParams(suite.ctx, params)

testCases := []struct {
name string
expectedGasWanted uint64
Expand Down
33 changes: 14 additions & 19 deletions app/ante/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ func (suite *AnteTestSuite) SetupTest() {
encodingConfig := encoding.MakeConfig(app.ModuleBasics)
// We're using TestMsg amino encoding in some tests, so register it here.
encodingConfig.Amino.RegisterConcrete(&testdata.TestMsg{}, "testdata.TestMsg", nil)
eip712.SetEncodingConfig(encodingConfig)

suite.clientCtx = client.Context{}.WithTxConfig(encodingConfig.TxConfig)

Expand Down Expand Up @@ -285,6 +284,15 @@ func (suite *AnteTestSuite) CreateTestEIP712TxBuilderMsgDelegate(from sdk.AccAdd
return suite.CreateTestEIP712SingleMessageTxBuilder(from, priv, chainId, gas, gasAmount, msgSend)
}

func (suite *AnteTestSuite) CreateTestEIP712TxBuilderMultipleMsgs(from sdk.AccAddress, priv cryptotypes.PrivKey, chainId string, gas uint64, gasAmount sdk.Coins) client.TxBuilder {
valEthAddr := tests.GenerateAddress()
valAddr := sdk.ValAddress(valEthAddr.Bytes())
recipient := sdk.AccAddress(common.Address{}.Bytes())
msgSend := banktypes.NewMsgSend(from, recipient, sdk.NewCoins(sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewInt(1))))
msgDelegate := stakingtypes.NewMsgDelegate(from, valAddr, sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewInt(20)))
return suite.CreateTestEIP712CosmosTxBuilder(from, priv, chainId, gas, gasAmount, []sdk.Msg{msgSend, msgDelegate, msgDelegate, msgSend})
}

func (suite *AnteTestSuite) CreateTestEIP712MsgCreateValidator(from sdk.AccAddress, priv cryptotypes.PrivKey, chainId string, gas uint64, gasAmount sdk.Coins) client.TxBuilder {
// Build MsgCreateValidator
valAddr := sdk.ValAddress(from.Bytes())
Expand Down Expand Up @@ -486,19 +494,18 @@ func (suite *AnteTestSuite) CreateTestEIP712CosmosTxBuilder(
ethChainId := pc.Uint64()

// GenerateTypedData TypedData
var ethermintCodec codec.ProtoCodecMarshaler
registry := codectypes.NewInterfaceRegistry()
types.RegisterInterfaces(registry)
ethermintCodec = codec.NewProtoCodec(registry)
cryptocodec.RegisterInterfaces(registry)

fee := legacytx.NewStdFee(gas, gasAmount)
accNumber := suite.app.AccountKeeper.GetAccount(suite.ctx, from).GetAccountNumber()

data := legacytx.StdSignBytes(chainId, accNumber, nonce, 0, fee, msgs, "", nil)
typedData, err := eip712.WrapTxToTypedData(ethermintCodec, ethChainId, msgs[0], data, &eip712.FeeDelegationOptions{
data := eip712.ConstructUntypedEIP712Data(chainId, accNumber, nonce, 0, fee, msgs, "", nil)
evmParams := suite.app.EvmKeeper.GetParams(suite.ctx)
typedData, err := eip712.WrapTxToTypedData(ethChainId, msgs, data, &eip712.FeeDelegationOptions{
FeePayer: from,
})
}, evmParams)
suite.Require().NoError(err)

sigHash, _, err := apitypes.TypedDataAndHash(typedData)
Expand Down Expand Up @@ -560,19 +567,7 @@ func (suite *AnteTestSuite) GenerateMultipleKeys(n int) ([]cryptotypes.PrivKey,

// generateSingleSignature signs the given sign doc bytes using the given signType (EIP-712 or Standard)
func (suite *AnteTestSuite) generateSingleSignature(signMode signing.SignMode, privKey cryptotypes.PrivKey, signDocBytes []byte, signType string) (signature signing.SignatureV2) {
var (
msg []byte
err error
)

msg = signDocBytes

if signType == "EIP-712" {
msg, err = eip712.GetEIP712BytesForMsg(signDocBytes)
suite.Require().NoError(err)
}

sigBytes, _ := privKey.Sign(msg)
sigBytes, _ := privKey.Sign(signDocBytes)
sigData := &signing.SingleSignatureData{
SignMode: signMode,
Signature: sigBytes,
Expand Down
Loading

0 comments on commit d9a405f

Please sign in to comment.