@ -28,6 +28,7 @@ import java.util.Collection;
import java.util.List ;
import java.util.List ;
import java.util.Optional ;
import java.util.Optional ;
import java.util.Set ;
import java.util.Set ;
import java.util.function.Supplier ;
import org.apache.tuweni.bytes.Bytes ;
import org.apache.tuweni.bytes.Bytes ;
import org.slf4j.Logger ;
import org.slf4j.Logger ;
@ -101,7 +102,17 @@ public class ContractCreationProcessor extends AbstractMessageProcessor {
final MutableAccount contract = frame . getWorldUpdater ( ) . getOrCreate ( contractAddress ) ;
final MutableAccount contract = frame . getWorldUpdater ( ) . getOrCreate ( contractAddress ) ;
long statelessGasCost =
long statelessGasCost =
evm . getGasCalculator ( ) . proofOfAbsenceCost ( frame , contract . getAddress ( ) ) ;
evm . getGasCalculator ( ) . proofOfAbsenceCost ( frame , contract . getAddress ( ) ) ;
if ( handleInsufficientGas (
frame ,
statelessGasCost ,
( ) - >
String . format (
"Not enough gas to cover proof of absence fee for %s: remaining gas = %d < %d = creation fee" ,
frame . getContractAddress ( ) , frame . getRemainingGas ( ) , statelessGasCost ) ) ) {
return ;
}
frame . decrementRemainingGas ( statelessGasCost ) ;
frame . decrementRemainingGas ( statelessGasCost ) ;
if ( accountExists ( contract ) ) {
if ( accountExists ( contract ) ) {
LOG . trace (
LOG . trace (
"Contract creation error: account has already been created for address {}" ,
"Contract creation error: account has already been created for address {}" ,
@ -111,6 +122,20 @@ public class ContractCreationProcessor extends AbstractMessageProcessor {
operationTracer . traceAccountCreationResult (
operationTracer . traceAccountCreationResult (
frame , Optional . of ( ExceptionalHaltReason . ILLEGAL_STATE_CHANGE ) ) ;
frame , Optional . of ( ExceptionalHaltReason . ILLEGAL_STATE_CHANGE ) ) ;
} else {
} else {
final long accountCreationFee =
evm . getGasCalculator ( ) . completedCreateContractGasCost ( frame ) ;
if ( handleInsufficientGas (
frame ,
accountCreationFee ,
( ) - >
String . format (
"Not enough gas to pay the contract creation fee for %s: "
+ "remaining gas = %d < %d = creation fee" ,
frame . getContractAddress ( ) , frame . getRemainingGas ( ) , accountCreationFee ) ) ) {
return ;
}
frame . decrementRemainingGas ( accountCreationFee ) ;
frame . addCreate ( contractAddress ) ;
frame . addCreate ( contractAddress ) ;
contract . incrementBalance ( frame . getValue ( ) ) ;
contract . incrementBalance ( frame . getValue ( ) ) ;
contract . setNonce ( initialContractNonce ) ;
contract . setNonce ( initialContractNonce ) ;
@ -132,47 +157,41 @@ public class ContractCreationProcessor extends AbstractMessageProcessor {
final long depositFee = evm . getGasCalculator ( ) . codeDepositGasCost ( frame , contractCode . size ( ) ) ;
final long depositFee = evm . getGasCalculator ( ) . codeDepositGasCost ( frame , contractCode . size ( ) ) ;
if ( frame . getRemainingGas ( ) < depositFee ) {
if ( handleInsufficientGas (
LOG . trace (
frame ,
"Not enough gas to pay the code deposit fee for {}: "
depositFee ,
+ "remaining gas = {} < {} = deposit fee" ,
( ) - >
frame . getContractAddress ( ) ,
String . format (
frame . getRemainingGas ( ) ,
"Not enough gas to pay the code deposit fee for %s: "
depositFee ) ;
+ "remaining gas = %d < %d = deposit fee" ,
frame . getContractAddress ( ) , frame . getRemainingGas ( ) , depositFee ) ) ) {
if ( requireCodeDepositToSucceed ) {
if ( requireCodeDepositToSucceed ) {
LOG . trace ( "Contract creation error: insufficient funds for code deposit" ) ;
frame . setExceptionalHaltReason ( Optional . of ( ExceptionalHaltReason . INSUFFICIENT_GAS ) ) ;
frame . setState ( MessageFrame . State . EXCEPTIONAL_HALT ) ;
operationTracer . traceAccountCreationResult (
operationTracer . traceAccountCreationResult (
frame , Optional . of ( ExceptionalHaltReason . INSUFFICIENT_GAS ) ) ;
frame , Optional . of ( ExceptionalHaltReason . INSUFFICIENT_GAS ) ) ;
} else {
} else {
frame . setState ( MessageFrame . State . COMPLETED_SUCCESS ) ;
frame . setState ( MessageFrame . State . COMPLETED_SUCCESS ) ;
}
}
} else {
return ;
}
final var invalidReason =
final var invalidReason =
contractValidationRules . stream ( )
contractValidationRules . stream ( )
. map ( rule - > rule . validate ( contractCode , frame , evm ) )
. map ( rule - > rule . validate ( contractCode , frame , evm ) )
. filter ( Optional : : isPresent )
. filter ( Optional : : isPresent )
. findFirst ( ) ;
. findFirst ( ) ;
if ( invalidReason . isEmpty ( ) ) {
if ( invalidReason . isPresent ( ) ) {
final Optional < ExceptionalHaltReason > exceptionalHaltReason = invalidReason . get ( ) ;
frame . setExceptionalHaltReason ( exceptionalHaltReason ) ;
frame . setState ( MessageFrame . State . EXCEPTIONAL_HALT ) ;
operationTracer . traceAccountCreationResult ( frame , exceptionalHaltReason ) ;
return ;
}
frame . decrementRemainingGas ( depositFee ) ;
frame . decrementRemainingGas ( depositFee ) ;
final long statelessContractCompletionFee =
evm . getGasCalculator ( ) . completedCreateContractGasCost ( frame ) ;
if ( frame . getRemainingGas ( ) < statelessContractCompletionFee ) {
LOG . trace (
"Not enough gas to pay the contract creation completion fee for {}: "
+ "remaining gas = {} < {} = deposit fee" ,
frame . getContractAddress ( ) ,
frame . getRemainingGas ( ) ,
statelessContractCompletionFee ) ;
frame . setExceptionalHaltReason ( Optional . of ( ExceptionalHaltReason . INSUFFICIENT_GAS ) ) ;
frame . setState ( MessageFrame . State . EXCEPTIONAL_HALT ) ;
} else {
frame . decrementRemainingGas ( statelessContractCompletionFee ) ;
// Finalize contract creation, setting the contract code.
// Finalize contract creation, setting the contract code.
final MutableAccount contract =
final MutableAccount contract = frame . getWorldUpdater ( ) . getOrCreate ( frame . getContractAddress ( ) ) ;
frame . getWorldUpdater ( ) . getOrCreate ( frame . getContractAddress ( ) ) ;
contract . setCode ( contractCode ) ;
contract . setCode ( contractCode ) ;
LOG . trace (
LOG . trace (
"Successful creation of contract {} with code of size {} (Gas remaining: {})" ,
"Successful creation of contract {} with code of size {} (Gas remaining: {})" ,
@ -180,17 +199,20 @@ public class ContractCreationProcessor extends AbstractMessageProcessor {
contractCode . size ( ) ,
contractCode . size ( ) ,
frame . getRemainingGas ( ) ) ;
frame . getRemainingGas ( ) ) ;
frame . setState ( MessageFrame . State . COMPLETED_SUCCESS ) ;
frame . setState ( MessageFrame . State . COMPLETED_SUCCESS ) ;
}
if ( operationTracer . isExtendedTracing ( ) ) {
if ( operationTracer . isExtendedTracing ( ) ) {
operationTracer . traceAccountCreationResult ( frame , Optional . empty ( ) ) ;
operationTracer . traceAccountCreationResult ( frame , Optional . empty ( ) ) ;
}
}
} else {
final Optional < ExceptionalHaltReason > exceptionalHaltReason = invalidReason . get ( ) ;
frame . setExceptionalHaltReason ( exceptionalHaltReason ) ;
frame . setState ( MessageFrame . State . EXCEPTIONAL_HALT ) ;
operationTracer . traceAccountCreationResult ( frame , exceptionalHaltReason ) ;
}
}
private static boolean handleInsufficientGas (
final MessageFrame frame , final long gasFee , final Supplier < String > message ) {
if ( frame . getRemainingGas ( ) < gasFee ) {
LOG . trace ( message . get ( ) ) ;
frame . setExceptionalHaltReason ( Optional . of ( ExceptionalHaltReason . INSUFFICIENT_GAS ) ) ;
frame . setState ( MessageFrame . State . EXCEPTIONAL_HALT ) ;
return true ;
}
}
return false ;
}
}
}
}