validation.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2019 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 fourbyte
  17. import (
  18. "bytes"
  19. "errors"
  20. "fmt"
  21. "math/big"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/signer/core/apitypes"
  24. )
  25. // ValidateTransaction does a number of checks on the supplied transaction, and
  26. // returns either a list of warnings, or an error (indicating that the transaction
  27. // should be immediately rejected).
  28. func (db *Database) ValidateTransaction(selector *string, tx *apitypes.SendTxArgs) (*apitypes.ValidationMessages, error) {
  29. messages := new(apitypes.ValidationMessages)
  30. // Prevent accidental erroneous usage of both 'input' and 'data' (show stopper)
  31. if tx.Data != nil && tx.Input != nil && !bytes.Equal(*tx.Data, *tx.Input) {
  32. return nil, errors.New(`ambiguous request: both "data" and "input" are set and are not identical`)
  33. }
  34. // Place data on 'data', and nil 'input'
  35. var data []byte
  36. if tx.Input != nil {
  37. tx.Data = tx.Input
  38. tx.Input = nil
  39. }
  40. if tx.Data != nil {
  41. data = *tx.Data
  42. }
  43. // Contract creation doesn't validate call data, handle first
  44. if tx.To == nil {
  45. // Contract creation should contain sufficient data to deploy a contract. A
  46. // typical error is omitting sender due to some quirk in the javascript call
  47. // e.g. https://github.com/ethereum/go-ethereum/issues/16106.
  48. if len(data) == 0 {
  49. // Prevent sending ether into black hole (show stopper)
  50. if tx.Value.ToInt().Cmp(big.NewInt(0)) > 0 {
  51. return nil, errors.New("transaction will create a contract with value but empty code")
  52. }
  53. // No value submitted at least, critically Warn, but don't blow up
  54. messages.Crit("Transaction will create a contract with empty code")
  55. } else if len(data) < 40 { // arbitrary heuristic limit
  56. messages.Warn(fmt.Sprintf("Transaction will create a contract, but the payload is suspiciously small (%d bytes)", len(data)))
  57. }
  58. // Method selector should be nil for contract creation
  59. if selector != nil {
  60. messages.Warn("Transaction will create a contract, but method selector supplied, indicating an intent to call a method")
  61. }
  62. return messages, nil
  63. }
  64. // Not a contract creation, validate as a plain transaction
  65. if !tx.To.ValidChecksum() {
  66. messages.Warn("Invalid checksum on recipient address")
  67. }
  68. if bytes.Equal(tx.To.Address().Bytes(), common.Address{}.Bytes()) {
  69. messages.Crit("Transaction recipient is the zero address")
  70. }
  71. switch {
  72. case tx.GasPrice == nil && tx.MaxFeePerGas == nil:
  73. messages.Crit("Neither 'gasPrice' nor 'maxFeePerGas' specified.")
  74. case tx.GasPrice == nil && tx.MaxPriorityFeePerGas == nil:
  75. messages.Crit("Neither 'gasPrice' nor 'maxPriorityFeePerGas' specified.")
  76. case tx.GasPrice != nil && tx.MaxFeePerGas != nil:
  77. messages.Crit("Both 'gasPrice' and 'maxFeePerGas' specified.")
  78. case tx.GasPrice != nil && tx.MaxPriorityFeePerGas != nil:
  79. messages.Crit("Both 'gasPrice' and 'maxPriorityFeePerGas' specified.")
  80. }
  81. // Semantic fields validated, try to make heads or tails of the call data
  82. db.ValidateCallData(selector, data, messages)
  83. return messages, nil
  84. }
  85. // ValidateCallData checks if the ABI call-data + method selector (if given) can
  86. // be parsed and seems to match.
  87. func (db *Database) ValidateCallData(selector *string, data []byte, messages *apitypes.ValidationMessages) {
  88. // If the data is empty, we have a plain value transfer, nothing more to do
  89. if len(data) == 0 {
  90. return
  91. }
  92. // Validate the call data that it has the 4byte prefix and the rest divisible by 32 bytes
  93. if len(data) < 4 {
  94. messages.Warn("Transaction data is not valid ABI (missing the 4 byte call prefix)")
  95. return
  96. }
  97. if n := len(data) - 4; n%32 != 0 {
  98. messages.Warn(fmt.Sprintf("Transaction data is not valid ABI (length should be a multiple of 32 (was %d))", n))
  99. }
  100. // If a custom method selector was provided, validate with that
  101. if selector != nil {
  102. if info, err := verifySelector(*selector, data); err != nil {
  103. messages.Warn(fmt.Sprintf("Transaction contains data, but provided ABI signature could not be matched: %v", err))
  104. } else {
  105. messages.Info(fmt.Sprintf("Transaction invokes the following method: %q", info.String()))
  106. db.AddSelector(*selector, data[:4])
  107. }
  108. return
  109. }
  110. // No method selector was provided, check the database for embedded ones
  111. embedded, err := db.Selector(data[:4])
  112. if err != nil {
  113. messages.Warn(fmt.Sprintf("Transaction contains data, but the ABI signature could not be found: %v", err))
  114. return
  115. }
  116. if info, err := verifySelector(embedded, data); err != nil {
  117. messages.Warn(fmt.Sprintf("Transaction contains data, but provided ABI signature could not be verified: %v", err))
  118. } else {
  119. messages.Info(fmt.Sprintf("Transaction invokes the following method: %q", info.String()))
  120. }
  121. }