feat(jsonrpc): implement base eth_simulateV1 JSON-RPC method#6785
Open
APshenkin wants to merge 4 commits into
Open
feat(jsonrpc): implement base eth_simulateV1 JSON-RPC method#6785APshenkin wants to merge 4 commits into
APshenkin wants to merge 4 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Implements geth's
eth_simulateV1on java-tron's JSON-RPC surface for the MVP trading-flow use case: a single round-trip that runs N dependent calls against current head state and returns each call's effect plus synthetic transfer logs.The endpoint is opt-in via existing
eth_call-style flags; no existing behaviour changes.Why are these changes required?
This is first step to resolve #6199
Tron's JSON-RPC exposes
eth_callbut noteth_simulateV1. Two concrete consumers benefit:eth_simulateV1lets the wallet run the unsigned transaction against current head state and decode the resulting transfer logs directly into a human-readable diff.Both consumers simulate against current head state only — they don't need historical-block context or state overrides. The current implementation covers those cases end-to-end and is sufficient for what we expect to be the majority of
eth_simulateV1usage on Tron.Future work needed for the remaining use cases (debugging historical txs, what-if analysis with state overrides) requires changes in the archive node to support simulation on a specific block +
stateOverrides/blockOverrides. That's intentionally out of scope here.JSON-RPC surface
blockStateCalls: [{ calls: [...] }]. Multi-block /blockOverrides/stateOverrides→-32602.blockOverridesandstateOverridesare excluded by design — both consumers in the Motivation section simulate against current head state only, and supporting overrides would require the same archive-node plumbing called out as future work (rewinding to a specific block, applying account/storage patches before VM execution). Rejecting them with a clear error is better than silently ignoring them. Hard cap of 32 calls per block — geth's defaults (5000/block, 10000 total) are tuned for general-purpose use; our concrete cases (trading-flow approval+swap+settle, wallet preview of a single user-signed tx that fans out a few internal calls) realistically stay under ~10. Capping at 32 leaves comfortable headroom while bounding per-request memory: at ~10KB of accumulated state per call, the shared root's in-memory cache stays well under ~1MB worst-case (vs ~50MB at 5000). Anything beyond that should either be a separate request or signal misuse.blockNumOrTag: only"latest"and"pending"accepted; both resolve to the head block (Tron has instant finality — no mempool state distinct from latest).traceTransfers: truesynthesizes logs at the ERC-7528 native pseudo-address (0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE), distinguished bytopic[0]:Transfer(address,address,uint256)for native TRX moves.TRC10Transfer(address,address,uint256,uint256)for TRC-10 moves —topic[3]carriestokenIdso consumers can filter logs by asset via topics;datacarriesamount.returnFullTransactions: truereturnsblock.transactionsasTransactionResultobjects (synthetic deterministichash,gasPrice = "0x0",nonce = "0x0",v/r/szero,blockHash = keccak256("sim:" + headHash + ":1")). Defaultfalsereturns hash strings.validation: true— Tron-flavored. Geth's checks (baseFee/gasPrice/nonce) don't apply to Tron, so we pre-check per call: sender account exists and sender balance ≥callValue. Failed pre-check →status: "0x0"witherrorMessage, VM not invoked. Defaultfalsepreserves the existing constant-call permissive behaviour.Tracing
Five hook sites per transfer kind, each firing after the real balance change succeeds:
VMActuator.call()(depth 0)MUtil.transferMUtil.transferTokenVMActuator.create()(depth 0)MUtil.transferMUtil.transferTokenProgram.callToAddress(depth ≥1)addBalancepairaddTokenBalancepairProgram.callToPrecompiledAddress(depth ≥1)addTokenBalancepairProgram.suicide/suicide2MUtil.transfertransferAllTokenWithTracehelper — snapshotassetMapV2beforeMUtil.transferAllToken, emit one entry per non-zero assetDELEGATECALL/CALLCODEare explicitly skipped (no real value transfer even whensenderAddress != contextAddress).A per-frame buffer (
BufferingSimulationTracer) with a unifiedseqcounter interleaves explicitLOGopcodes with both synthetic kinds in emission order. Reverted frames drop their entries;logIndexstill increments through gaps (matches geth'slogtracer.go:128).Implementation
SimulationTracer(enterFrame/exitFrame/revertFrame/onTransfer/onTokenTransfer/onLog). Default implBufferingSimulationTracerowns the frame stack andseqcounter.VMActuatorgets opt-in setters (setInjectedRootRepository,setSimulationTracer). When the injected root is null, the existing fresh-root code path runs unchanged.Programpropagates the tracer into childPrograminstances at every sub-call origination site so nested CALL/CREATE moves are captured.Wallet.simulateConstantContractsis the new entry point. It builds the shared root + per-call child Repositories and shares the per-call execute body with the existingcallConstantContractvia a new privateexecuteOneConstantInternalhelper.SimulateV1Args,SimulateBlock(uses@JsonAnySetterto detect forward-incompatible field names),SimulateCallResult,SimulateBlockResult extends BlockResult. ReusesLogFilterElement,CallArguments,TransactionResult(additive raw-fields constructor for synthetic full-tx output).This PR has been tested by:
Unit
20 tests total:
Unit (
EthSimulateV1ArgsTest, 11 tests)-32602input-validation surface, JSON round-trip ofSimulateBlockResult, non-numerictokenIdrejection. MockedWallet, no chain context.Integration (
EthSimulateV1IntegrationTest, 9 tests,BaseTest+ LevelDB)stateSharingAcrossCalls—set(42)→get()returns 42 in one simulate; on-chain slot unchanged.revertIsolatesPerCall—set(99)→setRevert(123)→get()returns 99.validationRejectsUnactivatedSender—validation: truewith a never-seenfrom→"sender account does not exist".validationRejectsInsufficientBalance—validation: truewithvalue > balance→"insufficient balance for value".createPopulatesContractAddress— CREATE call setscontractAddressto the actual deployed address (read from the VM, not re-synthesized).returnFullTransactionsShape— verifies both response shapes, deterministic hash equality across runs.traceTrc10TopLevelCall— owner sends 50 units of token 1000001 to the pre-deployed contract; verifies all four topics + data; owner on-chain TRC-10 balance unchanged.traceTrc10MixedWithTrx— single call with bothvalue: "0x64"andtokenValue: "0x32"to an "accept-anything" sink contract; both synthetic logs emitted in TRX-first order.buffering_dropsTokenTransferOnRevertFrame— direct buffer exercise:revertFramedrops a bufferedonTokenTransfer.Manual Testing
Launched Nile testnet node with changes and run multiple commands with different setups:
Requests and Responses are long, so hide under spoiler