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.

240 lines
5.0 KiB

11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 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. Type() byte
  19. GetSequence() uint64
  20. GetSignature() *Signature
  21. //IsValidation() bool
  22. Binary
  23. }
  24. const (
  25. // Account transactions
  26. txTypeSend = byte(0x01)
  27. txTypeName = byte(0x02)
  28. // Validation transactions
  29. txTypeBond = byte(0x11)
  30. txTypeUnbond = byte(0x12)
  31. txTypeTimeout = byte(0x13)
  32. txTypeDupeout = byte(0x14)
  33. )
  34. func ReadTx(r io.Reader, n *int64, err *error) Tx {
  35. switch t := ReadByte(r, n, err); t {
  36. case txTypeSend:
  37. return &SendTx{
  38. BaseTx: ReadBaseTx(r, n, err),
  39. Fee: ReadUInt64(r, n, err),
  40. To: ReadUInt64(r, n, err),
  41. Amount: ReadUInt64(r, n, err),
  42. }
  43. case txTypeName:
  44. return &NameTx{
  45. BaseTx: ReadBaseTx(r, n, err),
  46. Fee: ReadUInt64(r, n, err),
  47. Name: ReadString(r, n, err),
  48. PubKey: ReadByteSlice(r, n, err),
  49. }
  50. case txTypeBond:
  51. return &BondTx{
  52. BaseTx: ReadBaseTx(r, n, err),
  53. Fee: ReadUInt64(r, n, err),
  54. UnbondTo: ReadUInt64(r, n, err),
  55. Amount: ReadUInt64(r, n, err),
  56. }
  57. case txTypeUnbond:
  58. return &UnbondTx{
  59. BaseTx: ReadBaseTx(r, n, err),
  60. Fee: ReadUInt64(r, n, err),
  61. Amount: ReadUInt64(r, n, err),
  62. }
  63. case txTypeTimeout:
  64. return &TimeoutTx{
  65. BaseTx: ReadBaseTx(r, n, err),
  66. AccountId: ReadUInt64(r, n, err),
  67. Penalty: ReadUInt64(r, n, err),
  68. }
  69. case txTypeDupeout:
  70. return &DupeoutTx{
  71. BaseTx: ReadBaseTx(r, n, err),
  72. VoteA: *ReadVote(r, n, err),
  73. VoteB: *ReadVote(r, n, err),
  74. }
  75. default:
  76. *err = Errorf("Unknown Tx type %X", t)
  77. return nil
  78. }
  79. }
  80. //-----------------------------------------------------------------------------
  81. type BaseTx struct {
  82. Sequence uint64
  83. Signature
  84. }
  85. func ReadBaseTx(r io.Reader, n *int64, err *error) BaseTx {
  86. return BaseTx{
  87. Sequence: ReadUVarInt(r, n, err),
  88. Signature: ReadSignature(r, n, err),
  89. }
  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) WriteTo(w io.Writer) (n int64, err error) {
  98. WriteUVarInt(w, tx.Sequence, &n, &err)
  99. WriteBinary(w, tx.Signature, &n, &err)
  100. return
  101. }
  102. //-----------------------------------------------------------------------------
  103. type SendTx struct {
  104. BaseTx
  105. Fee uint64
  106. To uint64
  107. Amount uint64
  108. }
  109. func (tx *SendTx) Type() byte {
  110. return txTypeSend
  111. }
  112. func (tx *SendTx) WriteTo(w io.Writer) (n int64, err error) {
  113. WriteByte(w, tx.Type(), &n, &err)
  114. WriteBinary(w, &tx.BaseTx, &n, &err)
  115. WriteUInt64(w, tx.Fee, &n, &err)
  116. WriteUInt64(w, tx.To, &n, &err)
  117. WriteUInt64(w, tx.Amount, &n, &err)
  118. return
  119. }
  120. //-----------------------------------------------------------------------------
  121. type NameTx struct {
  122. BaseTx
  123. Fee uint64
  124. Name string
  125. PubKey []byte
  126. }
  127. func (tx *NameTx) Type() byte {
  128. return txTypeName
  129. }
  130. func (tx *NameTx) WriteTo(w io.Writer) (n int64, err error) {
  131. WriteByte(w, tx.Type(), &n, &err)
  132. WriteBinary(w, &tx.BaseTx, &n, &err)
  133. WriteUInt64(w, tx.Fee, &n, &err)
  134. WriteString(w, tx.Name, &n, &err)
  135. WriteByteSlice(w, tx.PubKey, &n, &err)
  136. return
  137. }
  138. //-----------------------------------------------------------------------------
  139. type BondTx struct {
  140. BaseTx
  141. Fee uint64
  142. UnbondTo uint64
  143. Amount uint64
  144. }
  145. func (tx *BondTx) Type() byte {
  146. return txTypeBond
  147. }
  148. func (tx *BondTx) WriteTo(w io.Writer) (n int64, err error) {
  149. WriteByte(w, tx.Type(), &n, &err)
  150. WriteBinary(w, &tx.BaseTx, &n, &err)
  151. WriteUInt64(w, tx.Fee, &n, &err)
  152. WriteUInt64(w, tx.UnbondTo, &n, &err)
  153. WriteUInt64(w, tx.Amount, &n, &err)
  154. return
  155. }
  156. //-----------------------------------------------------------------------------
  157. type UnbondTx struct {
  158. BaseTx
  159. Fee uint64
  160. Amount uint64
  161. }
  162. func (tx *UnbondTx) Type() byte {
  163. return txTypeUnbond
  164. }
  165. func (tx *UnbondTx) WriteTo(w io.Writer) (n int64, err error) {
  166. WriteByte(w, tx.Type(), &n, &err)
  167. WriteBinary(w, &tx.BaseTx, &n, &err)
  168. WriteUInt64(w, tx.Fee, &n, &err)
  169. WriteUInt64(w, tx.Amount, &n, &err)
  170. return
  171. }
  172. //-----------------------------------------------------------------------------
  173. type TimeoutTx struct {
  174. BaseTx
  175. AccountId uint64
  176. Penalty uint64
  177. }
  178. func (tx *TimeoutTx) Type() byte {
  179. return txTypeTimeout
  180. }
  181. func (tx *TimeoutTx) WriteTo(w io.Writer) (n int64, err error) {
  182. WriteByte(w, tx.Type(), &n, &err)
  183. WriteBinary(w, &tx.BaseTx, &n, &err)
  184. WriteUInt64(w, tx.AccountId, &n, &err)
  185. WriteUInt64(w, tx.Penalty, &n, &err)
  186. return
  187. }
  188. //-----------------------------------------------------------------------------
  189. type DupeoutTx struct {
  190. BaseTx
  191. VoteA Vote
  192. VoteB Vote
  193. }
  194. func (tx *DupeoutTx) Type() byte {
  195. return txTypeDupeout
  196. }
  197. func (tx *DupeoutTx) WriteTo(w io.Writer) (n int64, err error) {
  198. WriteByte(w, tx.Type(), &n, &err)
  199. WriteBinary(w, &tx.BaseTx, &n, &err)
  200. WriteBinary(w, &tx.VoteA, &n, &err)
  201. WriteBinary(w, &tx.VoteB, &n, &err)
  202. return
  203. }