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

     1  package state
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"math/big"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/0xPolygon/supernets2-node/state/metrics"
    11  	"github.com/0xPolygon/supernets2-node/state/runtime/instrumentation"
    12  	"github.com/ethereum/go-ethereum/common"
    13  	"github.com/ethereum/go-ethereum/core/types"
    14  )
    15  
    16  // ProcessRequest represents the request of a batch process.
    17  type ProcessRequest struct {
    18  	BatchNumber     uint64
    19  	GlobalExitRoot  common.Hash
    20  	OldStateRoot    common.Hash
    21  	OldAccInputHash common.Hash
    22  	Transactions    []byte
    23  	Coinbase        common.Address
    24  	Timestamp       time.Time
    25  	Caller          metrics.CallerLabel
    26  }
    27  
    28  // ProcessBatchResponse represents the response of a batch process.
    29  type ProcessBatchResponse struct {
    30  	NewStateRoot         common.Hash
    31  	NewAccInputHash      common.Hash
    32  	NewLocalExitRoot     common.Hash
    33  	NewBatchNumber       uint64
    34  	UsedZkCounters       ZKCounters
    35  	Responses            []*ProcessTransactionResponse
    36  	ExecutorError        error
    37  	IsRomLevelError      bool
    38  	IsExecutorLevelError bool
    39  	IsRomOOCError        bool
    40  	ReadWriteAddresses   map[common.Address]*InfoReadWrite
    41  }
    42  
    43  // ProcessTransactionResponse represents the response of a tx process.
    44  type ProcessTransactionResponse struct {
    45  	// TxHash is the hash of the transaction
    46  	TxHash common.Hash
    47  	// Type indicates legacy transaction
    48  	// It will be always 0 (legacy) in the executor
    49  	Type uint32
    50  	// ReturnValue is the returned data from the runtime (function result or data supplied with revert opcode)
    51  	ReturnValue []byte
    52  	// GasLeft is the total gas left as result of execution
    53  	GasLeft uint64
    54  	// GasUsed is the total gas used as result of execution or gas estimation
    55  	GasUsed uint64
    56  	// GasRefunded is the total gas refunded as result of execution
    57  	GasRefunded uint64
    58  	// RomError represents any error encountered during the execution
    59  	RomError error
    60  	// CreateAddress is the new SC Address in case of SC creation
    61  	CreateAddress common.Address
    62  	// StateRoot is the State Root
    63  	StateRoot common.Hash
    64  	// Logs emitted by LOG opcode
    65  	Logs []*types.Log
    66  	// ChangesStateRoot indicates if this tx affects the state
    67  	ChangesStateRoot bool
    68  	// Tx is the whole transaction object
    69  	Tx types.Transaction
    70  	// ExecutionTrace contains the traces produced in the execution
    71  	ExecutionTrace []instrumentation.StructLog
    72  	// CallTrace contains the call trace.
    73  	CallTrace instrumentation.ExecutorTrace
    74  }
    75  
    76  // ZKCounters counters for the tx
    77  type ZKCounters struct {
    78  	CumulativeGasUsed    uint64
    79  	UsedKeccakHashes     uint32
    80  	UsedPoseidonHashes   uint32
    81  	UsedPoseidonPaddings uint32
    82  	UsedMemAligns        uint32
    83  	UsedArithmetics      uint32
    84  	UsedBinaries         uint32
    85  	UsedSteps            uint32
    86  }
    87  
    88  // SumUp sum ups zk counters with passed tx zk counters
    89  func (z *ZKCounters) SumUp(other ZKCounters) {
    90  	z.CumulativeGasUsed += other.CumulativeGasUsed
    91  	z.UsedKeccakHashes += other.UsedKeccakHashes
    92  	z.UsedPoseidonHashes += other.UsedPoseidonHashes
    93  	z.UsedPoseidonPaddings += other.UsedPoseidonPaddings
    94  	z.UsedMemAligns += other.UsedMemAligns
    95  	z.UsedArithmetics += other.UsedArithmetics
    96  	z.UsedBinaries += other.UsedBinaries
    97  	z.UsedSteps += other.UsedSteps
    98  }
    99  
   100  // Sub subtract zk counters with passed zk counters (not safe)
   101  func (z *ZKCounters) Sub(other ZKCounters) error {
   102  	// ZKCounters
   103  	if other.CumulativeGasUsed > z.CumulativeGasUsed {
   104  		return GetZKCounterError("CumulativeGasUsed")
   105  	}
   106  	if other.UsedKeccakHashes > z.UsedKeccakHashes {
   107  		return GetZKCounterError("UsedKeccakHashes")
   108  	}
   109  	if other.UsedPoseidonHashes > z.UsedPoseidonHashes {
   110  		return GetZKCounterError("UsedPoseidonHashes")
   111  	}
   112  	if other.UsedPoseidonPaddings > z.UsedPoseidonPaddings {
   113  		return fmt.Errorf("underflow ZKCounter: UsedPoseidonPaddings")
   114  	}
   115  	if other.UsedMemAligns > z.UsedMemAligns {
   116  		return GetZKCounterError("UsedMemAligns")
   117  	}
   118  	if other.UsedArithmetics > z.UsedArithmetics {
   119  		return GetZKCounterError("UsedArithmetics")
   120  	}
   121  	if other.UsedBinaries > z.UsedBinaries {
   122  		return GetZKCounterError("UsedBinaries")
   123  	}
   124  	if other.UsedSteps > z.UsedSteps {
   125  		return GetZKCounterError("UsedSteps")
   126  	}
   127  
   128  	z.CumulativeGasUsed -= other.CumulativeGasUsed
   129  	z.UsedKeccakHashes -= other.UsedKeccakHashes
   130  	z.UsedPoseidonHashes -= other.UsedPoseidonHashes
   131  	z.UsedPoseidonPaddings -= other.UsedPoseidonPaddings
   132  	z.UsedMemAligns -= other.UsedMemAligns
   133  	z.UsedArithmetics -= other.UsedArithmetics
   134  	z.UsedBinaries -= other.UsedBinaries
   135  	z.UsedSteps -= other.UsedSteps
   136  
   137  	return nil
   138  }
   139  
   140  // BatchResources is a struct that contains the ZKEVM resources used by a batch/tx
   141  type BatchResources struct {
   142  	ZKCounters ZKCounters
   143  	Bytes      uint64
   144  }
   145  
   146  // Sub subtracts the batch resources from other
   147  func (r *BatchResources) Sub(other BatchResources) error {
   148  	// Bytes
   149  	if other.Bytes > r.Bytes {
   150  		return ErrBatchResourceBytesUnderflow
   151  	}
   152  	bytesBackup := r.Bytes
   153  	r.Bytes -= other.Bytes
   154  	err := r.ZKCounters.Sub(other.ZKCounters)
   155  	if err != nil {
   156  		r.Bytes = bytesBackup
   157  		return NewBatchRemainingResourcesUnderflowError(err, err.Error())
   158  	}
   159  
   160  	return err
   161  }
   162  
   163  // InfoReadWrite has information about modified addresses during the execution
   164  type InfoReadWrite struct {
   165  	Address common.Address
   166  	Nonce   *uint64
   167  	Balance *big.Int
   168  }
   169  
   170  // TraceConfig sets the debug configuration for the executor
   171  type TraceConfig struct {
   172  	DisableStorage   bool
   173  	DisableStack     bool
   174  	EnableMemory     bool
   175  	EnableReturnData bool
   176  	Tracer           *string
   177  	TracerConfig     json.RawMessage
   178  }
   179  
   180  // IsDefaultTracer returns true when no custom tracer is set
   181  func (t *TraceConfig) IsDefaultTracer() bool {
   182  	return t.Tracer == nil || *t.Tracer == ""
   183  }
   184  
   185  // Is4ByteTracer returns true when should use 4byteTracer
   186  func (t *TraceConfig) Is4ByteTracer() bool {
   187  	return t.Tracer != nil && *t.Tracer == "4byteTracer"
   188  }
   189  
   190  // IsCallTracer returns true when should use callTracer
   191  func (t *TraceConfig) IsCallTracer() bool {
   192  	return t.Tracer != nil && *t.Tracer == "callTracer"
   193  }
   194  
   195  // IsNoopTracer returns true when should use noopTracer
   196  func (t *TraceConfig) IsNoopTracer() bool {
   197  	return t.Tracer != nil && *t.Tracer == "noopTracer"
   198  }
   199  
   200  // IsPrestateTracer returns true when should use prestateTracer
   201  func (t *TraceConfig) IsPrestateTracer() bool {
   202  	return t.Tracer != nil && *t.Tracer == "prestateTracer"
   203  }
   204  
   205  // IsJSCustomTracer returns true when should use js custom tracer
   206  func (t *TraceConfig) IsJSCustomTracer() bool {
   207  	return t.Tracer != nil && strings.Contains(*t.Tracer, "result") && strings.Contains(*t.Tracer, "fault")
   208  }
   209  
   210  // TrustedReorg represents a trusted reorg
   211  type TrustedReorg struct {
   212  	BatchNumber uint64
   213  	Reason      string
   214  }
   215  
   216  // HexToAddressPtr create an address from a hex and returns its pointer
   217  func HexToAddressPtr(hex string) *common.Address {
   218  	a := common.HexToAddress(hex)
   219  	return &a
   220  }
   221  
   222  // HexToHashPtr create a hash from a hex and returns its pointer
   223  func HexToHashPtr(hex string) *common.Hash {
   224  	h := common.HexToHash(hex)
   225  	return &h
   226  }
   227  
   228  // AddressPtr returns a pointer to the provided address
   229  func AddressPtr(i common.Address) *common.Address {
   230  	return &i
   231  }
   232  
   233  // HashPtr returns a pointer to the provided hash
   234  func HashPtr(h common.Hash) *common.Hash {
   235  	return &h
   236  }