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.

210 lines
4.4 KiB

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