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	L2Txs             []*common.L2Tx
160	FeeIdxCoordinator []common.Idx
161	// Circuit selector
162	VerifierIdx int64
163	L1Batch     bool
164}
165
166// RollupInterface is the inteface to to Rollup Smart Contract
167type RollupInterface interface {
168	//
169	// Smart Contract Methods
170	//
171
172	// Public Functions
173
174	RollupForgeBatch(*RollupForgeBatchArgs) (*types.Transaction, error)
175	RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error)
176	// RollupWithdrawSNARK() (*types.Transaction, error) // TODO (Not defined in Hermez.sol)
177	RollupWithdrawMerkleProof(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey,
178		numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error)
179	RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error)
180	RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error)
181	RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey,
182		loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error)
183	RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16,
184		tokenID int64, toIdx int64) (*types.Transaction, error)
185	RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error)
186	RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte,
187		babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error)
188	RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16,
189		tokenID int64) (*types.Transaction, error)
190
191	RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error)
192	RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error)
193	RollupGetQueue(queue int64) ([]byte, error)
194
195	// Governance Public Functions
196	RollupUpdateForgeL1Timeout(newForgeL1Timeout int64) (*types.Transaction, error)
197	RollupUpdateFeeL1UserTx(newFeeL1UserTx *big.Int) (*types.Transaction, error)
198	RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error)
199	RollupUpdateTokensHEZ(newTokenHEZ ethCommon.Address) (*types.Transaction, error)
200	// RollupUpdateGovernance() (*types.Transaction, error) // TODO (Not defined in Hermez.sol)
201
202	//
203	// Smart Contract Status
204	//
205
206	RollupConstants() (*RollupConstants, error)
207	RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error)
208	RollupForgeBatchArgs(ethCommon.Hash) (*RollupForgeBatchArgs, error)
209}
210
211//
212// Implementation
213//
214
215// RollupClient is the implementation of the interface to the Rollup Smart Contract in ethereum.
216type RollupClient struct {
217}
218
219// RollupForgeBatch is the interface to call the smart contract function
220func (c *RollupClient) RollupForgeBatch(args *RollupForgeBatchArgs) (*types.Transaction, error) {
221	return nil, errTODO
222}
223
224// RollupAddToken is the interface to call the smart contract function
225func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error) {
226	return nil, errTODO
227}
228
229// RollupWithdrawSNARK is the interface to call the smart contract function
230// func (c *RollupClient) RollupWithdrawSNARK() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
231// 	return nil, errTODO
232// }
233
234// RollupWithdrawMerkleProof is the interface to call the smart contract function
235func (c *RollupClient) RollupWithdrawMerkleProof(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey, numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error) {
236	return nil, errTODO
237}
238
239// RollupForceExit is the interface to call the smart contract function
240func (c *RollupClient) RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error) {
241	return nil, errTODO
242}
243
244// RollupForceTransfer is the interface to call the smart contract function
245func (c *RollupClient) RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error) {
246	return nil, errTODO
247}
248
249// RollupCreateAccountDepositTransfer is the interface to call the smart contract function
250func (c *RollupClient) RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
251	return nil, errTODO
252}
253
254// RollupDepositTransfer is the interface to call the smart contract function
255func (c *RollupClient) RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
256	return nil, errTODO
257}
258
259// RollupDeposit is the interface to call the smart contract function
260func (c *RollupClient) RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
261	return nil, errTODO
262}
263
264// RollupCreateAccountDepositFromRelayer is the interface to call the smart contract function
265func (c *RollupClient) RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte, babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error) {
266	return nil, errTODO
267}
268
269// RollupCreateAccountDeposit is the interface to call the smart contract function
270func (c *RollupClient) RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
271	return nil, errTODO
272}
273
274// RollupGetTokenAddress is the interface to call the smart contract function
275func (c *RollupClient) RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error) {
276	return nil, errTODO
277}
278
279// RollupGetL1TxFromQueue is the interface to call the smart contract function
280func (c *RollupClient) RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error) {
281	return nil, errTODO
282}
283
284// RollupGetQueue is the interface to call the smart contract function
285func (c *RollupClient) RollupGetQueue(queue int64) ([]byte, error) {
286	return nil, errTODO
287}
288
289// RollupUpdateForgeL1Timeout is the interface to call the smart contract function
290func (c *RollupClient) RollupUpdateForgeL1Timeout(newForgeL1Timeout int64) (*types.Transaction, error) {
291	return nil, errTODO
292}
293
294// RollupUpdateFeeL1UserTx is the interface to call the smart contract function
295func (c *RollupClient) RollupUpdateFeeL1UserTx(newFeeL1UserTx *big.Int) (*types.Transaction, error) {
296	return nil, errTODO
297}
298
299// RollupUpdateFeeAddToken is the interface to call the smart contract function
300func (c *RollupClient) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error) {
301	return nil, errTODO
302}
303
304// RollupUpdateTokensHEZ is the interface to call the smart contract function
305func (c *RollupClient) RollupUpdateTokensHEZ(newTokenHEZ ethCommon.Address) (*types.Transaction, error) {
306	return nil, errTODO
307}
308
309// RollupUpdateGovernance is the interface to call the smart contract function
310// func (c *RollupClient) RollupUpdateGovernance() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
311// 	return nil, errTODO
312// }
313
314// RollupConstants returns the Constants of the Rollup Smart Contract
315func (c *RollupClient) RollupConstants() (*RollupConstants, error) {
316	return nil, errTODO
317}
318
319// RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
320func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error) {
321	return nil, nil, errTODO
322}
323
324// RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
325func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*RollupForgeBatchArgs, error) {
326	// tx := client.TransactionByHash(ethTxHash) -> types.Transaction
327	// txData := types.Transaction -> Data()
328	// m := abi.MethodById(txData) -> Method
329	// m.Inputs.Unpack(txData) -> Args
330	// client.TransactionReceipt()?
331	return nil, errTODO
332}