Skip to content
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

Migrate x/genutil to use TxConfig #6734

Merged
merged 44 commits into from
Jul 25, 2020
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
ab26eac
Update genutil collect and gentx to use TxGenerator
blushi Jul 15, 2020
aa5fd30
Remove print statement
blushi Jul 15, 2020
fd5d400
Use Tx in genutil DeliverGenTxs
blushi Jul 16, 2020
4259efc
Use Tx in genutil genesis_state
blushi Jul 16, 2020
cec4fa6
Use Tx in ValidateGenesis
blushi Jul 16, 2020
44a5ec0
Merge branch 'master' into marie/5917-x-genutil
blushi Jul 16, 2020
b02117c
Use amino txJSONDecoder and txBinaryEncoder in genutil InitGenesis
blushi Jul 17, 2020
406515c
Merge branch 'master' into marie/5917-x-genutil
blushi Jul 17, 2020
b1692af
Merge branch 'master' into marie/5917-x-genutil
jackzampolin Jul 21, 2020
2ae43ad
Merge branch 'master' into marie/5917-x-genutil
blushi Jul 22, 2020
f40eb9c
Use TxConfig in place of TxGenerator
blushi Jul 22, 2020
9e31db9
Add gentx tests
blushi Jul 23, 2020
7e394b0
Merge branch 'master' into marie/5917-x-genutil
blushi Jul 23, 2020
51a7e8a
Remove commented line
blushi Jul 23, 2020
f14d527
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ma…
aaronc Jul 23, 2020
6619018
Test fixes
aaronc Jul 23, 2020
10d9153
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into aa…
aaronc Jul 24, 2020
4d5181b
Apply suggestions from code review
aaronc Jul 24, 2020
380b369
Merge remote-tracking branch 'origin/aaronc/6734-updates' into aaronc…
aaronc Jul 24, 2020
1f1e38a
Merge branch 'master' into marie/5917-x-genutil
aaronc Jul 24, 2020
0896def
Fixes
aaronc Jul 24, 2020
77b1c22
Merge branch 'marie/5917-x-genutil' of https://github.com/cosmos/cosm…
aaronc Jul 24, 2020
391dcdc
Fixes
aaronc Jul 24, 2020
9b77a01
Fixes
aaronc Jul 24, 2020
3e6fc8d
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into aa…
aaronc Jul 24, 2020
ee904d5
Merge branch 'master' into marie/5917-x-genutil
aaronc Jul 24, 2020
22b1dcd
Merge branch 'marie/5917-x-genutil' of https://github.com/cosmos/cosm…
aaronc Jul 24, 2020
8545cf3
Fixes
aaronc Jul 24, 2020
0ac6d28
Remove unneeded test case (doesn't apply to proto marshaling)
aaronc Jul 24, 2020
b2dd715
linting
blushi Jul 24, 2020
6a6ae20
Merge branch 'master' into marie/5917-x-genutil
blushi Jul 24, 2020
8d463a8
Refactor to use new TxEncodingConfig interface in genutil module
blushi Jul 24, 2020
59064d9
Replace golang/protobuf with gogo/protobuf package
blushi Jul 24, 2020
1203a83
Use TxEncodingConfig in InitTestnet
blushi Jul 24, 2020
5070cd2
Remove old amino.go file
blushi Jul 24, 2020
5242be2
Use TxJSONDecoder in genutil ValidateGenesis
blushi Jul 24, 2020
72d0d15
Merge branch 'master' into marie/5917-x-genutil
aaronc Jul 24, 2020
5f3f0ed
Merge branch 'master' into marie/5917-x-genutil
aaronc Jul 24, 2020
6a9c975
Add parameter to ValidateGenesis to resolve the tx JSON decoder issue
aaronc Jul 25, 2020
62f9fad
Merge remote-tracking branch 'origin/marie/5917-x-genutil' into marie…
aaronc Jul 25, 2020
fa30ce3
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ma…
aaronc Jul 25, 2020
eb633a6
Address review feedback
aaronc Jul 25, 2020
37f09cc
Merge branch 'master' into marie/5917-x-genutil
mergify[bot] Jul 25, 2020
abb0c11
Merge branch 'master' into marie/5917-x-genutil
mergify[bot] Jul 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions client/tx_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ import (
)

type (
// TxEncodingConfig defines an interface that contains transaction
// encoders and decoders
TxEncodingConfig interface {
TxEncoder() sdk.TxEncoder
TxDecoder() sdk.TxDecoder
TxJSONEncoder() sdk.TxEncoder
TxJSONDecoder() sdk.TxDecoder
}

// TxConfig defines an interface a client can utilize to generate an
// application-defined concrete transaction type. The type returned must
// implement TxBuilder.
TxConfig interface {
TxEncodingConfig

NewTxBuilder() TxBuilder
SignModeHandler() signing.SignModeHandler

TxEncoder() sdk.TxEncoder
TxDecoder() sdk.TxDecoder
TxJSONEncoder() sdk.TxEncoder
TxJSONDecoder() sdk.TxDecoder
}

// TxBuilder defines an interface which an application-defined concrete transaction
Expand Down
5 changes: 4 additions & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,10 @@ func NewSimApp(
// NOTE: Any module instantiated in the module manager that is later modified
// must be passed by reference here.
app.mm = module.NewManager(
genutil.NewAppModule(app.AccountKeeper, app.StakingKeeper, app.BaseApp.DeliverTx),
genutil.NewAppModule(
app.AccountKeeper, app.StakingKeeper, app.BaseApp.DeliverTx,
encodingConfig.TxConfig,
),
auth.NewAppModule(appCodec, app.AccountKeeper),
bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper),
capability.NewAppModule(appCodec, *app.CapabilityKeeper),
Expand Down
2 changes: 1 addition & 1 deletion simapp/params/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func MakeEncodingConfig() EncodingConfig {
cdc := codec.New()
interfaceRegistry := types.NewInterfaceRegistry()
marshaler := codec.NewHybridCodec(cdc, interfaceRegistry)
txGen := tx.NewTxConfig(marshaler, std.DefaultPublicKeyCodec{}, tx.DefaultSignModeHandler())
txGen := tx.NewTxConfig(interfaceRegistry, std.DefaultPublicKeyCodec{}, tx.DefaultSignModeHandler())

return EncodingConfig{
InterfaceRegistry: interfaceRegistry,
Expand Down
10 changes: 6 additions & 4 deletions simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Example:
numValidators, _ := cmd.Flags().GetInt(flagNumValidators)

return InitTestnet(
cmd, config, cdc, mbm, genBalIterator, outputDir, chainID, minGasPrices,
cmd, config, cdc, clientCtx.TxConfig, mbm, genBalIterator, outputDir, chainID, minGasPrices,
nodeDirPrefix, nodeDaemonHome, nodeCLIHome, startingIPAddress, keyringBackend, numValidators,
)
},
Expand All @@ -98,6 +98,7 @@ const nodeDirPerm = 0755
// Initialize the testnet
func InitTestnet(
cmd *cobra.Command, config *tmconfig.Config, cdc codec.JSONMarshaler,
txEncodingConfig client.TxEncodingConfig,
mbm module.BasicManager, genBalIterator banktypes.GenesisBalancesIterator,
outputDir, chainID, minGasPrices, nodeDirPrefix, nodeDaemonHome,
nodeCLIHome, startingIPAddress, keyringBackend string, numValidators int,
Expand Down Expand Up @@ -240,7 +241,7 @@ func InitTestnet(
}

err := collectGenFiles(
cdc, config, chainID, nodeIDs, valPubKeys, numValidators,
cdc, txEncodingConfig, config, chainID, nodeIDs, valPubKeys, numValidators,
outputDir, nodeDirPrefix, nodeDaemonHome, genBalIterator,
)
if err != nil {
Expand Down Expand Up @@ -294,7 +295,8 @@ func initGenFiles(
}

func collectGenFiles(
cdc codec.JSONMarshaler, config *tmconfig.Config, chainID string,
cdc codec.JSONMarshaler, txEncodingConfig client.TxEncodingConfig,
config *tmconfig.Config, chainID string,
nodeIDs []string, valPubKeys []crypto.PubKey,
numValidators int, outputDir, nodeDirPrefix, nodeDaemonHome string,
genBalIterator banktypes.GenesisBalancesIterator,
Expand All @@ -319,7 +321,7 @@ func collectGenFiles(
return err
}

nodeAppState, err := genutil.GenAppStateFromConfig(cdc, config, initCfg, *genDoc, genBalIterator)
nodeAppState, err := genutil.GenAppStateFromConfig(cdc, txEncodingConfig, config, initCfg, *genDoc, genBalIterator)
aaronc marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down
17 changes: 12 additions & 5 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"testing"
"time"

"github.com/cosmos/cosmos-sdk/client/tx"

"github.com/stretchr/testify/require"
tmcfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto"
Expand Down Expand Up @@ -278,16 +280,21 @@ func New(t *testing.T, cfg Config) *Network {
require.NoError(t, err)

memo := fmt.Sprintf("%s@%s:%s", nodeIDs[i], p2pURL.Hostname(), p2pURL.Port())
tx := authtypes.NewStdTx([]sdk.Msg{createValMsg}, authtypes.StdFee{}, []authtypes.StdSignature{}, memo) //nolint:staticcheck // SA1019: authtypes.StdFee is deprecated
txBldr := authtypes.TxBuilder{}.
txBuilder := cfg.TxConfig.NewTxBuilder()
require.NoError(t, txBuilder.SetMsgs(createValMsg))
txBuilder.SetMemo(memo)

txFactory := tx.Factory{}
txFactory = txFactory.
WithChainID(cfg.ChainID).
WithMemo(memo).
WithKeybase(kb)
WithKeybase(kb).
WithTxConfig(cfg.TxConfig)

signedTx, err := txBldr.SignStdTx(nodeDirName, tx, false)
err = tx.Sign(txFactory, nodeDirName, txBuilder)
require.NoError(t, err)

txBz, err := cfg.Codec.MarshalJSON(signedTx)
txBz, err := cfg.TxConfig.TxJSONEncoder()(txBuilder.GetTx())
require.NoError(t, err)
require.NoError(t, writeFile(fmt.Sprintf("%v.json", nodeDirName), gentxsDir, txBz))

Expand Down
3 changes: 2 additions & 1 deletion testutil/network/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ func collectGenFiles(cfg Config, vals []*Validator, outputDir string) error {
return err
}

appState, err := genutil.GenAppStateFromConfig(cfg.Codec, tmCfg, initCfg, *genDoc, banktypes.GenesisBalancesIterator{})
appState, err := genutil.GenAppStateFromConfig(cfg.Codec, cfg.TxConfig,
tmCfg, initCfg, *genDoc, banktypes.GenesisBalancesIterator{})
if err != nil {
return err
}
Expand Down
19 changes: 13 additions & 6 deletions x/auth/tx/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package tx
import (
"fmt"

"github.com/gogo/protobuf/proto"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/x/auth/signing/direct"

Expand All @@ -16,7 +18,6 @@ import (

"github.com/tendermint/tendermint/crypto"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -37,7 +38,6 @@ type builder struct {
// or decoded from AuthInfo when GetPubKey's was called
pubKeys []crypto.PubKey

marshaler codec.Marshaler
pubkeyCodec types.PublicKeyCodec
}

Expand All @@ -47,15 +47,14 @@ var (
_ direct.ProtoTx = &builder{}
)

func newBuilder(marshaler codec.Marshaler, pubkeyCodec types.PublicKeyCodec) *builder {
func newBuilder(pubkeyCodec types.PublicKeyCodec) *builder {
return &builder{
tx: &tx.Tx{
Body: &tx.TxBody{},
AuthInfo: &tx.AuthInfo{
Fee: &tx.Fee{},
},
},
marshaler: marshaler,
pubkeyCodec: pubkeyCodec,
}
}
Expand Down Expand Up @@ -131,7 +130,11 @@ func (t *builder) GetBodyBytes() []byte {
// this method should always return the correct bytes. Note that after
// decoding bodyBz is derived from TxRaw so that it matches what was
// transmitted over the wire
t.bodyBz = t.marshaler.MustMarshalBinaryBare(t.tx.Body)
var err error
t.bodyBz, err = proto.Marshal(t.tx.Body)
if err != nil {
panic(err)
}
}
return t.bodyBz
}
Expand All @@ -143,7 +146,11 @@ func (t *builder) GetAuthInfoBytes() []byte {
// this method should always return the correct bytes. Note that after
// decoding authInfoBz is derived from TxRaw so that it matches what was
// transmitted over the wire
t.authInfoBz = t.marshaler.MustMarshalBinaryBare(t.tx.AuthInfo)
var err error
t.authInfoBz, err = proto.Marshal(t.tx.AuthInfo)
if err != nil {
panic(err)
}
}
return t.authInfoBz
}
Expand Down
5 changes: 2 additions & 3 deletions x/auth/tx/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestTxBuilder(t *testing.T) {
_, pubkey, addr := testdata.KeyTestPubAddr()

marshaler := codec.NewHybridCodec(codec.New(), codectypes.NewInterfaceRegistry())
tx := newBuilder(marshaler, std.DefaultPublicKeyCodec{})
tx := newBuilder(std.DefaultPublicKeyCodec{})

cdc := std.DefaultPublicKeyCodec{}

Expand Down Expand Up @@ -125,8 +125,7 @@ func TestBuilderValidateBasic(t *testing.T) {
// require to fail validation upon invalid fee
badFeeAmount := testdata.NewTestFeeAmount()
badFeeAmount[0].Amount = sdk.NewInt(-5)
marshaler := codec.NewHybridCodec(codec.New(), codectypes.NewInterfaceRegistry())
bldr := newBuilder(marshaler, std.DefaultPublicKeyCodec{})
bldr := newBuilder(std.DefaultPublicKeyCodec{})

var sig1, sig2 signing.SignatureV2
sig1 = signing.SignatureV2{
Expand Down
12 changes: 7 additions & 5 deletions x/auth/tx/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package tx
import (
"github.com/tendermint/tendermint/crypto"

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

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

"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// DefaultTxDecoder returns a default protobuf TxDecoder using the provided Marshaler and PublicKeyCodec
func DefaultTxDecoder(cdc codec.Marshaler, keyCodec cryptotypes.PublicKeyCodec) sdk.TxDecoder {
func DefaultTxDecoder(anyUnpacker types.AnyUnpacker, keyCodec cryptotypes.PublicKeyCodec) sdk.TxDecoder {
cdc := codec.NewProtoCodec(anyUnpacker)
return func(txBytes []byte) (sdk.Tx, error) {
var raw tx.TxRaw
err := cdc.UnmarshalBinaryBare(txBytes, &raw)
Expand All @@ -35,14 +38,14 @@ func DefaultTxDecoder(cdc codec.Marshaler, keyCodec cryptotypes.PublicKeyCodec)
bodyBz: raw.BodyBytes,
authInfoBz: raw.AuthInfoBytes,
pubKeys: pks,
marshaler: cdc,
pubkeyCodec: keyCodec,
}, nil
}
}

// DefaultTxDecoder returns a default protobuf JSON TxDecoder using the provided Marshaler and PublicKeyCodec
func DefaultJSONTxDecoder(cdc codec.Marshaler, keyCodec cryptotypes.PublicKeyCodec) sdk.TxDecoder {
func DefaultJSONTxDecoder(anyUnpacker types.AnyUnpacker, keyCodec cryptotypes.PublicKeyCodec) sdk.TxDecoder {
cdc := codec.NewProtoCodec(anyUnpacker)
return func(txBytes []byte) (sdk.Tx, error) {
var theTx tx.Tx
err := cdc.UnmarshalJSON(txBytes, &theTx)
Expand All @@ -58,7 +61,6 @@ func DefaultJSONTxDecoder(cdc codec.Marshaler, keyCodec cryptotypes.PublicKeyCod
return &builder{
tx: &theTx,
pubKeys: pks,
marshaler: cdc,
pubkeyCodec: keyCodec,
}, nil
}
Expand Down
10 changes: 6 additions & 4 deletions x/auth/tx/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package tx
import (
"fmt"

"github.com/gogo/protobuf/proto"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
)

// DefaultTxEncoder returns a default protobuf TxEncoder using the provided Marshaler
func DefaultTxEncoder(marshaler codec.Marshaler) types.TxEncoder {
func DefaultTxEncoder() types.TxEncoder {
return func(tx types.Tx) ([]byte, error) {
wrapper, ok := tx.(*builder)
if !ok {
Expand All @@ -22,18 +24,18 @@ func DefaultTxEncoder(marshaler codec.Marshaler) types.TxEncoder {
Signatures: wrapper.tx.Signatures,
}

return marshaler.MarshalBinaryBare(raw)
return proto.Marshal(raw)
}
}

// DefaultTxEncoder returns a default protobuf JSON TxEncoder using the provided Marshaler
func DefaultJSONTxEncoder(marshaler codec.Marshaler) types.TxEncoder {
func DefaultJSONTxEncoder() types.TxEncoder {
return func(tx types.Tx) ([]byte, error) {
wrapper, ok := tx.(*builder)
if !ok {
return nil, fmt.Errorf("expected %T, got %T", &builder{}, tx)
}

return marshaler.MarshalJSON(wrapper.tx)
return codec.ProtoMarshalJSON(wrapper.tx)
}
}
16 changes: 7 additions & 9 deletions x/auth/tx/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package tx

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
)

type generator struct {
marshaler codec.Marshaler
pubkeyCodec types.PublicKeyCodec
handler signing.SignModeHandler
decoder sdk.TxDecoder
Expand All @@ -19,20 +18,19 @@ type generator struct {
}

// NewTxConfig returns a new protobuf TxConfig using the provided Marshaler, PublicKeyCodec and SignModeHandler.
func NewTxConfig(marshaler codec.Marshaler, pubkeyCodec types.PublicKeyCodec, signModeHandler signing.SignModeHandler) client.TxConfig {
func NewTxConfig(anyUnpacker codectypes.AnyUnpacker, pubkeyCodec types.PublicKeyCodec, signModeHandler signing.SignModeHandler) client.TxConfig {
return &generator{
marshaler: marshaler,
pubkeyCodec: pubkeyCodec,
handler: signModeHandler,
decoder: DefaultTxDecoder(marshaler, pubkeyCodec),
encoder: DefaultTxEncoder(marshaler),
jsonDecoder: DefaultJSONTxDecoder(marshaler, pubkeyCodec),
jsonEncoder: DefaultJSONTxEncoder(marshaler),
decoder: DefaultTxDecoder(anyUnpacker, pubkeyCodec),
encoder: DefaultTxEncoder(),
jsonDecoder: DefaultJSONTxDecoder(anyUnpacker, pubkeyCodec),
jsonEncoder: DefaultJSONTxEncoder(),
}
}

func (g generator) NewTxBuilder() client.TxBuilder {
return newBuilder(g.marshaler, g.pubkeyCodec)
return newBuilder(g.pubkeyCodec)
}

func (g generator) SignModeHandler() signing.SignModeHandler {
Expand Down
1 change: 1 addition & 0 deletions x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func (am AppModule) NewQuerierHandler() sdk.Querier {
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate {
var genesisState types.GenesisState

cdc.MustUnmarshalJSON(data, &genesisState)
am.keeper.InitGenesis(ctx, genesisState)
return []abci.ValidatorUpdate{}
Expand Down
4 changes: 3 additions & 1 deletion x/genutil/client/cli/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ func CollectGenTxsCmd(genBalIterator types.GenesisBalancesIterator, defaultNodeH
toPrint := newPrintInfo(config.Moniker, genDoc.ChainID, nodeID, genTxsDir, json.RawMessage(""))
initCfg := types.NewInitConfig(genDoc.ChainID, genTxsDir, nodeID, valPubKey)

appMessage, err := genutil.GenAppStateFromConfig(cdc, config, initCfg, *genDoc, genBalIterator)
appMessage, err := genutil.GenAppStateFromConfig(cdc,
clientCtx.TxConfig,
config, initCfg, *genDoc, genBalIterator)
if err != nil {
return errors.Wrap(err, "failed to get genesis app state from config")
}
Expand Down
Loading