transaction_test_util.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package tests
  17. import (
  18. "bytes"
  19. "errors"
  20. "fmt"
  21. "math/big"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/common/hexutil"
  24. "github.com/ethereum/go-ethereum/common/math"
  25. "github.com/ethereum/go-ethereum/core/types"
  26. "github.com/ethereum/go-ethereum/params"
  27. "github.com/ethereum/go-ethereum/rlp"
  28. )
  29. // TransactionTest checks RLP decoding and sender derivation of transactions.
  30. type TransactionTest struct {
  31. json ttJSON
  32. }
  33. type ttJSON struct {
  34. BlockNumber math.HexOrDecimal64 `json:"blockNumber"`
  35. RLP hexutil.Bytes `json:"rlp"`
  36. Sender hexutil.Bytes `json:"sender"`
  37. Transaction *ttTransaction `json:"transaction"`
  38. }
  39. //go:generate gencodec -type ttTransaction -field-override ttTransactionMarshaling -out gen_tttransaction.go
  40. type ttTransaction struct {
  41. Data []byte `gencodec:"required"`
  42. GasLimit uint64 `gencodec:"required"`
  43. GasPrice *big.Int `gencodec:"required"`
  44. Nonce uint64 `gencodec:"required"`
  45. Value *big.Int `gencodec:"required"`
  46. R *big.Int `gencodec:"required"`
  47. S *big.Int `gencodec:"required"`
  48. V *big.Int `gencodec:"required"`
  49. To common.Address `gencodec:"required"`
  50. }
  51. type ttTransactionMarshaling struct {
  52. Data hexutil.Bytes
  53. GasLimit math.HexOrDecimal64
  54. GasPrice *math.HexOrDecimal256
  55. Nonce math.HexOrDecimal64
  56. Value *math.HexOrDecimal256
  57. R *math.HexOrDecimal256
  58. S *math.HexOrDecimal256
  59. V *math.HexOrDecimal256
  60. }
  61. func (tt *TransactionTest) Run(config *params.ChainConfig) error {
  62. tx := new(types.Transaction)
  63. if err := rlp.DecodeBytes(tt.json.RLP, tx); err != nil {
  64. if tt.json.Transaction == nil {
  65. return nil
  66. }
  67. return fmt.Errorf("RLP decoding failed: %v", err)
  68. }
  69. // Check sender derivation.
  70. signer := types.MakeSigner(config, new(big.Int).SetUint64(uint64(tt.json.BlockNumber)))
  71. sender, err := types.Sender(signer, tx)
  72. if err != nil {
  73. return err
  74. }
  75. if sender != common.BytesToAddress(tt.json.Sender) {
  76. return fmt.Errorf("Sender mismatch: got %x, want %x", sender, tt.json.Sender)
  77. }
  78. // Check decoded fields.
  79. err = tt.json.Transaction.verify(signer, tx)
  80. if tt.json.Sender == nil && err == nil {
  81. return errors.New("field validations succeeded but should fail")
  82. }
  83. if tt.json.Sender != nil && err != nil {
  84. return fmt.Errorf("field validations failed after RLP decoding: %s", err)
  85. }
  86. return nil
  87. }
  88. func (tt *ttTransaction) verify(signer types.Signer, tx *types.Transaction) error {
  89. if !bytes.Equal(tx.Data(), tt.Data) {
  90. return fmt.Errorf("Tx input data mismatch: got %x want %x", tx.Data(), tt.Data)
  91. }
  92. if tx.Gas() != tt.GasLimit {
  93. return fmt.Errorf("GasLimit mismatch: got %d, want %d", tx.Gas(), tt.GasLimit)
  94. }
  95. if tx.GasPrice().Cmp(tt.GasPrice) != 0 {
  96. return fmt.Errorf("GasPrice mismatch: got %v, want %v", tx.GasPrice(), tt.GasPrice)
  97. }
  98. if tx.Nonce() != tt.Nonce {
  99. return fmt.Errorf("Nonce mismatch: got %v, want %v", tx.Nonce(), tt.Nonce)
  100. }
  101. v, r, s := tx.RawSignatureValues()
  102. if r.Cmp(tt.R) != 0 {
  103. return fmt.Errorf("R mismatch: got %v, want %v", r, tt.R)
  104. }
  105. if s.Cmp(tt.S) != 0 {
  106. return fmt.Errorf("S mismatch: got %v, want %v", s, tt.S)
  107. }
  108. if v.Cmp(tt.V) != 0 {
  109. return fmt.Errorf("V mismatch: got %v, want %v", v, tt.V)
  110. }
  111. if tx.To() == nil {
  112. if tt.To != (common.Address{}) {
  113. return fmt.Errorf("To mismatch when recipient is nil (contract creation): %x", tt.To)
  114. }
  115. } else if *tx.To() != tt.To {
  116. return fmt.Errorf("To mismatch: got %x, want %x", *tx.To(), tt.To)
  117. }
  118. if tx.Value().Cmp(tt.Value) != 0 {
  119. return fmt.Errorf("Value mismatch: got %x, want %x", tx.Value(), tt.Value)
  120. }
  121. return nil
  122. }