Merge PR #1741: CoreContext Refactor

This commit is contained in:
Alexander Bezobchuk
2018-08-06 20:11:30 +02:00
committed by Christopher Goes
parent 3c4985c315
commit 12c2c236c2
48 changed files with 1521 additions and 1195 deletions
+21 -34
View File
@@ -1,78 +1,65 @@
package cli
import (
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/examples/democoin/x/cool"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
"github.com/cosmos/cosmos-sdk/examples/democoin/x/cool"
authctx "github.com/cosmos/cosmos-sdk/x/auth/client/context"
)
// take the coolness quiz transaction
// QuizTxCmd invokes the coolness quiz transaction.
func QuizTxCmd(cdc *wire.Codec) *cobra.Command {
return &cobra.Command{
Use: "cool [answer]",
Short: "What's cooler than being cool?",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
txCtx := authctx.NewTxContextFromCLI().WithCodec(cdc)
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithLogger(os.Stdout).
WithAccountDecoder(authcmd.GetAccountDecoder(cdc))
// get the from address from the name flag
from, err := ctx.GetFromAddress()
from, err := cliCtx.GetFromAddress()
if err != nil {
return err
}
// create the message
msg := cool.NewMsgQuiz(from, args[0])
// get account name
name := viper.GetString(client.FlagName)
// build and sign the transaction, then broadcast to Tendermint
err = ctx.EnsureSignBuildBroadcast(name, []sdk.Msg{msg}, cdc)
if err != nil {
return err
}
return nil
return utils.SendTx(txCtx, cliCtx, []sdk.Msg{msg})
},
}
}
// set a new cool trend transaction
// SetTrendTxCmd sends a new cool trend transaction.
func SetTrendTxCmd(cdc *wire.Codec) *cobra.Command {
return &cobra.Command{
Use: "setcool [answer]",
Short: "You're so cool, tell us what is cool!",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
txCtx := authctx.NewTxContextFromCLI().WithCodec(cdc)
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithLogger(os.Stdout).
WithAccountDecoder(authcmd.GetAccountDecoder(cdc))
// get the from address from the name flag
from, err := ctx.GetFromAddress()
from, err := cliCtx.GetFromAddress()
if err != nil {
return err
}
// get account name
name := viper.GetString(client.FlagName)
// create the message
msg := cool.NewMsgSetTrend(from, args[0])
// build and sign the transaction, then broadcast to Tendermint
err = ctx.EnsureSignBuildBroadcast(name, []sdk.Msg{msg}, cdc)
if err != nil {
return err
}
return nil
return utils.SendTx(txCtx, cliCtx, []sdk.Msg{msg})
},
}
}
+16 -16
View File
@@ -1,16 +1,18 @@
package cli
import (
"os"
"strconv"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/examples/democoin/x/pow"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
authctx "github.com/cosmos/cosmos-sdk/x/auth/client/context"
"github.com/spf13/cobra"
)
// command to mine some pow!
@@ -20,9 +22,13 @@ func MineCmd(cdc *wire.Codec) *cobra.Command {
Short: "Mine some coins with proof-of-work!",
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
txCtx := authctx.NewTxContextFromCLI().WithCodec(cdc)
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithLogger(os.Stdout).
WithAccountDecoder(authcmd.GetAccountDecoder(cdc))
from, err := ctx.GetFromAddress()
from, err := cliCtx.GetFromAddress()
if err != nil {
return err
}
@@ -31,29 +37,23 @@ func MineCmd(cdc *wire.Codec) *cobra.Command {
if err != nil {
return err
}
count, err := strconv.ParseUint(args[1], 0, 64)
if err != nil {
return err
}
nonce, err := strconv.ParseUint(args[2], 0, 64)
if err != nil {
return err
}
solution := []byte(args[3])
msg := pow.NewMsgMine(from, difficulty, count, nonce, solution)
// get account name
name := ctx.FromAddressName
// build and sign the transaction, then broadcast to Tendermint
err = ctx.EnsureSignBuildBroadcast(name, []sdk.Msg{msg}, cdc)
if err != nil {
return err
}
return nil
// Build and sign the transaction, then broadcast to a Tendermint
// node.
return utils.SendTx(txCtx, cliCtx, []sdk.Msg{msg})
},
}
}
@@ -3,18 +3,20 @@ package cli
import (
"encoding/hex"
"fmt"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/spf13/viper"
"os"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/examples/democoin/x/simplestake"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
authctx "github.com/cosmos/cosmos-sdk/x/auth/client/context"
"github.com/cosmos/cosmos-sdk/examples/democoin/x/simplestake"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/crypto/ed25519"
)
const (
@@ -28,9 +30,13 @@ func BondTxCmd(cdc *wire.Codec) *cobra.Command {
Use: "bond",
Short: "Bond to a validator",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewCoreContextFromViper()
txCtx := authctx.NewTxContextFromCLI().WithCodec(cdc)
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithLogger(os.Stdout).
WithAccountDecoder(authcmd.GetAccountDecoder(cdc))
from, err := ctx.GetFromAddress()
from, err := cliCtx.GetFromAddress()
if err != nil {
return err
}
@@ -60,11 +66,15 @@ func BondTxCmd(cdc *wire.Codec) *cobra.Command {
msg := simplestake.NewMsgBond(from, stake, pubKeyEd)
return sendMsg(cdc, msg)
// Build and sign the transaction, then broadcast to a Tendermint
// node.
return utils.SendTx(txCtx, cliCtx, []sdk.Msg{msg})
},
}
cmd.Flags().String(flagStake, "", "Amount of coins to stake")
cmd.Flags().String(flagValidator, "", "Validator address to stake")
return cmd
}
@@ -74,23 +84,23 @@ func UnbondTxCmd(cdc *wire.Codec) *cobra.Command {
Use: "unbond",
Short: "Unbond from a validator",
RunE: func(cmd *cobra.Command, args []string) error {
from, err := context.NewCoreContextFromViper().GetFromAddress()
txCtx := authctx.NewTxContextFromCLI().WithCodec(cdc)
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithLogger(os.Stdout)
from, err := cliCtx.GetFromAddress()
if err != nil {
return err
}
msg := simplestake.NewMsgUnbond(from)
return sendMsg(cdc, msg)
// Build and sign the transaction, then broadcast to a Tendermint
// node.
return utils.SendTx(txCtx, cliCtx, []sdk.Msg{msg})
},
}
return cmd
}
func sendMsg(cdc *wire.Codec, msg sdk.Msg) error {
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
err := ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, []sdk.Msg{msg}, cdc)
if err != nil {
return err
}
return nil
}