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 // [5 bytes] depositAmountFloat40 + [5 bytes] amountFloat40 + [4 bytes] tokenId + [6 bytes] toIdx
28 RollupConstL1UserTotalBytes = 78
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)
44
45var (
46 // RollupConstLimitDepositAmount Max deposit amount allowed (depositAmount: L1 --> L2)
47 RollupConstLimitDepositAmount, _ = new(big.Int).SetString("340282366920938463463374607431768211456", 10)
48 // RollupConstLimitL2TransferAmount Max amount allowed (amount L2 --> L2)
49 RollupConstLimitL2TransferAmount, _ = new(big.Int).SetString("6277101735386680763835789423207666416102355444464034512896", 10)
50
51 // RollupConstEthAddressInternalOnly This ethereum address is used internally for rollup accounts that don't have ethereum address, only Babyjubjub
52 // This non-ethereum accounts can be created by the coordinator and allow users to have a rollup
53 // account without needing an ethereum address
54 RollupConstEthAddressInternalOnly = ethCommon.HexToAddress("0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF")
55 // RollupConstRfield Modulus zkSNARK
56 RollupConstRfield, _ = new(big.Int).SetString(
57 "21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
58
59 // RollupConstERC1820 ERC1820Registry address
60 RollupConstERC1820 = ethCommon.HexToAddress("0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24")
61
62 // ERC777 tokens signatures
63
64 // RollupConstRecipientInterfaceHash ERC777 recipient interface hash
65 RollupConstRecipientInterfaceHash = crypto.Keccak256([]byte("ERC777TokensRecipient"))
66 // RollupConstPerformL1UserTxSignature the signature of the function that can be called thru an ERC777 `send`
67 RollupConstPerformL1UserTxSignature = crypto.Keccak256([]byte("addL1Transaction(uint256,uint48,uint16,uint16,uint32,uint48)"))
68 // RollupConstAddTokenSignature the signature of the function that can be called thru an ERC777 `send`
69 RollupConstAddTokenSignature = crypto.Keccak256([]byte("addToken(address)"))
70 // RollupConstSendSignature ERC777 Signature
71 RollupConstSendSignature = crypto.Keccak256([]byte("send(address,uint256,bytes)"))
72 // RollupConstERC777Granularity ERC777 Signature
73 RollupConstERC777Granularity = crypto.Keccak256([]byte("granularity()"))
74 // RollupConstWithdrawalDelayerDeposit This constant are used to deposit tokens from ERC77 tokens into withdrawal delayer
75 RollupConstWithdrawalDelayerDeposit = crypto.Keccak256([]byte("deposit(address,address,uint192)"))
76
77 // ERC20 signature
78
79 // RollupConstTransferSignature This constant is used in the _safeTransfer internal method in order to safe GAS.
80 RollupConstTransferSignature = crypto.Keccak256([]byte("transfer(address,uint256)"))
81 // RollupConstTransferFromSignature This constant is used in the _safeTransfer internal method in order to safe GAS.
82 RollupConstTransferFromSignature = crypto.Keccak256([]byte("transferFrom(address,address,uint256)"))
83 // RollupConstApproveSignature This constant is used in the _safeTransfer internal method in order to safe GAS.
84 RollupConstApproveSignature = crypto.Keccak256([]byte("approve(address,uint256)"))
85 // RollupConstERC20Signature ERC20 decimals signature
86 RollupConstERC20Signature = crypto.Keccak256([]byte("decimals()"))
87)
88
89// RollupVerifierStruct is the information about verifiers of the Rollup Smart Contract
90type RollupVerifierStruct struct {
91 MaxTx int64 `json:"maxTx"`
92 NLevels int64 `json:"nlevels"`
93}
94
95// RollupConstants are the constants of the Rollup Smart Contract
96type RollupConstants struct {
97 AbsoluteMaxL1L2BatchTimeout int64 `json:"absoluteMaxL1L2BatchTimeout"`
98 TokenHEZ ethCommon.Address `json:"tokenHEZ"`
99 Verifiers []RollupVerifierStruct `json:"verifiers"`
100 HermezAuctionContract ethCommon.Address `json:"hermezAuctionContract"`
101 HermezGovernanceAddress ethCommon.Address `json:"hermezGovernanceAddress"`
102 WithdrawDelayerContract ethCommon.Address `json:"withdrawDelayerContract"`
103}
104
105// FindVerifierIdx tries to find a matching verifier in the RollupConstants and
106// returns its index
107func (c *RollupConstants) FindVerifierIdx(MaxTx, NLevels int64) (int, error) {
108 for i, verifier := range c.Verifiers {
109 if verifier.MaxTx == MaxTx && verifier.NLevels == NLevels {
110 return i, nil
111 }
112 }
113 return 0, tracerr.Wrap(fmt.Errorf("verifier not found for MaxTx: %v, NLevels: %v",
114 MaxTx, NLevels))
115}
116
117// BucketParams are the parameter variables of each Bucket of Rollup Smart
118// Contract
119type BucketParams struct {
120 CeilUSD *big.Int
121 Withdrawals *big.Int
122 BlockWithdrawalRate *big.Int
123 MaxWithdrawals *big.Int
124}
125
126// BucketUpdate are the bucket updates (tracking the withdrawals value changes)
127// in Rollup Smart Contract
128type BucketUpdate struct {
129 EthBlockNum int64 `meddler:"eth_block_num"`
130 NumBucket int `meddler:"num_bucket"`
131 BlockStamp int64 `meddler:"block_stamp"`
132 Withdrawals *big.Int `meddler:"withdrawals,bigint"`
133}
134
135// TokenExchange are the exchange value for tokens registered in the Rollup
136// Smart Contract
137type TokenExchange struct {
138 EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
139 Address ethCommon.Address `json:"address" meddler:"eth_addr"`
140 ValueUSD int64 `json:"valueUSD" meddler:"value_usd"`
141}
142
143// RollupVariables are the variables of the Rollup Smart Contract
144type RollupVariables struct {
145 EthBlockNum int64 `meddler:"eth_block_num"`
146 FeeAddToken *big.Int `meddler:"fee_add_token,bigint" validate:"required"`
147 ForgeL1L2BatchTimeout int64 `meddler:"forge_l1_timeout" validate:"required"`
148 WithdrawalDelay uint64 `meddler:"withdrawal_delay" validate:"required"`
149 Buckets [RollupConstNumBuckets]BucketParams `meddler:"buckets,json"`
150 SafeMode bool `meddler:"safe_mode"`
151}
152
153// Copy returns a deep copy of the Variables
154func (v *RollupVariables) Copy() *RollupVariables {
155 vCpy := *v
156 return &vCpy
157}