Remove remnants of basecoin tx as ibc functions are now under relay
This commit is contained in:
parent
cb075bbb7e
commit
a57e2d34b1
|
@ -19,7 +19,6 @@ func main() {
|
|||
commands.InitCmd,
|
||||
commands.StartCmd,
|
||||
commands.RelayCmd,
|
||||
commands.TxCmd,
|
||||
commands.UnsafeResetAllCmd,
|
||||
commands.VersionCmd,
|
||||
)
|
||||
|
|
|
@ -1,87 +1,8 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/tendermint/basecoin/plugins/ibc"
|
||||
|
||||
"github.com/tendermint/go-wire"
|
||||
)
|
||||
import "github.com/tendermint/basecoin/plugins/ibc"
|
||||
|
||||
// returns a new IBC plugin to be registered with Basecoin
|
||||
func NewIBCPlugin() *ibc.IBCPlugin {
|
||||
return ibc.New()
|
||||
}
|
||||
|
||||
//commands
|
||||
var (
|
||||
IBCTxCmd = &cobra.Command{
|
||||
Use: "ibc",
|
||||
Short: "An IBC transaction, for InterBlockchain Communication",
|
||||
}
|
||||
|
||||
IBCRegisterTxCmd = &cobra.Command{
|
||||
Use: "register",
|
||||
Short: "Register a blockchain via IBC",
|
||||
RunE: ibcRegisterTxCmd,
|
||||
}
|
||||
)
|
||||
|
||||
//flags
|
||||
var (
|
||||
ibcChainIDFlag string
|
||||
ibcGenesisFlag string
|
||||
)
|
||||
|
||||
func init() {
|
||||
// register flags
|
||||
registerFlags := []Flag2Register{
|
||||
{&ibcChainIDFlag, "ibc_chain_id", "", "ChainID for the new blockchain"},
|
||||
{&ibcGenesisFlag, "genesis", "", "Genesis file for the new blockchain"},
|
||||
}
|
||||
|
||||
RegisterFlags(IBCRegisterTxCmd, registerFlags)
|
||||
|
||||
//register commands
|
||||
IBCTxCmd.AddCommand(IBCRegisterTxCmd)
|
||||
RegisterTxSubcommand(IBCTxCmd)
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// ibc command implementations
|
||||
|
||||
func ibcRegisterTxCmd(cmd *cobra.Command, args []string) error {
|
||||
chainID := ibcChainIDFlag
|
||||
genesisFile := ibcGenesisFlag
|
||||
|
||||
genesisBytes, err := ioutil.ReadFile(genesisFile)
|
||||
if err != nil {
|
||||
return errors.Errorf("Error reading genesis file %v: %v\n", genesisFile, err)
|
||||
}
|
||||
|
||||
ibcTx := ibc.IBCRegisterChainTx{
|
||||
ibc.BlockchainGenesis{
|
||||
ChainID: chainID,
|
||||
Genesis: string(genesisBytes),
|
||||
},
|
||||
}
|
||||
|
||||
out, err := json.Marshal(ibcTx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("IBCTx: %s\n", string(out))
|
||||
|
||||
data := []byte(wire.BinaryBytes(struct {
|
||||
ibc.IBCTx `json:"unwrap"`
|
||||
}{ibcTx}))
|
||||
name := "IBC"
|
||||
|
||||
return AppTx(name, data)
|
||||
}
|
||||
|
|
|
@ -19,11 +19,6 @@ func RegisterStartPlugin(name string, newPlugin func() types.Plugin) {
|
|||
plugins = append(plugins, plugin{name: name, newPlugin: newPlugin})
|
||||
}
|
||||
|
||||
// Register a subcommand of TxCmd to craft transactions for plugins
|
||||
func RegisterTxSubcommand(cmd *cobra.Command) {
|
||||
TxCmd.AddCommand(cmd)
|
||||
}
|
||||
|
||||
//Returns a version command based on version input
|
||||
func QuickVersionCmd(version string) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
|
|
|
@ -118,7 +118,7 @@ func loop(addr1, addr2, id1, id2 string) {
|
|||
nextSeq := 0
|
||||
|
||||
// load the priv key
|
||||
privKey, err := LoadKey(fromFlag)
|
||||
privKey, err := LoadKey(fromFileFlag)
|
||||
if err != nil {
|
||||
logger.Error(err.Error())
|
||||
cmn.PanicCrisis(err.Error())
|
||||
|
|
|
@ -1,142 +0,0 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/tendermint/basecoin/types"
|
||||
|
||||
wire "github.com/tendermint/go-wire"
|
||||
"github.com/tendermint/tendermint/rpc/client"
|
||||
)
|
||||
|
||||
//commands
|
||||
var (
|
||||
TxCmd = &cobra.Command{
|
||||
Use: "tx",
|
||||
Short: "Create, sign, and broadcast a transaction",
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
//persistent flags
|
||||
txNodeFlag string
|
||||
amountFlag string
|
||||
fromFlag string
|
||||
seqFlag int
|
||||
gasFlag int
|
||||
feeFlag string
|
||||
chainIDFlag string
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
// register flags
|
||||
cmdTxFlags := []Flag2Register{
|
||||
{&txNodeFlag, "node", "tcp://localhost:46657", "Tendermint RPC address"},
|
||||
{&chainIDFlag, "chain_id", "test_chain_id", "ID of the chain for replay protection"},
|
||||
{&fromFlag, "from", "key.json", "Path to a private key to sign the transaction"},
|
||||
{&amountFlag, "amount", "", "Coins to send in transaction of the format <amt><coin>,<amt2><coin2>,... (eg: 1btc,2gold,5silver)"},
|
||||
{&gasFlag, "gas", 0, "The amount of gas for the transaction"},
|
||||
{&feeFlag, "fee", "0coin", "Coins for the transaction fee of the format <amt><coin>"},
|
||||
{&seqFlag, "sequence", -1, "Sequence number for the account (-1 to autocalculate)"},
|
||||
}
|
||||
RegisterPersistentFlags(TxCmd, cmdTxFlags)
|
||||
}
|
||||
|
||||
func AppTx(name string, data []byte) error {
|
||||
|
||||
privKey, err := LoadKey(fromFlag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sequence, err := getSeq(privKey.Address[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//parse the fee and amounts into coin types
|
||||
feeCoin, err := types.ParseCoin(feeFlag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
amountCoins, err := types.ParseCoins(amountFlag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
input := types.NewTxInput(privKey.PubKey, amountCoins, sequence)
|
||||
tx := &types.AppTx{
|
||||
Gas: int64(gasFlag),
|
||||
Fee: feeCoin,
|
||||
Name: name,
|
||||
Input: input,
|
||||
Data: data,
|
||||
}
|
||||
|
||||
tx.Input.Signature = privKey.Sign(tx.SignBytes(chainIDFlag))
|
||||
|
||||
out := wire.BinaryBytes(tx)
|
||||
fmt.Println("Signed AppTx:")
|
||||
fmt.Printf("%X\n", out)
|
||||
|
||||
data, log, err := broadcastTx(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Response: %X ; %s\n", data, log)
|
||||
return nil
|
||||
}
|
||||
|
||||
// broadcast the transaction to tendermint
|
||||
func broadcastTx(tx types.Tx) ([]byte, string, error) {
|
||||
httpClient := client.NewHTTP(txNodeFlag, "/websocket")
|
||||
// Don't you hate having to do this?
|
||||
// How many times have I lost an hour over this trick?!
|
||||
txBytes := []byte(wire.BinaryBytes(struct {
|
||||
types.Tx `json:"unwrap"`
|
||||
}{tx}))
|
||||
res, err := httpClient.BroadcastTxCommit(txBytes)
|
||||
if err != nil {
|
||||
return nil, "", errors.Errorf("Error on broadcast tx: %v", err)
|
||||
}
|
||||
|
||||
// if it fails check, we don't even get a delivertx back!
|
||||
if !res.CheckTx.Code.IsOK() {
|
||||
r := res.CheckTx
|
||||
return nil, "", errors.Errorf("BroadcastTxCommit got non-zero exit code: %v. %X; %s", r.Code, r.Data, r.Log)
|
||||
}
|
||||
|
||||
if !res.DeliverTx.Code.IsOK() {
|
||||
r := res.DeliverTx
|
||||
return nil, "", errors.Errorf("BroadcastTxCommit got non-zero exit code: %v. %X; %s", r.Code, r.Data, r.Log)
|
||||
}
|
||||
|
||||
return res.DeliverTx.Data, res.DeliverTx.Log, nil
|
||||
}
|
||||
|
||||
// if the sequence flag is set, return it;
|
||||
// else, fetch the account by querying the app and return the sequence number
|
||||
func getSeq(address []byte) (int, error) {
|
||||
if seqFlag >= 0 {
|
||||
return seqFlag, nil
|
||||
}
|
||||
|
||||
httpClient := client.NewHTTP(txNodeFlag, "/websocket")
|
||||
acc, err := getAccWithClient(httpClient, address)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return acc.Sequence + 1, nil
|
||||
}
|
||||
|
||||
func newOutput(to []byte, amount types.Coins) types.TxOutput {
|
||||
return types.TxOutput{
|
||||
Address: to,
|
||||
Coins: amount,
|
||||
}
|
||||
}
|
|
@ -1,59 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
wire "github.com/tendermint/go-wire"
|
||||
|
||||
"github.com/tendermint/basecoin/cmd/commands"
|
||||
"github.com/tendermint/basecoin/plugins/counter"
|
||||
"github.com/tendermint/basecoin/types"
|
||||
)
|
||||
|
||||
//commands
|
||||
var CounterTxCmd = &cobra.Command{
|
||||
Use: "counter",
|
||||
Short: "Create, sign, and broadcast a transaction to the counter plugin",
|
||||
RunE: counterTxCmd,
|
||||
}
|
||||
|
||||
const (
|
||||
flagValid = "valid"
|
||||
flagCountFee = "countfee"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
CounterTxCmd.Flags().Bool(flagValid, false, "Set valid field in CounterTx")
|
||||
CounterTxCmd.Flags().String(flagCountFee, "", "Coins for the counter fee of the format <amt><coin>")
|
||||
|
||||
commands.RegisterTxSubcommand(CounterTxCmd)
|
||||
commands.RegisterStartPlugin("counter", func() types.Plugin { return counter.New() })
|
||||
}
|
||||
|
||||
func counterTxCmd(cmd *cobra.Command, args []string) error {
|
||||
|
||||
countFee, err := types.ParseCoins(viper.GetString(flagCountFee))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
counterTx := counter.CounterTx{
|
||||
Valid: viper.GetBool(flagValid),
|
||||
Fee: countFee,
|
||||
}
|
||||
|
||||
out, err := json.Marshal(counterTx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("CounterTx:", string(out))
|
||||
|
||||
data := wire.BinaryBytes(counterTx)
|
||||
name := "counter"
|
||||
|
||||
return commands.AppTx(name, data)
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@ func main() {
|
|||
RootCmd.AddCommand(
|
||||
commands.InitCmd,
|
||||
commands.StartCmd,
|
||||
commands.TxCmd,
|
||||
commands.UnsafeResetAllCmd,
|
||||
commands.VersionCmd,
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue