98 lines
1.8 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
)
/*
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-07-01 14:50:24 -07:00
Type() Byte
Binary
2014-06-05 02:34:45 -07:00
}
const (
2014-07-01 14:50:24 -07:00
TX_TYPE_SEND = Byte(0x01)
TX_TYPE_NAME = Byte(0x02)
2014-06-05 02:34:45 -07:00
)
func ReadTx(r io.Reader) Tx {
2014-07-01 14:50:24 -07:00
switch t := ReadByte(r); t {
case TX_TYPE_SEND:
return &SendTx{
2014-08-31 01:48:40 -07:00
Fee: Readuint64(r),
To: Readuint64(r),
Amount: Readuint64(r),
2014-07-01 14:50:24 -07:00
Signature: ReadSignature(r),
}
case TX_TYPE_NAME:
return &NameTx{
2014-08-31 01:48:40 -07:00
Fee: Readuint64(r),
2014-07-01 14:50:24 -07:00
Name: ReadString(r),
PubKey: ReadByteSlice(r),
Signature: ReadSignature(r),
}
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
}
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-07-18 21:21:42 -07:00
n, err = WriteTo(self.Type(), w, n, err)
2014-08-31 01:48:40 -07:00
n, err = WriteTo(UInt64(self.Fee), w, n, err)
n, err = WriteTo(UInt64(self.To), w, n, err)
n, err = WriteTo(UInt64(self.Amount), w, n, err)
2014-07-18 21:21:42 -07:00
n, err = WriteTo(self.Signature, w, 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-07-01 14:50:24 -07:00
Name String
PubKey ByteSlice
Signature
2014-06-05 02:34:45 -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-07-18 21:21:42 -07:00
n, err = WriteTo(self.Type(), w, n, err)
2014-08-31 01:48:40 -07:00
n, err = WriteTo(UInt64(self.Fee), w, n, err)
2014-07-18 21:21:42 -07:00
n, err = WriteTo(self.Name, w, n, err)
n, err = WriteTo(self.PubKey, w, n, err)
n, err = WriteTo(self.Signature, w, n, err)
2014-07-01 14:50:24 -07:00
return
2014-06-05 02:34:45 -07:00
}