arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
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}
220
221// RollupForgeBatch is the interface to call the smart contract function
222func (c *RollupClient) RollupForgeBatch(args *RollupForgeBatchArgs) (*types.Transaction, error) {
223	log.Error("TODO")
224	return nil, errTODO
225}
226
227// RollupAddToken is the interface to call the smart contract function
228func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error) {
229	log.Error("TODO")
230	return nil, errTODO
231}
232
233// RollupWithdrawSNARK is the interface to call the smart contract function
234// func (c *RollupClient) RollupWithdrawSNARK() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
235// 	return nil, errTODO
236// }
237
238// RollupWithdrawMerkleProof is the interface to call the smart contract function
239func (c *RollupClient) RollupWithdrawMerkleProof(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey, numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error) {
240	log.Error("TODO")
241	return nil, errTODO
242}
243
244// RollupForceExit is the interface to call the smart contract function
245func (c *RollupClient) RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error) {
246	log.Error("TODO")
247	return nil, errTODO
248}
249
250// RollupForceTransfer is the interface to call the smart contract function
251func (c *RollupClient) RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error) {
252	log.Error("TODO")
253	return nil, errTODO
254}
255
256// RollupCreateAccountDepositTransfer is the interface to call the smart contract function
257func (c *RollupClient) RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
258	log.Error("TODO")
259	return nil, errTODO
260}
261
262// RollupDepositTransfer is the interface to call the smart contract function
263func (c *RollupClient) RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
264	log.Error("TODO")
265	return nil, errTODO
266}
267
268// RollupDeposit is the interface to call the smart contract function
269func (c *RollupClient) RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
270	log.Error("TODO")
271	return nil, errTODO
272}
273
274// RollupCreateAccountDepositFromRelayer is the interface to call the smart contract function
275func (c *RollupClient) RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte, babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error) {
276	log.Error("TODO")
277	return nil, errTODO
278}
279
280// RollupCreateAccountDeposit is the interface to call the smart contract function
281func (c *RollupClient) RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
282	log.Error("TODO")
283	return nil, errTODO
284}
285
286// RollupGetTokenAddress is the interface to call the smart contract function
287func (c *RollupClient) RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error) {
288	log.Error("TODO")
289	return nil, errTODO
290}
291
292// RollupGetL1TxFromQueue is the interface to call the smart contract function
293func (c *RollupClient) RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error) {
294	log.Error("TODO")
295	return nil, errTODO
296}
297
298// RollupGetQueue is the interface to call the smart contract function
299func (c *RollupClient) RollupGetQueue(queue int64) ([]byte, error) {
300	log.Error("TODO")
301	return nil, errTODO
302}
303
304// RollupUpdateForgeL1Timeout is the interface to call the smart contract function
305func (c *RollupClient) RollupUpdateForgeL1Timeout(newForgeL1Timeout int64) (*types.Transaction, error) {
306	log.Error("TODO")
307	return nil, errTODO
308}
309
310// RollupUpdateFeeL1UserTx is the interface to call the smart contract function
311func (c *RollupClient) RollupUpdateFeeL1UserTx(newFeeL1UserTx *big.Int) (*types.Transaction, error) {
312	log.Error("TODO")
313	return nil, errTODO
314}
315
316// RollupUpdateFeeAddToken is the interface to call the smart contract function
317func (c *RollupClient) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error) {
318	log.Error("TODO")
319	return nil, errTODO
320}
321
322// RollupUpdateTokensHEZ is the interface to call the smart contract function
323func (c *RollupClient) RollupUpdateTokensHEZ(newTokenHEZ ethCommon.Address) (*types.Transaction, error) {
324	log.Error("TODO")
325	return nil, errTODO
326}
327
328// RollupUpdateGovernance is the interface to call the smart contract function
329// func (c *RollupClient) RollupUpdateGovernance() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
330// 	return nil, errTODO
331// }
332
333// RollupConstants returns the Constants of the Rollup Smart Contract
334func (c *RollupClient) RollupConstants() (*RollupConstants, error) {
335	log.Error("TODO")
336	return nil, errTODO
337}
338
339// RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
340func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error) {
341	log.Error("TODO")
342	return nil, nil, errTODO
343}
344
345// RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
346func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*RollupForgeBatchArgs, error) {
347	// tx := client.TransactionByHash(ethTxHash) -> types.Transaction
348	// txData := types.Transaction -> Data()
349	// m := abi.MethodById(txData) -> Method
350	// m.Inputs.Unpack(txData) -> Args
351	// client.TransactionReceipt()?
352	log.Error("TODO")
353	return nil, errTODO
354}