From a32bf5475fdbcb81aade84cf4d8c3c2e362358f7 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 16 May 2017 21:40:07 +0200 Subject: [PATCH] Start working on apptx for counter in basecli --- cmd/basecli/adapters.go | 8 +++--- cmd/basecli/apptx.go | 59 +++++++++++++++++++++++++++++++++++++++++ cmd/basecli/counter.go | 24 +++++++++++++++++ cmd/basecli/main.go | 1 + cmd/counter/main.go | 4 ++- types/tx.go | 6 ++--- 6 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 cmd/basecli/apptx.go create mode 100644 cmd/basecli/counter.go diff --git a/cmd/basecli/adapters.go b/cmd/basecli/adapters.go index 4398b9731d..5b4869acdc 100644 --- a/cmd/basecli/adapters.go +++ b/cmd/basecli/adapters.go @@ -8,14 +8,14 @@ import ( flag "github.com/spf13/pflag" "github.com/spf13/viper" - "github.com/tendermint/basecoin/state" - btypes "github.com/tendermint/basecoin/types" - keycmd "github.com/tendermint/go-crypto/cmd" wire "github.com/tendermint/go-wire" lightclient "github.com/tendermint/light-client" "github.com/tendermint/light-client/commands" "github.com/tendermint/light-client/proofs" + + "github.com/tendermint/basecoin/state" + btypes "github.com/tendermint/basecoin/types" ) type AccountPresenter struct{} @@ -44,6 +44,8 @@ func (_ BaseTxPresenter) ParseData(raw []byte) (interface{}, error) { return tx, err } +/******** SendTx *********/ + type SendTxMaker struct{} func (m SendTxMaker) MakeReader() (lightclient.TxReader, error) { diff --git a/cmd/basecli/apptx.go b/cmd/basecli/apptx.go new file mode 100644 index 0000000000..9c9523fc47 --- /dev/null +++ b/cmd/basecli/apptx.go @@ -0,0 +1,59 @@ +package main + +import ( + "github.com/pkg/errors" + + crypto "github.com/tendermint/go-crypto" + keys "github.com/tendermint/go-crypto/keys" + wire "github.com/tendermint/go-wire" + + bc "github.com/tendermint/basecoin/types" +) + +type AppTx struct { + chainID string + signers []crypto.PubKey + Tx *bc.AppTx +} + +var _ keys.Signable = &AppTx{} + +// SignBytes returned the unsigned bytes, needing a signature +func (s *AppTx) SignBytes() []byte { + return s.Tx.SignBytes(s.chainID) +} + +// Sign will add a signature and pubkey. +// +// Depending on the Signable, one may be able to call this multiple times for multisig +// Returns error if called with invalid data or too many times +func (s *AppTx) Sign(pubkey crypto.PubKey, sig crypto.Signature) error { + if len(s.signers) > 0 { + return errors.New("AppTx already signed") + } + s.Tx.SetSignature(sig) + s.signers = []crypto.PubKey{pubkey} + return nil +} + +// Signers will return the public key(s) that signed if the signature +// is valid, or an error if there is any issue with the signature, +// including if there are no signatures +func (s *AppTx) Signers() ([]crypto.PubKey, error) { + if len(s.signers) == 0 { + return nil, errors.New("No signatures on AppTx") + } + return s.signers, nil +} + +// TxBytes returns the transaction data as well as all signatures +// It should return an error if Sign was never called +func (s *AppTx) TxBytes() ([]byte, error) { + // TODO: verify it is signed + + // Code and comment from: basecoin/cmd/commands/tx.go + // Don't you hate having to do this? + // How many times have I lost an hour over this trick?! + txBytes := wire.BinaryBytes(bc.TxS{s.Tx}) + return txBytes, nil +} diff --git a/cmd/basecli/counter.go b/cmd/basecli/counter.go new file mode 100644 index 0000000000..15608b9e95 --- /dev/null +++ b/cmd/basecli/counter.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + + wire "github.com/tendermint/go-wire" + + "github.com/tendermint/basecoin/plugins/counter" +) + +type CounterPresenter struct{} + +func (_ CounterPresenter) MakeKey(str string) ([]byte, error) { + key := counter.New().StateKey() + fmt.Println(string(key)) + return key, nil +} + +func (_ CounterPresenter) ParseData(raw []byte) (interface{}, error) { + fmt.Println("Data", len(raw)) + var cp counter.CounterPluginState + err := wire.ReadBinaryBytes(raw, &cp) + return cp, err +} diff --git a/cmd/basecli/main.go b/cmd/basecli/main.go index 8b97b76b65..a8f3a8419f 100644 --- a/cmd/basecli/main.go +++ b/cmd/basecli/main.go @@ -30,6 +30,7 @@ func main() { //initialize proofs and txs proofs.StatePresenters.Register("account", AccountPresenter{}) + proofs.StatePresenters.Register("counter", CounterPresenter{}) proofs.TxPresenters.Register("base", BaseTxPresenter{}) txs.Register("send", SendTxMaker{}) diff --git a/cmd/counter/main.go b/cmd/counter/main.go index 8254fce271..083535733f 100644 --- a/cmd/counter/main.go +++ b/cmd/counter/main.go @@ -16,6 +16,7 @@ func main() { } RootCmd.AddCommand( + commands.InitCmd, commands.StartCmd, commands.TxCmd, commands.QueryCmd, @@ -23,7 +24,8 @@ func main() { commands.VerifyCmd, commands.BlockCmd, commands.AccountCmd, - commands.QuickVersionCmd("0.1.0"), + commands.UnsafeResetAllCmd, + commands.VersionCmd, ) cmd := cli.PrepareMainCmd(RootCmd, "BC", os.ExpandEnv("$HOME/.basecoin")) diff --git a/types/tx.go b/types/tx.go index 7b1b547a0b..ed3e2ca0c9 100644 --- a/types/tx.go +++ b/types/tx.go @@ -5,10 +5,10 @@ import ( "encoding/json" abci "github.com/tendermint/abci/types" - . "github.com/tendermint/tmlibs/common" "github.com/tendermint/go-crypto" - "github.com/tendermint/go-wire/data" "github.com/tendermint/go-wire" + "github.com/tendermint/go-wire/data" + . "github.com/tendermint/tmlibs/common" ) /* @@ -46,7 +46,7 @@ func init() { // TxS add json serialization to Tx type TxS struct { - Tx + Tx `json:"unwrap"` } func (p TxS) MarshalJSON() ([]byte, error) {