arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/common/ethrollup.go
common/ethrollup.goRaw
  1package common
  2
  3import (
  4	"fmt"
  5	"math/big"
  6
  7	ethCommon "github.com/ethereum/go-ethereum/common"
  8	"github.com/ethereum/go-ethereum/crypto"
  9	"github.com/hermeznetwork/tracerr"
 10)
 11
 12const (
 13	// RollupConstMaxFeeIdxCoordinator is the maximum number of tokens the
 14	// coordinator can use to collect fees (determines the number of tokens
 15	// that the coordinator can collect fees from).  This value is
 16	// determined by the circuit.
 17	RollupConstMaxFeeIdxCoordinator = 64
 18	// RollupConstReservedIDx First 256 indexes reserved, first user index will be the 256
 19	RollupConstReservedIDx = 255
 20	// RollupConstExitIDx IDX 1 is reserved for exits
 21	RollupConstExitIDx = 1
 22	// RollupConstLimitTokens Max number of tokens allowed to be registered inside the rollup
 23	RollupConstLimitTokens = (1 << 32)
 24	// RollupConstL1CoordinatorTotalBytes [4 bytes] token + [32 bytes] babyjub + [65 bytes] compressedSignature
 25	RollupConstL1CoordinatorTotalBytes = 101
 26	// RollupConstL1UserTotalBytes [20 bytes] fromEthAddr + [32 bytes] fromBjj-compressed + [6 bytes] fromIdx +
 27	// [2 bytes] depositAmountFloat16 + [2 bytes] amountFloat16 + [4 bytes] tokenId + [6 bytes] toIdx
 28	RollupConstL1UserTotalBytes = 72
 29	// RollupConstMaxL1UserTx Maximum L1-user transactions allowed to be queued in a batch
 30	RollupConstMaxL1UserTx = 128
 31	// RollupConstMaxL1Tx Maximum L1 transactions allowed to be queued in a batch
 32	RollupConstMaxL1Tx = 256
 33	// RollupConstInputSHAConstantBytes [6 bytes] lastIdx + [6 bytes] newLastIdx  + [32 bytes] stateRoot  + [32 bytes] newStRoot  + [32 bytes] newExitRoot +
 34	// [_MAX_L1_TX * _L1_USER_TOTALBYTES bytes] l1TxsData + totalL2TxsDataLength + feeIdxCoordinatorLength + [2 bytes] chainID =
 35	// 18542 bytes +  totalL2TxsDataLength + feeIdxCoordinatorLength
 36	RollupConstInputSHAConstantBytes = 18546
 37	// RollupConstNumBuckets Number of buckets
 38	RollupConstNumBuckets = 5
 39	// RollupConstMaxWithdrawalDelay max withdrawal delay in seconds
 40	RollupConstMaxWithdrawalDelay = 2 * 7 * 24 * 60 * 60
 41	// RollupConstExchangeMultiplier exchange multiplier
 42	RollupConstExchangeMultiplier = 1e14
 43	// LenVerifiers number of Rollup Smart Contract Verifiers
 44	LenVerifiers = 1
 45)
 46
 47var (
 48	// RollupConstLimitDepositAmount Max deposit amount allowed (depositAmount: L1 --> L2)
 49	RollupConstLimitDepositAmount, _ = new(big.Int).SetString("340282366920938463463374607431768211456", 10)
 50	// RollupConstLimitL2TransferAmount Max amount allowed (amount L2 --> L2)
 51	RollupConstLimitL2TransferAmount, _ = new(big.Int).SetString("6277101735386680763835789423207666416102355444464034512896", 10)
 52
 53	// RollupConstEthAddressInternalOnly This ethereum address is used internally for rollup accounts that don't have ethereum address, only Babyjubjub
 54	// This non-ethereum accounts can be created by the coordinator and allow users to have a rollup
 55	// account without needing an ethereum address
 56	RollupConstEthAddressInternalOnly = ethCommon.HexToAddress("0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF")
 57	// RollupConstRfield Modulus zkSNARK
 58	RollupConstRfield, _ = new(big.Int).SetString(
 59		"21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
 60
 61	// RollupConstERC1820 ERC1820Registry address
 62	RollupConstERC1820 = ethCommon.HexToAddress("0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24")
 63
 64	// ERC777 tokens signatures
 65
 66	// RollupConstRecipientInterfaceHash ERC777 recipient interface hash
 67	RollupConstRecipientInterfaceHash = crypto.Keccak256([]byte("ERC777TokensRecipient"))
 68	// RollupConstPerformL1UserTxSignature the signature of the function that can be called thru an ERC777 `send`
 69	RollupConstPerformL1UserTxSignature = crypto.Keccak256([]byte("addL1Transaction(uint256,uint48,uint16,uint16,uint32,uint48)"))
 70	// RollupConstAddTokenSignature the signature of the function that can be called thru an ERC777 `send`
 71	RollupConstAddTokenSignature = crypto.Keccak256([]byte("addToken(address)"))
 72	// RollupConstSendSignature ERC777 Signature
 73	RollupConstSendSignature = crypto.Keccak256([]byte("send(address,uint256,bytes)"))
 74	// RollupConstERC777Granularity ERC777 Signature
 75	RollupConstERC777Granularity = crypto.Keccak256([]byte("granularity()"))
 76	// RollupConstWithdrawalDelayerDeposit  This constant are used to deposit tokens from ERC77 tokens into withdrawal delayer
 77	RollupConstWithdrawalDelayerDeposit = crypto.Keccak256([]byte("deposit(address,address,uint192)"))
 78
 79	// ERC20 signature
 80
 81	// RollupConstTransferSignature This constant is used in the _safeTransfer internal method in order to safe GAS.
 82	RollupConstTransferSignature = crypto.Keccak256([]byte("transfer(address,uint256)"))
 83	// RollupConstTransferFromSignature This constant is used in the _safeTransfer internal method in order to safe GAS.
 84	RollupConstTransferFromSignature = crypto.Keccak256([]byte("transferFrom(address,address,uint256)"))
 85	// RollupConstApproveSignature This constant is used in the _safeTransfer internal method in order to safe GAS.
 86	RollupConstApproveSignature = crypto.Keccak256([]byte("approve(address,uint256)"))
 87	// RollupConstERC20Signature ERC20 decimals signature
 88	RollupConstERC20Signature = crypto.Keccak256([]byte("decimals()"))
 89)
 90
 91// RollupVerifierStruct is the information about verifiers of the Rollup Smart Contract
 92type RollupVerifierStruct struct {
 93	MaxTx   int64 `json:"maxTx"`
 94	NLevels int64 `json:"nlevels"`
 95}
 96
 97// RollupConstants are the constants of the Rollup Smart Contract
 98type RollupConstants struct {
 99	AbsoluteMaxL1L2BatchTimeout int64                  `json:"absoluteMaxL1L2BatchTimeout"`
100	TokenHEZ                    ethCommon.Address      `json:"tokenHEZ"`
101	Verifiers                   []RollupVerifierStruct `json:"verifiers"`
102	HermezAuctionContract       ethCommon.Address      `json:"hermezAuctionContract"`
103	HermezGovernanceAddress     ethCommon.Address      `json:"hermezGovernanceAddress"`
104	WithdrawDelayerContract     ethCommon.Address      `json:"withdrawDelayerContract"`
105}
106
107// FindVerifierIdx tries to find a matching verifier in the RollupConstants and
108// returns its index
109func (c *RollupConstants) FindVerifierIdx(MaxTx, NLevels int64) (int, error) {
110	for i, verifier := range c.Verifiers {
111		if verifier.MaxTx == MaxTx && verifier.NLevels == NLevels {
112			return i, nil
113		}
114	}
115	return 0, tracerr.Wrap(fmt.Errorf("verifier not found for MaxTx: %v, NLevels: %v",
116		MaxTx, NLevels))
117}
118
119// BucketParams are the parameter variables of each Bucket of Rollup Smart
120// Contract
121type BucketParams struct {
122	CeilUSD             *big.Int `json:"ceilUSD"`
123	Withdrawals         *big.Int `json:"withdrawals"`
124	BlockWithdrawalRate *big.Int `json:"blockWithdrawalRate"`
125	MaxWithdrawals      *big.Int `json:"maxWithdrawals"`
126}
127
128// BucketUpdate are the bucket updates (tracking the withdrawals value changes)
129// in Rollup Smart Contract
130type BucketUpdate struct {
131	EthBlockNum int64    `json:"ethereumBlockNum" meddler:"eth_block_num"`
132	NumBucket   int      `json:"numBucket" meddler:"num_bucket"`
133	BlockStamp  int64    `json:"blockStamp" meddler:"block_stamp"`
134	Withdrawals *big.Int `json:"withdrawals" meddler:"withdrawals,bigint"`
135}
136
137// TokenExchange are the exchange value for tokens registered in the Rollup
138// Smart Contract
139type TokenExchange struct {
140	EthBlockNum int64             `json:"ethereumBlockNum" meddler:"eth_block_num"`
141	Address     ethCommon.Address `json:"address" meddler:"eth_addr"`
142	ValueUSD    int64             `json:"valueUSD" meddler:"value_usd"`
143}
144
145// RollupVariables are the variables of the Rollup Smart Contract
146type RollupVariables struct {
147	EthBlockNum           int64                               `json:"ethereumBlockNum" meddler:"eth_block_num"`
148	FeeAddToken           *big.Int                            `json:"feeAddToken" meddler:"fee_add_token,bigint" validate:"required"`
149	ForgeL1L2BatchTimeout int64                               `json:"forgeL1L2BatchTimeout" meddler:"forge_l1_timeout" validate:"required"`
150	WithdrawalDelay       uint64                              `json:"withdrawalDelay" meddler:"withdrawal_delay" validate:"required"`
151	Buckets               [RollupConstNumBuckets]BucketParams `json:"buckets" meddler:"buckets,json"`
152	SafeMode              bool                                `json:"safeMode" meddler:"safe_mode"`
153}
154
155// Copy returns a deep copy of the Variables
156func (v *RollupVariables) Copy() *RollupVariables {
157	vCpy := *v
158	return &vCpy
159}