github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/state/runtime/executor/errors.go (about)

     1  package executor
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  
     7  	"github.com/0xPolygon/supernets2-node/state/runtime"
     8  	"github.com/0xPolygon/supernets2-node/state/runtime/executor/pb"
     9  )
    10  
    11  const (
    12  	// ROM_ERROR_UNSPECIFIED indicates the execution ended successfully
    13  	ROM_ERROR_UNSPECIFIED int32 = iota
    14  	// ROM_ERROR_NO_ERROR indicates the execution ended successfully
    15  	ROM_ERROR_NO_ERROR
    16  	// ROM_ERROR_OUT_OF_GAS indicates there is not enough balance to continue the execution
    17  	ROM_ERROR_OUT_OF_GAS
    18  	// ROM_ERROR_STACK_OVERFLOW indicates a stack overflow has happened
    19  	ROM_ERROR_STACK_OVERFLOW
    20  	// ROM_ERROR_STACK_UNDERFLOW indicates a stack overflow has happened
    21  	ROM_ERROR_STACK_UNDERFLOW
    22  	// ROM_ERROR_MAX_CODE_SIZE_EXCEEDED indicates the code size is beyond the maximum
    23  	ROM_ERROR_MAX_CODE_SIZE_EXCEEDED
    24  	// ROM_ERROR_CONTRACT_ADDRESS_COLLISION there is a collision regarding contract addresses
    25  	ROM_ERROR_CONTRACT_ADDRESS_COLLISION
    26  	// ROM_ERROR_EXECUTION_REVERTED indicates the execution has been reverted
    27  	ROM_ERROR_EXECUTION_REVERTED
    28  	// ROM_ERROR_OUT_OF_COUNTERS_STEP indicates there is not enough step counters to continue the execution
    29  	ROM_ERROR_OUT_OF_COUNTERS_STEP
    30  	// ROM_ERROR_OUT_OF_COUNTERS_KECCAK indicates there is not enough keccak counters to continue the execution
    31  	ROM_ERROR_OUT_OF_COUNTERS_KECCAK
    32  	// ROM_ERROR_OUT_OF_COUNTERS_BINARY indicates there is not enough binary counters to continue the execution
    33  	ROM_ERROR_OUT_OF_COUNTERS_BINARY
    34  	// ROM_ERROR_OUT_OF_COUNTERS_MEM indicates there is not enough memory aligncounters to continue the execution
    35  	ROM_ERROR_OUT_OF_COUNTERS_MEM
    36  	// ROM_ERROR_OUT_OF_COUNTERS_ARITH indicates there is not enough arith counters to continue the execution
    37  	ROM_ERROR_OUT_OF_COUNTERS_ARITH
    38  	// ROM_ERROR_OUT_OF_COUNTERS_PADDING indicates there is not enough padding counters to continue the execution
    39  	ROM_ERROR_OUT_OF_COUNTERS_PADDING
    40  	// ROM_ERROR_OUT_OF_COUNTERS_POSEIDON indicates there is not enough poseidon counters to continue the execution
    41  	ROM_ERROR_OUT_OF_COUNTERS_POSEIDON
    42  	// ROM_ERROR_INVALID_JUMP indicates there is an invalid jump opcode
    43  	ROM_ERROR_INVALID_JUMP
    44  	// ROM_ERROR_INVALID_OPCODE indicates there is an invalid opcode
    45  	ROM_ERROR_INVALID_OPCODE
    46  	// ROM_ERROR_INVALID_STATIC indicates there is an invalid static call
    47  	ROM_ERROR_INVALID_STATIC
    48  	// ROM_ERROR_INVALID_BYTECODE_STARTS_EF indicates there is a bytecode starting with 0xEF
    49  	ROM_ERROR_INVALID_BYTECODE_STARTS_EF
    50  	// ROM_ERROR_INTRINSIC_INVALID_SIGNATURE indicates the transaction is failing at the signature intrinsic check
    51  	ROM_ERROR_INTRINSIC_INVALID_SIGNATURE
    52  	// ROM_ERROR_INTRINSIC_INVALID_CHAIN_ID indicates the transaction is failing at the chain id intrinsic check
    53  	ROM_ERROR_INTRINSIC_INVALID_CHAIN_ID
    54  	// ROM_ERROR_INTRINSIC_INVALID_NONCE indicates the transaction is failing at the nonce intrinsic check
    55  	ROM_ERROR_INTRINSIC_INVALID_NONCE
    56  	// ROM_ERROR_INTRINSIC_INVALID_GAS_LIMIT indicates the transaction is failing at the gas limit intrinsic check
    57  	ROM_ERROR_INTRINSIC_INVALID_GAS_LIMIT
    58  	// ROM_ERROR_INTRINSIC_INVALID_BALANCE indicates the transaction is failing at balance intrinsic check
    59  	ROM_ERROR_INTRINSIC_INVALID_BALANCE
    60  	// ROM_ERROR_INTRINSIC_INVALID_BATCH_GAS_LIMIT indicates the batch is exceeding the batch gas limit
    61  	ROM_ERROR_INTRINSIC_INVALID_BATCH_GAS_LIMIT
    62  	// ROM_ERROR_INTRINSIC_INVALID_SENDER_CODE indicates the batch is exceeding the batch gas limit
    63  	ROM_ERROR_INTRINSIC_INVALID_SENDER_CODE
    64  	// ROM_ERROR_INTRINSIC_TX_GAS_OVERFLOW indicates the transaction gasLimit*gasPrice > MAX_UINT_256 - 1
    65  	ROM_ERROR_INTRINSIC_TX_GAS_OVERFLOW
    66  	// ROM_ERROR_BATCH_DATA_TOO_BIG indicates the batch_l2_data is too big to be processed
    67  	ROM_ERROR_BATCH_DATA_TOO_BIG
    68  	// ROM_ERROR_UNSUPPORTED_FORK_ID indicates that the fork id is not supported
    69  	ROM_ERROR_UNSUPPORTED_FORK_ID
    70  	// EXECUTOR_ERROR_UNSPECIFIED indicates the execution ended successfully
    71  	EXECUTOR_ERROR_UNSPECIFIED = 0
    72  	// EXECUTOR_ERROR_NO_ERROR indicates there was no error
    73  	EXECUTOR_ERROR_NO_ERROR = 1
    74  	// EXECUTOR_ERROR_COUNTERS_OVERFLOW_KECCAK indicates that the keccak counter exceeded the maximum
    75  	EXECUTOR_ERROR_COUNTERS_OVERFLOW_KECCAK = 2
    76  	// EXECUTOR_ERROR_COUNTERS_OVERFLOW_BINARY indicates that the binary counter exceeded the maximum
    77  	EXECUTOR_ERROR_COUNTERS_OVERFLOW_BINARY = 3
    78  	// EXECUTOR_ERROR_COUNTERS_OVERFLOW_MEM indicates that the memory align counter exceeded the maximum
    79  	EXECUTOR_ERROR_COUNTERS_OVERFLOW_MEM = 4
    80  	// EXECUTOR_ERROR_COUNTERS_OVERFLOW_ARITH indicates that the arith counter exceeded the maximum
    81  	EXECUTOR_ERROR_COUNTERS_OVERFLOW_ARITH = 5
    82  	// EXECUTOR_ERROR_COUNTERS_OVERFLOW_PADDING indicates that the padding counter exceeded the maximum
    83  	EXECUTOR_ERROR_COUNTERS_OVERFLOW_PADDING = 6
    84  	// EXECUTOR_ERROR_COUNTERS_OVERFLOW_POSEIDON indicates that the poseidon counter exceeded the maximum
    85  	EXECUTOR_ERROR_COUNTERS_OVERFLOW_POSEIDON = 7
    86  	// EXECUTOR_ERROR_UNSUPPORTED_FORK_ID indicates that the fork id is not supported
    87  	EXECUTOR_ERROR_UNSUPPORTED_FORK_ID = 8
    88  	// EXECUTOR_ERROR_BALANCE_MISMATCH indicates that there is a balance mismatch error in the ROM
    89  	EXECUTOR_ERROR_BALANCE_MISMATCH = 9
    90  	// EXECUTOR_ERROR_FEA2SCALAR indicates that there is a fea2scalar error in the execution
    91  	EXECUTOR_ERROR_FEA2SCALAR = 10
    92  	// EXECUTOR_ERROR_TOS32 indicates that there is a TOS32 error in the execution
    93  	EXECUTOR_ERROR_TOS32 = 11
    94  )
    95  
    96  var (
    97  	// ErrUnspecified indicates an unspecified executor error
    98  	ErrUnspecified = fmt.Errorf("unspecified executor error")
    99  	// ErrUnknown indicates an unknown executor error
   100  	ErrUnknown = fmt.Errorf("unknown error")
   101  )
   102  
   103  // RomErr returns an instance of error related to the ExecutorError
   104  func RomErr(errorCode pb.RomError) error {
   105  	e := int32(errorCode)
   106  	switch e {
   107  	case ROM_ERROR_UNSPECIFIED:
   108  		return fmt.Errorf("unspecified ROM error")
   109  	case ROM_ERROR_NO_ERROR:
   110  		return nil
   111  	case ROM_ERROR_OUT_OF_GAS:
   112  		return runtime.ErrOutOfGas
   113  	case ROM_ERROR_STACK_OVERFLOW:
   114  		return runtime.ErrStackOverflow
   115  	case ROM_ERROR_STACK_UNDERFLOW:
   116  		return runtime.ErrStackUnderflow
   117  	case ROM_ERROR_MAX_CODE_SIZE_EXCEEDED:
   118  		return runtime.ErrMaxCodeSizeExceeded
   119  	case ROM_ERROR_CONTRACT_ADDRESS_COLLISION:
   120  		return runtime.ErrContractAddressCollision
   121  	case ROM_ERROR_EXECUTION_REVERTED:
   122  		return runtime.ErrExecutionReverted
   123  	case ROM_ERROR_OUT_OF_COUNTERS_STEP:
   124  		return runtime.ErrOutOfCountersStep
   125  	case ROM_ERROR_OUT_OF_COUNTERS_KECCAK:
   126  		return runtime.ErrOutOfCountersKeccak
   127  	case ROM_ERROR_OUT_OF_COUNTERS_BINARY:
   128  		return runtime.ErrOutOfCountersBinary
   129  	case ROM_ERROR_OUT_OF_COUNTERS_MEM:
   130  		return runtime.ErrOutOfCountersMemory
   131  	case ROM_ERROR_OUT_OF_COUNTERS_ARITH:
   132  		return runtime.ErrOutOfCountersArith
   133  	case ROM_ERROR_OUT_OF_COUNTERS_PADDING:
   134  		return runtime.ErrOutOfCountersPadding
   135  	case ROM_ERROR_OUT_OF_COUNTERS_POSEIDON:
   136  		return runtime.ErrOutOfCountersPoseidon
   137  	case ROM_ERROR_INVALID_JUMP:
   138  		return runtime.ErrInvalidJump
   139  	case ROM_ERROR_INVALID_OPCODE:
   140  		return runtime.ErrInvalidOpCode
   141  	case ROM_ERROR_INVALID_STATIC:
   142  		return runtime.ErrInvalidStatic
   143  	case ROM_ERROR_INVALID_BYTECODE_STARTS_EF:
   144  		return runtime.ErrInvalidByteCodeStartsEF
   145  	case ROM_ERROR_INTRINSIC_INVALID_SIGNATURE:
   146  		return runtime.ErrIntrinsicInvalidSignature
   147  	case ROM_ERROR_INTRINSIC_INVALID_CHAIN_ID:
   148  		return runtime.ErrIntrinsicInvalidChainID
   149  	case ROM_ERROR_INTRINSIC_INVALID_NONCE:
   150  		return runtime.ErrIntrinsicInvalidNonce
   151  	case ROM_ERROR_INTRINSIC_INVALID_GAS_LIMIT:
   152  		return runtime.ErrIntrinsicInvalidGasLimit
   153  	case ROM_ERROR_INTRINSIC_INVALID_BALANCE:
   154  		return runtime.ErrIntrinsicInvalidBalance
   155  	case ROM_ERROR_INTRINSIC_INVALID_BATCH_GAS_LIMIT:
   156  		return runtime.ErrIntrinsicInvalidBatchGasLimit
   157  	case ROM_ERROR_INTRINSIC_INVALID_SENDER_CODE:
   158  		return runtime.ErrIntrinsicInvalidSenderCode
   159  	case ROM_ERROR_INTRINSIC_TX_GAS_OVERFLOW:
   160  		return runtime.ErrIntrinsicInvalidTxGasOverflow
   161  	case ROM_ERROR_BATCH_DATA_TOO_BIG:
   162  		return runtime.ErrBatchDataTooBig
   163  	case ROM_ERROR_UNSUPPORTED_FORK_ID:
   164  		return runtime.ErrUnsupportedForkId
   165  	}
   166  	return fmt.Errorf("unknown error")
   167  }
   168  
   169  // RomErrorCode returns the error code for a given error
   170  func RomErrorCode(err error) pb.RomError {
   171  	switch err {
   172  	case nil:
   173  		return pb.RomError(ROM_ERROR_NO_ERROR)
   174  	case runtime.ErrOutOfGas:
   175  		return pb.RomError(ROM_ERROR_OUT_OF_GAS)
   176  	case runtime.ErrStackOverflow:
   177  		return pb.RomError(ROM_ERROR_STACK_OVERFLOW)
   178  	case runtime.ErrStackUnderflow:
   179  		return pb.RomError(ROM_ERROR_STACK_UNDERFLOW)
   180  	case runtime.ErrMaxCodeSizeExceeded:
   181  		return pb.RomError(ROM_ERROR_MAX_CODE_SIZE_EXCEEDED)
   182  	case runtime.ErrContractAddressCollision:
   183  		return pb.RomError(ROM_ERROR_CONTRACT_ADDRESS_COLLISION)
   184  	case runtime.ErrExecutionReverted:
   185  		return pb.RomError(ROM_ERROR_EXECUTION_REVERTED)
   186  	case runtime.ErrOutOfCountersStep:
   187  		return pb.RomError(ROM_ERROR_OUT_OF_COUNTERS_STEP)
   188  	case runtime.ErrOutOfCountersKeccak:
   189  		return pb.RomError(ROM_ERROR_OUT_OF_COUNTERS_KECCAK)
   190  	case runtime.ErrOutOfCountersBinary:
   191  		return pb.RomError(ROM_ERROR_OUT_OF_COUNTERS_BINARY)
   192  	case runtime.ErrOutOfCountersMemory:
   193  		return pb.RomError(ROM_ERROR_OUT_OF_COUNTERS_MEM)
   194  	case runtime.ErrOutOfCountersArith:
   195  		return pb.RomError(ROM_ERROR_OUT_OF_COUNTERS_ARITH)
   196  	case runtime.ErrOutOfCountersPadding:
   197  		return pb.RomError(ROM_ERROR_OUT_OF_COUNTERS_PADDING)
   198  	case runtime.ErrOutOfCountersPoseidon:
   199  		return pb.RomError(ROM_ERROR_OUT_OF_COUNTERS_POSEIDON)
   200  	case runtime.ErrInvalidJump:
   201  		return pb.RomError(ROM_ERROR_INVALID_JUMP)
   202  	case runtime.ErrInvalidOpCode:
   203  		return pb.RomError(ROM_ERROR_INVALID_OPCODE)
   204  	case runtime.ErrInvalidStatic:
   205  		return pb.RomError(ROM_ERROR_INVALID_STATIC)
   206  	case runtime.ErrInvalidByteCodeStartsEF:
   207  		return pb.RomError(ROM_ERROR_INVALID_BYTECODE_STARTS_EF)
   208  	case runtime.ErrIntrinsicInvalidSignature:
   209  		return pb.RomError(ROM_ERROR_INTRINSIC_INVALID_SIGNATURE)
   210  	case runtime.ErrIntrinsicInvalidChainID:
   211  		return pb.RomError(ROM_ERROR_INTRINSIC_INVALID_CHAIN_ID)
   212  	case runtime.ErrIntrinsicInvalidNonce:
   213  		return pb.RomError(ROM_ERROR_INTRINSIC_INVALID_NONCE)
   214  	case runtime.ErrIntrinsicInvalidGasLimit:
   215  		return pb.RomError(ROM_ERROR_INTRINSIC_INVALID_GAS_LIMIT)
   216  	case runtime.ErrIntrinsicInvalidBalance:
   217  		return pb.RomError(ROM_ERROR_INTRINSIC_INVALID_BALANCE)
   218  	case runtime.ErrIntrinsicInvalidBatchGasLimit:
   219  		return pb.RomError(ROM_ERROR_INTRINSIC_INVALID_BATCH_GAS_LIMIT)
   220  	case runtime.ErrIntrinsicInvalidSenderCode:
   221  		return pb.RomError(ROM_ERROR_INTRINSIC_INVALID_SENDER_CODE)
   222  	case runtime.ErrIntrinsicInvalidTxGasOverflow:
   223  		return pb.RomError(ROM_ERROR_INTRINSIC_TX_GAS_OVERFLOW)
   224  	case runtime.ErrBatchDataTooBig:
   225  		return pb.RomError(ROM_ERROR_BATCH_DATA_TOO_BIG)
   226  	case runtime.ErrUnsupportedForkId:
   227  		return pb.RomError(ROM_ERROR_UNSUPPORTED_FORK_ID)
   228  	}
   229  	return math.MaxInt32
   230  }
   231  
   232  // IsROMOutOfCountersError indicates if the error is an ROM OOC
   233  func IsROMOutOfCountersError(error pb.RomError) bool {
   234  	return int32(error) >= ROM_ERROR_OUT_OF_COUNTERS_STEP && int32(error) <= ROM_ERROR_OUT_OF_COUNTERS_POSEIDON
   235  }
   236  
   237  // IsROMOutOfGasError indicates if the error is an ROM OOG
   238  func IsROMOutOfGasError(error pb.RomError) bool {
   239  	return int32(error) == ROM_ERROR_OUT_OF_GAS
   240  }
   241  
   242  // IsExecutorOutOfCountersError indicates if the error is an ROM OOC
   243  func IsExecutorOutOfCountersError(error pb.ExecutorError) bool {
   244  	return int32(error) >= EXECUTOR_ERROR_COUNTERS_OVERFLOW_KECCAK && int32(error) <= ROM_ERROR_OUT_OF_COUNTERS_POSEIDON
   245  }
   246  
   247  // IsExecutorUnspecifiedError indicates an unspecified error in the executor
   248  func IsExecutorUnspecifiedError(error pb.ExecutorError) bool {
   249  	return int32(error) == EXECUTOR_ERROR_UNSPECIFIED
   250  }
   251  
   252  // IsIntrinsicError indicates if the error is due to a intrinsic check
   253  func IsIntrinsicError(error pb.RomError) bool {
   254  	return int32(error) >= ROM_ERROR_INTRINSIC_INVALID_SIGNATURE && int32(error) <= ROM_ERROR_INTRINSIC_TX_GAS_OVERFLOW
   255  }
   256  
   257  // IsInvalidNonceError indicates if the error is due to a invalid nonce
   258  func IsInvalidNonceError(error pb.RomError) bool {
   259  	return int32(error) == ROM_ERROR_INTRINSIC_INVALID_NONCE
   260  }
   261  
   262  // IsInvalidBalanceError indicates if the error is due to a invalid balance
   263  func IsInvalidBalanceError(error pb.RomError) bool {
   264  	return int32(error) == ROM_ERROR_INTRINSIC_INVALID_BALANCE
   265  }
   266  
   267  // ExecutorErr returns an instance of error related to the ExecutorError
   268  func ExecutorErr(errorCode pb.ExecutorError) error {
   269  	e := int32(errorCode)
   270  	switch e {
   271  	case EXECUTOR_ERROR_UNSPECIFIED:
   272  		return ErrUnspecified
   273  	case EXECUTOR_ERROR_NO_ERROR:
   274  		return nil
   275  	case EXECUTOR_ERROR_COUNTERS_OVERFLOW_KECCAK:
   276  		return runtime.ErrOutOfCountersKeccak
   277  	case EXECUTOR_ERROR_COUNTERS_OVERFLOW_BINARY:
   278  		return runtime.ErrOutOfCountersBinary
   279  	case EXECUTOR_ERROR_COUNTERS_OVERFLOW_MEM:
   280  		return runtime.ErrOutOfCountersMemory
   281  	case EXECUTOR_ERROR_COUNTERS_OVERFLOW_ARITH:
   282  		return runtime.ErrOutOfCountersArith
   283  	case EXECUTOR_ERROR_COUNTERS_OVERFLOW_PADDING:
   284  		return runtime.ErrOutOfCountersPadding
   285  	case EXECUTOR_ERROR_COUNTERS_OVERFLOW_POSEIDON:
   286  		return runtime.ErrOutOfCountersPoseidon
   287  	case EXECUTOR_ERROR_UNSUPPORTED_FORK_ID:
   288  		return runtime.ErrUnsupportedForkId
   289  	case EXECUTOR_ERROR_BALANCE_MISMATCH:
   290  		return runtime.ErrBalanceMismatch
   291  	case EXECUTOR_ERROR_FEA2SCALAR:
   292  		return runtime.ErrFea2Scalar
   293  	case EXECUTOR_ERROR_TOS32:
   294  		return runtime.ErrTos32
   295  	}
   296  	return ErrUnknown
   297  }
   298  
   299  // ExecutorErrorCode returns the error code for a given error
   300  func ExecutorErrorCode(err error) pb.ExecutorError {
   301  	switch err {
   302  	case nil:
   303  		return pb.ExecutorError(EXECUTOR_ERROR_NO_ERROR)
   304  	case runtime.ErrOutOfCountersKeccak:
   305  		return pb.ExecutorError(EXECUTOR_ERROR_COUNTERS_OVERFLOW_KECCAK)
   306  	case runtime.ErrOutOfCountersBinary:
   307  		return pb.ExecutorError(EXECUTOR_ERROR_COUNTERS_OVERFLOW_BINARY)
   308  	case runtime.ErrOutOfCountersMemory:
   309  		return pb.ExecutorError(EXECUTOR_ERROR_COUNTERS_OVERFLOW_MEM)
   310  	case runtime.ErrOutOfCountersArith:
   311  		return pb.ExecutorError(EXECUTOR_ERROR_COUNTERS_OVERFLOW_ARITH)
   312  	case runtime.ErrOutOfCountersPadding:
   313  		return pb.ExecutorError(EXECUTOR_ERROR_COUNTERS_OVERFLOW_PADDING)
   314  	case runtime.ErrOutOfCountersPoseidon:
   315  		return pb.ExecutorError(EXECUTOR_ERROR_COUNTERS_OVERFLOW_POSEIDON)
   316  	case runtime.ErrUnsupportedForkId:
   317  		return pb.ExecutorError(EXECUTOR_ERROR_UNSUPPORTED_FORK_ID)
   318  	case runtime.ErrBalanceMismatch:
   319  		return pb.ExecutorError(EXECUTOR_ERROR_BALANCE_MISMATCH)
   320  	case runtime.ErrFea2Scalar:
   321  		return pb.ExecutorError(EXECUTOR_ERROR_FEA2SCALAR)
   322  	case runtime.ErrTos32:
   323  		return pb.ExecutorError(EXECUTOR_ERROR_TOS32)
   324  	}
   325  	return math.MaxInt32
   326  }