tendermint/blocks/tx.go

247 lines
5.1 KiB
Go
Raw Normal View History

2014-06-05 02:34:45 -07:00
package blocks
import (
2014-07-01 14:50:24 -07:00
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
"io"
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
5. Timeout Validator times out
6. Dupeout Validator dupes out (signs twice)
2014-06-05 02:34:45 -07:00
*/
type Tx interface {
Type() byte
//IsValidation() bool
2014-07-01 14:50:24 -07:00
Binary
2014-06-05 02:34:45 -07:00
}
const (
2014-09-10 02:43:16 -07:00
// Account transactions
2014-10-04 19:16:49 -07:00
txTypeSend = byte(0x01)
txTypeName = byte(0x02)
2014-09-10 02:43:16 -07:00
// Validation transactions
2014-10-04 19:16:49 -07:00
txTypeBond = byte(0x11)
txTypeUnbond = byte(0x12)
txTypeTimeout = byte(0x13)
txTypeDupeout = byte(0x14)
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-04 19:16:49 -07:00
case txTypeSend:
2014-07-01 14:50:24 -07:00
return &SendTx{
BaseTx: ReadBaseTx(r, n, err),
Fee: ReadUInt64(r, n, err),
To: ReadUInt64(r, n, err),
Amount: ReadUInt64(r, n, err),
2014-07-01 14:50:24 -07:00
}
2014-10-04 19:16:49 -07:00
case txTypeName:
2014-07-01 14:50:24 -07:00
return &NameTx{
BaseTx: ReadBaseTx(r, n, err),
Fee: ReadUInt64(r, n, err),
Name: ReadString(r, n, err),
PubKey: ReadByteSlice(r, n, err),
2014-07-01 14:50:24 -07:00
}
2014-10-04 19:16:49 -07:00
case txTypeBond:
2014-09-10 02:43:16 -07:00
return &BondTx{
BaseTx: ReadBaseTx(r, n, err),
Fee: ReadUInt64(r, n, err),
UnbondTo: ReadUInt64(r, n, err),
Amount: ReadUInt64(r, n, err),
2014-09-10 02:43:16 -07:00
}
2014-10-04 19:16:49 -07:00
case txTypeUnbond:
2014-09-10 02:43:16 -07:00
return &UnbondTx{
BaseTx: ReadBaseTx(r, n, err),
Fee: ReadUInt64(r, n, err),
Amount: ReadUInt64(r, n, err),
2014-09-10 02:43:16 -07:00
}
2014-10-04 19:16:49 -07:00
case txTypeTimeout:
2014-09-10 02:43:16 -07:00
return &TimeoutTx{
BaseTx: ReadBaseTx(r, n, err),
2014-09-10 02:43:16 -07:00
AccountId: ReadUInt64(r, n, err),
Penalty: ReadUInt64(r, n, err),
}
2014-10-04 19:16:49 -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 {
Sequence uint64
Signature
}
func ReadBaseTx(r io.Reader, n *int64, err *error) BaseTx {
return BaseTx{
Sequence: ReadUVarInt(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)
WriteBinary(w, tx.Signature, &n, &err)
return
}
2014-10-07 19:37:20 -07:00
func (tx *BaseTx) GetSignature() Signature {
return tx.Signature
}
2014-10-07 19:37:20 -07:00
func (tx *BaseTx) SetSignature(sig Signature) {
tx.Signature = sig
}
//-----------------------------------------------------------------------------
2014-06-05 02:34:45 -07:00
type SendTx struct {
BaseTx
2014-08-31 01:48:40 -07:00
Fee uint64
To uint64
Amount uint64
2014-06-05 02:34:45 -07:00
}
func (tx *SendTx) Type() byte {
2014-10-04 19:16:49 -07:00
return txTypeSend
2014-06-05 02:34:45 -07:00
}
func (tx *SendTx) WriteTo(w io.Writer) (n int64, err error) {
WriteByte(w, tx.Type(), &n, &err)
2014-10-07 19:37:20 -07:00
WriteBinary(w, tx.BaseTx, &n, &err)
WriteUInt64(w, tx.Fee, &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
}
2014-09-10 02:43:16 -07:00
//-----------------------------------------------------------------------------
2014-06-05 02:34:45 -07:00
type NameTx struct {
BaseTx
2014-08-31 01:48:40 -07:00
Fee uint64
Name string
PubKey []byte
2014-06-05 02:34:45 -07:00
}
func (tx *NameTx) Type() byte {
2014-10-04 19:16:49 -07:00
return txTypeName
2014-06-05 02:34:45 -07:00
}
func (tx *NameTx) WriteTo(w io.Writer) (n int64, err error) {
WriteByte(w, tx.Type(), &n, &err)
2014-10-07 19:37:20 -07:00
WriteBinary(w, tx.BaseTx, &n, &err)
WriteUInt64(w, tx.Fee, &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
//-----------------------------------------------------------------------------
type BondTx struct {
BaseTx
2014-09-10 02:43:16 -07:00
Fee uint64
UnbondTo uint64
Amount uint64
}
func (tx *BondTx) Type() byte {
2014-10-04 19:16:49 -07:00
return txTypeBond
2014-09-10 02:43:16 -07:00
}
func (tx *BondTx) WriteTo(w io.Writer) (n int64, err error) {
WriteByte(w, tx.Type(), &n, &err)
2014-10-07 19:37:20 -07:00
WriteBinary(w, tx.BaseTx, &n, &err)
WriteUInt64(w, tx.Fee, &n, &err)
WriteUInt64(w, tx.UnbondTo, &n, &err)
WriteUInt64(w, tx.Amount, &n, &err)
2014-09-10 02:43:16 -07:00
return
}
//-----------------------------------------------------------------------------
type UnbondTx struct {
BaseTx
2014-09-10 02:43:16 -07:00
Fee uint64
Amount uint64
}
func (tx *UnbondTx) Type() byte {
2014-10-04 19:16:49 -07:00
return txTypeUnbond
2014-09-10 02:43:16 -07:00
}
func (tx *UnbondTx) WriteTo(w io.Writer) (n int64, err error) {
WriteByte(w, tx.Type(), &n, &err)
2014-10-07 19:37:20 -07:00
WriteBinary(w, tx.BaseTx, &n, &err)
WriteUInt64(w, tx.Fee, &n, &err)
WriteUInt64(w, tx.Amount, &n, &err)
2014-09-10 02:43:16 -07:00
return
}
//-----------------------------------------------------------------------------
type TimeoutTx struct {
BaseTx
2014-09-10 02:43:16 -07:00
AccountId uint64
Penalty uint64
}
func (tx *TimeoutTx) Type() byte {
2014-10-04 19:16:49 -07:00
return txTypeTimeout
2014-09-10 02:43:16 -07:00
}
func (tx *TimeoutTx) WriteTo(w io.Writer) (n int64, err error) {
WriteByte(w, tx.Type(), &n, &err)
2014-10-07 19:37:20 -07:00
WriteBinary(w, tx.BaseTx, &n, &err)
WriteUInt64(w, tx.AccountId, &n, &err)
WriteUInt64(w, tx.Penalty, &n, &err)
2014-09-10 02:43:16 -07:00
return
}
//-----------------------------------------------------------------------------
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) Type() byte {
2014-10-04 19:16:49 -07:00
return txTypeDupeout
2014-09-10 02:43:16 -07:00
}
func (tx *DupeoutTx) WriteTo(w io.Writer) (n int64, err error) {
WriteByte(w, tx.Type(), &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
}
2014-10-07 19:37:20 -07:00
func (tx *DupeoutTx) GenDocument() []byte {
oldSig := tx.Signature
tx.Signature = Signature{}
doc := BinaryBytes(tx)
tx.Signature = oldSig
return doc
}