mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 23:26:44 +00:00
* cmd, consensus: add option to disable mmap for DAG caches/datasets (#20484) * cmd, consensus: add option to disable mmap for DAG caches/datasets * consensus: add benchmarks for mmap with/with lock * cmd/clef: add newaccount command (#20782) * cmd/clef: add newaccount command * cmd/clef: document clef_New, update API versioning * Update cmd/clef/intapi_changelog.md Co-Authored-By: ligi <ligi@ligi.de> * Update signer/core/uiapi.go Co-Authored-By: ligi <ligi@ligi.de> Co-authored-by: ligi <ligi@ligi.de> * eth: add debug_accountRange API (#19645) This new API allows reading accounts and their content by address range. Co-authored-by: Martin Holst Swende <martin@swende.se> Co-authored-by: Felix Lange <fjl@twurst.com> * travis: allow cocoapods deploy to fail (#20833) * whisper/whisperv6: decrease pow requirement in tests (#20815) * metrics: improve TestTimerFunc (#20818) The test failed due to what appears to be fluctuations in time.Sleep, which is not the actual method under test. This change modifies it so we compare the metered Max to the actual time instead of the desired time. * les: create utilities as common package (#20509) * les: move execqueue into utilities package execqueue is a util for executing queued functions in a serial order which is used by both les server and les client. Move it to common package. * les: move randselect to utilities package weighted_random_selector is a helpful tool for randomly select items maintained in a set but based on the item weight. It's used anywhere is LES package, mainly by les client but will be used in les server with very high chance. So move it into a common package as the second step for les separation. * les: rename to utils * README: update private network genesis spec with istanbul (#20841) * add istanbul and muirGlacier to genesis states in README * remove muirGlacier, relocate istanbul * whisper: fix whisper go routine leak with sync wait group (#20844) * cmd/evm: Rework execution stats (#20792) - Dump stats also for --bench flag. - From memory stats only show number and size of allocations. This is what `test -bench` shows. I doubt others like number of GC runs are any useful, but can be added if requested. - Now the mem stats are for single execution in case of --bench. * cmd/devp2p, cmd/wnode, whisper: add missing calls to Timer.Stop (#20843) * p2p/server: add UDP port mapping goroutine to wait group (#20846) * accounts/abi faster unpacking of int256 (#20850) * p2p/discv5: add missing Timer.Stop calls (#20853) * miner/worker: add missing timer.Stop call (#20857) * cmd/geth: fix bad genesis test (#20860) * eth/filters: add missing Ticker.Stop call (#20862) * eth/fetcher: add missing timer.Stop calls (#20861) * les: add missing Ticker.Stop call (#20864) * event: add missing timer.Stop call in TestFeed (#20868) * metrics: add missing calls to Ticker.Stop in tests (#20866) * ethstats: add missing Ticker.Stop call (#20867) * p2p/discv5, p2p/testing: add missing Timer.Stop calls in tests (#20869) * core: add missing Timer.Stop call in TestLogReorgs (#20870) * rpc: add missing timer.Stop calls in websocket tests (#20863) * crypto/ecies: improve concatKDF (#20836) This removes a bunch of weird code around the counter overflow check in concatKDF and makes it actually work for different hash output sizes. The overflow check worked as follows: concatKDF applies the hash function N times, where N is roundup(kdLen, hashsize) / hashsize. N should not overflow 32 bits because that would lead to a repetition in the KDF output. A couple issues with the overflow check: - It used the hash.BlockSize, which is wrong because the block size is about the input of the hash function. Luckily, all standard hash functions have a block size that's greater than the output size, so concatKDF didn't crash, it just generated too much key material. - The check used big.Int to compare against 2^32-1. - The calculation could still overflow before reaching the check. The new code in concatKDF doesn't check for overflow. Instead, there is a new check on ECIESParams which ensures that params.KeyLen is < 512. This removes any possibility of overflow. There are a couple of miscellaneous improvements bundled in with this change: - The key buffer is pre-allocated instead of appending the hash output to an initially empty slice. - The code that uses concatKDF to derive keys is now shared between Encrypt and Decrypt. - There was a redundant invocation of IsOnCurve in Decrypt. This is now removed because elliptic.Unmarshal already checks whether the input is a valid curve point since Go 1.5. Co-authored-by: Felix Lange <fjl@twurst.com> * rpc: metrics for JSON-RPC method calls (#20847) This adds a couple of metrics for tracking the timing and frequency of method calls: - rpc/requests gauge counts all requests - rpc/success gauge counts requests which return err == nil - rpc/failure gauge counts requests which return err != nil - rpc/duration/all timer tracks timing of all requests - rpc/duration/<method>/<success/failure> tracks per-method timing * mobile: use bind.NewKeyedTransactor instead of duplicating (#20888) It's better to reuse the existing code to create a keyed transactor than to rewrite the logic again. * all: fix a bunch of inconsequential goroutine leaks (#20667) The leaks were mostly in unit tests, and could all be resolved by adding suitably-sized channel buffers or by restructuring the test to not send on a channel after an error has occurred. There is an unavoidable goroutine leak in Console.Interactive: when we receive a signal, the line reader cannot be unblocked and will get stuck. This leak is now documented and I've tried to make it slightly less bad by adding a one-element buffer to the output channels of the line-reading loop. Should the reader eventually awake from its blocked state (i.e. when stdin is closed), at least it won't get stuck trying to send to the interpreter loop which has quit long ago. Co-authored-by: Felix Lange <fjl@twurst.com> * internal/ethapi: add CallArgs.ToMessage method (#20854) ToMessage is used to convert between ethapi.CallArgs and types.Message. It reduces the length of the DoCall method by about half by abstracting out the conversion between the CallArgs and the Message. This should improve the code's maintainability and reusability. * eth, les: fix flaky tests (#20897) * les: fix flaky test * eth: fix flaky test * cmd/geth: enable metrics for geth import command (#20738) * cmd/geth: enable metrics for geth import command * cmd/geth: enable metrics-flags for import command * core/vm: use a callcontext struct (#20761) * core/vm: use a callcontext struct * core/vm: fix tests * core/vm/runtime: benchmark * core/vm: make intpool push inlineable, unexpose callcontext * docs/audits: add discv5 protocol audits from LA and C53 (#20898) * .github: change gitter reference to discord link in issue template (#20896) * couple of fixes to docs in clef (#20900) * p2p/discover: add initial discovery v5 implementation (#20750) This adds an implementation of the current discovery v5 spec. There is full integration with cmd/devp2p and enode.Iterator in this version. In theory we could enable the new protocol as a replacement of discovery v4 at any time. In practice, there will likely be a few more changes to the spec and implementation before this can happen. * build: upgrade to golangci-lint 1.24.0 (#20901) * accounts/scwallet: remove unnecessary uses of fmt.Sprintf * cmd/puppeth: remove unnecessary uses of fmt.Sprintf * p2p/discv5: remove unnecessary use of fmt.Sprintf * whisper/mailserver: remove unnecessary uses of fmt.Sprintf * core: goimports -w tx_pool_test.go * eth/downloader: goimports -w downloader_test.go * build: upgrade to golangci-lint 1.24.0 * whisper/mailserver : recover corrupt db files before opening (#20891) * whisper/mailserver : recover db file when openfile corrupted * whisper/mailserver : fix db -> s.db * whisper/mailserver : common/errors for dbfile * accounts/abi/bind: Refactored topics (#20851) * accounts/abi/bind: refactored topics * accounts/abi/bind: use store function to remove code duplication * accounts/abi/bind: removed unused type defs * accounts/abi/bind: error on tuples in topics * Cosmetic changes to restart travis build Co-authored-by: Guillaume Ballet <gballet@gmail.com> * node: allow websocket and HTTP on the same port (#20810) This change makes it possible to run geth with JSON-RPC over HTTP and WebSocket on the same TCP port. The default port for WebSocket is still 8546. geth --rpc --rpcport 8545 --ws --wsport 8545 This also removes a lot of deprecated API surface from package rpc. The rpc package is now purely about serving JSON-RPC and no longer provides a way to start an HTTP server. * crypto: improve error messages in LoadECDSA (#20718) This improves error messages when the file is too short or too long. Also rewrite the test for SaveECDSA because LoadECDSA has its own test now. Co-authored-by: Felix Lange <fjl@twurst.com> * changed date of rpcstack.go since new file (#20904) * accounts/abi/bind: fixed erroneous filtering of negative ints (#20865) * accounts/abi/bind: fixed erroneous packing of negative ints * accounts/abi/bind: added test cases for negative ints in topics * accounts/abi/bind: fixed genIntType for go 1.12 * accounts/abi: minor nitpick * cmd: deprecate --testnet, use named networks instead (#20852) * cmd/utils: make goerli the default testnet * cmd/geth: explicitly rename testnet to ropsten * core: explicitly rename testnet to ropsten * params: explicitly rename testnet to ropsten * cmd: explicitly rename testnet to ropsten * miner: explicitly rename testnet to ropsten * mobile: allow for returning the goerli spec * tests: explicitly rename testnet to ropsten * docs: update readme to reflect changes to the default testnet * mobile: allow for configuring goerli and rinkeby nodes * cmd/geth: revert --testnet back to ropsten and mark as legacy * cmd/util: mark --testnet flag as deprecated * docs: update readme to properly reflect the 3 testnets * cmd/utils: add an explicit deprecation warning on startup * cmd/utils: swap goerli and ropsten in usage * cmd/geth: swap goerli and ropsten in usage * cmd/geth: if running a known preset, log it for convenience * docs: improve readme on usage of ropsten's testnet datadir * cmd/utils: check if legacy `testnet` datadir exists for ropsten * cmd/geth: check for legacy testnet path in console command * cmd/geth: use switch statement for complex conditions in main * cmd/geth: move known preset log statement to the very top * cmd/utils: create new ropsten configurations in the ropsten datadir * cmd/utils: makedatadir should check for existing testnet dir * cmd/geth: add legacy testnet flag to the copy db command * cmd/geth: add legacy testnet flag to the inspect command * les, les/lespay/client: add service value statistics and API (#20837) This PR adds service value measurement statistics to the light client. It also adds a private API that makes these statistics accessible. A follow-up PR will add the new server pool which uses these statistics to select servers with good performance. This document describes the function of the new components: https://gist.github.com/zsfelfoldi/3c7ace895234b7b345ab4f71dab102d4 Co-authored-by: rjl493456442 <garyrong0905@gmail.com> Co-authored-by: rjl493456442 <garyrong0905@gmail.com> * README: update min go version to 1.13 (#20911) * travis, appveyor, build, Dockerfile: bump Go to 1.14.2 (#20913) * travis, appveyor, build, Dockerfile: bump Go to 1.14.2 * travis, appveyor: force GO111MODULE=on for every build * core/rawdb: fix data race between Retrieve and Close (#20919) * core/rawdb: fixed data race between retrieve and close closes https://github.com/ethereum/go-ethereum/issues/20420 * core/rawdb: use non-atomic load while holding mutex * accounts/abi: implement new fallback functions (#20764) * accounts/abi: implement new fackball functions In Solidity v0.6.0, the original fallback is separated into two different sub types: fallback and receive. This PR addes the support for parsing new format abi and the relevant abigen functionalities. * accounts/abi: fix unit tests * accounts/abi: minor fixes * accounts/abi, mobile: support jave binding * accounts/abi: address marius's comment * accounts/abi: Work around the uin64 conversion issue Co-authored-by: Guillaume Ballet <gballet@gmail.com> * trie: fix concurrent usage of secKeyBuf, ref #20920 * all: simplify and fix database iteration with prefix/start (#20808) * core/state/snapshot: start fixing disk iterator seek * ethdb, rawdb, leveldb, memorydb: implement iterators with prefix and start * les, core/state/snapshot: iterator fixes * all: remove two iterator methods * all: rename Iteratee.NewIteratorWith -> NewIterator * ethdb: fix review concerns * params: update CHTs for the 1.9.13 release * params: release Geth v1.9.13 * params: begin v1.9.14 release cycle * p2p/discover: add helper methods to UDPv5 (#20918) This adds two new methods to UDPv5, AllNodes and LocalNode. AllNodes returns all the nodes stored in the local table; this is useful for the purposes of metrics collection and also debugging any potential issues with other discovery v5 implementations. LocalNode returns the local node object. The reason for exposing this is so that users can modify and set/delete new key-value entries in the local record. * event: fix inconsistency in Lock and Unlock (#20933) Co-authored-by: Felix Lange <fjl@twurst.com> * accounts/abi: Prevent recalculation of internal fields (#20895) * accounts/abi: prevent recalculation of ID, Sig and String * accounts/abi: fixed unpacking of no values * accounts/abi: multiple fixes to arguments * accounts/abi: refactored methodName and eventName This commit moves the complicated logic of how we assign method names and event names if they already exist into their own functions for better readability. * accounts/abi: prevent recalculation of internal In this commit, I changed the way we calculate the string representations, sig representations and the id's of methods. Before that these fields would be recalculated everytime someone called .Sig() .String() or .ID() on a method or an event. Additionally this commit fixes issue #20856 as we assign names to inputs with no name (input with name "" becomes "arg0") * accounts/abi: added unnamed event params test * accounts/abi: fixed rebasing errors in method sig * accounts/abi: fixed rebasing errors in method sig * accounts/abi: addressed comments * accounts/abi: added FunctionType enumeration * accounts/abi/bind: added test for unnamed arguments * accounts/abi: improved readability in NewMethod, nitpicks * accounts/abi: method/eventName -> overloadedMethodName * cmd, core: remove override muir glacier and override istanbul (#20942) * snapshot: add Unlock before return (#20948) * Forget Unlock in snapshot * Remove Unlock before panic * core/state/snapshot: make difflayer account iterator seek operation inclusive * p2p: defer wait group done in protocol start (#20951) * go.mod : update fastcache to 1.5.7 (#20936) * all: seperate consensus error and evm internal error (#20830) * all: seperate consensus error and evm internal error There are actually two types of error will be returned when a tranaction/message call is executed: (a) consensus error (b) evm internal error. The former should be converted to a consensus issue, e.g. The sender doesn't enough asset to purchase the gas it specifies. The latter is allowed since evm itself is a blackbox and internal error is allowed to happen. This PR emphasizes the difference by introducing a executionResult structure. The evm error is embedded inside. So if any error returned, it indicates consensus issue happens. And also this PR improve the `EstimateGas` API to return the concrete revert reason if the transaction always fails * all: polish * accounts/abi/bind/backends: add tests * accounts/abi/bind/backends, internal: cleanup error message * all: address comments * core: fix lint * accounts, core, eth, internal: address comments * accounts, internal: resolve revert reason if possible * accounts, internal: address comments * core: mirror full node reorg logic in light client too (#20931) * core: fix the condition of reorg * core: fix nitpick to only retrieve head once * core: don't reorg if received chain is longer at same diff Co-authored-by: Péter Szilágyi <peterke@gmail.com> * accounts/keystore: fix double import race (#20915) * accounts/keystore: fix race in Import/ImportECDSA * accounts/keystore: added import/export tests * cmd/geth: improved TestAccountImport test * accounts/keystore: added import/export tests * accounts/keystore: fixed naming * accounts/keystore: fixed typo * accounts/keystore: use mutex instead of rwmutex * accounts: use errors instead of fmt * trie: initial implementation for range proof (#20908) * trie: initial implementation for range proof * trie: add benchmark * trie: fix lint * trie: fix minor issue * trie: unset the edge valuenode as well * trie: unset the edge valuenode as nilValuenode * core/state/snapshot: fix binary iterator (#20970) * p2p/enode: update code comment (#20972) It is possible to specify enode URLs using domain name since commitb90cdbaa79, but the code comment still said that only IP addresses are allowed. Co-authored-by: admin@komgo.io <KomgoRocks2018!> * eth: fix shutdown regression to abort downloads, not just cancel * node: shut down all node-related HTTP servers gracefully (#20956) Rather than just closing the underlying network listener to stop our HTTP servers, use the graceful shutdown procedure, waiting for any in-process requests to finish. * accounts/abi: added abi test cases, minor bug fixes (#20903) * accounts/abi: added documentation * accounts/abi: reduced usage of arguments.LengthNonIndexed * accounts/abi: simplified reflection logic * accounts/abi: moved testjson data into global declaration * accounts/abi: removed duplicate test cases * accounts/abi: reworked abi tests * accounts/abi: added more tests for abi packing * accounts/abi/bind: refactored base tests * accounts/abi: run pack tests as subtests * accounts/abi: removed duplicate tests * accounts/abi: removed unnused arguments.LengthNonIndexed Due to refactors to the code, we do not need the arguments.LengthNonIndexed function anymore. You can still get the length by calling len(arguments.NonIndexed()) * accounts/abi: added type test * accounts/abi: modified unpack test to pack test * accounts/abi: length check on arrayTy * accounts/abi: test invalid abi * accounts/abi: fixed rebase error * accounts/abi: fixed rebase errors * accounts/abi: removed unused definition * accounts/abi: merged packing/unpacking tests * accounts/abi: fixed [][][32]bytes encoding * accounts/abi: added tuple test cases * accounts/abi: renamed getMockLog -> newMockLog * accounts/abi: removed duplicate test * accounts/abi: bools -> booleans * core: add check in AddChildIndexer to avoid double lock (#20982) This fixes a theoretical double lock condition which could occur in indexer.AddChildIndexer(indexer) Nobody would ever do that though. Co-authored-by: Felix Lange <fjl@twurst.com> * rpc: make ExampleClientSubscription work with the geth API (#19483) This corrects the call to eth_getBlockByNumber, which previously returned this error: can't get latest block: missing value for required argument 1 Co-authored-by: Felix Lange <fjl@twurst.com> * core: improve TestLogRebirth (#20961) This is a resubmit of #20668 which rewrites the problematic test without any additional goroutines. It also documents the test better. The purpose of this test is checking whether log events are sent correctly when importing blocks. The test was written at a time when blockchain events were delivered asynchronously, making the check hard to pull off. Now that core.BlockChain delivers events synchronously during the call to InsertChain, the test can be simplified. Co-authored-by: BurtonQin <bobbqqin@gmail.com> * go.mod: upgrade to golang-lru v0.5.4 (#20992) golang-lru is now a go module, and the upgrade corrects a couple of minor issues. In particular, the library could crash if you inserted nil into an LRU cache. * rpc: add explicit 200 response for empty HTTP GET (#20952) * event, whisper/whisperv6: use defer where possible (#20940) * signer, log: properly escape character sequences (#20987) * signer: properly handle terminal escape characters * log: use strconv conversion instead of custom escape function * log: remove relection tests for nil * go.mod : goupnp v1.0.0 upgrade (#20996) * core/state/snapshot: implement storage iterator (#20971) * core/state/snapshot: implement storage iterator * core/state/snapshot, tests: implement helper function * core/state/snapshot: fix storage issue If an account is deleted in the tx_1 but recreated in the tx_2, the it can happen that in this diff layer, both destructedSet and storageData records this account. In this case, the storage iterator should be able to iterate the slots belong to new account but disable further iteration in deeper layers(belong to old account) * core/state/snapshot: address peter and martin's comment * core/state: address comments * core/state/snapshot: fix test * core/state/snapshot: fix journal nil deserialziation * core/state/snapshot: fix trie generator reporter (#21004) * accounts/external: fill account-cache if that hasn't already been done, fixes #20995 (#20998) * go.sum: run go mod tidy (#21014) * les: remove invalid use of t.Fatal in TestHandshake (#21012) * accounts/external: convert signature v value to 0/1 (#20997) This fixes an issue with clef, which already transforms the signature to use the legacy 27/28 encoding. Fixes #20994 * tests: cleanup snapshot generator goroutine leak * accounts/abi: removed Kind from Type struct (#21009) * accounts/abi: removed Kind from Type struct * accounts/abi: removed unused code * accounts/abi: move U256Bytes to common/math (#21020) * core/state/snapshot: release iterator after verification * cmd/utils: renames flags related to http-rpc server (#20935) * rpc flags related to starting http server renamed to http * old rpc flags aliased and still functional * pprof flags fixed * renames gpo related flags * linted * renamed rpc flags for consistency and clarity * added warn logs * added more warn logs for all deprecated flags for consistency * moves legacy flags to separate file, hides older flags under show-deprecated-flags command * legacy prefix and moved some more legacy flags to legacy file * fixed circular import * added docs * fixed imports lint error * added notes about when flags were deprecated * cmd/utils: group flags by deprecation date + reorder by date, * modified deprecated comments for consistency, added warn log for --rpc * making sure deprecated flags are still functional * show-deprecated-flags command cleaned up * fixed lint errors * corrected merge conflict * IsSet --> GlobalIsSet * uncategorized flags, if not deprecated, displayed under misc Co-authored-by: Martin Holst Swende <martin@swende.se> * tests: skip consensus test using 1GB RAM * cmd/geth: handle memfixes on 32bit arch with large RAM * build: raise test timeout back to 10 mins (#21027) * account/abi: remove superfluous type checking (#21022) * accounts/abi: added getType func to Type struct * accounts/abi: fixed tuple unpack * accounts/abi: removed type.Type * accounts/abi: added comment * accounts/abi: removed unused types * accounts/abi: removed superfluous declarations * accounts/abi: typo * eth: skip transaction announcer goroutine on eth<65 * eth: don't inadvertently enable snapshots in archive nodes (#21025) * eth: don't reassign more cache than is being made available * eth: don't inadvertently enable snapshot in a case where --snapshot wasn't given * trie: fix TestBadRangeProof unit test (#21034) * eth/downloader: minor typo fixes in comments (#21035) * core: avoid double-lock in tx_pool_test (#20984) * core/state/snapshot: don't create storage list for non-existing accounts * core/state/snapshot: fix typo (#21037) * accounts/abi/bind: add void if no return args specified (#21002) * accounts/abi/bind: add void if no return args specified Currently the java generator generates invalid input on pure/view functions that have no return type. e.g. `function f(uint u) view public {}` This is not a problem in practice as people rarely ever write functions like this. * accounts/abi/bind: use elseif instead of nested if * core/state: include zero-address in state dump if present (#21038) * Include 0x0000 address into the dump if it is present * core/state: go fmt Co-authored-by: Alexey Akhunov <akhounov@gmail.com> * core/state: abort commit if read errors have occurred (#21039) This finally adds the error check that the documentation of StateDB.dbErr promises to do. dbErr was added in9e5f03b6c(June 2017), and the check was already missing in that commit. We somehow survived without it for three years. * core/state: avoid statedb.dbErr due to emptyCode (#21051) * core/state: more verbose stateb errors * core/state: fix flaw * core/state: fixed lint Co-authored-by: Marius van der Wijden <m.vanderwijden@live.de> * build: fix CLI params for windows LNK files (#21055) * build: Fix CLI params for windows LNK files closes #21054 * Remove parameters * core/state: make GetCodeSize mirror GetCode implementation wise * accounts/abi/bind/backend, internal/ethapi: recap gas limit with balance (#21043) * accounts/abi/bind/backend, internal/ethapi: recap gas limit with balance * accounts, internal: address comment and fix lint * accounts, internal: extend log message * tiny nits to format hexutil.Big and nil properly Co-authored-by: Péter Szilágyi <peterke@gmail.com> * params, cmd/utils: remove outdated discv5 bootnodes, deprecate flags (#20949) * params: remove outdated discv5 bootnodes * cmd/utils: deprecated bootnodesv4/v5 flags * console: fix some crashes/errors in the bridge (#21050) Fixes #21046 * core/rawdb: stop freezer process as part of freezer.Close() (#21010) * core/rawdb: Stop freezer process as part of freezer.Close() When you call db.Close(), it was closing the leveldb database first, then closing the freezer, but never stopping the freezer process. This could cause the freezer to attempt to write to leveldb after leveldb had been closed, leading to a crash with a non-zero exit code. This change adds a quit channel to the freezer, and freezer.Close() will not return until the freezer process has stopped. Additionally, when you call freezerdb.Close(), it will close the AncientStore before closing leveldb, to ensure that the freezer goroutine will be stopped before leveldb is closed. * core/rawdb: Fix formatting for golint * core/rawdb: Use backoff flag to avoid repeating select * core/rawdb: Include accidentally omitted backoff * p2p: add 0 port check in dialer (#21008) * p2p: add low port check in dialer We already have a check like this for UDP ports, add a similar one in the dialer. This prevents dials to port zero and it's also an extra layer of protection against spamming HTTP servers. * p2p/discover: use errLowPort in v4 code * p2p: change port check * p2p: add comment * p2p/simulations/adapters: ensure assigned port is in all node records * params: bump CHTs for the v1.9.14 release * cmd, core, eth: background transaction indexing (#20302) * cmd, core, eth: init tx lookup in background * core/rawdb: tiny log fixes to make it clearer what's happening * core, eth: fix rebase errors * core/rawdb: make reindexing less generic, but more optimal * rlp: implement rlp list iterator * core/rawdb: new implementation of tx indexing/unindex using generic tx iterator and hashing rlp-data * core/rawdb, cmd/utils: fix review concerns * cmd/utils: fix merge issue * core/rawdb: add some log formatting polishes Co-authored-by: rjl493456442 <garyrong0905@gmail.com> Co-authored-by: Péter Szilágyi <peterke@gmail.com> * core: fixup blockchain tests (#21062) core: fixup blockchain tests * les: drop the message if the entire p2p connection is stuck (#21033) * les: drop the message if the entire p2p connection is stuck * les: fix lint * core/rawdb : log format fix for Unindexing transaction (#21064) * core/rawdb : log format fix for Unindexing transaction * core/rawdb: tiny fixup Co-authored-by: Péter Szilágyi <peterke@gmail.com> * core/rawdb: remove unused math (#21065) * miner: support disabling empty blockprecommits form the Go API (#20736) * cmd, miner: add noempty-precommit flag * cmd, miner: get rid of external flag * miner: change bool to atomic int * miner: fix tiny typo Co-authored-by: Péter Szilágyi <peterke@gmail.com> * accounts/abi: accounts/abi/bind: Move topics to abi package (#21057) * accounts/abi/bind: added test cases for waitDeployed * accounts/abi/bind: added test case for boundContract * accounts/abi/bind: removed unnecessary resolve methods * accounts/abi: moved topics from /bind to /abi * accounts/abi/bind: cleaned up format... functions * accounts/abi: improved log message * accounts/abi: added type tests * accounts/abi/bind: remove superfluous template methods * accounts/abi: allow overloaded argument names (#21060) * accounts/abi: allow overloaded argument names In solidity it is possible to create the following contract: ``` contract Overloader { struct F { uint _f; uint __f; uint f; } function f(F memory f) public {} } ``` This however resulted in a panic in the abi package. * accounts/abi fixed error handling * core: fix missing receipt on Clique crashes (#21045) * core: fix missing receipt * core: address comment * ethstats: stop report ticker in each loop cycle #21070 (#21071) Co-authored-by: Hao Duan <duan.hao@hyperchain.cn> * params: release go-ethereum v1.9.14 * params: begin v1.9.15 release cycle * cmd: fix log contexts (#21077) * accounts/abi: simplify reflection logic (#21058) * accounts/abi: simplified reflection logic * accounts/abi: simplified reflection logic * accounts/abi: removed unpack * accounts/abi: removed comments * accounts/abi: removed uneccessary complications * accounts/abi: minor changes in error messages * accounts/abi: removed unnused code * accounts/abi: fixed indexed argument unpacking * accounts/abi: removed superfluous test cases This commit removes two test cases. The first one is trivially invalid as we have the same test cases as passing in packing_test.go L375. The second one passes now, because we don't need the mapArgNamesToStructFields in unpack_atomic anymore. Checking for purely underscored arg names generally should not be something we do as the abi/contract is generally out of the control of the user. * accounts/abi: removed comments, debug println * accounts/abi: added commented out code * accounts/abi: addressed comments * accounts/abi: remove unnecessary dst.CanSet check * accounts/abi: added dst.CanSet checks * console: fix unlockAccount argument count check (#21081) * cmd/clef, signer/core: password input fixes (#20960) * cmd/clef, signer/core: use better terminal input for passwords, make it possible to avoid boot-up warning * all: move commonly used prompter to isolated (small) package * cmd/clef: Add new --acceptWarn to clef README * cmd/clef: rename flag 'acceptWarn' to 'suppress-bootwarn' Co-authored-by: ligi <ligi@ligi.de> * accounts: add blockByNumberNoLock() to avoid double-lock (#20983) * abi/bind/backends: testcase for double-lock * accounts: add blockByNumberNoLock to avoid double-lock * backend/simulated: use stateroot, not blockhash for retrieveing state Co-authored-by: Martin Holst Swende <martin@swende.se> * whisper: use canonical import name of package go-ethereum (#21099) * trie: extend range proofs with non-existence (#21000) * trie: implement range proof with non-existent edge proof * trie: fix cornercase * trie: consider empty range * trie: add singleSide test * trie: support all-elements range proof * trie: fix typo * trie: tiny typos and formulations Co-authored-by: Péter Szilágyi <peterke@gmail.com> * les, les/lespay: implement new server pool (#20758) This PR reimplements the light client server pool. It is also a first step to move certain logic into a new lespay package. This package will contain the implementation of the lespay token sale functions, the token buying and selling logic and other components related to peer selection/prioritization and service quality evaluation. Over the long term this package will be reusable for incentivizing future protocols. Since the LES peer logic is now based on enode.Iterator, it can now use DNS-based fallback discovery to find servers. This document describes the function of the new components: https://gist.github.com/zsfelfoldi/3c7ace895234b7b345ab4f71dab102d4 * miner: replace use of 'self' as receiver name (#21113) * all: fix typos in comments (#21118) * consensus/clique: remove redundant pair of parentheses (#21104) * core/vm: improve jumpdest lookup (#21123) * consensus/ethash: fix flaky test by reading seal results (#21085) * cmd/utils: fix default DNS discovery configuration (#21124) * metrics/prometheus: define TYPE once, add tests (#21068) * metrics/prometheus: define type once for histograms * metrics/prometheus: test collector * ethdb/leveldb: use timer instead of time.After (#21066) * ethstats: avoid blocking chan when received invalid stats request (#21073) * ethstats: avoid blocking chan when received invalid stats request * ethstats: minor code polishes Co-authored-by: Péter Szilágyi <peterke@gmail.com> * trie: fix for range proof (#21107) * trie: fix for range proof * trie: fix typo * build: upgrade to golangci lint v1.27.0 (#21127) * build: upgrade to golangci-lint v1.27.0 * build: raise lint timeout to 3 minutes * eth: interrupt chain insertion on shutdown (#21114) This adds a new API method on core.BlockChain to allow interrupting running data inserts, and calls the method before shutting down the downloader. The BlockChain interrupt checks are now done through a method instead of inlining the atomic load everywhere. There is no loss of efficiency from this and it makes the interrupt protocol a lot clearer because the check is defined next to the method that sets the flag. * consensus/clique: make internal error private (#21132) Co-authored-by: linjing <linjingjing@baidu.com> * trie: enforce monotonic range in prover and return end marker (#21130) * trie: add hasRightElement indicator * trie: ensure the range is monotonic increasing * trie: address comment and fix lint * trie: address comment * trie: make linter happy Co-authored-by: Péter Szilágyi <peterke@gmail.com> * eth/downloader: more context in errors (#21067) This PR makes use of go 1.13 error handling, wrapping errors and using errors.Is to check a wrapped root-cause. It also removes the travis builders for go 1.11 and go 1.12. * metrics: replace gosigar with gopsutil (#21041) * replace gosigar with gopsutil * removed check for whether GOOS is openbsd * removed accidental import of runtime * potential fix for difference in units between gosig and gopsutil * fixed lint error * remove multiplication factor * uses cpu.ClocksPerSec as the multiplication factor * changed dependency from shirou to renaynay (#20) * updated dep * switching back from using renaynay fork to using upstream as PRs were merged on upstream * removed empty line * optimized imports * tidied go mod * core/vm: EIP-2315, JUMPSUB for the EVM (#20619) * core/vm: implement EIP 2315, subroutines for the EVM * core/vm: eip 2315 - lintfix + check jump dest validity + check ret stack size constraints logger: markdown-friendly traces, validate jumpdest, more testcase, correct opcodes * core/vm: update subroutines acc to eip: disallow walk-into * core/vm/eips: gas cost changes for subroutines * core/vm: update opcodes for EIP-2315 * core/vm: define RETURNSUB as a 'jumping' operation + review concerns Co-authored-by: Martin Holst Swende <martin@swende.se> * p2p/discover: move discv4 encoding to new 'v4wire' package (#21147) This moves all v4 protocol definitions to a new package, p2p/discover/v4wire. The new package will be used for low-level protocol tests. * rpc: send websocket ping when connection is idle (#21142) * rpc: send websocket ping when connection is idle * rpc: use non-blocking send for websocket pingReset * console: handle undefined + null in console funcs (#21160) * core: collect NewTxsEvent items without holding reorg lock (#21145) * core/vm, crypto/bls12381, params: add bls12-381 elliptic curve precompiles (#21018) * crypto: add bls12-381 elliptic curve wrapper * params: add bls12-381 precompile gas parameters * core/vm: add bls12-381 precompiles * core/vm: add bls12-381 precompile tests * go.mod, go.sum: use latest bls12381 lib * core/vm: move point encode/decode functions to base library * crypto/bls12381: introduce bls12-381 library init function * crypto/bls12381: import bls12381 elliptic curve implementation * go.mod, go.sum: remove bls12-381 library * remove unsued frobenious coeffs supress warning for inp that used in asm * add mappings tests for zero inputs fix swu g2 minus z inverse constant * crypto/bls12381: fix typo * crypto/bls12381: better comments for bls12381 constants * crypto/bls12381: swu, use single conditional for e2 * crypto/bls12381: utils, delete empty line * crypto/bls12381: utils, use FromHex for string to big * crypto/bls12381: g1, g2, strict length check for FromBytes * crypto/bls12381: field_element, comparision changes * crypto/bls12381: change swu, isogeny constants with hex values * core/vm: fix point multiplication comments * core/vm: fix multiexp gas calculation and lookup for g1 and g2 * core/vm: simpler imput length check for multiexp and pairing precompiles * core/vm: rm empty multiexp result declarations * crypto/bls12381: remove modulus type definition * crypto/bls12381: use proper init function * crypto/bls12381: get rid of new lines at fatal desciprtions * crypto/bls12-381: fix no-adx assembly multiplication * crypto/bls12-381: remove old config function * crypto/bls12381: update multiplication backend this commit changes mul backend to 6limb eip1962 backend mul assign operations are dropped * core/vm/contracts_tests: externalize test vectors for precompiles * core/vm/contracts_test: externalize failure-cases for precompiles * core/vm: linting * go.mod: tiny up sum file * core/vm: fix goimports linter issues * crypto/bls12381: build tags for plain ASM or ADX implementation Co-authored-by: Martin Holst Swende <martin@swende.se> Co-authored-by: Péter Szilágyi <peterke@gmail.com> * cmd, core, params: inital support for yolo-v1 testnet (#21154) * core,params,puppeth: inital support for yolo-v1 testnet * cmd/geth, core: add yolov1 console flag * cmd, core, params: YoloV1 bakein fixups Co-authored-by: Péter Szilágyi <peterke@gmail.com> * cmd/geth: fix the fork orders for DAO tests * core: filter out txs with invalid signatures as soon as possible Once we detect an invalid transaction during recovering signatures, we should directly exclude this transaction to avoid validating the signatures hereafter. This should optimize the validations times of transactions with invalid signatures to only one time. * acounts/keystore, cmd/faucet: fix faucet double import, fix twitter url * accounts/keystore, cmd/faucet: return old account to allow unlock * go.mod: upgrade go-duktape to hide unused function warning (#21168) * node: missing comma on toml tags (#21187) * core/vm, crypt/bls12381: fixed comments in bls (#21182) * core/vm: crypto/bls12381: minor code comments * crypto/bls12381: fix comment * core: fix typo in comments (#21181) * internal/ethapi: return revert reason for eth_call (#21083) * internal/ethapi: return revert reason for eth_call * internal/ethapi: moved revert reason logic to doCall * accounts/abi/bind/backends: added revert reason logic to simulated backend * internal/ethapi: fixed linting error * internal/ethapi: check if require reason can be unpacked * internal/ethapi: better error logic * internal/ethapi: simplify logic * internal/ethapi: return vmError() * internal/ethapi: move handling of revert out of docall * graphql: removed revert logic until spec change * rpc: internal/ethapi: added custom error types * graphql: use returndata instead of return Return() checks if there is an error. If an error is found, we return nil. For most use cases it can be beneficial to return the output even if there was an error. This code should be changed anyway once the spec supports error reasons in graphql responses * accounts/abi/bind/backends: added tests for revert reason * internal/ethapi: add errorCode to revert error * internal/ethapi: add errorCode of 3 to revertError * internal/ethapi: unified estimateGasErrors, simplified logic * internal/ethapi: unified handling of errors in DoEstimateGas * rpc: print error data field * accounts/abi/bind/backends: unify simulatedBackend and RPC * internal/ethapi: added binary data to revertError data * internal/ethapi: refactored unpacking logic into newRevertError * accounts/abi/bind/backends: fix EstimateGas * accounts, console, internal, rpc: minor error interface cleanups * Revert "accounts, console, internal, rpc: minor error interface cleanups" This reverts commit2d3ef53c53. * re-apply the good parts of2d3ef53c53* rpc: add test for returning server error data from client Co-authored-by: rjl493456442 <garyrong0905@gmail.com> Co-authored-by: Péter Szilágyi <peterke@gmail.com> Co-authored-by: Felix Lange <fjl@twurst.com> * params: update CHTs for 1.9.15 release * params: go-ethereum v1.9.15 stable * params: begin v1.9.16 release cycle * cmd/ethkey: support --passwordfile in generate command (#21183) * ethstats: use timer instead of time.sleep (#20924) * core/vm: use uint256 in EVM implementation (#20787) * core/vm: use fixed uint256 library instead of big * core/vm: remove intpools * core/vm: upgrade uint256, fixes uint256.NewFromBig * core/vm: use uint256.Int by value in Stack * core/vm: upgrade uint256 to v1.0.0 * core/vm: don't preallocate space for 1024 stack items (only 16) Co-authored-by: Martin Holst Swende <martin@swende.se> * core/state: avoid escape analysis fault when accessing cached state * accounts/keystore: fix typo in error message (#21200) * accounts/abi: make GetType public again (#21157) * eth/downloader: fix spuriously failing tests (#21149) * eth/downloader tests: fix spurious failing test due to race between receipts/headers * miner tests: fix travis failure on arm64 * eth/downloader: tests - store td in ancients too * core, eth, internal: include read storage entries in structlog output (#21204) * core, eth, internal: extend structLog tracer * core/vm, internal: add storage view * core, internal: add slots to storage directly * core: remove useless * core: address martin's comment * core/vm: fix tests * build, internal/ethapi, crypto/bls12381: fix typos (#21210) speicifc -> specific assigened -> assigned frobenious -> frobenius * go.mod: update uint256 to 1.1.0 (#21206) * internal/web3ext: add missing params to debug.accountRange (#21208) * eth/downloader: don't use defer for unlock before return (#21227) Co-authored-by: linjing <linjingjing@baidu.com> * core/rawdb: swap tailId and itemOffset for deleted items in freezer (#21220) * fix(freezer): tailId filenum offset were misplaced * core/rawdb: assume first item in freezer always start from zero * cmd, eth, internal, les: add txfee cap (#21212) * cmd, eth, internal, les: add gasprice cap * cmd/utils, eth: add default value for gasprice cap * all: use txfee cap * cmd, eth: add fix * cmd, internal: address comments * go.mod: update snappy dependency (#21237) * whisper : use timer.Ticker instead of sleep (#21240) * whisper : use timer.Ticker instead of sleep * lint: Fix linter error Co-authored-by: Guillaume Ballet <gballet@gmail.com> * core/rawdb: fix high memory usage in freezer (#21243) The ancients variable in the freezer is a list of hashes, which identifies all of the hashes to be frozen. The slice is being allocated with a capacity of `limit`, which is the number of the last block this batch will attempt to add to the freezer. That means we are allocating memory for all of the blocks in the freezer, not just the ones to be added. If instead we allocate `limit - f.frozen`, we will only allocate enough space for the blocks we're about to add to the freezer. On mainnet this reduces usage by about 320 MB. * common/fdlimit: build on DragonflyBSD (#21241) * common/fdlimit: build on DragonflyBSD * review feedback * whisper: fix time.sleep by time.ticker in whisper_test (#21251) * p2p: measure packet throughput too, not just bandwidth * cmd/devp2p: add commands for node key management (#21202) These commands mirror the key/URL generation functions of cmd/bootnode. $ devp2p key generate mynode.key $ devp2p key to-enode mynode.key -ip 203.0.113.21 -tcp 30304 enode://78a7746089baf4b8615f54a5f0b67b22b1... * crypto/secp256k1: enable 128-bit int code and endomorphism optimization (#21203) * crypto/secp256k1: enable use of __int128 This speeds up scalar & field calculations a lot. * crypto/secp256k1: enable endomorphism optimization * eth: don't block if transaction broadcast loop fails (#21255) * eth: don't block if transaction broadcast loop is returned * eth: kick out peer if we failed to send message * eth: address comment * trie: reduce allocs in insertPreimage (#21261) * core/vm: fix incorrect computation of BLS discount (#21253) * core/vm: fix incorrect computation of discount During testing on Yolov1 we found that the way geth calculates the discount is not in line with the specification. Basically what we did is calculate 128 * Bls12381GXMulGas * discount / 1000 whenever we received more than 128 pairs of values. Correct would be to calculate k * Bls12381... for k > 128. * core/vm: better logic for discount calculation * core/vm: better calculation logic, added worstcase benchmarks * core/vm: better benchmarking logic * cmd/geth: make import cmd exit with 1 if import errors occurred (#21244) The import command should not return a 0 status code if the import finishes prematurely becaues of an import error. Returning the error causes the program to exit with 1 if the err is non nil. Signed-off-by: meows <b5c6@protonmail.com> * eth: returned revert reason in traceTx (#21195) * eth: returned revert reason in traceTx * eth: return result data * utils: fix ineffectual miner config flags (#21271) Without use of global, these flags didn't actually modify miner configuration, since we weren't grabbing from the proper context scope, which should be global (vs. subcommand). Signed-off-by: meows <b5c6@protonmail.com> * cmd: abstract `getPassPhrase` functions into one (#21219) * [cmd] Abstract `getPassPhrase` functions into one. * cmd/ethkey: fix compilation failure Co-authored-by: rjl493456442 <garyrong0905@gmail.com> * cmd/evm: add state transition tool for testing (#20958) This PR implements the EVM state transition tool, which is intended to be the replacement for our retesteth client implementation. Documentation is present in the cmd/evm/README.md file. Co-authored-by: Felix Lange <fjl@twurst.com> * core: types: less allocations when hashing and tx handling (#21265) * core, crypto: various allocation savings regarding tx handling * core: reduce allocs for gas price comparison This change reduces the allocations needed for comparing different transactions to each other. A call to `tx.GasPrice()` copies the gas price as it has to be safe against modifications and also needs to be threadsafe. For comparing and ordering different transactions we don't need these guarantees * core: added tx.GasPriceIntCmp for comparison without allocation adds a method to remove unneeded allocation in comparison to tx.gasPrice * core/types: pool legacykeccak256 objects in rlpHash rlpHash is by far the most used function in core that allocates a legacyKeccak256 object on each call. Since it is so widely used it makes sense to add pooling here so we relieve the GC. On my machine these changes result in > 100 MILLION less allocations and > 30 GB less allocated memory. * reverted some changes * reverted some changes * trie: use crypto.KeccakState instead of replicating code Co-authored-by: Martin Holst Swende <martin@swende.se> * go.mod: bump gopsutil version (#21275) * go.mod: updated crypto deps causing build failure (#21276) * eth/downloader: fixes data race between synchronize and other methods (#21201) * eth/downloaded: fixed datarace between synchronize and Progress There was a race condition between `downloader.synchronize()` and `Progress` `syncWithPeer` `fetchHeight` `findAncestors` and `processHeaders` This PR changes the behavior of the downloader a bit. Previously the functions `Progress` `syncWithPeer` `fetchHeight` `findAncestors` and `processHeaders` read the syncMode anew within their loops. Now they read the syncMode at the start of their function and don't change it during their runtime. * eth/downloaded: comment * eth/downloader: added comment * cmd/geth: LES priority client test (#20719) This adds a regression test for the LES priority client API. * rlp: avoid list header allocation in encoder (#21274) List headers made up 11% of all allocations during sync. This change removes most of those allocations by keeping the list header values cached in the encoder buffer instead. Since encoder buffers are pooled, list headers are no longer allocated in the common case where an encoder buffer is available for reuse. Co-authored-by: Felix Lange <fjl@twurst.com> * core, txpool: less allocations when handling transactions (#21232) * core: use uint64 for total tx costs instead of big.Int * core: added local tx pool test case * core, crypto: various allocation savings regarding tx handling * Update core/tx_list.go * core: added tx.GasPriceIntCmp for comparison without allocation adds a method to remove unneeded allocation in comparison to tx.gasPrice * core: handle pools full of locals better * core/tests: benchmark for tx_list * core/txlist, txpool: save a reheap operation, avoid some bigint allocs Co-authored-by: Martin Holst Swende <martin@swende.se> * rpc, internal/ethapi: default rpc gascap at 25M + better error message (#21229) * rpc, internal/ethapi: default rpc gascap at 50M + better error message * eth,internal: make globalgascap uint64 * core/tests: fix compilation failure * eth/config: gascap at 25M + minor review concerns * internal/ethapi: default gas to maxgascap, not max int64 (#21284) * ethapi: don't crash when keystore-specific methods are called but external signer used (#21279) * console: prevent importRawKey from getting into CLI history * internal/ethapi: error on keystore-methods when no keystore is present * les/checkpointoracle: don't lookup checkpoint more than once per minute (#21285) * les/checkpointoracle: don't lookup checkpoint more than once per second * les/checkpoint/oracle: change oracle checktime to 1 minute * eth/gasprice: lighter gas price oracle for light client (#20409) This PR reduces the bandwidth used by the light client to compute the recommended gas price. The current mechanism for suggesting the price is: - retrieve recent 20 blocks - get the lowest gas price of these blocks - sort the price array and return the middle(60%) one This works for full nodes, which have all blocks available locally. However, this is very expensive for the light client because the light client needs to retrieve block bodies from the network. The PR changes the default options for light client. With the new config, the light client only retrieves the two latest blocks, but in order to collect more sample transactions, the 3 lowest prices are collected from each block. This PR also changes the behavior for empty blocks. If the block is empty, the lastest price is reused for sampling. * cmd/geth: allow configuring metrics HTTP server on separate endpoint (#21290) Exposing /debug/metrics and /debug/metrics/prometheus was dependent on --pprof, which also exposes other HTTP APIs. This change makes it possible to run the metrics server on an independent endpoint without enabling pprof. * build/ci: handle split up listing (#21293) * rlp: reduce allocations for big.Int and byte array encoding (#21291) This change further improves the performance of RLP encoding by removing allocations for big.Int and [...]byte types. I have added a new benchmark that measures RLP encoding of types.Block to verify that performance is improved. * cmd, node: dump empty value config (#21296) * cmd/clef: Update README with external v6.0.0 & internal v7.0.1 APIs (#21298) Changes include: * Updates response docs for `account_new`, `account_list`, `account_signTransaction` * Removes `account_import`, `account_export` docs * Adds `account_version` docs * Updates request docs for `ui_approveListing`, `ui_approveSignData`, `ui_showInfo`, `ui_showError`, `ui_onApprovedTx` * Adds `ui_approveNewAccount`, `ui_onInputRequired` docs * cmd/devp2p: add discv4 test suite (#21163) This adds a test suite for discovery v4. The test suite is a port of the Hive suite for discovery, and will replace the current suite on Hive soon-ish. The tests can be run locally with this command: devp2p discv4 test -remote enode//... Co-authored-by: Felix Lange <fjl@twurst.com> * cmd/clef: Fix broken link in README and other minor fixes (#21303) * eth: increase timeout in TestBroadcastBlock (#21299) * go.mod: upgrade to github.com/golang/snappy with arm64 asm (#21304) * eth/downloader: fix peer idleness tracking when restarting state sync (#21260) This fixes two issues with state sync restarts: When sync restarts with a new root, some peers can have in-flight requests. Since all peers with active requests were marked idle when exiting sync, the new sync would schedule more requests for those peers. When the response for the earlier request arrived, the new sync would reject it and mark the peer idle again, rendering the peer useless until it disconnected. The other issue was that peers would not be marked idle when they had delivered a response, but the response hadn't been processed before restarting the state sync. This also made the peer useless because it would be permanently marked busy. Co-authored-by: Felix Lange <fjl@twurst.com> * Revert "core, txpool: less allocations when handling transactions (#21232)" Reverting because this change started handling account balances as uint64 in the transaction pool, which is incorrect. This reverts commitaf5c97aebe. * common/math: use math/bits intrinsics for Safe* (#21316) This is a resubmit of ledgerwatch/turbo-geth#556. The performance benefit of this change is negligible, but it does remove a TODO. * cmd/geth, cmd/puppeth: replace deprecated rpc and ws flags in tests and docs (#21317) * accounts/external: remove dependency on internal/ethapi (#21319) Fixes #20535 Co-authored-by: Felix Lange <fjl@twurst.com> * params: go-ethereum v1.9.16 stable * chg: no need GO111MODULE * chg: refactor sync event Co-authored-by: Hanjiang Yu <42531996+de1acr0ix@users.noreply.github.com> Co-authored-by: Martin Holst Swende <martin@swende.se> Co-authored-by: ligi <ligi@ligi.de> Co-authored-by: Wenbiao Zheng <delweng@gmail.com> Co-authored-by: Felix Lange <fjl@twurst.com> Co-authored-by: Adam Schmideg <adamschmideg@users.noreply.github.com> Co-authored-by: gary rong <garyrong0905@gmail.com> Co-authored-by: Jeff Wentworth <jeff@curvegrid.com> Co-authored-by: ucwong <ucwong@126.com> Co-authored-by: Paweł Bylica <chfast@gmail.com> Co-authored-by: Marius van der Wijden <m.vanderwijden@live.de> Co-authored-by: Luke Champine <luke.champine@gmail.com> Co-authored-by: Boqin Qin <Bobbqqin@gmail.com> Co-authored-by: William Morriss <wjmelements@gmail.com> Co-authored-by: rene <41963722+renaynay@users.noreply.github.com> Co-authored-by: Guillaume Ballet <gballet@gmail.com> Co-authored-by: Raw Pong Ghmoa <58883403+q9f@users.noreply.github.com> Co-authored-by: Felföldi Zsolt <zsfelfoldi@gmail.com> Co-authored-by: Péter Szilágyi <peterke@gmail.com> Co-authored-by: Nishant Das <nish1993@hotmail.com> Co-authored-by: icodezjb <icodezjb@163.com> Co-authored-by: tgyKomgo <52910426+tgyKomgo@users.noreply.github.com> Co-authored-by: Steven E. Harris <seh@panix.com> Co-authored-by: Julian Y <jyap808@users.noreply.github.com> Co-authored-by: ploui <64719999+ploui@users.noreply.github.com> Co-authored-by: Alexey Akhunov <akhounov@gmail.com> Co-authored-by: AusIV <git@ausiv.com> Co-authored-by: Hao Duan <duanhao0814@gmail.com> Co-authored-by: Hao Duan <duan.hao@hyperchain.cn> Co-authored-by: Satpal <28562234+SatpalSandhu61@users.noreply.github.com> Co-authored-by: Martin Michlmayr <tbm@cyrius.com> Co-authored-by: yutianwu <wzxingbupt@gmail.com> Co-authored-by: meowsbits <45600330+meowsbits@users.noreply.github.com> Co-authored-by: Richard Patel <me@terorie.dev> Co-authored-by: sixdays <lj491685571@126.com> Co-authored-by: linjing <linjingjing@baidu.com> Co-authored-by: Greg Colvin <greg@colvin.org> Co-authored-by: chenglin <910372762@qq.com> Co-authored-by: kilic <onurkilic1004@gmail.com> Co-authored-by: Mariano Cortesi <mcortesi@gmail.com> Co-authored-by: Ev <ev@ethereum.org> Co-authored-by: libby kent <viskovitzzz@gmail.com> Co-authored-by: Pau <pau@dabax.net> Co-authored-by: Natsu Kagami <natsukagami@gmail.com> Co-authored-by: Yang Hau <vulxj0j8j8@gmail.com> Co-authored-by: HackyMiner <hackyminer@gmail.com> Co-authored-by: zhangsoledad <787953403@qq.com> Co-authored-by: AusIV <code@ausiv.com> Co-authored-by: meowsbits <b5c6@protonmail.com> Co-authored-by: Binacs <bin646891055@gmail.com> Co-authored-by: chris-j-h <39617426+chris-j-h@users.noreply.github.com>
2466 lines
93 KiB
Go
2466 lines
93 KiB
Go
// Copyright 2014 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// Package core implements the Ethereum consensus protocol.
|
|
package core
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"math/big"
|
|
mrand "math/rand"
|
|
"sort"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
lru "github.com/hashicorp/golang-lru"
|
|
"github.com/maticnetwork/bor/common"
|
|
"github.com/maticnetwork/bor/common/mclock"
|
|
"github.com/maticnetwork/bor/common/prque"
|
|
"github.com/maticnetwork/bor/consensus"
|
|
"github.com/maticnetwork/bor/core/rawdb"
|
|
"github.com/maticnetwork/bor/core/state"
|
|
"github.com/maticnetwork/bor/core/state/snapshot"
|
|
"github.com/maticnetwork/bor/core/types"
|
|
"github.com/maticnetwork/bor/core/vm"
|
|
"github.com/maticnetwork/bor/ethdb"
|
|
"github.com/maticnetwork/bor/event"
|
|
"github.com/maticnetwork/bor/log"
|
|
"github.com/maticnetwork/bor/metrics"
|
|
"github.com/maticnetwork/bor/params"
|
|
"github.com/maticnetwork/bor/rlp"
|
|
"github.com/maticnetwork/bor/trie"
|
|
)
|
|
|
|
var (
|
|
headBlockGauge = metrics.NewRegisteredGauge("chain/head/block", nil)
|
|
headHeaderGauge = metrics.NewRegisteredGauge("chain/head/header", nil)
|
|
headFastBlockGauge = metrics.NewRegisteredGauge("chain/head/receipt", nil)
|
|
|
|
accountReadTimer = metrics.NewRegisteredTimer("chain/account/reads", nil)
|
|
accountHashTimer = metrics.NewRegisteredTimer("chain/account/hashes", nil)
|
|
accountUpdateTimer = metrics.NewRegisteredTimer("chain/account/updates", nil)
|
|
accountCommitTimer = metrics.NewRegisteredTimer("chain/account/commits", nil)
|
|
|
|
storageReadTimer = metrics.NewRegisteredTimer("chain/storage/reads", nil)
|
|
storageHashTimer = metrics.NewRegisteredTimer("chain/storage/hashes", nil)
|
|
storageUpdateTimer = metrics.NewRegisteredTimer("chain/storage/updates", nil)
|
|
storageCommitTimer = metrics.NewRegisteredTimer("chain/storage/commits", nil)
|
|
|
|
snapshotAccountReadTimer = metrics.NewRegisteredTimer("chain/snapshot/account/reads", nil)
|
|
snapshotStorageReadTimer = metrics.NewRegisteredTimer("chain/snapshot/storage/reads", nil)
|
|
snapshotCommitTimer = metrics.NewRegisteredTimer("chain/snapshot/commits", nil)
|
|
|
|
blockInsertTimer = metrics.NewRegisteredTimer("chain/inserts", nil)
|
|
blockValidationTimer = metrics.NewRegisteredTimer("chain/validation", nil)
|
|
blockExecutionTimer = metrics.NewRegisteredTimer("chain/execution", nil)
|
|
blockWriteTimer = metrics.NewRegisteredTimer("chain/write", nil)
|
|
blockReorgAddMeter = metrics.NewRegisteredMeter("chain/reorg/drop", nil)
|
|
blockReorgDropMeter = metrics.NewRegisteredMeter("chain/reorg/add", nil)
|
|
|
|
blockPrefetchExecuteTimer = metrics.NewRegisteredTimer("chain/prefetch/executes", nil)
|
|
blockPrefetchInterruptMeter = metrics.NewRegisteredMeter("chain/prefetch/interrupts", nil)
|
|
|
|
errInsertionInterrupted = errors.New("insertion is interrupted")
|
|
)
|
|
|
|
const (
|
|
bodyCacheLimit = 256
|
|
blockCacheLimit = 256
|
|
receiptsCacheLimit = 32
|
|
txLookupCacheLimit = 1024
|
|
maxFutureBlocks = 256
|
|
maxTimeFutureBlocks = 30
|
|
badBlockLimit = 10
|
|
TriesInMemory = 128
|
|
|
|
// BlockChainVersion ensures that an incompatible database forces a resync from scratch.
|
|
//
|
|
// Changelog:
|
|
//
|
|
// - Version 4
|
|
// The following incompatible database changes were added:
|
|
// * the `BlockNumber`, `TxHash`, `TxIndex`, `BlockHash` and `Index` fields of log are deleted
|
|
// * the `Bloom` field of receipt is deleted
|
|
// * the `BlockIndex` and `TxIndex` fields of txlookup are deleted
|
|
// - Version 5
|
|
// The following incompatible database changes were added:
|
|
// * the `TxHash`, `GasCost`, and `ContractAddress` fields are no longer stored for a receipt
|
|
// * the `TxHash`, `GasCost`, and `ContractAddress` fields are computed by looking up the
|
|
// receipts' corresponding block
|
|
// - Version 6
|
|
// The following incompatible database changes were added:
|
|
// * Transaction lookup information stores the corresponding block number instead of block hash
|
|
// - Version 7
|
|
// The following incompatible database changes were added:
|
|
// * Use freezer as the ancient database to maintain all ancient data
|
|
BlockChainVersion uint64 = 7
|
|
)
|
|
|
|
// CacheConfig contains the configuration values for the trie caching/pruning
|
|
// that's resident in a blockchain.
|
|
type CacheConfig struct {
|
|
TrieCleanLimit int // Memory allowance (MB) to use for caching trie nodes in memory
|
|
TrieCleanNoPrefetch bool // Whether to disable heuristic state prefetching for followup blocks
|
|
TrieDirtyLimit int // Memory limit (MB) at which to start flushing dirty trie nodes to disk
|
|
TrieDirtyDisabled bool // Whether to disable trie write caching and GC altogether (archive node)
|
|
TrieTimeLimit time.Duration // Time limit after which to flush the current in-memory trie to disk
|
|
SnapshotLimit int // Memory allowance (MB) to use for caching snapshot entries in memory
|
|
|
|
SnapshotWait bool // Wait for snapshot construction on startup. TODO(karalabe): This is a dirty hack for testing, nuke it
|
|
}
|
|
|
|
// BlockChain represents the canonical chain given a database with a genesis
|
|
// block. The Blockchain manages chain imports, reverts, chain reorganisations.
|
|
//
|
|
// Importing blocks in to the block chain happens according to the set of rules
|
|
// defined by the two stage Validator. Processing of blocks is done using the
|
|
// Processor which processes the included transaction. The validation of the state
|
|
// is done in the second part of the Validator. Failing results in aborting of
|
|
// the import.
|
|
//
|
|
// The BlockChain also helps in returning blocks from **any** chain included
|
|
// in the database as well as blocks that represents the canonical chain. It's
|
|
// important to note that GetBlock can return any block and does not need to be
|
|
// included in the canonical one where as GetBlockByNumber always represents the
|
|
// canonical chain.
|
|
type BlockChain struct {
|
|
chainConfig *params.ChainConfig // Chain & network configuration
|
|
cacheConfig *CacheConfig // Cache configuration for pruning
|
|
|
|
db ethdb.Database // Low level persistent database to store final content in
|
|
snaps *snapshot.Tree // Snapshot tree for fast trie leaf access
|
|
triegc *prque.Prque // Priority queue mapping block numbers to tries to gc
|
|
gcproc time.Duration // Accumulates canonical block processing for trie dumping
|
|
|
|
// txLookupLimit is the maximum number of blocks from head whose tx indices
|
|
// are reserved:
|
|
// * 0: means no limit and regenerate any missing indexes
|
|
// * N: means N block limit [HEAD-N+1, HEAD] and delete extra indexes
|
|
// * nil: disable tx reindexer/deleter, but still index new blocks
|
|
txLookupLimit uint64
|
|
|
|
hc *HeaderChain
|
|
rmLogsFeed event.Feed
|
|
chainFeed event.Feed
|
|
chainSideFeed event.Feed
|
|
chainHeadFeed event.Feed
|
|
logsFeed event.Feed
|
|
blockProcFeed event.Feed
|
|
stateSyncFeed event.Feed
|
|
|
|
scope event.SubscriptionScope
|
|
genesisBlock *types.Block
|
|
|
|
chainmu sync.RWMutex // blockchain insertion lock
|
|
|
|
currentBlock atomic.Value // Current head of the block chain
|
|
currentFastBlock atomic.Value // Current head of the fast-sync chain (may be above the block chain!)
|
|
|
|
stateCache state.Database // State database to reuse between imports (contains state cache)
|
|
bodyCache *lru.Cache // Cache for the most recent block bodies
|
|
bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format
|
|
receiptsCache *lru.Cache // Cache for the most recent receipts per block
|
|
blockCache *lru.Cache // Cache for the most recent entire blocks
|
|
txLookupCache *lru.Cache // Cache for the most recent transaction lookup data.
|
|
futureBlocks *lru.Cache // future blocks are blocks added for later processing
|
|
|
|
quit chan struct{} // blockchain quit channel
|
|
wg sync.WaitGroup // chain processing wait group for shutting down
|
|
running int32 // 0 if chain is running, 1 when stopped
|
|
procInterrupt int32 // interrupt signaler for block processing
|
|
|
|
engine consensus.Engine
|
|
validator Validator // Block and state validator interface
|
|
prefetcher Prefetcher // Block state prefetcher interface
|
|
processor Processor // Block transaction processor interface
|
|
vmConfig vm.Config
|
|
|
|
badBlocks *lru.Cache // Bad block cache
|
|
shouldPreserve func(*types.Block) bool // Function used to determine whether should preserve the given block.
|
|
terminateInsert func(common.Hash, uint64) bool // Testing hook used to terminate ancient receipt chain insertion.
|
|
}
|
|
|
|
// NewBlockChain returns a fully initialised block chain using information
|
|
// available in the database. It initialises the default Ethereum Validator and
|
|
// Processor.
|
|
func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *params.ChainConfig, engine consensus.Engine, vmConfig vm.Config, shouldPreserve func(block *types.Block) bool, txLookupLimit *uint64) (*BlockChain, error) {
|
|
if cacheConfig == nil {
|
|
cacheConfig = &CacheConfig{
|
|
TrieCleanLimit: 256,
|
|
TrieDirtyLimit: 256,
|
|
TrieTimeLimit: 5 * time.Minute,
|
|
SnapshotLimit: 256,
|
|
SnapshotWait: true,
|
|
}
|
|
}
|
|
bodyCache, _ := lru.New(bodyCacheLimit)
|
|
bodyRLPCache, _ := lru.New(bodyCacheLimit)
|
|
receiptsCache, _ := lru.New(receiptsCacheLimit)
|
|
blockCache, _ := lru.New(blockCacheLimit)
|
|
txLookupCache, _ := lru.New(txLookupCacheLimit)
|
|
futureBlocks, _ := lru.New(maxFutureBlocks)
|
|
badBlocks, _ := lru.New(badBlockLimit)
|
|
|
|
bc := &BlockChain{
|
|
chainConfig: chainConfig,
|
|
cacheConfig: cacheConfig,
|
|
db: db,
|
|
triegc: prque.New(nil),
|
|
stateCache: state.NewDatabaseWithCache(db, cacheConfig.TrieCleanLimit),
|
|
quit: make(chan struct{}),
|
|
shouldPreserve: shouldPreserve,
|
|
bodyCache: bodyCache,
|
|
bodyRLPCache: bodyRLPCache,
|
|
receiptsCache: receiptsCache,
|
|
blockCache: blockCache,
|
|
txLookupCache: txLookupCache,
|
|
futureBlocks: futureBlocks,
|
|
engine: engine,
|
|
vmConfig: vmConfig,
|
|
badBlocks: badBlocks,
|
|
}
|
|
bc.validator = NewBlockValidator(chainConfig, bc, engine)
|
|
bc.prefetcher = newStatePrefetcher(chainConfig, bc, engine)
|
|
bc.processor = NewStateProcessor(chainConfig, bc, engine)
|
|
|
|
var err error
|
|
bc.hc, err = NewHeaderChain(db, chainConfig, engine, bc.insertStopped)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
bc.genesisBlock = bc.GetBlockByNumber(0)
|
|
if bc.genesisBlock == nil {
|
|
return nil, ErrNoGenesis
|
|
}
|
|
|
|
var nilBlock *types.Block
|
|
bc.currentBlock.Store(nilBlock)
|
|
bc.currentFastBlock.Store(nilBlock)
|
|
|
|
// Initialize the chain with ancient data if it isn't empty.
|
|
var txIndexBlock uint64
|
|
|
|
if bc.empty() {
|
|
rawdb.InitDatabaseFromFreezer(bc.db)
|
|
// If ancient database is not empty, reconstruct all missing
|
|
// indices in the background.
|
|
frozen, _ := bc.db.Ancients()
|
|
if frozen > 0 {
|
|
txIndexBlock = frozen
|
|
}
|
|
}
|
|
|
|
if err := bc.loadLastState(); err != nil {
|
|
return nil, err
|
|
}
|
|
// The first thing the node will do is reconstruct the verification data for
|
|
// the head block (ethash cache or clique voting snapshot). Might as well do
|
|
// it in advance.
|
|
// bc.engine.VerifyHeader(bc, bc.CurrentHeader(), true)
|
|
|
|
if frozen, err := bc.db.Ancients(); err == nil && frozen > 0 {
|
|
var (
|
|
needRewind bool
|
|
low uint64
|
|
)
|
|
// The head full block may be rolled back to a very low height due to
|
|
// blockchain repair. If the head full block is even lower than the ancient
|
|
// chain, truncate the ancient store.
|
|
fullBlock := bc.CurrentBlock()
|
|
if fullBlock != nil && fullBlock != bc.genesisBlock && fullBlock.NumberU64() < frozen-1 {
|
|
needRewind = true
|
|
low = fullBlock.NumberU64()
|
|
}
|
|
// In fast sync, it may happen that ancient data has been written to the
|
|
// ancient store, but the LastFastBlock has not been updated, truncate the
|
|
// extra data here.
|
|
fastBlock := bc.CurrentFastBlock()
|
|
if fastBlock != nil && fastBlock.NumberU64() < frozen-1 {
|
|
needRewind = true
|
|
if fastBlock.NumberU64() < low || low == 0 {
|
|
low = fastBlock.NumberU64()
|
|
}
|
|
}
|
|
if needRewind {
|
|
var hashes []common.Hash
|
|
previous := bc.CurrentHeader().Number.Uint64()
|
|
for i := low + 1; i <= bc.CurrentHeader().Number.Uint64(); i++ {
|
|
hashes = append(hashes, rawdb.ReadCanonicalHash(bc.db, i))
|
|
}
|
|
bc.Rollback(hashes)
|
|
log.Warn("Truncate ancient chain", "from", previous, "to", low)
|
|
}
|
|
}
|
|
// Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain
|
|
for hash := range BadHashes {
|
|
if header := bc.GetHeaderByHash(hash); header != nil {
|
|
// get the canonical block corresponding to the offending header's number
|
|
headerByNumber := bc.GetHeaderByNumber(header.Number.Uint64())
|
|
// make sure the headerByNumber (if present) is in our current canonical chain
|
|
if headerByNumber != nil && headerByNumber.Hash() == header.Hash() {
|
|
log.Error("Found bad hash, rewinding chain", "number", header.Number, "hash", header.ParentHash)
|
|
bc.SetHead(header.Number.Uint64() - 1)
|
|
log.Error("Chain rewind was successful, resuming normal operation")
|
|
}
|
|
}
|
|
}
|
|
// Load any existing snapshot, regenerating it if loading failed
|
|
if bc.cacheConfig.SnapshotLimit > 0 {
|
|
bc.snaps = snapshot.New(bc.db, bc.stateCache.TrieDB(), bc.cacheConfig.SnapshotLimit, bc.CurrentBlock().Root(), !bc.cacheConfig.SnapshotWait)
|
|
}
|
|
// Take ownership of this particular state
|
|
go bc.update()
|
|
if txLookupLimit != nil {
|
|
bc.txLookupLimit = *txLookupLimit
|
|
go bc.maintainTxIndex(txIndexBlock)
|
|
}
|
|
return bc, nil
|
|
}
|
|
|
|
// GetVMConfig returns the block chain VM config.
|
|
func (bc *BlockChain) GetVMConfig() *vm.Config {
|
|
return &bc.vmConfig
|
|
}
|
|
|
|
// empty returns an indicator whether the blockchain is empty.
|
|
// Note, it's a special case that we connect a non-empty ancient
|
|
// database with an empty node, so that we can plugin the ancient
|
|
// into node seamlessly.
|
|
func (bc *BlockChain) empty() bool {
|
|
genesis := bc.genesisBlock.Hash()
|
|
for _, hash := range []common.Hash{rawdb.ReadHeadBlockHash(bc.db), rawdb.ReadHeadHeaderHash(bc.db), rawdb.ReadHeadFastBlockHash(bc.db)} {
|
|
if hash != genesis {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// loadLastState loads the last known chain state from the database. This method
|
|
// assumes that the chain manager mutex is held.
|
|
func (bc *BlockChain) loadLastState() error {
|
|
// Restore the last known head block
|
|
head := rawdb.ReadHeadBlockHash(bc.db)
|
|
if head == (common.Hash{}) {
|
|
// Corrupt or empty database, init from scratch
|
|
log.Warn("Empty database, resetting chain")
|
|
return bc.Reset()
|
|
}
|
|
// Make sure the entire head block is available
|
|
currentBlock := bc.GetBlockByHash(head)
|
|
if currentBlock == nil {
|
|
// Corrupt or empty database, init from scratch
|
|
log.Warn("Head block missing, resetting chain", "hash", head)
|
|
return bc.Reset()
|
|
}
|
|
// Make sure the state associated with the block is available
|
|
if _, err := state.New(currentBlock.Root(), bc.stateCache, bc.snaps); err != nil {
|
|
// Dangling block without a state associated, init from scratch
|
|
log.Warn("Head state missing, repairing chain", "number", currentBlock.Number(), "hash", currentBlock.Hash())
|
|
if err := bc.repair(¤tBlock); err != nil {
|
|
return err
|
|
}
|
|
rawdb.WriteHeadBlockHash(bc.db, currentBlock.Hash())
|
|
}
|
|
// Everything seems to be fine, set as the head block
|
|
bc.currentBlock.Store(currentBlock)
|
|
headBlockGauge.Update(int64(currentBlock.NumberU64()))
|
|
|
|
// Restore the last known head header
|
|
currentHeader := currentBlock.Header()
|
|
if head := rawdb.ReadHeadHeaderHash(bc.db); head != (common.Hash{}) {
|
|
if header := bc.GetHeaderByHash(head); header != nil {
|
|
currentHeader = header
|
|
}
|
|
}
|
|
bc.hc.SetCurrentHeader(currentHeader)
|
|
|
|
// Restore the last known head fast block
|
|
bc.currentFastBlock.Store(currentBlock)
|
|
headFastBlockGauge.Update(int64(currentBlock.NumberU64()))
|
|
|
|
if head := rawdb.ReadHeadFastBlockHash(bc.db); head != (common.Hash{}) {
|
|
if block := bc.GetBlockByHash(head); block != nil {
|
|
bc.currentFastBlock.Store(block)
|
|
headFastBlockGauge.Update(int64(block.NumberU64()))
|
|
}
|
|
}
|
|
// Issue a status log for the user
|
|
currentFastBlock := bc.CurrentFastBlock()
|
|
|
|
headerTd := bc.GetTd(currentHeader.Hash(), currentHeader.Number.Uint64())
|
|
blockTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64())
|
|
fastTd := bc.GetTd(currentFastBlock.Hash(), currentFastBlock.NumberU64())
|
|
|
|
log.Info("Loaded most recent local header", "number", currentHeader.Number, "hash", currentHeader.Hash(), "td", headerTd, "age", common.PrettyAge(time.Unix(int64(currentHeader.Time), 0)))
|
|
log.Info("Loaded most recent local full block", "number", currentBlock.Number(), "hash", currentBlock.Hash(), "td", blockTd, "age", common.PrettyAge(time.Unix(int64(currentBlock.Time()), 0)))
|
|
log.Info("Loaded most recent local fast block", "number", currentFastBlock.Number(), "hash", currentFastBlock.Hash(), "td", fastTd, "age", common.PrettyAge(time.Unix(int64(currentFastBlock.Time()), 0)))
|
|
|
|
return nil
|
|
}
|
|
|
|
// SetHead rewinds the local chain to a new head. In the case of headers, everything
|
|
// above the new head will be deleted and the new one set. In the case of blocks
|
|
// though, the head may be further rewound if block bodies are missing (non-archive
|
|
// nodes after a fast sync).
|
|
func (bc *BlockChain) SetHead(head uint64) error {
|
|
log.Warn("Rewinding blockchain", "target", head)
|
|
|
|
bc.chainmu.Lock()
|
|
defer bc.chainmu.Unlock()
|
|
|
|
updateFn := func(db ethdb.KeyValueWriter, header *types.Header) {
|
|
// Rewind the block chain, ensuring we don't end up with a stateless head block
|
|
if currentBlock := bc.CurrentBlock(); currentBlock != nil && header.Number.Uint64() < currentBlock.NumberU64() {
|
|
newHeadBlock := bc.GetBlock(header.Hash(), header.Number.Uint64())
|
|
if newHeadBlock == nil {
|
|
newHeadBlock = bc.genesisBlock
|
|
} else {
|
|
if _, err := state.New(newHeadBlock.Root(), bc.stateCache, bc.snaps); err != nil {
|
|
// Rewound state missing, rolled back to before pivot, reset to genesis
|
|
newHeadBlock = bc.genesisBlock
|
|
}
|
|
}
|
|
rawdb.WriteHeadBlockHash(db, newHeadBlock.Hash())
|
|
|
|
// Degrade the chain markers if they are explicitly reverted.
|
|
// In theory we should update all in-memory markers in the
|
|
// last step, however the direction of SetHead is from high
|
|
// to low, so it's safe the update in-memory markers directly.
|
|
bc.currentBlock.Store(newHeadBlock)
|
|
headBlockGauge.Update(int64(newHeadBlock.NumberU64()))
|
|
}
|
|
|
|
// Rewind the fast block in a simpleton way to the target head
|
|
if currentFastBlock := bc.CurrentFastBlock(); currentFastBlock != nil && header.Number.Uint64() < currentFastBlock.NumberU64() {
|
|
newHeadFastBlock := bc.GetBlock(header.Hash(), header.Number.Uint64())
|
|
// If either blocks reached nil, reset to the genesis state
|
|
if newHeadFastBlock == nil {
|
|
newHeadFastBlock = bc.genesisBlock
|
|
}
|
|
rawdb.WriteHeadFastBlockHash(db, newHeadFastBlock.Hash())
|
|
|
|
// Degrade the chain markers if they are explicitly reverted.
|
|
// In theory we should update all in-memory markers in the
|
|
// last step, however the direction of SetHead is from high
|
|
// to low, so it's safe the update in-memory markers directly.
|
|
bc.currentFastBlock.Store(newHeadFastBlock)
|
|
headFastBlockGauge.Update(int64(newHeadFastBlock.NumberU64()))
|
|
}
|
|
}
|
|
|
|
// Rewind the header chain, deleting all block bodies until then
|
|
delFn := func(db ethdb.KeyValueWriter, hash common.Hash, num uint64) {
|
|
// Ignore the error here since light client won't hit this path
|
|
frozen, _ := bc.db.Ancients()
|
|
if num+1 <= frozen {
|
|
// Truncate all relative data(header, total difficulty, body, receipt
|
|
// and canonical hash) from ancient store.
|
|
if err := bc.db.TruncateAncients(num + 1); err != nil {
|
|
log.Crit("Failed to truncate ancient data", "number", num, "err", err)
|
|
}
|
|
|
|
// Remove the hash <-> number mapping from the active store.
|
|
rawdb.DeleteHeaderNumber(db, hash)
|
|
} else {
|
|
// Remove relative body and receipts from the active store.
|
|
// The header, total difficulty and canonical hash will be
|
|
// removed in the hc.SetHead function.
|
|
rawdb.DeleteBody(db, hash, num)
|
|
rawdb.DeleteReceipts(db, hash, num)
|
|
}
|
|
// Todo(rjl493456442) txlookup, bloombits, etc
|
|
}
|
|
bc.hc.SetHead(head, updateFn, delFn)
|
|
|
|
// Clear out any stale content from the caches
|
|
bc.bodyCache.Purge()
|
|
bc.bodyRLPCache.Purge()
|
|
bc.receiptsCache.Purge()
|
|
bc.blockCache.Purge()
|
|
bc.txLookupCache.Purge()
|
|
bc.futureBlocks.Purge()
|
|
|
|
return bc.loadLastState()
|
|
}
|
|
|
|
// FastSyncCommitHead sets the current head block to the one defined by the hash
|
|
// irrelevant what the chain contents were prior.
|
|
func (bc *BlockChain) FastSyncCommitHead(hash common.Hash) error {
|
|
// Make sure that both the block as well at its state trie exists
|
|
block := bc.GetBlockByHash(hash)
|
|
if block == nil {
|
|
return fmt.Errorf("non existent block [%x…]", hash[:4])
|
|
}
|
|
if _, err := trie.NewSecure(block.Root(), bc.stateCache.TrieDB()); err != nil {
|
|
return err
|
|
}
|
|
// If all checks out, manually set the head block
|
|
bc.chainmu.Lock()
|
|
bc.currentBlock.Store(block)
|
|
headBlockGauge.Update(int64(block.NumberU64()))
|
|
bc.chainmu.Unlock()
|
|
|
|
// Destroy any existing state snapshot and regenerate it in the background
|
|
if bc.snaps != nil {
|
|
bc.snaps.Rebuild(block.Root())
|
|
}
|
|
log.Info("Committed new head block", "number", block.Number(), "hash", hash)
|
|
return nil
|
|
}
|
|
|
|
// GasLimit returns the gas limit of the current HEAD block.
|
|
func (bc *BlockChain) GasLimit() uint64 {
|
|
return bc.CurrentBlock().GasLimit()
|
|
}
|
|
|
|
// CurrentBlock retrieves the current head block of the canonical chain. The
|
|
// block is retrieved from the blockchain's internal cache.
|
|
func (bc *BlockChain) CurrentBlock() *types.Block {
|
|
return bc.currentBlock.Load().(*types.Block)
|
|
}
|
|
|
|
// Snapshot returns the blockchain snapshot tree. This method is mainly used for
|
|
// testing, to make it possible to verify the snapshot after execution.
|
|
//
|
|
// Warning: There are no guarantees about the safety of using the returned 'snap' if the
|
|
// blockchain is simultaneously importing blocks, so take care.
|
|
func (bc *BlockChain) Snapshot() *snapshot.Tree {
|
|
return bc.snaps
|
|
}
|
|
|
|
// CurrentFastBlock retrieves the current fast-sync head block of the canonical
|
|
// chain. The block is retrieved from the blockchain's internal cache.
|
|
func (bc *BlockChain) CurrentFastBlock() *types.Block {
|
|
return bc.currentFastBlock.Load().(*types.Block)
|
|
}
|
|
|
|
// Validator returns the current validator.
|
|
func (bc *BlockChain) Validator() Validator {
|
|
return bc.validator
|
|
}
|
|
|
|
// Processor returns the current processor.
|
|
func (bc *BlockChain) Processor() Processor {
|
|
return bc.processor
|
|
}
|
|
|
|
// State returns a new mutable state based on the current HEAD block.
|
|
func (bc *BlockChain) State() (*state.StateDB, error) {
|
|
return bc.StateAt(bc.CurrentBlock().Root())
|
|
}
|
|
|
|
// StateAt returns a new mutable state based on a particular point in time.
|
|
func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) {
|
|
return state.New(root, bc.stateCache, bc.snaps)
|
|
}
|
|
|
|
// StateCache returns the caching database underpinning the blockchain instance.
|
|
func (bc *BlockChain) StateCache() state.Database {
|
|
return bc.stateCache
|
|
}
|
|
|
|
// Reset purges the entire blockchain, restoring it to its genesis state.
|
|
func (bc *BlockChain) Reset() error {
|
|
return bc.ResetWithGenesisBlock(bc.genesisBlock)
|
|
}
|
|
|
|
// ResetWithGenesisBlock purges the entire blockchain, restoring it to the
|
|
// specified genesis state.
|
|
func (bc *BlockChain) ResetWithGenesisBlock(genesis *types.Block) error {
|
|
// Dump the entire block chain and purge the caches
|
|
if err := bc.SetHead(0); err != nil {
|
|
return err
|
|
}
|
|
bc.chainmu.Lock()
|
|
defer bc.chainmu.Unlock()
|
|
|
|
// Prepare the genesis block and reinitialise the chain
|
|
batch := bc.db.NewBatch()
|
|
rawdb.WriteTd(batch, genesis.Hash(), genesis.NumberU64(), genesis.Difficulty())
|
|
rawdb.WriteBlock(batch, genesis)
|
|
if err := batch.Write(); err != nil {
|
|
log.Crit("Failed to write genesis block", "err", err)
|
|
}
|
|
bc.writeHeadBlock(genesis)
|
|
|
|
// Last update all in-memory chain markers
|
|
bc.genesisBlock = genesis
|
|
bc.currentBlock.Store(bc.genesisBlock)
|
|
headBlockGauge.Update(int64(bc.genesisBlock.NumberU64()))
|
|
bc.hc.SetGenesis(bc.genesisBlock.Header())
|
|
bc.hc.SetCurrentHeader(bc.genesisBlock.Header())
|
|
bc.currentFastBlock.Store(bc.genesisBlock)
|
|
headFastBlockGauge.Update(int64(bc.genesisBlock.NumberU64()))
|
|
return nil
|
|
}
|
|
|
|
// repair tries to repair the current blockchain by rolling back the current block
|
|
// until one with associated state is found. This is needed to fix incomplete db
|
|
// writes caused either by crashes/power outages, or simply non-committed tries.
|
|
//
|
|
// This method only rolls back the current block. The current header and current
|
|
// fast block are left intact.
|
|
func (bc *BlockChain) repair(head **types.Block) error {
|
|
for {
|
|
// Abort if we've rewound to a head block that does have associated state
|
|
if _, err := state.New((*head).Root(), bc.stateCache, bc.snaps); err == nil {
|
|
log.Info("Rewound blockchain to past state", "number", (*head).Number(), "hash", (*head).Hash())
|
|
return nil
|
|
}
|
|
// Otherwise rewind one block and recheck state availability there
|
|
block := bc.GetBlock((*head).ParentHash(), (*head).NumberU64()-1)
|
|
if block == nil {
|
|
return fmt.Errorf("missing block %d [%x]", (*head).NumberU64()-1, (*head).ParentHash())
|
|
}
|
|
*head = block
|
|
}
|
|
}
|
|
|
|
// Export writes the active chain to the given writer.
|
|
func (bc *BlockChain) Export(w io.Writer) error {
|
|
return bc.ExportN(w, uint64(0), bc.CurrentBlock().NumberU64())
|
|
}
|
|
|
|
// ExportN writes a subset of the active chain to the given writer.
|
|
func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error {
|
|
bc.chainmu.RLock()
|
|
defer bc.chainmu.RUnlock()
|
|
|
|
if first > last {
|
|
return fmt.Errorf("export failed: first (%d) is greater than last (%d)", first, last)
|
|
}
|
|
log.Info("Exporting batch of blocks", "count", last-first+1)
|
|
|
|
start, reported := time.Now(), time.Now()
|
|
for nr := first; nr <= last; nr++ {
|
|
block := bc.GetBlockByNumber(nr)
|
|
if block == nil {
|
|
return fmt.Errorf("export failed on #%d: not found", nr)
|
|
}
|
|
if err := block.EncodeRLP(w); err != nil {
|
|
return err
|
|
}
|
|
if time.Since(reported) >= statsReportLimit {
|
|
log.Info("Exporting blocks", "exported", block.NumberU64()-first, "elapsed", common.PrettyDuration(time.Since(start)))
|
|
reported = time.Now()
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// writeHeadBlock injects a new head block into the current block chain. This method
|
|
// assumes that the block is indeed a true head. It will also reset the head
|
|
// header and the head fast sync block to this very same block if they are older
|
|
// or if they are on a different side chain.
|
|
//
|
|
// Note, this function assumes that the `mu` mutex is held!
|
|
func (bc *BlockChain) writeHeadBlock(block *types.Block) {
|
|
// If the block is on a side chain or an unknown one, force other heads onto it too
|
|
updateHeads := rawdb.ReadCanonicalHash(bc.db, block.NumberU64()) != block.Hash()
|
|
|
|
// Add the block to the canonical chain number scheme and mark as the head
|
|
batch := bc.db.NewBatch()
|
|
rawdb.WriteCanonicalHash(batch, block.Hash(), block.NumberU64())
|
|
rawdb.WriteTxLookupEntries(batch, block)
|
|
rawdb.WriteHeadBlockHash(batch, block.Hash())
|
|
|
|
// If the block is better than our head or is on a different chain, force update heads
|
|
if updateHeads {
|
|
rawdb.WriteHeadHeaderHash(batch, block.Hash())
|
|
rawdb.WriteHeadFastBlockHash(batch, block.Hash())
|
|
}
|
|
// Flush the whole batch into the disk, exit the node if failed
|
|
if err := batch.Write(); err != nil {
|
|
log.Crit("Failed to update chain indexes and markers", "err", err)
|
|
}
|
|
// Update all in-memory chain markers in the last step
|
|
if updateHeads {
|
|
bc.hc.SetCurrentHeader(block.Header())
|
|
bc.currentFastBlock.Store(block)
|
|
headFastBlockGauge.Update(int64(block.NumberU64()))
|
|
}
|
|
bc.currentBlock.Store(block)
|
|
headBlockGauge.Update(int64(block.NumberU64()))
|
|
}
|
|
|
|
// Genesis retrieves the chain's genesis block.
|
|
func (bc *BlockChain) Genesis() *types.Block {
|
|
return bc.genesisBlock
|
|
}
|
|
|
|
// GetBody retrieves a block body (transactions and uncles) from the database by
|
|
// hash, caching it if found.
|
|
func (bc *BlockChain) GetBody(hash common.Hash) *types.Body {
|
|
// Short circuit if the body's already in the cache, retrieve otherwise
|
|
if cached, ok := bc.bodyCache.Get(hash); ok {
|
|
body := cached.(*types.Body)
|
|
return body
|
|
}
|
|
number := bc.hc.GetBlockNumber(hash)
|
|
if number == nil {
|
|
return nil
|
|
}
|
|
body := rawdb.ReadBody(bc.db, hash, *number)
|
|
if body == nil {
|
|
return nil
|
|
}
|
|
// Cache the found body for next time and return
|
|
bc.bodyCache.Add(hash, body)
|
|
return body
|
|
}
|
|
|
|
// GetBodyRLP retrieves a block body in RLP encoding from the database by hash,
|
|
// caching it if found.
|
|
func (bc *BlockChain) GetBodyRLP(hash common.Hash) rlp.RawValue {
|
|
// Short circuit if the body's already in the cache, retrieve otherwise
|
|
if cached, ok := bc.bodyRLPCache.Get(hash); ok {
|
|
return cached.(rlp.RawValue)
|
|
}
|
|
number := bc.hc.GetBlockNumber(hash)
|
|
if number == nil {
|
|
return nil
|
|
}
|
|
body := rawdb.ReadBodyRLP(bc.db, hash, *number)
|
|
if len(body) == 0 {
|
|
return nil
|
|
}
|
|
// Cache the found body for next time and return
|
|
bc.bodyRLPCache.Add(hash, body)
|
|
return body
|
|
}
|
|
|
|
// HasBlock checks if a block is fully present in the database or not.
|
|
func (bc *BlockChain) HasBlock(hash common.Hash, number uint64) bool {
|
|
if bc.blockCache.Contains(hash) {
|
|
return true
|
|
}
|
|
return rawdb.HasBody(bc.db, hash, number)
|
|
}
|
|
|
|
// HasFastBlock checks if a fast block is fully present in the database or not.
|
|
func (bc *BlockChain) HasFastBlock(hash common.Hash, number uint64) bool {
|
|
if !bc.HasBlock(hash, number) {
|
|
return false
|
|
}
|
|
if bc.receiptsCache.Contains(hash) {
|
|
return true
|
|
}
|
|
return rawdb.HasReceipts(bc.db, hash, number)
|
|
}
|
|
|
|
// HasState checks if state trie is fully present in the database or not.
|
|
func (bc *BlockChain) HasState(hash common.Hash) bool {
|
|
_, err := bc.stateCache.OpenTrie(hash)
|
|
return err == nil
|
|
}
|
|
|
|
// HasBlockAndState checks if a block and associated state trie is fully present
|
|
// in the database or not, caching it if present.
|
|
func (bc *BlockChain) HasBlockAndState(hash common.Hash, number uint64) bool {
|
|
// Check first that the block itself is known
|
|
block := bc.GetBlock(hash, number)
|
|
if block == nil {
|
|
return false
|
|
}
|
|
return bc.HasState(block.Root())
|
|
}
|
|
|
|
// GetBlock retrieves a block from the database by hash and number,
|
|
// caching it if found.
|
|
func (bc *BlockChain) GetBlock(hash common.Hash, number uint64) *types.Block {
|
|
// Short circuit if the block's already in the cache, retrieve otherwise
|
|
if block, ok := bc.blockCache.Get(hash); ok {
|
|
return block.(*types.Block)
|
|
}
|
|
block := rawdb.ReadBlock(bc.db, hash, number)
|
|
if block == nil {
|
|
return nil
|
|
}
|
|
// Cache the found block for next time and return
|
|
bc.blockCache.Add(block.Hash(), block)
|
|
return block
|
|
}
|
|
|
|
// GetBlockByHash retrieves a block from the database by hash, caching it if found.
|
|
func (bc *BlockChain) GetBlockByHash(hash common.Hash) *types.Block {
|
|
number := bc.hc.GetBlockNumber(hash)
|
|
if number == nil {
|
|
return nil
|
|
}
|
|
return bc.GetBlock(hash, *number)
|
|
}
|
|
|
|
// GetBlockByNumber retrieves a block from the database by number, caching it
|
|
// (associated with its hash) if found.
|
|
func (bc *BlockChain) GetBlockByNumber(number uint64) *types.Block {
|
|
hash := rawdb.ReadCanonicalHash(bc.db, number)
|
|
if hash == (common.Hash{}) {
|
|
return nil
|
|
}
|
|
return bc.GetBlock(hash, number)
|
|
}
|
|
|
|
// GetReceiptsByHash retrieves the receipts for all transactions in a given block.
|
|
func (bc *BlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts {
|
|
if receipts, ok := bc.receiptsCache.Get(hash); ok {
|
|
return receipts.(types.Receipts)
|
|
}
|
|
number := rawdb.ReadHeaderNumber(bc.db, hash)
|
|
if number == nil {
|
|
return nil
|
|
}
|
|
receipts := rawdb.ReadReceipts(bc.db, hash, *number, bc.chainConfig)
|
|
if receipts == nil {
|
|
return nil
|
|
}
|
|
bc.receiptsCache.Add(hash, receipts)
|
|
return receipts
|
|
}
|
|
|
|
// GetBlocksFromHash returns the block corresponding to hash and up to n-1 ancestors.
|
|
// [deprecated by eth/62]
|
|
func (bc *BlockChain) GetBlocksFromHash(hash common.Hash, n int) (blocks []*types.Block) {
|
|
number := bc.hc.GetBlockNumber(hash)
|
|
if number == nil {
|
|
return nil
|
|
}
|
|
for i := 0; i < n; i++ {
|
|
block := bc.GetBlock(hash, *number)
|
|
if block == nil {
|
|
break
|
|
}
|
|
blocks = append(blocks, block)
|
|
hash = block.ParentHash()
|
|
*number--
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetUnclesInChain retrieves all the uncles from a given block backwards until
|
|
// a specific distance is reached.
|
|
func (bc *BlockChain) GetUnclesInChain(block *types.Block, length int) []*types.Header {
|
|
uncles := []*types.Header{}
|
|
for i := 0; block != nil && i < length; i++ {
|
|
uncles = append(uncles, block.Uncles()...)
|
|
block = bc.GetBlock(block.ParentHash(), block.NumberU64()-1)
|
|
}
|
|
return uncles
|
|
}
|
|
|
|
// TrieNode retrieves a blob of data associated with a trie node (or code hash)
|
|
// either from ephemeral in-memory cache, or from persistent storage.
|
|
func (bc *BlockChain) TrieNode(hash common.Hash) ([]byte, error) {
|
|
return bc.stateCache.TrieDB().Node(hash)
|
|
}
|
|
|
|
// Stop stops the blockchain service. If any imports are currently in progress
|
|
// it will abort them using the procInterrupt.
|
|
func (bc *BlockChain) Stop() {
|
|
if !atomic.CompareAndSwapInt32(&bc.running, 0, 1) {
|
|
return
|
|
}
|
|
// Unsubscribe all subscriptions registered from blockchain
|
|
bc.scope.Close()
|
|
close(bc.quit)
|
|
bc.StopInsert()
|
|
bc.wg.Wait()
|
|
|
|
// Ensure that the entirety of the state snapshot is journalled to disk.
|
|
var snapBase common.Hash
|
|
if bc.snaps != nil {
|
|
var err error
|
|
if snapBase, err = bc.snaps.Journal(bc.CurrentBlock().Root()); err != nil {
|
|
log.Error("Failed to journal state snapshot", "err", err)
|
|
}
|
|
}
|
|
// Ensure the state of a recent block is also stored to disk before exiting.
|
|
// We're writing three different states to catch different restart scenarios:
|
|
// - HEAD: So we don't need to reprocess any blocks in the general case
|
|
// - HEAD-1: So we don't do large reorgs if our HEAD becomes an uncle
|
|
// - HEAD-127: So we have a hard limit on the number of blocks reexecuted
|
|
if !bc.cacheConfig.TrieDirtyDisabled {
|
|
triedb := bc.stateCache.TrieDB()
|
|
|
|
for _, offset := range []uint64{0, 1, TriesInMemory - 1} {
|
|
if number := bc.CurrentBlock().NumberU64(); number > offset {
|
|
recent := bc.GetBlockByNumber(number - offset)
|
|
|
|
log.Info("Writing cached state to disk", "block", recent.Number(), "hash", recent.Hash(), "root", recent.Root())
|
|
if err := triedb.Commit(recent.Root(), true); err != nil {
|
|
log.Error("Failed to commit recent state trie", "err", err)
|
|
}
|
|
}
|
|
}
|
|
if snapBase != (common.Hash{}) {
|
|
log.Info("Writing snapshot state to disk", "root", snapBase)
|
|
if err := triedb.Commit(snapBase, true); err != nil {
|
|
log.Error("Failed to commit recent state trie", "err", err)
|
|
}
|
|
}
|
|
for !bc.triegc.Empty() {
|
|
triedb.Dereference(bc.triegc.PopItem().(common.Hash))
|
|
}
|
|
if size, _ := triedb.Size(); size != 0 {
|
|
log.Error("Dangling trie nodes after full cleanup")
|
|
}
|
|
}
|
|
log.Info("Blockchain stopped")
|
|
}
|
|
|
|
// StopInsert interrupts all insertion methods, causing them to return
|
|
// errInsertionInterrupted as soon as possible. Insertion is permanently disabled after
|
|
// calling this method.
|
|
func (bc *BlockChain) StopInsert() {
|
|
atomic.StoreInt32(&bc.procInterrupt, 1)
|
|
}
|
|
|
|
// insertStopped returns true after StopInsert has been called.
|
|
func (bc *BlockChain) insertStopped() bool {
|
|
return atomic.LoadInt32(&bc.procInterrupt) == 1
|
|
}
|
|
|
|
func (bc *BlockChain) procFutureBlocks() {
|
|
blocks := make([]*types.Block, 0, bc.futureBlocks.Len())
|
|
for _, hash := range bc.futureBlocks.Keys() {
|
|
if block, exist := bc.futureBlocks.Peek(hash); exist {
|
|
blocks = append(blocks, block.(*types.Block))
|
|
}
|
|
}
|
|
if len(blocks) > 0 {
|
|
sort.Slice(blocks, func(i, j int) bool {
|
|
return blocks[i].NumberU64() < blocks[j].NumberU64()
|
|
})
|
|
// Insert one by one as chain insertion needs contiguous ancestry between blocks
|
|
for i := range blocks {
|
|
bc.InsertChain(blocks[i : i+1])
|
|
}
|
|
}
|
|
}
|
|
|
|
// WriteStatus status of write
|
|
type WriteStatus byte
|
|
|
|
const (
|
|
NonStatTy WriteStatus = iota
|
|
CanonStatTy
|
|
SideStatTy
|
|
)
|
|
|
|
// Rollback is designed to remove a chain of links from the database that aren't
|
|
// certain enough to be valid.
|
|
func (bc *BlockChain) Rollback(chain []common.Hash) {
|
|
bc.chainmu.Lock()
|
|
defer bc.chainmu.Unlock()
|
|
|
|
batch := bc.db.NewBatch()
|
|
for i := len(chain) - 1; i >= 0; i-- {
|
|
hash := chain[i]
|
|
|
|
// Degrade the chain markers if they are explicitly reverted.
|
|
// In theory we should update all in-memory markers in the
|
|
// last step, however the direction of rollback is from high
|
|
// to low, so it's safe the update in-memory markers directly.
|
|
currentHeader := bc.hc.CurrentHeader()
|
|
if currentHeader.Hash() == hash {
|
|
newHeadHeader := bc.GetHeader(currentHeader.ParentHash, currentHeader.Number.Uint64()-1)
|
|
rawdb.WriteHeadHeaderHash(batch, currentHeader.ParentHash)
|
|
bc.hc.SetCurrentHeader(newHeadHeader)
|
|
}
|
|
if currentFastBlock := bc.CurrentFastBlock(); currentFastBlock.Hash() == hash {
|
|
newFastBlock := bc.GetBlock(currentFastBlock.ParentHash(), currentFastBlock.NumberU64()-1)
|
|
rawdb.WriteHeadFastBlockHash(batch, currentFastBlock.ParentHash())
|
|
bc.currentFastBlock.Store(newFastBlock)
|
|
headFastBlockGauge.Update(int64(newFastBlock.NumberU64()))
|
|
}
|
|
if currentBlock := bc.CurrentBlock(); currentBlock.Hash() == hash {
|
|
newBlock := bc.GetBlock(currentBlock.ParentHash(), currentBlock.NumberU64()-1)
|
|
rawdb.WriteHeadBlockHash(batch, currentBlock.ParentHash())
|
|
bc.currentBlock.Store(newBlock)
|
|
headBlockGauge.Update(int64(newBlock.NumberU64()))
|
|
}
|
|
}
|
|
if err := batch.Write(); err != nil {
|
|
log.Crit("Failed to rollback chain markers", "err", err)
|
|
}
|
|
// Truncate ancient data which exceeds the current header.
|
|
//
|
|
// Notably, it can happen that system crashes without truncating the ancient data
|
|
// but the head indicator has been updated in the active store. Regarding this issue,
|
|
// system will self recovery by truncating the extra data during the setup phase.
|
|
if err := bc.truncateAncient(bc.hc.CurrentHeader().Number.Uint64()); err != nil {
|
|
log.Crit("Truncate ancient store failed", "err", err)
|
|
}
|
|
}
|
|
|
|
// truncateAncient rewinds the blockchain to the specified header and deletes all
|
|
// data in the ancient store that exceeds the specified header.
|
|
func (bc *BlockChain) truncateAncient(head uint64) error {
|
|
frozen, err := bc.db.Ancients()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Short circuit if there is no data to truncate in ancient store.
|
|
if frozen <= head+1 {
|
|
return nil
|
|
}
|
|
// Truncate all the data in the freezer beyond the specified head
|
|
if err := bc.db.TruncateAncients(head + 1); err != nil {
|
|
return err
|
|
}
|
|
// Clear out any stale content from the caches
|
|
bc.hc.headerCache.Purge()
|
|
bc.hc.tdCache.Purge()
|
|
bc.hc.numberCache.Purge()
|
|
|
|
// Clear out any stale content from the caches
|
|
bc.bodyCache.Purge()
|
|
bc.bodyRLPCache.Purge()
|
|
bc.receiptsCache.Purge()
|
|
bc.blockCache.Purge()
|
|
bc.txLookupCache.Purge()
|
|
bc.futureBlocks.Purge()
|
|
|
|
log.Info("Rewind ancient data", "number", head)
|
|
return nil
|
|
}
|
|
|
|
// numberHash is just a container for a number and a hash, to represent a block
|
|
type numberHash struct {
|
|
number uint64
|
|
hash common.Hash
|
|
}
|
|
|
|
// InsertReceiptChain attempts to complete an already existing header chain with
|
|
// transaction and receipt data.
|
|
func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain []types.Receipts, ancientLimit uint64) (int, error) {
|
|
// We don't require the chainMu here since we want to maximize the
|
|
// concurrency of header insertion and receipt insertion.
|
|
bc.wg.Add(1)
|
|
defer bc.wg.Done()
|
|
|
|
var (
|
|
ancientBlocks, liveBlocks types.Blocks
|
|
ancientReceipts, liveReceipts []types.Receipts
|
|
)
|
|
// Do a sanity check that the provided chain is actually ordered and linked
|
|
for i := 0; i < len(blockChain); i++ {
|
|
if i != 0 {
|
|
if blockChain[i].NumberU64() != blockChain[i-1].NumberU64()+1 || blockChain[i].ParentHash() != blockChain[i-1].Hash() {
|
|
log.Error("Non contiguous receipt insert", "number", blockChain[i].Number(), "hash", blockChain[i].Hash(), "parent", blockChain[i].ParentHash(),
|
|
"prevnumber", blockChain[i-1].Number(), "prevhash", blockChain[i-1].Hash())
|
|
return 0, fmt.Errorf("non contiguous insert: item %d is #%d [%x…], item %d is #%d [%x…] (parent [%x…])", i-1, blockChain[i-1].NumberU64(),
|
|
blockChain[i-1].Hash().Bytes()[:4], i, blockChain[i].NumberU64(), blockChain[i].Hash().Bytes()[:4], blockChain[i].ParentHash().Bytes()[:4])
|
|
}
|
|
}
|
|
if blockChain[i].NumberU64() <= ancientLimit {
|
|
ancientBlocks, ancientReceipts = append(ancientBlocks, blockChain[i]), append(ancientReceipts, receiptChain[i])
|
|
} else {
|
|
liveBlocks, liveReceipts = append(liveBlocks, blockChain[i]), append(liveReceipts, receiptChain[i])
|
|
}
|
|
}
|
|
|
|
var (
|
|
stats = struct{ processed, ignored int32 }{}
|
|
start = time.Now()
|
|
size = 0
|
|
)
|
|
// updateHead updates the head fast sync block if the inserted blocks are better
|
|
// and returns an indicator whether the inserted blocks are canonical.
|
|
updateHead := func(head *types.Block) bool {
|
|
bc.chainmu.Lock()
|
|
|
|
// Rewind may have occurred, skip in that case.
|
|
if bc.CurrentHeader().Number.Cmp(head.Number()) >= 0 {
|
|
currentFastBlock, td := bc.CurrentFastBlock(), bc.GetTd(head.Hash(), head.NumberU64())
|
|
if bc.GetTd(currentFastBlock.Hash(), currentFastBlock.NumberU64()).Cmp(td) < 0 {
|
|
rawdb.WriteHeadFastBlockHash(bc.db, head.Hash())
|
|
bc.currentFastBlock.Store(head)
|
|
headFastBlockGauge.Update(int64(head.NumberU64()))
|
|
bc.chainmu.Unlock()
|
|
return true
|
|
}
|
|
}
|
|
bc.chainmu.Unlock()
|
|
return false
|
|
}
|
|
// writeAncient writes blockchain and corresponding receipt chain into ancient store.
|
|
//
|
|
// this function only accepts canonical chain data. All side chain will be reverted
|
|
// eventually.
|
|
writeAncient := func(blockChain types.Blocks, receiptChain []types.Receipts) (int, error) {
|
|
var (
|
|
previous = bc.CurrentFastBlock()
|
|
batch = bc.db.NewBatch()
|
|
)
|
|
// If any error occurs before updating the head or we are inserting a side chain,
|
|
// all the data written this time wll be rolled back.
|
|
defer func() {
|
|
if previous != nil {
|
|
if err := bc.truncateAncient(previous.NumberU64()); err != nil {
|
|
log.Crit("Truncate ancient store failed", "err", err)
|
|
}
|
|
}
|
|
}()
|
|
var deleted []*numberHash
|
|
for i, block := range blockChain {
|
|
// Short circuit insertion if shutting down or processing failed
|
|
if bc.insertStopped() {
|
|
return 0, errInsertionInterrupted
|
|
}
|
|
// Short circuit insertion if it is required(used in testing only)
|
|
if bc.terminateInsert != nil && bc.terminateInsert(block.Hash(), block.NumberU64()) {
|
|
return i, errors.New("insertion is terminated for testing purpose")
|
|
}
|
|
// Short circuit if the owner header is unknown
|
|
if !bc.HasHeader(block.Hash(), block.NumberU64()) {
|
|
return i, fmt.Errorf("containing header #%d [%x…] unknown", block.Number(), block.Hash().Bytes()[:4])
|
|
}
|
|
var (
|
|
start = time.Now()
|
|
logged = time.Now()
|
|
count int
|
|
)
|
|
// Migrate all ancient blocks. This can happen if someone upgrades from Geth
|
|
// 1.8.x to 1.9.x mid-fast-sync. Perhaps we can get rid of this path in the
|
|
// long term.
|
|
for {
|
|
// We can ignore the error here since light client won't hit this code path.
|
|
frozen, _ := bc.db.Ancients()
|
|
if frozen >= block.NumberU64() {
|
|
break
|
|
}
|
|
h := rawdb.ReadCanonicalHash(bc.db, frozen)
|
|
b := rawdb.ReadBlock(bc.db, h, frozen)
|
|
size += rawdb.WriteAncientBlock(bc.db, b, rawdb.ReadReceipts(bc.db, h, frozen, bc.chainConfig), rawdb.ReadTd(bc.db, h, frozen))
|
|
count += 1
|
|
|
|
// Always keep genesis block in active database.
|
|
if b.NumberU64() != 0 {
|
|
deleted = append(deleted, &numberHash{b.NumberU64(), b.Hash()})
|
|
}
|
|
if time.Since(logged) > 8*time.Second {
|
|
log.Info("Migrating ancient blocks", "count", count, "elapsed", common.PrettyDuration(time.Since(start)))
|
|
logged = time.Now()
|
|
}
|
|
// Don't collect too much in-memory, write it out every 100K blocks
|
|
if len(deleted) > 100000 {
|
|
// Sync the ancient store explicitly to ensure all data has been flushed to disk.
|
|
if err := bc.db.Sync(); err != nil {
|
|
return 0, err
|
|
}
|
|
// Wipe out canonical block data.
|
|
for _, nh := range deleted {
|
|
rawdb.DeleteBlockWithoutNumber(batch, nh.hash, nh.number)
|
|
rawdb.DeleteCanonicalHash(batch, nh.number)
|
|
}
|
|
if err := batch.Write(); err != nil {
|
|
return 0, err
|
|
}
|
|
batch.Reset()
|
|
// Wipe out side chain too.
|
|
for _, nh := range deleted {
|
|
for _, hash := range rawdb.ReadAllHashes(bc.db, nh.number) {
|
|
rawdb.DeleteBlock(batch, hash, nh.number)
|
|
}
|
|
}
|
|
if err := batch.Write(); err != nil {
|
|
return 0, err
|
|
}
|
|
batch.Reset()
|
|
deleted = deleted[0:]
|
|
}
|
|
}
|
|
if count > 0 {
|
|
log.Info("Migrated ancient blocks", "count", count, "elapsed", common.PrettyDuration(time.Since(start)))
|
|
}
|
|
// Flush data into ancient database.
|
|
size += rawdb.WriteAncientBlock(bc.db, block, receiptChain[i], bc.GetTd(block.Hash(), block.NumberU64()))
|
|
|
|
// Write tx indices if any condition is satisfied:
|
|
// * If user requires to reserve all tx indices(txlookuplimit=0)
|
|
// * If all ancient tx indices are required to be reserved(txlookuplimit is even higher than ancientlimit)
|
|
// * If block number is large enough to be regarded as a recent block
|
|
// It means blocks below the ancientLimit-txlookupLimit won't be indexed.
|
|
//
|
|
// But if the `TxIndexTail` is not nil, e.g. Geth is initialized with
|
|
// an external ancient database, during the setup, blockchain will start
|
|
// a background routine to re-indexed all indices in [ancients - txlookupLimit, ancients)
|
|
// range. In this case, all tx indices of newly imported blocks should be
|
|
// generated.
|
|
if bc.txLookupLimit == 0 || ancientLimit <= bc.txLookupLimit || block.NumberU64() >= ancientLimit-bc.txLookupLimit {
|
|
rawdb.WriteTxLookupEntries(batch, block)
|
|
} else if rawdb.ReadTxIndexTail(bc.db) != nil {
|
|
rawdb.WriteTxLookupEntries(batch, block)
|
|
}
|
|
stats.processed++
|
|
}
|
|
// Flush all tx-lookup index data.
|
|
size += batch.ValueSize()
|
|
if err := batch.Write(); err != nil {
|
|
return 0, err
|
|
}
|
|
batch.Reset()
|
|
|
|
// Sync the ancient store explicitly to ensure all data has been flushed to disk.
|
|
if err := bc.db.Sync(); err != nil {
|
|
return 0, err
|
|
}
|
|
if !updateHead(blockChain[len(blockChain)-1]) {
|
|
return 0, errors.New("side blocks can't be accepted as the ancient chain data")
|
|
}
|
|
previous = nil // disable rollback explicitly
|
|
|
|
// Wipe out canonical block data.
|
|
for _, nh := range deleted {
|
|
rawdb.DeleteBlockWithoutNumber(batch, nh.hash, nh.number)
|
|
rawdb.DeleteCanonicalHash(batch, nh.number)
|
|
}
|
|
for _, block := range blockChain {
|
|
// Always keep genesis block in active database.
|
|
if block.NumberU64() != 0 {
|
|
rawdb.DeleteBlockWithoutNumber(batch, block.Hash(), block.NumberU64())
|
|
rawdb.DeleteCanonicalHash(batch, block.NumberU64())
|
|
}
|
|
}
|
|
if err := batch.Write(); err != nil {
|
|
return 0, err
|
|
}
|
|
batch.Reset()
|
|
|
|
// Wipe out side chain too.
|
|
for _, nh := range deleted {
|
|
for _, hash := range rawdb.ReadAllHashes(bc.db, nh.number) {
|
|
rawdb.DeleteBlock(batch, hash, nh.number)
|
|
}
|
|
}
|
|
for _, block := range blockChain {
|
|
// Always keep genesis block in active database.
|
|
if block.NumberU64() != 0 {
|
|
for _, hash := range rawdb.ReadAllHashes(bc.db, block.NumberU64()) {
|
|
rawdb.DeleteBlock(batch, hash, block.NumberU64())
|
|
}
|
|
}
|
|
}
|
|
if err := batch.Write(); err != nil {
|
|
return 0, err
|
|
}
|
|
return 0, nil
|
|
}
|
|
// writeLive writes blockchain and corresponding receipt chain into active store.
|
|
writeLive := func(blockChain types.Blocks, receiptChain []types.Receipts) (int, error) {
|
|
batch := bc.db.NewBatch()
|
|
for i, block := range blockChain {
|
|
// Short circuit insertion if shutting down or processing failed
|
|
if bc.insertStopped() {
|
|
return 0, errInsertionInterrupted
|
|
}
|
|
// Short circuit if the owner header is unknown
|
|
if !bc.HasHeader(block.Hash(), block.NumberU64()) {
|
|
return i, fmt.Errorf("containing header #%d [%x…] unknown", block.Number(), block.Hash().Bytes()[:4])
|
|
}
|
|
if bc.HasBlock(block.Hash(), block.NumberU64()) {
|
|
stats.ignored++
|
|
continue
|
|
}
|
|
// Write all the data out into the database
|
|
rawdb.WriteBody(batch, block.Hash(), block.NumberU64(), block.Body())
|
|
rawdb.WriteReceipts(batch, block.Hash(), block.NumberU64(), receiptChain[i])
|
|
rawdb.WriteTxLookupEntries(batch, block) // Always write tx indices for live blocks, we assume they are needed
|
|
|
|
// Write everything belongs to the blocks into the database. So that
|
|
// we can ensure all components of body is completed(body, receipts,
|
|
// tx indexes)
|
|
if batch.ValueSize() >= ethdb.IdealBatchSize {
|
|
if err := batch.Write(); err != nil {
|
|
return 0, err
|
|
}
|
|
size += batch.ValueSize()
|
|
batch.Reset()
|
|
}
|
|
stats.processed++
|
|
}
|
|
// Write everything belongs to the blocks into the database. So that
|
|
// we can ensure all components of body is completed(body, receipts,
|
|
// tx indexes)
|
|
if batch.ValueSize() > 0 {
|
|
size += batch.ValueSize()
|
|
if err := batch.Write(); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
updateHead(blockChain[len(blockChain)-1])
|
|
return 0, nil
|
|
}
|
|
// Write downloaded chain data and corresponding receipt chain data
|
|
if len(ancientBlocks) > 0 {
|
|
if n, err := writeAncient(ancientBlocks, ancientReceipts); err != nil {
|
|
if err == errInsertionInterrupted {
|
|
return 0, nil
|
|
}
|
|
return n, err
|
|
}
|
|
}
|
|
// Write the tx index tail (block number from where we index) before write any live blocks
|
|
if len(liveBlocks) > 0 && liveBlocks[0].NumberU64() == ancientLimit+1 {
|
|
// The tx index tail can only be one of the following two options:
|
|
// * 0: all ancient blocks have been indexed
|
|
// * ancient-limit: the indices of blocks before ancient-limit are ignored
|
|
if tail := rawdb.ReadTxIndexTail(bc.db); tail == nil {
|
|
if bc.txLookupLimit == 0 || ancientLimit <= bc.txLookupLimit {
|
|
rawdb.WriteTxIndexTail(bc.db, 0)
|
|
} else {
|
|
rawdb.WriteTxIndexTail(bc.db, ancientLimit-bc.txLookupLimit)
|
|
}
|
|
}
|
|
}
|
|
if len(liveBlocks) > 0 {
|
|
if n, err := writeLive(liveBlocks, liveReceipts); err != nil {
|
|
if err == errInsertionInterrupted {
|
|
return 0, nil
|
|
}
|
|
return n, err
|
|
}
|
|
}
|
|
|
|
head := blockChain[len(blockChain)-1]
|
|
context := []interface{}{
|
|
"count", stats.processed, "elapsed", common.PrettyDuration(time.Since(start)),
|
|
"number", head.Number(), "hash", head.Hash(), "age", common.PrettyAge(time.Unix(int64(head.Time()), 0)),
|
|
"size", common.StorageSize(size),
|
|
}
|
|
if stats.ignored > 0 {
|
|
context = append(context, []interface{}{"ignored", stats.ignored}...)
|
|
}
|
|
log.Info("Imported new block receipts", context...)
|
|
|
|
return 0, nil
|
|
}
|
|
|
|
// SetTxLookupLimit is responsible for updating the txlookup limit to the
|
|
// original one stored in db if the new mismatches with the old one.
|
|
func (bc *BlockChain) SetTxLookupLimit(limit uint64) {
|
|
bc.txLookupLimit = limit
|
|
}
|
|
|
|
// TxLookupLimit retrieves the txlookup limit used by blockchain to prune
|
|
// stale transaction indices.
|
|
func (bc *BlockChain) TxLookupLimit() uint64 {
|
|
return bc.txLookupLimit
|
|
}
|
|
|
|
var lastWrite uint64
|
|
|
|
// writeBlockWithoutState writes only the block and its metadata to the database,
|
|
// but does not write any state. This is used to construct competing side forks
|
|
// up to the point where they exceed the canonical total difficulty.
|
|
func (bc *BlockChain) writeBlockWithoutState(block *types.Block, td *big.Int) (err error) {
|
|
bc.wg.Add(1)
|
|
defer bc.wg.Done()
|
|
|
|
batch := bc.db.NewBatch()
|
|
rawdb.WriteTd(batch, block.Hash(), block.NumberU64(), td)
|
|
rawdb.WriteBlock(batch, block)
|
|
if err := batch.Write(); err != nil {
|
|
log.Crit("Failed to write block into disk", "err", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// writeKnownBlock updates the head block flag with a known block
|
|
// and introduces chain reorg if necessary.
|
|
func (bc *BlockChain) writeKnownBlock(block *types.Block) error {
|
|
bc.wg.Add(1)
|
|
defer bc.wg.Done()
|
|
|
|
current := bc.CurrentBlock()
|
|
if block.ParentHash() != current.Hash() {
|
|
if err := bc.reorg(current, block); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
bc.writeHeadBlock(block)
|
|
return nil
|
|
}
|
|
|
|
// WriteBlockWithState writes the block and all associated state to the database.
|
|
func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) {
|
|
bc.chainmu.Lock()
|
|
defer bc.chainmu.Unlock()
|
|
|
|
return bc.writeBlockWithState(block, receipts, logs, state, emitHeadEvent)
|
|
}
|
|
|
|
// writeBlockWithState writes the block and all associated state to the database,
|
|
// but is expects the chain mutex to be held.
|
|
func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) {
|
|
bc.wg.Add(1)
|
|
defer bc.wg.Done()
|
|
|
|
// Calculate the total difficulty of the block
|
|
ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1)
|
|
if ptd == nil {
|
|
return NonStatTy, consensus.ErrUnknownAncestor
|
|
}
|
|
// Make sure no inconsistent state is leaked during insertion
|
|
currentBlock := bc.CurrentBlock()
|
|
localTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64())
|
|
externTd := new(big.Int).Add(block.Difficulty(), ptd)
|
|
|
|
// Irrelevant of the canonical status, write the block itself to the database.
|
|
//
|
|
// Note all the components of block(td, hash->number map, header, body, receipts)
|
|
// should be written atomically. BlockBatch is used for containing all components.
|
|
blockBatch := bc.db.NewBatch()
|
|
rawdb.WriteTd(blockBatch, block.Hash(), block.NumberU64(), externTd)
|
|
rawdb.WriteBlock(blockBatch, block)
|
|
rawdb.WriteReceipts(blockBatch, block.Hash(), block.NumberU64(), receipts)
|
|
rawdb.WritePreimages(blockBatch, state.Preimages())
|
|
if err := blockBatch.Write(); err != nil {
|
|
log.Crit("Failed to write block into disk", "err", err)
|
|
}
|
|
// Commit all cached state changes into underlying memory database.
|
|
root, err := state.Commit(bc.chainConfig.IsEIP158(block.Number()))
|
|
if err != nil {
|
|
return NonStatTy, err
|
|
}
|
|
triedb := bc.stateCache.TrieDB()
|
|
|
|
// If we're running an archive node, always flush
|
|
if bc.cacheConfig.TrieDirtyDisabled {
|
|
if err := triedb.Commit(root, false); err != nil {
|
|
return NonStatTy, err
|
|
}
|
|
} else {
|
|
// Full but not archive node, do proper garbage collection
|
|
triedb.Reference(root, common.Hash{}) // metadata reference to keep trie alive
|
|
bc.triegc.Push(root, -int64(block.NumberU64()))
|
|
|
|
if current := block.NumberU64(); current > TriesInMemory {
|
|
// If we exceeded our memory allowance, flush matured singleton nodes to disk
|
|
var (
|
|
nodes, imgs = triedb.Size()
|
|
limit = common.StorageSize(bc.cacheConfig.TrieDirtyLimit) * 1024 * 1024
|
|
)
|
|
if nodes > limit || imgs > 4*1024*1024 {
|
|
triedb.Cap(limit - ethdb.IdealBatchSize)
|
|
}
|
|
// Find the next state trie we need to commit
|
|
chosen := current - TriesInMemory
|
|
|
|
// If we exceeded out time allowance, flush an entire trie to disk
|
|
if bc.gcproc > bc.cacheConfig.TrieTimeLimit {
|
|
// If the header is missing (canonical chain behind), we're reorging a low
|
|
// diff sidechain. Suspend committing until this operation is completed.
|
|
header := bc.GetHeaderByNumber(chosen)
|
|
if header == nil {
|
|
log.Warn("Reorg in progress, trie commit postponed", "number", chosen)
|
|
} else {
|
|
// If we're exceeding limits but haven't reached a large enough memory gap,
|
|
// warn the user that the system is becoming unstable.
|
|
if chosen < lastWrite+TriesInMemory && bc.gcproc >= 2*bc.cacheConfig.TrieTimeLimit {
|
|
log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", bc.cacheConfig.TrieTimeLimit, "optimum", float64(chosen-lastWrite)/TriesInMemory)
|
|
}
|
|
// Flush an entire trie and restart the counters
|
|
triedb.Commit(header.Root, true)
|
|
lastWrite = chosen
|
|
bc.gcproc = 0
|
|
}
|
|
}
|
|
// Garbage collect anything below our required write retention
|
|
for !bc.triegc.Empty() {
|
|
root, number := bc.triegc.Pop()
|
|
if uint64(-number) > chosen {
|
|
bc.triegc.Push(root, number)
|
|
break
|
|
}
|
|
triedb.Dereference(root.(common.Hash))
|
|
}
|
|
}
|
|
}
|
|
// If the total difficulty is higher than our known, add it to the canonical chain
|
|
// Second clause in the if statement reduces the vulnerability to selfish mining.
|
|
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
|
|
reorg := externTd.Cmp(localTd) > 0
|
|
currentBlock = bc.CurrentBlock()
|
|
if !reorg && externTd.Cmp(localTd) == 0 {
|
|
// Split same-difficulty blocks by number, then preferentially select
|
|
// the block generated by the local miner as the canonical block.
|
|
if block.NumberU64() < currentBlock.NumberU64() {
|
|
reorg = true
|
|
} else if block.NumberU64() == currentBlock.NumberU64() {
|
|
var currentPreserve, blockPreserve bool
|
|
if bc.shouldPreserve != nil {
|
|
currentPreserve, blockPreserve = bc.shouldPreserve(currentBlock), bc.shouldPreserve(block)
|
|
}
|
|
reorg = !currentPreserve && (blockPreserve || mrand.Float64() < 0.5)
|
|
}
|
|
}
|
|
if reorg {
|
|
// Reorganise the chain if the parent is not the head block
|
|
if block.ParentHash() != currentBlock.Hash() {
|
|
if err := bc.reorg(currentBlock, block); err != nil {
|
|
return NonStatTy, err
|
|
}
|
|
}
|
|
status = CanonStatTy
|
|
} else {
|
|
status = SideStatTy
|
|
}
|
|
// Set new head.
|
|
if status == CanonStatTy {
|
|
bc.writeHeadBlock(block)
|
|
}
|
|
bc.futureBlocks.Remove(block.Hash())
|
|
|
|
if status == CanonStatTy {
|
|
bc.chainFeed.Send(ChainEvent{Block: block, Hash: block.Hash(), Logs: logs})
|
|
if len(logs) > 0 {
|
|
bc.logsFeed.Send(logs)
|
|
}
|
|
// In theory we should fire a ChainHeadEvent when we inject
|
|
// a canonical block, but sometimes we can insert a batch of
|
|
// canonicial blocks. Avoid firing too much ChainHeadEvents,
|
|
// we will fire an accumulated ChainHeadEvent and disable fire
|
|
// event here.
|
|
if emitHeadEvent {
|
|
bc.chainHeadFeed.Send(ChainHeadEvent{Block: block})
|
|
}
|
|
} else {
|
|
bc.chainSideFeed.Send(ChainSideEvent{Block: block})
|
|
}
|
|
return status, nil
|
|
}
|
|
|
|
// addFutureBlock checks if the block is within the max allowed window to get
|
|
// accepted for future processing, and returns an error if the block is too far
|
|
// ahead and was not added.
|
|
func (bc *BlockChain) addFutureBlock(block *types.Block) error {
|
|
max := uint64(time.Now().Unix() + maxTimeFutureBlocks)
|
|
if block.Time() > max {
|
|
return fmt.Errorf("future block timestamp %v > allowed %v", block.Time(), max)
|
|
}
|
|
bc.futureBlocks.Add(block.Hash(), block)
|
|
return nil
|
|
}
|
|
|
|
// InsertChain attempts to insert the given batch of blocks in to the canonical
|
|
// chain or, otherwise, create a fork. If an error is returned it will return
|
|
// the index number of the failing block as well an error describing what went
|
|
// wrong.
|
|
//
|
|
// After insertion is done, all accumulated events will be fired.
|
|
func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) {
|
|
// Sanity check that we have something meaningful to import
|
|
if len(chain) == 0 {
|
|
return 0, nil
|
|
}
|
|
|
|
bc.blockProcFeed.Send(true)
|
|
defer bc.blockProcFeed.Send(false)
|
|
|
|
// Remove already known canon-blocks
|
|
var (
|
|
block, prev *types.Block
|
|
)
|
|
// Do a sanity check that the provided chain is actually ordered and linked
|
|
for i := 1; i < len(chain); i++ {
|
|
block = chain[i]
|
|
prev = chain[i-1]
|
|
if block.NumberU64() != prev.NumberU64()+1 || block.ParentHash() != prev.Hash() {
|
|
// Chain broke ancestry, log a message (programming error) and skip insertion
|
|
log.Error("Non contiguous block insert", "number", block.Number(), "hash", block.Hash(),
|
|
"parent", block.ParentHash(), "prevnumber", prev.Number(), "prevhash", prev.Hash())
|
|
|
|
return 0, fmt.Errorf("non contiguous insert: item %d is #%d [%x…], item %d is #%d [%x…] (parent [%x…])", i-1, prev.NumberU64(),
|
|
prev.Hash().Bytes()[:4], i, block.NumberU64(), block.Hash().Bytes()[:4], block.ParentHash().Bytes()[:4])
|
|
}
|
|
}
|
|
// Pre-checks passed, start the full block imports
|
|
bc.wg.Add(1)
|
|
bc.chainmu.Lock()
|
|
n, err := bc.insertChain(chain, true)
|
|
bc.chainmu.Unlock()
|
|
bc.wg.Done()
|
|
|
|
return n, err
|
|
}
|
|
|
|
// insertChain is the internal implementation of InsertChain, which assumes that
|
|
// 1) chains are contiguous, and 2) The chain mutex is held.
|
|
//
|
|
// This method is split out so that import batches that require re-injecting
|
|
// historical blocks can do so without releasing the lock, which could lead to
|
|
// racey behaviour. If a sidechain import is in progress, and the historic state
|
|
// is imported, but then new canon-head is added before the actual sidechain
|
|
// completes, then the historic state could be pruned again
|
|
func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, error) {
|
|
// If the chain is terminating, don't even bother starting up
|
|
if atomic.LoadInt32(&bc.procInterrupt) == 1 {
|
|
return 0, nil
|
|
}
|
|
// Start a parallel signature recovery (signer will fluke on fork transition, minimal perf loss)
|
|
senderCacher.recoverFromBlocks(types.MakeSigner(bc.chainConfig, chain[0].Number()), chain)
|
|
|
|
var (
|
|
stats = insertStats{startTime: mclock.Now()}
|
|
lastCanon *types.Block
|
|
)
|
|
// Fire a single chain head event if we've progressed the chain
|
|
defer func() {
|
|
if lastCanon != nil && bc.CurrentBlock().Hash() == lastCanon.Hash() {
|
|
bc.chainHeadFeed.Send(ChainHeadEvent{lastCanon})
|
|
}
|
|
}()
|
|
// Start the parallel header verifier
|
|
headers := make([]*types.Header, len(chain))
|
|
seals := make([]bool, len(chain))
|
|
|
|
for i, block := range chain {
|
|
headers[i] = block.Header()
|
|
seals[i] = verifySeals
|
|
}
|
|
abort, results := bc.engine.VerifyHeaders(bc, headers, seals)
|
|
defer close(abort)
|
|
|
|
// Peek the error for the first block to decide the directing import logic
|
|
it := newInsertIterator(chain, results, bc.validator)
|
|
|
|
block, err := it.next()
|
|
|
|
// Left-trim all the known blocks
|
|
if err == ErrKnownBlock {
|
|
// First block (and state) is known
|
|
// 1. We did a roll-back, and should now do a re-import
|
|
// 2. The block is stored as a sidechain, and is lying about it's stateroot, and passes a stateroot
|
|
// from the canonical chain, which has not been verified.
|
|
// Skip all known blocks that are behind us
|
|
var (
|
|
current = bc.CurrentBlock()
|
|
localTd = bc.GetTd(current.Hash(), current.NumberU64())
|
|
externTd = bc.GetTd(block.ParentHash(), block.NumberU64()-1) // The first block can't be nil
|
|
)
|
|
for block != nil && err == ErrKnownBlock {
|
|
externTd = new(big.Int).Add(externTd, block.Difficulty())
|
|
if localTd.Cmp(externTd) < 0 {
|
|
break
|
|
}
|
|
log.Debug("Ignoring already known block", "number", block.Number(), "hash", block.Hash())
|
|
stats.ignored++
|
|
|
|
block, err = it.next()
|
|
}
|
|
// The remaining blocks are still known blocks, the only scenario here is:
|
|
// During the fast sync, the pivot point is already submitted but rollback
|
|
// happens. Then node resets the head full block to a lower height via `rollback`
|
|
// and leaves a few known blocks in the database.
|
|
//
|
|
// When node runs a fast sync again, it can re-import a batch of known blocks via
|
|
// `insertChain` while a part of them have higher total difficulty than current
|
|
// head full block(new pivot point).
|
|
for block != nil && err == ErrKnownBlock {
|
|
log.Debug("Writing previously known block", "number", block.Number(), "hash", block.Hash())
|
|
if err := bc.writeKnownBlock(block); err != nil {
|
|
return it.index, err
|
|
}
|
|
lastCanon = block
|
|
|
|
block, err = it.next()
|
|
}
|
|
// Falls through to the block import
|
|
}
|
|
switch {
|
|
// First block is pruned, insert as sidechain and reorg only if TD grows enough
|
|
case err == consensus.ErrPrunedAncestor:
|
|
log.Debug("Pruned ancestor, inserting as sidechain", "number", block.Number(), "hash", block.Hash())
|
|
return bc.insertSideChain(block, it)
|
|
|
|
// First block is future, shove it (and all children) to the future queue (unknown ancestor)
|
|
case err == consensus.ErrFutureBlock || (err == consensus.ErrUnknownAncestor && bc.futureBlocks.Contains(it.first().ParentHash())):
|
|
for block != nil && (it.index == 0 || err == consensus.ErrUnknownAncestor) {
|
|
log.Debug("Future block, postponing import", "number", block.Number(), "hash", block.Hash())
|
|
if err := bc.addFutureBlock(block); err != nil {
|
|
return it.index, err
|
|
}
|
|
block, err = it.next()
|
|
}
|
|
stats.queued += it.processed()
|
|
stats.ignored += it.remaining()
|
|
|
|
// If there are any still remaining, mark as ignored
|
|
return it.index, err
|
|
|
|
// Some other error occurred, abort
|
|
case err != nil:
|
|
bc.futureBlocks.Remove(block.Hash())
|
|
stats.ignored += len(it.chain)
|
|
bc.reportBlock(block, nil, err)
|
|
return it.index, err
|
|
}
|
|
// No validation errors for the first block (or chain prefix skipped)
|
|
for ; block != nil && err == nil || err == ErrKnownBlock; block, err = it.next() {
|
|
// If the chain is terminating, stop processing blocks
|
|
if bc.insertStopped() {
|
|
log.Debug("Abort during block processing")
|
|
break
|
|
}
|
|
// If the header is a banned one, straight out abort
|
|
if BadHashes[block.Hash()] {
|
|
bc.reportBlock(block, nil, ErrBlacklistedHash)
|
|
return it.index, ErrBlacklistedHash
|
|
}
|
|
// If the block is known (in the middle of the chain), it's a special case for
|
|
// Clique blocks where they can share state among each other, so importing an
|
|
// older block might complete the state of the subsequent one. In this case,
|
|
// just skip the block (we already validated it once fully (and crashed), since
|
|
// its header and body was already in the database).
|
|
if err == ErrKnownBlock {
|
|
logger := log.Debug
|
|
if bc.chainConfig.Clique == nil {
|
|
logger = log.Warn
|
|
}
|
|
logger("Inserted known block", "number", block.Number(), "hash", block.Hash(),
|
|
"uncles", len(block.Uncles()), "txs", len(block.Transactions()), "gas", block.GasUsed(),
|
|
"root", block.Root())
|
|
|
|
// Special case. Commit the empty receipt slice if we meet the known
|
|
// block in the middle. It can only happen in the clique chain. Whenever
|
|
// we insert blocks via `insertSideChain`, we only commit `td`, `header`
|
|
// and `body` if it's non-existent. Since we don't have receipts without
|
|
// reexecution, so nothing to commit. But if the sidechain will be adpoted
|
|
// as the canonical chain eventually, it needs to be reexecuted for missing
|
|
// state, but if it's this special case here(skip reexecution) we will lose
|
|
// the empty receipt entry.
|
|
if len(block.Transactions()) == 0 {
|
|
rawdb.WriteReceipts(bc.db, block.Hash(), block.NumberU64(), nil)
|
|
} else {
|
|
log.Error("Please file an issue, skip known block execution without receipt",
|
|
"hash", block.Hash(), "number", block.NumberU64())
|
|
}
|
|
if err := bc.writeKnownBlock(block); err != nil {
|
|
return it.index, err
|
|
}
|
|
stats.processed++
|
|
|
|
// We can assume that logs are empty here, since the only way for consecutive
|
|
// Clique blocks to have the same state is if there are no transactions.
|
|
lastCanon = block
|
|
continue
|
|
}
|
|
// Retrieve the parent block and it's state to execute on top
|
|
start := time.Now()
|
|
|
|
parent := it.previous()
|
|
if parent == nil {
|
|
parent = bc.GetHeader(block.ParentHash(), block.NumberU64()-1)
|
|
}
|
|
statedb, err := state.New(parent.Root, bc.stateCache, bc.snaps)
|
|
if err != nil {
|
|
return it.index, err
|
|
}
|
|
// If we have a followup block, run that against the current state to pre-cache
|
|
// transactions and probabilistically some of the account/storage trie nodes.
|
|
var followupInterrupt uint32
|
|
if !bc.cacheConfig.TrieCleanNoPrefetch {
|
|
if followup, err := it.peek(); followup != nil && err == nil {
|
|
throwaway, _ := state.New(parent.Root, bc.stateCache, bc.snaps)
|
|
go func(start time.Time, followup *types.Block, throwaway *state.StateDB, interrupt *uint32) {
|
|
bc.prefetcher.Prefetch(followup, throwaway, bc.vmConfig, &followupInterrupt)
|
|
|
|
blockPrefetchExecuteTimer.Update(time.Since(start))
|
|
if atomic.LoadUint32(interrupt) == 1 {
|
|
blockPrefetchInterruptMeter.Mark(1)
|
|
}
|
|
}(time.Now(), followup, throwaway, &followupInterrupt)
|
|
}
|
|
}
|
|
// Process block using the parent state as reference point
|
|
substart := time.Now()
|
|
receipts, logs, usedGas, err := bc.processor.Process(block, statedb, bc.vmConfig)
|
|
if err != nil {
|
|
bc.reportBlock(block, receipts, err)
|
|
atomic.StoreUint32(&followupInterrupt, 1)
|
|
return it.index, err
|
|
}
|
|
// Update the metrics touched during block processing
|
|
accountReadTimer.Update(statedb.AccountReads) // Account reads are complete, we can mark them
|
|
storageReadTimer.Update(statedb.StorageReads) // Storage reads are complete, we can mark them
|
|
accountUpdateTimer.Update(statedb.AccountUpdates) // Account updates are complete, we can mark them
|
|
storageUpdateTimer.Update(statedb.StorageUpdates) // Storage updates are complete, we can mark them
|
|
snapshotAccountReadTimer.Update(statedb.SnapshotAccountReads) // Account reads are complete, we can mark them
|
|
snapshotStorageReadTimer.Update(statedb.SnapshotStorageReads) // Storage reads are complete, we can mark them
|
|
|
|
triehash := statedb.AccountHashes + statedb.StorageHashes // Save to not double count in validation
|
|
trieproc := statedb.SnapshotAccountReads + statedb.AccountReads + statedb.AccountUpdates
|
|
trieproc += statedb.SnapshotStorageReads + statedb.StorageReads + statedb.StorageUpdates
|
|
|
|
blockExecutionTimer.Update(time.Since(substart) - trieproc - triehash)
|
|
|
|
// Validate the state using the default validator
|
|
substart = time.Now()
|
|
if err := bc.validator.ValidateState(block, statedb, receipts, usedGas); err != nil {
|
|
bc.reportBlock(block, receipts, err)
|
|
atomic.StoreUint32(&followupInterrupt, 1)
|
|
return it.index, err
|
|
}
|
|
proctime := time.Since(start)
|
|
|
|
// Update the metrics touched during block validation
|
|
accountHashTimer.Update(statedb.AccountHashes) // Account hashes are complete, we can mark them
|
|
storageHashTimer.Update(statedb.StorageHashes) // Storage hashes are complete, we can mark them
|
|
|
|
blockValidationTimer.Update(time.Since(substart) - (statedb.AccountHashes + statedb.StorageHashes - triehash))
|
|
|
|
// Write the block to the chain and get the status.
|
|
substart = time.Now()
|
|
status, err := bc.writeBlockWithState(block, receipts, logs, statedb, false)
|
|
atomic.StoreUint32(&followupInterrupt, 1)
|
|
if err != nil {
|
|
return it.index, err
|
|
}
|
|
|
|
// Update the metrics touched during block commit
|
|
accountCommitTimer.Update(statedb.AccountCommits) // Account commits are complete, we can mark them
|
|
storageCommitTimer.Update(statedb.StorageCommits) // Storage commits are complete, we can mark them
|
|
snapshotCommitTimer.Update(statedb.SnapshotCommits) // Snapshot commits are complete, we can mark them
|
|
|
|
blockWriteTimer.Update(time.Since(substart) - statedb.AccountCommits - statedb.StorageCommits - statedb.SnapshotCommits)
|
|
blockInsertTimer.UpdateSince(start)
|
|
|
|
switch status {
|
|
case CanonStatTy:
|
|
log.Debug("Inserted new block", "number", block.Number(), "hash", block.Hash(),
|
|
"uncles", len(block.Uncles()), "txs", len(block.Transactions()), "gas", block.GasUsed(),
|
|
"elapsed", common.PrettyDuration(time.Since(start)),
|
|
"root", block.Root())
|
|
|
|
lastCanon = block
|
|
|
|
// Only count canonical blocks for GC processing time
|
|
bc.gcproc += proctime
|
|
|
|
case SideStatTy:
|
|
log.Debug("Inserted forked block", "number", block.Number(), "hash", block.Hash(),
|
|
"diff", block.Difficulty(), "elapsed", common.PrettyDuration(time.Since(start)),
|
|
"txs", len(block.Transactions()), "gas", block.GasUsed(), "uncles", len(block.Uncles()),
|
|
"root", block.Root())
|
|
|
|
default:
|
|
// This in theory is impossible, but lets be nice to our future selves and leave
|
|
// a log, instead of trying to track down blocks imports that don't emit logs.
|
|
log.Warn("Inserted block with unknown status", "number", block.Number(), "hash", block.Hash(),
|
|
"diff", block.Difficulty(), "elapsed", common.PrettyDuration(time.Since(start)),
|
|
"txs", len(block.Transactions()), "gas", block.GasUsed(), "uncles", len(block.Uncles()),
|
|
"root", block.Root())
|
|
}
|
|
stats.processed++
|
|
stats.usedGas += usedGas
|
|
|
|
dirty, _ := bc.stateCache.TrieDB().Size()
|
|
stats.report(chain, it.index, dirty)
|
|
}
|
|
// Any blocks remaining here? The only ones we care about are the future ones
|
|
if block != nil && err == consensus.ErrFutureBlock {
|
|
if err := bc.addFutureBlock(block); err != nil {
|
|
return it.index, err
|
|
}
|
|
block, err = it.next()
|
|
|
|
for ; block != nil && err == consensus.ErrUnknownAncestor; block, err = it.next() {
|
|
if err := bc.addFutureBlock(block); err != nil {
|
|
return it.index, err
|
|
}
|
|
stats.queued++
|
|
}
|
|
}
|
|
stats.ignored += it.remaining()
|
|
|
|
return it.index, err
|
|
}
|
|
|
|
// insertSideChain is called when an import batch hits upon a pruned ancestor
|
|
// error, which happens when a sidechain with a sufficiently old fork-block is
|
|
// found.
|
|
//
|
|
// The method writes all (header-and-body-valid) blocks to disk, then tries to
|
|
// switch over to the new chain if the TD exceeded the current chain.
|
|
func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator) (int, error) {
|
|
var (
|
|
externTd *big.Int
|
|
current = bc.CurrentBlock()
|
|
)
|
|
// The first sidechain block error is already verified to be ErrPrunedAncestor.
|
|
// Since we don't import them here, we expect ErrUnknownAncestor for the remaining
|
|
// ones. Any other errors means that the block is invalid, and should not be written
|
|
// to disk.
|
|
err := consensus.ErrPrunedAncestor
|
|
for ; block != nil && (err == consensus.ErrPrunedAncestor); block, err = it.next() {
|
|
// Check the canonical state root for that number
|
|
if number := block.NumberU64(); current.NumberU64() >= number {
|
|
canonical := bc.GetBlockByNumber(number)
|
|
if canonical != nil && canonical.Hash() == block.Hash() {
|
|
// Not a sidechain block, this is a re-import of a canon block which has it's state pruned
|
|
|
|
// Collect the TD of the block. Since we know it's a canon one,
|
|
// we can get it directly, and not (like further below) use
|
|
// the parent and then add the block on top
|
|
externTd = bc.GetTd(block.Hash(), block.NumberU64())
|
|
continue
|
|
}
|
|
if canonical != nil && canonical.Root() == block.Root() {
|
|
// This is most likely a shadow-state attack. When a fork is imported into the
|
|
// database, and it eventually reaches a block height which is not pruned, we
|
|
// just found that the state already exist! This means that the sidechain block
|
|
// refers to a state which already exists in our canon chain.
|
|
//
|
|
// If left unchecked, we would now proceed importing the blocks, without actually
|
|
// having verified the state of the previous blocks.
|
|
log.Warn("Sidechain ghost-state attack detected", "number", block.NumberU64(), "sideroot", block.Root(), "canonroot", canonical.Root())
|
|
|
|
// If someone legitimately side-mines blocks, they would still be imported as usual. However,
|
|
// we cannot risk writing unverified blocks to disk when they obviously target the pruning
|
|
// mechanism.
|
|
return it.index, errors.New("sidechain ghost-state attack")
|
|
}
|
|
}
|
|
if externTd == nil {
|
|
externTd = bc.GetTd(block.ParentHash(), block.NumberU64()-1)
|
|
}
|
|
externTd = new(big.Int).Add(externTd, block.Difficulty())
|
|
|
|
if !bc.HasBlock(block.Hash(), block.NumberU64()) {
|
|
start := time.Now()
|
|
if err := bc.writeBlockWithoutState(block, externTd); err != nil {
|
|
return it.index, err
|
|
}
|
|
log.Debug("Injected sidechain block", "number", block.Number(), "hash", block.Hash(),
|
|
"diff", block.Difficulty(), "elapsed", common.PrettyDuration(time.Since(start)),
|
|
"txs", len(block.Transactions()), "gas", block.GasUsed(), "uncles", len(block.Uncles()),
|
|
"root", block.Root())
|
|
}
|
|
}
|
|
// At this point, we've written all sidechain blocks to database. Loop ended
|
|
// either on some other error or all were processed. If there was some other
|
|
// error, we can ignore the rest of those blocks.
|
|
//
|
|
// If the externTd was larger than our local TD, we now need to reimport the previous
|
|
// blocks to regenerate the required state
|
|
localTd := bc.GetTd(current.Hash(), current.NumberU64())
|
|
if localTd.Cmp(externTd) > 0 {
|
|
log.Info("Sidechain written to disk", "start", it.first().NumberU64(), "end", it.previous().Number, "sidetd", externTd, "localtd", localTd)
|
|
return it.index, err
|
|
}
|
|
// Gather all the sidechain hashes (full blocks may be memory heavy)
|
|
var (
|
|
hashes []common.Hash
|
|
numbers []uint64
|
|
)
|
|
parent := it.previous()
|
|
for parent != nil && !bc.HasState(parent.Root) {
|
|
hashes = append(hashes, parent.Hash())
|
|
numbers = append(numbers, parent.Number.Uint64())
|
|
|
|
parent = bc.GetHeader(parent.ParentHash, parent.Number.Uint64()-1)
|
|
}
|
|
if parent == nil {
|
|
return it.index, errors.New("missing parent")
|
|
}
|
|
// Import all the pruned blocks to make the state available
|
|
var (
|
|
blocks []*types.Block
|
|
memory common.StorageSize
|
|
)
|
|
for i := len(hashes) - 1; i >= 0; i-- {
|
|
// Append the next block to our batch
|
|
block := bc.GetBlock(hashes[i], numbers[i])
|
|
|
|
blocks = append(blocks, block)
|
|
memory += block.Size()
|
|
|
|
// If memory use grew too large, import and continue. Sadly we need to discard
|
|
// all raised events and logs from notifications since we're too heavy on the
|
|
// memory here.
|
|
if len(blocks) >= 2048 || memory > 64*1024*1024 {
|
|
log.Info("Importing heavy sidechain segment", "blocks", len(blocks), "start", blocks[0].NumberU64(), "end", block.NumberU64())
|
|
if _, err := bc.insertChain(blocks, false); err != nil {
|
|
return 0, err
|
|
}
|
|
blocks, memory = blocks[:0], 0
|
|
|
|
// If the chain is terminating, stop processing blocks
|
|
if bc.insertStopped() {
|
|
log.Debug("Abort during blocks processing")
|
|
return 0, nil
|
|
}
|
|
}
|
|
}
|
|
if len(blocks) > 0 {
|
|
log.Info("Importing sidechain segment", "start", blocks[0].NumberU64(), "end", blocks[len(blocks)-1].NumberU64())
|
|
return bc.insertChain(blocks, false)
|
|
}
|
|
return 0, nil
|
|
}
|
|
|
|
// reorg takes two blocks, an old chain and a new chain and will reconstruct the
|
|
// blocks and inserts them to be part of the new canonical chain and accumulates
|
|
// potential missing transactions and post an event about them.
|
|
func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
|
|
var (
|
|
newChain types.Blocks
|
|
oldChain types.Blocks
|
|
commonBlock *types.Block
|
|
|
|
deletedTxs types.Transactions
|
|
addedTxs types.Transactions
|
|
|
|
deletedLogs [][]*types.Log
|
|
rebirthLogs [][]*types.Log
|
|
|
|
// collectLogs collects the logs that were generated or removed during
|
|
// the processing of the block that corresponds with the given hash.
|
|
// These logs are later announced as deleted or reborn
|
|
collectLogs = func(hash common.Hash, removed bool) {
|
|
number := bc.hc.GetBlockNumber(hash)
|
|
if number == nil {
|
|
return
|
|
}
|
|
receipts := rawdb.ReadReceipts(bc.db, hash, *number, bc.chainConfig)
|
|
|
|
var logs []*types.Log
|
|
for _, receipt := range receipts {
|
|
for _, log := range receipt.Logs {
|
|
l := *log
|
|
if removed {
|
|
l.Removed = true
|
|
} else {
|
|
}
|
|
logs = append(logs, &l)
|
|
}
|
|
}
|
|
if len(logs) > 0 {
|
|
if removed {
|
|
deletedLogs = append(deletedLogs, logs)
|
|
} else {
|
|
rebirthLogs = append(rebirthLogs, logs)
|
|
}
|
|
}
|
|
}
|
|
// mergeLogs returns a merged log slice with specified sort order.
|
|
mergeLogs = func(logs [][]*types.Log, reverse bool) []*types.Log {
|
|
var ret []*types.Log
|
|
if reverse {
|
|
for i := len(logs) - 1; i >= 0; i-- {
|
|
ret = append(ret, logs[i]...)
|
|
}
|
|
} else {
|
|
for i := 0; i < len(logs); i++ {
|
|
ret = append(ret, logs[i]...)
|
|
}
|
|
}
|
|
return ret
|
|
}
|
|
)
|
|
// Reduce the longer chain to the same number as the shorter one
|
|
if oldBlock.NumberU64() > newBlock.NumberU64() {
|
|
// Old chain is longer, gather all transactions and logs as deleted ones
|
|
for ; oldBlock != nil && oldBlock.NumberU64() != newBlock.NumberU64(); oldBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1) {
|
|
oldChain = append(oldChain, oldBlock)
|
|
deletedTxs = append(deletedTxs, oldBlock.Transactions()...)
|
|
collectLogs(oldBlock.Hash(), true)
|
|
}
|
|
} else {
|
|
// New chain is longer, stash all blocks away for subsequent insertion
|
|
for ; newBlock != nil && newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1) {
|
|
newChain = append(newChain, newBlock)
|
|
}
|
|
}
|
|
if oldBlock == nil {
|
|
return fmt.Errorf("invalid old chain")
|
|
}
|
|
if newBlock == nil {
|
|
return fmt.Errorf("invalid new chain")
|
|
}
|
|
// Both sides of the reorg are at the same number, reduce both until the common
|
|
// ancestor is found
|
|
for {
|
|
// If the common ancestor was found, bail out
|
|
if oldBlock.Hash() == newBlock.Hash() {
|
|
commonBlock = oldBlock
|
|
break
|
|
}
|
|
// Remove an old block as well as stash away a new block
|
|
oldChain = append(oldChain, oldBlock)
|
|
deletedTxs = append(deletedTxs, oldBlock.Transactions()...)
|
|
collectLogs(oldBlock.Hash(), true)
|
|
|
|
newChain = append(newChain, newBlock)
|
|
|
|
// Step back with both chains
|
|
oldBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1)
|
|
if oldBlock == nil {
|
|
return fmt.Errorf("invalid old chain")
|
|
}
|
|
newBlock = bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1)
|
|
if newBlock == nil {
|
|
return fmt.Errorf("invalid new chain")
|
|
}
|
|
}
|
|
// Ensure the user sees large reorgs
|
|
if len(oldChain) > 0 && len(newChain) > 0 {
|
|
logFn := log.Info
|
|
msg := "Chain reorg detected"
|
|
if len(oldChain) > 63 {
|
|
msg = "Large chain reorg detected"
|
|
logFn = log.Warn
|
|
}
|
|
logFn(msg, "number", commonBlock.Number(), "hash", commonBlock.Hash(),
|
|
"drop", len(oldChain), "dropfrom", oldChain[0].Hash(), "add", len(newChain), "addfrom", newChain[0].Hash())
|
|
blockReorgAddMeter.Mark(int64(len(newChain)))
|
|
blockReorgDropMeter.Mark(int64(len(oldChain)))
|
|
} else {
|
|
log.Error("Impossible reorg, please file an issue", "oldnum", oldBlock.Number(), "oldhash", oldBlock.Hash(), "newnum", newBlock.Number(), "newhash", newBlock.Hash())
|
|
}
|
|
// Insert the new chain(except the head block(reverse order)),
|
|
// taking care of the proper incremental order.
|
|
for i := len(newChain) - 1; i >= 1; i-- {
|
|
// Insert the block in the canonical way, re-writing history
|
|
bc.writeHeadBlock(newChain[i])
|
|
|
|
// Collect reborn logs due to chain reorg
|
|
collectLogs(newChain[i].Hash(), false)
|
|
|
|
// Collect the new added transactions.
|
|
addedTxs = append(addedTxs, newChain[i].Transactions()...)
|
|
}
|
|
// Delete useless indexes right now which includes the non-canonical
|
|
// transaction indexes, canonical chain indexes which above the head.
|
|
indexesBatch := bc.db.NewBatch()
|
|
for _, tx := range types.TxDifference(deletedTxs, addedTxs) {
|
|
rawdb.DeleteTxLookupEntry(indexesBatch, tx.Hash())
|
|
}
|
|
// Delete any canonical number assignments above the new head
|
|
number := bc.CurrentBlock().NumberU64()
|
|
for i := number + 1; ; i++ {
|
|
hash := rawdb.ReadCanonicalHash(bc.db, i)
|
|
if hash == (common.Hash{}) {
|
|
break
|
|
}
|
|
rawdb.DeleteCanonicalHash(indexesBatch, i)
|
|
}
|
|
if err := indexesBatch.Write(); err != nil {
|
|
log.Crit("Failed to delete useless indexes", "err", err)
|
|
}
|
|
// If any logs need to be fired, do it now. In theory we could avoid creating
|
|
// this goroutine if there are no events to fire, but realistcally that only
|
|
// ever happens if we're reorging empty blocks, which will only happen on idle
|
|
// networks where performance is not an issue either way.
|
|
if len(deletedLogs) > 0 {
|
|
bc.rmLogsFeed.Send(RemovedLogsEvent{mergeLogs(deletedLogs, true)})
|
|
}
|
|
if len(rebirthLogs) > 0 {
|
|
bc.logsFeed.Send(mergeLogs(rebirthLogs, false))
|
|
}
|
|
if len(oldChain) > 0 {
|
|
for i := len(oldChain) - 1; i >= 0; i-- {
|
|
bc.chainSideFeed.Send(ChainSideEvent{Block: oldChain[i]})
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (bc *BlockChain) update() {
|
|
futureTimer := time.NewTicker(5 * time.Second)
|
|
defer futureTimer.Stop()
|
|
for {
|
|
select {
|
|
case <-futureTimer.C:
|
|
bc.procFutureBlocks()
|
|
case <-bc.quit:
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// maintainTxIndex is responsible for the construction and deletion of the
|
|
// transaction index.
|
|
//
|
|
// User can use flag `txlookuplimit` to specify a "recentness" block, below
|
|
// which ancient tx indices get deleted. If `txlookuplimit` is 0, it means
|
|
// all tx indices will be reserved.
|
|
//
|
|
// The user can adjust the txlookuplimit value for each launch after fast
|
|
// sync, Geth will automatically construct the missing indices and delete
|
|
// the extra indices.
|
|
func (bc *BlockChain) maintainTxIndex(ancients uint64) {
|
|
// Before starting the actual maintenance, we need to handle a special case,
|
|
// where user might init Geth with an external ancient database. If so, we
|
|
// need to reindex all necessary transactions before starting to process any
|
|
// pruning requests.
|
|
if ancients > 0 {
|
|
var from = uint64(0)
|
|
if bc.txLookupLimit != 0 && ancients > bc.txLookupLimit {
|
|
from = ancients - bc.txLookupLimit
|
|
}
|
|
rawdb.IndexTransactions(bc.db, from, ancients)
|
|
}
|
|
// indexBlocks reindexes or unindexes transactions depending on user configuration
|
|
indexBlocks := func(tail *uint64, head uint64, done chan struct{}) {
|
|
defer func() { done <- struct{}{} }()
|
|
|
|
// If the user just upgraded Geth to a new version which supports transaction
|
|
// index pruning, write the new tail and remove anything older.
|
|
if tail == nil {
|
|
if bc.txLookupLimit == 0 || head < bc.txLookupLimit {
|
|
// Nothing to delete, write the tail and return
|
|
rawdb.WriteTxIndexTail(bc.db, 0)
|
|
} else {
|
|
// Prune all stale tx indices and record the tx index tail
|
|
rawdb.UnindexTransactions(bc.db, 0, head-bc.txLookupLimit+1)
|
|
}
|
|
return
|
|
}
|
|
// If a previous indexing existed, make sure that we fill in any missing entries
|
|
if bc.txLookupLimit == 0 || head < bc.txLookupLimit {
|
|
if *tail > 0 {
|
|
rawdb.IndexTransactions(bc.db, 0, *tail)
|
|
}
|
|
return
|
|
}
|
|
// Update the transaction index to the new chain state
|
|
if head-bc.txLookupLimit+1 < *tail {
|
|
// Reindex a part of missing indices and rewind index tail to HEAD-limit
|
|
rawdb.IndexTransactions(bc.db, head-bc.txLookupLimit+1, *tail)
|
|
} else {
|
|
// Unindex a part of stale indices and forward index tail to HEAD-limit
|
|
rawdb.UnindexTransactions(bc.db, *tail, head-bc.txLookupLimit+1)
|
|
}
|
|
}
|
|
// Any reindexing done, start listening to chain events and moving the index window
|
|
var (
|
|
done chan struct{} // Non-nil if background unindexing or reindexing routine is active.
|
|
headCh = make(chan ChainHeadEvent, 1) // Buffered to avoid locking up the event feed
|
|
)
|
|
sub := bc.SubscribeChainHeadEvent(headCh)
|
|
if sub == nil {
|
|
return
|
|
}
|
|
defer sub.Unsubscribe()
|
|
|
|
for {
|
|
select {
|
|
case head := <-headCh:
|
|
if done == nil {
|
|
done = make(chan struct{})
|
|
go indexBlocks(rawdb.ReadTxIndexTail(bc.db), head.Block.NumberU64(), done)
|
|
}
|
|
case <-done:
|
|
done = nil
|
|
case <-bc.quit:
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// BadBlocks returns a list of the last 'bad blocks' that the client has seen on the network
|
|
func (bc *BlockChain) BadBlocks() []*types.Block {
|
|
blocks := make([]*types.Block, 0, bc.badBlocks.Len())
|
|
for _, hash := range bc.badBlocks.Keys() {
|
|
if blk, exist := bc.badBlocks.Peek(hash); exist {
|
|
block := blk.(*types.Block)
|
|
blocks = append(blocks, block)
|
|
}
|
|
}
|
|
return blocks
|
|
}
|
|
|
|
// addBadBlock adds a bad block to the bad-block LRU cache
|
|
func (bc *BlockChain) addBadBlock(block *types.Block) {
|
|
bc.badBlocks.Add(block.Hash(), block)
|
|
}
|
|
|
|
// reportBlock logs a bad block error.
|
|
func (bc *BlockChain) reportBlock(block *types.Block, receipts types.Receipts, err error) {
|
|
bc.addBadBlock(block)
|
|
|
|
var receiptString string
|
|
for i, receipt := range receipts {
|
|
receiptString += fmt.Sprintf("\t %d: cumulative: %v gas: %v contract: %v status: %v tx: %v logs: %v bloom: %x state: %x\n",
|
|
i, receipt.CumulativeGasUsed, receipt.GasUsed, receipt.ContractAddress.Hex(),
|
|
receipt.Status, receipt.TxHash.Hex(), receipt.Logs, receipt.Bloom, receipt.PostState)
|
|
}
|
|
log.Error(fmt.Sprintf(`
|
|
########## BAD BLOCK #########
|
|
Chain config: %v
|
|
|
|
Number: %v
|
|
Hash: 0x%x
|
|
%v
|
|
|
|
Error: %v
|
|
##############################
|
|
`, bc.chainConfig, block.Number(), block.Hash(), receiptString, err))
|
|
}
|
|
|
|
// InsertHeaderChain attempts to insert the given header chain in to the local
|
|
// chain, possibly creating a reorg. If an error is returned, it will return the
|
|
// index number of the failing header as well an error describing what went wrong.
|
|
//
|
|
// The verify parameter can be used to fine tune whether nonce verification
|
|
// should be done or not. The reason behind the optional check is because some
|
|
// of the header retrieval mechanisms already need to verify nonces, as well as
|
|
// because nonces can be verified sparsely, not needing to check each.
|
|
func (bc *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) {
|
|
start := time.Now()
|
|
if i, err := bc.hc.ValidateHeaderChain(chain, checkFreq); err != nil {
|
|
return i, err
|
|
}
|
|
|
|
// Make sure only one thread manipulates the chain at once
|
|
bc.chainmu.Lock()
|
|
defer bc.chainmu.Unlock()
|
|
|
|
bc.wg.Add(1)
|
|
defer bc.wg.Done()
|
|
|
|
whFunc := func(header *types.Header) error {
|
|
_, err := bc.hc.WriteHeader(header)
|
|
return err
|
|
}
|
|
return bc.hc.InsertHeaderChain(chain, whFunc, start)
|
|
}
|
|
|
|
// CurrentHeader retrieves the current head header of the canonical chain. The
|
|
// header is retrieved from the HeaderChain's internal cache.
|
|
func (bc *BlockChain) CurrentHeader() *types.Header {
|
|
return bc.hc.CurrentHeader()
|
|
}
|
|
|
|
// GetTd retrieves a block's total difficulty in the canonical chain from the
|
|
// database by hash and number, caching it if found.
|
|
func (bc *BlockChain) GetTd(hash common.Hash, number uint64) *big.Int {
|
|
return bc.hc.GetTd(hash, number)
|
|
}
|
|
|
|
// GetTdByHash retrieves a block's total difficulty in the canonical chain from the
|
|
// database by hash, caching it if found.
|
|
func (bc *BlockChain) GetTdByHash(hash common.Hash) *big.Int {
|
|
return bc.hc.GetTdByHash(hash)
|
|
}
|
|
|
|
// GetHeader retrieves a block header from the database by hash and number,
|
|
// caching it if found.
|
|
func (bc *BlockChain) GetHeader(hash common.Hash, number uint64) *types.Header {
|
|
return bc.hc.GetHeader(hash, number)
|
|
}
|
|
|
|
// GetHeaderByHash retrieves a block header from the database by hash, caching it if
|
|
// found.
|
|
func (bc *BlockChain) GetHeaderByHash(hash common.Hash) *types.Header {
|
|
return bc.hc.GetHeaderByHash(hash)
|
|
}
|
|
|
|
// HasHeader checks if a block header is present in the database or not, caching
|
|
// it if present.
|
|
func (bc *BlockChain) HasHeader(hash common.Hash, number uint64) bool {
|
|
return bc.hc.HasHeader(hash, number)
|
|
}
|
|
|
|
// GetCanonicalHash returns the canonical hash for a given block number
|
|
func (bc *BlockChain) GetCanonicalHash(number uint64) common.Hash {
|
|
return bc.hc.GetCanonicalHash(number)
|
|
}
|
|
|
|
// GetBlockHashesFromHash retrieves a number of block hashes starting at a given
|
|
// hash, fetching towards the genesis block.
|
|
func (bc *BlockChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash {
|
|
return bc.hc.GetBlockHashesFromHash(hash, max)
|
|
}
|
|
|
|
// GetAncestor retrieves the Nth ancestor of a given block. It assumes that either the given block or
|
|
// a close ancestor of it is canonical. maxNonCanonical points to a downwards counter limiting the
|
|
// number of blocks to be individually checked before we reach the canonical chain.
|
|
//
|
|
// Note: ancestor == 0 returns the same block, 1 returns its parent and so on.
|
|
func (bc *BlockChain) GetAncestor(hash common.Hash, number, ancestor uint64, maxNonCanonical *uint64) (common.Hash, uint64) {
|
|
return bc.hc.GetAncestor(hash, number, ancestor, maxNonCanonical)
|
|
}
|
|
|
|
// GetHeaderByNumber retrieves a block header from the database by number,
|
|
// caching it (associated with its hash) if found.
|
|
func (bc *BlockChain) GetHeaderByNumber(number uint64) *types.Header {
|
|
return bc.hc.GetHeaderByNumber(number)
|
|
}
|
|
|
|
// GetTransactionLookup retrieves the lookup associate with the given transaction
|
|
// hash from the cache or database.
|
|
func (bc *BlockChain) GetTransactionLookup(hash common.Hash) *rawdb.LegacyTxLookupEntry {
|
|
// Short circuit if the txlookup already in the cache, retrieve otherwise
|
|
if lookup, exist := bc.txLookupCache.Get(hash); exist {
|
|
return lookup.(*rawdb.LegacyTxLookupEntry)
|
|
}
|
|
tx, blockHash, blockNumber, txIndex := rawdb.ReadTransaction(bc.db, hash)
|
|
if tx == nil {
|
|
return nil
|
|
}
|
|
lookup := &rawdb.LegacyTxLookupEntry{BlockHash: blockHash, BlockIndex: blockNumber, Index: txIndex}
|
|
bc.txLookupCache.Add(hash, lookup)
|
|
return lookup
|
|
}
|
|
|
|
// Config retrieves the chain's fork configuration.
|
|
func (bc *BlockChain) Config() *params.ChainConfig { return bc.chainConfig }
|
|
|
|
// Engine retrieves the blockchain's consensus engine.
|
|
func (bc *BlockChain) Engine() consensus.Engine { return bc.engine }
|
|
|
|
// SubscribeRemovedLogsEvent registers a subscription of RemovedLogsEvent.
|
|
func (bc *BlockChain) SubscribeRemovedLogsEvent(ch chan<- RemovedLogsEvent) event.Subscription {
|
|
return bc.scope.Track(bc.rmLogsFeed.Subscribe(ch))
|
|
}
|
|
|
|
// SubscribeChainEvent registers a subscription of ChainEvent.
|
|
func (bc *BlockChain) SubscribeChainEvent(ch chan<- ChainEvent) event.Subscription {
|
|
return bc.scope.Track(bc.chainFeed.Subscribe(ch))
|
|
}
|
|
|
|
// SubscribeChainHeadEvent registers a subscription of ChainHeadEvent.
|
|
func (bc *BlockChain) SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription {
|
|
return bc.scope.Track(bc.chainHeadFeed.Subscribe(ch))
|
|
}
|
|
|
|
// SubscribeChainSideEvent registers a subscription of ChainSideEvent.
|
|
func (bc *BlockChain) SubscribeChainSideEvent(ch chan<- ChainSideEvent) event.Subscription {
|
|
return bc.scope.Track(bc.chainSideFeed.Subscribe(ch))
|
|
}
|
|
|
|
// SubscribeLogsEvent registers a subscription of []*types.Log.
|
|
func (bc *BlockChain) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
|
|
return bc.scope.Track(bc.logsFeed.Subscribe(ch))
|
|
}
|
|
|
|
// SubscribeStateSyncEvent registers a subscription of StateSyncEvent.
|
|
func (bc *BlockChain) SubscribeStateSyncEvent(ch chan<- StateSyncEvent) event.Subscription {
|
|
return bc.scope.Track(bc.stateSyncFeed.Subscribe(ch))
|
|
}
|
|
|
|
// SubscribeBlockProcessingEvent registers a subscription of bool where true means
|
|
// block processing has started while false means it has stopped.
|
|
func (bc *BlockChain) SubscribeBlockProcessingEvent(ch chan<- bool) event.Subscription {
|
|
return bc.scope.Track(bc.blockProcFeed.Subscribe(ch))
|
|
}
|