mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-28 07:36:44 +00:00
* cmd, core, trie: verkle-capable `geth init` (#28270) This change allows the creation of a genesis block for verkle testnets. This makes for a chunk of code that is easier to review and still touches many discussion points. * eth/tracers/js: fix isPush for push0 (#28520) Fixes so that `push0` opcode is correctly reported as `true` by the `IsPush` function --------- Co-authored-by: Martin Holst Swende <martin@swende.se> * trie: spelling - fix comments in hasher (#28507) Co-authored-by: VM <arimas@foxmail.com> * tests/fuzzers: move fuzzers into native packages (#28467) This PR moves our fuzzers from tests/fuzzers into whatever their respective 'native' package is. The historical reason why they were placed in an external location, is that when they were based on go-fuzz, they could not be "hidden" via the _test.go prefix. So in order to shove them away from the go-ethereum "production code", they were put aside. But now we've rewritten them to be based on golang testing, and thus can be brought back. I've left (in tests/) the ones that are not production (bls128381), require non-standard imports (secp requires btcec, bn256 requires gnark/google/cloudflare deps). This PR also adds a fuzzer for precompiled contracts, because why not. This PR utilizes a newly rewritten replacement for go-118-fuzz-build, namely gofuzz-shim, which utilises the inputs from the fuzzing engine better. * tests: skip tests on windows 32bit CI (#28521) tests: skip half the blockchain- and state-tests on windows 32bit CI-tests * cmd/geth: more special cases logging tests (#28527) adds logging tests for errors and custom fmt.Stringer-types which output strings that needs to be quoted/escaped. * accounts,cmd,console,les,metrics: refactor some errors checked by (ST1005) go-staticcheck (#28532) fix: fix some (ST1005)go-staticcheck * miner: run tests in parallel (#28506) Changes many of the tests in the miner package to run in parallel * internal/jsre/deps: fix typo in jsdoc (#28511) minor typo fix * accounts/abi: improve readability of method-to-string conversion (#28530) refactor: improve readability of NewMethod print * all: replace some cases of strings.SplitN with strings.Cut (#28446) * ethdb/memorydb, trie: reduced allocations (#28473) * trie: use pooling of iterator states in iterator The node iterator burns through a lot of memory while iterating a trie, and a lot of that can be avoided by using a fairly small pool (max 40 items). name old time/op new time/op delta Iterator-8 6.22ms ± 3% 5.40ms ± 6% -13.18% (p=0.008 n=5+5) name old alloc/op new alloc/op delta Iterator-8 2.36MB ± 0% 1.67MB ± 0% -29.23% (p=0.008 n=5+5) name old allocs/op new allocs/op delta Iterator-8 37.0k ± 0% 29.8k ± 0% ~ (p=0.079 n=4+5) * ethdb/memorydb: avoid one copying of key By making the transformation from []byte to string at an earlier point, we save an allocation which otherwise happens later on. name old time/op new time/op delta BatchAllocs-8 412µs ± 6% 382µs ± 2% -7.18% (p=0.016 n=5+4) name old alloc/op new alloc/op delta BatchAllocs-8 480kB ± 0% 490kB ± 0% +1.93% (p=0.008 n=5+5) name old allocs/op new allocs/op delta BatchAllocs-8 3.03k ± 0% 2.03k ± 0% -32.98% (p=0.008 n=5+5) * Dockerfile: update Go to 1.21 (#28538) * cmd/evm: validate blockchain tests poststate account storage (#28443) This PR verifies the accounts' storage as specified in a blockchain test's postState field The expect-section, it does really only check that the test works. It's meant for the test-author to verify that "If the test does what it's supposed to, then the nonce of X should be 2, and the slot Y at Z should be 0x123. This expect-section is not exhaustive (not full post-state) It is also not auto-generated, but put there manually by the author. We can still check it, as a test-sanity-check, in geth * signer: run tests in parallel (#28536) marks tests as parallel-safe in package signer * accounts, cmd: fix typos (#28526) * core/txpool/legacypool: respect nolocals-setting (#28435) This change adds a check to ensure that transactions added to the legacy pool are not treated as 'locals' if the global locals-management has been disabled. This change makes the pool enforce the --txpool.pricelimit setting. * cmd: run tests in parallel (#28546) * core/state/snapshot: print correct error from trie iterator (#28560) * cmd/evm: capitalize evm commands (#28569) * standard:fix for a unified standard * standard:fix more as a complements --------- Co-authored-by: haotian <haotian@haotiandeMacBook-Air.local> * accounts/abi: context info on unpack-errors (#28529) adds contextual information to errors returned by unpack * core, trie, rpc: speed up tests (#28461) * rpc: make subscription test faster reduces time for TestClientSubscriptionChannelClose from 25 sec to < 1 sec. * trie: cache trie nodes for faster sanity check This reduces the time spent on TestIncompleteSyncHash from ~25s to ~16s. * core/forkid: speed up validation test This takes the validation test from > 5s to sub 1 sec * core/state: improve snapshot test run brings the time for TestSnapshotRandom from 13s down to 6s * accounts/keystore: improve keyfile test This removes some unnecessary waits and reduces the runtime of TestUpdatedKeyfileContents from 5 to 3 seconds * trie: remove resolver * trie: only check ~5% of all trie nodes * ethdb/pebble: don't double-close iterator inside pebbleIterator (#28566) Adds 'released' flag to pebbleIterator to avoid double closing cockroachdb/pebble.Iterator as it is an invalid operation. Fixes #28565 * eth/filters: reuse error msg for invalid block range (#28479) * core/types: make 'v' optional for DynamicFeeTx and BlobTx (#28564) This fixes an issue where transactions would not be accepted when they have only 'yParity' and not 'v'. * rpc: improve performance of subscription notification encoding (#28328) It turns out that encoding json.RawMessage is slow because package json basically parses the message again to ensure it is valid. We can avoid the slowdown by encoding the entire RPC notification once, which yields a 30% speedup. * cmd/utils: validate pre-existing genesis in --dev mode (#28468) geth --dev can be used with an existing data directory and genesis block. Since dev mode only works with PoS, we need to verify that the merge has happened. Co-authored-by: Felix Lange <fjl@twurst.com> * cmd/geth: add support for --dev flag in dumpgenesis (#28463) Co-authored-by: Felix Lange <fjl@twurst.com> Co-authored-by: lightclient <lightclient@protonmail.com> * les/vflux: run tests in parallel (#28524) * cmd/{geth,utils}: add cmd to export preimages in snap enumeration order (#28256) Adds a subcommand: `geth snapshot export-preimages`, to export preimages of every hash found during a snapshot enumeration: that is, it exports _only the active state_, and not _all_ preimages that have been used but are no longer part of the state. This tool is needed for the verkle transition, in order to distribute the preimages needed for the conversion. Since only the 'active' preimages are exported, the output is shrunk from ~70GB to ~4GB. The order of the output is the order used by the snapshot enumeration, which avoids database thrashing. However, it also means that storage-slot preimages are not deduplicated. * cmd/geth: fix build error (#28585) * cmd/devp2p/internal/ethtest: undo debug-hack (#28588) cmd/devp2p/internal/ethtest: remove a debug-hack flaw which prevented certain tests from running * params: update discV5 bootnodes (#28562) update discV5 bootnodes from https://github.com/eth-clients/eth2-networks/blob/master/shared/mainnet/bootstrap_nodes.txt * cmd, les, tests: remove light client code (#28586) * cmd, les, tests: remove light client code This commit removes the light client (LES) code. Since the merge the light client has been broken and it is hard to maintain it alongside the normal client. We decided it would be best to remove it for now and maybe rework and reintroduce it in the future. * cmd, eth: remove some more mentions of light mode * cmd: re-add flags and mark as deprecated * cmd: warn the user about deprecated flags * eth: better error message * eth, internal/ethapi: drop some weird indirection (#28597) * trie: fix random test generator early terminate (#28590) This change fixes a minor bug in the `randTest.Generate` function, which caused the `quick.Check` to be a no-op. * eth/gasestimator, internal/ethapi: move gas estimator out of rpc (#28600) * go.mod: update uint256 to v1.2.4 (#28612) * eth/catalyst, eth/downloader: expose more sync information (#28584) This change exposes more information from sync module internally * light: remove package light(#28614) This changes removes the package 'light', which is currently unused. * cmd/evm, core/state: fix post-exec dump of state (statetests, blockchaintests) (#28504) There were several problems related to dumping state. - If a preimage was missing, even if we had set the `OnlyWithAddresses` to `false`, to export them anyway, the way the mapping was constructed (using `common.Address` as key) made the entries get lost anyway. Concerns both state- and blockchain tests. - Blockchain test execution was not configured to store preimages. This changes makes it so that the block test executor takes a callback, just like the state test executor already does. This callback can be used to examine the post-execution state, e.g. to aid debugging of test failures. * ethereum: remove TODO comment about subscription (#28609) * eth/tracers/js: fix type inconsistencies (#28488) This change fixes two type-inconsistencies in the JS tracer: - In most places we return byte arrays as a `Uint8Array` to the tracer. However it seems we missed doing the conversion for `ctx` fields which are passed to the tracer during `result`. They are passed as simple arrays. I think Uint8Arrays are more suitable and we should change this inconsistency. Note: this will be a breaking-change. But I believe the effect is small. If we look at our tracers we see that these fields (`ctx.from`, `ctx.to`, etc.) are used in 2 ways. Passed to `toHex` which takes both array or buffer. Or the length was measured which is the same for both types. - The `slice` taking in `int, int` params versus `memory.slice` taking `int64, int64` params. I suggest changing `slice` types to `int64`. This should have no effect almost in any case. * crypto/secp256k1: fix 32-bit tests when CGO_ENABLED=0 (#28602) * consensus: verify the nonexistence of shanghai- and cancun-specific header fields (#28605) * eth/gasestimator: allow slight estimation error in favor of less iterations (#28618) * eth/gasestimator: early exit for plain transfer and error allowance * core, eth/gasestimator: hard guess at a possible required gas * internal/ethapi: update estimation tests with the error ratio * eth/gasestimator: I hate you linter * graphql: fix gas estimation test --------- Co-authored-by: Oren <orenyomtov@users.noreply.github.com> * all: replace log15 with slog (#28187) This PR replaces Geth's logger package (a fork of [log15](https://github.com/inconshreveable/log15)) with an implementation using slog, a logging library included as part of the Go standard library as of Go1.21. Main changes are as follows: * removes any log handlers that were unused in the Geth codebase. * Json, logfmt, and terminal formatters are now slog handlers. * Verbosity level constants are changed to match slog constant values. Internal translation is done to make this opaque to the user and backwards compatible with existing `--verbosity` and `--vmodule` options. * `--log.backtraceat` and `--log.debug` are removed. The external-facing API is largely the same as the existing Geth logger. Logger method signatures remain unchanged. A small semantic difference is that a `Handler` can only be set once per `Logger` and not changed dynamically. This just means that a new logger must be instantiated every time the handler of the root logger is changed. ---- For users of the `go-ethereum/log` module. If you were using this module for your own project, you will need to change the initialization. If you previously did ```golang log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true)))) ``` You now instead need to do ```golang log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelInfo, true))) ``` See more about reasoning here: https://github.com/ethereum/go-ethereum/issues/28558#issuecomment-1820606613 * core/state: make stateobject.create selfcontain (#28459) * trie/triedb/hashdb: take lock around access to dirties cache (#28542) Add read locking of db lock around access to dirties cache in hashdb.Database to prevent data race versus hashdb.Database.dereference which can modify the dirities map by deleting an item. Fixes #28541 --------- Co-authored-by: Gary Rong <garyrong0905@gmail.com> * accounts/abi/bind: fix typo (#28630) * slog: faster and less memory-consumption (#28621) These changes improves the performance of the non-coloured terminal formatting, _quite a lot_. ``` name old time/op new time/op delta TerminalHandler-8 10.2µs ±15% 5.4µs ± 9% -47.02% (p=0.008 n=5+5) name old alloc/op new alloc/op delta TerminalHandler-8 2.17kB ± 0% 0.40kB ± 0% -81.46% (p=0.008 n=5+5) name old allocs/op new allocs/op delta TerminalHandler-8 33.0 ± 0% 5.0 ± 0% -84.85% (p=0.008 n=5+5) ``` I tried to _somewhat_ organize the commits, but the it might still be a bit chaotic. Some core insights: - The function `terminalHandler.Handl` uses a mutex, and writes all output immediately to 'upstream'. Thus, it can reuse a scratch-buffer every time. - This buffer can be propagated internally, making all the internal formatters either write directly to it, - OR, make use of the `tmp := buf.AvailableBuffer()` in some cases, where a byte buffer "extra capacity" can be temporarily used. - The `slog` package uses `Attr` by value. It makes sense to minimize operating on them, since iterating / collecting into a new slice, iterating again etc causes copy-on-heap. Better to operate on them only once. - If we want to do padding, it's better to copy from a constant `space`-buffer than to invoke `bytes.Repeat` every single time. * eth/tracers: tx-level state in debug_traceCall (#28460) * cmd/evm: fix Env struct json tag (#28635) * accounts/abi/bind: fixed typos (#28634) * Update auth.go * Update backend.go * Update bind.go * Update bind_test.go * eth/fetcher: fix invalid tracking of received at time for block (#28637) eth/fetcher: fix invalid tracking of received at time * accounts: run tests in parallel (#28544) * eth/tracers/logger: make structlog/json-log stack hex again (#28628) * common/hexutil: define hex wrappers for uint256.Int * eth/tracers/logger: make structlog/json-log stack hex again * common/hexutil: goimports * log: remove lazy, remove unused interfaces, unexport methods (#28622) This change - Removes interface `log.Format`, - Removes method `log.FormatFunc`, - unexports `TerminalHandler.TerminalFormat` formatting methods (renamed to `TerminalHandler.format`) - removes the notion of `log.Lazy` values The lazy handler was useful in the old log package, since it could defer the evaluation of costly attributes until later in the log pipeline: thus, if the logging was done at 'Trace', we could skip evaluation if logging only was set to 'Info'. With the move to slog, this way of deferring evaluation is no longer needed, since slog introduced 'Enabled': the caller can thus do the evaluate-or-not decision at the callsite, which is much more straight-forward than dealing with lazy reflect-based evaluation. Also, lazy evaluation would not work with 'native' slog, as in, these two statements would be evaluated differently: ```golang log.Info("foo", "my lazy", lazyObj) slog.Info("foo", "my lazy", lazyObj) ``` * .github: use github actions to run 32-bit linux tests (#28549) use github actions to run 32-bit linux tests * ethdb/pebble: remove a dependency (#28627) The dependency was not really used anyway, so we can get rid of it. Co-authored-by: Felix Lange <fjl@twurst.com> * tests/fuzzers/bls12381: deactivate BLS fuzzer when CGO_ENABLED=0 (#28653) tests/fuzzers/bls12381: deactivate fuzzer when CGO_ENABLED=0 * build: upgrade -dlgo version to Go 1.21.5 (#28648) * rpc: fix ns/µs mismatch in metrics (#28649) The rpc/duration/all meter was in nanoseconds, the individual meter in microseconds. This PR changes it so both of them use nanoseconds. * cmd/evm: fix dump after state-test exec (#28650) The dump after state-test didn't work, the problem was an error, "Already committed", which was silently ignored. This change re-initialises the state, so the dumping works again. * beacon/light: add CommitteeChain (#27766) This change implements CommitteeChain which is a key component of the beacon light client. It is a passive data structure that can validate, hold and update a chain of beacon light sync committees and updates, starting from a checkpoint that proves the starting committee through a beacon block hash, header and corresponding state. Once synced to the current sync period, CommitteeChain can also validate signed beacon headers. * cmd/utils, eth: disallow invalid snap sync / snapshot flag combos (#28657) * eth: prevent startup in snap mode without snapshots * cmd/utils: try to fix bad flag combos wrt snap sync and snapshot generation * trie: remove inconsistent trie nodes during sync in path mode (#28595) This fixes a database corruption issue that could occur during state healing. When sync is aborted while certain modifications were already committed, and a reorg occurs, the database would contain incorrect trie nodes stored by path. These nodes need to detected/deleted in order to obtain a complete and fully correct state after state healing. --------- Co-authored-by: Felix Lange <fjl@twurst.com> * cmd/utils: fix HTTPHost, WSHost flag priority (#28669) Co-authored-by: Felix Lange <fjl@twurst.com> * eth/protocols/eth: fix typos in comments (#28652) * core/txpool : small cleanup refactors (#28654) * eth/fetcher, eth/gasestimator: fix typos in comments (#28675) * all: fix typos in comments (#28662) Co-authored-by: Felix Lange <fjl@twurst.com> * miner: eliminate the dead loop possibility for `newWorkLoop` and `mainLoop` (#28677) discard the intervalAdjust message if the channel is full * all: fix typos in comments (#28682) chore(core,eth):fix a couple of typos * p2p/discover: add liveness check in collectTableNodes (#28686) * p2p/discover: add liveness check in collectTableNodes * p2p/discover: fix test * p2p/discover: rename to appendLiveNodes * p2p/discover: add dedup logic back * p2p/discover: simplify * p2p/discover: fix issue found by test * internal/flags: add missing flag types for auto-env-var generation (#28692) Certain flags, such as `--rpc.txfeecap` currently do not have an env-var auto-generated for them. This change adds three missing cli flag types to the auto env-var helper function to fix this. * cmd/evm: default to mirror mainnet forks enabled (#28691) cmd/evm: default to using dev chain config (all mainnet HFs activated at block/timestamp 0 * cmd/evm, cmd/clef, cmd/bootnode: fix / unify logging (#28696) This change fixes a problem with our non-core binaries: evm, clef, bootnode. First of all, they failed to convert from legacy loglevels 1 to 5, to the new slog loglevels -4 to 4. Secondly, the logging was actually setup in the init phase, and then overridden in the main. This is not needed for evm, since it used the same flag name as the main geth verbosity. Better to let the flags/internal handle the logging init. * cmd/evm: t8n support custom tracers (#28557) This change implements ability for the `evm t8n` tool to use custom tracers; either 'native' golang tracers or javascript tracers. * params: release go-ethereum v1.13.6 stable * Fix build errors * Fix test-integration --------- Co-authored-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Co-authored-by: Sina Mahmoodi <1591639+s1na@users.noreply.github.com> Co-authored-by: Martin Holst Swende <martin@swende.se> Co-authored-by: VM <112189277+sysvm@users.noreply.github.com> Co-authored-by: VM <arimas@foxmail.com> Co-authored-by: jwasinger <j-wasinger@hotmail.com> Co-authored-by: Zoro <40222601+BabyHalimao@users.noreply.github.com> Co-authored-by: Håvard Anda Estensen <haavard.ae@gmail.com> Co-authored-by: aliening <128203330+aliening@users.noreply.github.com> Co-authored-by: Halimao <1065621723@qq.com> Co-authored-by: danceratopz <danceratopz@gmail.com> Co-authored-by: levisyin <150114626+levisyin@users.noreply.github.com> Co-authored-by: jp-imx <109574657+jp-imx@users.noreply.github.com> Co-authored-by: rjl493456442 <garyrong0905@gmail.com> Co-authored-by: Haotian <51777534+tmelhao@users.noreply.github.com> Co-authored-by: haotian <haotian@haotiandeMacBook-Air.local> Co-authored-by: Marius van der Wijden <m.vanderwijden@live.de> Co-authored-by: Maciej Kulawik <10907694+magicxyyz@users.noreply.github.com> Co-authored-by: ucwong <ucwong@126.com> Co-authored-by: Mario Vega <marioevz@gmail.com> Co-authored-by: Delweng <delweng@gmail.com> Co-authored-by: Felix Lange <fjl@twurst.com> Co-authored-by: lightclient <lightclient@protonmail.com> Co-authored-by: Mikel Cortes <45786396+cortze@users.noreply.github.com> Co-authored-by: Péter Szilágyi <peterke@gmail.com> Co-authored-by: Ng Wei Han <47109095+weiihann@users.noreply.github.com> Co-authored-by: lightclient <14004106+lightclient@users.noreply.github.com> Co-authored-by: Shivam Sandbhor <shivam.sandbhor@gmail.com> Co-authored-by: Jakub Freebit <49676311+jakub-freebit@users.noreply.github.com> Co-authored-by: Oren <orenyomtov@users.noreply.github.com> Co-authored-by: BorkBorked <107079055+BorkBorked@users.noreply.github.com> Co-authored-by: ddl <dengdiliang@gmail.com> Co-authored-by: Manav Darji <manavdarji.india@gmail.com> Co-authored-by: Marius Kjærstad <sandakersmann@users.noreply.github.com> Co-authored-by: Felföldi Zsolt <zsfelfoldi@gmail.com> Co-authored-by: Ford <153042616+guerrierindien@users.noreply.github.com> Co-authored-by: Ursulafe <152976968+Ursulafe@users.noreply.github.com> Co-authored-by: Elias Rad <146735585+nnsW3@users.noreply.github.com> Co-authored-by: FletcherMan <fanciture@163.com> Co-authored-by: alex <152680487+bodhi-crypo@users.noreply.github.com> Co-authored-by: Sebastian Stammler <seb@oplabs.co>
1645 lines
53 KiB
Go
1645 lines
53 KiB
Go
// Copyright 2021 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 catalyst
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
crand "crypto/rand"
|
|
"fmt"
|
|
"math/big"
|
|
"math/rand"
|
|
"reflect"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/ethereum/go-ethereum/beacon/engine"
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
"github.com/ethereum/go-ethereum/consensus"
|
|
beaconConsensus "github.com/ethereum/go-ethereum/consensus/beacon"
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
|
"github.com/ethereum/go-ethereum/core"
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
"github.com/ethereum/go-ethereum/crypto/kzg4844"
|
|
"github.com/ethereum/go-ethereum/eth"
|
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
|
"github.com/ethereum/go-ethereum/eth/ethconfig"
|
|
"github.com/ethereum/go-ethereum/log"
|
|
"github.com/ethereum/go-ethereum/miner"
|
|
"github.com/ethereum/go-ethereum/node"
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
"github.com/ethereum/go-ethereum/params"
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
"github.com/mattn/go-colorable"
|
|
)
|
|
|
|
var (
|
|
// testKey is a private key to use for funding a tester account.
|
|
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
|
|
|
// testAddr is the Ethereum address of the tester account.
|
|
testAddr = crypto.PubkeyToAddress(testKey.PublicKey)
|
|
|
|
testBalance = big.NewInt(2e18)
|
|
)
|
|
|
|
func generateMergeChain(n int, merged bool) (*core.Genesis, []*types.Block) {
|
|
config := *params.AllEthashProtocolChanges
|
|
engine := consensus.Engine(beaconConsensus.New(ethash.NewFaker()))
|
|
if merged {
|
|
config.TerminalTotalDifficulty = common.Big0
|
|
config.TerminalTotalDifficultyPassed = true
|
|
engine = beaconConsensus.NewFaker()
|
|
}
|
|
genesis := &core.Genesis{
|
|
Config: &config,
|
|
Alloc: core.GenesisAlloc{
|
|
testAddr: {Balance: testBalance},
|
|
params.BeaconRootsStorageAddress: {Balance: common.Big0, Code: common.Hex2Bytes("3373fffffffffffffffffffffffffffffffffffffffe14604457602036146024575f5ffd5b620180005f350680545f35146037575f5ffd5b6201800001545f5260205ff35b6201800042064281555f359062018000015500")},
|
|
},
|
|
ExtraData: []byte("test genesis"),
|
|
Timestamp: 9000,
|
|
BaseFee: big.NewInt(params.InitialBaseFee),
|
|
Difficulty: big.NewInt(0),
|
|
}
|
|
testNonce := uint64(0)
|
|
generate := func(i int, g *core.BlockGen) {
|
|
g.OffsetTime(5)
|
|
g.SetExtra([]byte("test"))
|
|
tx, _ := types.SignTx(types.NewTransaction(testNonce, common.HexToAddress("0x9a9070028361F7AAbeB3f2F2Dc07F82C4a98A02a"), big.NewInt(1), params.TxGas, big.NewInt(params.InitialBaseFee*2), nil), types.LatestSigner(&config), testKey)
|
|
g.AddTx(tx)
|
|
testNonce++
|
|
}
|
|
_, blocks, _ := core.GenerateChainWithGenesis(genesis, engine, n, generate)
|
|
|
|
if !merged {
|
|
totalDifficulty := big.NewInt(0)
|
|
for _, b := range blocks {
|
|
totalDifficulty.Add(totalDifficulty, b.Difficulty())
|
|
}
|
|
config.TerminalTotalDifficulty = totalDifficulty
|
|
}
|
|
|
|
return genesis, blocks
|
|
}
|
|
|
|
func TestEth2AssembleBlock(t *testing.T) {
|
|
genesis, blocks := generateMergeChain(10, false)
|
|
n, ethservice := startEthService(t, genesis, blocks)
|
|
defer n.Close()
|
|
|
|
api := NewConsensusAPI(ethservice)
|
|
signer := types.NewEIP155Signer(ethservice.BlockChain().Config().ChainID)
|
|
tx, err := types.SignTx(types.NewTransaction(uint64(10), blocks[9].Coinbase(), big.NewInt(1000), params.TxGas, big.NewInt(params.InitialBaseFee), nil), signer, testKey)
|
|
if err != nil {
|
|
t.Fatalf("error signing transaction, err=%v", err)
|
|
}
|
|
ethservice.TxPool().Add([]*types.Transaction{tx}, true, false)
|
|
blockParams := engine.PayloadAttributes{
|
|
Timestamp: blocks[9].Time() + 5,
|
|
}
|
|
// The miner needs to pick up on the txs in the pool, so a few retries might be
|
|
// needed.
|
|
if _, testErr := assembleWithTransactions(api, blocks[9].Hash(), &blockParams, 1); testErr != nil {
|
|
t.Fatal(testErr)
|
|
}
|
|
}
|
|
|
|
// assembleWithTransactions tries to assemble a block, retrying until it has 'want',
|
|
// number of transactions in it, or it has retried three times.
|
|
func assembleWithTransactions(api *ConsensusAPI, parentHash common.Hash, params *engine.PayloadAttributes, want int) (execData *engine.ExecutableData, err error) {
|
|
for retries := 3; retries > 0; retries-- {
|
|
execData, err = assembleBlock(api, parentHash, params)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if have, want := len(execData.Transactions), want; have != want {
|
|
err = fmt.Errorf("invalid number of transactions, have %d want %d", have, want)
|
|
continue
|
|
}
|
|
return execData, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
func TestEth2AssembleBlockWithAnotherBlocksTxs(t *testing.T) {
|
|
genesis, blocks := generateMergeChain(10, false)
|
|
n, ethservice := startEthService(t, genesis, blocks[:9])
|
|
defer n.Close()
|
|
|
|
api := NewConsensusAPI(ethservice)
|
|
|
|
// Put the 10th block's tx in the pool and produce a new block
|
|
txs := blocks[9].Transactions()
|
|
api.eth.TxPool().Add(txs, false, true)
|
|
blockParams := engine.PayloadAttributes{
|
|
Timestamp: blocks[8].Time() + 5,
|
|
}
|
|
// The miner needs to pick up on the txs in the pool, so a few retries might be
|
|
// needed.
|
|
if _, err := assembleWithTransactions(api, blocks[8].Hash(), &blockParams, blocks[9].Transactions().Len()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestSetHeadBeforeTotalDifficulty(t *testing.T) {
|
|
genesis, blocks := generateMergeChain(10, false)
|
|
n, ethservice := startEthService(t, genesis, blocks)
|
|
defer n.Close()
|
|
|
|
api := NewConsensusAPI(ethservice)
|
|
fcState := engine.ForkchoiceStateV1{
|
|
HeadBlockHash: blocks[5].Hash(),
|
|
SafeBlockHash: common.Hash{},
|
|
FinalizedBlockHash: common.Hash{},
|
|
}
|
|
if resp, err := api.ForkchoiceUpdatedV1(fcState, nil); err != nil {
|
|
t.Errorf("fork choice updated should not error: %v", err)
|
|
} else if resp.PayloadStatus.Status != engine.INVALID_TERMINAL_BLOCK.Status {
|
|
t.Errorf("fork choice updated before total terminal difficulty should be INVALID")
|
|
}
|
|
}
|
|
|
|
func TestEth2PrepareAndGetPayload(t *testing.T) {
|
|
genesis, blocks := generateMergeChain(10, false)
|
|
// We need to properly set the terminal total difficulty
|
|
genesis.Config.TerminalTotalDifficulty.Sub(genesis.Config.TerminalTotalDifficulty, blocks[9].Difficulty())
|
|
n, ethservice := startEthService(t, genesis, blocks[:9])
|
|
defer n.Close()
|
|
|
|
api := NewConsensusAPI(ethservice)
|
|
|
|
// Put the 10th block's tx in the pool and produce a new block
|
|
txs := blocks[9].Transactions()
|
|
ethservice.TxPool().Add(txs, true, false)
|
|
blockParams := engine.PayloadAttributes{
|
|
Timestamp: blocks[8].Time() + 5,
|
|
}
|
|
fcState := engine.ForkchoiceStateV1{
|
|
HeadBlockHash: blocks[8].Hash(),
|
|
SafeBlockHash: common.Hash{},
|
|
FinalizedBlockHash: common.Hash{},
|
|
}
|
|
_, err := api.ForkchoiceUpdatedV1(fcState, &blockParams)
|
|
if err != nil {
|
|
t.Fatalf("error preparing payload, err=%v", err)
|
|
}
|
|
// give the payload some time to be built
|
|
time.Sleep(100 * time.Millisecond)
|
|
payloadID := (&miner.BuildPayloadArgs{
|
|
Parent: fcState.HeadBlockHash,
|
|
Timestamp: blockParams.Timestamp,
|
|
FeeRecipient: blockParams.SuggestedFeeRecipient,
|
|
Random: blockParams.Random,
|
|
BeaconRoot: blockParams.BeaconRoot,
|
|
}).Id()
|
|
execData, err := api.GetPayloadV1(payloadID)
|
|
if err != nil {
|
|
t.Fatalf("error getting payload, err=%v", err)
|
|
}
|
|
if len(execData.Transactions) != blocks[9].Transactions().Len() {
|
|
t.Fatalf("invalid number of transactions %d != 1", len(execData.Transactions))
|
|
}
|
|
// Test invalid payloadID
|
|
var invPayload engine.PayloadID
|
|
copy(invPayload[:], payloadID[:])
|
|
invPayload[0] = ^invPayload[0]
|
|
_, err = api.GetPayloadV1(invPayload)
|
|
if err == nil {
|
|
t.Fatal("expected error retrieving invalid payload")
|
|
}
|
|
}
|
|
|
|
func checkLogEvents(t *testing.T, logsCh <-chan []*types.Log, rmLogsCh <-chan core.RemovedLogsEvent, wantNew, wantRemoved int) {
|
|
t.Helper()
|
|
|
|
if len(logsCh) != wantNew {
|
|
t.Fatalf("wrong number of log events: got %d, want %d", len(logsCh), wantNew)
|
|
}
|
|
if len(rmLogsCh) != wantRemoved {
|
|
t.Fatalf("wrong number of removed log events: got %d, want %d", len(rmLogsCh), wantRemoved)
|
|
}
|
|
// Drain events.
|
|
for i := 0; i < len(logsCh); i++ {
|
|
<-logsCh
|
|
}
|
|
for i := 0; i < len(rmLogsCh); i++ {
|
|
<-rmLogsCh
|
|
}
|
|
}
|
|
|
|
func TestInvalidPayloadTimestamp(t *testing.T) {
|
|
genesis, preMergeBlocks := generateMergeChain(10, false)
|
|
n, ethservice := startEthService(t, genesis, preMergeBlocks)
|
|
defer n.Close()
|
|
var (
|
|
api = NewConsensusAPI(ethservice)
|
|
parent = ethservice.BlockChain().CurrentBlock()
|
|
)
|
|
tests := []struct {
|
|
time uint64
|
|
shouldErr bool
|
|
}{
|
|
{0, true},
|
|
{parent.Time, true},
|
|
{parent.Time - 1, true},
|
|
|
|
// TODO (MariusVanDerWijden) following tests are currently broken,
|
|
// fixed in upcoming merge-kiln-v2 pr
|
|
//{parent.Time() + 1, false},
|
|
//{uint64(time.Now().Unix()) + uint64(time.Minute), false},
|
|
}
|
|
|
|
for i, test := range tests {
|
|
t.Run(fmt.Sprintf("Timestamp test: %v", i), func(t *testing.T) {
|
|
params := engine.PayloadAttributes{
|
|
Timestamp: test.time,
|
|
Random: crypto.Keccak256Hash([]byte{byte(123)}),
|
|
SuggestedFeeRecipient: parent.Coinbase,
|
|
}
|
|
fcState := engine.ForkchoiceStateV1{
|
|
HeadBlockHash: parent.Hash(),
|
|
SafeBlockHash: common.Hash{},
|
|
FinalizedBlockHash: common.Hash{},
|
|
}
|
|
_, err := api.ForkchoiceUpdatedV1(fcState, ¶ms)
|
|
if test.shouldErr && err == nil {
|
|
t.Fatalf("expected error preparing payload with invalid timestamp, err=%v", err)
|
|
} else if !test.shouldErr && err != nil {
|
|
t.Fatalf("error preparing payload with valid timestamp, err=%v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEth2NewBlock(t *testing.T) {
|
|
genesis, preMergeBlocks := generateMergeChain(10, false)
|
|
n, ethservice := startEthService(t, genesis, preMergeBlocks)
|
|
defer n.Close()
|
|
|
|
var (
|
|
api = NewConsensusAPI(ethservice)
|
|
parent = preMergeBlocks[len(preMergeBlocks)-1]
|
|
|
|
// This EVM code generates a log when the contract is created.
|
|
logCode = common.Hex2Bytes("60606040525b7f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405180905060405180910390a15b600a8060416000396000f360606040526008565b00")
|
|
)
|
|
// The event channels.
|
|
newLogCh := make(chan []*types.Log, 10)
|
|
rmLogsCh := make(chan core.RemovedLogsEvent, 10)
|
|
ethservice.BlockChain().SubscribeLogsEvent(newLogCh)
|
|
ethservice.BlockChain().SubscribeRemovedLogsEvent(rmLogsCh)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
statedb, _ := ethservice.BlockChain().StateAt(parent.Root())
|
|
nonce := statedb.GetNonce(testAddr)
|
|
tx, _ := types.SignTx(types.NewContractCreation(nonce, new(big.Int), 1000000, big.NewInt(2*params.InitialBaseFee), logCode), types.LatestSigner(ethservice.BlockChain().Config()), testKey)
|
|
ethservice.TxPool().Add([]*types.Transaction{tx}, true, false)
|
|
|
|
execData, err := assembleWithTransactions(api, parent.Hash(), &engine.PayloadAttributes{
|
|
Timestamp: parent.Time() + 5,
|
|
}, 1)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create the executable data %v", err)
|
|
}
|
|
block, err := engine.ExecutableDataToBlock(*execData, nil, nil)
|
|
if err != nil {
|
|
t.Fatalf("Failed to convert executable data to block %v", err)
|
|
}
|
|
newResp, err := api.NewPayloadV1(*execData)
|
|
switch {
|
|
case err != nil:
|
|
t.Fatalf("Failed to insert block: %v", err)
|
|
case newResp.Status != "VALID":
|
|
t.Fatalf("Failed to insert block: %v", newResp.Status)
|
|
case ethservice.BlockChain().CurrentBlock().Number.Uint64() != block.NumberU64()-1:
|
|
t.Fatalf("Chain head shouldn't be updated")
|
|
}
|
|
checkLogEvents(t, newLogCh, rmLogsCh, 0, 0)
|
|
fcState := engine.ForkchoiceStateV1{
|
|
HeadBlockHash: block.Hash(),
|
|
SafeBlockHash: block.Hash(),
|
|
FinalizedBlockHash: block.Hash(),
|
|
}
|
|
if _, err := api.ForkchoiceUpdatedV1(fcState, nil); err != nil {
|
|
t.Fatalf("Failed to insert block: %v", err)
|
|
}
|
|
if have, want := ethservice.BlockChain().CurrentBlock().Number.Uint64(), block.NumberU64(); have != want {
|
|
t.Fatalf("Chain head should be updated, have %d want %d", have, want)
|
|
}
|
|
checkLogEvents(t, newLogCh, rmLogsCh, 1, 0)
|
|
|
|
parent = block
|
|
}
|
|
|
|
// Introduce fork chain
|
|
var (
|
|
head = ethservice.BlockChain().CurrentBlock().Number.Uint64()
|
|
)
|
|
parent = preMergeBlocks[len(preMergeBlocks)-1]
|
|
for i := 0; i < 10; i++ {
|
|
execData, err := assembleBlock(api, parent.Hash(), &engine.PayloadAttributes{
|
|
Timestamp: parent.Time() + 6,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Failed to create the executable data %v", err)
|
|
}
|
|
block, err := engine.ExecutableDataToBlock(*execData, nil, nil)
|
|
if err != nil {
|
|
t.Fatalf("Failed to convert executable data to block %v", err)
|
|
}
|
|
newResp, err := api.NewPayloadV1(*execData)
|
|
if err != nil || newResp.Status != "VALID" {
|
|
t.Fatalf("Failed to insert block: %v", err)
|
|
}
|
|
if ethservice.BlockChain().CurrentBlock().Number.Uint64() != head {
|
|
t.Fatalf("Chain head shouldn't be updated")
|
|
}
|
|
|
|
fcState := engine.ForkchoiceStateV1{
|
|
HeadBlockHash: block.Hash(),
|
|
SafeBlockHash: block.Hash(),
|
|
FinalizedBlockHash: block.Hash(),
|
|
}
|
|
if _, err := api.ForkchoiceUpdatedV1(fcState, nil); err != nil {
|
|
t.Fatalf("Failed to insert block: %v", err)
|
|
}
|
|
if ethservice.BlockChain().CurrentBlock().Number.Uint64() != block.NumberU64() {
|
|
t.Fatalf("Chain head should be updated")
|
|
}
|
|
parent, head = block, block.NumberU64()
|
|
}
|
|
}
|
|
|
|
func TestEth2DeepReorg(t *testing.T) {
|
|
// TODO (MariusVanDerWijden) TestEth2DeepReorg is currently broken, because it tries to reorg
|
|
// before the totalTerminalDifficulty threshold
|
|
/*
|
|
genesis, preMergeBlocks := generateMergeChain(core.TriesInMemory * 2, false)
|
|
n, ethservice := startEthService(t, genesis, preMergeBlocks)
|
|
defer n.Close()
|
|
|
|
var (
|
|
api = NewConsensusAPI(ethservice, nil)
|
|
parent = preMergeBlocks[len(preMergeBlocks)-core.TriesInMemory-1]
|
|
head = ethservice.BlockChain().CurrentBlock().Number.Uint64()()
|
|
)
|
|
if ethservice.BlockChain().HasBlockAndState(parent.Hash(), parent.NumberU64()) {
|
|
t.Errorf("Block %d not pruned", parent.NumberU64())
|
|
}
|
|
for i := 0; i < 10; i++ {
|
|
execData, err := api.assembleBlock(AssembleBlockParams{
|
|
ParentHash: parent.Hash(),
|
|
Timestamp: parent.Time() + 5,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Failed to create the executable data %v", err)
|
|
}
|
|
block, err := ExecutableDataToBlock(ethservice.BlockChain().Config(), parent.Header(), *execData)
|
|
if err != nil {
|
|
t.Fatalf("Failed to convert executable data to block %v", err)
|
|
}
|
|
newResp, err := api.ExecutePayload(*execData)
|
|
if err != nil || newResp.Status != "VALID" {
|
|
t.Fatalf("Failed to insert block: %v", err)
|
|
}
|
|
if ethservice.BlockChain().CurrentBlock().Number.Uint64()() != head {
|
|
t.Fatalf("Chain head shouldn't be updated")
|
|
}
|
|
if err := api.setHead(block.Hash()); err != nil {
|
|
t.Fatalf("Failed to set head: %v", err)
|
|
}
|
|
if ethservice.BlockChain().CurrentBlock().Number.Uint64()() != block.NumberU64() {
|
|
t.Fatalf("Chain head should be updated")
|
|
}
|
|
parent, head = block, block.NumberU64()
|
|
}
|
|
*/
|
|
}
|
|
|
|
// startEthService creates a full node instance for testing.
|
|
func startEthService(t *testing.T, genesis *core.Genesis, blocks []*types.Block) (*node.Node, *eth.Ethereum) {
|
|
t.Helper()
|
|
|
|
n, err := node.New(&node.Config{
|
|
P2P: p2p.Config{
|
|
ListenAddr: "0.0.0.0:0",
|
|
NoDiscovery: true,
|
|
MaxPeers: 25,
|
|
}})
|
|
if err != nil {
|
|
t.Fatal("can't create node:", err)
|
|
}
|
|
|
|
ethcfg := ðconfig.Config{Genesis: genesis, SyncMode: downloader.FullSync, TrieTimeout: time.Minute, TrieDirtyCache: 256, TrieCleanCache: 256}
|
|
ethservice, err := eth.New(n, ethcfg)
|
|
if err != nil {
|
|
t.Fatal("can't create eth service:", err)
|
|
}
|
|
if err := n.Start(); err != nil {
|
|
t.Fatal("can't start node:", err)
|
|
}
|
|
if _, err := ethservice.BlockChain().InsertChain(blocks); err != nil {
|
|
n.Close()
|
|
t.Fatal("can't import test blocks:", err)
|
|
}
|
|
|
|
ethservice.SetEtherbase(testAddr)
|
|
ethservice.SetSynced()
|
|
return n, ethservice
|
|
}
|
|
|
|
func TestFullAPI(t *testing.T) {
|
|
genesis, preMergeBlocks := generateMergeChain(10, false)
|
|
n, ethservice := startEthService(t, genesis, preMergeBlocks)
|
|
defer n.Close()
|
|
var (
|
|
parent = ethservice.BlockChain().CurrentBlock()
|
|
// This EVM code generates a log when the contract is created.
|
|
logCode = common.Hex2Bytes("60606040525b7f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405180905060405180910390a15b600a8060416000396000f360606040526008565b00")
|
|
)
|
|
|
|
callback := func(parent *types.Header) {
|
|
statedb, _ := ethservice.BlockChain().StateAt(parent.Root)
|
|
nonce := statedb.GetNonce(testAddr)
|
|
tx, _ := types.SignTx(types.NewContractCreation(nonce, new(big.Int), 1000000, big.NewInt(2*params.InitialBaseFee), logCode), types.LatestSigner(ethservice.BlockChain().Config()), testKey)
|
|
ethservice.TxPool().Add([]*types.Transaction{tx}, true, false)
|
|
}
|
|
|
|
setupBlocks(t, ethservice, 10, parent, callback, nil)
|
|
}
|
|
|
|
func setupBlocks(t *testing.T, ethservice *eth.Ethereum, n int, parent *types.Header, callback func(parent *types.Header), withdrawals [][]*types.Withdrawal) []*types.Header {
|
|
api := NewConsensusAPI(ethservice)
|
|
var blocks []*types.Header
|
|
for i := 0; i < n; i++ {
|
|
callback(parent)
|
|
var w []*types.Withdrawal
|
|
if withdrawals != nil {
|
|
w = withdrawals[i]
|
|
}
|
|
|
|
payload := getNewPayload(t, api, parent, w)
|
|
execResp, err := api.NewPayloadV2(*payload)
|
|
if err != nil {
|
|
t.Fatalf("can't execute payload: %v", err)
|
|
}
|
|
if execResp.Status != engine.VALID {
|
|
t.Fatalf("invalid status: %v", execResp.Status)
|
|
}
|
|
fcState := engine.ForkchoiceStateV1{
|
|
HeadBlockHash: payload.BlockHash,
|
|
SafeBlockHash: payload.ParentHash,
|
|
FinalizedBlockHash: payload.ParentHash,
|
|
}
|
|
if _, err := api.ForkchoiceUpdatedV1(fcState, nil); err != nil {
|
|
t.Fatalf("Failed to insert block: %v", err)
|
|
}
|
|
if ethservice.BlockChain().CurrentBlock().Number.Uint64() != payload.Number {
|
|
t.Fatal("Chain head should be updated")
|
|
}
|
|
if ethservice.BlockChain().CurrentFinalBlock().Number.Uint64() != payload.Number-1 {
|
|
t.Fatal("Finalized block should be updated")
|
|
}
|
|
parent = ethservice.BlockChain().CurrentBlock()
|
|
blocks = append(blocks, parent)
|
|
}
|
|
return blocks
|
|
}
|
|
|
|
func TestExchangeTransitionConfig(t *testing.T) {
|
|
genesis, preMergeBlocks := generateMergeChain(10, false)
|
|
n, ethservice := startEthService(t, genesis, preMergeBlocks)
|
|
defer n.Close()
|
|
|
|
// invalid ttd
|
|
api := NewConsensusAPI(ethservice)
|
|
config := engine.TransitionConfigurationV1{
|
|
TerminalTotalDifficulty: (*hexutil.Big)(big.NewInt(0)),
|
|
TerminalBlockHash: common.Hash{},
|
|
TerminalBlockNumber: 0,
|
|
}
|
|
if _, err := api.ExchangeTransitionConfigurationV1(config); err == nil {
|
|
t.Fatal("expected error on invalid config, invalid ttd")
|
|
}
|
|
// invalid terminal block hash
|
|
config = engine.TransitionConfigurationV1{
|
|
TerminalTotalDifficulty: (*hexutil.Big)(genesis.Config.TerminalTotalDifficulty),
|
|
TerminalBlockHash: common.Hash{1},
|
|
TerminalBlockNumber: 0,
|
|
}
|
|
if _, err := api.ExchangeTransitionConfigurationV1(config); err == nil {
|
|
t.Fatal("expected error on invalid config, invalid hash")
|
|
}
|
|
// valid config
|
|
config = engine.TransitionConfigurationV1{
|
|
TerminalTotalDifficulty: (*hexutil.Big)(genesis.Config.TerminalTotalDifficulty),
|
|
TerminalBlockHash: common.Hash{},
|
|
TerminalBlockNumber: 0,
|
|
}
|
|
if _, err := api.ExchangeTransitionConfigurationV1(config); err != nil {
|
|
t.Fatalf("expected no error on valid config, got %v", err)
|
|
}
|
|
// valid config
|
|
config = engine.TransitionConfigurationV1{
|
|
TerminalTotalDifficulty: (*hexutil.Big)(genesis.Config.TerminalTotalDifficulty),
|
|
TerminalBlockHash: preMergeBlocks[5].Hash(),
|
|
TerminalBlockNumber: 6,
|
|
}
|
|
if _, err := api.ExchangeTransitionConfigurationV1(config); err != nil {
|
|
t.Fatalf("expected no error on valid config, got %v", err)
|
|
}
|
|
}
|
|
|
|
/*
|
|
TestNewPayloadOnInvalidChain sets up a valid chain and tries to feed blocks
|
|
from an invalid chain to test if latestValidHash (LVH) works correctly.
|
|
|
|
We set up the following chain where P1 ... Pn and P1” are valid while
|
|
P1' is invalid.
|
|
We expect
|
|
(1) The LVH to point to the current inserted payload if it was valid.
|
|
(2) The LVH to point to the valid parent on an invalid payload (if the parent is available).
|
|
(3) If the parent is unavailable, the LVH should not be set.
|
|
|
|
CommonAncestor◄─▲── P1 ◄── P2 ◄─ P3 ◄─ ... ◄─ Pn
|
|
│
|
|
└── P1' ◄─ P2' ◄─ P3' ◄─ ... ◄─ Pn'
|
|
│
|
|
└── P1''
|
|
*/
|
|
func TestNewPayloadOnInvalidChain(t *testing.T) {
|
|
genesis, preMergeBlocks := generateMergeChain(10, false)
|
|
n, ethservice := startEthService(t, genesis, preMergeBlocks)
|
|
defer n.Close()
|
|
|
|
var (
|
|
api = NewConsensusAPI(ethservice)
|
|
parent = ethservice.BlockChain().CurrentBlock()
|
|
signer = types.LatestSigner(ethservice.BlockChain().Config())
|
|
// This EVM code generates a log when the contract is created.
|
|
logCode = common.Hex2Bytes("60606040525b7f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405180905060405180910390a15b600a8060416000396000f360606040526008565b00")
|
|
)
|
|
for i := 0; i < 10; i++ {
|
|
statedb, _ := ethservice.BlockChain().StateAt(parent.Root)
|
|
tx := types.MustSignNewTx(testKey, signer, &types.LegacyTx{
|
|
Nonce: statedb.GetNonce(testAddr),
|
|
Value: new(big.Int),
|
|
Gas: 1000000,
|
|
GasPrice: big.NewInt(2 * params.InitialBaseFee),
|
|
Data: logCode,
|
|
})
|
|
ethservice.TxPool().Add([]*types.Transaction{tx}, false, true)
|
|
var (
|
|
params = engine.PayloadAttributes{
|
|
Timestamp: parent.Time + 1,
|
|
Random: crypto.Keccak256Hash([]byte{byte(i)}),
|
|
SuggestedFeeRecipient: parent.Coinbase,
|
|
}
|
|
fcState = engine.ForkchoiceStateV1{
|
|
HeadBlockHash: parent.Hash(),
|
|
SafeBlockHash: common.Hash{},
|
|
FinalizedBlockHash: common.Hash{},
|
|
}
|
|
payload *engine.ExecutableData
|
|
resp engine.ForkChoiceResponse
|
|
err error
|
|
)
|
|
for i := 0; ; i++ {
|
|
if resp, err = api.ForkchoiceUpdatedV1(fcState, ¶ms); err != nil {
|
|
t.Fatalf("error preparing payload, err=%v", err)
|
|
}
|
|
if resp.PayloadStatus.Status != engine.VALID {
|
|
t.Fatalf("error preparing payload, invalid status: %v", resp.PayloadStatus.Status)
|
|
}
|
|
// give the payload some time to be built
|
|
time.Sleep(50 * time.Millisecond)
|
|
if payload, err = api.GetPayloadV1(*resp.PayloadID); err != nil {
|
|
t.Fatalf("can't get payload: %v", err)
|
|
}
|
|
if len(payload.Transactions) > 0 {
|
|
break
|
|
}
|
|
// No luck this time we need to update the params and try again.
|
|
params.Timestamp = params.Timestamp + 1
|
|
if i > 10 {
|
|
t.Fatalf("payload should not be empty")
|
|
}
|
|
}
|
|
execResp, err := api.NewPayloadV1(*payload)
|
|
if err != nil {
|
|
t.Fatalf("can't execute payload: %v", err)
|
|
}
|
|
if execResp.Status != engine.VALID {
|
|
t.Fatalf("invalid status: %v", execResp.Status)
|
|
}
|
|
fcState = engine.ForkchoiceStateV1{
|
|
HeadBlockHash: payload.BlockHash,
|
|
SafeBlockHash: payload.ParentHash,
|
|
FinalizedBlockHash: payload.ParentHash,
|
|
}
|
|
if _, err := api.ForkchoiceUpdatedV1(fcState, nil); err != nil {
|
|
t.Fatalf("Failed to insert block: %v", err)
|
|
}
|
|
if ethservice.BlockChain().CurrentBlock().Number.Uint64() != payload.Number {
|
|
t.Fatalf("Chain head should be updated")
|
|
}
|
|
parent = ethservice.BlockChain().CurrentBlock()
|
|
}
|
|
}
|
|
|
|
func assembleBlock(api *ConsensusAPI, parentHash common.Hash, params *engine.PayloadAttributes) (*engine.ExecutableData, error) {
|
|
args := &miner.BuildPayloadArgs{
|
|
Parent: parentHash,
|
|
Timestamp: params.Timestamp,
|
|
FeeRecipient: params.SuggestedFeeRecipient,
|
|
Random: params.Random,
|
|
Withdrawals: params.Withdrawals,
|
|
BeaconRoot: params.BeaconRoot,
|
|
}
|
|
payload, err := api.eth.Miner().BuildPayload(args)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return payload.ResolveFull().ExecutionPayload, nil
|
|
}
|
|
|
|
func TestEmptyBlocks(t *testing.T) {
|
|
genesis, preMergeBlocks := generateMergeChain(10, false)
|
|
n, ethservice := startEthService(t, genesis, preMergeBlocks)
|
|
defer n.Close()
|
|
|
|
commonAncestor := ethservice.BlockChain().CurrentBlock()
|
|
api := NewConsensusAPI(ethservice)
|
|
|
|
// Setup 10 blocks on the canonical chain
|
|
setupBlocks(t, ethservice, 10, commonAncestor, func(parent *types.Header) {}, nil)
|
|
|
|
// (1) check LatestValidHash by sending a normal payload (P1'')
|
|
payload := getNewPayload(t, api, commonAncestor, nil)
|
|
|
|
status, err := api.NewPayloadV1(*payload)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if status.Status != engine.VALID {
|
|
t.Errorf("invalid status: expected VALID got: %v", status.Status)
|
|
}
|
|
if !bytes.Equal(status.LatestValidHash[:], payload.BlockHash[:]) {
|
|
t.Fatalf("invalid LVH: got %v want %v", status.LatestValidHash, payload.BlockHash)
|
|
}
|
|
|
|
// (2) Now send P1' which is invalid
|
|
payload = getNewPayload(t, api, commonAncestor, nil)
|
|
payload.GasUsed += 1
|
|
payload = setBlockhash(payload)
|
|
// Now latestValidHash should be the common ancestor
|
|
status, err = api.NewPayloadV1(*payload)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if status.Status != engine.INVALID {
|
|
t.Errorf("invalid status: expected INVALID got: %v", status.Status)
|
|
}
|
|
// Expect 0x0 on INVALID block on top of PoW block
|
|
expected := common.Hash{}
|
|
if !bytes.Equal(status.LatestValidHash[:], expected[:]) {
|
|
t.Fatalf("invalid LVH: got %v want %v", status.LatestValidHash, expected)
|
|
}
|
|
|
|
// (3) Now send a payload with unknown parent
|
|
payload = getNewPayload(t, api, commonAncestor, nil)
|
|
payload.ParentHash = common.Hash{1}
|
|
payload = setBlockhash(payload)
|
|
// Now latestValidHash should be the common ancestor
|
|
status, err = api.NewPayloadV1(*payload)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if status.Status != engine.SYNCING {
|
|
t.Errorf("invalid status: expected SYNCING got: %v", status.Status)
|
|
}
|
|
if status.LatestValidHash != nil {
|
|
t.Fatalf("invalid LVH: got %v wanted nil", status.LatestValidHash)
|
|
}
|
|
}
|
|
|
|
func getNewPayload(t *testing.T, api *ConsensusAPI, parent *types.Header, withdrawals []*types.Withdrawal) *engine.ExecutableData {
|
|
params := engine.PayloadAttributes{
|
|
Timestamp: parent.Time + 1,
|
|
Random: crypto.Keccak256Hash([]byte{byte(1)}),
|
|
SuggestedFeeRecipient: parent.Coinbase,
|
|
Withdrawals: withdrawals,
|
|
}
|
|
|
|
payload, err := assembleBlock(api, parent.Hash(), ¶ms)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return payload
|
|
}
|
|
|
|
// setBlockhash sets the blockhash of a modified ExecutableData.
|
|
// Can be used to make modified payloads look valid.
|
|
func setBlockhash(data *engine.ExecutableData) *engine.ExecutableData {
|
|
txs, _ := decodeTransactions(data.Transactions)
|
|
number := big.NewInt(0)
|
|
number.SetUint64(data.Number)
|
|
header := &types.Header{
|
|
ParentHash: data.ParentHash,
|
|
UncleHash: types.EmptyUncleHash,
|
|
Coinbase: data.FeeRecipient,
|
|
Root: data.StateRoot,
|
|
TxHash: types.DeriveSha(types.Transactions(txs), trie.NewStackTrie(nil)),
|
|
ReceiptHash: data.ReceiptsRoot,
|
|
Bloom: types.BytesToBloom(data.LogsBloom),
|
|
Difficulty: common.Big0,
|
|
Number: number,
|
|
GasLimit: data.GasLimit,
|
|
GasUsed: data.GasUsed,
|
|
Time: data.Timestamp,
|
|
BaseFee: data.BaseFeePerGas,
|
|
Extra: data.ExtraData,
|
|
MixDigest: data.Random,
|
|
}
|
|
block := types.NewBlockWithHeader(header).WithBody(txs, nil /* uncles */)
|
|
data.BlockHash = block.Hash()
|
|
return data
|
|
}
|
|
|
|
func decodeTransactions(enc [][]byte) ([]*types.Transaction, error) {
|
|
var txs = make([]*types.Transaction, len(enc))
|
|
for i, encTx := range enc {
|
|
var tx types.Transaction
|
|
if err := tx.UnmarshalBinary(encTx); err != nil {
|
|
return nil, fmt.Errorf("invalid transaction %d: %v", i, err)
|
|
}
|
|
txs[i] = &tx
|
|
}
|
|
return txs, nil
|
|
}
|
|
|
|
func TestTrickRemoteBlockCache(t *testing.T) {
|
|
// Setup two nodes
|
|
genesis, preMergeBlocks := generateMergeChain(10, false)
|
|
nodeA, ethserviceA := startEthService(t, genesis, preMergeBlocks)
|
|
nodeB, ethserviceB := startEthService(t, genesis, preMergeBlocks)
|
|
defer nodeA.Close()
|
|
defer nodeB.Close()
|
|
for nodeB.Server().NodeInfo().Ports.Listener == 0 {
|
|
time.Sleep(250 * time.Millisecond)
|
|
}
|
|
nodeA.Server().AddPeer(nodeB.Server().Self())
|
|
nodeB.Server().AddPeer(nodeA.Server().Self())
|
|
apiA := NewConsensusAPI(ethserviceA)
|
|
apiB := NewConsensusAPI(ethserviceB)
|
|
|
|
commonAncestor := ethserviceA.BlockChain().CurrentBlock()
|
|
|
|
// Setup 10 blocks on the canonical chain
|
|
setupBlocks(t, ethserviceA, 10, commonAncestor, func(parent *types.Header) {}, nil)
|
|
commonAncestor = ethserviceA.BlockChain().CurrentBlock()
|
|
|
|
var invalidChain []*engine.ExecutableData
|
|
// create a valid payload (P1)
|
|
//payload1 := getNewPayload(t, apiA, commonAncestor)
|
|
//invalidChain = append(invalidChain, payload1)
|
|
|
|
// create an invalid payload2 (P2)
|
|
payload2 := getNewPayload(t, apiA, commonAncestor, nil)
|
|
//payload2.ParentHash = payload1.BlockHash
|
|
payload2.GasUsed += 1
|
|
payload2 = setBlockhash(payload2)
|
|
invalidChain = append(invalidChain, payload2)
|
|
|
|
head := payload2
|
|
// create some valid payloads on top
|
|
for i := 0; i < 10; i++ {
|
|
payload := getNewPayload(t, apiA, commonAncestor, nil)
|
|
payload.ParentHash = head.BlockHash
|
|
payload = setBlockhash(payload)
|
|
invalidChain = append(invalidChain, payload)
|
|
head = payload
|
|
}
|
|
|
|
// feed the payloads to node B
|
|
for _, payload := range invalidChain {
|
|
status, err := apiB.NewPayloadV1(*payload)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
if status.Status == engine.VALID {
|
|
t.Error("invalid status: VALID on an invalid chain")
|
|
}
|
|
// Now reorg to the head of the invalid chain
|
|
resp, err := apiB.ForkchoiceUpdatedV1(engine.ForkchoiceStateV1{HeadBlockHash: payload.BlockHash, SafeBlockHash: payload.BlockHash, FinalizedBlockHash: payload.ParentHash}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp.PayloadStatus.Status == engine.VALID {
|
|
t.Error("invalid status: VALID on an invalid chain")
|
|
}
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
func TestInvalidBloom(t *testing.T) {
|
|
genesis, preMergeBlocks := generateMergeChain(10, false)
|
|
n, ethservice := startEthService(t, genesis, preMergeBlocks)
|
|
ethservice.Merger().ReachTTD()
|
|
defer n.Close()
|
|
|
|
commonAncestor := ethservice.BlockChain().CurrentBlock()
|
|
api := NewConsensusAPI(ethservice)
|
|
|
|
// Setup 10 blocks on the canonical chain
|
|
setupBlocks(t, ethservice, 10, commonAncestor, func(parent *types.Header) {}, nil)
|
|
|
|
// (1) check LatestValidHash by sending a normal payload (P1'')
|
|
payload := getNewPayload(t, api, commonAncestor, nil)
|
|
payload.LogsBloom = append(payload.LogsBloom, byte(1))
|
|
status, err := api.NewPayloadV1(*payload)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if status.Status != engine.INVALID {
|
|
t.Errorf("invalid status: expected INVALID got: %v", status.Status)
|
|
}
|
|
}
|
|
|
|
func TestNewPayloadOnInvalidTerminalBlock(t *testing.T) {
|
|
genesis, preMergeBlocks := generateMergeChain(100, false)
|
|
n, ethservice := startEthService(t, genesis, preMergeBlocks)
|
|
defer n.Close()
|
|
api := NewConsensusAPI(ethservice)
|
|
|
|
// Test parent already post TTD in FCU
|
|
parent := preMergeBlocks[len(preMergeBlocks)-2]
|
|
fcState := engine.ForkchoiceStateV1{
|
|
HeadBlockHash: parent.Hash(),
|
|
SafeBlockHash: common.Hash{},
|
|
FinalizedBlockHash: common.Hash{},
|
|
}
|
|
resp, err := api.ForkchoiceUpdatedV1(fcState, nil)
|
|
if err != nil {
|
|
t.Fatalf("error sending forkchoice, err=%v", err)
|
|
}
|
|
if resp.PayloadStatus != engine.INVALID_TERMINAL_BLOCK {
|
|
t.Fatalf("error sending invalid forkchoice, invalid status: %v", resp.PayloadStatus.Status)
|
|
}
|
|
|
|
// Test parent already post TTD in NewPayload
|
|
args := &miner.BuildPayloadArgs{
|
|
Parent: parent.Hash(),
|
|
Timestamp: parent.Time() + 1,
|
|
Random: crypto.Keccak256Hash([]byte{byte(1)}),
|
|
FeeRecipient: parent.Coinbase(),
|
|
}
|
|
payload, err := api.eth.Miner().BuildPayload(args)
|
|
if err != nil {
|
|
t.Fatalf("error preparing payload, err=%v", err)
|
|
}
|
|
data := *payload.Resolve().ExecutionPayload
|
|
// We need to recompute the blockhash, since the miner computes a wrong (correct) blockhash
|
|
txs, _ := decodeTransactions(data.Transactions)
|
|
header := &types.Header{
|
|
ParentHash: data.ParentHash,
|
|
UncleHash: types.EmptyUncleHash,
|
|
Coinbase: data.FeeRecipient,
|
|
Root: data.StateRoot,
|
|
TxHash: types.DeriveSha(types.Transactions(txs), trie.NewStackTrie(nil)),
|
|
ReceiptHash: data.ReceiptsRoot,
|
|
Bloom: types.BytesToBloom(data.LogsBloom),
|
|
Difficulty: common.Big0,
|
|
Number: new(big.Int).SetUint64(data.Number),
|
|
GasLimit: data.GasLimit,
|
|
GasUsed: data.GasUsed,
|
|
Time: data.Timestamp,
|
|
BaseFee: data.BaseFeePerGas,
|
|
Extra: data.ExtraData,
|
|
MixDigest: data.Random,
|
|
}
|
|
block := types.NewBlockWithHeader(header).WithBody(txs, nil /* uncles */)
|
|
data.BlockHash = block.Hash()
|
|
// Send the new payload
|
|
resp2, err := api.NewPayloadV1(data)
|
|
if err != nil {
|
|
t.Fatalf("error sending NewPayload, err=%v", err)
|
|
}
|
|
if resp2 != engine.INVALID_TERMINAL_BLOCK {
|
|
t.Fatalf("error sending invalid forkchoice, invalid status: %v", resp.PayloadStatus.Status)
|
|
}
|
|
}
|
|
|
|
// TestSimultaneousNewBlock does several parallel inserts, both as
|
|
// newPayLoad and forkchoiceUpdate. This is to test that the api behaves
|
|
// well even of the caller is not being 'serial'.
|
|
func TestSimultaneousNewBlock(t *testing.T) {
|
|
genesis, preMergeBlocks := generateMergeChain(10, false)
|
|
n, ethservice := startEthService(t, genesis, preMergeBlocks)
|
|
defer n.Close()
|
|
|
|
var (
|
|
api = NewConsensusAPI(ethservice)
|
|
parent = preMergeBlocks[len(preMergeBlocks)-1]
|
|
)
|
|
for i := 0; i < 10; i++ {
|
|
execData, err := assembleBlock(api, parent.Hash(), &engine.PayloadAttributes{
|
|
Timestamp: parent.Time() + 5,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Failed to create the executable data %v", err)
|
|
}
|
|
// Insert it 10 times in parallel. Should be ignored.
|
|
{
|
|
var (
|
|
wg sync.WaitGroup
|
|
testErr error
|
|
errMu sync.Mutex
|
|
)
|
|
wg.Add(10)
|
|
for ii := 0; ii < 10; ii++ {
|
|
go func() {
|
|
defer wg.Done()
|
|
if newResp, err := api.NewPayloadV1(*execData); err != nil {
|
|
errMu.Lock()
|
|
testErr = fmt.Errorf("Failed to insert block: %w", err)
|
|
errMu.Unlock()
|
|
} else if newResp.Status != "VALID" {
|
|
errMu.Lock()
|
|
testErr = fmt.Errorf("Failed to insert block: %v", newResp.Status)
|
|
errMu.Unlock()
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
if testErr != nil {
|
|
t.Fatal(testErr)
|
|
}
|
|
}
|
|
block, err := engine.ExecutableDataToBlock(*execData, nil, nil)
|
|
if err != nil {
|
|
t.Fatalf("Failed to convert executable data to block %v", err)
|
|
}
|
|
if ethservice.BlockChain().CurrentBlock().Number.Uint64() != block.NumberU64()-1 {
|
|
t.Fatalf("Chain head shouldn't be updated")
|
|
}
|
|
fcState := engine.ForkchoiceStateV1{
|
|
HeadBlockHash: block.Hash(),
|
|
SafeBlockHash: block.Hash(),
|
|
FinalizedBlockHash: block.Hash(),
|
|
}
|
|
{
|
|
var (
|
|
wg sync.WaitGroup
|
|
testErr error
|
|
errMu sync.Mutex
|
|
)
|
|
wg.Add(10)
|
|
// Do each FCU 10 times
|
|
for ii := 0; ii < 10; ii++ {
|
|
go func() {
|
|
defer wg.Done()
|
|
if _, err := api.ForkchoiceUpdatedV1(fcState, nil); err != nil {
|
|
errMu.Lock()
|
|
testErr = fmt.Errorf("Failed to insert block: %w", err)
|
|
errMu.Unlock()
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
if testErr != nil {
|
|
t.Fatal(testErr)
|
|
}
|
|
}
|
|
if have, want := ethservice.BlockChain().CurrentBlock().Number.Uint64(), block.NumberU64(); have != want {
|
|
t.Fatalf("Chain head should be updated, have %d want %d", have, want)
|
|
}
|
|
parent = block
|
|
}
|
|
}
|
|
|
|
// TestWithdrawals creates and verifies two post-Shanghai blocks. The first
|
|
// includes zero withdrawals and the second includes two.
|
|
func TestWithdrawals(t *testing.T) {
|
|
t.Skip()
|
|
// genesis, blocks := generateMergeChain(10, true)
|
|
// // Set shanghai time to last block + 5 seconds (first post-merge block)
|
|
// time := blocks[len(blocks)-1].Time() + 5
|
|
// genesis.Config.ShanghaiBlock = &time
|
|
|
|
// n, ethservice := startEthService(t, genesis, blocks)
|
|
// ethservice.Merger().ReachTTD()
|
|
// defer n.Close()
|
|
|
|
// api := NewConsensusAPI(ethservice)
|
|
|
|
// // 10: Build Shanghai block with no withdrawals.
|
|
// parent := ethservice.BlockChain().CurrentHeader()
|
|
// blockParams := engine.PayloadAttributes{
|
|
// Timestamp: parent.Time + 5,
|
|
// Withdrawals: make([]*types.Withdrawal, 0),
|
|
// }
|
|
// fcState := engine.ForkchoiceStateV1{
|
|
// HeadBlockHash: parent.Hash(),
|
|
// }
|
|
// resp, err := api.ForkchoiceUpdatedV2(fcState, &blockParams)
|
|
// if err != nil {
|
|
// t.Fatalf("error preparing payload, err=%v", err)
|
|
// }
|
|
// if resp.PayloadStatus.Status != engine.VALID {
|
|
// t.Fatalf("unexpected status (got: %s, want: %s)", resp.PayloadStatus.Status, engine.VALID)
|
|
// }
|
|
|
|
// 10: verify state root is the same as parent
|
|
// payloadID := (&miner.BuildPayloadArgs{
|
|
// Parent: fcState.HeadBlockHash,
|
|
// Timestamp: blockParams.Timestamp,
|
|
// FeeRecipient: blockParams.SuggestedFeeRecipient,
|
|
// Random: blockParams.Random,
|
|
// Withdrawals: blockParams.Withdrawals,
|
|
// BeaconRoot: blockParams.BeaconRoot,
|
|
// }).Id()
|
|
// execData, err := api.GetPayloadV2(payloadID)
|
|
// if err != nil {
|
|
// t.Fatalf("error getting payload, err=%v", err)
|
|
// }
|
|
// if execData.ExecutionPayload.StateRoot != parent.Root {
|
|
// t.Fatalf("mismatch state roots (got: %s, want: %s)", execData.ExecutionPayload.StateRoot, blocks[8].Root())
|
|
// }
|
|
|
|
// // 10: verify locally built block
|
|
// if status, err := api.NewPayloadV2(*execData.ExecutionPayload); err != nil {
|
|
// t.Fatalf("error validating payload: %v", err)
|
|
// } else if status.Status != engine.VALID {
|
|
// t.Fatalf("invalid payload")
|
|
// }
|
|
|
|
// // 11: build shanghai block with withdrawal
|
|
// aa := common.Address{0xaa}
|
|
// bb := common.Address{0xbb}
|
|
// blockParams = engine.PayloadAttributes{
|
|
// Timestamp: execData.ExecutionPayload.Timestamp + 5,
|
|
// Withdrawals: []*types.Withdrawal{
|
|
// {
|
|
// Index: 0,
|
|
// Address: aa,
|
|
// Amount: 32,
|
|
// },
|
|
// {
|
|
// Index: 1,
|
|
// Address: bb,
|
|
// Amount: 33,
|
|
// },
|
|
// },
|
|
// }
|
|
// fcState.HeadBlockHash = execData.ExecutionPayload.BlockHash
|
|
// _, err = api.ForkchoiceUpdatedV2(fcState, &blockParams)
|
|
// if err != nil {
|
|
// t.Fatalf("error preparing payload, err=%v", err)
|
|
// }
|
|
|
|
// 11: verify locally build block.
|
|
// payloadID = (&miner.BuildPayloadArgs{
|
|
// Parent: fcState.HeadBlockHash,
|
|
// Timestamp: blockParams.Timestamp,
|
|
// FeeRecipient: blockParams.SuggestedFeeRecipient,
|
|
// Random: blockParams.Random,
|
|
// Withdrawals: blockParams.Withdrawals,
|
|
// BeaconRoot: blockParams.BeaconRoot,
|
|
// }).Id()
|
|
// execData, err = api.GetPayloadV2(payloadID)
|
|
// if err != nil {
|
|
// t.Fatalf("error getting payload, err=%v", err)
|
|
// }
|
|
// if status, err := api.NewPayloadV2(*execData.ExecutionPayload); err != nil {
|
|
// t.Fatalf("error validating payload: %v", err)
|
|
// } else if status.Status != engine.VALID {
|
|
// t.Fatalf("invalid payload")
|
|
// }
|
|
|
|
// // 11: set block as head.
|
|
// fcState.HeadBlockHash = execData.ExecutionPayload.BlockHash
|
|
// _, err = api.ForkchoiceUpdatedV2(fcState, nil)
|
|
// if err != nil {
|
|
// t.Fatalf("error preparing payload, err=%v", err)
|
|
// }
|
|
|
|
// // 11: verify withdrawals were processed.
|
|
// db, _, err := ethservice.APIBackend.StateAndHeaderByNumber(context.Background(), rpc.BlockNumber(execData.ExecutionPayload.Number))
|
|
// if err != nil {
|
|
// t.Fatalf("unable to load db: %v", err)
|
|
// }
|
|
// for i, w := range blockParams.Withdrawals {
|
|
// // w.Amount is in gwei, balance in wei
|
|
// if db.GetBalance(w.Address).Uint64() != w.Amount*params.GWei {
|
|
// t.Fatalf("failed to process withdrawal %d", i)
|
|
// }
|
|
// }
|
|
}
|
|
|
|
func TestNilWithdrawals(t *testing.T) {
|
|
t.Skip()
|
|
// genesis, blocks := generateMergeChain(10, true)
|
|
// // Set shanghai time to last block + 4 seconds (first post-merge block)
|
|
// time := blocks[len(blocks)-1].Time() + 4
|
|
// genesis.Config.ShanghaiBlock = &time
|
|
|
|
// n, ethservice := startEthService(t, genesis, blocks)
|
|
// ethservice.Merger().ReachTTD()
|
|
// defer n.Close()
|
|
|
|
// api := NewConsensusAPI(ethservice)
|
|
// parent := ethservice.BlockChain().CurrentHeader()
|
|
// aa := common.Address{0xaa}
|
|
|
|
// type test struct {
|
|
// blockParams engine.PayloadAttributes
|
|
// wantErr bool
|
|
// }
|
|
// tests := []test{
|
|
// // Before Shanghai
|
|
// {
|
|
// blockParams: engine.PayloadAttributes{
|
|
// Timestamp: parent.Time + 2,
|
|
// Withdrawals: nil,
|
|
// },
|
|
// wantErr: false,
|
|
// },
|
|
// {
|
|
// blockParams: engine.PayloadAttributes{
|
|
// Timestamp: parent.Time + 2,
|
|
// Withdrawals: make([]*types.Withdrawal, 0),
|
|
// },
|
|
// wantErr: true,
|
|
// },
|
|
// {
|
|
// blockParams: engine.PayloadAttributes{
|
|
// Timestamp: parent.Time + 2,
|
|
// Withdrawals: []*types.Withdrawal{
|
|
// {
|
|
// Index: 0,
|
|
// Address: aa,
|
|
// Amount: 32,
|
|
// },
|
|
// },
|
|
// },
|
|
// wantErr: true,
|
|
// },
|
|
// // After Shanghai
|
|
// {
|
|
// blockParams: engine.PayloadAttributes{
|
|
// Timestamp: parent.Time + 5,
|
|
// Withdrawals: nil,
|
|
// },
|
|
// wantErr: true,
|
|
// },
|
|
// {
|
|
// blockParams: engine.PayloadAttributes{
|
|
// Timestamp: parent.Time + 5,
|
|
// Withdrawals: make([]*types.Withdrawal, 0),
|
|
// },
|
|
// wantErr: false,
|
|
// },
|
|
// {
|
|
// blockParams: engine.PayloadAttributes{
|
|
// Timestamp: parent.Time + 5,
|
|
// Withdrawals: []*types.Withdrawal{
|
|
// {
|
|
// Index: 0,
|
|
// Address: aa,
|
|
// Amount: 32,
|
|
// },
|
|
// },
|
|
// },
|
|
// wantErr: false,
|
|
// },
|
|
// }
|
|
|
|
// fcState := engine.ForkchoiceStateV1{
|
|
// HeadBlockHash: parent.Hash(),
|
|
// }
|
|
|
|
// for _, test := range tests {
|
|
// _, err := api.ForkchoiceUpdatedV2(fcState, &test.blockParams)
|
|
// if test.wantErr {
|
|
// if err == nil {
|
|
// t.Fatal("wanted error on fcuv2 with invalid withdrawals")
|
|
// }
|
|
// continue
|
|
// }
|
|
// if err != nil {
|
|
// t.Fatalf("error preparing payload, err=%v", err)
|
|
// }
|
|
|
|
// // 11: verify locally build block.
|
|
// payloadID := (&miner.BuildPayloadArgs{
|
|
// Parent: fcState.HeadBlockHash,
|
|
// Timestamp: test.blockParams.Timestamp,
|
|
// FeeRecipient: test.blockParams.SuggestedFeeRecipient,
|
|
// Random: test.blockParams.Random,
|
|
// BeaconRoot: test.blockParams.BeaconRoot,
|
|
// }).Id()
|
|
// execData, err := api.GetPayloadV2(payloadID)
|
|
// if err != nil {
|
|
// t.Fatalf("error getting payload, err=%v", err)
|
|
// }
|
|
// if status, err := api.NewPayloadV2(*execData.ExecutionPayload); err != nil {
|
|
// t.Fatalf("error validating payload: %v", err)
|
|
// } else if status.Status != engine.VALID {
|
|
// t.Fatalf("invalid payload")
|
|
// }
|
|
// }
|
|
}
|
|
|
|
func setupBodies(t *testing.T) (*node.Node, *eth.Ethereum, []*types.Block) {
|
|
genesis, blocks := generateMergeChain(10, true)
|
|
// enable shanghai on the last block
|
|
genesis.Config.ShanghaiBlock = big.NewInt(int64(len(blocks) - 1))
|
|
n, ethservice := startEthService(t, genesis, blocks)
|
|
|
|
var (
|
|
parent = ethservice.BlockChain().CurrentBlock()
|
|
// This EVM code generates a log when the contract is created.
|
|
logCode = common.Hex2Bytes("60606040525b7f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405180905060405180910390a15b600a8060416000396000f360606040526008565b00")
|
|
)
|
|
|
|
callback := func(parent *types.Header) {
|
|
statedb, _ := ethservice.BlockChain().StateAt(parent.Root)
|
|
nonce := statedb.GetNonce(testAddr)
|
|
tx, _ := types.SignTx(types.NewContractCreation(nonce, new(big.Int), 1000000, big.NewInt(2*params.InitialBaseFee), logCode), types.LatestSigner(ethservice.BlockChain().Config()), testKey)
|
|
ethservice.TxPool().Add([]*types.Transaction{tx}, false, false)
|
|
}
|
|
|
|
withdrawals := make([][]*types.Withdrawal, 10)
|
|
withdrawals[0] = nil // should be filtered out by miner
|
|
withdrawals[1] = make([]*types.Withdrawal, 0)
|
|
for i := 2; i < len(withdrawals); i++ {
|
|
addr := make([]byte, 20)
|
|
crand.Read(addr)
|
|
withdrawals[i] = []*types.Withdrawal{
|
|
{Index: rand.Uint64(), Validator: rand.Uint64(), Amount: rand.Uint64(), Address: common.BytesToAddress(addr)},
|
|
}
|
|
}
|
|
|
|
postShanghaiHeaders := setupBlocks(t, ethservice, 10, parent, callback, withdrawals)
|
|
postShanghaiBlocks := make([]*types.Block, len(postShanghaiHeaders))
|
|
for i, header := range postShanghaiHeaders {
|
|
postShanghaiBlocks[i] = ethservice.BlockChain().GetBlock(header.Hash(), header.Number.Uint64())
|
|
}
|
|
return n, ethservice, append(blocks, postShanghaiBlocks...)
|
|
}
|
|
|
|
func allHashes(blocks []*types.Block) []common.Hash {
|
|
var hashes []common.Hash
|
|
for _, b := range blocks {
|
|
hashes = append(hashes, b.Hash())
|
|
}
|
|
return hashes
|
|
}
|
|
func allBodies(blocks []*types.Block) []*types.Body {
|
|
var bodies []*types.Body
|
|
for _, b := range blocks {
|
|
bodies = append(bodies, b.Body())
|
|
}
|
|
return bodies
|
|
}
|
|
|
|
func TestGetBlockBodiesByHash(t *testing.T) {
|
|
t.Skip()
|
|
node, eth, blocks := setupBodies(t)
|
|
api := NewConsensusAPI(eth)
|
|
defer node.Close()
|
|
|
|
tests := []struct {
|
|
results []*types.Body
|
|
hashes []common.Hash
|
|
}{
|
|
// First pow block
|
|
{
|
|
results: []*types.Body{eth.BlockChain().GetBlockByNumber(0).Body()},
|
|
hashes: []common.Hash{eth.BlockChain().GetBlockByNumber(0).Hash()},
|
|
},
|
|
// Last pow block
|
|
{
|
|
results: []*types.Body{blocks[9].Body()},
|
|
hashes: []common.Hash{blocks[9].Hash()},
|
|
},
|
|
// First post-merge block
|
|
{
|
|
results: []*types.Body{blocks[10].Body()},
|
|
hashes: []common.Hash{blocks[10].Hash()},
|
|
},
|
|
// Pre & post merge blocks
|
|
{
|
|
results: []*types.Body{blocks[0].Body(), blocks[9].Body(), blocks[14].Body()},
|
|
hashes: []common.Hash{blocks[0].Hash(), blocks[9].Hash(), blocks[14].Hash()},
|
|
},
|
|
// unavailable block
|
|
{
|
|
results: []*types.Body{blocks[0].Body(), nil, blocks[14].Body()},
|
|
hashes: []common.Hash{blocks[0].Hash(), {1, 2}, blocks[14].Hash()},
|
|
},
|
|
// same block multiple times
|
|
{
|
|
results: []*types.Body{blocks[0].Body(), nil, blocks[0].Body(), blocks[0].Body()},
|
|
hashes: []common.Hash{blocks[0].Hash(), {1, 2}, blocks[0].Hash(), blocks[0].Hash()},
|
|
},
|
|
// all blocks
|
|
{
|
|
results: allBodies(blocks),
|
|
hashes: allHashes(blocks),
|
|
},
|
|
}
|
|
|
|
for k, test := range tests {
|
|
result := api.GetPayloadBodiesByHashV1(test.hashes)
|
|
for i, r := range result {
|
|
if !equalBody(test.results[i], r) {
|
|
t.Fatalf("test %v: invalid response: expected %+v got %+v", k, test.results[i], r)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGetBlockBodiesByRange(t *testing.T) {
|
|
t.Skip()
|
|
node, eth, blocks := setupBodies(t)
|
|
api := NewConsensusAPI(eth)
|
|
defer node.Close()
|
|
|
|
tests := []struct {
|
|
results []*types.Body
|
|
start hexutil.Uint64
|
|
count hexutil.Uint64
|
|
}{
|
|
{
|
|
results: []*types.Body{blocks[9].Body()},
|
|
start: 10,
|
|
count: 1,
|
|
},
|
|
// Genesis
|
|
{
|
|
results: []*types.Body{blocks[0].Body()},
|
|
start: 1,
|
|
count: 1,
|
|
},
|
|
// First post-merge block
|
|
{
|
|
results: []*types.Body{blocks[9].Body()},
|
|
start: 10,
|
|
count: 1,
|
|
},
|
|
// Pre & post merge blocks
|
|
{
|
|
results: []*types.Body{blocks[7].Body(), blocks[8].Body(), blocks[9].Body(), blocks[10].Body()},
|
|
start: 8,
|
|
count: 4,
|
|
},
|
|
// unavailable block
|
|
{
|
|
results: []*types.Body{blocks[18].Body(), blocks[19].Body()},
|
|
start: 19,
|
|
count: 3,
|
|
},
|
|
// unavailable block
|
|
{
|
|
results: []*types.Body{blocks[19].Body()},
|
|
start: 20,
|
|
count: 2,
|
|
},
|
|
{
|
|
results: []*types.Body{blocks[19].Body()},
|
|
start: 20,
|
|
count: 1,
|
|
},
|
|
// whole range unavailable
|
|
{
|
|
results: make([]*types.Body, 0),
|
|
start: 22,
|
|
count: 2,
|
|
},
|
|
// allBlocks
|
|
{
|
|
results: allBodies(blocks),
|
|
start: 1,
|
|
count: hexutil.Uint64(len(blocks)),
|
|
},
|
|
}
|
|
|
|
for k, test := range tests {
|
|
result, err := api.GetPayloadBodiesByRangeV1(test.start, test.count)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(result) == len(test.results) {
|
|
for i, r := range result {
|
|
if !equalBody(test.results[i], r) {
|
|
t.Fatalf("test %d: invalid response: expected \n%+v\ngot\n%+v", k, test.results[i], r)
|
|
}
|
|
}
|
|
} else {
|
|
t.Fatalf("test %d: invalid length want %v got %v", k, len(test.results), len(result))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGetBlockBodiesByRangeInvalidParams(t *testing.T) {
|
|
t.Skip()
|
|
node, eth, _ := setupBodies(t)
|
|
api := NewConsensusAPI(eth)
|
|
defer node.Close()
|
|
tests := []struct {
|
|
start hexutil.Uint64
|
|
count hexutil.Uint64
|
|
want *engine.EngineAPIError
|
|
}{
|
|
// Genesis
|
|
{
|
|
start: 0,
|
|
count: 1,
|
|
want: engine.InvalidParams,
|
|
},
|
|
// No block requested
|
|
{
|
|
start: 1,
|
|
count: 0,
|
|
want: engine.InvalidParams,
|
|
},
|
|
// Genesis & no block
|
|
{
|
|
start: 0,
|
|
count: 0,
|
|
want: engine.InvalidParams,
|
|
},
|
|
// More than 1024 blocks
|
|
{
|
|
start: 1,
|
|
count: 1025,
|
|
want: engine.TooLargeRequest,
|
|
},
|
|
}
|
|
for i, tc := range tests {
|
|
result, err := api.GetPayloadBodiesByRangeV1(tc.start, tc.count)
|
|
if err == nil {
|
|
t.Fatalf("test %d: expected error, got %v", i, result)
|
|
}
|
|
if have, want := err.Error(), tc.want.Error(); have != want {
|
|
t.Fatalf("test %d: have %s, want %s", i, have, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func equalBody(a *types.Body, b *engine.ExecutionPayloadBodyV1) bool {
|
|
if a == nil && b == nil {
|
|
return true
|
|
} else if a == nil || b == nil {
|
|
return false
|
|
}
|
|
if len(a.Transactions) != len(b.TransactionData) {
|
|
return false
|
|
}
|
|
for i, tx := range a.Transactions {
|
|
data, _ := tx.MarshalBinary()
|
|
if !bytes.Equal(data, b.TransactionData[i]) {
|
|
return false
|
|
}
|
|
}
|
|
return reflect.DeepEqual(a.Withdrawals, b.Withdrawals)
|
|
}
|
|
|
|
func TestBlockToPayloadWithBlobs(t *testing.T) {
|
|
header := types.Header{}
|
|
var txs []*types.Transaction
|
|
|
|
inner := types.BlobTx{
|
|
BlobHashes: make([]common.Hash, 1),
|
|
}
|
|
|
|
txs = append(txs, types.NewTx(&inner))
|
|
sidecars := []*types.BlobTxSidecar{
|
|
{
|
|
Blobs: make([]kzg4844.Blob, 1),
|
|
Commitments: make([]kzg4844.Commitment, 1),
|
|
Proofs: make([]kzg4844.Proof, 1),
|
|
},
|
|
}
|
|
|
|
block := types.NewBlock(&header, txs, nil, nil, trie.NewStackTrie(nil))
|
|
envelope := engine.BlockToExecutableData(block, nil, sidecars)
|
|
var want int
|
|
for _, tx := range txs {
|
|
want += len(tx.BlobHashes())
|
|
}
|
|
if got := len(envelope.BlobsBundle.Commitments); got != want {
|
|
t.Fatalf("invalid number of commitments: got %v, want %v", got, want)
|
|
}
|
|
if got := len(envelope.BlobsBundle.Proofs); got != want {
|
|
t.Fatalf("invalid number of proofs: got %v, want %v", got, want)
|
|
}
|
|
if got := len(envelope.BlobsBundle.Blobs); got != want {
|
|
t.Fatalf("invalid number of blobs: got %v, want %v", got, want)
|
|
}
|
|
_, err := engine.ExecutableDataToBlock(*envelope.ExecutionPayload, make([]common.Hash, 1), nil)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
// This checks that beaconRoot is applied to the state from the engine API.
|
|
func TestParentBeaconBlockRoot(t *testing.T) {
|
|
t.Skip("This test is not compatible with bor")
|
|
|
|
log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(colorable.NewColorableStderr(), log.LevelTrace, true)))
|
|
|
|
genesis, blocks := generateMergeChain(10, true)
|
|
|
|
n, ethservice := startEthService(t, genesis, blocks)
|
|
ethservice.Merger().ReachTTD()
|
|
defer n.Close()
|
|
|
|
api := NewConsensusAPI(ethservice)
|
|
|
|
// 11: Build Shanghai block with no withdrawals.
|
|
parent := ethservice.BlockChain().CurrentHeader()
|
|
blockParams := engine.PayloadAttributes{
|
|
Timestamp: parent.Time + 5,
|
|
Withdrawals: make([]*types.Withdrawal, 0),
|
|
BeaconRoot: &common.Hash{42},
|
|
}
|
|
fcState := engine.ForkchoiceStateV1{
|
|
HeadBlockHash: parent.Hash(),
|
|
}
|
|
resp, err := api.ForkchoiceUpdatedV2(fcState, &blockParams)
|
|
if err != nil {
|
|
t.Fatalf("error preparing payload, err=%v", err.(*engine.EngineAPIError).ErrorData())
|
|
}
|
|
if resp.PayloadStatus.Status != engine.VALID {
|
|
t.Fatalf("unexpected status (got: %s, want: %s)", resp.PayloadStatus.Status, engine.VALID)
|
|
}
|
|
|
|
// 11: verify state root is the same as parent
|
|
payloadID := (&miner.BuildPayloadArgs{
|
|
Parent: fcState.HeadBlockHash,
|
|
Timestamp: blockParams.Timestamp,
|
|
FeeRecipient: blockParams.SuggestedFeeRecipient,
|
|
Random: blockParams.Random,
|
|
Withdrawals: blockParams.Withdrawals,
|
|
BeaconRoot: blockParams.BeaconRoot,
|
|
}).Id()
|
|
execData, err := api.GetPayloadV3(payloadID)
|
|
if err != nil {
|
|
t.Fatalf("error getting payload, err=%v", err)
|
|
}
|
|
|
|
// 11: verify locally built block
|
|
if status, err := api.NewPayloadV3(*execData.ExecutionPayload, []common.Hash{}, &common.Hash{42}); err != nil {
|
|
t.Fatalf("error validating payload: %v", err)
|
|
} else if status.Status != engine.VALID {
|
|
t.Fatalf("invalid payload")
|
|
}
|
|
|
|
fcState.HeadBlockHash = execData.ExecutionPayload.BlockHash
|
|
resp, err = api.ForkchoiceUpdatedV3(fcState, nil)
|
|
if err != nil {
|
|
t.Fatalf("error preparing payload, err=%v", err.(*engine.EngineAPIError).ErrorData())
|
|
}
|
|
if resp.PayloadStatus.Status != engine.VALID {
|
|
t.Fatalf("unexpected status (got: %s, want: %s)", resp.PayloadStatus.Status, engine.VALID)
|
|
}
|
|
|
|
// 11: verify beacon root was processed.
|
|
db, _, err := ethservice.APIBackend.StateAndHeaderByNumber(context.Background(), rpc.BlockNumber(execData.ExecutionPayload.Number))
|
|
if err != nil {
|
|
t.Fatalf("unable to load db: %v", err)
|
|
}
|
|
var (
|
|
timeIdx = common.BigToHash(big.NewInt(int64(execData.ExecutionPayload.Timestamp % 98304)))
|
|
rootIdx = common.BigToHash(big.NewInt(int64((execData.ExecutionPayload.Timestamp % 98304) + 98304)))
|
|
)
|
|
|
|
if num := db.GetState(params.BeaconRootsStorageAddress, timeIdx); num != timeIdx {
|
|
t.Fatalf("incorrect number stored: want %s, got %s", timeIdx, num)
|
|
}
|
|
if root := db.GetState(params.BeaconRootsStorageAddress, rootIdx); root != *blockParams.BeaconRoot {
|
|
t.Fatalf("incorrect root stored: want %s, got %s", *blockParams.BeaconRoot, root)
|
|
}
|
|
}
|