|
21 | 21 | package core |
22 | 22 |
|
23 | 23 | import ( |
| 24 | + "context" |
24 | 25 | "errors" |
| 26 | + "fmt" |
25 | 27 |
|
26 | 28 | "github.com/berachain/polaris/eth/core/state" |
27 | 29 |
|
28 | 30 | "github.com/ethereum/go-ethereum/common" |
| 31 | + gethcore "github.com/ethereum/go-ethereum/core" |
| 32 | + ethtypes "github.com/ethereum/go-ethereum/core/types" |
29 | 33 | "github.com/ethereum/go-ethereum/core/vm" |
| 34 | + "github.com/ethereum/go-ethereum/eth/tracers" |
30 | 35 | "github.com/ethereum/go-ethereum/params" |
31 | 36 | ) |
32 | 37 |
|
33 | 38 | // ChainResources is the interface that defines functions for code paths within the chain to |
34 | 39 | // acquire resources to use in execution such as StateDBss and EVMss. |
35 | 40 | type ChainResources interface { |
| 41 | + // state snapshots |
36 | 42 | StateAtBlockNumber(uint64) (state.StateDB, error) |
37 | 43 | StateAt(root common.Hash) (state.StateDB, error) |
| 44 | + |
| 45 | + // state for tracing |
| 46 | + StateAtBlock( |
| 47 | + _ context.Context, block *ethtypes.Block, _ uint64, |
| 48 | + _ state.StateDB, _ bool, _ bool, |
| 49 | + ) (state.StateDB, tracers.StateReleaseFunc, error) |
| 50 | + StateAtTransaction( |
| 51 | + ctx context.Context, block *ethtypes.Block, |
| 52 | + txIndex int, reexec uint64, |
| 53 | + ) (*gethcore.Message, vm.BlockContext, state.StateDB, tracers.StateReleaseFunc, error) |
| 54 | + |
| 55 | + // vm/chain config |
38 | 56 | GetVMConfig() *vm.Config |
39 | 57 | Config() *params.ChainConfig |
40 | 58 | } |
@@ -76,7 +94,103 @@ func (bc *blockchain) HasBlockAndState(hash common.Hash, number uint64) bool { |
76 | 94 | return true |
77 | 95 | } |
78 | 96 |
|
| 97 | +// StateAtBlock retrieves the state database associated with a certain block. |
| 98 | +// If no state is locally available for the given block, a number of blocks |
| 99 | +// are attempted to be reexecuted to generate the desired state. The optional |
| 100 | +// base layer statedb can be provided which is regarded as the statedb of the |
| 101 | +// parent block. |
| 102 | +// |
| 103 | +// An additional release function will be returned if the requested state is |
| 104 | +// available. Release is expected to be invoked when the returned state is no |
| 105 | +// longer needed. Its purpose is to prevent resource leaking. Though it can be |
| 106 | +// noop in some cases. |
| 107 | +// |
| 108 | +// Parameters: |
| 109 | +// - block: The block for which we want the state(state = block.Root) |
| 110 | +// - reexec: The maximum number of blocks to reprocess trying to obtain the desired state |
| 111 | +// - base: If the caller is tracing multiple blocks, the caller can provide the parent |
| 112 | +// state continuously from the callsite. |
| 113 | +// - readOnly: If true, then the live 'blockchain' state database is used. No mutation should |
| 114 | +// be made from caller, e.g. perform Commit or other 'save-to-disk' changes. |
| 115 | +// Otherwise, the trash generated by caller may be persisted permanently. |
| 116 | +// - preferDisk: This arg can be used by the caller to signal that even though the 'base' is |
| 117 | +// provided, it would be preferable to start from a fresh state, if we have it |
| 118 | +// on disk. |
| 119 | +func (bc *blockchain) StateAtBlock( |
| 120 | + _ context.Context, block *ethtypes.Block, _ uint64, |
| 121 | + _ state.StateDB, _ bool, _ bool, |
| 122 | +) (state.StateDB, tracers.StateReleaseFunc, error) { |
| 123 | + // Check if the requested state is available in the live chain. |
| 124 | + statedb, err := bc.StateAtBlockNumber(block.Number().Uint64()) |
| 125 | + if err != nil { |
| 126 | + // If there is an error, it means the state is not available. |
| 127 | + // TODO: Historic state is not supported in path-based scheme. |
| 128 | + // Fully archive node in pbss will be implemented by relying |
| 129 | + // on state history, but needs more work on top. |
| 130 | + return nil, nil, errors.Join( |
| 131 | + err, errors.New("historical state not available in path scheme yet"), |
| 132 | + ) |
| 133 | + } |
| 134 | + |
| 135 | + // If there is no error, return the state, a no-op function, and no error. |
| 136 | + return statedb, func() {}, nil |
| 137 | +} |
| 138 | + |
| 139 | +// StateAtTransaction returns the execution environment of a certain transaction. |
| 140 | +func (bc *blockchain) StateAtTransaction( |
| 141 | + ctx context.Context, block *ethtypes.Block, |
| 142 | + txIndex int, reexec uint64, |
| 143 | +) (*gethcore.Message, vm.BlockContext, state.StateDB, tracers.StateReleaseFunc, error) { |
| 144 | + // Short circuit if it's genesis block. |
| 145 | + if block.NumberU64() == 0 { |
| 146 | + return nil, vm.BlockContext{}, nil, nil, errors.New("no transaction in genesis") |
| 147 | + } |
| 148 | + // Create the parent state database |
| 149 | + parent := bc.GetBlock(block.ParentHash(), block.NumberU64()-1) |
| 150 | + if parent == nil { |
| 151 | + return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("parent %#x not found", block.ParentHash()) |
| 152 | + } |
| 153 | + // Lookup the statedb of parent block from the live database, |
| 154 | + // otherwise regenerate it on the flight. |
| 155 | + statedb, release, err := bc.StateAtBlock(ctx, parent, reexec, nil, true, false) |
| 156 | + if err != nil { |
| 157 | + return nil, vm.BlockContext{}, nil, nil, err |
| 158 | + } |
| 159 | + if txIndex == 0 && len(block.Transactions()) == 0 { |
| 160 | + return nil, vm.BlockContext{}, statedb, release, nil |
| 161 | + } |
| 162 | + // Recompute transactions up to the target index. |
| 163 | + signer := ethtypes.MakeSigner(bc.Config(), block.Number(), block.Time()) |
| 164 | + for idx, tx := range block.Transactions() { |
| 165 | + // Assemble the transaction call message and return if the requested offset |
| 166 | + msg, _ := gethcore.TransactionToMessage(tx, signer, block.BaseFee()) |
| 167 | + txContext := gethcore.NewEVMTxContext(msg) |
| 168 | + context := gethcore.NewEVMBlockContext(block.Header(), bc, nil) |
| 169 | + if idx == txIndex { |
| 170 | + return msg, context, statedb, release, nil |
| 171 | + } |
| 172 | + // Not yet the searched for transaction, execute on top of the current state |
| 173 | + vmenv := vm.NewEVM(context, txContext, statedb, bc.Config(), vm.Config{}) |
| 174 | + statedb.SetTxContext(tx.Hash(), idx) |
| 175 | + if _, err = gethcore.ApplyMessage(vmenv, |
| 176 | + msg, new(gethcore.GasPool).AddGas(tx.Gas())); err != nil { |
| 177 | + return nil, vm.BlockContext{}, nil, nil, |
| 178 | + fmt.Errorf("transaction %s failed: %w", tx.Hash().Hex(), err) |
| 179 | + } |
| 180 | + // Ensure any modifications are committed to the state |
| 181 | + // Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect |
| 182 | + statedb.Finalise(vmenv.ChainConfig().IsEIP158(block.Number())) |
| 183 | + } |
| 184 | + return nil, vm.BlockContext{}, nil, nil, |
| 185 | + fmt.Errorf("transaction index %d out of range for block %#x", txIndex, block.Hash()) |
| 186 | +} |
| 187 | + |
79 | 188 | // GetVMConfig returns the vm.Config for the current chain. |
80 | 189 | func (bc *blockchain) GetVMConfig() *vm.Config { |
81 | 190 | return bc.vmConfig |
82 | 191 | } |
| 192 | + |
| 193 | +// Config returns the Ethereum chain config from the host chain. |
| 194 | +func (bc *blockchain) Config() *params.ChainConfig { |
| 195 | + return bc.config |
| 196 | +} |
0 commit comments