Start working on apptx for counter in basecli

This commit is contained in:
Ethan Frey 2017-05-16 21:40:07 +02:00
parent 791f12624d
commit a32bf5475f
6 changed files with 95 additions and 7 deletions

View File

@ -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) {

59
cmd/basecli/apptx.go Normal file
View File

@ -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
}

24
cmd/basecli/counter.go Normal file
View File

@ -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
}

View File

@ -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{})

View File

@ -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"))

View File

@ -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) {