auto-sequencing

This commit is contained in:
rigel rozanski
2017-07-19 00:13:39 -04:00
parent 942506c21a
commit 0a9460dc93
11 changed files with 240 additions and 213 deletions
-1
View File
@@ -15,7 +15,6 @@ var (
errNoOutputs = fmt.Errorf("No Output Coins")
errInvalidAddress = fmt.Errorf("Invalid Address")
errInvalidCoins = fmt.Errorf("Invalid Coins")
errInvalidSequence = fmt.Errorf("Invalid Sequence")
)
var (
+21 -11
View File
@@ -6,9 +6,10 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
lc "github.com/tendermint/light-client"
"github.com/tendermint/basecoin"
lcmd "github.com/tendermint/basecoin/client/commands"
proofcmd "github.com/tendermint/basecoin/client/commands/proofs"
lc "github.com/tendermint/light-client"
"github.com/tendermint/basecoin/modules/nonce"
"github.com/tendermint/basecoin/stack"
@@ -18,28 +19,37 @@ import (
var NonceQueryCmd = &cobra.Command{
Use: "nonce [address]",
Short: "Get details of a nonce sequence number, with proof",
RunE: lcmd.RequireInit(doNonceQuery),
RunE: lcmd.RequireInit(nonceQueryCmd),
}
func doNonceQuery(cmd *cobra.Command, args []string) error {
func nonceQueryCmd(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("Missing required argument [address]")
}
addr := strings.Join(args, ",")
act, err := parseActors(addr)
var signers []basecoin.Actor
signers, err := parseActors(addr)
if err != nil {
return err
}
key := stack.PrefixedKey(nonce.NameNonce, nonce.GetSeqKey(act))
var seq uint32
proof, err := proofcmd.GetAndParseAppProof(key, &seq)
if lc.IsNoDataErr(err) {
return errors.Errorf("Sequence is empty for address %s ", addr)
} else if err != nil {
seq, proof, err := doNonceQuery(signers)
if err != nil {
return err
}
return proofcmd.OutputProof(seq, proof.BlockHeight())
}
func doNonceQuery(signers []basecoin.Actor) (sequence uint32, proof lc.Proof, err error) {
key := stack.PrefixedKey(nonce.NameNonce, nonce.GetSeqKey(signers))
proof, err = proofcmd.GetAndParseAppProof(key, &sequence)
if lc.IsNoDataErr(err) {
err = errors.Errorf("Sequence is empty for key %s ", key)
}
return
}
+27 -10
View File
@@ -28,15 +28,17 @@ var _ txcmd.Wrapper = NonceWrapper{}
// the tx with this nonce. Grabs the permission from the signer,
// as we still only support single sig on the cli
func (NonceWrapper) Wrap(tx basecoin.Tx) (res basecoin.Tx, err error) {
seq, err := readSequence()
if err != nil {
return res, err
}
signers, err := readNonceKey()
if err != nil {
return res, err
}
seq, err := readSequence(signers)
if err != nil {
return res, err
}
res = nonce.NewTx(seq, signers, tx)
return
}
@@ -67,13 +69,28 @@ func parseActors(key string) (signers []basecoin.Actor, err error) {
return
}
func readSequence() (uint32, error) {
// read the sequence from the flag or query for it if flag is -1
func readSequence(signers []basecoin.Actor) (seq uint32, err error) {
//add the nonce tx layer to the tx
seq := viper.GetInt(FlagSequence)
if seq > 0 {
return uint32(seq), nil
seqFlag := viper.GetInt(FlagSequence)
switch {
case seqFlag > 0:
seq = uint32(seqFlag)
case seqFlag == -1:
//autocalculation for default sequence
seq, _, err = doNonceQuery(signers)
if err != nil {
return
}
//increase the sequence by 1!
seq++
default:
err = fmt.Errorf("sequence must be either greater than 0, or -1 for autocalculation")
}
// TODO: try to download from query..
return 0, fmt.Errorf("sequence must be greater than 0")
return
}