hermez-node/test/ethclient.go
test/ethclient.goRaw
1package test
2
3import (
4 "context"
5 "fmt"
6 "math/big"
7 "time"
8
9 ethCommon "github.com/ethereum/go-ethereum/common"
10 "github.com/ethereum/go-ethereum/core/types"
11 "github.com/hermeznetwork/hermez-node/common"
12 "github.com/hermeznetwork/hermez-node/eth"
13 "github.com/hermeznetwork/hermez-node/log"
14 "github.com/hermeznetwork/hermez-node/utils"
15 "github.com/iden3/go-iden3-crypto/babyjub"
16)
17
18// Client implements the eth.ClientInterface interface, allowing to manipulate the
19// values for testing, working with deterministic results.
20type Client struct {
21 log bool
22 blockNum *big.Int
23}
24
25// NewClient returns a new test Client that implements the eth.IClient
26// interface, at the given initialBlockNumber.
27func NewClient(l bool, initialBlockNumber int64) *Client {
28 return &Client{
29 log: l,
30 blockNum: big.NewInt(initialBlockNumber),
31 }
32}
33
34// Advance moves one block forward
35func (c *Client) Advance() {
36 c.blockNum = c.blockNum.Add(c.blockNum, big.NewInt(1))
37 if c.log {
38 log.Debugf("TestEthClient blockNum advanced: %d", c.blockNum)
39 }
40}
41
42// SetBlockNum sets the Client.blockNum to the given blockNum
43func (c *Client) SetBlockNum(blockNum *big.Int) {
44 c.blockNum = blockNum
45 if c.log {
46 log.Debugf("TestEthClient blockNum set to: %d", c.blockNum)
47 }
48}
49
50// EthCurrentBlock returns the current blockNum
51func (c *Client) EthCurrentBlock() (*big.Int, error) {
52 return c.blockNum, nil
53}
54
55func newHeader(number *big.Int) *types.Header {
56 return &types.Header{
57 Number: number,
58 Time: uint64(number.Int64()),
59 }
60}
61
62// EthHeaderByNumber returns the *types.Header for the given block number in a
63// deterministic way.
64func (c *Client) EthHeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
65 return newHeader(number), nil
66}
67
68// EthBlockByNumber returns the *common.Block for the given block number in a
69// deterministic way.
70func (c *Client) EthBlockByNumber(ctx context.Context, number *big.Int) (*common.Block, error) {
71 header := newHeader(number)
72
73 return &common.Block{
74 EthBlockNum: uint64(number.Int64()),
75 Timestamp: time.Unix(number.Int64(), 0),
76 Hash: header.Hash(),
77 }, nil
78}
79
80var errTODO = fmt.Errorf("TODO: Not implemented yet")
81
82//
83// Rollup
84//
85
86// RollupForgeBatch is the interface to call the smart contract function
87func (c *Client) RollupForgeBatch(*eth.RollupForgeBatchArgs) (*types.Transaction, error) {
88 return nil, errTODO
89}
90
91// RollupAddToken is the interface to call the smart contract function
92func (c *Client) RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error) {
93 return nil, errTODO
94}
95
96// RollupWithdrawSNARK is the interface to call the smart contract function
97// func (c *Client) RollupWithdrawSNARK() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
98// return nil, errTODO
99// }
100
101// RollupWithdrawMerkleProof is the interface to call the smart contract function
102func (c *Client) RollupWithdrawMerkleProof(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey, numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error) {
103 return nil, errTODO
104}
105
106// RollupForceExit is the interface to call the smart contract function
107func (c *Client) RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error) {
108 return nil, errTODO
109}
110
111// RollupForceTransfer is the interface to call the smart contract function
112func (c *Client) RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error) {
113 return nil, errTODO
114}
115
116// RollupCreateAccountDepositTransfer is the interface to call the smart contract function
117func (c *Client) RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
118 return nil, errTODO
119}
120
121// RollupDepositTransfer is the interface to call the smart contract function
122func (c *Client) RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
123 return nil, errTODO
124}
125
126// RollupDeposit is the interface to call the smart contract function
127func (c *Client) RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
128 return nil, errTODO
129}
130
131// RollupCreateAccountDepositFromRelayer is the interface to call the smart contract function
132func (c *Client) RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte, babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error) {
133 return nil, errTODO
134}
135
136// RollupCreateAccountDeposit is the interface to call the smart contract function
137func (c *Client) RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
138 return nil, errTODO
139}
140
141// RollupGetTokenAddress is the interface to call the smart contract function
142func (c *Client) RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error) {
143 return nil, errTODO
144}
145
146// RollupGetL1TxFromQueue is the interface to call the smart contract function
147func (c *Client) RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error) {
148 return nil, errTODO
149}
150
151// RollupGetQueue is the interface to call the smart contract function
152func (c *Client) RollupGetQueue(queue int64) ([]byte, error) {
153 return nil, errTODO
154}
155
156// RollupUpdateForgeL1Timeout is the interface to call the smart contract function
157func (c *Client) RollupUpdateForgeL1Timeout(newForgeL1Timeout int64) (*types.Transaction, error) {
158 return nil, errTODO
159}
160
161// RollupUpdateFeeL1UserTx is the interface to call the smart contract function
162func (c *Client) RollupUpdateFeeL1UserTx(newFeeL1UserTx *big.Int) (*types.Transaction, error) {
163 return nil, errTODO
164}
165
166// RollupUpdateFeeAddToken is the interface to call the smart contract function
167func (c *Client) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error) {
168 return nil, errTODO
169}
170
171// RollupUpdateTokensHEZ is the interface to call the smart contract function
172func (c *Client) RollupUpdateTokensHEZ(newTokenHEZ ethCommon.Address) (*types.Transaction, error) {
173 return nil, errTODO
174}
175
176// RollupUpdateGovernance is the interface to call the smart contract function
177// func (c *Client) RollupUpdateGovernance() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
178// return nil, errTODO
179// }
180
181// RollupConstants returns the Constants of the Rollup Smart Contract
182func (c *Client) RollupConstants() (*eth.RollupConstants, error) {
183 return nil, errTODO
184}
185
186// RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
187func (c *Client) RollupEventsByBlock(blockNum int64) (*eth.RollupEvents, *ethCommon.Hash, error) {
188 return nil, nil, errTODO
189}
190
191// RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
192func (c *Client) RollupForgeBatchArgs(transaction *types.Transaction) (*eth.RollupForgeBatchArgs, error) {
193 return nil, errTODO
194}
195
196//
197// Auction
198//
199
200// AuctionSetSlotDeadline is the interface to call the smart contract function
201func (c *Client) AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error) {
202 return nil, errTODO
203}
204
205// AuctionGetSlotDeadline is the interface to call the smart contract function
206func (c *Client) AuctionGetSlotDeadline() (uint8, error) {
207 return 0, errTODO
208}
209
210// AuctionSetOpenAuctionSlots is the interface to call the smart contract function
211func (c *Client) AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error) {
212 return nil, errTODO
213}
214
215// AuctionGetOpenAuctionSlots is the interface to call the smart contract function
216func (c *Client) AuctionGetOpenAuctionSlots() (uint16, error) { return 0, errTODO }
217
218// AuctionSetClosedAuctionSlots is the interface to call the smart contract function
219func (c *Client) AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error) {
220 return nil, errTODO
221}
222
223// AuctionGetClosedAuctionSlots is the interface to call the smart contract function
224func (c *Client) AuctionGetClosedAuctionSlots() (uint16, error) {
225 return 0, errTODO
226}
227
228// AuctionSetOutbidding is the interface to call the smart contract function
229func (c *Client) AuctionSetOutbidding(newOutbidding uint8) (*types.Transaction, error) {
230 return nil, errTODO
231}
232
233// AuctionGetOutbidding is the interface to call the smart contract function
234func (c *Client) AuctionGetOutbidding() (uint8, error) {
235 return 0, errTODO
236}
237
238// AuctionSetAllocationRatio is the interface to call the smart contract function
239func (c *Client) AuctionSetAllocationRatio(newAllocationRatio [3]uint8) (*types.Transaction, error) {
240 return nil, errTODO
241}
242
243// AuctionGetAllocationRatio is the interface to call the smart contract function
244func (c *Client) AuctionGetAllocationRatio() ([3]uint8, error) {
245 return [3]uint8{}, errTODO
246}
247
248// AuctionSetDonationAddress is the interface to call the smart contract function
249func (c *Client) AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error) {
250 return nil, errTODO
251}
252
253// AuctionGetDonationAddress is the interface to call the smart contract function
254func (c *Client) AuctionGetDonationAddress() (*ethCommon.Address, error) {
255 return nil, errTODO
256}
257
258// AuctionSetBootCoordinator is the interface to call the smart contract function
259func (c *Client) AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error) {
260 return nil, errTODO
261}
262
263// AuctionGetBootCoordinator is the interface to call the smart contract function
264func (c *Client) AuctionGetBootCoordinator() (*ethCommon.Address, error) {
265 return nil, errTODO
266}
267
268// AuctionChangeEpochMinBid is the interface to call the smart contract function
269func (c *Client) AuctionChangeEpochMinBid(slotEpoch int64, newInitialMinBid *big.Int) (*types.Transaction, error) {
270 return nil, errTODO
271}
272
273// AuctionRegisterCoordinator is the interface to call the smart contract function
274func (c *Client) AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error) {
275 return nil, errTODO
276}
277
278// AuctionIsRegisteredCoordinator is the interface to call the smart contract function
279func (c *Client) AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error) {
280 return false, errTODO
281}
282
283// AuctionUpdateCoordinatorInfo is the interface to call the smart contract function
284func (c *Client) AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error) {
285 return nil, errTODO
286}
287
288// AuctionGetCurrentSlotNumber is the interface to call the smart contract function
289func (c *Client) AuctionGetCurrentSlotNumber() (int64, error) {
290 return 0, errTODO
291}
292
293// AuctionGetMinBidBySlot is the interface to call the smart contract function
294func (c *Client) AuctionGetMinBidBySlot(slot int64) (*big.Int, error) {
295 return nil, errTODO
296}
297
298// AuctionGetMinBidEpoch is the interface to call the smart contract function
299func (c *Client) AuctionGetMinBidEpoch(epoch uint8) (*big.Int, error) {
300 return nil, errTODO
301}
302
303// AuctionTokensReceived is the interface to call the smart contract function
304// func (c *Client) AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int, userData, operatorData []byte) error {
305// return errTODO
306// }
307
308// AuctionBid is the interface to call the smart contract function
309func (c *Client) AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
310 return nil, errTODO
311}
312
313// AuctionMultiBid is the interface to call the smart contract function
314func (c *Client) AuctionMultiBid(startingSlot int64, endingSlot int64, slotEpoch [6]bool, maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
315 return nil, errTODO
316}
317
318// AuctionCanForge is the interface to call the smart contract function
319func (c *Client) AuctionCanForge(forger ethCommon.Address) (bool, error) {
320 return false, errTODO
321}
322
323// AuctionForge is the interface to call the smart contract function
324// func (c *Client) AuctionForge(forger ethCommon.Address) (bool, error) {
325// return false, errTODO
326// }
327
328// AuctionClaimHEZ is the interface to call the smart contract function
329func (c *Client) AuctionClaimHEZ() (*types.Transaction, error) {
330 return nil, errTODO
331}
332
333// AuctionConstants returns the Constants of the Auction Smart Contract
334func (c *Client) AuctionConstants() (*eth.AuctionConstants, error) {
335 return nil, errTODO
336}
337
338// AuctionEventsByBlock returns the events in a block that happened in the Auction Smart Contract
339func (c *Client) AuctionEventsByBlock(blockNum int64) (*eth.AuctionEvents, *ethCommon.Hash, error) {
340 return nil, nil, errTODO
341}