mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-29 08:03:48 +00:00
* ethdb/pebble: fix nil callbacks (#26650) * eth/downloader: fix timeout resurrection panic (#26652) * common/prque, eth/downloader: fix timeout resurrection panic * common/prque: revert -1 hack for les, temporaryly! * core/state, trie: remove unused error-return from trie Commit operation (#26641) * go.mod: update pebble to latest master (#26654) * core/vm: set tracer-observable `value` of a delegatecall to match parent `value` (#26632) This is a breaking change in the tracing hooks API as well as semantics of the callTracer: - CaptureEnter hook provided a nil value argument in case of DELEGATECALL. However to stay consistent with how delegate calls behave in EVM this hook is changed to pass in the value of the parent call. - callTracer will return parent call's value for DELEGATECALL frames. --------- Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com> * ethdb: add benchmark test suite (#26659) * params: schedule shanghai fork on sepolia (#26662) * params: schedule shanghai fork on sepolia * params: u64 -> newUint64 * eth/filters: avoid block body retrieval when no matching logs (#25199) Logs stored on disk have minimal information. Contextual information such as block number, index of log in block, index of transaction in block are filled in upon request. We can fill in all these fields only having the block header and list of receipts. But determining the transaction hash of a log requires the block body. The goal of this PR is postponing this retrieval until we are sure we the transaction hash. It happens often that the header bloom filter signals there might be matches in a block, but after actually checking them reveals the logs do not match. We want to avoid fetching the body in this case. Note that this changes the semantics of Backend.GetLogs. Downstream callers of GetLogs now assume log context fields have not been derived, and need to call DeriveFields on the logs if necessary. * eth/tracers: more fork overrides in traceBlockToFile (#26655) This change allows all post-Berlin forks to be specified as overrides for futureForkBlock in the config parameter for traceBlockToFile. * tests/fuzzers: supply gnark multiexp config, fixes #26669 (#26670) This change fixes a fuzzer which broke when we updated the gnark dependency earlier. * cmd/devp2p: reduce output of node crawler (#26674) Our discovery crawler spits out a huge amount of logs, most of which is pretty non-interesting. This change moves the very verbose output to Debug, and adds a 8-second status log message giving the general idea about what's going on. * params: update mainnet + rinkeby CHT (#26677) This change updates the CHT entries for mainnet and rinkeby * eth/filters: replace atomic pointer with value (#26689) * eth/filters: replace atomic.Pointer * fix * improve Co-authored-by: Martin Holst Swende <martin@swende.se> --------- Co-authored-by: Martin Holst Swende <martin@swende.se> * p2p/dnsdisc: fix tests with Go 1.20 (#26690) * eth/catalyst: return error if withdrawals are nil post-shanghai (#26691) Spec: https://github.com/ethereum/execution-apis/blob/main/src/engine/shanghai.md#request * ethdb/pebble: Fix `MemTableStopWritesThreshold` (#26692) MemTableStopWritesThreshold was set to the max size of all memtables before blocking writing but should be set to the max number of memtables. This is documented [here](https://github.com/cockroachdb/pebble/blob/master/options.go#L738-L742). * eth/downloader: handle missing withdrawals if empty list is expected (#26675) This PR relaxes the block body ingress handling a bit: if block body withdrawals are missing (but expected to be empty), the body withdrawals are set to 'empty list' before being passed to upper layers. This fixes an issue where a block passed from EthereumJS to geth was deemed invalid. * params: go-ethereum v1.11.0 stable * params: begin v1.11.1 release cycle * travis, build: update Go to 1.20.1 (#26653) travis, build: update Go to 1.20 * core: check genesis state presence by disk read (#26703) * core, eth/downloader: make body validation more strict (#26704) * eth/downloader: fix empty-body case in queue fetchresult (#26707) * eth/downloader: fix typo (#26716) * all: remove deprecated uses of math.rand (#26710) This PR is a (superior) alternative to https://github.com/ethereum/go-ethereum/pull/26708, it handles deprecation, primarily two specific cases. `rand.Seed` is typically used in two ways - `rand.Seed(time.Now().UnixNano())` -- we seed it, just to be sure to get some random, and not always get the same thing on every run. This is not needed, with global seeding, so those are just removed. - `rand.Seed(1)` this is typically done to ensure we have a stable test. If we rely on this, we need to fix up the tests to use a deterministic prng-source. A few occurrences like this has been replaced with a proper custom source. `rand.Read` has been replaced by `crypto/rand`.`Read` in this PR. * params: go-ethereum v1.11.1 stable * params: begin v1.11.2 release cycle * eth/catalyst: send INVALID instead of INVALID_BLOCK_HASH (#26696) This change will break one hive test, but pass another and it will be the better way going forward * ci: disable coverage reporting in appveyor and travis * eth/catalyst: request too large error (#26722) The method `GetPayloadBodiesByRangeV1` now returns "-38004: Too large request" error if the requested range is too large, according to spec Co-authored-by: Martin Holst Swende <martin@swende.se> * core/trie: remove trie tracer (#26665) This PR contains a small portion of the full pbss PR, namely Remove the tracer from trie (and comitter), and instead using an accessList. Related changes to the Nodeset. --------- Co-authored-by: Gary Rong <garyrong0905@gmail.com> * rpc: fix unmarshaling of null result in CallContext (#26723) The change fixes unmarshaling of JSON null results into json.RawMessage. --------- Co-authored-by: Jason Yuan <jason.yuan@curvegrid.com> Co-authored-by: Jason Yuan <jason.yuan869@gmail.com> * build: ship bootstrapper Go along with builder for PPA (#26731) * build: fix setting env var, temp early exit * build: fix gobootstrap path for the PPA * build: add some PPA debug logs, sigh * internal/build: revert raising the chunk size for PPA * build: yet another weird PPA fix * build: fix (finaly?) the PPA env vars for Go bootstrapping * build: fix Go 1.19.0 bootstrapper issues on 386 PPA * build: enable Lunar Lobster PPA builds * Revert "core/trie: remove trie tracer (#26665)" (#26732) This reverts commit7c749c947a. * cmd/geth: clarify dumpconfig options (#26729) Clarifies the documentation around dumpconfi Signed-off-by: Sungwoo Kim <git@sung-woo.kim> * core, eth: merge snap-sync chain download progress logs (#26676) * core: fix accessor mismatch for genesis state (#26747) * core/rawdb: expose chain freezer constructor without internals (#26748) * all: use unified emptyRootHash and emptyCodeHash (#26718) The EmptyRootHash and EmptyCodeHash are defined everywhere in the codebase, this PR replaces all of them with unified one defined in core/types package, and also defines constants for TxRoot, WithdrawalsRoot and UncleRoot * eth/filters: fix a breaking change and return rpctransaction (#26757) * eth/filters: fix a breaking change and return rpctransaction * eth/filters: fix test cases --------- Co-authored-by: Catror <me@catror.com> * common/math: allow HexOrDecimal to accept unquoted decimals too (#26758) * params: release Geth v1.11.2 * params: begin v.1.11.3 release cycle * log: improve documentation (#26753) Add usage examples * core/rawdb, node: use standalone flock dependency (#26633) * eth: use the last announced finalized block as the sync ancient limit (#26685) * cmd/devp2p: faster crawling + less verbose dns updates (#26697) This improves the speed of DHT crawling by using concurrent requests. It also removes logging of individual DNS updates. * eth/tracers: add native flatCallTracer (aka parity style tracer) (#26377) Adds support for a native call tracer with the Parity format, which outputs call frames in a flat array. This tracer accepts the following options: - `convertParityErrors: true` will convert error messages to match those of Parity - `includePrecompiles: true` will report all calls to precompiles. The default matches Parity's behavior where CALL and STATICCALLs to precompiles are excluded Incompatibilities with Parity include: - Parity removes the result object in case of failure. This behavior is maintained with the exception of reverts. Revert output usually contains useful information, i.e. Solidity revert reason. - The `gasUsed` field accounts for intrinsic gas (e.g. 21000 for simple transfers) and refunds unlike Parity - Block rewards are not reported Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com> * core: improve withdrawal index assignment in GenerateChain (#26756) This fixes an issue where the withdrawal index was not calculated correctly for multiple withdrawals in a single block. Co-authored-by: Gary Rong <garyrong0905@gmail.com> Co-authored-by: Felix Lange <fjl@twurst.com> * ethdb/pebble: fix range compaction (#26771) * ethdb/pebble: fix range compaction * ethdb/pebble: add comment * ethdb/pebble: fix max memorytable size (#26776) * ethclient: include withdrawals in ethclient block responses (#26778) * include withdrawals in ethclient responses * omit empty withdrawals array in json serialization * all: change chain head markers from block to header (#26777) * core/rawdb, ethdb/pebble: disable pebble on openbsd (#26801) * core: fix a merge fault (#26802) * README, go.mod, event, internal/version: bump min Go to 1.19 (#26803) * travi: remove strange leftover Go version * core, params: schedule Shanghai on goerli (#26795) * core: params: schedule Shanghai on goerli * core/forkid: fix comment * eth: remove admin.peers[i].eth.head and difficulty (#26804) * core/types: add EffectiveGasPrice in Receipt (#26713) This change adds a struct field EffectiveGasPrice in types.Receipt. The field is present in RPC responses, but not in the Go struct, and thus can't easily be accessed via ethclient. Co-authored-by: PulsarAI <dev@pulsar-systems.fi> * core, eth/catalyst: fix race conditions in tests (#26790) Fixes a race in TestNewPayloadOnInvalidTerminalBlock where setting the TTD raced with the miner. Solution: set the TTD on the blockchain config not the genesis config. Also fixes a race in CopyHeader which resulted in race reports all over the place. * metrics: improve accuracy of CPU gauges (#26793) This PR changes metrics collection to actually measure the time interval between collections, rather than assume 3 seconds. I did some ad hoc profiling, and on slower hardware (eg, my Raspberry Pi 4) I routinely saw intervals between 3.3 - 3.5 seconds, with some being as high as 4.5 seconds. This will generally cause the CPU gauge readings to be too high, and in some cases can cause impossibly large values for the CPU load metrics (eg. greater than 400 for a 4 core CPU). --------- Co-authored-by: Felix Lange <fjl@twurst.com> * ethclient: fix panic when requesting missing blocks (#26817) This fixes a regression introduced by #26723. Fixes #26816. * core, miner: revert block gas counter in case of invalid transaction (#26799) This change fixes a flaw where, in certain scenarios, the block sealer did not accurately reset the remaining gas after failing to include an invalid transaction. Fixes #26791 * internal/ethapi: add tests for transaction types JSON marshal/unmarshal (#26667) Checks that Transaction.MarshalJSON and newRPCTransaction JSON output can be parsed by Transaction.UnmarshalJSON --------- Co-authored-by: Martin Holst Swende <martin@swende.se> * cmd/evm: correct `alloc` for `t8n` testdata (#26822) Fixes a minor error in the testdata * eth/tracers/native: set created address to nil in case of failure (#26779) Fixes #26073 * accounts/usbwallet: mitigate ledger app chunking issue (#26773) This PR mitigates an issue with Ledger's on-device RLP deserialization, see https://github.com/LedgerHQ/app-ethereum/issues/409 Ledger's RLP deserialization code does not validate the length of the RLP list received, and it may prematurely enter the signing flow when a APDU chunk boundary falls immediately before the EIP-155 chain_id when deserializing a transaction. Since the chain_id is uninitialized, it is 0 during this signing flow. This may cause the user to accidentally sign the transaction with chain_id = 0. That signature would be returned from the device 1 packet earlier than expected by the communication loop. The device blocks the second-to-last packet waiting for the signer flow, and then errors on the successive packet (which contains the chain_id, zeroed r, and zeroed s) Since the signature's early arrival causes successive errors during the communication process, geth does not parse the improper signature produced by the device, and therefore no improperly-signed transaction can be created. User funds are not at risk. We mitigate by selecting the highest chunk size that leaves at least 4 bytes in the final chunk. * beacon/engine: don't omit empty withdrawals in ExecutionPayloadBodies (#26698) This ensures the "withdrawals" field will always be present in responses to getPayloadBodiesByRangeV1 and getPayloadBodiesByHashV1. --------- Co-authored-by: Felix Lange <fjl@twurst.com> * build: update to go 1.20.2 (#26824) * params: go-ethereum v1.11.3 stable * params: begin v1.11.4 release cycle * core/rawdb: find smallest block stored in key-value store when chain gapped (#26719) This change prints out more information about the problem, in the case where geth detects a gap between leveldb and ancients, so we can determine more exactly where the gap is (what the first missing is). Also prints out more metadata. --------- Co-authored-by: Martin Holst Swende <martin@swende.se> * signer/core: accept all solidity primitive types for EIP-712 signing (#26770) Accept all primitive types in Solidity for EIP-712 from intN, uintN, intN[], uintN[] for N as 0 to 256 in multiples of 8 --------- Co-authored-by: Martin Holst Swende <martin@swende.se> * params: remove EF azure bootnodes (#26828) * core/vm: use golang native big.Int (#26834) reverts #26021, to use the upstream bigint instead. * core/vm: fix typo in comment (#26838) fixes eip 220 -> 2200 * core/forkid: fix issue in validation test (#26544) This changes the test to match the comment description. Using timestampedConfig in this test case is incorrect, the comment says 'local is at Gray Glacier' and isn't aware of more forks. * cmd/evm: update readmes for the tests (#26841) * core, core/types: plain Message struct (#25977) Here, the core.Message interface turns into a plain struct and types.Message gets removed. This is a breaking change to packages core and core/types. While we do not promise API stability for package core, we do for core/types. An exception can be made for types.Message, since it doesn't have any purpose apart from invoking the state transition in package core. types.Message was also marked deprecated by the same commit it got added in,4dca5d4db7(November 2016). The core.Message interface was added in December 2014, in commitdb494170dc, for the purpose of 'testing' state transitions. It's the same change that made transaction struct fields private. Before that, the state transition used *types.Transaction directly. Over time, multiple implementations of the interface accrued across different packages, since constructing a Message is required whenever one wants to invoke the state transition. These implementations all looked very similar, a struct with private fields exposing the fields as accessor methods. By changing Message into a struct with public fields we can remove all these useless interface implementations. It will also hopefully simplify future changes to the type with less updates to apply across all of go-ethereum when a field is added to Message. --------- Co-authored-by: Felix Lange <fjl@twurst.com> * travis: only build PPAs nightly, not on every push, too heavy (#26846) * p2p: small comment typo (#26850) Update server.go * core: add Timestamp method in BlockGen (#26844) Since forks are now scheduled by block time, it can be necessary to check the timestamp of a block while generating transactions. * core/txpool: implement additional DoS defenses (#26648) This adds two new rules to the transaction pool: - A future transaction can not evict a pending transaction. - A transaction can not overspend available funds of a sender. --- Co-authored-by: dwn1998 <42262393+dwn1998@users.noreply.github.com> Co-authored-by: Martin Holst Swende <martin@swende.se> * params: go-ethereum v1.11.4 stable * params: begin v1.11.5 release cycle * tests: define `MuirGlacier` fork (#26856) add muir glacier to t8n * code/vm: fix comment typo (#26865) it should be constantinople rather than contantinople * core: minor code refactor (#26852) * core: refactor code * core: drop it from this anonymous goroutine func * core/txpool: use priceList.Put instead of heap.Push (#26863) Minor refactor to use the 'intended' accessor * eth: return error if 'safe' or 'finalized' tag used pre-merge (#26862) Co-authored-by: Martin Holst Swende <martin@swende.se> Co-authored-by: Felix Lange <fjl@twurst.com> * .travis.yml: reenable PPA build on tag push (#26873) * core/state, trie: port changes from PBSS (#26763) * p2p/discover: pass invalid discv5 packets to Unhandled channel (#26699) This makes it possible to run another protocol alongside discv5, by reading unhandled packets from the channel. * all: update links in documentation (#26882) Co-authored-by: Stephen Flynn <stephen.flynn@gapac.com> * Increase websocket frame size (from erigon rpc client) (#26883) This increases the maximum allowed message size to 32MB. Originally submitted at https://github.com/ledgerwatch/erigon/pull/2739 example block failure: https://etherscan.io/tx/0x1317d973a55cedf9b0f2df6ea48e8077dd176f5444a3423368a46d6e4db89982#internal * cmd/devp2p, cmd/geth: add version in --help output (#26895) Not sure why this was removed, it's pretty useful to see the version also in --help. * core: show db error-info in case of mismatched hash root (#26870) When a database failure occurs, bubble it up a into statedb, and report it in suitable places, such as during a 'bad block' report. * consensus: improve consensus engine definition (#26871) Makes clear the distinction between Finalize and FinalizedAndAssemble: - In Finalize function, a series of state operations are applied according to consensus rules. The statedb is mutated and the root hash can be checked and compared afterwards. This function should be used in block processing(receive afrom network and apply it locally) but not block generation. - In FinalizeAndAssemble function, after applying state mutations, the block is also to be assembled with the latest state root computed, updating the header. This function should be used in block generation only. * eth/catalyst: increase update consensus timeout (#26840) Increases the time between consensus updates that we give the CL before we start warning the user. * internal/ethapi: avoid int overflow in GetTransactionReceipt (#26911) * trie, accounts/abi: add error-checks (#26914) * rlp: support for uint256 (#26898) This adds built-in support in package rlp for encoding, decoding and generating code dealing with uint256.Int. --------- Co-authored-by: Felix Lange <fjl@twurst.com> * eth: fix output file permissions in admin_exportChain (#26912) * api: Use 0700 file permissions for ExportChain * change perm to 0644 * Update api.go --------- Co-authored-by: Felix Lange <fjl@twurst.com> * trie: reduce unit test time (#26918) * core/txpool: use atomic int added in go1.19 (#26913) Makes use of atomic.Uint64 instead of atomic by pointer * params: schedule shanghai fork on mainnet (#26908) Schedules the shanghai hardfork on timestamp 1681338455 as discussed on ACDE 157: https://github.com/ethereum/execution-specs/pull/727 * core/txpool: allow future local transactions (#26930) Local transactions should not be subject to the "future shouldn't churn pending txs" rule * params: go-ethereum v1.11.5 stable * params: begin v1.11.6 release cycle * build: allow building nightly archives via cron jobs (#26938) * log: add special casing of uint256 into the logger (#26936) * core/rawdb: use atomic int added in go1.19 (#26935) * core/vm: expose jumptable constructors (#26880) When interacting with geth as a library to e.g. produce state tests, it is desirable to obtain the consensus-correct jumptable definition for a given fork. This changes adds accessors so the instructionset can be obtained and characteristics about opcodes can be inspected. * eth/catalyst: fix races (#26950) * core/rawdb: update freezertable read meter (#26946) The meter for "for measuring the effective amount of data read" within the freezertable was never updated. This change remedies that. --------- Signed-off-by: jsvisa <delweng@gmail.com> * cmd/evm, tests: record preimages if dump is expected (#26955) With #25287 we made it so that preimages were not recorded by default. This had the side effect that the evm command is no longer able to dump state since it does a preimage lookup to determine the address represented by a key. This change enables the recording of preimages when the dump command is given. * core/state: add account address to Trie slot accessors (#26934) This changes the Trie interface to add the plain account address as a parameter to all storage-related methods. After the introduction of the TryAccount* functions, TryGet, TryUpdate and TryDelete are now only meant to read an account's storage. In their current form, they assume that an account storage is stored in a separate trie, and that the hashing of the slot is independent of its account's address. The proposed structure for a stateless storage breaks these two assumptions: the hashing of a slot key requires the address and all slots and accounts are stored in a single trie. This PR therefore adds an address parameter to the interface. It is ignored in the MPT version, so this change has no functional impact, however it will reduce the diff size when merging verkle trees. * metrics: add cpu counters (#26796) This PR adds counter metrics for the CPU system and the Geth process. Currently the only metrics available for these items are gauges. Gauges are fine when the consumer scrapes metrics data at the same interval as Geth produces new values (every 3 seconds), but it is likely that most consumers will not scrape that often. Intervals of 10, 15, or maybe even 30 seconds are probably more common. So the problem is, how does the consumer estimate what the CPU was doing in between scrapes. With a counter, it's easy ... you just subtract two successive values and divide by the time to get a nice, accurate average. But with a gauge, you can't do that. A gauge reading is an instantaneous picture of what was happening at that moment, but it gives you no idea about what was going on between scrapes. Taking an average of values is meaningless. * metrics/influxdb: use smaller dependency and reuse code between v1 and v2 reporters (#26963) This change switches to use the smaller influxdata/influxdb1-client package instead of depending on the whole infuxdb package. The new smaller client is very similar to the influxdb-v2 client, which made it possible to refactor the two reporters to reuse code a lot more. * eth/gasprice: change feehistory input type from int to uint64 (#26922) Change input param type from int to uint64 * go.mod: update golang.org/x/tools (#26960) * rlp/rlpgen: print want/expect output string if mismatch (#26932) Co-authored-by: Marius van der Wijden <m.vanderwijden@live.de> * ethclient: ensure returned subscription is nil on error (#26976) * core/state, trie: remove Try prefix in Trie accessors (#26975) This change renames StateTrie methods to remove the Try* prefix. We added the Trie methods with prefix 'Try' a long time ago, working around the problem that most existing methods of Trie did not return the database error. This weird naming convention has persisted until now. Co-authored-by: Gary Rong <garyrong0905@gmail.com> * metrics/librato: ensure resp.body closed (#26969) This change ensures that we call Close on a http response body, in various places in the source code (mostly tests) * core/vm: use atomic.Bool (#26951) Make use of new atomic types --------- Co-authored-by: Felix Lange <fjl@twurst.com> Co-authored-by: Martin Holst Swende <martin@swende.se> * core/bloombits: use atomic type (#26993) * core/state: use atomic.Bool (#26992) * graphql: fix data races (#26965) Fixes multiple data races caused by the fact that resolving fields are done concurrently by the graphql library. It also enforces caching at the stateobject level for account fields. * eth/tracers/native: prevent panic for LOG edge-cases (#26848) This PR fixes OOM panic in the callTracer as well as panicing on opcode validation errors (e.g. stack underflow) in callTracer and prestateTracer. Co-authored-by: Martin Holst Swende <martin@swende.se> * internal/debug: add log.logfmt flag to set logging to use logfmt (#26970) * docs: update outdated DeriveSha docs comment (#26968) * remove @gballet as a GraphQL codeowner (#27012) * core: use atomic type (#27011) * graphql: revert storage access regression (#27007) * cmd/geth: Add `--log.format` cli param (#27001) Removes the new --log.logfmt directive and hides --log.json, replacing both with log.format=(json|logfmt|terminal). The hidden log.json option is still respected if log.format is not specified for backwards compatibility. Co-authored-by: Martin Holst Swende <martin@swende.se> * ethdb/pebble: use atomic type (#27014) * common: fix json marshaller MixedcaseAddress (#26998) Fix the json marshaller of MixedcaseAddress * eth/catalyst: improve consensus heartbeat (#26896) improve the heartbeat function that is no longer suitable in the current situation Co-authored-by: “openex27” <“openexkevin@gmail.com”> * miner: use atomic type (#27013) Use the new typed atomics in the miner package * accounts/abi/bind: handle UnpackLog with zero topics (#26920) Adds error handling for the case that UnpackLog or UnpackLogIntoMap is called with a log that has zero topics. --------- Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com> * cmd/evm: use correct parent number for t8n base fee calculation (#27032) Currently the t8n tool uses the same block number for the current block and its parent while calculating the base fee. This causes incorrect base fee calculation for the london fork block. This commit sets the parent block number to be one less than the current block number * go.mod : update snappy (#27027) * common: delete MakeName (#27023) common,p2p: remove unused function MakeName * cmd/geth: enable log rotation (#26843) This change enables log rotation, which can be activated using the flag --log.rotate. Additional parameters that can be given are: - log.maxsize to set maximum size before files are rotated, - log.maxbackups to set how many files are retailed, - log.maxage to configure max age of rotated files, - log.compress whether to compress rotated files The way to configure location of the logfile(s) is left unchanged, via the `log.logfile` parameter. --------- Co-authored-by: Martin Holst Swende <martin@swende.se> * cmd, miner, signer: avoid panic if keystore is not available (#27039) * cmd, miner, singer: avoid panic if keystore is not available * cmd/geth: print warning instead of panic * test/fuzzers: fuzz rlp handling of big.Int and uint256.Int (#26917) test/fuzzers: fuzz rlp handling of big.Lnt and uint256.Int * core/txpool: move some validation to outside of mutex (#27006) Currently, most of transaction validation while holding the txpool mutex: one exception being an early-on signature check. This PR changes that, so that we do all non-stateful checks before we entering the mutex area. This means they can be performed in parallel, and to enable that, certain fields have been made atomic bools and uint64. * eth/downloader: use atomic types (#27030) * eth/downloader: use atomic type * Update eth/downloader/downloader_test.go Co-authored-by: Martin Holst Swende <martin@swende.se> * Update eth/downloader/downloader_test.go Co-authored-by: Martin Holst Swende <martin@swende.se> --------- Co-authored-by: Martin Holst Swende <martin@swende.se> * core/vm: clarify comment (#27045) * consensus, core/typer: add 4844 excessDataGas to header, tie it to Cancun (#27046) * consensus/misc, params: add EIP-4844 blobfee conversions (#27041) * consensus/misc, params: add EIP-4844 blobfee conversions * consensus/misc: pull in fakeExponential test cases * consensus/misc: reuse bigints * consensus/misc: nit renames, additional larger testcase --------- Co-authored-by: Roberto Bayardo <bayardo@alum.mit.edu> Co-authored-by: Martin Holst Swende <martin@swende.se> * eth/tracers: report correct gasLimit in call tracers (#27029) This includes a semantic change to the `callTracer` as well as `flatCallTracer`. The value of field `gas` in the **first** call frame will change as follows: - It previously contained gas available after initial deductions (i.e. tx costs) - It will now contain the full tx gasLimit value Signed-off-by: jsvisa <delweng@gmail.com> * all: remove debug-field from vm config (#27048) This PR removes the Debug field from vmconfig, making it so that if a tracer is set, debug=true is implied. --------- Co-authored-by: 0xTylerHolmes <tyler@ethereum.org> Co-authored-by: Sina Mahmoodi <1591639+s1na@users.noreply.github.com> * metrics: make gauge_float64 and counter_float64 lock free (#27025) Makes the float-gauges lock-free name old time/op new time/op delta CounterFloat64Parallel-8 1.45µs ±10% 0.85µs ± 6% -41.65% (p=0.008 n=5+5) --------- Co-authored-by: Exca-DK <dev@DESKTOP-RI45P4J.localdomain> Co-authored-by: Martin Holst Swende <martin@swende.se> * eth/tracers: use atomic type (#27031) Use the new atomic types in package eth/tracers --------- Co-authored-by: Martin Holst Swende <martin@swende.se> Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com> * build: upgrade -dlgo version to Go 1.20.3 * core/txpool: disallow future churn by remote txs (#26907) Prior to this change, it was possible that transactions are erroneously deemed as 'future' although they are in fact 'pending', causing them to be dropped due to 'future' not being allowed to replace 'pending'. This change fixes that, by doing a more in-depth inspection of the queue. * core, miner: drop transactions from the same sender when error occurs (#27038) This PR unifies the error handling in miner. Whenever an error occur while applying a transaction, the transaction should be regarded as invalid and all following transactions from the same sender not executable because of the nonce restriction. The only exception is the `nonceTooLow` error which is handled separately. * params: new sepolia bootnodes (#27099) New sepolia bootnodes managed by EF devops * cmd/devp2p: fix erroneous log output in crawler (#27089) cmd/devp2p: fix log of ignored recent nodes counter * signer/core: rename testdata files (#27063) Sets a meaningful name on test-files * core: fix comment to reflect function name (#27070) * params: remove `EIP150Hash` from chainconfig (#27087) The EIP150Hash was an idea where, after the fork, we hardcoded the forked hash as an extra defensive mechanism. It wasn't really used, since forks weren't contentious and for all the various testnets and private networks it's been a hassle to have around. This change removes that config field. --------- Signed-off-by: jsvisa <delweng@gmail.com> * p2p: access embedded fields of Server directly (#27078) * consensus/ethash: use atomic type (#27068) * cmd/devp2p: make crawler-route53-updater less verbose (#27116) Follow-up to #26697, makes the crawler less verbose on route53-based scenarios. It also changes the loglevel from debug to info on Updates, which are typically the root, and can be interesting to see. * cmd/geth: rename --vmodule to --log.vmodule (#27071) renames `--vmodule` to `--log.vmodule`, and prints a warning if the old form is used. * core/vm: order opcodes properly (#27113) * metrics: use atomic type (#27121) * all: refactor trie API (#26995) In this PR, all TryXXX(e.g. TryGet) APIs of trie are renamed to XXX(e.g. Get) with an error returned. The original XXX(e.g. Get) APIs are renamed to MustXXX(e.g. MustGet) and does not return any error -- they print a log output. A future PR will change the behaviour to panic on errorrs. * params: go-ethereum v1.11.6 stable * dev: chg: regression changes for bor after merge * dev: chg: more regression changes for bor after merge * dev: chg: txpool_test regression changes after merge * dev: chg: gomock re-generate mocks for backend interface * dev: chg: regression changes after develop is merged into upstream-merge * dev: chg: further fixes merging develop into upstream-merge * dev: chg: apply changes to NewParallelBlockChain * dev: chg: solve some TODOs * dev: fix: CreateConsensusEngine for new ethereum objects * dev: fix: NewParallelBlockChain using Genesis * dev: fix: build ci.go * dev: fix: thelper and tparallel lint * dev: fix: http related and nilnil lint errors * dev: fix: ineffassign lint errors * dev: chg: comment position fix * dev: fix: govet lint errors * dev: fix: error related lint issues * dev: fix: bodyclose lint issues * dev: fix: some wsl lint issues * dev: fix: more wsl lint issues * dev: fix: errorcheck lint issues * dev: fix: solve more lint issues * dev: fix: more wsl lint issues * dev: fix: more errcheck lint issues * dev: fix: most of wsl lint issues * dev: fix: all remaining lint issues * dev: fix: t.Parallel called multiple times * dev: fix: tests failing due to t.Parallel * fix : runtime testcases * fix : testcase : config, addTxWithChain, burnAmount * fix : ethhash bor burn contract * fix : genspec config * fix : freezer, TestStateProcessorErrors * core,eth,miner: fix initial test cases (#922) * fix : TestTransactionIndices, testBeaconSync * fix : TestBeaconSync66 * core,eth: fix TestDeduplication, TestSyncAccountPerformance, TestTraceTransaction * fix : TestTxIndexer * rm : multiple coinbase balance * fix : testCommitInterruptExperimentBor * adding balance send to burntcontract back * Fix header encoding test * core,eth/tracers: fix TestPrestateWithDiffModeTracer * consensus/bor,eth/filters,miner,params,tests: fix mocks * fix : TestGraphQLConcurrentResolvers * fix : TestBuildPayload * common,core,miner: fix goleaks,duplicate init and inconsistent mutex (un)locks * fix : lint * fix : lint * fix : test-integration * fix : test-integration * core: restore AddFeeTransferLog post miner tipping * fix : TestGraphQLConcurrentResolvers * fix : deadlocks * rm: t.parallel from testQueueTimeLimiting tests * Merge branch 'develop' into mardizzone/upstream-merge * fix : lint * fix : test-integration * fix : TxDependency * add rpc.enabledeprecatedpersonal flag --------- Signed-off-by: Sungwoo Kim <git@sung-woo.kim> Signed-off-by: jsvisa <delweng@gmail.com> Co-authored-by: Péter Szilágyi <peterke@gmail.com> Co-authored-by: Martin Holst Swende <martin@swende.se> Co-authored-by: Chris Ziogas <ziogaschr@gmail.com> Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com> Co-authored-by: rjl493456442 <garyrong0905@gmail.com> Co-authored-by: Marius van der Wijden <m.vanderwijden@live.de> Co-authored-by: Sina Mahmoodi <1591639+s1na@users.noreply.github.com> Co-authored-by: Darioush Jalali <darioush.jalali@avalabs.org> Co-authored-by: Felix Lange <fjl@twurst.com> Co-authored-by: Patrick O'Grady <prohb125@gmail.com> Co-authored-by: ucwong <ucwong@126.com> Co-authored-by: Roman Krasiuk <rokrassyuk@gmail.com> Co-authored-by: Jason Yuan <jason.yuan@curvegrid.com> Co-authored-by: Jason Yuan <jason.yuan869@gmail.com> Co-authored-by: Sungwoo Kim <git@sung-woo.kim> Co-authored-by: Yier <90763233+yierx@users.noreply.github.com> Co-authored-by: Catror <me@catror.com> Co-authored-by: Nate Armstrong <naterarmstrong@gmail.com> Co-authored-by: Dan Cline <6798349+Rjected@users.noreply.github.com> Co-authored-by: Peter (bitfly) <1674920+peterbitfly@users.noreply.github.com> Co-authored-by: PulsarAI <dev@pulsar-systems.fi> Co-authored-by: turboboost55 <7891737+turboboost55@users.noreply.github.com> Co-authored-by: Adrian Sutton <adrian@symphonious.net> Co-authored-by: Guruprasad Kamath <48196632+gurukamath@users.noreply.github.com> Co-authored-by: James Prestwich <10149425+prestwich@users.noreply.github.com> Co-authored-by: Daniel Fernandes <711733+daferna@users.noreply.github.com> Co-authored-by: Rafael Matias <rafael@skyle.net> Co-authored-by: xiyang <90125263+JBossBC@users.noreply.github.com> Co-authored-by: Roberto Bayardo <bayardo@alum.mit.edu> Co-authored-by: panicalways <113693386+panicalways@users.noreply.github.com> Co-authored-by: dwn1998 <42262393+dwn1998@users.noreply.github.com> Co-authored-by: s7v7nislands <s7v7nislands@gmail.com> Co-authored-by: lightclient <14004106+lightclient@users.noreply.github.com> Co-authored-by: Stephen Flynn <ssflynn@gmail.com> Co-authored-by: Stephen Flynn <stephen.flynn@gapac.com> Co-authored-by: Jonathan Otto <jonathan.otto@gmail.com> Co-authored-by: Delweng <delweng@gmail.com> Co-authored-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Co-authored-by: aaronbuchwald <aaron.buchwald56@gmail.com> Co-authored-by: ucwong <ethereum2k@gmail.com> Co-authored-by: norwnd <112318969+norwnd@users.noreply.github.com> Co-authored-by: jwasinger <j-wasinger@hotmail.com> Co-authored-by: Adrian Sutton <adrian@oplabs.co> Co-authored-by: David Murdoch <187813+davidmurdoch@users.noreply.github.com> Co-authored-by: openex <openexkevin@gmail.com> Co-authored-by: “openex27” <“openexkevin@gmail.com”> Co-authored-by: sudeep <sudeepdino008@gmail.com> Co-authored-by: joohhnnn <68833933+joohhnnn@users.noreply.github.com> Co-authored-by: 0xTylerHolmes <tyler@ethereum.org> Co-authored-by: Exca-DK <85954505+Exca-DK@users.noreply.github.com> Co-authored-by: Exca-DK <dev@DESKTOP-RI45P4J.localdomain> Co-authored-by: Marius Kjærstad <mkjaerstad@protonmail.com> Co-authored-by: Parithosh Jayanthi <parithosh@indenwolken.xyz> Co-authored-by: noel <72006780+0x00Duke@users.noreply.github.com> Co-authored-by: Taeguk Kwon <xornrbboy@gmail.com> Co-authored-by: Anusha <63559942+anusha-ctrl@users.noreply.github.com> Co-authored-by: Alex Beregszaszi <alex@rtfs.hu> Co-authored-by: Shivam Sharma <shivam691999@gmail.com> Co-authored-by: Raneet Debnath <35629432+Raneet10@users.noreply.github.com> Co-authored-by: Raneet Debnath <raneetdebnath10@gmail.com> Co-authored-by: Jerry <jerrycgh@gmail.com> Co-authored-by: Manav Darji <manavdarji.india@gmail.com>
1657 lines
49 KiB
Go
1657 lines
49 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 state provides a caching layer atop the Ethereum state trie.
|
|
package state
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"math/big"
|
|
"sort"
|
|
"time"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/core/blockstm"
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
|
"github.com/ethereum/go-ethereum/core/state/snapshot"
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
"github.com/ethereum/go-ethereum/log"
|
|
"github.com/ethereum/go-ethereum/metrics"
|
|
"github.com/ethereum/go-ethereum/params"
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
)
|
|
|
|
type revision struct {
|
|
id int
|
|
journalIndex int
|
|
}
|
|
|
|
type proofList [][]byte
|
|
|
|
func (n *proofList) Put(key []byte, value []byte) error {
|
|
*n = append(*n, value)
|
|
return nil
|
|
}
|
|
|
|
func (n *proofList) Delete(key []byte) error {
|
|
panic("not supported")
|
|
}
|
|
|
|
// StateDB structs within the ethereum protocol are used to store anything
|
|
// within the merkle trie. StateDBs take care of caching and storing
|
|
// nested states. It's the general query interface to retrieve:
|
|
// * Contracts
|
|
// * Accounts
|
|
type StateDB struct {
|
|
db Database
|
|
prefetcher *triePrefetcher
|
|
trie Trie
|
|
hasher crypto.KeccakState
|
|
|
|
// originalRoot is the pre-state root, before any changes were made.
|
|
// It will be updated when the Commit is called.
|
|
originalRoot common.Hash
|
|
|
|
snaps *snapshot.Tree
|
|
snap snapshot.Snapshot
|
|
snapAccounts map[common.Hash][]byte
|
|
snapStorage map[common.Hash]map[common.Hash][]byte
|
|
|
|
// This map holds 'live' objects, which will get modified while processing a state transition.
|
|
stateObjects map[common.Address]*stateObject
|
|
stateObjectsPending map[common.Address]struct{} // State objects finalized but not yet written to the trie
|
|
stateObjectsDirty map[common.Address]struct{} // State objects modified in the current execution
|
|
stateObjectsDestruct map[common.Address]struct{} // State objects destructed in the block
|
|
|
|
// Block-stm related fields
|
|
mvHashmap *blockstm.MVHashMap
|
|
incarnation int
|
|
readMap map[blockstm.Key]blockstm.ReadDescriptor
|
|
writeMap map[blockstm.Key]blockstm.WriteDescriptor
|
|
revertedKeys map[blockstm.Key]struct{}
|
|
dep int
|
|
|
|
// DB error.
|
|
// State objects are used by the consensus core and VM which are
|
|
// unable to deal with database-level errors. Any error that occurs
|
|
// during a database read is memoized here and will eventually be
|
|
// returned by StateDB.Commit. Notably, this error is also shared
|
|
// by all cached state objects in case the database failure occurs
|
|
// when accessing state of accounts.
|
|
dbErr error
|
|
|
|
// The refund counter, also used by state transitioning.
|
|
refund uint64
|
|
|
|
thash common.Hash
|
|
txIndex int
|
|
logs map[common.Hash][]*types.Log
|
|
logSize uint
|
|
|
|
preimages map[common.Hash][]byte
|
|
|
|
// Per-transaction access list
|
|
accessList *accessList
|
|
|
|
// Transient storage
|
|
transientStorage transientStorage
|
|
|
|
// Journal of state modifications. This is the backbone of
|
|
// Snapshot and RevertToSnapshot.
|
|
journal *journal
|
|
validRevisions []revision
|
|
nextRevisionId int
|
|
|
|
// Measurements gathered during execution for debugging purposes
|
|
AccountReads time.Duration
|
|
AccountHashes time.Duration
|
|
AccountUpdates time.Duration
|
|
AccountCommits time.Duration
|
|
StorageReads time.Duration
|
|
StorageHashes time.Duration
|
|
StorageUpdates time.Duration
|
|
StorageCommits time.Duration
|
|
SnapshotAccountReads time.Duration
|
|
SnapshotStorageReads time.Duration
|
|
SnapshotCommits time.Duration
|
|
TrieDBCommits time.Duration
|
|
|
|
AccountUpdated int
|
|
StorageUpdated int
|
|
AccountDeleted int
|
|
StorageDeleted int
|
|
}
|
|
|
|
// New creates a new state from a given trie.
|
|
func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
|
|
tr, err := db.OpenTrie(root)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
sdb := &StateDB{
|
|
db: db,
|
|
trie: tr,
|
|
originalRoot: root,
|
|
snaps: snaps,
|
|
stateObjects: make(map[common.Address]*stateObject),
|
|
stateObjectsPending: make(map[common.Address]struct{}),
|
|
stateObjectsDirty: make(map[common.Address]struct{}),
|
|
stateObjectsDestruct: make(map[common.Address]struct{}),
|
|
revertedKeys: make(map[blockstm.Key]struct{}),
|
|
logs: make(map[common.Hash][]*types.Log),
|
|
preimages: make(map[common.Hash][]byte),
|
|
journal: newJournal(),
|
|
accessList: newAccessList(),
|
|
transientStorage: newTransientStorage(),
|
|
hasher: crypto.NewKeccakState(),
|
|
}
|
|
if sdb.snaps != nil {
|
|
if sdb.snap = sdb.snaps.Snapshot(root); sdb.snap != nil {
|
|
sdb.snapAccounts = make(map[common.Hash][]byte)
|
|
sdb.snapStorage = make(map[common.Hash]map[common.Hash][]byte)
|
|
}
|
|
}
|
|
|
|
return sdb, nil
|
|
}
|
|
|
|
func NewWithMVHashmap(root common.Hash, db Database, snaps *snapshot.Tree, mvhm *blockstm.MVHashMap) (*StateDB, error) {
|
|
if sdb, err := New(root, db, snaps); err != nil {
|
|
return nil, err
|
|
} else {
|
|
sdb.mvHashmap = mvhm
|
|
sdb.dep = -1
|
|
|
|
return sdb, nil
|
|
}
|
|
}
|
|
|
|
func (s *StateDB) SetMVHashmap(mvhm *blockstm.MVHashMap) {
|
|
s.mvHashmap = mvhm
|
|
s.dep = -1
|
|
}
|
|
|
|
func (s *StateDB) GetMVHashmap() *blockstm.MVHashMap {
|
|
return s.mvHashmap
|
|
}
|
|
|
|
func (s *StateDB) MVWriteList() []blockstm.WriteDescriptor {
|
|
writes := make([]blockstm.WriteDescriptor, 0, len(s.writeMap))
|
|
|
|
for _, v := range s.writeMap {
|
|
if _, ok := s.revertedKeys[v.Path]; !ok {
|
|
writes = append(writes, v)
|
|
}
|
|
}
|
|
|
|
return writes
|
|
}
|
|
|
|
func (s *StateDB) MVFullWriteList() []blockstm.WriteDescriptor {
|
|
writes := make([]blockstm.WriteDescriptor, 0, len(s.writeMap))
|
|
|
|
for _, v := range s.writeMap {
|
|
writes = append(writes, v)
|
|
}
|
|
|
|
return writes
|
|
}
|
|
|
|
func (s *StateDB) MVReadMap() map[blockstm.Key]blockstm.ReadDescriptor {
|
|
return s.readMap
|
|
}
|
|
|
|
func (s *StateDB) MVReadList() []blockstm.ReadDescriptor {
|
|
reads := make([]blockstm.ReadDescriptor, 0, len(s.readMap))
|
|
|
|
for _, v := range s.MVReadMap() {
|
|
reads = append(reads, v)
|
|
}
|
|
|
|
return reads
|
|
}
|
|
|
|
func (s *StateDB) ensureReadMap() {
|
|
if s.readMap == nil {
|
|
s.readMap = make(map[blockstm.Key]blockstm.ReadDescriptor)
|
|
}
|
|
}
|
|
|
|
func (s *StateDB) ensureWriteMap() {
|
|
if s.writeMap == nil {
|
|
s.writeMap = make(map[blockstm.Key]blockstm.WriteDescriptor)
|
|
}
|
|
}
|
|
|
|
func (s *StateDB) ClearReadMap() {
|
|
s.readMap = make(map[blockstm.Key]blockstm.ReadDescriptor)
|
|
}
|
|
|
|
func (s *StateDB) ClearWriteMap() {
|
|
s.writeMap = make(map[blockstm.Key]blockstm.WriteDescriptor)
|
|
}
|
|
|
|
func (s *StateDB) HadInvalidRead() bool {
|
|
return s.dep >= 0
|
|
}
|
|
|
|
func (s *StateDB) DepTxIndex() int {
|
|
return s.dep
|
|
}
|
|
|
|
func (s *StateDB) SetIncarnation(inc int) {
|
|
s.incarnation = inc
|
|
}
|
|
|
|
type StorageVal[T any] struct {
|
|
Value *T
|
|
}
|
|
|
|
func MVRead[T any](s *StateDB, k blockstm.Key, defaultV T, readStorage func(s *StateDB) T) (v T) {
|
|
if s.mvHashmap == nil {
|
|
return readStorage(s)
|
|
}
|
|
|
|
s.ensureReadMap()
|
|
|
|
if s.writeMap != nil {
|
|
if _, ok := s.writeMap[k]; ok {
|
|
return readStorage(s)
|
|
}
|
|
}
|
|
|
|
if !k.IsAddress() {
|
|
// If we are reading subpath from a deleted account, return default value instead of reading from MVHashmap
|
|
addr := k.GetAddress()
|
|
if s.getStateObject(addr) == nil {
|
|
return defaultV
|
|
}
|
|
}
|
|
|
|
res := s.mvHashmap.Read(k, s.txIndex)
|
|
|
|
var rd blockstm.ReadDescriptor
|
|
|
|
rd.V = blockstm.Version{
|
|
TxnIndex: res.DepIdx(),
|
|
Incarnation: res.Incarnation(),
|
|
}
|
|
|
|
rd.Path = k
|
|
|
|
switch res.Status() {
|
|
case blockstm.MVReadResultDone:
|
|
{
|
|
v = readStorage(res.Value().(*StateDB))
|
|
rd.Kind = blockstm.ReadKindMap
|
|
}
|
|
case blockstm.MVReadResultDependency:
|
|
{
|
|
s.dep = res.DepIdx()
|
|
|
|
panic("Found dependency")
|
|
}
|
|
case blockstm.MVReadResultNone:
|
|
{
|
|
v = readStorage(s)
|
|
rd.Kind = blockstm.ReadKindStorage
|
|
}
|
|
default:
|
|
return defaultV
|
|
}
|
|
|
|
// TODO: I assume we don't want to overwrite an existing read because this could - for example - change a storage
|
|
// read to map if the same value is read multiple times.
|
|
if _, ok := s.readMap[k]; !ok {
|
|
s.readMap[k] = rd
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func MVWrite(s *StateDB, k blockstm.Key) {
|
|
if s.mvHashmap != nil {
|
|
s.ensureWriteMap()
|
|
s.writeMap[k] = blockstm.WriteDescriptor{
|
|
Path: k,
|
|
V: s.Version(),
|
|
Val: s,
|
|
}
|
|
}
|
|
}
|
|
|
|
func RevertWrite(s *StateDB, k blockstm.Key) {
|
|
s.revertedKeys[k] = struct{}{}
|
|
}
|
|
|
|
func MVWritten(s *StateDB, k blockstm.Key) bool {
|
|
if s.mvHashmap == nil || s.writeMap == nil {
|
|
return false
|
|
}
|
|
|
|
_, ok := s.writeMap[k]
|
|
|
|
return ok
|
|
}
|
|
|
|
// FlushMVWriteSet applies entries in the write set to MVHashMap. Note that this function does not clear the write set.
|
|
func (s *StateDB) FlushMVWriteSet() {
|
|
if s.mvHashmap != nil && s.writeMap != nil {
|
|
s.mvHashmap.FlushMVWriteSet(s.MVFullWriteList())
|
|
}
|
|
}
|
|
|
|
// ApplyMVWriteSet applies entries in a given write set to StateDB. Note that this function does not change MVHashMap nor write set
|
|
// of the current StateDB.
|
|
func (s *StateDB) ApplyMVWriteSet(writes []blockstm.WriteDescriptor) {
|
|
for i := range writes {
|
|
path := writes[i].Path
|
|
sr := writes[i].Val.(*StateDB)
|
|
|
|
if path.IsState() {
|
|
addr := path.GetAddress()
|
|
stateKey := path.GetStateKey()
|
|
state := sr.GetState(addr, stateKey)
|
|
s.SetState(addr, stateKey, state)
|
|
} else if path.IsAddress() {
|
|
continue
|
|
} else {
|
|
addr := path.GetAddress()
|
|
|
|
switch path.GetSubpath() {
|
|
case BalancePath:
|
|
s.SetBalance(addr, sr.GetBalance(addr))
|
|
case NoncePath:
|
|
s.SetNonce(addr, sr.GetNonce(addr))
|
|
case CodePath:
|
|
s.SetCode(addr, sr.GetCode(addr))
|
|
case SuicidePath:
|
|
stateObject := sr.getDeletedStateObject(addr)
|
|
if stateObject != nil && stateObject.deleted {
|
|
s.Suicide(addr)
|
|
}
|
|
default:
|
|
panic(fmt.Errorf("unknown key type: %d", path.GetSubpath()))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
type DumpStruct struct {
|
|
TxIdx int
|
|
TxInc int
|
|
VerIdx int
|
|
VerInc int
|
|
Path []byte
|
|
Op string
|
|
}
|
|
|
|
// GetReadMapDump gets readMap Dump of format: "TxIdx, Inc, Path, Read"
|
|
func (s *StateDB) GetReadMapDump() []DumpStruct {
|
|
readList := s.MVReadList()
|
|
res := make([]DumpStruct, 0, len(readList))
|
|
|
|
for _, val := range readList {
|
|
temp := &DumpStruct{
|
|
TxIdx: s.txIndex,
|
|
TxInc: s.incarnation,
|
|
VerIdx: val.V.TxnIndex,
|
|
VerInc: val.V.Incarnation,
|
|
Path: val.Path[:],
|
|
Op: "Read\n",
|
|
}
|
|
res = append(res, *temp)
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
// GetWriteMapDump gets writeMap Dump of format: "TxIdx, Inc, Path, Write"
|
|
func (s *StateDB) GetWriteMapDump() []DumpStruct {
|
|
writeList := s.MVReadList()
|
|
res := make([]DumpStruct, 0, len(writeList))
|
|
|
|
for _, val := range writeList {
|
|
temp := &DumpStruct{
|
|
TxIdx: s.txIndex,
|
|
TxInc: s.incarnation,
|
|
VerIdx: val.V.TxnIndex,
|
|
VerInc: val.V.Incarnation,
|
|
Path: val.Path[:],
|
|
Op: "Write\n",
|
|
}
|
|
res = append(res, *temp)
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
// AddEmptyMVHashMap adds empty MVHashMap to StateDB
|
|
func (s *StateDB) AddEmptyMVHashMap() {
|
|
mvh := blockstm.MakeMVHashMap()
|
|
s.mvHashmap = mvh
|
|
}
|
|
|
|
// StartPrefetcher initializes a new trie prefetcher to pull in nodes from the
|
|
// state trie concurrently while the state is mutated so that when we reach the
|
|
// commit phase, most of the needed data is already hot.
|
|
func (s *StateDB) StartPrefetcher(namespace string) {
|
|
if s.prefetcher != nil {
|
|
s.prefetcher.close()
|
|
s.prefetcher = nil
|
|
}
|
|
|
|
if s.snap != nil {
|
|
s.prefetcher = newTriePrefetcher(s.db, s.originalRoot, namespace)
|
|
}
|
|
}
|
|
|
|
// StopPrefetcher terminates a running prefetcher and reports any leftover stats
|
|
// from the gathered metrics.
|
|
func (s *StateDB) StopPrefetcher() {
|
|
if s.prefetcher != nil {
|
|
s.prefetcher.close()
|
|
s.prefetcher = nil
|
|
}
|
|
}
|
|
|
|
// setError remembers the first non-nil error it is called with.
|
|
func (s *StateDB) setError(err error) {
|
|
if s.dbErr == nil {
|
|
s.dbErr = err
|
|
}
|
|
}
|
|
|
|
// Error returns the memorized database failure occurred earlier.
|
|
func (s *StateDB) Error() error {
|
|
return s.dbErr
|
|
}
|
|
|
|
func (s *StateDB) AddLog(log *types.Log) {
|
|
s.journal.append(addLogChange{txhash: s.thash})
|
|
|
|
log.TxHash = s.thash
|
|
log.TxIndex = uint(s.txIndex)
|
|
log.Index = s.logSize
|
|
s.logs[s.thash] = append(s.logs[s.thash], log)
|
|
s.logSize++
|
|
}
|
|
|
|
// GetLogs returns the logs matching the specified transaction hash, and annotates
|
|
// them with the given blockNumber and blockHash.
|
|
func (s *StateDB) GetLogs(hash common.Hash, blockNumber uint64, blockHash common.Hash) []*types.Log {
|
|
logs := s.logs[hash]
|
|
for _, l := range logs {
|
|
l.BlockNumber = blockNumber
|
|
l.BlockHash = blockHash
|
|
}
|
|
|
|
return logs
|
|
}
|
|
|
|
func (s *StateDB) Logs() []*types.Log {
|
|
var logs []*types.Log
|
|
for _, lgs := range s.logs {
|
|
logs = append(logs, lgs...)
|
|
}
|
|
|
|
return logs
|
|
}
|
|
|
|
// AddPreimage records a SHA3 preimage seen by the VM.
|
|
func (s *StateDB) AddPreimage(hash common.Hash, preimage []byte) {
|
|
if _, ok := s.preimages[hash]; !ok {
|
|
s.journal.append(addPreimageChange{hash: hash})
|
|
|
|
pi := make([]byte, len(preimage))
|
|
copy(pi, preimage)
|
|
s.preimages[hash] = pi
|
|
}
|
|
}
|
|
|
|
// Preimages returns a list of SHA3 preimages that have been submitted.
|
|
func (s *StateDB) Preimages() map[common.Hash][]byte {
|
|
return s.preimages
|
|
}
|
|
|
|
// AddRefund adds gas to the refund counter
|
|
func (s *StateDB) AddRefund(gas uint64) {
|
|
s.journal.append(refundChange{prev: s.refund})
|
|
s.refund += gas
|
|
}
|
|
|
|
// SubRefund removes gas from the refund counter.
|
|
// This method will panic if the refund counter goes below zero
|
|
func (s *StateDB) SubRefund(gas uint64) {
|
|
s.journal.append(refundChange{prev: s.refund})
|
|
|
|
if gas > s.refund {
|
|
panic(fmt.Sprintf("Refund counter below zero (gas: %d > refund: %d)", gas, s.refund))
|
|
}
|
|
|
|
s.refund -= gas
|
|
}
|
|
|
|
// Exist reports whether the given account address exists in the state.
|
|
// Notably this also returns true for suicided accounts.
|
|
func (s *StateDB) Exist(addr common.Address) bool {
|
|
return s.getStateObject(addr) != nil
|
|
}
|
|
|
|
// Empty returns whether the state object is either non-existent
|
|
// or empty according to the EIP161 specification (balance = nonce = code = 0)
|
|
func (s *StateDB) Empty(addr common.Address) bool {
|
|
so := s.getStateObject(addr)
|
|
return so == nil || so.empty()
|
|
}
|
|
|
|
// Create a unique path for special fields (e.g. balance, code) in a state object.
|
|
// func subPath(prefix []byte, s uint8) [blockstm.KeyLength]byte {
|
|
// path := append(prefix, common.Hash{}.Bytes()...) // append a full empty hash to avoid collision with storage state
|
|
// path = append(path, s) // append the special field identifier
|
|
|
|
// return path
|
|
// }
|
|
|
|
const BalancePath = 1
|
|
const NoncePath = 2
|
|
const CodePath = 3
|
|
const SuicidePath = 4
|
|
|
|
// GetBalance retrieves the balance from the given address or 0 if object not found
|
|
func (s *StateDB) GetBalance(addr common.Address) *big.Int {
|
|
return MVRead(s, blockstm.NewSubpathKey(addr, BalancePath), common.Big0, func(s *StateDB) *big.Int {
|
|
stateObject := s.getStateObject(addr)
|
|
if stateObject != nil {
|
|
return stateObject.Balance()
|
|
}
|
|
|
|
return common.Big0
|
|
})
|
|
}
|
|
|
|
func (s *StateDB) GetNonce(addr common.Address) uint64 {
|
|
return MVRead(s, blockstm.NewSubpathKey(addr, NoncePath), 0, func(s *StateDB) uint64 {
|
|
stateObject := s.getStateObject(addr)
|
|
if stateObject != nil {
|
|
return stateObject.Nonce()
|
|
}
|
|
|
|
return 0
|
|
})
|
|
}
|
|
|
|
// TxIndex returns the current transaction index set by Prepare.
|
|
func (s *StateDB) TxIndex() int {
|
|
return s.txIndex
|
|
}
|
|
|
|
func (s *StateDB) Version() blockstm.Version {
|
|
return blockstm.Version{
|
|
TxnIndex: s.txIndex,
|
|
Incarnation: s.incarnation,
|
|
}
|
|
}
|
|
|
|
func (s *StateDB) GetCode(addr common.Address) []byte {
|
|
return MVRead(s, blockstm.NewSubpathKey(addr, CodePath), nil, func(s *StateDB) []byte {
|
|
stateObject := s.getStateObject(addr)
|
|
if stateObject != nil {
|
|
return stateObject.Code(s.db)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (s *StateDB) GetCodeSize(addr common.Address) int {
|
|
return MVRead(s, blockstm.NewSubpathKey(addr, CodePath), 0, func(s *StateDB) int {
|
|
stateObject := s.getStateObject(addr)
|
|
if stateObject != nil {
|
|
return stateObject.CodeSize(s.db)
|
|
}
|
|
|
|
return 0
|
|
})
|
|
}
|
|
|
|
func (s *StateDB) GetCodeHash(addr common.Address) common.Hash {
|
|
return MVRead(s, blockstm.NewSubpathKey(addr, CodePath), common.Hash{}, func(s *StateDB) common.Hash {
|
|
stateObject := s.getStateObject(addr)
|
|
if stateObject == nil {
|
|
return common.Hash{}
|
|
}
|
|
|
|
return common.BytesToHash(stateObject.CodeHash())
|
|
})
|
|
}
|
|
|
|
// GetState retrieves a value from the given account's storage trie.
|
|
func (s *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash {
|
|
return MVRead(s, blockstm.NewStateKey(addr, hash), common.Hash{}, func(s *StateDB) common.Hash {
|
|
stateObject := s.getStateObject(addr)
|
|
if stateObject != nil {
|
|
return stateObject.GetState(s.db, hash)
|
|
}
|
|
|
|
return common.Hash{}
|
|
})
|
|
}
|
|
|
|
// GetProof returns the Merkle proof for a given account.
|
|
func (s *StateDB) GetProof(addr common.Address) ([][]byte, error) {
|
|
return s.GetProofByHash(crypto.Keccak256Hash(addr.Bytes()))
|
|
}
|
|
|
|
// GetProofByHash returns the Merkle proof for a given account.
|
|
func (s *StateDB) GetProofByHash(addrHash common.Hash) ([][]byte, error) {
|
|
var proof proofList
|
|
err := s.trie.Prove(addrHash[:], 0, &proof)
|
|
|
|
return proof, err
|
|
}
|
|
|
|
// GetStorageProof returns the Merkle proof for given storage slot.
|
|
func (s *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, error) {
|
|
trie, err := s.StorageTrie(a)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if trie == nil {
|
|
return nil, errors.New("storage trie for requested address does not exist")
|
|
}
|
|
|
|
var proof proofList
|
|
err = trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return proof, nil
|
|
}
|
|
|
|
// GetCommittedState retrieves a value from the given account's committed storage trie.
|
|
func (s *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash {
|
|
return MVRead(s, blockstm.NewStateKey(addr, hash), common.Hash{}, func(s *StateDB) common.Hash {
|
|
stateObject := s.getStateObject(addr)
|
|
if stateObject != nil {
|
|
return stateObject.GetCommittedState(s.db, hash)
|
|
}
|
|
|
|
return common.Hash{}
|
|
})
|
|
}
|
|
|
|
// Database retrieves the low level database supporting the lower level trie ops.
|
|
func (s *StateDB) Database() Database {
|
|
return s.db
|
|
}
|
|
|
|
// StorageTrie returns the storage trie of an account. The return value is a copy
|
|
// and is nil for non-existent accounts. An error will be returned if storage trie
|
|
// is existent but can't be loaded correctly.
|
|
func (s *StateDB) StorageTrie(addr common.Address) (Trie, error) {
|
|
stateObject := s.getStateObject(addr)
|
|
if stateObject == nil {
|
|
//nolint:nilnil
|
|
return nil, nil
|
|
}
|
|
|
|
cpy := stateObject.deepCopy(s)
|
|
|
|
if _, err := cpy.updateTrie(s.db); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return cpy.getTrie(s.db)
|
|
}
|
|
|
|
func (s *StateDB) HasSuicided(addr common.Address) bool {
|
|
return MVRead(s, blockstm.NewSubpathKey(addr, SuicidePath), false, func(s *StateDB) bool {
|
|
stateObject := s.getStateObject(addr)
|
|
if stateObject != nil {
|
|
return stateObject.suicided
|
|
}
|
|
|
|
return false
|
|
})
|
|
}
|
|
|
|
/*
|
|
* SETTERS
|
|
*/
|
|
|
|
// AddBalance adds amount to the account associated with addr.
|
|
func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) {
|
|
stateObject := s.GetOrNewStateObject(addr)
|
|
|
|
if s.mvHashmap != nil {
|
|
// ensure a read balance operation is recorded in mvHashmap
|
|
s.GetBalance(addr)
|
|
}
|
|
|
|
if stateObject != nil {
|
|
stateObject = s.mvRecordWritten(stateObject)
|
|
stateObject.AddBalance(amount)
|
|
MVWrite(s, blockstm.NewSubpathKey(addr, BalancePath))
|
|
}
|
|
}
|
|
|
|
// SubBalance subtracts amount from the account associated with addr.
|
|
func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) {
|
|
stateObject := s.GetOrNewStateObject(addr)
|
|
|
|
if s.mvHashmap != nil {
|
|
// ensure a read balance operation is recorded in mvHashmap
|
|
s.GetBalance(addr)
|
|
}
|
|
|
|
if stateObject != nil {
|
|
stateObject = s.mvRecordWritten(stateObject)
|
|
stateObject.SubBalance(amount)
|
|
MVWrite(s, blockstm.NewSubpathKey(addr, BalancePath))
|
|
}
|
|
}
|
|
|
|
func (s *StateDB) SetBalance(addr common.Address, amount *big.Int) {
|
|
stateObject := s.GetOrNewStateObject(addr)
|
|
if stateObject != nil {
|
|
stateObject = s.mvRecordWritten(stateObject)
|
|
stateObject.SetBalance(amount)
|
|
MVWrite(s, blockstm.NewSubpathKey(addr, BalancePath))
|
|
}
|
|
}
|
|
|
|
func (s *StateDB) SetNonce(addr common.Address, nonce uint64) {
|
|
stateObject := s.GetOrNewStateObject(addr)
|
|
if stateObject != nil {
|
|
stateObject = s.mvRecordWritten(stateObject)
|
|
stateObject.SetNonce(nonce)
|
|
MVWrite(s, blockstm.NewSubpathKey(addr, NoncePath))
|
|
}
|
|
}
|
|
|
|
func (s *StateDB) SetCode(addr common.Address, code []byte) {
|
|
stateObject := s.GetOrNewStateObject(addr)
|
|
if stateObject != nil {
|
|
stateObject = s.mvRecordWritten(stateObject)
|
|
stateObject.SetCode(crypto.Keccak256Hash(code), code)
|
|
MVWrite(s, blockstm.NewSubpathKey(addr, CodePath))
|
|
}
|
|
}
|
|
|
|
func (s *StateDB) SetState(addr common.Address, key, value common.Hash) {
|
|
stateObject := s.GetOrNewStateObject(addr)
|
|
if stateObject != nil {
|
|
stateObject = s.mvRecordWritten(stateObject)
|
|
stateObject.SetState(s.db, key, value)
|
|
MVWrite(s, blockstm.NewStateKey(addr, key))
|
|
}
|
|
}
|
|
|
|
// SetStorage replaces the entire storage for the specified account with given
|
|
// storage. This function should only be used for debugging.
|
|
func (s *StateDB) SetStorage(addr common.Address, storage map[common.Hash]common.Hash) {
|
|
// SetStorage needs to wipe existing storage. We achieve this by pretending
|
|
// that the account self-destructed earlier in this block, by flagging
|
|
// it in stateObjectsDestruct. The effect of doing so is that storage lookups
|
|
// will not hit disk, since it is assumed that the disk-data is belonging
|
|
// to a previous incarnation of the object.
|
|
s.stateObjectsDestruct[addr] = struct{}{}
|
|
stateObject := s.GetOrNewStateObject(addr)
|
|
|
|
for k, v := range storage {
|
|
stateObject.SetState(s.db, k, v)
|
|
}
|
|
}
|
|
|
|
// Suicide marks the given account as suicided.
|
|
// This clears the account balance.
|
|
//
|
|
// The account's state object is still available until the state is committed,
|
|
// getStateObject will return a non-nil account after Suicide.
|
|
func (s *StateDB) Suicide(addr common.Address) bool {
|
|
stateObject := s.getStateObject(addr)
|
|
if stateObject == nil {
|
|
return false
|
|
}
|
|
|
|
stateObject = s.mvRecordWritten(stateObject)
|
|
s.journal.append(suicideChange{
|
|
account: &addr,
|
|
prev: stateObject.suicided,
|
|
prevbalance: new(big.Int).Set(stateObject.Balance()),
|
|
})
|
|
stateObject.markSuicided()
|
|
stateObject.data.Balance = new(big.Int)
|
|
|
|
MVWrite(s, blockstm.NewSubpathKey(addr, SuicidePath))
|
|
MVWrite(s, blockstm.NewSubpathKey(addr, BalancePath))
|
|
|
|
return true
|
|
}
|
|
|
|
// SetTransientState sets transient storage for a given account. It
|
|
// adds the change to the journal so that it can be rolled back
|
|
// to its previous value if there is a revert.
|
|
func (s *StateDB) SetTransientState(addr common.Address, key, value common.Hash) {
|
|
prev := s.GetTransientState(addr, key)
|
|
if prev == value {
|
|
return
|
|
}
|
|
|
|
s.journal.append(transientStorageChange{
|
|
account: &addr,
|
|
key: key,
|
|
prevalue: prev,
|
|
})
|
|
s.setTransientState(addr, key, value)
|
|
}
|
|
|
|
// setTransientState is a lower level setter for transient storage. It
|
|
// is called during a revert to prevent modifications to the journal.
|
|
func (s *StateDB) setTransientState(addr common.Address, key, value common.Hash) {
|
|
s.transientStorage.Set(addr, key, value)
|
|
}
|
|
|
|
// GetTransientState gets transient storage for a given account.
|
|
func (s *StateDB) GetTransientState(addr common.Address, key common.Hash) common.Hash {
|
|
return s.transientStorage.Get(addr, key)
|
|
}
|
|
|
|
//
|
|
// Setting, updating & deleting state object methods.
|
|
//
|
|
|
|
// updateStateObject writes the given object to the trie.
|
|
func (s *StateDB) updateStateObject(obj *stateObject) {
|
|
// Track the amount of time wasted on updating the account from the trie
|
|
if metrics.EnabledExpensive {
|
|
defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now())
|
|
}
|
|
// Encode the account and update the account trie
|
|
addr := obj.Address()
|
|
if err := s.trie.UpdateAccount(addr, &obj.data); err != nil {
|
|
s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err))
|
|
}
|
|
|
|
// If state snapshotting is active, cache the data til commit. Note, this
|
|
// update mechanism is not symmetric to the deletion, because whereas it is
|
|
// enough to track account updates at commit time, deletions need tracking
|
|
// at transaction boundary level to ensure we capture state clearing.
|
|
if s.snap != nil {
|
|
s.snapAccounts[obj.addrHash] = snapshot.SlimAccountRLP(obj.data.Nonce, obj.data.Balance, obj.data.Root, obj.data.CodeHash)
|
|
}
|
|
}
|
|
|
|
// deleteStateObject removes the given object from the state trie.
|
|
func (s *StateDB) deleteStateObject(obj *stateObject) {
|
|
// Track the amount of time wasted on deleting the account from the trie
|
|
if metrics.EnabledExpensive {
|
|
defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now())
|
|
}
|
|
// Delete the account from the trie
|
|
addr := obj.Address()
|
|
if err := s.trie.DeleteAccount(addr); err != nil {
|
|
s.setError(fmt.Errorf("deleteStateObject (%x) error: %v", addr[:], err))
|
|
}
|
|
}
|
|
|
|
// getStateObject retrieves a state object given by the address, returning nil if
|
|
// the object is not found or was deleted in this execution context. If you need
|
|
// to differentiate between non-existent/just-deleted, use getDeletedStateObject.
|
|
func (s *StateDB) getStateObject(addr common.Address) *stateObject {
|
|
if obj := s.getDeletedStateObject(addr); obj != nil && !obj.deleted {
|
|
return obj
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// getDeletedStateObject is similar to getStateObject, but instead of returning
|
|
// nil for a deleted state object, it returns the actual object with the deleted
|
|
// flag set. This is needed by the state journal to revert to the correct s-
|
|
// destructed object instead of wiping all knowledge about the state object.
|
|
func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject {
|
|
return MVRead(s, blockstm.NewAddressKey(addr), nil, func(s *StateDB) *stateObject {
|
|
// Prefer live objects if any is available
|
|
if obj := s.stateObjects[addr]; obj != nil {
|
|
return obj
|
|
}
|
|
// If no live objects are available, attempt to use snapshots
|
|
var data *types.StateAccount
|
|
|
|
if s.snap != nil { // nolint
|
|
start := time.Now()
|
|
acc, err := s.snap.Account(crypto.HashData(crypto.NewKeccakState(), addr.Bytes()))
|
|
|
|
if metrics.EnabledExpensive {
|
|
s.SnapshotAccountReads += time.Since(start)
|
|
}
|
|
|
|
if err == nil {
|
|
if acc == nil {
|
|
return nil
|
|
}
|
|
|
|
data = &types.StateAccount{
|
|
Nonce: acc.Nonce,
|
|
Balance: acc.Balance,
|
|
CodeHash: acc.CodeHash,
|
|
Root: common.BytesToHash(acc.Root),
|
|
}
|
|
if len(data.CodeHash) == 0 {
|
|
data.CodeHash = types.EmptyCodeHash.Bytes()
|
|
}
|
|
|
|
if data.Root == (common.Hash{}) {
|
|
data.Root = types.EmptyRootHash
|
|
}
|
|
}
|
|
}
|
|
// If snapshot unavailable or reading from it failed, load from the database
|
|
if data == nil {
|
|
start := time.Now()
|
|
|
|
var err error
|
|
data, err = s.trie.GetAccount(addr)
|
|
|
|
if metrics.EnabledExpensive {
|
|
s.AccountReads += time.Since(start)
|
|
}
|
|
|
|
if err != nil {
|
|
s.setError(fmt.Errorf("getDeleteStateObject (%x) error: %w", addr.Bytes(), err))
|
|
return nil
|
|
}
|
|
|
|
if data == nil {
|
|
return nil
|
|
}
|
|
}
|
|
// Insert into the live set
|
|
obj := newObject(s, addr, *data)
|
|
s.setStateObject(obj)
|
|
|
|
return obj
|
|
})
|
|
}
|
|
|
|
func (s *StateDB) setStateObject(object *stateObject) {
|
|
s.stateObjects[object.Address()] = object
|
|
}
|
|
|
|
// GetOrNewStateObject retrieves a state object or create a new state object if nil.
|
|
func (s *StateDB) GetOrNewStateObject(addr common.Address) *stateObject {
|
|
stateObject := s.getStateObject(addr)
|
|
if stateObject == nil {
|
|
stateObject, _ = s.createObject(addr)
|
|
}
|
|
|
|
return stateObject
|
|
}
|
|
|
|
// mvRecordWritten checks whether a state object is already present in the current MV writeMap.
|
|
// If yes, it returns the object directly.
|
|
// If not, it clones the object and inserts it into the writeMap before returning it.
|
|
func (s *StateDB) mvRecordWritten(object *stateObject) *stateObject {
|
|
if s.mvHashmap == nil {
|
|
return object
|
|
}
|
|
|
|
addrKey := blockstm.NewAddressKey(object.Address())
|
|
|
|
if MVWritten(s, addrKey) {
|
|
return object
|
|
}
|
|
|
|
// Deepcopy is needed to ensure that objects are not written by multiple transactions at the same time, because
|
|
// the input state object can come from a different transaction.
|
|
s.setStateObject(object.deepCopy(s))
|
|
MVWrite(s, addrKey)
|
|
|
|
return s.stateObjects[object.Address()]
|
|
}
|
|
|
|
// createObject creates a new state object. If there is an existing account with
|
|
// the given address, it is overwritten and returned as the second return value.
|
|
func (s *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) {
|
|
prev = s.getDeletedStateObject(addr) // Note, prev might have been deleted, we need that!
|
|
|
|
var prevdestruct bool
|
|
if prev != nil {
|
|
_, prevdestruct = s.stateObjectsDestruct[prev.address]
|
|
if !prevdestruct {
|
|
s.stateObjectsDestruct[prev.address] = struct{}{}
|
|
}
|
|
}
|
|
|
|
newobj = newObject(s, addr, types.StateAccount{})
|
|
|
|
if prev == nil {
|
|
s.journal.append(createObjectChange{account: &addr})
|
|
} else {
|
|
s.journal.append(resetObjectChange{prev: prev, prevdestruct: prevdestruct})
|
|
}
|
|
|
|
s.setStateObject(newobj)
|
|
|
|
MVWrite(s, blockstm.NewAddressKey(addr))
|
|
|
|
if prev != nil && !prev.deleted {
|
|
return newobj, prev
|
|
}
|
|
|
|
return newobj, nil
|
|
}
|
|
|
|
// CreateAccount explicitly creates a state object. If a state object with the address
|
|
// already exists the balance is carried over to the new account.
|
|
//
|
|
// CreateAccount is called during the EVM CREATE operation. The situation might arise that
|
|
// a contract does the following:
|
|
//
|
|
// 1. sends funds to sha(account ++ (nonce + 1))
|
|
// 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1)
|
|
//
|
|
// Carrying over the balance ensures that Ether doesn't disappear.
|
|
func (s *StateDB) CreateAccount(addr common.Address) {
|
|
newObj, prev := s.createObject(addr)
|
|
if prev != nil {
|
|
newObj.setBalance(prev.data.Balance)
|
|
MVWrite(s, blockstm.NewSubpathKey(addr, BalancePath))
|
|
}
|
|
}
|
|
|
|
func (s *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) error {
|
|
so := s.getStateObject(addr)
|
|
if so == nil {
|
|
return nil
|
|
}
|
|
|
|
tr, err := so.getTrie(s.db)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
it := trie.NewIterator(tr.NodeIterator(nil))
|
|
|
|
for it.Next() {
|
|
key := common.BytesToHash(s.trie.GetKey(it.Key))
|
|
if value, dirty := so.dirtyStorage[key]; dirty {
|
|
if !cb(key, value) {
|
|
return nil
|
|
}
|
|
|
|
continue
|
|
}
|
|
|
|
if len(it.Value) > 0 {
|
|
_, content, _, err := rlp.Split(it.Value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !cb(key, common.BytesToHash(content)) {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Copy creates a deep, independent copy of the state.
|
|
// Snapshots of the copied state cannot be applied to the copy.
|
|
func (s *StateDB) Copy() *StateDB {
|
|
// Copy all the basic fields, initialize the memory ones
|
|
state := &StateDB{
|
|
db: s.db,
|
|
trie: s.db.CopyTrie(s.trie),
|
|
originalRoot: s.originalRoot,
|
|
stateObjects: make(map[common.Address]*stateObject, len(s.journal.dirties)),
|
|
stateObjectsPending: make(map[common.Address]struct{}, len(s.stateObjectsPending)),
|
|
stateObjectsDirty: make(map[common.Address]struct{}, len(s.journal.dirties)),
|
|
stateObjectsDestruct: make(map[common.Address]struct{}, len(s.stateObjectsDestruct)),
|
|
revertedKeys: make(map[blockstm.Key]struct{}),
|
|
refund: s.refund,
|
|
logs: make(map[common.Hash][]*types.Log, len(s.logs)),
|
|
logSize: s.logSize,
|
|
preimages: make(map[common.Hash][]byte, len(s.preimages)),
|
|
journal: newJournal(),
|
|
hasher: crypto.NewKeccakState(),
|
|
}
|
|
// Copy the dirty states, logs, and preimages
|
|
for addr := range s.journal.dirties {
|
|
// As documented [here](https://github.com/ethereum/go-ethereum/pull/16485#issuecomment-380438527),
|
|
// and in the Finalise-method, there is a case where an object is in the journal but not
|
|
// in the stateObjects: OOG after touch on ripeMD prior to Byzantium. Thus, we need to check for
|
|
// nil
|
|
if object, exist := s.stateObjects[addr]; exist {
|
|
// Even though the original object is dirty, we are not copying the journal,
|
|
// so we need to make sure that any side-effect the journal would have caused
|
|
// during a commit (or similar op) is already applied to the copy.
|
|
state.stateObjects[addr] = object.deepCopy(state)
|
|
|
|
state.stateObjectsDirty[addr] = struct{}{} // Mark the copy dirty to force internal (code/state) commits
|
|
state.stateObjectsPending[addr] = struct{}{} // Mark the copy pending to force external (account) commits
|
|
}
|
|
}
|
|
// Above, we don't copy the actual journal. This means that if the copy
|
|
// is copied, the loop above will be a no-op, since the copy's journal
|
|
// is empty. Thus, here we iterate over stateObjects, to enable copies
|
|
// of copies.
|
|
for addr := range s.stateObjectsPending {
|
|
if _, exist := state.stateObjects[addr]; !exist {
|
|
state.stateObjects[addr] = s.stateObjects[addr].deepCopy(state)
|
|
}
|
|
|
|
state.stateObjectsPending[addr] = struct{}{}
|
|
}
|
|
|
|
for addr := range s.stateObjectsDirty {
|
|
if _, exist := state.stateObjects[addr]; !exist {
|
|
state.stateObjects[addr] = s.stateObjects[addr].deepCopy(state)
|
|
}
|
|
|
|
state.stateObjectsDirty[addr] = struct{}{}
|
|
}
|
|
// Deep copy the destruction flag.
|
|
for addr := range s.stateObjectsDestruct {
|
|
state.stateObjectsDestruct[addr] = struct{}{}
|
|
}
|
|
|
|
for hash, logs := range s.logs {
|
|
cpy := make([]*types.Log, len(logs))
|
|
for i, l := range logs {
|
|
cpy[i] = new(types.Log)
|
|
*cpy[i] = *l
|
|
}
|
|
|
|
state.logs[hash] = cpy
|
|
}
|
|
|
|
for hash, preimage := range s.preimages {
|
|
state.preimages[hash] = preimage
|
|
}
|
|
// Do we need to copy the access list and transient storage?
|
|
// In practice: No. At the start of a transaction, these two lists are empty.
|
|
// In practice, we only ever copy state _between_ transactions/blocks, never
|
|
// in the middle of a transaction. However, it doesn't cost us much to copy
|
|
// empty lists, so we do it anyway to not blow up if we ever decide copy them
|
|
// in the middle of a transaction.
|
|
state.accessList = s.accessList.Copy()
|
|
state.transientStorage = s.transientStorage.Copy()
|
|
|
|
// If there's a prefetcher running, make an inactive copy of it that can
|
|
// only access data but does not actively preload (since the user will not
|
|
// know that they need to explicitly terminate an active copy).
|
|
if s.prefetcher != nil {
|
|
state.prefetcher = s.prefetcher.copy()
|
|
}
|
|
|
|
if s.snaps != nil {
|
|
// In order for the miner to be able to use and make additions
|
|
// to the snapshot tree, we need to copy that as well.
|
|
// Otherwise, any block mined by ourselves will cause gaps in the tree,
|
|
// and force the miner to operate trie-backed only
|
|
state.snaps = s.snaps
|
|
state.snap = s.snap
|
|
|
|
// deep copy needed
|
|
state.snapAccounts = make(map[common.Hash][]byte)
|
|
for k, v := range s.snapAccounts {
|
|
state.snapAccounts[k] = v
|
|
}
|
|
|
|
state.snapStorage = make(map[common.Hash]map[common.Hash][]byte)
|
|
|
|
for k, v := range s.snapStorage {
|
|
temp := make(map[common.Hash][]byte)
|
|
for kk, vv := range v {
|
|
temp[kk] = vv
|
|
}
|
|
|
|
state.snapStorage[k] = temp
|
|
}
|
|
}
|
|
|
|
if s.mvHashmap != nil {
|
|
state.mvHashmap = s.mvHashmap
|
|
}
|
|
|
|
return state
|
|
}
|
|
|
|
// Snapshot returns an identifier for the current revision of the state.
|
|
func (s *StateDB) Snapshot() int {
|
|
id := s.nextRevisionId
|
|
s.nextRevisionId++
|
|
s.validRevisions = append(s.validRevisions, revision{id, s.journal.length()})
|
|
|
|
return id
|
|
}
|
|
|
|
// RevertToSnapshot reverts all state changes made since the given revision.
|
|
func (s *StateDB) RevertToSnapshot(revid int) {
|
|
// Find the snapshot in the stack of valid snapshots.
|
|
idx := sort.Search(len(s.validRevisions), func(i int) bool {
|
|
return s.validRevisions[i].id >= revid
|
|
})
|
|
if idx == len(s.validRevisions) || s.validRevisions[idx].id != revid {
|
|
panic(fmt.Errorf("revision id %v cannot be reverted", revid))
|
|
}
|
|
|
|
snapshot := s.validRevisions[idx].journalIndex
|
|
|
|
// Replay the journal to undo changes and remove invalidated snapshots
|
|
s.journal.revert(s, snapshot)
|
|
s.validRevisions = s.validRevisions[:idx]
|
|
}
|
|
|
|
// GetRefund returns the current value of the refund counter.
|
|
func (s *StateDB) GetRefund() uint64 {
|
|
return s.refund
|
|
}
|
|
|
|
// Finalise finalises the state by removing the destructed objects and clears
|
|
// the journal as well as the refunds. Finalise, however, will not push any updates
|
|
// into the tries just yet. Only IntermediateRoot or Commit will do that.
|
|
func (s *StateDB) Finalise(deleteEmptyObjects bool) {
|
|
addressesToPrefetch := make([][]byte, 0, len(s.journal.dirties))
|
|
|
|
for addr := range s.journal.dirties {
|
|
obj, exist := s.stateObjects[addr]
|
|
if !exist {
|
|
// ripeMD is 'touched' at block 1714175, in tx 0x1237f737031e40bcde4a8b7e717b2d15e3ecadfe49bb1bbc71ee9deb09c6fcf2
|
|
// That tx goes out of gas, and although the notion of 'touched' does not exist there, the
|
|
// touch-event will still be recorded in the journal. Since ripeMD is a special snowflake,
|
|
// it will persist in the journal even though the journal is reverted. In this special circumstance,
|
|
// it may exist in `s.journal.dirties` but not in `s.stateObjects`.
|
|
// Thus, we can safely ignore it here
|
|
continue
|
|
}
|
|
|
|
if obj.suicided || (deleteEmptyObjects && obj.empty()) {
|
|
obj.deleted = true
|
|
|
|
// We need to maintain account deletions explicitly (will remain
|
|
// set indefinitely).
|
|
s.stateObjectsDestruct[obj.address] = struct{}{}
|
|
|
|
// If state snapshotting is active, also mark the destruction there.
|
|
// Note, we can't do this only at the end of a block because multiple
|
|
// transactions within the same block might self destruct and then
|
|
// resurrect an account; but the snapshotter needs both events.
|
|
if s.snap != nil {
|
|
delete(s.snapAccounts, obj.addrHash) // Clear out any previously updated account data (may be recreated via a resurrect)
|
|
delete(s.snapStorage, obj.addrHash) // Clear out any previously updated storage data (may be recreated via a resurrect)
|
|
}
|
|
} else {
|
|
obj.finalise(true) // Prefetch slots in the background
|
|
}
|
|
|
|
s.stateObjectsPending[addr] = struct{}{}
|
|
s.stateObjectsDirty[addr] = struct{}{}
|
|
|
|
// At this point, also ship the address off to the precacher. The precacher
|
|
// will start loading tries, and when the change is eventually committed,
|
|
// the commit-phase will be a lot faster
|
|
addressesToPrefetch = append(addressesToPrefetch, common.CopyBytes(addr[:])) // Copy needed for closure
|
|
}
|
|
|
|
if s.prefetcher != nil && len(addressesToPrefetch) > 0 {
|
|
s.prefetcher.prefetch(common.Hash{}, s.originalRoot, common.Address{}, addressesToPrefetch)
|
|
}
|
|
// Invalidate journal because reverting across transactions is not allowed.
|
|
s.clearJournalAndRefund()
|
|
}
|
|
|
|
// IntermediateRoot computes the current root hash of the state trie.
|
|
// It is called in between transactions to get the root hash that
|
|
// goes into transaction receipts.
|
|
func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
|
|
// Finalise all the dirty storage states and write them into the tries
|
|
s.Finalise(deleteEmptyObjects)
|
|
|
|
// If there was a trie prefetcher operating, it gets aborted and irrevocably
|
|
// modified after we start retrieving tries. Remove it from the statedb after
|
|
// this round of use.
|
|
//
|
|
// This is weird pre-byzantium since the first tx runs with a prefetcher and
|
|
// the remainder without, but pre-byzantium even the initial prefetcher is
|
|
// useless, so no sleep lost.
|
|
prefetcher := s.prefetcher
|
|
if s.prefetcher != nil {
|
|
defer func() {
|
|
s.prefetcher.close()
|
|
s.prefetcher = nil
|
|
}()
|
|
}
|
|
// Although naively it makes sense to retrieve the account trie and then do
|
|
// the contract storage and account updates sequentially, that short circuits
|
|
// the account prefetcher. Instead, let's process all the storage updates
|
|
// first, giving the account prefetches just a few more milliseconds of time
|
|
// to pull useful data from disk.
|
|
for addr := range s.stateObjectsPending {
|
|
if obj := s.stateObjects[addr]; !obj.deleted {
|
|
obj.updateRoot(s.db)
|
|
}
|
|
}
|
|
// Now we're about to start to write changes to the trie. The trie is so far
|
|
// _untouched_. We can check with the prefetcher, if it can give us a trie
|
|
// which has the same root, but also has some content loaded into it.
|
|
if prefetcher != nil {
|
|
if trie := prefetcher.trie(common.Hash{}, s.originalRoot); trie != nil {
|
|
s.trie = trie
|
|
}
|
|
}
|
|
|
|
usedAddrs := make([][]byte, 0, len(s.stateObjectsPending))
|
|
|
|
for addr := range s.stateObjectsPending {
|
|
if obj := s.stateObjects[addr]; obj.deleted {
|
|
s.deleteStateObject(obj)
|
|
s.AccountDeleted += 1
|
|
} else {
|
|
s.updateStateObject(obj)
|
|
s.AccountUpdated += 1
|
|
}
|
|
|
|
usedAddrs = append(usedAddrs, common.CopyBytes(addr[:])) // Copy needed for closure
|
|
}
|
|
|
|
if prefetcher != nil {
|
|
prefetcher.used(common.Hash{}, s.originalRoot, usedAddrs)
|
|
}
|
|
|
|
if len(s.stateObjectsPending) > 0 {
|
|
s.stateObjectsPending = make(map[common.Address]struct{})
|
|
}
|
|
// Track the amount of time wasted on hashing the account trie
|
|
if metrics.EnabledExpensive {
|
|
defer func(start time.Time) { s.AccountHashes += time.Since(start) }(time.Now())
|
|
}
|
|
|
|
return s.trie.Hash()
|
|
}
|
|
|
|
// SetTxContext sets the current transaction hash and index which are
|
|
// used when the EVM emits new state logs. It should be invoked before
|
|
// transaction execution.
|
|
func (s *StateDB) SetTxContext(thash common.Hash, ti int) {
|
|
s.thash = thash
|
|
s.txIndex = ti
|
|
}
|
|
|
|
func (s *StateDB) clearJournalAndRefund() {
|
|
if len(s.journal.entries) > 0 {
|
|
s.journal = newJournal()
|
|
s.refund = 0
|
|
}
|
|
|
|
s.validRevisions = s.validRevisions[:0] // Snapshots can be created without journal entries
|
|
}
|
|
|
|
// Commit writes the state to the underlying in-memory trie database.
|
|
func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
|
|
// Short circuit in case any database failure occurred earlier.
|
|
if s.dbErr != nil {
|
|
return common.Hash{}, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr)
|
|
}
|
|
// Finalize any pending changes and merge everything into the tries
|
|
s.IntermediateRoot(deleteEmptyObjects)
|
|
|
|
// Commit objects to the trie, measuring the elapsed time
|
|
var (
|
|
accountTrieNodesUpdated int
|
|
accountTrieNodesDeleted int
|
|
storageTrieNodesUpdated int
|
|
storageTrieNodesDeleted int
|
|
nodes = trie.NewMergedNodeSet()
|
|
codeWriter = s.db.DiskDB().NewBatch()
|
|
)
|
|
|
|
for addr := range s.stateObjectsDirty {
|
|
if obj := s.stateObjects[addr]; !obj.deleted {
|
|
// Write any contract code associated with the state object
|
|
if obj.code != nil && obj.dirtyCode {
|
|
rawdb.WriteCode(codeWriter, common.BytesToHash(obj.CodeHash()), obj.code)
|
|
obj.dirtyCode = false
|
|
}
|
|
// Write any storage changes in the state object to its storage trie
|
|
set, err := obj.commitTrie(s.db)
|
|
if err != nil {
|
|
return common.Hash{}, err
|
|
}
|
|
// Merge the dirty nodes of storage trie into global set
|
|
if set != nil {
|
|
if err := nodes.Merge(set); err != nil {
|
|
return common.Hash{}, err
|
|
}
|
|
|
|
updates, deleted := set.Size()
|
|
storageTrieNodesUpdated += updates
|
|
storageTrieNodesDeleted += deleted
|
|
}
|
|
}
|
|
// If the contract is destructed, the storage is still left in the
|
|
// database as dangling data. Theoretically it's should be wiped from
|
|
// database as well, but in hash-based-scheme it's extremely hard to
|
|
// determine that if the trie nodes are also referenced by other storage,
|
|
// and in path-based-scheme some technical challenges are still unsolved.
|
|
// Although it won't affect the correctness but please fix it TODO(rjl493456442).
|
|
}
|
|
|
|
if len(s.stateObjectsDirty) > 0 {
|
|
s.stateObjectsDirty = make(map[common.Address]struct{})
|
|
}
|
|
|
|
if codeWriter.ValueSize() > 0 {
|
|
if err := codeWriter.Write(); err != nil {
|
|
log.Crit("Failed to commit dirty codes", "error", err)
|
|
}
|
|
}
|
|
// Write the account trie changes, measuring the amount of wasted time
|
|
var start time.Time
|
|
if metrics.EnabledExpensive {
|
|
start = time.Now()
|
|
}
|
|
|
|
root, set := s.trie.Commit(true)
|
|
// Merge the dirty nodes of account trie into global set
|
|
if set != nil {
|
|
if err := nodes.Merge(set); err != nil {
|
|
return common.Hash{}, err
|
|
}
|
|
|
|
accountTrieNodesUpdated, accountTrieNodesDeleted = set.Size()
|
|
}
|
|
|
|
if metrics.EnabledExpensive {
|
|
s.AccountCommits += time.Since(start)
|
|
|
|
accountUpdatedMeter.Mark(int64(s.AccountUpdated))
|
|
storageUpdatedMeter.Mark(int64(s.StorageUpdated))
|
|
accountDeletedMeter.Mark(int64(s.AccountDeleted))
|
|
storageDeletedMeter.Mark(int64(s.StorageDeleted))
|
|
accountTrieUpdatedMeter.Mark(int64(accountTrieNodesUpdated))
|
|
accountTrieDeletedMeter.Mark(int64(accountTrieNodesDeleted))
|
|
storageTriesUpdatedMeter.Mark(int64(storageTrieNodesUpdated))
|
|
storageTriesDeletedMeter.Mark(int64(storageTrieNodesDeleted))
|
|
|
|
s.AccountUpdated, s.AccountDeleted = 0, 0
|
|
s.StorageUpdated, s.StorageDeleted = 0, 0
|
|
}
|
|
// If snapshotting is enabled, update the snapshot tree with this new version
|
|
if s.snap != nil {
|
|
start := time.Now()
|
|
// Only update if there's a state transition (skip empty Clique blocks)
|
|
if parent := s.snap.Root(); parent != root {
|
|
if err := s.snaps.Update(root, parent, s.convertAccountSet(s.stateObjectsDestruct), s.snapAccounts, s.snapStorage); err != nil {
|
|
log.Warn("Failed to update snapshot tree", "from", parent, "to", root, "err", err)
|
|
}
|
|
// Keep 128 diff layers in the memory, persistent layer is 129th.
|
|
// - head layer is paired with HEAD state
|
|
// - head-1 layer is paired with HEAD-1 state
|
|
// - head-127 layer(bottom-most diff layer) is paired with HEAD-127 state
|
|
if err := s.snaps.Cap(root, 128); err != nil {
|
|
log.Warn("Failed to cap snapshot tree", "root", root, "layers", 128, "err", err)
|
|
}
|
|
}
|
|
|
|
if metrics.EnabledExpensive {
|
|
s.SnapshotCommits += time.Since(start)
|
|
}
|
|
|
|
s.snap, s.snapAccounts, s.snapStorage = nil, nil, nil
|
|
}
|
|
|
|
if len(s.stateObjectsDestruct) > 0 {
|
|
s.stateObjectsDestruct = make(map[common.Address]struct{})
|
|
}
|
|
|
|
if root == (common.Hash{}) {
|
|
root = types.EmptyRootHash
|
|
}
|
|
|
|
origin := s.originalRoot
|
|
|
|
if origin == (common.Hash{}) {
|
|
origin = types.EmptyRootHash
|
|
}
|
|
|
|
if root != origin {
|
|
start := time.Now()
|
|
|
|
if err := s.db.TrieDB().Update(nodes); err != nil {
|
|
return common.Hash{}, err
|
|
}
|
|
|
|
s.originalRoot = root
|
|
|
|
if metrics.EnabledExpensive {
|
|
s.TrieDBCommits += time.Since(start)
|
|
}
|
|
}
|
|
|
|
return root, nil
|
|
}
|
|
|
|
// Prepare handles the preparatory steps for executing a state transition with.
|
|
// This method must be invoked before state transition.
|
|
//
|
|
// Berlin fork:
|
|
// - Add sender to access list (2929)
|
|
// - Add destination to access list (2929)
|
|
// - Add precompiles to access list (2929)
|
|
// - Add the contents of the optional tx access list (2930)
|
|
//
|
|
// Potential EIPs:
|
|
// - Reset access list (Berlin)
|
|
// - Add coinbase to access list (EIP-3651)
|
|
// - Reset transient storage (EIP-1153)
|
|
func (s *StateDB) Prepare(rules params.Rules, sender, coinbase common.Address, dst *common.Address, precompiles []common.Address, list types.AccessList) {
|
|
if rules.IsBerlin {
|
|
// Clear out any leftover from previous executions
|
|
al := newAccessList()
|
|
s.accessList = al
|
|
|
|
al.AddAddress(sender)
|
|
|
|
if dst != nil {
|
|
// If it's a create-tx, the destination will be added inside evm.create
|
|
al.AddAddress(*dst)
|
|
}
|
|
|
|
for _, addr := range precompiles {
|
|
al.AddAddress(addr)
|
|
}
|
|
|
|
for _, el := range list {
|
|
al.AddAddress(el.Address)
|
|
|
|
for _, key := range el.StorageKeys {
|
|
al.AddSlot(el.Address, key)
|
|
}
|
|
}
|
|
// TODO marcello double check
|
|
if rules.IsShanghai { // EIP-3651: warm coinbase
|
|
al.AddAddress(coinbase)
|
|
}
|
|
}
|
|
// Reset transient storage at the beginning of transaction execution
|
|
s.transientStorage = newTransientStorage()
|
|
}
|
|
|
|
// AddAddressToAccessList adds the given address to the access list
|
|
func (s *StateDB) AddAddressToAccessList(addr common.Address) {
|
|
if s.accessList.AddAddress(addr) {
|
|
s.journal.append(accessListAddAccountChange{&addr})
|
|
}
|
|
}
|
|
|
|
// AddSlotToAccessList adds the given (address, slot)-tuple to the access list
|
|
func (s *StateDB) AddSlotToAccessList(addr common.Address, slot common.Hash) {
|
|
addrMod, slotMod := s.accessList.AddSlot(addr, slot)
|
|
if addrMod {
|
|
// In practice, this should not happen, since there is no way to enter the
|
|
// scope of 'address' without having the 'address' become already added
|
|
// to the access list (via call-variant, create, etc).
|
|
// Better safe than sorry, though
|
|
s.journal.append(accessListAddAccountChange{&addr})
|
|
}
|
|
|
|
if slotMod {
|
|
s.journal.append(accessListAddSlotChange{
|
|
address: &addr,
|
|
slot: &slot,
|
|
})
|
|
}
|
|
}
|
|
|
|
// AddressInAccessList returns true if the given address is in the access list.
|
|
func (s *StateDB) AddressInAccessList(addr common.Address) bool {
|
|
return s.accessList.ContainsAddress(addr)
|
|
}
|
|
|
|
// SlotInAccessList returns true if the given (address, slot)-tuple is in the access list.
|
|
func (s *StateDB) SlotInAccessList(addr common.Address, slot common.Hash) (addressPresent bool, slotPresent bool) {
|
|
return s.accessList.Contains(addr, slot)
|
|
}
|
|
|
|
// convertAccountSet converts a provided account set from address keyed to hash keyed.
|
|
func (s *StateDB) convertAccountSet(set map[common.Address]struct{}) map[common.Hash]struct{} {
|
|
ret := make(map[common.Hash]struct{})
|
|
|
|
for addr := range set {
|
|
obj, exist := s.stateObjects[addr]
|
|
if !exist {
|
|
ret[crypto.Keccak256Hash(addr[:])] = struct{}{}
|
|
} else {
|
|
ret[obj.addrHash] = struct{}{}
|
|
}
|
|
}
|
|
|
|
return ret
|
|
}
|