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
5.4 KiB

10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
  1. package block
  2. import (
  3. "errors"
  4. "io"
  5. "reflect"
  6. . "github.com/tendermint/tendermint/account"
  7. . "github.com/tendermint/tendermint/binary"
  8. . "github.com/tendermint/tendermint/common"
  9. )
  10. var (
  11. ErrTxInvalidAddress = errors.New("Error invalid address")
  12. ErrTxDuplicateAddress = errors.New("Error duplicate address")
  13. ErrTxInvalidAmount = errors.New("Error invalid amount")
  14. ErrTxInsufficientFunds = errors.New("Error insufficient funds")
  15. ErrTxUnknownPubKey = errors.New("Error unknown pubkey")
  16. ErrTxInvalidPubKey = errors.New("Error invalid pubkey")
  17. ErrTxRedeclaredPubKey = errors.New("Error redeclared pubkey")
  18. ErrTxInvalidSignature = errors.New("Error invalid signature")
  19. ErrTxInvalidSequence = errors.New("Error invalid sequence")
  20. )
  21. /*
  22. Tx (Transaction) is an atomic operation on the ledger state.
  23. Account Txs:
  24. - SendTx Send coins to address
  25. Validation Txs:
  26. - BondTx New validator posts a bond
  27. - UnbondTx Validator leaves
  28. - DupeoutTx Validator dupes out (equivocates)
  29. */
  30. type Tx interface {
  31. WriteSignBytes(w io.Writer, n *int64, err *error)
  32. }
  33. // Types of Tx implementations
  34. const (
  35. // Account transactions
  36. TxTypeSend = byte(0x01)
  37. // Validation transactions
  38. TxTypeBond = byte(0x11)
  39. TxTypeUnbond = byte(0x12)
  40. TxTypeRebond = byte(0x13)
  41. TxTypeDupeout = byte(0x14)
  42. )
  43. //-------------------------------------
  44. // for binary.readReflect
  45. func TxDecoder(r Unreader, n *int64, err *error) interface{} {
  46. switch t := PeekByte(r, n, err); t {
  47. case TxTypeSend:
  48. return ReadBinary(&SendTx{}, r, n, err)
  49. case TxTypeBond:
  50. return ReadBinary(&BondTx{}, r, n, err)
  51. case TxTypeUnbond:
  52. return ReadBinary(&UnbondTx{}, r, n, err)
  53. case TxTypeRebond:
  54. return ReadBinary(&RebondTx{}, r, n, err)
  55. case TxTypeDupeout:
  56. return ReadBinary(&DupeoutTx{}, r, n, err)
  57. default:
  58. *err = Errorf("Unknown Tx type %X", t)
  59. return nil
  60. }
  61. }
  62. var _ = RegisterType(&TypeInfo{
  63. Type: reflect.TypeOf((*Tx)(nil)).Elem(),
  64. Decoder: TxDecoder,
  65. })
  66. //-----------------------------------------------------------------------------
  67. type TxInput struct {
  68. Address []byte // Hash of the PubKey
  69. Amount uint64 // Must not exceed account balance
  70. Sequence uint // Must be 1 greater than the last committed TxInput
  71. Signature Signature // Depends on the PubKey type and the whole Tx
  72. PubKey PubKey // Must not be nil, may be PubKeyNil.
  73. }
  74. func (txIn *TxInput) ValidateBasic() error {
  75. if len(txIn.Address) != 20 {
  76. return ErrTxInvalidAddress
  77. }
  78. if txIn.Amount == 0 {
  79. return ErrTxInvalidAmount
  80. }
  81. return nil
  82. }
  83. func (txIn *TxInput) WriteSignBytes(w io.Writer, n *int64, err *error) {
  84. WriteByteSlice(txIn.Address, w, n, err)
  85. WriteUint64(txIn.Amount, w, n, err)
  86. WriteUvarint(txIn.Sequence, w, n, err)
  87. }
  88. //-----------------------------------------------------------------------------
  89. type TxOutput struct {
  90. Address []byte // Hash of the PubKey
  91. Amount uint64 // The sum of all outputs must not exceed the inputs.
  92. }
  93. func (txOut *TxOutput) ValidateBasic() error {
  94. if len(txOut.Address) != 20 {
  95. return ErrTxInvalidAddress
  96. }
  97. if txOut.Amount == 0 {
  98. return ErrTxInvalidAmount
  99. }
  100. return nil
  101. }
  102. func (txOut *TxOutput) WriteSignBytes(w io.Writer, n *int64, err *error) {
  103. WriteByteSlice(txOut.Address, w, n, err)
  104. WriteUint64(txOut.Amount, w, n, err)
  105. }
  106. //-----------------------------------------------------------------------------
  107. type SendTx struct {
  108. Inputs []*TxInput
  109. Outputs []*TxOutput
  110. }
  111. func (tx *SendTx) TypeByte() byte { return TxTypeSend }
  112. func (tx *SendTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
  113. WriteUvarint(uint(len(tx.Inputs)), w, n, err)
  114. for _, in := range tx.Inputs {
  115. in.WriteSignBytes(w, n, err)
  116. }
  117. WriteUvarint(uint(len(tx.Outputs)), w, n, err)
  118. for _, out := range tx.Outputs {
  119. out.WriteSignBytes(w, n, err)
  120. }
  121. }
  122. //-----------------------------------------------------------------------------
  123. type BondTx struct {
  124. PubKey PubKeyEd25519
  125. Inputs []*TxInput
  126. UnbondTo []*TxOutput
  127. }
  128. func (tx *BondTx) TypeByte() byte { return TxTypeBond }
  129. func (tx *BondTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
  130. WriteBinary(tx.PubKey, w, n, err)
  131. WriteUvarint(uint(len(tx.Inputs)), w, n, err)
  132. for _, in := range tx.Inputs {
  133. in.WriteSignBytes(w, n, err)
  134. }
  135. WriteUvarint(uint(len(tx.UnbondTo)), w, n, err)
  136. for _, out := range tx.UnbondTo {
  137. out.WriteSignBytes(w, n, err)
  138. }
  139. }
  140. //-----------------------------------------------------------------------------
  141. type UnbondTx struct {
  142. Address []byte
  143. Height uint
  144. Signature SignatureEd25519
  145. }
  146. func (tx *UnbondTx) TypeByte() byte { return TxTypeUnbond }
  147. func (tx *UnbondTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
  148. WriteByteSlice(tx.Address, w, n, err)
  149. WriteUvarint(tx.Height, w, n, err)
  150. }
  151. //-----------------------------------------------------------------------------
  152. type RebondTx struct {
  153. Address []byte
  154. Height uint
  155. Signature SignatureEd25519
  156. }
  157. func (tx *RebondTx) TypeByte() byte { return TxTypeRebond }
  158. func (tx *RebondTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
  159. WriteByteSlice(tx.Address, w, n, err)
  160. WriteUvarint(tx.Height, w, n, err)
  161. }
  162. //-----------------------------------------------------------------------------
  163. type DupeoutTx struct {
  164. Address []byte
  165. VoteA Vote
  166. VoteB Vote
  167. }
  168. func (tx *DupeoutTx) TypeByte() byte { return TxTypeDupeout }
  169. func (tx *DupeoutTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
  170. panic("DupeoutTx has no sign bytes")
  171. }