diff --git a/core/plugeth_injection_test.go b/core/plugeth_injection_test.go new file mode 100644 index 000000000..faa7c2399 --- /dev/null +++ b/core/plugeth_injection_test.go @@ -0,0 +1,93 @@ +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") + } + }) + + +} \ No newline at end of file diff --git a/core/plugin_hooks.go b/core/plugin_hooks.go index f2c5def39..9abbfa723 100644 --- a/core/plugin_hooks.go +++ b/core/plugin_hooks.go @@ -18,6 +18,8 @@ import ( "github.com/openrelayxyz/plugeth-utils/core" ) +var injectionCalled *bool + func PluginPreProcessBlock(pl *plugins.PluginLoader, block *types.Block) { fnList := pl.Lookup("PreProcessBlock", func(item interface{}) bool { _, ok := item.(func(core.Hash, uint64, []byte)) @@ -68,6 +70,12 @@ func PluginBlockProcessingError(pl *plugins.PluginLoader, tx *types.Transaction, } } func pluginBlockProcessingError(tx *types.Transaction, block *types.Block, err error) { + + if injectionCalled != nil { + called := true + injectionCalled = &called + } + if plugins.DefaultPluginLoader == nil { log.Warn("Attempting BlockProcessingError, but default PluginLoader has not been initialized") return diff --git a/core/rawdb/plugeth_hook_test.go b/core/rawdb/plugeth_injection_test.go similarity index 100% rename from core/rawdb/plugeth_hook_test.go rename to core/rawdb/plugeth_injection_test.go