hermez-node/eth/rollup.go
eth/rollup.goRaw
1package eth
2
3import (
4 "math/big"
5
6 ethCommon "github.com/ethereum/go-ethereum/common"
7 "github.com/ethereum/go-ethereum/core/types"
8 "github.com/hermeznetwork/hermez-node/common"
9 "github.com/hermeznetwork/hermez-node/log"
10 "github.com/hermeznetwork/hermez-node/utils"
11 "github.com/iden3/go-iden3-crypto/babyjub"
12)
13
14const (
15 // FeeIdxCoordinatorLen is the number of tokens the coordinator can use
16 // to collect fees (determines the number of tokens that the
17 // coordinator can collect fees from). This value is determined by the
18 // circuit.
19 FeeIdxCoordinatorLen = 64
20)
21
22// RollupConstants are the constants of the Rollup Smart Contract
23type RollupConstants struct {
24 // Maxim Deposit allowed
25 MaxAmountDeposit *big.Int
26 MaxAmountL2 *big.Int
27 MaxTokens uint32
28 // maximum L1 transactions allowed to be queued for a batch
29 MaxL1Tx int
30 // maximum L1 user transactions allowed to be queued for a batch
31 MaxL1UserTx int
32 Rfield *big.Int
33 L1CoordinatorBytes int
34 L1UserBytes int
35 L2Bytes int
36}
37
38// RollupVariables are the variables of the Rollup Smart Contract
39type RollupVariables struct {
40 MaxTxVerifiers []int
41 TokenHEZ ethCommon.Address
42 GovernanceAddress ethCommon.Address
43 SafetyBot ethCommon.Address
44 ConsensusContract ethCommon.Address
45 WithdrawalContract ethCommon.Address
46 FeeAddToken *big.Int
47 ForgeL1Timeout int64
48 FeeL1UserTx *big.Int
49}
50
51// QueueStruct is the queue of L1Txs for a batch
52//nolint:structcheck
53type QueueStruct struct {
54 L1TxQueue []common.L1Tx
55 TotalL1TxFee *big.Int
56}
57
58// NewQueueStruct creates a new clear QueueStruct.
59func NewQueueStruct() *QueueStruct {
60 return &QueueStruct{
61 L1TxQueue: make([]common.L1Tx, 0),
62 TotalL1TxFee: big.NewInt(0),
63 }
64}
65
66// RollupState represents the state of the Rollup in the Smart Contract
67//nolint:structcheck,unused
68type RollupState struct {
69 StateRoot *big.Int
70 ExitRoots []*big.Int
71 ExitNullifierMap map[[256 / 8]byte]bool
72 TokenList []ethCommon.Address
73 TokenMap map[ethCommon.Address]bool
74 MapL1TxQueue map[int64]*QueueStruct
75 LastL1L2Batch int64
76 CurrentToForgeL1TxsNum int64
77 LastToForgeL1TxsNum int64
78 CurrentIdx int64
79}
80
81// RollupEventL1UserTx is an event of the Rollup Smart Contract
82type RollupEventL1UserTx struct {
83 L1Tx common.L1Tx
84}
85
86// RollupEventAddToken is an event of the Rollup Smart Contract
87type RollupEventAddToken struct {
88 Address ethCommon.Address
89 TokenID uint32
90}
91
92// RollupEventForgeBatch is an event of the Rollup Smart Contract
93type RollupEventForgeBatch struct {
94 BatchNum int64
95 EthTxHash ethCommon.Hash
96}
97
98// RollupEventUpdateForgeL1Timeout is an event of the Rollup Smart Contract
99type RollupEventUpdateForgeL1Timeout struct {
100 ForgeL1Timeout int64
101}
102
103// RollupEventUpdateFeeL1UserTx is an event of the Rollup Smart Contract
104type RollupEventUpdateFeeL1UserTx struct {
105 FeeL1UserTx *big.Int
106}
107
108// RollupEventUpdateFeeAddToken is an event of the Rollup Smart Contract
109type RollupEventUpdateFeeAddToken struct {
110 FeeAddToken *big.Int
111}
112
113// RollupEventUpdateTokenHez is an event of the Rollup Smart Contract
114type RollupEventUpdateTokenHez struct {
115 TokenHEZ ethCommon.Address
116}
117
118// RollupEventWithdraw is an event of the Rollup Smart Contract
119type RollupEventWithdraw struct {
120 Idx int64
121 NumExitRoot int
122}
123
124// RollupEvents is the list of events in a block of the Rollup Smart Contract
125type RollupEvents struct { //nolint:structcheck
126 L1UserTx []RollupEventL1UserTx
127 AddToken []RollupEventAddToken
128 ForgeBatch []RollupEventForgeBatch
129 UpdateForgeL1Timeout []RollupEventUpdateForgeL1Timeout
130 UpdateFeeL1UserTx []RollupEventUpdateFeeL1UserTx
131 UpdateFeeAddToken []RollupEventUpdateFeeAddToken
132 UpdateTokenHez []RollupEventUpdateTokenHez
133 Withdraw []RollupEventWithdraw
134}
135
136// NewRollupEvents creates an empty RollupEvents with the slices initialized.
137func NewRollupEvents() RollupEvents {
138 return RollupEvents{
139 L1UserTx: make([]RollupEventL1UserTx, 0),
140 AddToken: make([]RollupEventAddToken, 0),
141 ForgeBatch: make([]RollupEventForgeBatch, 0),
142 UpdateForgeL1Timeout: make([]RollupEventUpdateForgeL1Timeout, 0),
143 UpdateFeeL1UserTx: make([]RollupEventUpdateFeeL1UserTx, 0),
144 UpdateFeeAddToken: make([]RollupEventUpdateFeeAddToken, 0),
145 UpdateTokenHez: make([]RollupEventUpdateTokenHez, 0),
146 Withdraw: make([]RollupEventWithdraw, 0),
147 }
148}
149
150// RollupForgeBatchArgs are the arguments to the ForgeBatch function in the Rollup Smart Contract
151//nolint:structcheck,unused
152type RollupForgeBatchArgs struct {
153 ProofA [2]*big.Int
154 ProofB [2][2]*big.Int
155 ProofC [2]*big.Int
156 NewLastIdx int64
157 NewStRoot *big.Int
158 NewExitRoot *big.Int
159 L1CoordinatorTxs []*common.L1Tx
160 L1CoordinatorTxsAuths [][]byte // Authorization for accountCreations for each L1CoordinatorTxs
161 L2Txs []*common.L2Tx
162 FeeIdxCoordinator []common.Idx
163 // Circuit selector
164 VerifierIdx int64
165 L1Batch bool
166}
167
168// RollupInterface is the inteface to to Rollup Smart Contract
169type RollupInterface interface {
170 //
171 // Smart Contract Methods
172 //
173
174 // Public Functions
175
176 RollupForgeBatch(*RollupForgeBatchArgs) (*types.Transaction, error)
177 RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error)
178 // RollupWithdrawSNARK() (*types.Transaction, error) // TODO (Not defined in Hermez.sol)
179 RollupWithdrawMerkleProof(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey,
180 numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error)
181 RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error)
182 RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error)
183 RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey,
184 loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error)
185 RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16,
186 tokenID int64, toIdx int64) (*types.Transaction, error)
187 RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error)
188 RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte,
189 babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error)
190 RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16,
191 tokenID int64) (*types.Transaction, error)
192
193 RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error)
194 RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error)
195 RollupGetQueue(queue int64) ([]byte, error)
196
197 // Governance Public Functions
198 RollupUpdateForgeL1Timeout(newForgeL1Timeout int64) (*types.Transaction, error)
199 RollupUpdateFeeL1UserTx(newFeeL1UserTx *big.Int) (*types.Transaction, error)
200 RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error)
201 RollupUpdateTokensHEZ(newTokenHEZ ethCommon.Address) (*types.Transaction, error)
202 // RollupUpdateGovernance() (*types.Transaction, error) // TODO (Not defined in Hermez.sol)
203
204 //
205 // Smart Contract Status
206 //
207
208 RollupConstants() (*RollupConstants, error)
209 RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error)
210 RollupForgeBatchArgs(ethCommon.Hash) (*RollupForgeBatchArgs, error)
211}
212
213//
214// Implementation
215//
216
217// RollupClient is the implementation of the interface to the Rollup Smart Contract in ethereum.
218type RollupClient struct {
219 client *EthereumClient
220 address ethCommon.Address
221}
222
223// NewRollupClient creates a new RollupClient
224func NewRollupClient(client *EthereumClient, address ethCommon.Address) *RollupClient {
225 return &RollupClient{
226 client: client,
227 address: address,
228 }
229}
230
231// RollupForgeBatch is the interface to call the smart contract function
232func (c *RollupClient) RollupForgeBatch(args *RollupForgeBatchArgs) (*types.Transaction, error) {
233 log.Error("TODO")
234 return nil, errTODO
235}
236
237// RollupAddToken is the interface to call the smart contract function
238func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error) {
239 log.Error("TODO")
240 return nil, errTODO
241}
242
243// RollupWithdrawSNARK is the interface to call the smart contract function
244// func (c *RollupClient) RollupWithdrawSNARK() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
245// return nil, errTODO
246// }
247
248// RollupWithdrawMerkleProof is the interface to call the smart contract function
249func (c *RollupClient) RollupWithdrawMerkleProof(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey, numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error) {
250 log.Error("TODO")
251 return nil, errTODO
252}
253
254// RollupForceExit is the interface to call the smart contract function
255func (c *RollupClient) RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error) {
256 log.Error("TODO")
257 return nil, errTODO
258}
259
260// RollupForceTransfer is the interface to call the smart contract function
261func (c *RollupClient) RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error) {
262 log.Error("TODO")
263 return nil, errTODO
264}
265
266// RollupCreateAccountDepositTransfer is the interface to call the smart contract function
267func (c *RollupClient) RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
268 log.Error("TODO")
269 return nil, errTODO
270}
271
272// RollupDepositTransfer is the interface to call the smart contract function
273func (c *RollupClient) RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
274 log.Error("TODO")
275 return nil, errTODO
276}
277
278// RollupDeposit is the interface to call the smart contract function
279func (c *RollupClient) RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
280 log.Error("TODO")
281 return nil, errTODO
282}
283
284// RollupCreateAccountDepositFromRelayer is the interface to call the smart contract function
285func (c *RollupClient) RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte, babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error) {
286 log.Error("TODO")
287 return nil, errTODO
288}
289
290// RollupCreateAccountDeposit is the interface to call the smart contract function
291func (c *RollupClient) RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
292 log.Error("TODO")
293 return nil, errTODO
294}
295
296// RollupGetTokenAddress is the interface to call the smart contract function
297func (c *RollupClient) RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error) {
298 log.Error("TODO")
299 return nil, errTODO
300}
301
302// RollupGetL1TxFromQueue is the interface to call the smart contract function
303func (c *RollupClient) RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error) {
304 log.Error("TODO")
305 return nil, errTODO
306}
307
308// RollupGetQueue is the interface to call the smart contract function
309func (c *RollupClient) RollupGetQueue(queue int64) ([]byte, error) {
310 log.Error("TODO")
311 return nil, errTODO
312}
313
314// RollupUpdateForgeL1Timeout is the interface to call the smart contract function
315func (c *RollupClient) RollupUpdateForgeL1Timeout(newForgeL1Timeout int64) (*types.Transaction, error) {
316 log.Error("TODO")
317 return nil, errTODO
318}
319
320// RollupUpdateFeeL1UserTx is the interface to call the smart contract function
321func (c *RollupClient) RollupUpdateFeeL1UserTx(newFeeL1UserTx *big.Int) (*types.Transaction, error) {
322 log.Error("TODO")
323 return nil, errTODO
324}
325
326// RollupUpdateFeeAddToken is the interface to call the smart contract function
327func (c *RollupClient) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error) {
328 log.Error("TODO")
329 return nil, errTODO
330}
331
332// RollupUpdateTokensHEZ is the interface to call the smart contract function
333func (c *RollupClient) RollupUpdateTokensHEZ(newTokenHEZ ethCommon.Address) (*types.Transaction, error) {
334 log.Error("TODO")
335 return nil, errTODO
336}
337
338// RollupUpdateGovernance is the interface to call the smart contract function
339// func (c *RollupClient) RollupUpdateGovernance() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
340// return nil, errTODO
341// }
342
343// RollupConstants returns the Constants of the Rollup Smart Contract
344func (c *RollupClient) RollupConstants() (*RollupConstants, error) {
345 log.Error("TODO")
346 return nil, errTODO
347}
348
349// RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
350func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error) {
351 log.Error("TODO")
352 return nil, nil, errTODO
353}
354
355// RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
356func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*RollupForgeBatchArgs, error) {
357 // tx := client.TransactionByHash(ethTxHash) -> types.Transaction
358 // txData := types.Transaction -> Data()
359 // m := abi.MethodById(txData) -> Method
360 // m.Inputs.Unpack(txData) -> Args
361 // client.TransactionReceipt()?
362 log.Error("TODO")
363 return nil, errTODO
364}