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