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.

211 lines
4.5 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
  1. package blocks
  2. import (
  3. . "github.com/tendermint/tendermint/binary"
  4. . "github.com/tendermint/tendermint/common"
  5. "io"
  6. )
  7. /*
  8. Account Txs:
  9. 1. Send Send coins to account
  10. 2. Name Associate account with a name
  11. Validation Txs:
  12. 3. Bond New validator posts a bond
  13. 4. Unbond Validator leaves
  14. 5. Timeout Validator times out
  15. 6. Dupeout Validator dupes out (signs twice)
  16. */
  17. type Tx interface {
  18. Signable
  19. GetSequence() uint64
  20. }
  21. const (
  22. // Account transactions
  23. TxTypeSend = byte(0x01)
  24. TxTypeName = byte(0x02)
  25. // Validation transactions
  26. TxTypeBond = byte(0x11)
  27. TxTypeUnbond = byte(0x12)
  28. TxTypeTimeout = byte(0x13)
  29. TxTypeDupeout = byte(0x14)
  30. )
  31. func ReadTx(r io.Reader, n *int64, err *error) Tx {
  32. switch t := ReadByte(r, n, err); t {
  33. case TxTypeSend:
  34. return &SendTx{
  35. BaseTx: ReadBaseTx(r, n, err),
  36. Fee: ReadUInt64(r, n, err),
  37. To: ReadUInt64(r, n, err),
  38. Amount: ReadUInt64(r, n, err),
  39. }
  40. case TxTypeName:
  41. return &NameTx{
  42. BaseTx: ReadBaseTx(r, n, err),
  43. Fee: ReadUInt64(r, n, err),
  44. Name: ReadString(r, n, err),
  45. PubKey: ReadByteSlice(r, n, err),
  46. }
  47. case TxTypeBond:
  48. return &BondTx{
  49. BaseTx: ReadBaseTx(r, n, err),
  50. Fee: ReadUInt64(r, n, err),
  51. UnbondTo: ReadUInt64(r, n, err),
  52. }
  53. case TxTypeUnbond:
  54. return &UnbondTx{
  55. BaseTx: ReadBaseTx(r, n, err),
  56. Fee: ReadUInt64(r, n, err),
  57. }
  58. case TxTypeTimeout:
  59. return &TimeoutTx{
  60. BaseTx: ReadBaseTx(r, n, err),
  61. AccountId: ReadUInt64(r, n, err),
  62. Penalty: ReadUInt64(r, n, err),
  63. }
  64. case TxTypeDupeout:
  65. return &DupeoutTx{
  66. BaseTx: ReadBaseTx(r, n, err),
  67. VoteA: *ReadVote(r, n, err),
  68. VoteB: *ReadVote(r, n, err),
  69. }
  70. default:
  71. *err = Errorf("Unknown Tx type %X", t)
  72. return nil
  73. }
  74. }
  75. //-----------------------------------------------------------------------------
  76. type BaseTx struct {
  77. Sequence uint64
  78. Signature
  79. }
  80. func ReadBaseTx(r io.Reader, n *int64, err *error) BaseTx {
  81. return BaseTx{
  82. Sequence: ReadUVarInt(r, n, err),
  83. Signature: ReadSignature(r, n, err),
  84. }
  85. }
  86. func (tx BaseTx) WriteTo(w io.Writer) (n int64, err error) {
  87. WriteUVarInt(w, tx.Sequence, &n, &err)
  88. WriteBinary(w, tx.Signature, &n, &err)
  89. return
  90. }
  91. func (tx *BaseTx) GetSequence() uint64 {
  92. return tx.Sequence
  93. }
  94. func (tx *BaseTx) GetSignature() Signature {
  95. return tx.Signature
  96. }
  97. func (tx *BaseTx) SetSignature(sig Signature) {
  98. tx.Signature = sig
  99. }
  100. //-----------------------------------------------------------------------------
  101. type SendTx struct {
  102. BaseTx
  103. Fee uint64
  104. To uint64
  105. Amount uint64
  106. }
  107. func (tx *SendTx) WriteTo(w io.Writer) (n int64, err error) {
  108. WriteByte(w, TxTypeSend, &n, &err)
  109. WriteBinary(w, tx.BaseTx, &n, &err)
  110. WriteUInt64(w, tx.Fee, &n, &err)
  111. WriteUInt64(w, tx.To, &n, &err)
  112. WriteUInt64(w, tx.Amount, &n, &err)
  113. return
  114. }
  115. //-----------------------------------------------------------------------------
  116. type NameTx struct {
  117. BaseTx
  118. Fee uint64
  119. Name string
  120. PubKey []byte
  121. }
  122. func (tx *NameTx) WriteTo(w io.Writer) (n int64, err error) {
  123. WriteByte(w, TxTypeName, &n, &err)
  124. WriteBinary(w, tx.BaseTx, &n, &err)
  125. WriteUInt64(w, tx.Fee, &n, &err)
  126. WriteString(w, tx.Name, &n, &err)
  127. WriteByteSlice(w, tx.PubKey, &n, &err)
  128. return
  129. }
  130. //-----------------------------------------------------------------------------
  131. type BondTx struct {
  132. BaseTx
  133. Fee uint64
  134. UnbondTo uint64
  135. }
  136. func (tx *BondTx) WriteTo(w io.Writer) (n int64, err error) {
  137. WriteByte(w, TxTypeBond, &n, &err)
  138. WriteBinary(w, tx.BaseTx, &n, &err)
  139. WriteUInt64(w, tx.Fee, &n, &err)
  140. WriteUInt64(w, tx.UnbondTo, &n, &err)
  141. return
  142. }
  143. //-----------------------------------------------------------------------------
  144. type UnbondTx struct {
  145. BaseTx
  146. Fee uint64
  147. }
  148. func (tx *UnbondTx) WriteTo(w io.Writer) (n int64, err error) {
  149. WriteByte(w, TxTypeUnbond, &n, &err)
  150. WriteBinary(w, tx.BaseTx, &n, &err)
  151. WriteUInt64(w, tx.Fee, &n, &err)
  152. return
  153. }
  154. //-----------------------------------------------------------------------------
  155. type TimeoutTx struct {
  156. BaseTx
  157. AccountId uint64
  158. Penalty uint64
  159. }
  160. func (tx *TimeoutTx) WriteTo(w io.Writer) (n int64, err error) {
  161. WriteByte(w, TxTypeTimeout, &n, &err)
  162. WriteBinary(w, tx.BaseTx, &n, &err)
  163. WriteUInt64(w, tx.AccountId, &n, &err)
  164. WriteUInt64(w, tx.Penalty, &n, &err)
  165. return
  166. }
  167. //-----------------------------------------------------------------------------
  168. type DupeoutTx struct {
  169. BaseTx
  170. VoteA Vote
  171. VoteB Vote
  172. }
  173. func (tx *DupeoutTx) WriteTo(w io.Writer) (n int64, err error) {
  174. WriteByte(w, TxTypeDupeout, &n, &err)
  175. WriteBinary(w, tx.BaseTx, &n, &err)
  176. WriteBinary(w, &tx.VoteA, &n, &err)
  177. WriteBinary(w, &tx.VoteB, &n, &err)
  178. return
  179. }