2020-12-04 11:22:19 +00:00
// Copyright 2020 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package core
import (
2021-03-16 11:44:38 +00:00
"context"
2020-12-04 11:22:19 +00:00
"math/big"
"testing"
2021-03-16 11:44:38 +00:00
"github.com/holiman/uint256"
2021-03-12 17:26:06 +00:00
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/consensus"
"github.com/ledgerwatch/turbo-geth/consensus/ethash"
"github.com/ledgerwatch/turbo-geth/core/types"
"github.com/ledgerwatch/turbo-geth/core/vm"
"github.com/ledgerwatch/turbo-geth/crypto"
2021-03-16 11:44:38 +00:00
"github.com/ledgerwatch/turbo-geth/ethdb"
2021-03-12 17:26:06 +00:00
"github.com/ledgerwatch/turbo-geth/params"
2020-12-04 11:22:19 +00:00
"golang.org/x/crypto/sha3"
)
// TestStateProcessorErrors tests the output from the 'core' errors
// as defined in core/error.go. These errors are generated when the
// blockchain imports bad blocks, meaning blocks which have valid headers but
// contain invalid transactions
func TestStateProcessorErrors ( t * testing . T ) {
var (
signer = types . HomesteadSigner { }
testKey , _ = crypto . HexToECDSA ( "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
2021-03-16 11:44:38 +00:00
db = ethdb . NewMemDatabase ( )
2020-12-04 11:22:19 +00:00
gspec = & Genesis {
Config : params . TestChainConfig ,
}
genesis = gspec . MustCommit ( db )
blockchain , _ = NewBlockChain ( db , nil , gspec . Config , ethash . NewFaker ( ) , vm . Config { } , nil , nil )
)
defer blockchain . Stop ( )
2021-03-16 11:44:38 +00:00
var makeTx = func ( nonce uint64 , to common . Address , amount * uint256 . Int , gasLimit uint64 , gasPrice * uint256 . Int , data [ ] byte ) * types . Transaction {
2020-12-04 11:22:19 +00:00
tx , _ := types . SignTx ( types . NewTransaction ( nonce , to , amount , gasLimit , gasPrice , data ) , signer , testKey )
return tx
}
2021-03-16 11:44:38 +00:00
uint256Zero := uint256 . NewInt ( )
uint256FF , _ := uint256 . FromBig ( big . NewInt ( 0xffffff ) )
2020-12-04 11:22:19 +00:00
for i , tt := range [ ] struct {
txs [ ] * types . Transaction
want string
} {
{
txs : [ ] * types . Transaction {
2021-03-16 11:44:38 +00:00
makeTx ( 0 , common . Address { } , uint256Zero , params . TxGas , nil , nil ) ,
makeTx ( 0 , common . Address { } , uint256Zero , params . TxGas , nil , nil ) ,
2020-12-04 11:22:19 +00:00
} ,
want : "could not apply tx 1 [0x36bfa6d14f1cd35a1be8cc2322982a595fabc0e799f09c1de3bad7bd5b1f7626]: nonce too low: address 0x71562b71999873DB5b286dF957af199Ec94617F7, tx: 0 state: 1" ,
} ,
{
txs : [ ] * types . Transaction {
2021-03-16 11:44:38 +00:00
makeTx ( 100 , common . Address { } , uint256Zero , params . TxGas , nil , nil ) ,
2020-12-04 11:22:19 +00:00
} ,
want : "could not apply tx 0 [0x51cd272d41ef6011d8138e18bf4043797aca9b713c7d39a97563f9bbe6bdbe6f]: nonce too high: address 0x71562b71999873DB5b286dF957af199Ec94617F7, tx: 100 state: 0" ,
} ,
{
txs : [ ] * types . Transaction {
2021-03-16 11:44:38 +00:00
makeTx ( 0 , common . Address { } , uint256Zero , 21000000 , nil , nil ) ,
2020-12-04 11:22:19 +00:00
} ,
want : "could not apply tx 0 [0x54c58b530824b0bb84b7a98183f08913b5d74e1cebc368515ef3c65edf8eb56a]: gas limit reached" ,
} ,
{
txs : [ ] * types . Transaction {
2021-03-17 11:56:39 +00:00
makeTx ( 0 , common . Address { } , uint256 . NewInt ( ) . SetUint64 ( 1 ) , params . TxGas , nil , nil ) ,
2020-12-04 11:22:19 +00:00
} ,
want : "could not apply tx 0 [0x3094b17498940d92b13baccf356ce8bfd6f221e926abc903d642fa1466c5b50e]: insufficient funds for transfer: address 0x71562b71999873DB5b286dF957af199Ec94617F7" ,
} ,
{
txs : [ ] * types . Transaction {
2021-03-16 11:44:38 +00:00
makeTx ( 0 , common . Address { } , uint256Zero , params . TxGas , uint256FF , nil ) ,
2020-12-04 11:22:19 +00:00
} ,
want : "could not apply tx 0 [0xaa3f7d86802b1f364576d9071bf231e31d61b392d306831ac9cf706ff5371ce0]: insufficient funds for gas * price + value: address 0x71562b71999873DB5b286dF957af199Ec94617F7 have 0 want 352321515000" ,
} ,
{
txs : [ ] * types . Transaction {
2021-03-16 11:44:38 +00:00
makeTx ( 0 , common . Address { } , uint256Zero , params . TxGas , nil , nil ) ,
makeTx ( 1 , common . Address { } , uint256Zero , params . TxGas , nil , nil ) ,
makeTx ( 2 , common . Address { } , uint256Zero , params . TxGas , nil , nil ) ,
makeTx ( 3 , common . Address { } , uint256Zero , params . TxGas - 1000 , uint256Zero , nil ) ,
2020-12-04 11:22:19 +00:00
} ,
want : "could not apply tx 3 [0x836fab5882205362680e49b311a20646de03b630920f18ec6ee3b111a2cf6835]: intrinsic gas too low: have 20000, want 21000" ,
} ,
// The last 'core' error is ErrGasUintOverflow: "gas uint64 overflow", but in order to
// trigger that one, we'd have to allocate a _huge_ chunk of data, such that the
// multiplication len(data) +gas_per_byte overflows uint64. Not testable at the moment
} {
block := GenerateBadBlock ( genesis , ethash . NewFaker ( ) , tt . txs )
2021-03-16 11:44:38 +00:00
_ , err := blockchain . InsertChain ( context . TODO ( ) , types . Blocks { block } )
2020-12-04 11:22:19 +00:00
if err == nil {
t . Fatal ( "block imported without errors" )
}
if have , want := err . Error ( ) , tt . want ; have != want {
t . Errorf ( "test %d:\nhave \"%v\"\nwant \"%v\"\n" , i , have , want )
}
}
}
// GenerateBadBlock constructs a "block" which contains the transactions. The transactions are not expected to be
// valid, and no proper post-state can be made. But from the perspective of the blockchain, the block is sufficiently
// valid to be considered for import:
// - valid pow (fake), ancestry, difficulty, gaslimit etc
func GenerateBadBlock ( parent * types . Block , engine consensus . Engine , txs types . Transactions ) * types . Block {
header := & types . Header {
ParentHash : parent . Hash ( ) ,
Coinbase : parent . Coinbase ( ) ,
2021-03-16 11:44:38 +00:00
Difficulty : engine . CalcDifficulty ( & fakeChainReader { params . TestChainConfig } , parent . Time ( ) + 10 ,
parent . Time ( ) , parent . Difficulty ( ) , parent . Number ( ) , parent . Hash ( ) , parent . UncleHash ( ) ) ,
2021-03-23 09:00:07 +00:00
GasLimit : CalcGasLimit ( parent . GasUsed ( ) , parent . GasLimit ( ) , parent . GasLimit ( ) , parent . GasLimit ( ) ) ,
2020-12-04 11:22:19 +00:00
Number : new ( big . Int ) . Add ( parent . Number ( ) , common . Big1 ) ,
Time : parent . Time ( ) + 10 ,
UncleHash : types . EmptyUncleHash ,
}
2021-03-18 10:44:22 +00:00
var receipts [ ] * types . Receipt //nolint:prealloc
2020-12-04 11:22:19 +00:00
// The post-state result doesn't need to be correct (this is a bad block), but we do need something there
// Preferably something unique. So let's use a combo of blocknum + txhash
hasher := sha3 . NewLegacyKeccak256 ( )
2021-03-18 06:58:14 +00:00
//nolint:errcheck
2020-12-04 11:22:19 +00:00
hasher . Write ( header . Number . Bytes ( ) )
var cumulativeGas uint64
for _ , tx := range txs {
txh := tx . Hash ( )
2021-03-18 06:58:14 +00:00
//nolint:errcheck
2020-12-04 11:22:19 +00:00
hasher . Write ( txh [ : ] )
2021-03-16 11:44:38 +00:00
receipt := types . NewReceipt ( false , cumulativeGas + tx . Gas ( ) )
2020-12-04 11:22:19 +00:00
receipt . TxHash = tx . Hash ( )
receipt . GasUsed = tx . Gas ( )
receipts = append ( receipts , receipt )
cumulativeGas += tx . Gas ( )
}
header . Root = common . BytesToHash ( hasher . Sum ( nil ) )
// Assemble and return the final block for sealing
2021-03-15 18:34:22 +00:00
return types . NewBlock ( header , txs , nil , receipts )
2020-12-04 11:22:19 +00:00
}