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 *big.Int
30 // maximum L1 transactions allowed to be queued for a batch
31 MaxL1Tx *big.Int
32 // maximum L1 user transactions allowed to be queued for a batch
33 MaxL1UserTx *big.Int
34 Rfield *big.Int
35 L1CoordinatorBytes *big.Int
36 L1UserBytes *big.Int
37 L2Bytes *big.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 *big.Int
52 NumBuckets *big.Int
53 MaxWDelay *big.Int
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// RollupEventL1UserTxEvent is an event of the Rollup Smart Contract
93type RollupEventL1UserTxEvent struct {
94 L1Tx common.L1Tx
95 QueueIndex *big.Int
96 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 *big.Int
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 L1UserTxEvent []RollupEventL1UserTxEvent
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 L1UserTxEvent: make([]RollupEventL1UserTxEvent, 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 rollupConstants.L1CoordinatorBytes, err = rollup.L1COORDINATORBYTES(nil)
336 rollupConstants.L1UserBytes, err = rollup.L1USERBYTES(nil)
337 rollupConstants.L2Bytes, err = rollup.L2BYTES(nil)
338 rollupConstants.LastIDx, err = rollup.LASTIDX(nil)
339 rollupConstants.MaxAmountDeposit, err = rollup.MAXLOADAMOUNT(nil)
340 rollupConstants.MaxAmountL2, err = rollup.MAXAMOUNT(nil)
341 rollupConstants.MaxL1Tx, err = rollup.MAXL1TX(nil)
342 rollupConstants.MaxL1UserTx, err = rollup.MAXL1USERTX(nil)
343 rollupConstants.MaxTokens, err = rollup.MAXTOKENS(nil)
344 rollupConstants.MaxWDelay, err = rollup.MAXWITHDRAWALDELAY(nil)
345 rollupConstants.NoLimitToken, err = rollup.NOLIMIT(nil)
346 rollupConstants.NumBuckets, err = rollup.NUMBUCKETS(nil)
347 // rollupConstants.ReservedIDx =
348 rollupConstants.Rfield, err = rollup.RFIELD(nil)
349 // rollupConstants.SafetyBot =
350 // rollupConstants.TokenHEZ =
351 // rollupConstants.WithdrawalContract =
352 return err
353 }); err != nil {
354 return nil, err
355 }
356 return rollupConstants, nil
357}
358
359// RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
360func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error) {
361 log.Error("TODO")
362 return nil, nil, errTODO
363}
364
365// RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
366func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*RollupForgeBatchArgs, error) {
367 // tx := client.TransactionByHash(ethTxHash) -> types.Transaction
368 // txData := types.Transaction -> Data()
369 // m := abi.MethodById(txData) -> Method
370 // m.Inputs.Unpack(txData) -> Args
371 // client.TransactionReceipt()?
372 log.Error("TODO")
373 return nil, errTODO
374}