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