tendermint/blocks/tx.go

211 lines
4.4 KiB
Go
Raw Normal View History

2014-06-05 02:34:45 -07:00
package blocks
import (
"fmt"
"io"
2014-07-01 14:50:24 -07:00
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
2014-06-05 02:34:45 -07:00
)
/*
2014-09-10 02:43:16 -07:00
Account Txs:
1. Send Send coins to account
2. Name Associate account with a name
Validation Txs:
2014-09-10 02:43:16 -07:00
3. Bond New validator posts a bond
4. Unbond Validator leaves
2014-10-12 17:57:23 -07:00
5. Dupeout Validator dupes out (signs twice)
2014-06-05 02:34:45 -07:00
*/
type Tx interface {
2014-10-07 23:11:04 -07:00
Signable
2014-10-11 21:27:58 -07:00
GetSequence() uint
2014-10-12 21:14:10 -07:00
GetFee() uint64
String() string
2014-06-05 02:34:45 -07:00
}
const (
2014-09-10 02:43:16 -07:00
// Account transactions
2014-10-07 23:11:04 -07:00
TxTypeSend = byte(0x01)
TxTypeName = byte(0x02)
2014-09-10 02:43:16 -07:00
// Validation transactions
2014-10-07 23:11:04 -07:00
TxTypeBond = byte(0x11)
TxTypeUnbond = byte(0x12)
2014-10-12 17:57:23 -07:00
TxTypeDupeout = byte(0x13)
2014-06-05 02:34:45 -07:00
)
func ReadTx(r io.Reader, n *int64, err *error) Tx {
switch t := ReadByte(r, n, err); t {
2014-10-07 23:11:04 -07:00
case TxTypeSend:
2014-07-01 14:50:24 -07:00
return &SendTx{
BaseTx: ReadBaseTx(r, n, err),
To: ReadUInt64(r, n, err),
Amount: ReadUInt64(r, n, err),
2014-07-01 14:50:24 -07:00
}
2014-10-07 23:11:04 -07:00
case TxTypeName:
2014-07-01 14:50:24 -07:00
return &NameTx{
BaseTx: ReadBaseTx(r, n, err),
Name: ReadString(r, n, err),
PubKey: ReadByteSlice(r, n, err),
2014-07-01 14:50:24 -07:00
}
2014-10-07 23:11:04 -07:00
case TxTypeBond:
2014-09-10 02:43:16 -07:00
return &BondTx{
2014-10-12 21:14:10 -07:00
BaseTx: ReadBaseTx(r, n, err),
//UnbondTo: ReadUInt64(r, n, err),
2014-09-10 02:43:16 -07:00
}
2014-10-07 23:11:04 -07:00
case TxTypeUnbond:
2014-09-10 02:43:16 -07:00
return &UnbondTx{
BaseTx: ReadBaseTx(r, n, err),
2014-09-10 02:43:16 -07:00
}
2014-10-07 23:11:04 -07:00
case TxTypeDupeout:
2014-09-10 02:43:16 -07:00
return &DupeoutTx{
BaseTx: ReadBaseTx(r, n, err),
2014-09-14 15:37:32 -07:00
VoteA: *ReadVote(r, n, err),
VoteB: *ReadVote(r, n, err),
2014-09-10 02:43:16 -07:00
}
2014-07-01 14:50:24 -07:00
default:
2014-10-04 19:16:49 -07:00
*err = Errorf("Unknown Tx type %X", t)
2014-07-01 14:50:24 -07:00
return nil
}
2014-06-05 02:34:45 -07:00
}
2014-09-10 02:43:16 -07:00
//-----------------------------------------------------------------------------
2014-06-05 02:34:45 -07:00
type BaseTx struct {
2014-10-11 21:27:58 -07:00
Sequence uint
2014-10-12 21:14:10 -07:00
Fee uint64
Signature
}
func ReadBaseTx(r io.Reader, n *int64, err *error) BaseTx {
return BaseTx{
Sequence: ReadUVarInt(r, n, err),
2014-10-12 21:14:10 -07:00
Fee: ReadUInt64(r, n, err),
Signature: ReadSignature(r, n, err),
}
}
2014-10-07 19:37:20 -07:00
func (tx BaseTx) WriteTo(w io.Writer) (n int64, err error) {
WriteUVarInt(w, tx.Sequence, &n, &err)
2014-10-12 21:14:10 -07:00
WriteUInt64(w, tx.Fee, &n, &err)
2014-10-07 19:37:20 -07:00
WriteBinary(w, tx.Signature, &n, &err)
return
}
2014-10-11 21:27:58 -07:00
func (tx *BaseTx) GetSequence() uint {
2014-10-07 23:11:04 -07:00
return tx.Sequence
}
2014-10-07 19:37:20 -07:00
func (tx *BaseTx) GetSignature() Signature {
return tx.Signature
}
2014-10-12 21:14:10 -07:00
func (tx *BaseTx) GetFee() uint64 {
return tx.Fee
}
2014-10-07 19:37:20 -07:00
func (tx *BaseTx) SetSignature(sig Signature) {
tx.Signature = sig
}
func (tx *BaseTx) String() string {
return fmt.Sprintf("{S:%v F:%v Sig:%X}", tx.Sequence, tx.Fee, tx.Signature)
}
//-----------------------------------------------------------------------------
2014-06-05 02:34:45 -07:00
type SendTx struct {
BaseTx
2014-08-31 01:48:40 -07:00
To uint64
Amount uint64
2014-06-05 02:34:45 -07:00
}
func (tx *SendTx) WriteTo(w io.Writer) (n int64, err error) {
2014-10-07 23:11:04 -07:00
WriteByte(w, TxTypeSend, &n, &err)
2014-10-07 19:37:20 -07:00
WriteBinary(w, tx.BaseTx, &n, &err)
WriteUInt64(w, tx.To, &n, &err)
WriteUInt64(w, tx.Amount, &n, &err)
2014-07-01 14:50:24 -07:00
return
2014-06-05 02:34:45 -07:00
}
func (tx *SendTx) String() string {
return fmt.Sprintf("SendTx{%v To:%v Amount:%v}", tx.BaseTx, tx.To, tx.Amount)
}
2014-09-10 02:43:16 -07:00
//-----------------------------------------------------------------------------
2014-06-05 02:34:45 -07:00
type NameTx struct {
BaseTx
Name string
PubKey []byte
2014-06-05 02:34:45 -07:00
}
func (tx *NameTx) WriteTo(w io.Writer) (n int64, err error) {
2014-10-07 23:11:04 -07:00
WriteByte(w, TxTypeName, &n, &err)
2014-10-07 19:37:20 -07:00
WriteBinary(w, tx.BaseTx, &n, &err)
WriteString(w, tx.Name, &n, &err)
WriteByteSlice(w, tx.PubKey, &n, &err)
2014-07-01 14:50:24 -07:00
return
2014-06-05 02:34:45 -07:00
}
2014-09-10 02:43:16 -07:00
func (tx *NameTx) String() string {
return fmt.Sprintf("NameTx{%v Name:%v PubKey:%X}", tx.BaseTx, tx.Name, tx.PubKey)
}
2014-09-10 02:43:16 -07:00
//-----------------------------------------------------------------------------
type BondTx struct {
BaseTx
2014-10-12 21:14:10 -07:00
//UnbondTo uint64
2014-09-10 02:43:16 -07:00
}
func (tx *BondTx) WriteTo(w io.Writer) (n int64, err error) {
2014-10-07 23:11:04 -07:00
WriteByte(w, TxTypeBond, &n, &err)
2014-10-07 19:37:20 -07:00
WriteBinary(w, tx.BaseTx, &n, &err)
2014-10-12 21:14:10 -07:00
//WriteUInt64(w, tx.UnbondTo, &n, &err)
2014-09-10 02:43:16 -07:00
return
}
func (tx *BondTx) String() string {
return fmt.Sprintf("BondTx{%v}", tx.BaseTx)
}
2014-09-10 02:43:16 -07:00
//-----------------------------------------------------------------------------
type UnbondTx struct {
BaseTx
2014-09-10 02:43:16 -07:00
}
func (tx *UnbondTx) WriteTo(w io.Writer) (n int64, err error) {
2014-10-07 23:11:04 -07:00
WriteByte(w, TxTypeUnbond, &n, &err)
2014-10-07 19:37:20 -07:00
WriteBinary(w, tx.BaseTx, &n, &err)
2014-09-10 02:43:16 -07:00
return
}
func (tx *UnbondTx) String() string {
return fmt.Sprintf("UnbondTx{%v}", tx.BaseTx)
}
2014-09-10 02:43:16 -07:00
//-----------------------------------------------------------------------------
type DupeoutTx struct {
BaseTx
2014-09-14 15:37:32 -07:00
VoteA Vote
VoteB Vote
2014-09-10 02:43:16 -07:00
}
func (tx *DupeoutTx) WriteTo(w io.Writer) (n int64, err error) {
2014-10-07 23:11:04 -07:00
WriteByte(w, TxTypeDupeout, &n, &err)
2014-10-07 19:37:20 -07:00
WriteBinary(w, tx.BaseTx, &n, &err)
2014-09-14 15:37:32 -07:00
WriteBinary(w, &tx.VoteA, &n, &err)
WriteBinary(w, &tx.VoteB, &n, &err)
2014-09-10 02:43:16 -07:00
return
}
func (tx *DupeoutTx) String() string {
return fmt.Sprintf("DupeoutTx{%v VoteA:%v VoteB:%v}", tx.BaseTx, tx.VoteA, tx.VoteB)
}