state_processor_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // Copyright 2020 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 core
  17. import (
  18. "crypto/ecdsa"
  19. "math/big"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/common/math"
  23. "github.com/ethereum/go-ethereum/consensus"
  24. "github.com/ethereum/go-ethereum/consensus/ethash"
  25. "github.com/ethereum/go-ethereum/consensus/misc"
  26. "github.com/ethereum/go-ethereum/core/rawdb"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. "github.com/ethereum/go-ethereum/core/vm"
  29. "github.com/ethereum/go-ethereum/crypto"
  30. "github.com/ethereum/go-ethereum/params"
  31. "github.com/ethereum/go-ethereum/trie"
  32. "golang.org/x/crypto/sha3"
  33. )
  34. // TestStateProcessorErrors tests the output from the 'core' errors
  35. // as defined in core/error.go. These errors are generated when the
  36. // blockchain imports bad blocks, meaning blocks which have valid headers but
  37. // contain invalid transactions
  38. func TestStateProcessorErrors(t *testing.T) {
  39. var (
  40. config = &params.ChainConfig{
  41. ChainID: big.NewInt(1),
  42. HomesteadBlock: big.NewInt(0),
  43. EIP150Block: big.NewInt(0),
  44. EIP155Block: big.NewInt(0),
  45. EIP158Block: big.NewInt(0),
  46. ByzantiumBlock: big.NewInt(0),
  47. ConstantinopleBlock: big.NewInt(0),
  48. PetersburgBlock: big.NewInt(0),
  49. IstanbulBlock: big.NewInt(0),
  50. MuirGlacierBlock: big.NewInt(0),
  51. BerlinBlock: big.NewInt(0),
  52. LondonBlock: big.NewInt(0),
  53. Ethash: new(params.EthashConfig),
  54. }
  55. signer = types.LatestSigner(config)
  56. key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  57. key2, _ = crypto.HexToECDSA("0202020202020202020202020202020202020202020202020202002020202020")
  58. )
  59. var makeTx = func(key *ecdsa.PrivateKey, nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *types.Transaction {
  60. tx, _ := types.SignTx(types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, data), signer, key)
  61. return tx
  62. }
  63. var mkDynamicTx = func(nonce uint64, to common.Address, gasLimit uint64, gasTipCap, gasFeeCap *big.Int) *types.Transaction {
  64. tx, _ := types.SignTx(types.NewTx(&types.DynamicFeeTx{
  65. Nonce: nonce,
  66. GasTipCap: gasTipCap,
  67. GasFeeCap: gasFeeCap,
  68. Gas: gasLimit,
  69. To: &to,
  70. Value: big.NewInt(0),
  71. }), signer, key1)
  72. return tx
  73. }
  74. { // Tests against a 'recent' chain definition
  75. var (
  76. db = rawdb.NewMemoryDatabase()
  77. gspec = &Genesis{
  78. Config: config,
  79. Alloc: GenesisAlloc{
  80. common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7"): GenesisAccount{
  81. Balance: big.NewInt(1000000000000000000), // 1 ether
  82. Nonce: 0,
  83. },
  84. common.HexToAddress("0xfd0810DD14796680f72adf1a371963d0745BCc64"): GenesisAccount{
  85. Balance: big.NewInt(1000000000000000000), // 1 ether
  86. Nonce: math.MaxUint64,
  87. },
  88. },
  89. }
  90. genesis = gspec.MustCommit(db)
  91. blockchain, _ = NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil)
  92. )
  93. defer blockchain.Stop()
  94. bigNumber := new(big.Int).SetBytes(common.FromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"))
  95. tooBigNumber := new(big.Int).Set(bigNumber)
  96. tooBigNumber.Add(tooBigNumber, common.Big1)
  97. for i, tt := range []struct {
  98. txs []*types.Transaction
  99. want string
  100. }{
  101. { // ErrNonceTooLow
  102. txs: []*types.Transaction{
  103. makeTx(key1, 0, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(875000000), nil),
  104. makeTx(key1, 0, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(875000000), nil),
  105. },
  106. want: "could not apply tx 1 [0x0026256b3939ed97e2c4a6f3fce8ecf83bdcfa6d507c47838c308a1fb0436f62]: nonce too low: address 0x71562b71999873DB5b286dF957af199Ec94617F7, tx: 0 state: 1",
  107. },
  108. { // ErrNonceTooHigh
  109. txs: []*types.Transaction{
  110. makeTx(key1, 100, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(875000000), nil),
  111. },
  112. want: "could not apply tx 0 [0xdebad714ca7f363bd0d8121c4518ad48fa469ca81b0a081be3d10c17460f751b]: nonce too high: address 0x71562b71999873DB5b286dF957af199Ec94617F7, tx: 100 state: 0",
  113. },
  114. { // ErrNonceMax
  115. txs: []*types.Transaction{
  116. makeTx(key2, math.MaxUint64, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(875000000), nil),
  117. },
  118. want: "could not apply tx 0 [0x84ea18d60eb2bb3b040e3add0eb72f757727122cc257dd858c67cb6591a85986]: nonce has max value: address 0xfd0810DD14796680f72adf1a371963d0745BCc64, nonce: 18446744073709551615",
  119. },
  120. { // ErrGasLimitReached
  121. txs: []*types.Transaction{
  122. makeTx(key1, 0, common.Address{}, big.NewInt(0), 21000000, big.NewInt(875000000), nil),
  123. },
  124. want: "could not apply tx 0 [0xbd49d8dadfd47fb846986695f7d4da3f7b2c48c8da82dbc211a26eb124883de9]: gas limit reached",
  125. },
  126. { // ErrInsufficientFundsForTransfer
  127. txs: []*types.Transaction{
  128. makeTx(key1, 0, common.Address{}, big.NewInt(1000000000000000000), params.TxGas, big.NewInt(875000000), nil),
  129. },
  130. want: "could not apply tx 0 [0x98c796b470f7fcab40aaef5c965a602b0238e1034cce6fb73823042dd0638d74]: insufficient funds for gas * price + value: address 0x71562b71999873DB5b286dF957af199Ec94617F7 have 1000000000000000000 want 1000018375000000000",
  131. },
  132. { // ErrInsufficientFunds
  133. txs: []*types.Transaction{
  134. makeTx(key1, 0, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(900000000000000000), nil),
  135. },
  136. want: "could not apply tx 0 [0x4a69690c4b0cd85e64d0d9ea06302455b01e10a83db964d60281739752003440]: insufficient funds for gas * price + value: address 0x71562b71999873DB5b286dF957af199Ec94617F7 have 1000000000000000000 want 18900000000000000000000",
  137. },
  138. // ErrGasUintOverflow
  139. // One missing 'core' error is ErrGasUintOverflow: "gas uint64 overflow",
  140. // In order to trigger that one, we'd have to allocate a _huge_ chunk of data, such that the
  141. // multiplication len(data) +gas_per_byte overflows uint64. Not testable at the moment
  142. { // ErrIntrinsicGas
  143. txs: []*types.Transaction{
  144. makeTx(key1, 0, common.Address{}, big.NewInt(0), params.TxGas-1000, big.NewInt(875000000), nil),
  145. },
  146. want: "could not apply tx 0 [0xcf3b049a0b516cb4f9274b3e2a264359e2ba53b2fb64b7bda2c634d5c9d01fca]: intrinsic gas too low: have 20000, want 21000",
  147. },
  148. { // ErrGasLimitReached
  149. txs: []*types.Transaction{
  150. makeTx(key1, 0, common.Address{}, big.NewInt(0), params.TxGas*1000, big.NewInt(875000000), nil),
  151. },
  152. want: "could not apply tx 0 [0xbd49d8dadfd47fb846986695f7d4da3f7b2c48c8da82dbc211a26eb124883de9]: gas limit reached",
  153. },
  154. { // ErrFeeCapTooLow
  155. txs: []*types.Transaction{
  156. mkDynamicTx(0, common.Address{}, params.TxGas, big.NewInt(0), big.NewInt(0)),
  157. },
  158. want: "could not apply tx 0 [0xc4ab868fef0c82ae0387b742aee87907f2d0fc528fc6ea0a021459fb0fc4a4a8]: max fee per gas less than block base fee: address 0x71562b71999873DB5b286dF957af199Ec94617F7, maxFeePerGas: 0 baseFee: 875000000",
  159. },
  160. { // ErrTipVeryHigh
  161. txs: []*types.Transaction{
  162. mkDynamicTx(0, common.Address{}, params.TxGas, tooBigNumber, big.NewInt(1)),
  163. },
  164. want: "could not apply tx 0 [0x15b8391b9981f266b32f3ab7da564bbeb3d6c21628364ea9b32a21139f89f712]: max priority fee per gas higher than 2^256-1: address 0x71562b71999873DB5b286dF957af199Ec94617F7, maxPriorityFeePerGas bit length: 257",
  165. },
  166. { // ErrFeeCapVeryHigh
  167. txs: []*types.Transaction{
  168. mkDynamicTx(0, common.Address{}, params.TxGas, big.NewInt(1), tooBigNumber),
  169. },
  170. want: "could not apply tx 0 [0x48bc299b83fdb345c57478f239e89814bb3063eb4e4b49f3b6057a69255c16bd]: max fee per gas higher than 2^256-1: address 0x71562b71999873DB5b286dF957af199Ec94617F7, maxFeePerGas bit length: 257",
  171. },
  172. { // ErrTipAboveFeeCap
  173. txs: []*types.Transaction{
  174. mkDynamicTx(0, common.Address{}, params.TxGas, big.NewInt(2), big.NewInt(1)),
  175. },
  176. want: "could not apply tx 0 [0xf987a31ff0c71895780a7612f965a0c8b056deb54e020bb44fa478092f14c9b4]: max priority fee per gas higher than max fee per gas: address 0x71562b71999873DB5b286dF957af199Ec94617F7, maxPriorityFeePerGas: 2, maxFeePerGas: 1",
  177. },
  178. { // ErrInsufficientFunds
  179. // Available balance: 1000000000000000000
  180. // Effective cost: 18375000021000
  181. // FeeCap * gas: 1050000000000000000
  182. // This test is designed to have the effective cost be covered by the balance, but
  183. // the extended requirement on FeeCap*gas < balance to fail
  184. txs: []*types.Transaction{
  185. mkDynamicTx(0, common.Address{}, params.TxGas, big.NewInt(1), big.NewInt(50000000000000)),
  186. },
  187. want: "could not apply tx 0 [0x413603cd096a87f41b1660d3ed3e27d62e1da78eac138961c0a1314ed43bd129]: insufficient funds for gas * price + value: address 0x71562b71999873DB5b286dF957af199Ec94617F7 have 1000000000000000000 want 1050000000000000000",
  188. },
  189. { // Another ErrInsufficientFunds, this one to ensure that feecap/tip of max u256 is allowed
  190. txs: []*types.Transaction{
  191. mkDynamicTx(0, common.Address{}, params.TxGas, bigNumber, bigNumber),
  192. },
  193. want: "could not apply tx 0 [0xd82a0c2519acfeac9a948258c47e784acd20651d9d80f9a1c67b4137651c3a24]: insufficient funds for gas * price + value: address 0x71562b71999873DB5b286dF957af199Ec94617F7 have 1000000000000000000 want 2431633873983640103894990685182446064918669677978451844828609264166175722438635000",
  194. },
  195. } {
  196. block := GenerateBadBlock(genesis, ethash.NewFaker(), tt.txs, gspec.Config)
  197. _, err := blockchain.InsertChain(types.Blocks{block})
  198. if err == nil {
  199. t.Fatal("block imported without errors")
  200. }
  201. if have, want := err.Error(), tt.want; have != want {
  202. t.Errorf("test %d:\nhave \"%v\"\nwant \"%v\"\n", i, have, want)
  203. }
  204. }
  205. }
  206. // ErrTxTypeNotSupported, For this, we need an older chain
  207. {
  208. var (
  209. db = rawdb.NewMemoryDatabase()
  210. gspec = &Genesis{
  211. Config: &params.ChainConfig{
  212. ChainID: big.NewInt(1),
  213. HomesteadBlock: big.NewInt(0),
  214. EIP150Block: big.NewInt(0),
  215. EIP155Block: big.NewInt(0),
  216. EIP158Block: big.NewInt(0),
  217. ByzantiumBlock: big.NewInt(0),
  218. ConstantinopleBlock: big.NewInt(0),
  219. PetersburgBlock: big.NewInt(0),
  220. IstanbulBlock: big.NewInt(0),
  221. MuirGlacierBlock: big.NewInt(0),
  222. },
  223. Alloc: GenesisAlloc{
  224. common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7"): GenesisAccount{
  225. Balance: big.NewInt(1000000000000000000), // 1 ether
  226. Nonce: 0,
  227. },
  228. },
  229. }
  230. genesis = gspec.MustCommit(db)
  231. blockchain, _ = NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil)
  232. )
  233. defer blockchain.Stop()
  234. for i, tt := range []struct {
  235. txs []*types.Transaction
  236. want string
  237. }{
  238. { // ErrTxTypeNotSupported
  239. txs: []*types.Transaction{
  240. mkDynamicTx(0, common.Address{}, params.TxGas-1000, big.NewInt(0), big.NewInt(0)),
  241. },
  242. want: "could not apply tx 0 [0x88626ac0d53cb65308f2416103c62bb1f18b805573d4f96a3640bbbfff13c14f]: transaction type not supported",
  243. },
  244. } {
  245. block := GenerateBadBlock(genesis, ethash.NewFaker(), tt.txs, gspec.Config)
  246. _, err := blockchain.InsertChain(types.Blocks{block})
  247. if err == nil {
  248. t.Fatal("block imported without errors")
  249. }
  250. if have, want := err.Error(), tt.want; have != want {
  251. t.Errorf("test %d:\nhave \"%v\"\nwant \"%v\"\n", i, have, want)
  252. }
  253. }
  254. }
  255. // ErrSenderNoEOA, for this we need the sender to have contract code
  256. {
  257. var (
  258. db = rawdb.NewMemoryDatabase()
  259. gspec = &Genesis{
  260. Config: config,
  261. Alloc: GenesisAlloc{
  262. common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7"): GenesisAccount{
  263. Balance: big.NewInt(1000000000000000000), // 1 ether
  264. Nonce: 0,
  265. Code: common.FromHex("0xB0B0FACE"),
  266. },
  267. },
  268. }
  269. genesis = gspec.MustCommit(db)
  270. blockchain, _ = NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil)
  271. )
  272. defer blockchain.Stop()
  273. for i, tt := range []struct {
  274. txs []*types.Transaction
  275. want string
  276. }{
  277. { // ErrSenderNoEOA
  278. txs: []*types.Transaction{
  279. mkDynamicTx(0, common.Address{}, params.TxGas-1000, big.NewInt(0), big.NewInt(0)),
  280. },
  281. want: "could not apply tx 0 [0x88626ac0d53cb65308f2416103c62bb1f18b805573d4f96a3640bbbfff13c14f]: sender not an eoa: address 0x71562b71999873DB5b286dF957af199Ec94617F7, codehash: 0x9280914443471259d4570a8661015ae4a5b80186dbc619658fb494bebc3da3d1",
  282. },
  283. } {
  284. block := GenerateBadBlock(genesis, ethash.NewFaker(), tt.txs, gspec.Config)
  285. _, err := blockchain.InsertChain(types.Blocks{block})
  286. if err == nil {
  287. t.Fatal("block imported without errors")
  288. }
  289. if have, want := err.Error(), tt.want; have != want {
  290. t.Errorf("test %d:\nhave \"%v\"\nwant \"%v\"\n", i, have, want)
  291. }
  292. }
  293. }
  294. }
  295. // GenerateBadBlock constructs a "block" which contains the transactions. The transactions are not expected to be
  296. // valid, and no proper post-state can be made. But from the perspective of the blockchain, the block is sufficiently
  297. // valid to be considered for import:
  298. // - valid pow (fake), ancestry, difficulty, gaslimit etc
  299. func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Transactions, config *params.ChainConfig) *types.Block {
  300. header := &types.Header{
  301. ParentHash: parent.Hash(),
  302. Coinbase: parent.Coinbase(),
  303. Difficulty: engine.CalcDifficulty(&fakeChainReader{config}, parent.Time()+10, &types.Header{
  304. Number: parent.Number(),
  305. Time: parent.Time(),
  306. Difficulty: parent.Difficulty(),
  307. UncleHash: parent.UncleHash(),
  308. }),
  309. GasLimit: parent.GasLimit(),
  310. Number: new(big.Int).Add(parent.Number(), common.Big1),
  311. Time: parent.Time() + 10,
  312. UncleHash: types.EmptyUncleHash,
  313. }
  314. if config.IsLondon(header.Number) {
  315. header.BaseFee = misc.CalcBaseFee(config, parent.Header())
  316. }
  317. var receipts []*types.Receipt
  318. // The post-state result doesn't need to be correct (this is a bad block), but we do need something there
  319. // Preferably something unique. So let's use a combo of blocknum + txhash
  320. hasher := sha3.NewLegacyKeccak256()
  321. hasher.Write(header.Number.Bytes())
  322. var cumulativeGas uint64
  323. for _, tx := range txs {
  324. txh := tx.Hash()
  325. hasher.Write(txh[:])
  326. receipt := types.NewReceipt(nil, false, cumulativeGas+tx.Gas())
  327. receipt.TxHash = tx.Hash()
  328. receipt.GasUsed = tx.Gas()
  329. receipts = append(receipts, receipt)
  330. cumulativeGas += tx.Gas()
  331. }
  332. header.Root = common.BytesToHash(hasher.Sum(nil))
  333. // Assemble and return the final block for sealing
  334. return types.NewBlock(header, txs, nil, receipts, trie.NewStackTrie(nil))
  335. }