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/ethereum/go-ethereum/ethclient"
9 "github.com/hermeznetwork/hermez-node/common"
10 Hermez "github.com/hermeznetwork/hermez-node/eth/contracts/hermez"
11 "github.com/hermeznetwork/hermez-node/log"
12 "github.com/hermeznetwork/hermez-node/utils"
13 "github.com/iden3/go-iden3-crypto/babyjub"
14)
15
16const (
17 // FeeIdxCoordinatorLen is the number of tokens the coordinator can use
18 // to collect fees (determines the number of tokens that the
19 // coordinator can collect fees from). This value is determined by the
20 // circuit.
21 FeeIdxCoordinatorLen = 64
22)
23
24// RollupConstants are the constants of the Rollup Smart Contract
25type RollupConstants struct {
26 // Maxim Deposit allowed
27 MaxAmountDeposit *big.Int
28 MaxAmountL2 *big.Int
29 MaxTokens int64
30 // maximum L1 transactions allowed to be queued for a batch
31 MaxL1Tx int
32 // maximum L1 user transactions allowed to be queued for a batch
33 MaxL1UserTx int
34 Rfield *big.Int
35 L1CoordinatorBytes int
36 L1UserBytes int
37 L2Bytes int
38 MaxTxVerifiers []int
39 TokenHEZ ethCommon.Address
40 // Only test
41 GovernanceAddress ethCommon.Address
42 // Only test
43 SafetyBot ethCommon.Address
44 // Only test
45 ConsensusContract ethCommon.Address
46 // Only test
47 WithdrawalContract ethCommon.Address
48 ReservedIDx uint32
49 LastIDx uint32
50 ExitIDx uint32
51 NoLimitToken int
52 NumBuckets int
53 MaxWDelay int64
54}
55
56// RollupVariables are the variables of the Rollup Smart Contract
57type RollupVariables struct {
58 FeeAddToken *big.Int
59 ForgeL1Timeout int64
60}
61
62// QueueStruct is the queue of L1Txs for a batch
63//nolint:structcheck
64type QueueStruct struct {
65 L1TxQueue []common.L1Tx
66 TotalL1TxFee *big.Int
67}
68
69// NewQueueStruct creates a new clear QueueStruct.
70func NewQueueStruct() *QueueStruct {
71 return &QueueStruct{
72 L1TxQueue: make([]common.L1Tx, 0),
73 TotalL1TxFee: big.NewInt(0),
74 }
75}
76
77// RollupState represents the state of the Rollup in the Smart Contract
78//nolint:structcheck,unused
79type RollupState struct {
80 StateRoot *big.Int
81 ExitRoots []*big.Int
82 ExitNullifierMap map[[256 / 8]byte]bool
83 TokenList []ethCommon.Address
84 TokenMap map[ethCommon.Address]bool
85 MapL1TxQueue map[int64]*QueueStruct
86 LastL1L2Batch int64
87 CurrentToForgeL1TxsNum int64
88 LastToForgeL1TxsNum int64
89 CurrentIdx int64
90}
91
92// RollupEventL1UserTx is an event of the Rollup Smart Contract
93type RollupEventL1UserTx struct {
94 L1Tx common.L1Tx
95 ToForgeL1TxsNum int64 // QueueIndex *big.Int
96 Position int // TransactionIndex *big.Int
97}
98
99// RollupEventAddToken is an event of the Rollup Smart Contract
100type RollupEventAddToken struct {
101 Address ethCommon.Address
102 TokenID uint32
103}
104
105// RollupEventForgeBatch is an event of the Rollup Smart Contract
106type RollupEventForgeBatch struct {
107 BatchNum int64
108 EthTxHash ethCommon.Hash
109}
110
111// RollupEventUpdateForgeL1L2BatchTimeout is an event of the Rollup Smart Contract
112type RollupEventUpdateForgeL1L2BatchTimeout struct {
113 ForgeL1Timeout *big.Int
114}
115
116// RollupEventUpdateFeeAddToken is an event of the Rollup Smart Contract
117type RollupEventUpdateFeeAddToken struct {
118 FeeAddToken *big.Int
119}
120
121// RollupEventWithdrawEvent is an event of the Rollup Smart Contract
122type RollupEventWithdrawEvent struct {
123 Idx *big.Int
124 NumExitRoot *big.Int
125 InstantWithdraw bool
126}
127
128// RollupEvents is the list of events in a block of the Rollup Smart Contract
129type RollupEvents struct { //nolint:structcheck
130 L1UserTx []RollupEventL1UserTx
131 AddToken []RollupEventAddToken
132 ForgeBatch []RollupEventForgeBatch
133 UpdateForgeL1L2BatchTimeout []RollupEventUpdateForgeL1L2BatchTimeout
134 UpdateFeeAddToken []RollupEventUpdateFeeAddToken
135 WithdrawEvent []RollupEventWithdrawEvent
136}
137
138// NewRollupEvents creates an empty RollupEvents with the slices initialized.
139func NewRollupEvents() RollupEvents {
140 return RollupEvents{
141 L1UserTx: make([]RollupEventL1UserTx, 0),
142 AddToken: make([]RollupEventAddToken, 0),
143 ForgeBatch: make([]RollupEventForgeBatch, 0),
144 UpdateForgeL1L2BatchTimeout: make([]RollupEventUpdateForgeL1L2BatchTimeout, 0),
145 UpdateFeeAddToken: make([]RollupEventUpdateFeeAddToken, 0),
146 WithdrawEvent: make([]RollupEventWithdrawEvent, 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 RollupWithdraw(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey,
179 numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error)
180 RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error)
181 RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error)
182 RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey,
183 loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error)
184 RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16,
185 tokenID int64, toIdx int64) (*types.Transaction, error)
186 RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error)
187 RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte,
188 babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error)
189 RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16,
190 tokenID int64) (*types.Transaction, error)
191
192 RollupGetCurrentTokens() (*big.Int, error)
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 RollupUpdateForgeL1L2BatchTimeout(newForgeL1Timeout int64) (*types.Transaction, error)
199 RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error)
200
201 //
202 // Smart Contract Status
203 //
204
205 RollupConstants() (*RollupConstants, error)
206 RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error)
207 RollupForgeBatchArgs(ethCommon.Hash) (*RollupForgeBatchArgs, error)
208}
209
210//
211// Implementation
212//
213
214// RollupClient is the implementation of the interface to the Rollup Smart Contract in ethereum.
215type RollupClient struct {
216 client *EthereumClient
217 address ethCommon.Address
218}
219
220// NewRollupClient creates a new RollupClient
221func NewRollupClient(client *EthereumClient, address ethCommon.Address) *RollupClient {
222 return &RollupClient{
223 client: client,
224 address: address,
225 }
226}
227
228// RollupForgeBatch is the interface to call the smart contract function
229func (c *RollupClient) RollupForgeBatch(args *RollupForgeBatchArgs) (*types.Transaction, error) {
230 log.Error("TODO")
231 return nil, errTODO
232}
233
234// RollupAddToken is the interface to call the smart contract function
235func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error) {
236 log.Error("TODO")
237 return nil, errTODO
238}
239
240// RollupWithdrawSNARK is the interface to call the smart contract function
241// func (c *RollupClient) RollupWithdrawSNARK() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
242// return nil, errTODO
243// }
244
245// RollupWithdraw is the interface to call the smart contract function
246func (c *RollupClient) RollupWithdraw(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey, numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error) {
247 log.Error("TODO")
248 return nil, errTODO
249}
250
251// RollupForceExit is the interface to call the smart contract function
252func (c *RollupClient) RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error) {
253 log.Error("TODO")
254 return nil, errTODO
255}
256
257// RollupForceTransfer is the interface to call the smart contract function
258func (c *RollupClient) RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error) {
259 log.Error("TODO")
260 return nil, errTODO
261}
262
263// RollupCreateAccountDepositTransfer is the interface to call the smart contract function
264func (c *RollupClient) RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
265 log.Error("TODO")
266 return nil, errTODO
267}
268
269// RollupDepositTransfer is the interface to call the smart contract function
270func (c *RollupClient) RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
271 log.Error("TODO")
272 return nil, errTODO
273}
274
275// RollupDeposit is the interface to call the smart contract function
276func (c *RollupClient) RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
277 log.Error("TODO")
278 return nil, errTODO
279}
280
281// RollupCreateAccountDepositFromRelayer is the interface to call the smart contract function
282func (c *RollupClient) RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte, babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error) {
283 log.Error("TODO")
284 return nil, errTODO
285}
286
287// RollupCreateAccountDeposit is the interface to call the smart contract function
288func (c *RollupClient) RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
289 log.Error("TODO")
290 return nil, errTODO
291}
292
293// RollupGetTokenAddress is the interface to call the smart contract function
294/* func (c *RollupClient) RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error) {
295 return nil, errTODO
296} */
297
298// RollupGetCurrentTokens is the interface to call the smart contract function
299func (c *RollupClient) RollupGetCurrentTokens() (*big.Int, error) {
300 log.Error("TODO")
301 return nil, errTODO
302}
303
304// RollupGetL1TxFromQueue is the interface to call the smart contract function
305/* func (c *RollupClient) RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error) {
306 return nil, errTODO
307} */
308
309// RollupGetQueue is the interface to call the smart contract function
310/* func (c *RollupClient) RollupGetQueue(queue int64) ([]byte, error) {
311 return nil, errTODO
312}*/
313
314// RollupUpdateForgeL1L2BatchTimeout is the interface to call the smart contract function
315func (c *RollupClient) RollupUpdateForgeL1L2BatchTimeout(newForgeL1Timeout int64) (*types.Transaction, error) {
316 log.Error("TODO")
317 return nil, errTODO
318}
319
320// RollupUpdateFeeAddToken is the interface to call the smart contract function
321func (c *RollupClient) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error) {
322 log.Error("TODO")
323 return nil, errTODO
324}
325
326// RollupConstants returns the Constants of the Rollup Smart Contract
327func (c *RollupClient) RollupConstants() (*RollupConstants, error) {
328 rollupConstants := new(RollupConstants)
329 if err := c.client.Call(func(ec *ethclient.Client) error {
330 rollup, err := Hermez.NewHermez(c.address, ec)
331 if err != nil {
332 return err
333 }
334 // rollupConstants.GovernanceAddress :=
335 l1CoordinatorBytes, err := rollup.L1COORDINATORBYTES(nil)
336 if err != nil {
337 return err
338 }
339 rollupConstants.L1CoordinatorBytes = int(l1CoordinatorBytes.Int64())
340 l1UserBytes, err := rollup.L1USERBYTES(nil)
341 if err != nil {
342 return err
343 }
344 rollupConstants.L1UserBytes = int(l1UserBytes.Int64())
345 l2Bytes, err := rollup.L2BYTES(nil)
346 if err != nil {
347 return err
348 }
349 rollupConstants.L2Bytes = int(l2Bytes.Int64())
350 rollupConstants.LastIDx, err = rollup.LASTIDX(nil)
351 if err != nil {
352 return err
353 }
354 rollupConstants.MaxAmountDeposit, err = rollup.MAXLOADAMOUNT(nil)
355 if err != nil {
356 return err
357 }
358 rollupConstants.MaxAmountL2, err = rollup.MAXAMOUNT(nil)
359 if err != nil {
360 return err
361 }
362 maxL1Tx, err := rollup.MAXL1TX(nil)
363 if err != nil {
364 return err
365 }
366 rollupConstants.MaxL1Tx = int(maxL1Tx.Int64())
367 maxL1UserTx, err := rollup.MAXL1USERTX(nil)
368 if err != nil {
369 return err
370 }
371 rollupConstants.MaxL1UserTx = int(maxL1UserTx.Int64())
372 maxTokens, err := rollup.MAXTOKENS(nil)
373 if err != nil {
374 return err
375 }
376 rollupConstants.MaxTokens = maxTokens.Int64()
377 maxWDelay, err := rollup.MAXWITHDRAWALDELAY(nil)
378 if err != nil {
379 return err
380 }
381 rollupConstants.MaxWDelay = maxWDelay.Int64()
382 noLimitToken, err := rollup.NOLIMIT(nil)
383 if err != nil {
384 return err
385 }
386 rollupConstants.NoLimitToken = int(noLimitToken.Int64())
387 numBuckets, err := rollup.NUMBUCKETS(nil)
388 if err != nil {
389 return err
390 }
391 rollupConstants.NumBuckets = int(numBuckets.Int64())
392 // rollupConstants.ReservedIDx =
393 rollupConstants.Rfield, err = rollup.RFIELD(nil)
394 if err != nil {
395 return err
396 }
397 // rollupConstants.SafetyBot =
398 // rollupConstants.TokenHEZ =
399 // rollupConstants.WithdrawalContract =
400 return nil
401 }); err != nil {
402 return nil, err
403 }
404 return rollupConstants, nil
405}
406
407// RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
408func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error) {
409 log.Error("TODO")
410 return nil, nil, errTODO
411}
412
413// RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
414func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*RollupForgeBatchArgs, error) {
415 // tx := client.TransactionByHash(ethTxHash) -> types.Transaction
416 // txData := types.Transaction -> Data()
417 // m := abi.MethodById(txData) -> Method
418 // m.Inputs.Unpack(txData) -> Args
419 // client.TransactionReceipt()?
420 log.Error("TODO")
421 return nil, errTODO
422}