mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-31 17:13:57 +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>
2090 lines
66 KiB
Go
2090 lines
66 KiB
Go
// Copyright 2019 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 fetcher
|
|
|
|
import (
|
|
"errors"
|
|
"math/big"
|
|
"math/rand"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/common/mclock"
|
|
"github.com/ethereum/go-ethereum/core/txpool"
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
"github.com/ethereum/go-ethereum/params"
|
|
)
|
|
|
|
var (
|
|
// testTxs is a set of transactions to use during testing that have meaningful hashes.
|
|
testTxs = []*types.Transaction{
|
|
types.NewTransaction(5577006791947779410, common.Address{0x0f}, new(big.Int), 0, new(big.Int), nil),
|
|
types.NewTransaction(15352856648520921629, common.Address{0xbb}, new(big.Int), 0, new(big.Int), nil),
|
|
types.NewTransaction(3916589616287113937, common.Address{0x86}, new(big.Int), 0, new(big.Int), nil),
|
|
types.NewTransaction(9828766684487745566, common.Address{0xac}, new(big.Int), 0, new(big.Int), nil),
|
|
}
|
|
// testTxsHashes is the hashes of the test transactions above
|
|
testTxsHashes = []common.Hash{testTxs[0].Hash(), testTxs[1].Hash(), testTxs[2].Hash(), testTxs[3].Hash()}
|
|
testTxArrivalWait = 500 * time.Millisecond
|
|
)
|
|
|
|
type announce struct {
|
|
hash common.Hash
|
|
kind *byte
|
|
size *uint32
|
|
}
|
|
|
|
func typeptr(t byte) *byte { return &t }
|
|
func sizeptr(n uint32) *uint32 { return &n }
|
|
|
|
type doTxNotify struct {
|
|
peer string
|
|
hashes []common.Hash
|
|
types []byte
|
|
sizes []uint32
|
|
}
|
|
type doTxEnqueue struct {
|
|
peer string
|
|
txs []*types.Transaction
|
|
direct bool
|
|
}
|
|
type doWait struct {
|
|
time time.Duration
|
|
step bool
|
|
}
|
|
type doDrop string
|
|
type doFunc func()
|
|
|
|
type isWaitingWithMeta map[string][]announce
|
|
type isWaiting map[string][]common.Hash
|
|
|
|
type isScheduledWithMeta struct {
|
|
tracking map[string][]announce
|
|
fetching map[string][]common.Hash
|
|
dangling map[string][]common.Hash
|
|
}
|
|
type isScheduled struct {
|
|
tracking map[string][]common.Hash
|
|
fetching map[string][]common.Hash
|
|
dangling map[string][]common.Hash
|
|
}
|
|
type isUnderpriced int
|
|
|
|
// txFetcherTest represents a test scenario that can be executed by the test
|
|
// runner.
|
|
type txFetcherTest struct {
|
|
init func() *TxFetcher
|
|
steps []interface{}
|
|
}
|
|
|
|
// Tests that transaction announcements are added to a waitlist, and none
|
|
// of them are scheduled for retrieval until the wait expires.
|
|
func TestTransactionFetcherWaiting(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
nil,
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Initial announcement to get something into the waitlist
|
|
doTxNotify{peer: "A", hashes: []common.Hash{{0x01}, {0x02}}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
}),
|
|
// Announce from a new peer to check that no overwrite happens
|
|
doTxNotify{peer: "B", hashes: []common.Hash{{0x03}, {0x04}}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
"B": {{0x03}, {0x04}},
|
|
}),
|
|
// Announce clashing hashes but unique new peer
|
|
doTxNotify{peer: "C", hashes: []common.Hash{{0x01}, {0x04}}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
"B": {{0x03}, {0x04}},
|
|
"C": {{0x01}, {0x04}},
|
|
}),
|
|
// Announce existing and clashing hashes from existing peer
|
|
doTxNotify{peer: "A", hashes: []common.Hash{{0x01}, {0x03}, {0x05}}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}, {0x03}, {0x05}},
|
|
"B": {{0x03}, {0x04}},
|
|
"C": {{0x01}, {0x04}},
|
|
}),
|
|
isScheduled{tracking: nil, fetching: nil},
|
|
|
|
// Wait for the arrival timeout which should move all expired items
|
|
// from the wait list to the scheduler
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}, {0x03}, {0x05}},
|
|
"B": {{0x03}, {0x04}},
|
|
"C": {{0x01}, {0x04}},
|
|
},
|
|
fetching: map[string][]common.Hash{ // Depends on deterministic test randomizer
|
|
"A": {{0x02}, {0x03}, {0x05}},
|
|
"C": {{0x01}, {0x04}},
|
|
},
|
|
},
|
|
// Queue up a non-fetchable transaction and then trigger it with a new
|
|
// peer (weird case to test 1 line in the fetcher)
|
|
doTxNotify{peer: "C", hashes: []common.Hash{{0x06}, {0x07}}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"C": {{0x06}, {0x07}},
|
|
}),
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}, {0x03}, {0x05}},
|
|
"B": {{0x03}, {0x04}},
|
|
"C": {{0x01}, {0x04}, {0x06}, {0x07}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x02}, {0x03}, {0x05}},
|
|
"C": {{0x01}, {0x04}},
|
|
},
|
|
},
|
|
doTxNotify{peer: "D", hashes: []common.Hash{{0x06}, {0x07}}},
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}, {0x03}, {0x05}},
|
|
"B": {{0x03}, {0x04}},
|
|
"C": {{0x01}, {0x04}, {0x06}, {0x07}},
|
|
"D": {{0x06}, {0x07}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x02}, {0x03}, {0x05}},
|
|
"C": {{0x01}, {0x04}},
|
|
"D": {{0x06}, {0x07}},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that transaction announcements with associated metadata are added to a
|
|
// waitlist, and none of them are scheduled for retrieval until the wait expires.
|
|
//
|
|
// This test is an extended version of TestTransactionFetcherWaiting. It's mostly
|
|
// to cover the metadata checks without bloating up the basic behavioral tests
|
|
// with all the useless extra fields.
|
|
func TestTransactionFetcherWaitingWithMeta(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
nil,
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Initial announcement to get something into the waitlist
|
|
doTxNotify{peer: "A", hashes: []common.Hash{{0x01}, {0x02}}, types: []byte{types.LegacyTxType, types.LegacyTxType}, sizes: []uint32{111, 222}},
|
|
isWaitingWithMeta(map[string][]announce{
|
|
"A": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(111)},
|
|
{common.Hash{0x02}, typeptr(types.LegacyTxType), sizeptr(222)},
|
|
},
|
|
}),
|
|
// Announce from a new peer to check that no overwrite happens
|
|
doTxNotify{peer: "B", hashes: []common.Hash{{0x03}, {0x04}}, types: []byte{types.LegacyTxType, types.LegacyTxType}, sizes: []uint32{333, 444}},
|
|
isWaitingWithMeta(map[string][]announce{
|
|
"A": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(111)},
|
|
{common.Hash{0x02}, typeptr(types.LegacyTxType), sizeptr(222)},
|
|
},
|
|
"B": {
|
|
{common.Hash{0x03}, typeptr(types.LegacyTxType), sizeptr(333)},
|
|
{common.Hash{0x04}, typeptr(types.LegacyTxType), sizeptr(444)},
|
|
},
|
|
}),
|
|
// Announce clashing hashes but unique new peer
|
|
doTxNotify{peer: "C", hashes: []common.Hash{{0x01}, {0x04}}, types: []byte{types.LegacyTxType, types.LegacyTxType}, sizes: []uint32{111, 444}},
|
|
isWaitingWithMeta(map[string][]announce{
|
|
"A": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(111)},
|
|
{common.Hash{0x02}, typeptr(types.LegacyTxType), sizeptr(222)},
|
|
},
|
|
"B": {
|
|
{common.Hash{0x03}, typeptr(types.LegacyTxType), sizeptr(333)},
|
|
{common.Hash{0x04}, typeptr(types.LegacyTxType), sizeptr(444)},
|
|
},
|
|
"C": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(111)},
|
|
{common.Hash{0x04}, typeptr(types.LegacyTxType), sizeptr(444)},
|
|
},
|
|
}),
|
|
// Announce existing and clashing hashes from existing peer. Clashes
|
|
// should not overwrite previous announcements.
|
|
doTxNotify{peer: "A", hashes: []common.Hash{{0x01}, {0x03}, {0x05}}, types: []byte{types.LegacyTxType, types.LegacyTxType, types.LegacyTxType}, sizes: []uint32{999, 333, 555}},
|
|
isWaitingWithMeta(map[string][]announce{
|
|
"A": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(111)},
|
|
{common.Hash{0x02}, typeptr(types.LegacyTxType), sizeptr(222)},
|
|
{common.Hash{0x03}, typeptr(types.LegacyTxType), sizeptr(333)},
|
|
{common.Hash{0x05}, typeptr(types.LegacyTxType), sizeptr(555)},
|
|
},
|
|
"B": {
|
|
{common.Hash{0x03}, typeptr(types.LegacyTxType), sizeptr(333)},
|
|
{common.Hash{0x04}, typeptr(types.LegacyTxType), sizeptr(444)},
|
|
},
|
|
"C": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(111)},
|
|
{common.Hash{0x04}, typeptr(types.LegacyTxType), sizeptr(444)},
|
|
},
|
|
}),
|
|
// Announce clashing hashes with conflicting metadata. Somebody will
|
|
// be in the wrong, but we don't know yet who.
|
|
doTxNotify{peer: "D", hashes: []common.Hash{{0x01}, {0x02}}, types: []byte{types.LegacyTxType, types.BlobTxType}, sizes: []uint32{999, 222}},
|
|
isWaitingWithMeta(map[string][]announce{
|
|
"A": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(111)},
|
|
{common.Hash{0x02}, typeptr(types.LegacyTxType), sizeptr(222)},
|
|
{common.Hash{0x03}, typeptr(types.LegacyTxType), sizeptr(333)},
|
|
{common.Hash{0x05}, typeptr(types.LegacyTxType), sizeptr(555)},
|
|
},
|
|
"B": {
|
|
{common.Hash{0x03}, typeptr(types.LegacyTxType), sizeptr(333)},
|
|
{common.Hash{0x04}, typeptr(types.LegacyTxType), sizeptr(444)},
|
|
},
|
|
"C": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(111)},
|
|
{common.Hash{0x04}, typeptr(types.LegacyTxType), sizeptr(444)},
|
|
},
|
|
"D": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(999)},
|
|
{common.Hash{0x02}, typeptr(types.BlobTxType), sizeptr(222)},
|
|
},
|
|
}),
|
|
isScheduled{tracking: nil, fetching: nil},
|
|
|
|
// Wait for the arrival timeout which should move all expired items
|
|
// from the wait list to the scheduler
|
|
doWait{time: txArriveTimeout, step: true},
|
|
isWaiting(nil),
|
|
isScheduledWithMeta{
|
|
tracking: map[string][]announce{
|
|
"A": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(111)},
|
|
{common.Hash{0x02}, typeptr(types.LegacyTxType), sizeptr(222)},
|
|
{common.Hash{0x03}, typeptr(types.LegacyTxType), sizeptr(333)},
|
|
{common.Hash{0x05}, typeptr(types.LegacyTxType), sizeptr(555)},
|
|
},
|
|
"B": {
|
|
{common.Hash{0x03}, typeptr(types.LegacyTxType), sizeptr(333)},
|
|
{common.Hash{0x04}, typeptr(types.LegacyTxType), sizeptr(444)},
|
|
},
|
|
"C": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(111)},
|
|
{common.Hash{0x04}, typeptr(types.LegacyTxType), sizeptr(444)},
|
|
},
|
|
"D": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(999)},
|
|
{common.Hash{0x02}, typeptr(types.BlobTxType), sizeptr(222)},
|
|
},
|
|
},
|
|
fetching: map[string][]common.Hash{ // Depends on deterministic test randomizer
|
|
"A": {{0x03}, {0x05}},
|
|
"C": {{0x01}, {0x04}},
|
|
"D": {{0x02}},
|
|
},
|
|
},
|
|
// Queue up a non-fetchable transaction and then trigger it with a new
|
|
// peer (weird case to test 1 line in the fetcher)
|
|
doTxNotify{peer: "C", hashes: []common.Hash{{0x06}, {0x07}}, types: []byte{types.LegacyTxType, types.LegacyTxType}, sizes: []uint32{666, 777}},
|
|
isWaitingWithMeta(map[string][]announce{
|
|
"C": {
|
|
{common.Hash{0x06}, typeptr(types.LegacyTxType), sizeptr(666)},
|
|
{common.Hash{0x07}, typeptr(types.LegacyTxType), sizeptr(777)},
|
|
},
|
|
}),
|
|
doWait{time: txArriveTimeout, step: true},
|
|
isScheduledWithMeta{
|
|
tracking: map[string][]announce{
|
|
"A": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(111)},
|
|
{common.Hash{0x02}, typeptr(types.LegacyTxType), sizeptr(222)},
|
|
{common.Hash{0x03}, typeptr(types.LegacyTxType), sizeptr(333)},
|
|
{common.Hash{0x05}, typeptr(types.LegacyTxType), sizeptr(555)},
|
|
},
|
|
"B": {
|
|
{common.Hash{0x03}, typeptr(types.LegacyTxType), sizeptr(333)},
|
|
{common.Hash{0x04}, typeptr(types.LegacyTxType), sizeptr(444)},
|
|
},
|
|
"C": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(111)},
|
|
{common.Hash{0x04}, typeptr(types.LegacyTxType), sizeptr(444)},
|
|
{common.Hash{0x06}, typeptr(types.LegacyTxType), sizeptr(666)},
|
|
{common.Hash{0x07}, typeptr(types.LegacyTxType), sizeptr(777)},
|
|
},
|
|
"D": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(999)},
|
|
{common.Hash{0x02}, typeptr(types.BlobTxType), sizeptr(222)},
|
|
},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x03}, {0x05}},
|
|
"C": {{0x01}, {0x04}},
|
|
"D": {{0x02}},
|
|
},
|
|
},
|
|
doTxNotify{peer: "E", hashes: []common.Hash{{0x06}, {0x07}}, types: []byte{types.LegacyTxType, types.LegacyTxType}, sizes: []uint32{666, 777}},
|
|
isScheduledWithMeta{
|
|
tracking: map[string][]announce{
|
|
"A": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(111)},
|
|
{common.Hash{0x02}, typeptr(types.LegacyTxType), sizeptr(222)},
|
|
{common.Hash{0x03}, typeptr(types.LegacyTxType), sizeptr(333)},
|
|
{common.Hash{0x05}, typeptr(types.LegacyTxType), sizeptr(555)},
|
|
},
|
|
"B": {
|
|
{common.Hash{0x03}, typeptr(types.LegacyTxType), sizeptr(333)},
|
|
{common.Hash{0x04}, typeptr(types.LegacyTxType), sizeptr(444)},
|
|
},
|
|
"C": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(111)},
|
|
{common.Hash{0x04}, typeptr(types.LegacyTxType), sizeptr(444)},
|
|
{common.Hash{0x06}, typeptr(types.LegacyTxType), sizeptr(666)},
|
|
{common.Hash{0x07}, typeptr(types.LegacyTxType), sizeptr(777)},
|
|
},
|
|
"D": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(999)},
|
|
{common.Hash{0x02}, typeptr(types.BlobTxType), sizeptr(222)},
|
|
},
|
|
"E": {
|
|
{common.Hash{0x06}, typeptr(types.LegacyTxType), sizeptr(666)},
|
|
{common.Hash{0x07}, typeptr(types.LegacyTxType), sizeptr(777)},
|
|
},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x03}, {0x05}},
|
|
"C": {{0x01}, {0x04}},
|
|
"D": {{0x02}},
|
|
"E": {{0x06}, {0x07}},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that transaction announcements skip the waiting list if they are
|
|
// already scheduled.
|
|
func TestTransactionFetcherSkipWaiting(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
nil,
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Push an initial announcement through to the scheduled stage
|
|
doTxNotify{peer: "A", hashes: []common.Hash{{0x01}, {0x02}}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
}),
|
|
isScheduled{tracking: nil, fetching: nil},
|
|
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
},
|
|
},
|
|
// Announce overlaps from the same peer, ensure the new ones end up
|
|
// in stage one, and clashing ones don't get double tracked
|
|
doTxNotify{peer: "A", hashes: []common.Hash{{0x02}, {0x03}}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {{0x03}},
|
|
}),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
},
|
|
},
|
|
// Announce overlaps from a new peer, ensure new transactions end up
|
|
// in stage one and clashing ones get tracked for the new peer
|
|
doTxNotify{peer: "B", hashes: []common.Hash{{0x02}, {0x03}, {0x04}}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {{0x03}},
|
|
"B": {{0x03}, {0x04}},
|
|
}),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
"B": {{0x02}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that only a single transaction request gets scheduled to a peer
|
|
// and subsequent announces block or get allotted to someone else.
|
|
func TestTransactionFetcherSingletonRequesting(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
nil,
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Push an initial announcement through to the scheduled stage
|
|
doTxNotify{peer: "A", hashes: []common.Hash{{0x01}, {0x02}}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
}),
|
|
isScheduled{tracking: nil, fetching: nil},
|
|
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
},
|
|
},
|
|
// Announce a new set of transactions from the same peer and ensure
|
|
// they do not start fetching since the peer is already busy
|
|
doTxNotify{peer: "A", hashes: []common.Hash{{0x03}, {0x04}}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {{0x03}, {0x04}},
|
|
}),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
},
|
|
},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}, {0x03}, {0x04}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
},
|
|
},
|
|
// Announce a duplicate set of transactions from a new peer and ensure
|
|
// uniquely new ones start downloading, even if clashing.
|
|
doTxNotify{peer: "B", hashes: []common.Hash{{0x02}, {0x03}, {0x05}, {0x06}}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"B": {{0x05}, {0x06}},
|
|
}),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}, {0x03}, {0x04}},
|
|
"B": {{0x02}, {0x03}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
"B": {{0x03}},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that if a transaction retrieval fails, all the transactions get
|
|
// instantly schedule back to someone else or the announcements dropped
|
|
// if no alternate source is available.
|
|
func TestTransactionFetcherFailedRescheduling(t *testing.T) {
|
|
// Create a channel to control when tx requests can fail
|
|
proceed := make(chan struct{})
|
|
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
nil,
|
|
func(origin string, hashes []common.Hash) error {
|
|
<-proceed
|
|
return errors.New("peer disconnected")
|
|
},
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Push an initial announcement through to the scheduled stage
|
|
doTxNotify{peer: "A", hashes: []common.Hash{{0x01}, {0x02}}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
}),
|
|
isScheduled{tracking: nil, fetching: nil},
|
|
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
},
|
|
},
|
|
// While the original peer is stuck in the request, push in an second
|
|
// data source.
|
|
doTxNotify{peer: "B", hashes: []common.Hash{{0x02}}},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
"B": {{0x02}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
},
|
|
},
|
|
// Wait until the original request fails and check that transactions
|
|
// are either rescheduled or dropped
|
|
doFunc(func() {
|
|
proceed <- struct{}{} // Allow peer A to return the failure
|
|
}),
|
|
doWait{time: 0, step: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"B": {{0x02}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"B": {{0x02}},
|
|
},
|
|
},
|
|
doFunc(func() {
|
|
proceed <- struct{}{} // Allow peer B to return the failure
|
|
}),
|
|
doWait{time: 0, step: true},
|
|
isWaiting(nil),
|
|
isScheduled{nil, nil, nil},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that if a transaction retrieval succeeds, all alternate origins
|
|
// are cleaned up.
|
|
func TestTransactionFetcherCleanup(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
func(txs []*types.Transaction) []error {
|
|
return make([]error, len(txs))
|
|
},
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Push an initial announcement through to the scheduled stage
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[0]}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {testTxsHashes[0]},
|
|
}),
|
|
isScheduled{tracking: nil, fetching: nil},
|
|
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0]},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0]},
|
|
},
|
|
},
|
|
// Request should be delivered
|
|
doTxEnqueue{peer: "A", txs: []*types.Transaction{testTxs[0]}, direct: true},
|
|
isScheduled{nil, nil, nil},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that if a transaction retrieval succeeds, but the response is empty (no
|
|
// transactions available, then all are nuked instead of being rescheduled (yes,
|
|
// this was a bug)).
|
|
func TestTransactionFetcherCleanupEmpty(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
func(txs []*types.Transaction) []error {
|
|
return make([]error, len(txs))
|
|
},
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Push an initial announcement through to the scheduled stage
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[0]}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {testTxsHashes[0]},
|
|
}),
|
|
isScheduled{tracking: nil, fetching: nil},
|
|
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0]},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0]},
|
|
},
|
|
},
|
|
// Deliver an empty response and ensure the transaction is cleared, not rescheduled
|
|
doTxEnqueue{peer: "A", txs: []*types.Transaction{}, direct: true},
|
|
isScheduled{nil, nil, nil},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that non-returned transactions are either re-scheduled from a
|
|
// different peer, or self if they are after the cutoff point.
|
|
func TestTransactionFetcherMissingRescheduling(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
func(txs []*types.Transaction) []error {
|
|
return make([]error, len(txs))
|
|
},
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Push an initial announcement through to the scheduled stage
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[0], testTxsHashes[1], testTxsHashes[2]}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {testTxsHashes[0], testTxsHashes[1], testTxsHashes[2]},
|
|
}),
|
|
isScheduled{tracking: nil, fetching: nil},
|
|
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0], testTxsHashes[1], testTxsHashes[2]},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0], testTxsHashes[1], testTxsHashes[2]},
|
|
},
|
|
},
|
|
// Deliver the middle transaction requested, the one before which
|
|
// should be dropped and the one after re-requested.
|
|
doTxEnqueue{peer: "A", txs: []*types.Transaction{testTxs[0]}, direct: true}, // This depends on the deterministic random
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {testTxsHashes[2]},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {testTxsHashes[2]},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that out of two transactions, if one is missing and the last is
|
|
// delivered, the peer gets properly cleaned out from the internal state.
|
|
func TestTransactionFetcherMissingCleanup(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
func(txs []*types.Transaction) []error {
|
|
return make([]error, len(txs))
|
|
},
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Push an initial announcement through to the scheduled stage
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[0], testTxsHashes[1]}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {testTxsHashes[0], testTxsHashes[1]},
|
|
}),
|
|
isScheduled{tracking: nil, fetching: nil},
|
|
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0], testTxsHashes[1]},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0], testTxsHashes[1]},
|
|
},
|
|
},
|
|
// Deliver the middle transaction requested, the one before which
|
|
// should be dropped and the one after re-requested.
|
|
doTxEnqueue{peer: "A", txs: []*types.Transaction{testTxs[1]}, direct: true}, // This depends on the deterministic random
|
|
isScheduled{nil, nil, nil},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that transaction broadcasts properly clean up announcements.
|
|
func TestTransactionFetcherBroadcasts(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
func(txs []*types.Transaction) []error {
|
|
return make([]error, len(txs))
|
|
},
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Set up three transactions to be in different stats, waiting, queued and fetching
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[0]}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[1]}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[2]}},
|
|
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {testTxsHashes[2]},
|
|
}),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0], testTxsHashes[1]},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0]},
|
|
},
|
|
},
|
|
// Broadcast all the transactions and ensure everything gets cleaned
|
|
// up, but the dangling request is left alone to avoid doing multiple
|
|
// concurrent requests.
|
|
doTxEnqueue{peer: "A", txs: []*types.Transaction{testTxs[0], testTxs[1], testTxs[2]}, direct: false},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: nil,
|
|
fetching: nil,
|
|
dangling: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0]},
|
|
},
|
|
},
|
|
// Deliver the requested hashes
|
|
doTxEnqueue{peer: "A", txs: []*types.Transaction{testTxs[0], testTxs[1], testTxs[2]}, direct: true},
|
|
isScheduled{nil, nil, nil},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that the waiting list timers properly reset and reschedule.
|
|
func TestTransactionFetcherWaitTimerResets(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
nil,
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
doTxNotify{peer: "A", hashes: []common.Hash{{0x01}}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {{0x01}},
|
|
}),
|
|
isScheduled{nil, nil, nil},
|
|
doWait{time: testTxArrivalWait / 2, step: false},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {{0x01}},
|
|
}),
|
|
isScheduled{nil, nil, nil},
|
|
|
|
doTxNotify{peer: "A", hashes: []common.Hash{{0x02}}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
}),
|
|
isScheduled{nil, nil, nil},
|
|
doWait{time: testTxArrivalWait / 2, step: true},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {{0x02}},
|
|
}),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {{0x01}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x01}},
|
|
},
|
|
},
|
|
|
|
doWait{time: testTxArrivalWait / 2, step: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x01}},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that if a transaction request is not replied to, it will time
|
|
// out and be re-scheduled for someone else.
|
|
func TestTransactionFetcherTimeoutRescheduling(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
func(txs []*types.Transaction) []error {
|
|
return make([]error, len(txs))
|
|
},
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Push an initial announcement through to the scheduled stage
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[0]}},
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {testTxsHashes[0]},
|
|
}),
|
|
isScheduled{tracking: nil, fetching: nil},
|
|
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0]},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0]},
|
|
},
|
|
},
|
|
// Wait until the delivery times out, everything should be cleaned up
|
|
doWait{time: txFetchTimeout, step: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: nil,
|
|
fetching: nil,
|
|
dangling: map[string][]common.Hash{
|
|
"A": {},
|
|
},
|
|
},
|
|
// Ensure that followup announcements don't get scheduled
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[1]}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {testTxsHashes[1]},
|
|
},
|
|
fetching: nil,
|
|
dangling: map[string][]common.Hash{
|
|
"A": {},
|
|
},
|
|
},
|
|
// If the dangling request arrives a bit later, do not choke
|
|
doTxEnqueue{peer: "A", txs: []*types.Transaction{testTxs[0]}, direct: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {testTxsHashes[1]},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {testTxsHashes[1]},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that the fetching timeout timers properly reset and reschedule.
|
|
func TestTransactionFetcherTimeoutTimerResets(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
nil,
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
doTxNotify{peer: "A", hashes: []common.Hash{{0x01}}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
doTxNotify{peer: "B", hashes: []common.Hash{{0x02}}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {{0x01}},
|
|
"B": {{0x02}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x01}},
|
|
"B": {{0x02}},
|
|
},
|
|
},
|
|
doWait{time: txFetchTimeout - testTxArrivalWait, step: true},
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"B": {{0x02}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"B": {{0x02}},
|
|
},
|
|
dangling: map[string][]common.Hash{
|
|
"A": {},
|
|
},
|
|
},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
isScheduled{
|
|
tracking: nil,
|
|
fetching: nil,
|
|
dangling: map[string][]common.Hash{
|
|
"A": {},
|
|
"B": {},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that if thousands of transactions are announced, only a small
|
|
// number of them will be requested at a time.
|
|
func TestTransactionFetcherRateLimiting(t *testing.T) {
|
|
// Create a slew of transactions and announce them
|
|
var hashes []common.Hash
|
|
for i := 0; i < maxTxAnnounces; i++ {
|
|
hashes = append(hashes, common.Hash{byte(i / 256), byte(i % 256)})
|
|
}
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
nil,
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Announce all the transactions, wait a bit and ensure only a small
|
|
// percentage gets requested
|
|
doTxNotify{peer: "A", hashes: hashes},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": hashes,
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": hashes[1643 : 1643+maxTxRetrievals],
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that if huge transactions are announced, only a small number of them will
|
|
// be requested at a time, to keep the responses below a reasonable level.
|
|
func TestTransactionFetcherBandwidthLimiting(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
nil,
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Announce mid size transactions from A to verify that multiple
|
|
// ones can be piled into a single request.
|
|
doTxNotify{peer: "A",
|
|
hashes: []common.Hash{{0x01}, {0x02}, {0x03}, {0x04}},
|
|
types: []byte{types.LegacyTxType, types.LegacyTxType, types.LegacyTxType, types.LegacyTxType},
|
|
sizes: []uint32{48 * 1024, 48 * 1024, 48 * 1024, 48 * 1024},
|
|
},
|
|
// Announce exactly on the limit transactions to see that only one
|
|
// gets requested
|
|
doTxNotify{peer: "B",
|
|
hashes: []common.Hash{{0x05}, {0x06}},
|
|
types: []byte{types.LegacyTxType, types.LegacyTxType},
|
|
sizes: []uint32{maxTxRetrievalSize, maxTxRetrievalSize},
|
|
},
|
|
// Announce oversized blob transactions to see that overflows are ok
|
|
doTxNotify{peer: "C",
|
|
hashes: []common.Hash{{0x07}, {0x08}},
|
|
types: []byte{types.BlobTxType, types.BlobTxType},
|
|
sizes: []uint32{params.MaxBlobGasPerBlock, params.MaxBlobGasPerBlock},
|
|
},
|
|
doWait{time: txArriveTimeout, step: true},
|
|
isWaiting(nil),
|
|
isScheduledWithMeta{
|
|
tracking: map[string][]announce{
|
|
"A": {
|
|
{common.Hash{0x01}, typeptr(types.LegacyTxType), sizeptr(48 * 1024)},
|
|
{common.Hash{0x02}, typeptr(types.LegacyTxType), sizeptr(48 * 1024)},
|
|
{common.Hash{0x03}, typeptr(types.LegacyTxType), sizeptr(48 * 1024)},
|
|
{common.Hash{0x04}, typeptr(types.LegacyTxType), sizeptr(48 * 1024)},
|
|
},
|
|
"B": {
|
|
{common.Hash{0x05}, typeptr(types.LegacyTxType), sizeptr(maxTxRetrievalSize)},
|
|
{common.Hash{0x06}, typeptr(types.LegacyTxType), sizeptr(maxTxRetrievalSize)},
|
|
},
|
|
"C": {
|
|
{common.Hash{0x07}, typeptr(types.BlobTxType), sizeptr(params.MaxBlobGasPerBlock)},
|
|
{common.Hash{0x08}, typeptr(types.BlobTxType), sizeptr(params.MaxBlobGasPerBlock)},
|
|
},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x02}, {0x03}, {0x04}},
|
|
"B": {{0x06}},
|
|
"C": {{0x08}},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that then number of transactions a peer is allowed to announce and/or
|
|
// request at the same time is hard capped.
|
|
func TestTransactionFetcherDoSProtection(t *testing.T) {
|
|
// Create a slew of transactions and to announce them
|
|
var hashesA []common.Hash
|
|
for i := 0; i < maxTxAnnounces+1; i++ {
|
|
hashesA = append(hashesA, common.Hash{0x01, byte(i / 256), byte(i % 256)})
|
|
}
|
|
|
|
var hashesB []common.Hash
|
|
for i := 0; i < maxTxAnnounces+1; i++ {
|
|
hashesB = append(hashesB, common.Hash{0x02, byte(i / 256), byte(i % 256)})
|
|
}
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
nil,
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Announce half of the transaction and wait for them to be scheduled
|
|
doTxNotify{peer: "A", hashes: hashesA[:maxTxAnnounces/2]},
|
|
doTxNotify{peer: "B", hashes: hashesB[:maxTxAnnounces/2-1]},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
|
|
// Announce the second half and keep them in the wait list
|
|
doTxNotify{peer: "A", hashes: hashesA[maxTxAnnounces/2 : maxTxAnnounces]},
|
|
doTxNotify{peer: "B", hashes: hashesB[maxTxAnnounces/2-1 : maxTxAnnounces-1]},
|
|
|
|
// Ensure the hashes are split half and half
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": hashesA[maxTxAnnounces/2 : maxTxAnnounces],
|
|
"B": hashesB[maxTxAnnounces/2-1 : maxTxAnnounces-1],
|
|
}),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": hashesA[:maxTxAnnounces/2],
|
|
"B": hashesB[:maxTxAnnounces/2-1],
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": hashesA[1643 : 1643+maxTxRetrievals],
|
|
"B": append(append([]common.Hash{}, hashesB[maxTxAnnounces/2-3:maxTxAnnounces/2-1]...), hashesB[:maxTxRetrievals-2]...),
|
|
},
|
|
},
|
|
// Ensure that adding even one more hash results in dropping the hash
|
|
doTxNotify{peer: "A", hashes: []common.Hash{hashesA[maxTxAnnounces]}},
|
|
doTxNotify{peer: "B", hashes: hashesB[maxTxAnnounces-1 : maxTxAnnounces+1]},
|
|
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": hashesA[maxTxAnnounces/2 : maxTxAnnounces],
|
|
"B": hashesB[maxTxAnnounces/2-1 : maxTxAnnounces],
|
|
}),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": hashesA[:maxTxAnnounces/2],
|
|
"B": hashesB[:maxTxAnnounces/2-1],
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": hashesA[1643 : 1643+maxTxRetrievals],
|
|
"B": append(append([]common.Hash{}, hashesB[maxTxAnnounces/2-3:maxTxAnnounces/2-1]...), hashesB[:maxTxRetrievals-2]...),
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that underpriced transactions don't get rescheduled after being rejected.
|
|
func TestTransactionFetcherUnderpricedDedup(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
func(txs []*types.Transaction) []error {
|
|
errs := make([]error, len(txs))
|
|
for i := 0; i < len(errs); i++ {
|
|
if i%2 == 0 {
|
|
errs[i] = txpool.ErrUnderpriced
|
|
} else {
|
|
errs[i] = txpool.ErrReplaceUnderpriced
|
|
}
|
|
}
|
|
return errs
|
|
},
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Deliver a transaction through the fetcher, but reject as underpriced
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[0], testTxsHashes[1]}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
doTxEnqueue{peer: "A", txs: []*types.Transaction{testTxs[0], testTxs[1]}, direct: true},
|
|
isScheduled{nil, nil, nil},
|
|
|
|
// Try to announce the transaction again, ensure it's not scheduled back
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[0], testTxsHashes[1], testTxsHashes[2]}}, // [2] is needed to force a step in the fetcher
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {testTxsHashes[2]},
|
|
}),
|
|
isScheduled{nil, nil, nil},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that underpriced transactions don't get rescheduled after being rejected,
|
|
// but at the same time there's a hard cap on the number of transactions that are
|
|
// tracked.
|
|
func TestTransactionFetcherUnderpricedDoSProtection(t *testing.T) {
|
|
// Temporarily disable fetch timeouts as they massively mess up the simulated clock
|
|
defer func(timeout time.Duration) { txFetchTimeout = timeout }(txFetchTimeout)
|
|
txFetchTimeout = 24 * time.Hour
|
|
|
|
// Create a slew of transactions to max out the underpriced set
|
|
var txs []*types.Transaction
|
|
for i := 0; i < maxTxUnderpricedSetSize+1; i++ {
|
|
txs = append(txs, types.NewTransaction(rand.Uint64(), common.Address{byte(rand.Intn(256))}, new(big.Int), 0, new(big.Int), nil))
|
|
}
|
|
|
|
hashes := make([]common.Hash, len(txs))
|
|
for i, tx := range txs {
|
|
hashes[i] = tx.Hash()
|
|
}
|
|
// Generate a set of steps to announce and deliver the entire set of transactions
|
|
var steps []interface{}
|
|
for i := 0; i < maxTxUnderpricedSetSize/maxTxRetrievals; i++ {
|
|
steps = append(steps, doTxNotify{peer: "A", hashes: hashes[i*maxTxRetrievals : (i+1)*maxTxRetrievals]})
|
|
steps = append(steps, isWaiting(map[string][]common.Hash{
|
|
"A": hashes[i*maxTxRetrievals : (i+1)*maxTxRetrievals],
|
|
}))
|
|
steps = append(steps, doWait{time: testTxArrivalWait, step: true})
|
|
steps = append(steps, isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": hashes[i*maxTxRetrievals : (i+1)*maxTxRetrievals],
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": hashes[i*maxTxRetrievals : (i+1)*maxTxRetrievals],
|
|
},
|
|
})
|
|
steps = append(steps, doTxEnqueue{peer: "A", txs: txs[i*maxTxRetrievals : (i+1)*maxTxRetrievals], direct: true})
|
|
steps = append(steps, isWaiting(nil))
|
|
steps = append(steps, isScheduled{nil, nil, nil})
|
|
steps = append(steps, isUnderpriced((i+1)*maxTxRetrievals))
|
|
}
|
|
testTransactionFetcher(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
func(txs []*types.Transaction) []error {
|
|
errs := make([]error, len(txs))
|
|
for i := 0; i < len(errs); i++ {
|
|
errs[i] = txpool.ErrUnderpriced
|
|
}
|
|
return errs
|
|
},
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: append(steps, []interface{}{
|
|
// The preparation of the test has already been done in `steps`, add the last check
|
|
doTxNotify{peer: "A", hashes: []common.Hash{hashes[maxTxUnderpricedSetSize]}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
doTxEnqueue{peer: "A", txs: []*types.Transaction{txs[maxTxUnderpricedSetSize]}, direct: true},
|
|
isUnderpriced(maxTxUnderpricedSetSize),
|
|
}...),
|
|
})
|
|
}
|
|
|
|
// Tests that unexpected deliveries don't corrupt the internal state.
|
|
func TestTransactionFetcherOutOfBoundDeliveries(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
func(txs []*types.Transaction) []error {
|
|
return make([]error, len(txs))
|
|
},
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Deliver something out of the blue
|
|
isWaiting(nil),
|
|
isScheduled{nil, nil, nil},
|
|
doTxEnqueue{peer: "A", txs: []*types.Transaction{testTxs[0]}, direct: false},
|
|
isWaiting(nil),
|
|
isScheduled{nil, nil, nil},
|
|
|
|
// Set up a few hashes into various stages
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[0]}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[1]}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[2]}},
|
|
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {testTxsHashes[2]},
|
|
}),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0], testTxsHashes[1]},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0]},
|
|
},
|
|
},
|
|
// Deliver everything and more out of the blue
|
|
doTxEnqueue{peer: "B", txs: []*types.Transaction{testTxs[0], testTxs[1], testTxs[2], testTxs[3]}, direct: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: nil,
|
|
fetching: nil,
|
|
dangling: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0]},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that dropping a peer cleans out all internal data structures in all the
|
|
// live or dangling stages.
|
|
func TestTransactionFetcherDrop(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
func(txs []*types.Transaction) []error {
|
|
return make([]error, len(txs))
|
|
},
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Set up a few hashes into various stages
|
|
doTxNotify{peer: "A", hashes: []common.Hash{{0x01}}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
doTxNotify{peer: "A", hashes: []common.Hash{{0x02}}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
doTxNotify{peer: "A", hashes: []common.Hash{{0x03}}},
|
|
|
|
isWaiting(map[string][]common.Hash{
|
|
"A": {{0x03}},
|
|
}),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {{0x01}, {0x02}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x01}},
|
|
},
|
|
},
|
|
// Drop the peer and ensure everything's cleaned out
|
|
doDrop("A"),
|
|
isWaiting(nil),
|
|
isScheduled{nil, nil, nil},
|
|
|
|
// Push the node into a dangling (timeout) state
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[0]}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0]},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0]},
|
|
},
|
|
},
|
|
doWait{time: txFetchTimeout, step: true},
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: nil,
|
|
fetching: nil,
|
|
dangling: map[string][]common.Hash{
|
|
"A": {},
|
|
},
|
|
},
|
|
// Drop the peer and ensure everything's cleaned out
|
|
doDrop("A"),
|
|
isWaiting(nil),
|
|
isScheduled{nil, nil, nil},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that dropping a peer instantly reschedules failed announcements to any
|
|
// available peer.
|
|
func TestTransactionFetcherDropRescheduling(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
func(txs []*types.Transaction) []error {
|
|
return make([]error, len(txs))
|
|
},
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Set up a few hashes into various stages
|
|
doTxNotify{peer: "A", hashes: []common.Hash{{0x01}}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
doTxNotify{peer: "B", hashes: []common.Hash{{0x01}}},
|
|
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"A": {{0x01}},
|
|
"B": {{0x01}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {{0x01}},
|
|
},
|
|
},
|
|
// Drop the peer and ensure everything's cleaned out
|
|
doDrop("A"),
|
|
isWaiting(nil),
|
|
isScheduled{
|
|
tracking: map[string][]common.Hash{
|
|
"B": {{0x01}},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"B": {{0x01}},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
// Tests that announced transactions with the wrong transaction type or size will
|
|
// result in a dropped peer.
|
|
func TestInvalidAnnounceMetadata(t *testing.T) {
|
|
drop := make(chan string, 2)
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
func(txs []*types.Transaction) []error {
|
|
return make([]error, len(txs))
|
|
},
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
func(peer string) { drop <- peer },
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Initial announcement to get something into the waitlist
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[0], testTxsHashes[1]}, types: []byte{testTxs[0].Type(), testTxs[1].Type()}, sizes: []uint32{uint32(testTxs[0].Size()), uint32(testTxs[1].Size())}},
|
|
isWaitingWithMeta(map[string][]announce{
|
|
"A": {
|
|
{testTxsHashes[0], typeptr(testTxs[0].Type()), sizeptr(uint32(testTxs[0].Size()))},
|
|
{testTxsHashes[1], typeptr(testTxs[1].Type()), sizeptr(uint32(testTxs[1].Size()))},
|
|
},
|
|
}),
|
|
// Announce from new peers conflicting transactions
|
|
doTxNotify{peer: "B", hashes: []common.Hash{testTxsHashes[0]}, types: []byte{testTxs[0].Type()}, sizes: []uint32{1024 + uint32(testTxs[0].Size())}},
|
|
doTxNotify{peer: "C", hashes: []common.Hash{testTxsHashes[1]}, types: []byte{1 + testTxs[1].Type()}, sizes: []uint32{uint32(testTxs[1].Size())}},
|
|
isWaitingWithMeta(map[string][]announce{
|
|
"A": {
|
|
{testTxsHashes[0], typeptr(testTxs[0].Type()), sizeptr(uint32(testTxs[0].Size()))},
|
|
{testTxsHashes[1], typeptr(testTxs[1].Type()), sizeptr(uint32(testTxs[1].Size()))},
|
|
},
|
|
"B": {
|
|
{testTxsHashes[0], typeptr(testTxs[0].Type()), sizeptr(1024 + uint32(testTxs[0].Size()))},
|
|
},
|
|
"C": {
|
|
{testTxsHashes[1], typeptr(1 + testTxs[1].Type()), sizeptr(uint32(testTxs[1].Size()))},
|
|
},
|
|
}),
|
|
// Schedule all the transactions for retrieval
|
|
doWait{time: txArriveTimeout, step: true},
|
|
isWaitingWithMeta(nil),
|
|
isScheduledWithMeta{
|
|
tracking: map[string][]announce{
|
|
"A": {
|
|
{testTxsHashes[0], typeptr(testTxs[0].Type()), sizeptr(uint32(testTxs[0].Size()))},
|
|
{testTxsHashes[1], typeptr(testTxs[1].Type()), sizeptr(uint32(testTxs[1].Size()))},
|
|
},
|
|
"B": {
|
|
{testTxsHashes[0], typeptr(testTxs[0].Type()), sizeptr(1024 + uint32(testTxs[0].Size()))},
|
|
},
|
|
"C": {
|
|
{testTxsHashes[1], typeptr(1 + testTxs[1].Type()), sizeptr(uint32(testTxs[1].Size()))},
|
|
},
|
|
},
|
|
fetching: map[string][]common.Hash{
|
|
"A": {testTxsHashes[0]},
|
|
"C": {testTxsHashes[1]},
|
|
},
|
|
},
|
|
// Deliver the transactions and wait for B to be dropped
|
|
doTxEnqueue{peer: "A", txs: []*types.Transaction{testTxs[0], testTxs[1]}},
|
|
doFunc(func() { <-drop }),
|
|
doFunc(func() { <-drop }),
|
|
},
|
|
})
|
|
}
|
|
|
|
// This test reproduces a crash caught by the fuzzer. The root cause was a
|
|
// dangling transaction timing out and clashing on re-add with a concurrently
|
|
// announced one.
|
|
func TestTransactionFetcherFuzzCrash01(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
func(txs []*types.Transaction) []error {
|
|
return make([]error, len(txs))
|
|
},
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Get a transaction into fetching mode and make it dangling with a broadcast
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[0]}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
doTxEnqueue{peer: "A", txs: []*types.Transaction{testTxs[0]}},
|
|
|
|
// Notify the dangling transaction once more and crash via a timeout
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[0]}},
|
|
doWait{time: txFetchTimeout, step: true},
|
|
},
|
|
})
|
|
}
|
|
|
|
// This test reproduces a crash caught by the fuzzer. The root cause was a
|
|
// dangling transaction getting peer-dropped and clashing on re-add with a
|
|
// concurrently announced one.
|
|
func TestTransactionFetcherFuzzCrash02(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
func(txs []*types.Transaction) []error {
|
|
return make([]error, len(txs))
|
|
},
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Get a transaction into fetching mode and make it dangling with a broadcast
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[0]}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
doTxEnqueue{peer: "A", txs: []*types.Transaction{testTxs[0]}},
|
|
|
|
// Notify the dangling transaction once more, re-fetch, and crash via a drop and timeout
|
|
doTxNotify{peer: "B", hashes: []common.Hash{testTxsHashes[0]}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
doDrop("A"),
|
|
doWait{time: txFetchTimeout, step: true},
|
|
},
|
|
})
|
|
}
|
|
|
|
// This test reproduces a crash caught by the fuzzer. The root cause was a
|
|
// dangling transaction getting rescheduled via a partial delivery, clashing
|
|
// with a concurrent notify.
|
|
func TestTransactionFetcherFuzzCrash03(t *testing.T) {
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
func(txs []*types.Transaction) []error {
|
|
return make([]error, len(txs))
|
|
},
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Get a transaction into fetching mode and make it dangling with a broadcast
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[0], testTxsHashes[1]}},
|
|
doWait{time: txFetchTimeout, step: true},
|
|
doTxEnqueue{peer: "A", txs: []*types.Transaction{testTxs[0], testTxs[1]}},
|
|
|
|
// Notify the dangling transaction once more, partially deliver, clash&crash with a timeout
|
|
doTxNotify{peer: "B", hashes: []common.Hash{testTxsHashes[0]}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
|
|
doTxEnqueue{peer: "A", txs: []*types.Transaction{testTxs[1]}, direct: true},
|
|
doWait{time: txFetchTimeout, step: true},
|
|
},
|
|
})
|
|
}
|
|
|
|
// This test reproduces a crash caught by the fuzzer. The root cause was a
|
|
// dangling transaction getting rescheduled via a disconnect, clashing with
|
|
// a concurrent notify.
|
|
func TestTransactionFetcherFuzzCrash04(t *testing.T) {
|
|
// Create a channel to control when tx requests can fail
|
|
proceed := make(chan struct{})
|
|
|
|
testTransactionFetcherParallel(t, txFetcherTest{
|
|
init: func() *TxFetcher {
|
|
return NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
func(txs []*types.Transaction) []error {
|
|
return make([]error, len(txs))
|
|
},
|
|
func(string, []common.Hash) error {
|
|
<-proceed
|
|
return errors.New("peer disconnected")
|
|
},
|
|
testTxArrivalWait,
|
|
nil,
|
|
)
|
|
},
|
|
steps: []interface{}{
|
|
// Get a transaction into fetching mode and make it dangling with a broadcast
|
|
doTxNotify{peer: "A", hashes: []common.Hash{testTxsHashes[0]}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
doTxEnqueue{peer: "A", txs: []*types.Transaction{testTxs[0]}},
|
|
|
|
// Notify the dangling transaction once more, re-fetch, and crash via an in-flight disconnect
|
|
doTxNotify{peer: "B", hashes: []common.Hash{testTxsHashes[0]}},
|
|
doWait{time: testTxArrivalWait, step: true},
|
|
doFunc(func() {
|
|
proceed <- struct{}{} // Allow peer A to return the failure
|
|
}),
|
|
doWait{time: 0, step: true},
|
|
doWait{time: txFetchTimeout, step: true},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testTransactionFetcherParallel(t *testing.T, tt txFetcherTest) {
|
|
t.Parallel()
|
|
testTransactionFetcher(t, tt)
|
|
}
|
|
|
|
func testTransactionFetcher(t *testing.T, tt txFetcherTest) {
|
|
// Create a fetcher and hook into it's simulated fields
|
|
clock := new(mclock.Simulated)
|
|
wait := make(chan struct{})
|
|
|
|
fetcher := tt.init()
|
|
fetcher.clock = clock
|
|
fetcher.step = wait
|
|
fetcher.rand = rand.New(rand.NewSource(0x3a29))
|
|
|
|
fetcher.Start()
|
|
defer fetcher.Stop()
|
|
|
|
defer func() { // drain the wait chan on exit
|
|
for {
|
|
select {
|
|
case <-wait:
|
|
default:
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
// Crunch through all the test steps and execute them
|
|
for i, step := range tt.steps {
|
|
// Auto-expand certain steps to ones with metadata
|
|
switch old := step.(type) {
|
|
case isWaiting:
|
|
new := make(isWaitingWithMeta)
|
|
for peer, hashes := range old {
|
|
for _, hash := range hashes {
|
|
new[peer] = append(new[peer], announce{hash, nil, nil})
|
|
}
|
|
}
|
|
step = new
|
|
|
|
case isScheduled:
|
|
new := isScheduledWithMeta{
|
|
tracking: make(map[string][]announce),
|
|
fetching: old.fetching,
|
|
dangling: old.dangling,
|
|
}
|
|
for peer, hashes := range old.tracking {
|
|
for _, hash := range hashes {
|
|
new.tracking[peer] = append(new.tracking[peer], announce{hash, nil, nil})
|
|
}
|
|
}
|
|
step = new
|
|
}
|
|
// Process the original or expanded steps
|
|
switch step := step.(type) {
|
|
case doTxNotify:
|
|
if err := fetcher.Notify(step.peer, step.types, step.sizes, step.hashes); err != nil {
|
|
t.Errorf("step %d: %v", i, err)
|
|
}
|
|
|
|
<-wait // Fetcher needs to process this, wait until it's done
|
|
select {
|
|
case <-wait:
|
|
panic("wtf")
|
|
case <-time.After(time.Millisecond):
|
|
}
|
|
|
|
case doTxEnqueue:
|
|
if err := fetcher.Enqueue(step.peer, step.txs, step.direct); err != nil {
|
|
t.Errorf("step %d: %v", i, err)
|
|
}
|
|
|
|
<-wait // Fetcher needs to process this, wait until it's done
|
|
|
|
case doWait:
|
|
clock.Run(step.time)
|
|
|
|
if step.step {
|
|
<-wait // Fetcher supposed to do something, wait until it's done
|
|
}
|
|
|
|
case doDrop:
|
|
if err := fetcher.Drop(string(step)); err != nil {
|
|
t.Errorf("step %d: %v", i, err)
|
|
}
|
|
|
|
<-wait // Fetcher needs to process this, wait until it's done
|
|
|
|
case doFunc:
|
|
step()
|
|
|
|
case isWaitingWithMeta:
|
|
// We need to check that the waiting list (stage 1) internals
|
|
// match with the expected set. Check the peer->hash mappings
|
|
// first.
|
|
for peer, announces := range step {
|
|
waiting := fetcher.waitslots[peer]
|
|
if waiting == nil {
|
|
t.Errorf("step %d: peer %s missing from waitslots", i, peer)
|
|
continue
|
|
}
|
|
for _, ann := range announces {
|
|
if meta, ok := waiting[ann.hash]; !ok {
|
|
t.Errorf("step %d, peer %s: hash %x missing from waitslots", i, peer, ann.hash)
|
|
} else {
|
|
if (meta == nil && (ann.kind != nil || ann.size != nil)) ||
|
|
(meta != nil && (ann.kind == nil || ann.size == nil)) ||
|
|
(meta != nil && (meta.kind != *ann.kind || meta.size != *ann.size)) {
|
|
t.Errorf("step %d, peer %s, hash %x: waitslot metadata mismatch: want %v, have %v/%v", i, peer, ann.hash, meta, *ann.kind, *ann.size)
|
|
}
|
|
}
|
|
}
|
|
for hash, meta := range waiting {
|
|
ann := announce{hash: hash}
|
|
if meta != nil {
|
|
ann.kind, ann.size = &meta.kind, &meta.size
|
|
}
|
|
if !containsAnnounce(announces, ann) {
|
|
t.Errorf("step %d, peer %s: announce %v extra in waitslots", i, peer, ann)
|
|
}
|
|
}
|
|
}
|
|
|
|
for peer := range fetcher.waitslots {
|
|
if _, ok := step[peer]; !ok {
|
|
t.Errorf("step %d: peer %s extra in waitslots", i, peer)
|
|
}
|
|
}
|
|
// Peer->hash sets correct, check the hash->peer and timeout sets
|
|
for peer, announces := range step {
|
|
for _, ann := range announces {
|
|
if _, ok := fetcher.waitlist[ann.hash][peer]; !ok {
|
|
t.Errorf("step %d, hash %x: peer %s missing from waitlist", i, ann.hash, peer)
|
|
}
|
|
if _, ok := fetcher.waittime[ann.hash]; !ok {
|
|
t.Errorf("step %d: hash %x missing from waittime", i, ann.hash)
|
|
}
|
|
}
|
|
}
|
|
|
|
for hash, peers := range fetcher.waitlist {
|
|
if len(peers) == 0 {
|
|
t.Errorf("step %d, hash %x: empty peerset in waitlist", i, hash)
|
|
}
|
|
|
|
for peer := range peers {
|
|
if !containsHashInAnnounces(step[peer], hash) {
|
|
t.Errorf("step %d, hash %x: peer %s extra in waitlist", i, hash, peer)
|
|
}
|
|
}
|
|
}
|
|
|
|
for hash := range fetcher.waittime {
|
|
var found bool
|
|
for _, announces := range step {
|
|
if containsHashInAnnounces(announces, hash) {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
t.Errorf("step %d,: hash %x extra in waittime", i, hash)
|
|
}
|
|
}
|
|
|
|
case isScheduledWithMeta:
|
|
// Check that all scheduled announces are accounted for and no
|
|
// extra ones are present.
|
|
for peer, announces := range step.tracking {
|
|
scheduled := fetcher.announces[peer]
|
|
if scheduled == nil {
|
|
t.Errorf("step %d: peer %s missing from announces", i, peer)
|
|
continue
|
|
}
|
|
for _, ann := range announces {
|
|
if meta, ok := scheduled[ann.hash]; !ok {
|
|
t.Errorf("step %d, peer %s: hash %x missing from announces", i, peer, ann.hash)
|
|
} else {
|
|
if (meta == nil && (ann.kind != nil || ann.size != nil)) ||
|
|
(meta != nil && (ann.kind == nil || ann.size == nil)) ||
|
|
(meta != nil && (meta.kind != *ann.kind || meta.size != *ann.size)) {
|
|
t.Errorf("step %d, peer %s, hash %x: announce metadata mismatch: want %v, have %v/%v", i, peer, ann.hash, meta, *ann.kind, *ann.size)
|
|
}
|
|
}
|
|
}
|
|
for hash, meta := range scheduled {
|
|
ann := announce{hash: hash}
|
|
if meta != nil {
|
|
ann.kind, ann.size = &meta.kind, &meta.size
|
|
}
|
|
if !containsAnnounce(announces, ann) {
|
|
t.Errorf("step %d, peer %s: announce %x extra in announces", i, peer, hash)
|
|
}
|
|
}
|
|
}
|
|
|
|
for peer := range fetcher.announces {
|
|
if _, ok := step.tracking[peer]; !ok {
|
|
t.Errorf("step %d: peer %s extra in announces", i, peer)
|
|
}
|
|
}
|
|
// Check that all announces required to be fetching are in the
|
|
// appropriate sets
|
|
for peer, hashes := range step.fetching {
|
|
request := fetcher.requests[peer]
|
|
if request == nil {
|
|
t.Errorf("step %d: peer %s missing from requests", i, peer)
|
|
continue
|
|
}
|
|
|
|
for _, hash := range hashes {
|
|
if !containsHash(request.hashes, hash) {
|
|
t.Errorf("step %d, peer %s: hash %x missing from requests", i, peer, hash)
|
|
}
|
|
}
|
|
|
|
for _, hash := range request.hashes {
|
|
if !containsHash(hashes, hash) {
|
|
t.Errorf("step %d, peer %s: hash %x extra in requests", i, peer, hash)
|
|
}
|
|
}
|
|
}
|
|
|
|
for peer := range fetcher.requests {
|
|
if _, ok := step.fetching[peer]; !ok {
|
|
if _, ok := step.dangling[peer]; !ok {
|
|
t.Errorf("step %d: peer %s extra in requests", i, peer)
|
|
}
|
|
}
|
|
}
|
|
|
|
for peer, hashes := range step.fetching {
|
|
for _, hash := range hashes {
|
|
if _, ok := fetcher.fetching[hash]; !ok {
|
|
t.Errorf("step %d, peer %s: hash %x missing from fetching", i, peer, hash)
|
|
}
|
|
}
|
|
}
|
|
|
|
for hash := range fetcher.fetching {
|
|
var found bool
|
|
|
|
for _, req := range fetcher.requests {
|
|
if containsHash(req.hashes, hash) {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
t.Errorf("step %d: hash %x extra in fetching", i, hash)
|
|
}
|
|
}
|
|
|
|
for _, hashes := range step.fetching {
|
|
for _, hash := range hashes {
|
|
alternates := fetcher.alternates[hash]
|
|
if alternates == nil {
|
|
t.Errorf("step %d: hash %x missing from alternates", i, hash)
|
|
continue
|
|
}
|
|
|
|
for peer := range alternates {
|
|
if _, ok := fetcher.announces[peer]; !ok {
|
|
t.Errorf("step %d: peer %s extra in alternates", i, peer)
|
|
continue
|
|
}
|
|
|
|
if _, ok := fetcher.announces[peer][hash]; !ok {
|
|
t.Errorf("step %d, peer %s: hash %x extra in alternates", i, hash, peer)
|
|
continue
|
|
}
|
|
}
|
|
|
|
for p := range fetcher.announced[hash] {
|
|
if _, ok := alternates[p]; !ok {
|
|
t.Errorf("step %d, hash %x: peer %s missing from alternates", i, hash, p)
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for peer, hashes := range step.dangling {
|
|
request := fetcher.requests[peer]
|
|
if request == nil {
|
|
t.Errorf("step %d: peer %s missing from requests", i, peer)
|
|
continue
|
|
}
|
|
|
|
for _, hash := range hashes {
|
|
if !containsHash(request.hashes, hash) {
|
|
t.Errorf("step %d, peer %s: hash %x missing from requests", i, peer, hash)
|
|
}
|
|
}
|
|
|
|
for _, hash := range request.hashes {
|
|
if !containsHash(hashes, hash) {
|
|
t.Errorf("step %d, peer %s: hash %x extra in requests", i, peer, hash)
|
|
}
|
|
}
|
|
}
|
|
// Check that all transaction announces that are scheduled for
|
|
// retrieval but not actively being downloaded are tracked only
|
|
// in the stage 2 `announced` map.
|
|
var queued []common.Hash
|
|
for _, announces := range step.tracking {
|
|
for _, ann := range announces {
|
|
var found bool
|
|
|
|
for _, hs := range step.fetching {
|
|
if containsHash(hs, ann.hash) {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
queued = append(queued, ann.hash)
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, hash := range queued {
|
|
if _, ok := fetcher.announced[hash]; !ok {
|
|
t.Errorf("step %d: hash %x missing from announced", i, hash)
|
|
}
|
|
}
|
|
|
|
for hash := range fetcher.announced {
|
|
if !containsHash(queued, hash) {
|
|
t.Errorf("step %d: hash %x extra in announced", i, hash)
|
|
}
|
|
}
|
|
|
|
case isUnderpriced:
|
|
if fetcher.underpriced.Len() != int(step) {
|
|
t.Errorf("step %d: underpriced set size mismatch: have %d, want %d", i, fetcher.underpriced.Len(), step)
|
|
}
|
|
|
|
default:
|
|
t.Fatalf("step %d: unknown step type %T", i, step)
|
|
}
|
|
// After every step, cross validate the internal uniqueness invariants
|
|
// between stage one and stage two.
|
|
for hash := range fetcher.waittime {
|
|
if _, ok := fetcher.announced[hash]; ok {
|
|
t.Errorf("step %d: hash %s present in both stage 1 and 2", i, hash)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// containsAnnounce returns whether an announcement is contained within a slice
|
|
// of announcements.
|
|
func containsAnnounce(slice []announce, ann announce) bool {
|
|
for _, have := range slice {
|
|
if have.hash == ann.hash {
|
|
if have.kind == nil || ann.kind == nil {
|
|
if have.kind != ann.kind {
|
|
return false
|
|
}
|
|
} else if *have.kind != *ann.kind {
|
|
return false
|
|
}
|
|
if have.size == nil || ann.size == nil {
|
|
if have.size != ann.size {
|
|
return false
|
|
}
|
|
} else if *have.size != *ann.size {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// containsHashInAnnounces returns whether a hash is contained within a slice
|
|
// of announcements.
|
|
func containsHashInAnnounces(slice []announce, hash common.Hash) bool {
|
|
for _, have := range slice {
|
|
if have.hash == hash {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// containsHash returns whether a hash is contained within a hash slice.
|
|
func containsHash(slice []common.Hash, hash common.Hash) bool {
|
|
for _, have := range slice {
|
|
if have == hash {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// Tests that a transaction is forgotten after the timeout.
|
|
func TestTransactionForgotten(t *testing.T) {
|
|
fetcher := NewTxFetcher(
|
|
func(common.Hash) bool { return false },
|
|
func(txs []*types.Transaction) []error {
|
|
errs := make([]error, len(txs))
|
|
for i := 0; i < len(errs); i++ {
|
|
errs[i] = txpool.ErrUnderpriced
|
|
}
|
|
return errs
|
|
},
|
|
func(string, []common.Hash) error { return nil },
|
|
testTxArrivalWait,
|
|
func(string) {},
|
|
)
|
|
fetcher.Start()
|
|
defer fetcher.Stop()
|
|
// Create one TX which is 5 minutes old, and one which is recent
|
|
tx1 := types.NewTx(&types.LegacyTx{Nonce: 0})
|
|
tx1.SetTime(time.Now().Add(-maxTxUnderpricedTimeout - 1*time.Second))
|
|
tx2 := types.NewTx(&types.LegacyTx{Nonce: 1})
|
|
|
|
// Enqueue both in the fetcher. They will be immediately tagged as underpriced
|
|
if err := fetcher.Enqueue("asdf", []*types.Transaction{tx1, tx2}, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// isKnownUnderpriced should trigger removal of the first tx (no longer be known underpriced)
|
|
if fetcher.isKnownUnderpriced(tx1.Hash()) {
|
|
t.Fatal("transaction should be forgotten by now")
|
|
}
|
|
// isKnownUnderpriced should not trigger removal of the second
|
|
if !fetcher.isKnownUnderpriced(tx2.Hash()) {
|
|
t.Fatal("transaction should be known underpriced")
|
|
}
|
|
}
|