You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  1. package blocks
  2. import (
  3. . "github.com/tendermint/tendermint/common"
  4. . "github.com/tendermint/tendermint/binary"
  5. "io"
  6. )
  7. /*
  8. Tx wire format:
  9. |T|L...|MMM...|A...|SSS...|
  10. T type of the tx (1 byte)
  11. L length of M, varint encoded (1+ bytes)
  12. M Tx bytes (L bytes)
  13. A account number, varint encoded (1+ bytes)
  14. S signature of all prior bytes (32 bytes)
  15. */
  16. type Tx interface {
  17. Type() Byte
  18. Binary
  19. }
  20. const (
  21. TX_TYPE_SEND = Byte(0x01)
  22. TX_TYPE_NAME = Byte(0x02)
  23. )
  24. func ReadTx(r io.Reader) Tx {
  25. switch t := ReadByte(r); t {
  26. case TX_TYPE_SEND:
  27. return &SendTx{
  28. Fee: ReadUInt64(r),
  29. To: ReadAccountId(r),
  30. Amount: ReadUInt64(r),
  31. Signature: ReadSignature(r),
  32. }
  33. case TX_TYPE_NAME:
  34. return &NameTx{
  35. Fee: ReadUInt64(r),
  36. Name: ReadString(r),
  37. PubKey: ReadByteSlice(r),
  38. Signature: ReadSignature(r),
  39. }
  40. default:
  41. Panicf("Unknown Tx type %x", t)
  42. return nil
  43. }
  44. }
  45. /* SendTx < Tx */
  46. type SendTx struct {
  47. Fee UInt64
  48. To AccountId
  49. Amount UInt64
  50. Signature
  51. }
  52. func (self *SendTx) Type() Byte {
  53. return TX_TYPE_SEND
  54. }
  55. func (self *SendTx) WriteTo(w io.Writer) (n int64, err error) {
  56. n, err = WriteOnto(self.Type(), w, n, err)
  57. n, err = WriteOnto(self.Fee, w, n, err)
  58. n, err = WriteOnto(self.To, w, n, err)
  59. n, err = WriteOnto(self.Amount, w, n, err)
  60. n, err = WriteOnto(self.Signature, w, n, err)
  61. return
  62. }
  63. /* NameTx < Tx */
  64. type NameTx struct {
  65. Fee UInt64
  66. Name String
  67. PubKey ByteSlice
  68. Signature
  69. }
  70. func (self *NameTx) Type() Byte {
  71. return TX_TYPE_NAME
  72. }
  73. func (self *NameTx) WriteTo(w io.Writer) (n int64, err error) {
  74. n, err = WriteOnto(self.Type(), w, n, err)
  75. n, err = WriteOnto(self.Fee, w, n, err)
  76. n, err = WriteOnto(self.Name, w, n, err)
  77. n, err = WriteOnto(self.PubKey, w, n, err)
  78. n, err = WriteOnto(self.Signature, w, n, err)
  79. return
  80. }