Skip to content

Commit 9afb18d

Browse files
authored
core: add code to witness when state object is accessed (#30698)
I think the core code should generally be agnostic about the witness and the statedb layer should determine what elements need to be included in the witness. Because code is accessed via `GetCode`, and `GetCodeLength`, the statedb will always know when it needs to add that code into the witness. The edge case is block hashes, so we continue to add them manually in the implementation of `BLOCKHASH`. It probably makes sense to refactor statedb so we have a wrapped implementation that accumulates the witness, but this is a simpler change that makes #30078 less aggressive.
1 parent 25bc077 commit 9afb18d

File tree

3 files changed

+6
-19
lines changed

3 files changed

+6
-19
lines changed

core/state/statedb.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ func (s *StateDB) TxIndex() int {
341341
func (s *StateDB) GetCode(addr common.Address) []byte {
342342
stateObject := s.getStateObject(addr)
343343
if stateObject != nil {
344+
if s.witness != nil {
345+
s.witness.AddCode(stateObject.Code())
346+
}
344347
return stateObject.Code()
345348
}
346349
return nil
@@ -349,6 +352,9 @@ func (s *StateDB) GetCode(addr common.Address) []byte {
349352
func (s *StateDB) GetCodeSize(addr common.Address) int {
350353
stateObject := s.getStateObject(addr)
351354
if stateObject != nil {
355+
if s.witness != nil {
356+
s.witness.AddCode(stateObject.Code())
357+
}
352358
return stateObject.CodeSize()
353359
}
354360
return 0

core/vm/evm.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,6 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
213213
// Initialise a new contract and set the code that is to be used by the EVM.
214214
// The contract is a scoped environment for this execution context only.
215215
code := evm.StateDB.GetCode(addr)
216-
if witness := evm.StateDB.Witness(); witness != nil {
217-
witness.AddCode(code)
218-
}
219216
if len(code) == 0 {
220217
ret, err = nil, nil // gas is unchanged
221218
} else {
@@ -283,9 +280,6 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
283280
// Initialise a new contract and set the code that is to be used by the EVM.
284281
// The contract is a scoped environment for this execution context only.
285282
contract := NewContract(caller, AccountRef(caller.Address()), value, gas)
286-
if witness := evm.StateDB.Witness(); witness != nil {
287-
witness.AddCode(evm.StateDB.GetCode(addrCopy))
288-
}
289283
contract.SetCallCode(&addrCopy, evm.StateDB.GetCodeHash(addrCopy), evm.StateDB.GetCode(addrCopy))
290284
ret, err = evm.interpreter.Run(contract, input, false)
291285
gas = contract.Gas
@@ -333,9 +327,6 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
333327
addrCopy := addr
334328
// Initialise a new contract and make initialise the delegate values
335329
contract := NewContract(caller, AccountRef(caller.Address()), nil, gas).AsDelegate()
336-
if witness := evm.StateDB.Witness(); witness != nil {
337-
witness.AddCode(evm.StateDB.GetCode(addrCopy))
338-
}
339330
contract.SetCallCode(&addrCopy, evm.StateDB.GetCodeHash(addrCopy), evm.StateDB.GetCode(addrCopy))
340331
ret, err = evm.interpreter.Run(contract, input, false)
341332
gas = contract.Gas
@@ -391,9 +382,6 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
391382
// Initialise a new contract and set the code that is to be used by the EVM.
392383
// The contract is a scoped environment for this execution context only.
393384
contract := NewContract(caller, AccountRef(addrCopy), new(uint256.Int), gas)
394-
if witness := evm.StateDB.Witness(); witness != nil {
395-
witness.AddCode(evm.StateDB.GetCode(addrCopy))
396-
}
397385
contract.SetCallCode(&addrCopy, evm.StateDB.GetCodeHash(addrCopy), evm.StateDB.GetCode(addrCopy))
398386
// When an error was returned by the EVM or when setting the creation code
399387
// above we revert to the snapshot and consume any gas remaining. Additionally

core/vm/instructions.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,6 @@ func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeConte
340340

341341
func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
342342
slot := scope.Stack.peek()
343-
address := slot.Bytes20()
344-
if witness := interpreter.evm.StateDB.Witness(); witness != nil {
345-
witness.AddCode(interpreter.evm.StateDB.GetCode(address))
346-
}
347343
slot.SetUint64(uint64(interpreter.evm.StateDB.GetCodeSize(slot.Bytes20())))
348344
return nil, nil
349345
}
@@ -383,9 +379,6 @@ func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext)
383379
}
384380
addr := common.Address(a.Bytes20())
385381
code := interpreter.evm.StateDB.GetCode(addr)
386-
if witness := interpreter.evm.StateDB.Witness(); witness != nil {
387-
witness.AddCode(code)
388-
}
389382
codeCopy := getData(code, uint64CodeOffset, length.Uint64())
390383
scope.Memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
391384

0 commit comments

Comments
 (0)