Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast Call to Non contract addresses #7074

Merged
merged 4 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Evm/CodeAnalysis/CodeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public CodeInfo(ReadOnlyMemory<byte> code)
}

public bool IsPrecompile => Precompile is not null;
public bool IsEmpty => ReferenceEquals(_analyzer, _emptyAnalyzer) && !IsPrecompile;

public CodeInfo(IPrecompile precompile)
{
Expand Down
50 changes: 43 additions & 7 deletions src/Nethermind/Nethermind.Evm/VirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ internal readonly ref struct CallResult
public static CallResult StackUnderflowException => new(EvmExceptionType.StackUnderflow); // TODO: use these to avoid CALL POP attacks
public static CallResult InvalidCodeException => new(EvmExceptionType.InvalidCode);
public static CallResult Empty => new(default, null);
public static object BoxedEmpty { get; } = new object();

public CallResult(EvmState stateToExecute)
{
Expand Down Expand Up @@ -1869,6 +1870,11 @@ private CallResult ExecuteCode<TTracingInstructions, TTracingRefunds, TTracingSt
{
break;
}
if (ReferenceEquals(returnData, CallResult.BoxedEmpty))
{
// Non contract call continue rather than constructing a new frame
continue;
}

goto DataReturn;
}
Expand Down Expand Up @@ -2187,11 +2193,7 @@ private EvmExceptionType InstructionCall<TTracingInstructions, TTracingRefunds>(

if (typeof(TLogger) == typeof(IsTracing))
{
_logger.Trace($"caller {caller}");
_logger.Trace($"code source {codeSource}");
_logger.Trace($"target {target}");
_logger.Trace($"value {callValue}");
_logger.Trace($"transfer value {transferValue}");
TraceCallDetails(codeSource, ref callValue, ref transferValue, caller, target);
}

long gasExtra = 0L;
Expand Down Expand Up @@ -2256,11 +2258,19 @@ private EvmExceptionType InstructionCall<TTracingInstructions, TTracingRefunds>(
return EvmExceptionType.None;
}

ReadOnlyMemory<byte> callData = vmState.Memory.Load(in dataOffset, dataLength);

Snapshot snapshot = _worldState.TakeSnapshot();
_state.SubtractFromBalance(caller, transferValue, spec);

if (codeInfo.IsEmpty && typeof(TTracingInstructions) != typeof(IsTracing) && !_txTracer.IsTracingActions)
{
// Non contract call, no need to construct call frame can just credit balance and return gas
_returnDataBuffer = default;
stack.PushBytes(StatusCode.SuccessBytes.Span);
UpdateGasUp(gasLimitUl, ref gasAvailable);
return FastCall(spec, out returnData, in transferValue, target);
}

ReadOnlyMemory<byte> callData = vmState.Memory.Load(in dataOffset, dataLength);
ExecutionEnvironment callEnv = new
(
txExecutionContext: in env.TxExecutionContext,
Expand Down Expand Up @@ -2296,6 +2306,32 @@ private EvmExceptionType InstructionCall<TTracingInstructions, TTracingRefunds>(
isCreateOnPreExistingAccount: false);

return EvmExceptionType.None;

EvmExceptionType FastCall(IReleaseSpec spec, out object returnData, in UInt256 transferValue, Address target)
{
if (!_state.AccountExists(target))
{
_state.CreateAccount(target, transferValue);
}
else
{
_state.AddToBalance(target, transferValue, spec);
}
Metrics.EmptyCalls++;

returnData = CallResult.BoxedEmpty;
return EvmExceptionType.None;
}

[MethodImpl(MethodImplOptions.NoInlining)]
void TraceCallDetails(Address codeSource, ref UInt256 callValue, ref UInt256 transferValue, Address caller, Address target)
{
_logger.Trace($"caller {caller}");
_logger.Trace($"code source {codeSource}");
_logger.Trace($"target {target}");
_logger.Trace($"value {callValue}");
_logger.Trace($"transfer value {transferValue}");
}
}

[SkipLocalsInit]
Expand Down
Loading