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