state_processor_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "math/big"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/consensus"
  22. "github.com/ethereum/go-ethereum/consensus/ethash"
  23. "github.com/ethereum/go-ethereum/core/rawdb"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. "github.com/ethereum/go-ethereum/core/vm"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. "github.com/ethereum/go-ethereum/params"
  28. "github.com/ethereum/go-ethereum/trie"
  29. "golang.org/x/crypto/sha3"
  30. )
  31. // TestStateProcessorErrors tests the output from the 'core' errors
  32. // as defined in core/error.go. These errors are generated when the
  33. // blockchain imports bad blocks, meaning blocks which have valid headers but
  34. // contain invalid transactions
  35. func TestStateProcessorErrors(t *testing.T) {
  36. var (
  37. signer = types.HomesteadSigner{}
  38. testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  39. db = rawdb.NewMemoryDatabase()
  40. gspec = &Genesis{
  41. Config: params.TestChainConfig,
  42. }
  43. genesis = gspec.MustCommit(db)
  44. blockchain, _ = NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil, nil)
  45. )
  46. defer blockchain.Stop()
  47. var makeTx = func(nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *types.Transaction {
  48. tx, _ := types.SignTx(types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, data), signer, testKey)
  49. return tx
  50. }
  51. for i, tt := range []struct {
  52. txs []*types.Transaction
  53. want string
  54. }{
  55. {
  56. txs: []*types.Transaction{
  57. makeTx(0, common.Address{}, big.NewInt(0), params.TxGas, nil, nil),
  58. makeTx(0, common.Address{}, big.NewInt(0), params.TxGas, nil, nil),
  59. },
  60. want: "could not apply tx 1 [0x36bfa6d14f1cd35a1be8cc2322982a595fabc0e799f09c1de3bad7bd5b1f7626]: nonce too low: address 0x71562b71999873DB5b286dF957af199Ec94617F7, tx: 0 state: 1",
  61. },
  62. {
  63. txs: []*types.Transaction{
  64. makeTx(100, common.Address{}, big.NewInt(0), params.TxGas, nil, nil),
  65. },
  66. want: "could not apply tx 0 [0x51cd272d41ef6011d8138e18bf4043797aca9b713c7d39a97563f9bbe6bdbe6f]: nonce too high: address 0x71562b71999873DB5b286dF957af199Ec94617F7, tx: 100 state: 0",
  67. },
  68. {
  69. txs: []*types.Transaction{
  70. makeTx(0, common.Address{}, big.NewInt(0), 2100000000, nil, nil),
  71. },
  72. want: "could not apply tx 0 [0xa6111e2753b0495c90a4e5b709db7fadc4c3c7dc83ca5e80c2c8aedc53e6fa2c]: gas limit reached",
  73. },
  74. {
  75. txs: []*types.Transaction{
  76. makeTx(0, common.Address{}, big.NewInt(0), 21001, nil, nil),
  77. },
  78. want: "invalid gas used (remote: 0 local: 21000)", // "could not apply tx 0 [0x54c58b530824b0bb84b7a98183f08913b5d74e1cebc368515ef3c65edf8eb56a]: gas limit reached",
  79. },
  80. {
  81. txs: []*types.Transaction{
  82. makeTx(0, common.Address{}, big.NewInt(1), params.TxGas, nil, nil),
  83. },
  84. want: "could not apply tx 0 [0x3094b17498940d92b13baccf356ce8bfd6f221e926abc903d642fa1466c5b50e]: insufficient funds for transfer: address 0x71562b71999873DB5b286dF957af199Ec94617F7",
  85. },
  86. {
  87. txs: []*types.Transaction{
  88. makeTx(0, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(0xffffff), nil),
  89. },
  90. want: "could not apply tx 0 [0xaa3f7d86802b1f364576d9071bf231e31d61b392d306831ac9cf706ff5371ce0]: insufficient funds for gas * price + value: address 0x71562b71999873DB5b286dF957af199Ec94617F7 have 0 want 352321515000",
  91. },
  92. {
  93. txs: []*types.Transaction{
  94. makeTx(0, common.Address{}, big.NewInt(0), params.TxGas, nil, nil),
  95. makeTx(1, common.Address{}, big.NewInt(0), params.TxGas, nil, nil),
  96. makeTx(2, common.Address{}, big.NewInt(0), params.TxGas, nil, nil),
  97. makeTx(3, common.Address{}, big.NewInt(0), params.TxGas-1000, big.NewInt(0), nil),
  98. },
  99. want: "could not apply tx 3 [0x836fab5882205362680e49b311a20646de03b630920f18ec6ee3b111a2cf6835]: intrinsic gas too low: have 20000, want 21000",
  100. },
  101. // The last 'core' error is ErrGasUintOverflow: "gas uint64 overflow", but in order to
  102. // trigger that one, we'd have to allocate a _huge_ chunk of data, such that the
  103. // multiplication len(data) +gas_per_byte overflows uint64. Not testable at the moment
  104. } {
  105. block := GenerateBadBlock(genesis, ethash.NewFaker(), tt.txs)
  106. _, err := blockchain.InsertChain(types.Blocks{block})
  107. if err == nil {
  108. t.Fatal("block imported without errors")
  109. }
  110. if have, want := err.Error(), tt.want; have != want {
  111. t.Errorf("test %d:\nhave \"%v\"\nwant \"%v\"\n", i, have, want)
  112. }
  113. }
  114. }
  115. // GenerateBadBlock constructs a "block" which contains the transactions. The transactions are not expected to be
  116. // valid, and no proper post-state can be made. But from the perspective of the blockchain, the block is sufficiently
  117. // valid to be considered for import:
  118. // - valid pow (fake), ancestry, difficulty, gaslimit etc
  119. func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Transactions) *types.Block {
  120. header := &types.Header{
  121. ParentHash: parent.Hash(),
  122. Coinbase: parent.Coinbase(),
  123. Difficulty: engine.CalcDifficulty(&fakeChainReader{params.TestChainConfig}, parent.Time()+10, &types.Header{
  124. Number: parent.Number(),
  125. Time: parent.Time(),
  126. Difficulty: parent.Difficulty(),
  127. UncleHash: parent.UncleHash(),
  128. }),
  129. GasLimit: CalcGasLimit(parent, parent.GasLimit(), parent.GasLimit(), parent.GasLimit()),
  130. Number: new(big.Int).Add(parent.Number(), common.Big1),
  131. Time: parent.Time() + 10,
  132. UncleHash: types.EmptyUncleHash,
  133. }
  134. var receipts []*types.Receipt
  135. // The post-state result doesn't need to be correct (this is a bad block), but we do need something there
  136. // Preferably something unique. So let's use a combo of blocknum + txhash
  137. hasher := sha3.NewLegacyKeccak256()
  138. hasher.Write(header.Number.Bytes())
  139. var cumulativeGas uint64
  140. for _, tx := range txs {
  141. txh := tx.Hash()
  142. hasher.Write(txh[:])
  143. receipt := types.NewReceipt(nil, false, cumulativeGas+tx.Gas())
  144. receipt.TxHash = tx.Hash()
  145. receipt.GasUsed = tx.Gas()
  146. receipts = append(receipts, receipt)
  147. cumulativeGas += tx.Gas()
  148. }
  149. header.Root = common.BytesToHash(hasher.Sum(nil))
  150. // Assemble and return the final block for sealing
  151. return types.NewBlock(header, txs, nil, receipts, trie.NewStackTrie(nil))
  152. }