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/utils"
  9	"github.com/iden3/go-iden3-crypto/babyjub"
 10)
 11
 12// RollupConstants are the constants of the Rollup Smart Contract
 13type RollupConstants struct {
 14	// Maxim Deposit allowed
 15	MaxAmountDeposit *big.Int
 16	MaxAmountL2      *big.Int
 17	MaxTokens        uint32
 18	// maximum L1 transactions allowed to be queued for a batch
 19	MaxL1Tx int
 20	// maximum L1 user transactions allowed to be queued for a batch
 21	MaxL1UserTx        int
 22	Rfield             *big.Int
 23	L1CoordinatorBytes int
 24	L1UserBytes        int
 25	L2Bytes            int
 26}
 27
 28// RollupVariables are the variables of the Rollup Smart Contract
 29type RollupVariables struct {
 30	MaxTxVerifiers     []int
 31	TokenHEZ           ethCommon.Address
 32	GovernanceAddress  ethCommon.Address
 33	SafetyBot          ethCommon.Address
 34	ConsensusContract  ethCommon.Address
 35	WithdrawalContract ethCommon.Address
 36	FeeAddToken        *big.Int
 37	ForgeL1Timeout     int64
 38	FeeL1UserTx        *big.Int
 39}
 40
 41// QueueStruct is the queue of L1Txs for a batch
 42//nolint:structcheck
 43type QueueStruct struct {
 44	L1TxQueue    [][]byte
 45	CurrentIndex int64
 46	TotalL1TxFee *big.Int
 47}
 48
 49// RollupState represents the state of the Rollup in the Smart Contract
 50//nolint:structcheck,unused
 51type RollupState struct {
 52	StateRoot              *big.Int
 53	ExitRoots              []*big.Int
 54	ExiNullifierMap        map[[256 / 8]byte]bool
 55	TokenList              []ethCommon.Address
 56	TokenMap               map[ethCommon.Address]bool
 57	mapL1TxQueue           map[int64]QueueStruct
 58	LastLTxBatch           int64
 59	CurrentToForgeL1TxsNum int64
 60	LastToForgeL1TxsNum    int64
 61	CurrentIdx             int64
 62}
 63
 64// RollupEventL1UserTx is an event of the Rollup Smart Contract
 65type RollupEventL1UserTx struct {
 66	L1UserTx        []byte
 67	ToForgeL1TxsNum int64
 68	Position        int
 69}
 70
 71// RollupEventAddToken is an event of the Rollup Smart Contract
 72type RollupEventAddToken struct {
 73	Address ethCommon.Address
 74	TokenID uint32
 75}
 76
 77// RollupEventForgeBatch is an event of the Rollup Smart Contract
 78type RollupEventForgeBatch struct {
 79	BatchNum int64
 80}
 81
 82// RollupEventUpdateForgeL1Timeout is an event of the Rollup Smart Contract
 83type RollupEventUpdateForgeL1Timeout struct {
 84	ForgeL1Timeout int64
 85}
 86
 87// RollupEventUpdateFeeL1UserTx is an event of the Rollup Smart Contract
 88type RollupEventUpdateFeeL1UserTx struct {
 89	FeeL1UserTx *big.Int
 90}
 91
 92// RollupEventUpdateFeeAddToken is an event of the Rollup Smart Contract
 93type RollupEventUpdateFeeAddToken struct {
 94	FeeAddToken *big.Int
 95}
 96
 97// RollupEventUpdateTokenHez is an event of the Rollup Smart Contract
 98type RollupEventUpdateTokenHez struct {
 99	TokenHEZ ethCommon.Address
100}
101
102// RollupEventWithdraw is an event of the Rollup Smart Contract
103type RollupEventWithdraw struct {
104	Idx         int64
105	NumExitRoot int
106}
107
108// RollupEvents is the list of events in a block of the Rollup Smart Contract
109type RollupEvents struct { //nolint:structcheck
110	L1UserTx             []RollupEventL1UserTx
111	AddToken             []RollupEventAddToken
112	ForgeBatch           []RollupEventForgeBatch
113	UpdateForgeL1Timeout []RollupEventUpdateForgeL1Timeout
114	UpdateFeeL1UserTx    []RollupEventUpdateFeeL1UserTx
115	UpdateFeeAddToken    []RollupEventUpdateFeeAddToken
116	UpdateTokenHez       []RollupEventUpdateTokenHez
117	Withdraw             []RollupEventWithdraw
118}
119
120// RollupForgeBatchArgs are the arguments to the ForgeBatch function in the Rollup Smart Contract
121//nolint:structcheck,unused
122type RollupForgeBatchArgs struct {
123	proofA      [2]*big.Int
124	proofB      [2][2]*big.Int
125	proofC      [2]*big.Int
126	newLastIdx  int64
127	newStRoot   *big.Int
128	newExitRoot *big.Int
129	// TODO: Replace compressedL1CoordinatorTx, l2TxsData, feeIdxCoordinator for vectors
130	compressedL1CoordinatorTx []byte
131	l2TxsData                 []byte
132	feeIdxCoordinator         []byte
133	verifierIdx               int64
134	l1Batch                   bool
135}
136
137// RollupInterface is the inteface to to Rollup Smart Contract
138type RollupInterface interface {
139	//
140	// Smart Contract Methods
141	//
142
143	// Public Functions
144
145	RollupForgeBatch(*RollupForgeBatchArgs) (*types.Transaction, error)
146	RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error)
147	// RollupWithdrawSNARK() (*types.Transaction, error) // TODO (Not defined in Hermez.sol)
148	RollupWithdrawMerkleProof(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey,
149		numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error)
150	RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error)
151	RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error)
152	RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey,
153		loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error)
154	RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16,
155		tokenID int64, toIdx int64) (*types.Transaction, error)
156	RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error)
157	RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte,
158		babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error)
159	RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16,
160		tokenID int64) (*types.Transaction, error)
161
162	RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error)
163	RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error)
164	RollupGetQueue(queue int64) ([]byte, error)
165
166	// Governance Public Functions
167	RollupUpdateForgeL1Timeout(newForgeL1Timeout int64) (*types.Transaction, error)
168	RollupUpdateFeeL1UserTx(newFeeL1UserTx *big.Int) (*types.Transaction, error)
169	RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error)
170	RollupUpdateTokensHEZ(newTokenHEZ ethCommon.Address) (*types.Transaction, error)
171	// RollupUpdateGovernance() (*types.Transaction, error) // TODO (Not defined in Hermez.sol)
172
173	//
174	// Smart Contract Status
175	//
176
177	RollupConstants() (*RollupConstants, error)
178	RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error)
179	RollupForgeBatchArgs(*types.Transaction) (*RollupForgeBatchArgs, error)
180}
181
182//
183// Implementation
184//
185
186// RollupClient is the implementation of the interface to the Rollup Smart Contract in ethereum.
187type RollupClient struct {
188}
189
190// RollupForgeBatch is the interface to call the smart contract function
191func (c *RollupClient) RollupForgeBatch(args *RollupForgeBatchArgs) (*types.Transaction, error) {
192	return nil, errTODO
193}
194
195// RollupAddToken is the interface to call the smart contract function
196func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error) {
197	return nil, errTODO
198}
199
200// RollupWithdrawSNARK is the interface to call the smart contract function
201// func (c *RollupClient) RollupWithdrawSNARK() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
202// 	return nil, errTODO
203// }
204
205// RollupWithdrawMerkleProof is the interface to call the smart contract function
206func (c *RollupClient) RollupWithdrawMerkleProof(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey, numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error) {
207	return nil, errTODO
208}
209
210// RollupForceExit is the interface to call the smart contract function
211func (c *RollupClient) RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error) {
212	return nil, errTODO
213}
214
215// RollupForceTransfer is the interface to call the smart contract function
216func (c *RollupClient) RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error) {
217	return nil, errTODO
218}
219
220// RollupCreateAccountDepositTransfer is the interface to call the smart contract function
221func (c *RollupClient) RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
222	return nil, errTODO
223}
224
225// RollupDepositTransfer is the interface to call the smart contract function
226func (c *RollupClient) RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
227	return nil, errTODO
228}
229
230// RollupDeposit is the interface to call the smart contract function
231func (c *RollupClient) RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
232	return nil, errTODO
233}
234
235// RollupCreateAccountDepositFromRelayer is the interface to call the smart contract function
236func (c *RollupClient) RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte, babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error) {
237	return nil, errTODO
238}
239
240// RollupCreateAccountDeposit is the interface to call the smart contract function
241func (c *RollupClient) RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
242	return nil, errTODO
243}
244
245// RollupGetTokenAddress is the interface to call the smart contract function
246func (c *RollupClient) RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error) {
247	return nil, errTODO
248}
249
250// RollupGetL1TxFromQueue is the interface to call the smart contract function
251func (c *RollupClient) RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error) {
252	return nil, errTODO
253}
254
255// RollupGetQueue is the interface to call the smart contract function
256func (c *RollupClient) RollupGetQueue(queue int64) ([]byte, error) {
257	return nil, errTODO
258}
259
260// RollupUpdateForgeL1Timeout is the interface to call the smart contract function
261func (c *RollupClient) RollupUpdateForgeL1Timeout(newForgeL1Timeout int64) (*types.Transaction, error) {
262	return nil, errTODO
263}
264
265// RollupUpdateFeeL1UserTx is the interface to call the smart contract function
266func (c *RollupClient) RollupUpdateFeeL1UserTx(newFeeL1UserTx *big.Int) (*types.Transaction, error) {
267	return nil, errTODO
268}
269
270// RollupUpdateFeeAddToken is the interface to call the smart contract function
271func (c *RollupClient) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error) {
272	return nil, errTODO
273}
274
275// RollupUpdateTokensHEZ is the interface to call the smart contract function
276func (c *RollupClient) RollupUpdateTokensHEZ(newTokenHEZ ethCommon.Address) (*types.Transaction, error) {
277	return nil, errTODO
278}
279
280// RollupUpdateGovernance is the interface to call the smart contract function
281// func (c *RollupClient) RollupUpdateGovernance() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
282// 	return nil, errTODO
283// }
284
285// RollupConstants returns the Constants of the Rollup Smart Contract
286func (c *RollupClient) RollupConstants() (*RollupConstants, error) {
287	return nil, errTODO
288}
289
290// RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
291func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error) {
292	return nil, nil, errTODO
293}
294
295// RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
296func (c *RollupClient) RollupForgeBatchArgs(transaction *types.Transaction) (*RollupForgeBatchArgs, error) {
297	return nil, errTODO
298}