arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/synchronizer/synchronizer_test.go
synchronizer/synchronizer_test.goRaw
  1package synchronizer
  2
  3import (
  4	"context"
  5	"encoding/json"
  6	"fmt"
  7	"io/ioutil"
  8	"math/big"
  9	"os"
 10	"testing"
 11
 12	ethCommon "github.com/ethereum/go-ethereum/common"
 13	"github.com/hermeznetwork/hermez-node/common"
 14	dbUtils "github.com/hermeznetwork/hermez-node/db"
 15	"github.com/hermeznetwork/hermez-node/db/historydb"
 16	"github.com/hermeznetwork/hermez-node/db/statedb"
 17	"github.com/hermeznetwork/hermez-node/eth"
 18	"github.com/hermeznetwork/hermez-node/test"
 19	"github.com/hermeznetwork/hermez-node/test/til"
 20	"github.com/jinzhu/copier"
 21	"github.com/stretchr/testify/assert"
 22	"github.com/stretchr/testify/require"
 23)
 24
 25var tokenConsts = map[common.TokenID]eth.ERC20Consts{}
 26var forceExits = map[int64][]common.ExitInfo{} // ForgeL1TxsNum -> []exit
 27var nonces = map[common.Idx]common.Nonce{}
 28
 29type timer struct {
 30	time int64
 31}
 32
 33func (t *timer) Time() int64 {
 34	currentTime := t.time
 35	t.time++
 36	return currentTime
 37}
 38
 39// Check Sync output and HistoryDB state against expected values generated by
 40// til
 41func checkSyncBlock(t *testing.T, s *Synchronizer, blockNum int, block, syncBlock *common.BlockData) {
 42	// Check Blocks
 43	dbBlocks, err := s.historyDB.GetAllBlocks()
 44	require.Nil(t, err)
 45	dbBlocks = dbBlocks[1:] // ignore block 0, added by default in the DB
 46	assert.Equal(t, blockNum, len(dbBlocks))
 47	assert.Equal(t, int64(blockNum), dbBlocks[blockNum-1].EthBlockNum)
 48	assert.NotEqual(t, dbBlocks[blockNum-1].Hash, dbBlocks[blockNum-2].Hash)
 49	assert.Greater(t, dbBlocks[blockNum-1].Timestamp.Unix(), dbBlocks[blockNum-2].Timestamp.Unix())
 50
 51	// Check Tokens
 52	assert.Equal(t, len(block.AddedTokens), len(syncBlock.AddedTokens))
 53	dbTokens, err := s.historyDB.GetAllTokens()
 54	require.Nil(t, err)
 55	dbTokens = dbTokens[1:] // ignore token 0, added by default in the DB
 56	for i, token := range block.AddedTokens {
 57		dbToken := dbTokens[i]
 58		syncToken := syncBlock.AddedTokens[i]
 59
 60		assert.Equal(t, block.Block.EthBlockNum, syncToken.EthBlockNum)
 61		assert.Equal(t, token.TokenID, syncToken.TokenID)
 62		assert.Equal(t, token.EthAddr, syncToken.EthAddr)
 63		tokenConst := tokenConsts[token.TokenID]
 64		assert.Equal(t, tokenConst.Name, syncToken.Name)
 65		assert.Equal(t, tokenConst.Symbol, syncToken.Symbol)
 66		assert.Equal(t, tokenConst.Decimals, syncToken.Decimals)
 67
 68		var tokenCpy historydb.TokenWithUSD
 69		//nolint:gosec
 70		require.Nil(t, copier.Copy(&tokenCpy, &token))      // copy common.Token to historydb.TokenWithUSD
 71		require.Nil(t, copier.Copy(&tokenCpy, &tokenConst)) // copy common.Token to historydb.TokenWithUSD
 72		tokenCpy.ItemID = dbToken.ItemID                    // we don't care about ItemID
 73		assert.Equal(t, tokenCpy, dbToken)
 74	}
 75
 76	// Check L1UserTxs
 77	assert.Equal(t, len(block.L1UserTxs), len(syncBlock.L1UserTxs))
 78	dbL1UserTxs, err := s.historyDB.GetAllL1UserTxs()
 79	require.Nil(t, err)
 80	// Ignore BatchNum in syncBlock.L1UserTxs because this value is set by the HistoryDB
 81	for i := range syncBlock.L1UserTxs {
 82		syncBlock.L1UserTxs[i].BatchNum = block.L1UserTxs[i].BatchNum
 83	}
 84	assert.Equal(t, block.L1UserTxs, syncBlock.L1UserTxs)
 85	for _, tx := range block.L1UserTxs {
 86		var dbTx *common.L1Tx
 87		// Find tx in DB output
 88		for _, _dbTx := range dbL1UserTxs {
 89			if *tx.ToForgeL1TxsNum == *_dbTx.ToForgeL1TxsNum &&
 90				tx.Position == _dbTx.Position {
 91				dbTx = new(common.L1Tx)
 92				*dbTx = _dbTx
 93				break
 94			}
 95		}
 96		assert.Equal(t, &tx, dbTx) //nolint:gosec
 97	}
 98
 99	// Check Batches
100	assert.Equal(t, len(block.Batches), len(syncBlock.Batches))
101	dbBatches, err := s.historyDB.GetAllBatches()
102	require.Nil(t, err)
103
104	dbL1CoordinatorTxs, err := s.historyDB.GetAllL1CoordinatorTxs()
105	require.Nil(t, err)
106	// fmt.Printf("DBG dbL1CoordinatorTxs: %+v\n", dbL1CoordinatorTxs)
107	dbL2Txs, err := s.historyDB.GetAllL2Txs()
108	require.Nil(t, err)
109	// fmt.Printf("DBG dbL2Txs: %+v\n", dbL2Txs)
110	dbExits, err := s.historyDB.GetAllExits()
111	require.Nil(t, err)
112	// dbL1CoordinatorTxs := []common.L1Tx{}
113	for i, batch := range block.Batches {
114		var dbBatch *common.Batch
115		// Find batch in DB output
116		for _, _dbBatch := range dbBatches {
117			if batch.Batch.BatchNum == _dbBatch.BatchNum {
118				dbBatch = new(common.Batch)
119				*dbBatch = _dbBatch
120				break
121			}
122		}
123		syncBatch := syncBlock.Batches[i]
124
125		// We don't care about TotalFeesUSD.  Use the syncBatch that
126		// has a TotalFeesUSD inserted by the HistoryDB
127		batch.Batch.TotalFeesUSD = syncBatch.Batch.TotalFeesUSD
128		batch.CreatedAccounts = syncBatch.CreatedAccounts // til doesn't output CreatedAccounts
129		batch.Batch.NumAccounts = len(batch.CreatedAccounts)
130
131		// Test field by field to facilitate debugging of errors
132		assert.Equal(t, batch.L1CoordinatorTxs, syncBatch.L1CoordinatorTxs)
133		assert.Equal(t, batch.L2Txs, syncBatch.L2Txs)
134		// In exit tree, we only check AccountIdx and Balance, because
135		// it's what we have precomputed before.
136		for j := range batch.ExitTree {
137			exit := &batch.ExitTree[j]
138			assert.Equal(t, exit.AccountIdx, syncBatch.ExitTree[j].AccountIdx)
139			assert.Equal(t, exit.Balance, syncBatch.ExitTree[j].Balance)
140			*exit = syncBatch.ExitTree[j]
141		}
142		// We are collecting fees after blockNum=2 in 2 idxs
143		if block.Block.EthBlockNum > 2 {
144			// fmt.Printf("DBG collectedFees: %+v\n", syncBatch.Batch.CollectedFees)
145			assert.Equal(t, 2, len(syncBatch.Batch.CollectedFees))
146		}
147		batch.Batch.CollectedFees = syncBatch.Batch.CollectedFees
148		assert.Equal(t, batch.Batch, syncBatch.Batch)
149		assert.Equal(t, batch, syncBatch)
150		assert.Equal(t, &batch.Batch, dbBatch) //nolint:gosec
151
152		// Check L1CoordinatorTxs from DB
153		for _, tx := range batch.L1CoordinatorTxs {
154			var dbTx *common.L1Tx
155			// Find tx in DB output
156			for _, _dbTx := range dbL1CoordinatorTxs {
157				if *tx.BatchNum == *_dbTx.BatchNum &&
158					tx.Position == _dbTx.Position {
159					dbTx = new(common.L1Tx)
160					*dbTx = _dbTx
161					break
162				}
163			}
164			assert.Equal(t, &tx, dbTx) //nolint:gosec
165		}
166
167		// Check L2Txs from DB
168		for _, tx := range batch.L2Txs {
169			var dbTx *common.L2Tx
170			// Find tx in DB output
171			for _, _dbTx := range dbL2Txs {
172				if tx.BatchNum == _dbTx.BatchNum &&
173					tx.Position == _dbTx.Position {
174					dbTx = new(common.L2Tx)
175					*dbTx = _dbTx
176					break
177				}
178			}
179			assert.Equal(t, &tx, dbTx) //nolint:gosec
180		}
181
182		// Check Exits from DB
183		for _, exit := range batch.ExitTree {
184			var dbExit *common.ExitInfo
185			// Find exit in DB output
186			for _, _dbExit := range dbExits {
187				if exit.BatchNum == _dbExit.BatchNum &&
188					exit.AccountIdx == _dbExit.AccountIdx {
189					dbExit = new(common.ExitInfo)
190					*dbExit = _dbExit
191					break
192				}
193			}
194			// Compare MerkleProof in JSON because unmarshaled 0
195			// big.Int leaves the internal big.Int array at nil,
196			// and gives trouble when comparing big.Int with
197			// internal big.Int array != nil but empty.
198			mtp, err := json.Marshal(exit.MerkleProof)
199			require.Nil(t, err)
200			dbMtp, err := json.Marshal(dbExit.MerkleProof)
201			require.Nil(t, err)
202			assert.Equal(t, mtp, dbMtp)
203			dbExit.MerkleProof = exit.MerkleProof
204			assert.Equal(t, &exit, dbExit) //nolint:gosec
205		}
206	}
207}
208
209func TestSync(t *testing.T) {
210	//
211	// Setup
212	//
213
214	ctx := context.Background()
215	// Int State DB
216	dir, err := ioutil.TempDir("", "tmpdb")
217	require.Nil(t, err)
218	defer assert.Nil(t, os.RemoveAll(dir))
219
220	stateDB, err := statedb.NewStateDB(dir, statedb.TypeSynchronizer, 32)
221	assert.Nil(t, err)
222
223	// Init History DB
224	pass := os.Getenv("POSTGRES_PASS")
225	db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
226	require.Nil(t, err)
227	historyDB := historydb.NewHistoryDB(db)
228	// Clear DB
229	test.WipeDB(historyDB.DB())
230
231	// Init eth client
232	var timer timer
233	clientSetup := test.NewClientSetupExample()
234	bootCoordAddr := clientSetup.AuctionVariables.BootCoordinator
235	client := test.NewClient(true, &timer, &ethCommon.Address{}, clientSetup)
236
237	// Create Synchronizer
238	s, err := NewSynchronizer(client, historyDB, stateDB)
239	require.Nil(t, err)
240
241	//
242	// First Sync from an initial state
243	//
244
245	// Test Sync for rollup genesis block
246	syncBlock, discards, err := s.Sync2(ctx, nil)
247	require.Nil(t, err)
248	require.Nil(t, discards)
249	require.NotNil(t, syncBlock)
250	assert.Equal(t, int64(1), syncBlock.Block.EthBlockNum)
251	dbBlocks, err := s.historyDB.GetAllBlocks()
252	require.Nil(t, err)
253	assert.Equal(t, 2, len(dbBlocks))
254	assert.Equal(t, int64(1), dbBlocks[1].EthBlockNum)
255
256	// Sync again and expect no new blocks
257	syncBlock, discards, err = s.Sync2(ctx, nil)
258	require.Nil(t, err)
259	require.Nil(t, discards)
260	require.Nil(t, syncBlock)
261
262	//
263	// Generate blockchain and smart contract data, and fill the test smart contracts
264	//
265
266	// Generate blockchain data with til
267	set1 := `
268		Type: Blockchain
269
270		AddToken(1)
271		AddToken(2)
272		AddToken(3)
273
274		CreateAccountDeposit(1) C: 2000 // Idx=256+2=258
275		CreateAccountDeposit(2) A: 2000 // Idx=256+3=259
276		CreateAccountDeposit(1) D: 500  // Idx=256+4=260
277		CreateAccountDeposit(2) B: 500  // Idx=256+5=261
278		CreateAccountDeposit(2) C: 500  // Idx=256+6=262
279
280		CreateAccountCoordinator(1) A // Idx=256+0=256
281		CreateAccountCoordinator(1) B // Idx=256+1=257
282
283		> batchL1 // forge L1UserTxs{nil}, freeze defined L1UserTxs{5}
284		> batchL1 // forge defined L1UserTxs{5}, freeze L1UserTxs{nil}
285		> block // blockNum=2
286
287		CreateAccountDepositTransfer(1) E-A: 1000, 200 // Idx=256+7=263
288		ForceExit(1) A: 100
289		ForceTransfer(1) A-D: 100
290
291		Transfer(1) C-A: 100 (200)
292		Exit(1) D: 30 (200)
293
294		> batchL1 // forge L1UserTxs{nil}, freeze defined L1UserTxs{2}
295		> batchL1 // forge L1UserTxs{2}, freeze defined L1UserTxs{nil}
296		> block // blockNum=3
297
298	`
299	tc := til.NewContext(eth.RollupConstMaxL1UserTx)
300	blocks, err := tc.GenerateBlocks(set1)
301	require.Nil(t, err)
302	// Sanity check
303	require.Equal(t, 2, len(blocks))
304	// blocks 0 (blockNum=2)
305	i := 0
306	require.Equal(t, 2, int(blocks[i].Block.EthBlockNum))
307	require.Equal(t, 3, len(blocks[i].AddedTokens))
308	require.Equal(t, 5, len(blocks[i].L1UserTxs))
309	require.Equal(t, 2, len(blocks[i].Batches))
310	require.Equal(t, 2, len(blocks[i].Batches[0].L1CoordinatorTxs))
311	// blocks 1 (blockNum=3)
312	i = 1
313	require.Equal(t, 3, int(blocks[i].Block.EthBlockNum))
314	require.Equal(t, 3, len(blocks[i].L1UserTxs))
315	require.Equal(t, 2, len(blocks[i].Batches))
316	require.Equal(t, 2, len(blocks[i].Batches[0].L2Txs))
317
318	// Generate extra required data
319	for _, block := range blocks {
320		for _, token := range block.AddedTokens {
321			consts := eth.ERC20Consts{
322				Name:     fmt.Sprintf("Token %d", token.TokenID),
323				Symbol:   fmt.Sprintf("TK%d", token.TokenID),
324				Decimals: 18,
325			}
326			tokenConsts[token.TokenID] = consts
327			client.CtlAddERC20(token.EthAddr, consts)
328		}
329	}
330
331	// Add block data to the smart contracts
332	for _, block := range blocks {
333		for _, token := range block.AddedTokens {
334			_, err := client.RollupAddTokenSimple(token.EthAddr, clientSetup.RollupVariables.FeeAddToken)
335			require.Nil(t, err)
336		}
337		for _, tx := range block.L1UserTxs {
338			client.CtlSetAddr(tx.FromEthAddr)
339			_, err := client.RollupL1UserTxERC20ETH(tx.FromBJJ, int64(tx.FromIdx), tx.LoadAmount, tx.Amount,
340				uint32(tx.TokenID), int64(tx.ToIdx))
341			require.Nil(t, err)
342		}
343		client.CtlSetAddr(bootCoordAddr)
344		feeIdxCoordinator := []common.Idx{}
345		if block.Block.EthBlockNum > 2 {
346			// After blockNum=2 we have some accounts, use them as
347			// coordinator owned to receive fees.
348			feeIdxCoordinator = []common.Idx{common.Idx(256), common.Idx(259)}
349		}
350		for _, batch := range block.Batches {
351			_, err := client.RollupForgeBatch(&eth.RollupForgeBatchArgs{
352				NewLastIdx:            batch.Batch.LastIdx,
353				NewStRoot:             batch.Batch.StateRoot,
354				NewExitRoot:           batch.Batch.ExitRoot,
355				L1CoordinatorTxs:      batch.L1CoordinatorTxs,
356				L1CoordinatorTxsAuths: [][]byte{}, // Intentionally empty
357				L2TxsData:             batch.L2Txs,
358				FeeIdxCoordinator:     feeIdxCoordinator,
359				// Circuit selector
360				VerifierIdx: 0, // Intentionally empty
361				L1Batch:     batch.L1Batch,
362				ProofA:      [2]*big.Int{},    // Intentionally empty
363				ProofB:      [2][2]*big.Int{}, // Intentionally empty
364				ProofC:      [2]*big.Int{},    // Intentionally empty
365			})
366			require.Nil(t, err)
367		}
368		// Mine block and sync
369		client.CtlMineBlock()
370	}
371
372	// Fill extra fields not generated by til in til block
373	openToForge := int64(0)
374	toForgeL1TxsNum := int64(0)
375	l1UserTxsLen := map[int64]int{} // ForgeL1TxsNum -> len(L1UserTxs)
376	for i := range blocks {
377		block := &blocks[i]
378		// Count number of L1UserTxs in each queue, to figure out later
379		// position of L1CoordinatorTxs and L2Txs
380		for j := range block.L1UserTxs {
381			tx := &block.L1UserTxs[j]
382			l1UserTxsLen[*tx.ToForgeL1TxsNum]++
383			if tx.Type == common.TxTypeForceExit {
384				forceExits[*tx.ToForgeL1TxsNum] = append(forceExits[*tx.ToForgeL1TxsNum],
385					common.ExitInfo{
386						AccountIdx: tx.FromIdx,
387						Balance:    tx.Amount,
388					})
389			}
390		}
391		for j := range block.Batches {
392			batch := &block.Batches[j]
393			if batch.L1Batch {
394				// Set BatchNum for forged L1UserTxs to til blocks
395				bn := batch.Batch.BatchNum
396				for k := range blocks {
397					block := &blocks[k]
398					for l := range block.L1UserTxs {
399						tx := &block.L1UserTxs[l]
400						if *tx.ToForgeL1TxsNum == openToForge {
401							tx.BatchNum = &bn
402						}
403					}
404				}
405				openToForge++
406			}
407
408			batch.Batch.EthBlockNum = block.Block.EthBlockNum
409			batch.Batch.ForgerAddr = bootCoordAddr // til doesn't fill the batch forger addr
410			if batch.L1Batch {
411				toForgeL1TxsNumCpy := toForgeL1TxsNum
412				batch.Batch.ForgeL1TxsNum = &toForgeL1TxsNumCpy // til doesn't fill the ForgeL1TxsNum
413				toForgeL1TxsNum++
414			}
415
416			batchNum := batch.Batch.BatchNum
417			for k := range batch.L1CoordinatorTxs {
418				tx := &batch.L1CoordinatorTxs[k]
419				tx.BatchNum = &batchNum
420				tx.EthBlockNum = batch.Batch.EthBlockNum
421			}
422		}
423	}
424
425	// Fill expected positions in L1CoordinatorTxs and L2Txs
426	for i := range blocks {
427		block := &blocks[i]
428		for j := range block.Batches {
429			batch := &block.Batches[j]
430			position := 0
431			if batch.L1Batch {
432				position = l1UserTxsLen[*batch.Batch.ForgeL1TxsNum]
433			}
434			for k := range batch.L1CoordinatorTxs {
435				tx := &batch.L1CoordinatorTxs[k]
436				tx.Position = position
437				position++
438				nTx, err := common.NewL1Tx(tx)
439				require.Nil(t, err)
440				*tx = *nTx
441			}
442			for k := range batch.L2Txs {
443				tx := &batch.L2Txs[k]
444				tx.Position = position
445				position++
446				nonces[tx.FromIdx]++
447				tx.Nonce = nonces[tx.FromIdx]
448				nTx, err := common.NewL2Tx(tx)
449				require.Nil(t, err)
450				*tx = *nTx
451			}
452		}
453	}
454
455	// Fill ExitTree (only AccountIdx and Balance)
456	for i := range blocks {
457		block := &blocks[i]
458		for j := range block.Batches {
459			batch := &block.Batches[j]
460			if batch.L1Batch {
461				for forgeL1TxsNum, exits := range forceExits {
462					if forgeL1TxsNum == *batch.Batch.ForgeL1TxsNum {
463						batch.ExitTree = append(batch.ExitTree, exits...)
464					}
465				}
466			}
467			for k := range batch.L2Txs {
468				tx := &batch.L2Txs[k]
469				if tx.Type == common.TxTypeExit {
470					batch.ExitTree = append(batch.ExitTree, common.ExitInfo{
471						AccountIdx: tx.FromIdx,
472						Balance:    tx.Amount,
473					})
474				}
475			}
476		}
477	}
478
479	//
480	// Sync to synchronize the current state from the test smart contracts,
481	// and check the outcome
482	//
483
484	// Block 2
485
486	syncBlock, discards, err = s.Sync2(ctx, nil)
487	require.Nil(t, err)
488	require.Nil(t, discards)
489	require.NotNil(t, syncBlock)
490	assert.Equal(t, int64(2), syncBlock.Block.EthBlockNum)
491
492	checkSyncBlock(t, s, 2, &blocks[0], syncBlock)
493
494	// Block 3
495
496	syncBlock, discards, err = s.Sync2(ctx, nil)
497	require.Nil(t, err)
498	require.Nil(t, discards)
499	require.NotNil(t, syncBlock)
500	assert.Equal(t, int64(3), syncBlock.Block.EthBlockNum)
501
502	checkSyncBlock(t, s, 3, &blocks[1], syncBlock)
503
504	// TODO: Reorg will be properly tested once we have the mock ethClient implemented
505	/*
506		// Force a Reorg
507		lastSavedBlock, err := historyDB.GetLastBlock()
508		require.Nil(t, err)
509
510		lastSavedBlock.EthBlockNum++
511		err = historyDB.AddBlock(lastSavedBlock)
512		require.Nil(t, err)
513
514		lastSavedBlock.EthBlockNum++
515		err = historyDB.AddBlock(lastSavedBlock)
516		require.Nil(t, err)
517
518		log.Debugf("Wait for the blockchain to generate some blocks...")
519		time.Sleep(40 * time.Second)
520
521
522		err = s.Sync()
523		require.Nil(t, err)
524	*/
525}