2017-05-29 05:15:42 -07:00
|
|
|
package txs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/tendermint/basecoin"
|
|
|
|
|
|
|
|
"github.com/tendermint/basecoin/errors"
|
|
|
|
"github.com/tendermint/basecoin/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type TxInput struct {
|
2017-06-29 06:29:36 -07:00
|
|
|
Address basecoin.Actor `json:"address"`
|
|
|
|
Coins types.Coins `json:"coins"`
|
|
|
|
Sequence int `json:"sequence"` // Nonce: Must be 1 greater than the last committed TxInput
|
2017-05-29 05:15:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (txIn TxInput) ValidateBasic() error {
|
2017-06-29 06:29:36 -07:00
|
|
|
// TODO: knowledge of app-specific codings?
|
|
|
|
if txIn.Address.App == "" {
|
2017-05-29 05:15:42 -07:00
|
|
|
return errors.InvalidAddress()
|
|
|
|
}
|
|
|
|
if !txIn.Coins.IsValid() {
|
|
|
|
return errors.InvalidCoins()
|
|
|
|
}
|
|
|
|
if txIn.Coins.IsZero() {
|
|
|
|
return errors.InvalidCoins()
|
|
|
|
}
|
|
|
|
if txIn.Sequence <= 0 {
|
|
|
|
return errors.InvalidSequence()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (txIn TxInput) String() string {
|
|
|
|
return fmt.Sprintf("TxInput{%v,%v,%v}", txIn.Address, txIn.Coins, txIn.Sequence)
|
|
|
|
}
|
|
|
|
|
2017-06-29 06:29:36 -07:00
|
|
|
func NewTxInput(addr basecoin.Actor, coins types.Coins, sequence int) TxInput {
|
2017-05-29 05:15:42 -07:00
|
|
|
input := TxInput{
|
|
|
|
Address: addr,
|
|
|
|
Coins: coins,
|
|
|
|
Sequence: sequence,
|
|
|
|
}
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type TxOutput struct {
|
2017-06-29 06:29:36 -07:00
|
|
|
Address basecoin.Actor `json:"address"`
|
|
|
|
Coins types.Coins `json:"coins"`
|
2017-05-29 05:15:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (txOut TxOutput) ValidateBasic() error {
|
2017-06-29 06:29:36 -07:00
|
|
|
// TODO: knowledge of app-specific codings?
|
|
|
|
if txOut.Address.App == "" {
|
2017-05-29 05:15:42 -07:00
|
|
|
return errors.InvalidAddress()
|
|
|
|
}
|
|
|
|
if !txOut.Coins.IsValid() {
|
|
|
|
return errors.InvalidCoins()
|
|
|
|
}
|
|
|
|
if txOut.Coins.IsZero() {
|
|
|
|
return errors.InvalidCoins()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (txOut TxOutput) String() string {
|
|
|
|
return fmt.Sprintf("TxOutput{%X,%v}", txOut.Address, txOut.Coins)
|
|
|
|
}
|
|
|
|
|
2017-06-29 06:29:36 -07:00
|
|
|
func NewTxOutput(addr basecoin.Actor, coins types.Coins) TxOutput {
|
2017-05-29 05:15:42 -07:00
|
|
|
output := TxOutput{
|
|
|
|
Address: addr,
|
|
|
|
Coins: coins,
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type SendTx struct {
|
|
|
|
Inputs []TxInput `json:"inputs"`
|
|
|
|
Outputs []TxOutput `json:"outputs"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ basecoin.Tx = SendTx{}.Wrap()
|
|
|
|
|
|
|
|
func (tx SendTx) ValidateBasic() error {
|
|
|
|
// this just makes sure all the inputs and outputs are properly formatted,
|
|
|
|
// not that they actually have the money inside
|
|
|
|
if len(tx.Inputs) == 0 {
|
|
|
|
return errors.NoInputs()
|
|
|
|
}
|
|
|
|
if len(tx.Outputs) == 0 {
|
|
|
|
return errors.NoOutputs()
|
|
|
|
}
|
|
|
|
for _, in := range tx.Inputs {
|
|
|
|
if err := in.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, out := range tx.Outputs {
|
|
|
|
if err := out.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx SendTx) String() string {
|
|
|
|
return fmt.Sprintf("SendTx{%v->%v}", tx.Inputs, tx.Outputs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx SendTx) Wrap() basecoin.Tx {
|
|
|
|
return basecoin.Tx{tx}
|
|
|
|
}
|