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
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Tx wire format:
|
|
|
|
|
|
|
|
|T|L...|MMM...|A...|SSS...|
|
|
|
|
|
|
|
|
T type of the tx (1 byte)
|
|
|
|
L length of M, varint encoded (1+ bytes)
|
|
|
|
M Tx bytes (L bytes)
|
|
|
|
A account number, varint encoded (1+ bytes)
|
|
|
|
S signature of all prior bytes (32 bytes)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
type Tx interface {
|
2014-09-03 20:41:57 -07:00
|
|
|
Type() byte
|
2014-07-01 14:50:24 -07:00
|
|
|
Binary
|
2014-06-05 02:34:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2014-09-03 20:41:57 -07:00
|
|
|
TX_TYPE_SEND = byte(0x01)
|
|
|
|
TX_TYPE_NAME = byte(0x02)
|
2014-06-05 02:34:45 -07:00
|
|
|
)
|
|
|
|
|
2014-09-03 20:41:57 -07:00
|
|
|
func ReadTx(r io.Reader, n *int64, err *error) Tx {
|
|
|
|
switch t := ReadByte(r, n, err); t {
|
2014-07-01 14:50:24 -07:00
|
|
|
case TX_TYPE_SEND:
|
|
|
|
return &SendTx{
|
2014-09-03 20:41:57 -07:00
|
|
|
Fee: ReadUInt64(r, n, err),
|
|
|
|
To: ReadUInt64(r, n, err),
|
|
|
|
Amount: ReadUInt64(r, n, err),
|
|
|
|
Signature: ReadSignature(r, n, err),
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
case TX_TYPE_NAME:
|
|
|
|
return &NameTx{
|
2014-09-03 20:41:57 -07:00
|
|
|
Fee: ReadUInt64(r, n, err),
|
|
|
|
Name: ReadString(r, n, err),
|
|
|
|
PubKey: ReadByteSlice(r, n, err),
|
|
|
|
Signature: ReadSignature(r, n, err),
|
2014-07-01 14:50:24 -07:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
Panicf("Unknown Tx type %x", t)
|
|
|
|
return nil
|
|
|
|
}
|
2014-06-05 02:34:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* SendTx < Tx */
|
|
|
|
|
|
|
|
type SendTx struct {
|
2014-08-31 01:48:40 -07:00
|
|
|
Fee uint64
|
|
|
|
To uint64
|
|
|
|
Amount uint64
|
2014-07-01 14:50:24 -07:00
|
|
|
Signature
|
2014-06-05 02:34:45 -07:00
|
|
|
}
|
|
|
|
|
2014-09-03 20:41:57 -07:00
|
|
|
func (self *SendTx) Type() byte {
|
2014-07-01 14:50:24 -07:00
|
|
|
return TX_TYPE_SEND
|
2014-06-05 02:34:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *SendTx) WriteTo(w io.Writer) (n int64, err error) {
|
2014-09-03 20:41:57 -07:00
|
|
|
WriteByte(w, self.Type(), &n, &err)
|
|
|
|
WriteUInt64(w, self.Fee, &n, &err)
|
|
|
|
WriteUInt64(w, self.To, &n, &err)
|
|
|
|
WriteUInt64(w, self.Amount, &n, &err)
|
|
|
|
WriteBinary(w, self.Signature, &n, &err)
|
2014-07-01 14:50:24 -07:00
|
|
|
return
|
2014-06-05 02:34:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* NameTx < Tx */
|
|
|
|
|
|
|
|
type NameTx struct {
|
2014-08-31 01:48:40 -07:00
|
|
|
Fee uint64
|
2014-09-03 20:41:57 -07:00
|
|
|
Name string
|
|
|
|
PubKey []byte
|
2014-07-01 14:50:24 -07:00
|
|
|
Signature
|
2014-06-05 02:34:45 -07:00
|
|
|
}
|
|
|
|
|
2014-09-03 20:41:57 -07:00
|
|
|
func (self *NameTx) Type() byte {
|
2014-07-01 14:50:24 -07:00
|
|
|
return TX_TYPE_NAME
|
2014-06-05 02:34:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *NameTx) WriteTo(w io.Writer) (n int64, err error) {
|
2014-09-03 20:41:57 -07:00
|
|
|
WriteByte(w, self.Type(), &n, &err)
|
|
|
|
WriteUInt64(w, self.Fee, &n, &err)
|
|
|
|
WriteString(w, self.Name, &n, &err)
|
|
|
|
WriteByteSlice(w, self.PubKey, &n, &err)
|
|
|
|
WriteBinary(w, self.Signature, &n, &err)
|
2014-07-01 14:50:24 -07:00
|
|
|
return
|
2014-06-05 02:34:45 -07:00
|
|
|
}
|