93 lines
2.8 KiB
Go
93 lines
2.8 KiB
Go
|
package core
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"testing"
|
||
|
"math/big"
|
||
|
"crypto/ecdsa"
|
||
|
|
||
|
"github.com/ethereum/go-ethereum/common"
|
||
|
"github.com/ethereum/go-ethereum/core/types"
|
||
|
"github.com/ethereum/go-ethereum/params"
|
||
|
"github.com/ethereum/go-ethereum/crypto"
|
||
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
||
|
"github.com/ethereum/go-ethereum/core/vm"
|
||
|
"github.com/ethereum/go-ethereum/common/math"
|
||
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
||
|
"github.com/ethereum/go-ethereum/core/state"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
config = ¶ms.ChainConfig{
|
||
|
ChainID: big.NewInt(1),
|
||
|
HomesteadBlock: big.NewInt(0),
|
||
|
EIP150Block: big.NewInt(0),
|
||
|
EIP155Block: big.NewInt(0),
|
||
|
EIP158Block: big.NewInt(0),
|
||
|
ByzantiumBlock: big.NewInt(0),
|
||
|
ConstantinopleBlock: big.NewInt(0),
|
||
|
PetersburgBlock: big.NewInt(0),
|
||
|
IstanbulBlock: big.NewInt(0),
|
||
|
MuirGlacierBlock: big.NewInt(0),
|
||
|
BerlinBlock: big.NewInt(0),
|
||
|
LondonBlock: big.NewInt(0),
|
||
|
Ethash: new(params.EthashConfig),
|
||
|
}
|
||
|
signer = types.LatestSigner(config)
|
||
|
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||
|
key2, _ = crypto.HexToECDSA("0202020202020202020202020202020202020202020202020202002020202020")
|
||
|
)
|
||
|
|
||
|
var makeTx = func(key *ecdsa.PrivateKey, nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *types.Transaction {
|
||
|
tx, _ := types.SignTx(types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, data), signer, key)
|
||
|
return tx
|
||
|
}
|
||
|
|
||
|
var (
|
||
|
db = rawdb.NewMemoryDatabase()
|
||
|
gspec = &Genesis{
|
||
|
Config: config,
|
||
|
Alloc: GenesisAlloc{
|
||
|
common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7"): GenesisAccount{
|
||
|
Balance: big.NewInt(1000000000000000000), // 1 ether
|
||
|
Nonce: 0,
|
||
|
},
|
||
|
common.HexToAddress("0xfd0810DD14796680f72adf1a371963d0745BCc64"): GenesisAccount{
|
||
|
Balance: big.NewInt(1000000000000000000), // 1 ether
|
||
|
Nonce: math.MaxUint64,
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
)
|
||
|
|
||
|
|
||
|
func TestBlockProcessingInjections(t *testing.T) {
|
||
|
|
||
|
|
||
|
blockchain, _ := NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
|
||
|
|
||
|
engine := ethash.NewFaker()
|
||
|
|
||
|
sp := NewStateProcessor(config, blockchain, engine)
|
||
|
|
||
|
txns := []*types.Transaction{
|
||
|
makeTx(key1, 0, common.Address{}, big.NewInt(1000), params.TxGas-1000, big.NewInt(875000000), nil),
|
||
|
}
|
||
|
|
||
|
block := GenerateBadBlock(gspec.ToBlock(), engine, txns, gspec.Config)
|
||
|
|
||
|
statedb, _ := state.New(blockchain.GetBlockByHash(block.ParentHash()).Root(), blockchain.stateCache, nil)
|
||
|
|
||
|
t.Run(fmt.Sprintf("test BlockProcessingError"), func(t *testing.T) {
|
||
|
called := false
|
||
|
injectionCalled = &called
|
||
|
|
||
|
_, _, _, _ = sp.Process(block, statedb, vm.Config{})
|
||
|
|
||
|
if *injectionCalled != true {
|
||
|
t.Fatalf("pluginBlockProcessingError injection in stateProcessor.Process() not called")
|
||
|
}
|
||
|
})
|
||
|
|
||
|
|
||
|
}
|