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.Rollup.AddedTokens), len(syncBlock.Rollup.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.Rollup.AddedTokens {
 57		dbToken := dbTokens[i]
 58		syncToken := syncBlock.Rollup.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.Rollup.L1UserTxs), len(syncBlock.Rollup.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.Rollup.L1UserTxs {
 82		syncBlock.Rollup.L1UserTxs[i].BatchNum = block.Rollup.L1UserTxs[i].BatchNum
 83	}
 84	assert.Equal(t, block.Rollup.L1UserTxs, syncBlock.Rollup.L1UserTxs)
 85	for _, tx := range block.Rollup.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.Rollup.Batches), len(syncBlock.Rollup.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.Rollup.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.Rollup.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, Config{
239		StartBlockNum: ConfigStartBlockNum{
240			Rollup:   1,
241			Auction:  1,
242			WDelayer: 1,
243		},
244	})
245	require.Nil(t, err)
246
247	//
248	// First Sync from an initial state
249	//
250
251	// Test Sync for rollup genesis block
252	syncBlock, discards, err := s.Sync2(ctx, nil)
253	require.Nil(t, err)
254	require.Nil(t, discards)
255	require.NotNil(t, syncBlock)
256	assert.Equal(t, int64(1), syncBlock.Block.EthBlockNum)
257	dbBlocks, err := s.historyDB.GetAllBlocks()
258	require.Nil(t, err)
259	assert.Equal(t, 2, len(dbBlocks))
260	assert.Equal(t, int64(1), dbBlocks[1].EthBlockNum)
261
262	// Sync again and expect no new blocks
263	syncBlock, discards, err = s.Sync2(ctx, nil)
264	require.Nil(t, err)
265	require.Nil(t, discards)
266	require.Nil(t, syncBlock)
267
268	//
269	// Generate blockchain and smart contract data, and fill the test smart contracts
270	//
271
272	// Generate blockchain data with til
273	set1 := `
274		Type: Blockchain
275
276		AddToken(1)
277		AddToken(2)
278		AddToken(3)
279
280		CreateAccountDeposit(1) C: 2000 // Idx=256+2=258
281		CreateAccountDeposit(2) A: 2000 // Idx=256+3=259
282		CreateAccountDeposit(1) D: 500  // Idx=256+4=260
283		CreateAccountDeposit(2) B: 500  // Idx=256+5=261
284		CreateAccountDeposit(2) C: 500  // Idx=256+6=262
285
286		CreateAccountCoordinator(1) A // Idx=256+0=256
287		CreateAccountCoordinator(1) B // Idx=256+1=257
288
289		> batchL1 // forge L1UserTxs{nil}, freeze defined L1UserTxs{5}
290		> batchL1 // forge defined L1UserTxs{5}, freeze L1UserTxs{nil}
291		> block // blockNum=2
292
293		CreateAccountDepositTransfer(1) E-A: 1000, 200 // Idx=256+7=263
294		ForceExit(1) A: 100
295		ForceTransfer(1) A-D: 100
296
297		Transfer(1) C-A: 100 (200)
298		Exit(1) D: 30 (200)
299
300		> batchL1 // forge L1UserTxs{nil}, freeze defined L1UserTxs{2}
301		> batchL1 // forge L1UserTxs{2}, freeze defined L1UserTxs{nil}
302		> block // blockNum=3
303
304	`
305	tc := til.NewContext(common.RollupConstMaxL1UserTx)
306	blocks, err := tc.GenerateBlocks(set1)
307	require.Nil(t, err)
308	// Sanity check
309	require.Equal(t, 2, len(blocks))
310	// blocks 0 (blockNum=2)
311	i := 0
312	require.Equal(t, 2, int(blocks[i].Block.EthBlockNum))
313	require.Equal(t, 3, len(blocks[i].Rollup.AddedTokens))
314	require.Equal(t, 5, len(blocks[i].Rollup.L1UserTxs))
315	require.Equal(t, 2, len(blocks[i].Rollup.Batches))
316	require.Equal(t, 2, len(blocks[i].Rollup.Batches[0].L1CoordinatorTxs))
317	// blocks 1 (blockNum=3)
318	i = 1
319	require.Equal(t, 3, int(blocks[i].Block.EthBlockNum))
320	require.Equal(t, 3, len(blocks[i].Rollup.L1UserTxs))
321	require.Equal(t, 2, len(blocks[i].Rollup.Batches))
322	require.Equal(t, 2, len(blocks[i].Rollup.Batches[0].L2Txs))
323
324	// Generate extra required data
325	for _, block := range blocks {
326		for _, token := range block.Rollup.AddedTokens {
327			consts := eth.ERC20Consts{
328				Name:     fmt.Sprintf("Token %d", token.TokenID),
329				Symbol:   fmt.Sprintf("TK%d", token.TokenID),
330				Decimals: 18,
331			}
332			tokenConsts[token.TokenID] = consts
333			client.CtlAddERC20(token.EthAddr, consts)
334		}
335	}
336
337	// Add block data to the smart contracts
338	for _, block := range blocks {
339		for _, token := range block.Rollup.AddedTokens {
340			_, err := client.RollupAddTokenSimple(token.EthAddr, clientSetup.RollupVariables.FeeAddToken)
341			require.Nil(t, err)
342		}
343		for _, tx := range block.Rollup.L1UserTxs {
344			client.CtlSetAddr(tx.FromEthAddr)
345			_, err := client.RollupL1UserTxERC20ETH(tx.FromBJJ, int64(tx.FromIdx), tx.LoadAmount, tx.Amount,
346				uint32(tx.TokenID), int64(tx.ToIdx))
347			require.Nil(t, err)
348		}
349		client.CtlSetAddr(bootCoordAddr)
350		feeIdxCoordinator := []common.Idx{}
351		if block.Block.EthBlockNum > 2 {
352			// After blockNum=2 we have some accounts, use them as
353			// coordinator owned to receive fees.
354			feeIdxCoordinator = []common.Idx{common.Idx(256), common.Idx(259)}
355		}
356		for _, batch := range block.Rollup.Batches {
357			_, err := client.RollupForgeBatch(&eth.RollupForgeBatchArgs{
358				NewLastIdx:            batch.Batch.LastIdx,
359				NewStRoot:             batch.Batch.StateRoot,
360				NewExitRoot:           batch.Batch.ExitRoot,
361				L1CoordinatorTxs:      batch.L1CoordinatorTxs,
362				L1CoordinatorTxsAuths: [][]byte{}, // Intentionally empty
363				L2TxsData:             batch.L2Txs,
364				FeeIdxCoordinator:     feeIdxCoordinator,
365				// Circuit selector
366				VerifierIdx: 0, // Intentionally empty
367				L1Batch:     batch.L1Batch,
368				ProofA:      [2]*big.Int{},    // Intentionally empty
369				ProofB:      [2][2]*big.Int{}, // Intentionally empty
370				ProofC:      [2]*big.Int{},    // Intentionally empty
371			})
372			require.Nil(t, err)
373		}
374		// Mine block and sync
375		client.CtlMineBlock()
376	}
377
378	// Fill extra fields not generated by til in til block
379	openToForge := int64(0)
380	toForgeL1TxsNum := int64(0)
381	l1UserTxsLen := map[int64]int{} // ForgeL1TxsNum -> len(L1UserTxs)
382	for i := range blocks {
383		block := &blocks[i]
384		// Count number of L1UserTxs in each queue, to figure out later
385		// position of L1CoordinatorTxs and L2Txs
386		for j := range block.Rollup.L1UserTxs {
387			tx := &block.Rollup.L1UserTxs[j]
388			l1UserTxsLen[*tx.ToForgeL1TxsNum]++
389			if tx.Type == common.TxTypeForceExit {
390				forceExits[*tx.ToForgeL1TxsNum] = append(forceExits[*tx.ToForgeL1TxsNum],
391					common.ExitInfo{
392						AccountIdx: tx.FromIdx,
393						Balance:    tx.Amount,
394					})
395			}
396		}
397		for j := range block.Rollup.Batches {
398			batch := &block.Rollup.Batches[j]
399			if batch.L1Batch {
400				// Set BatchNum for forged L1UserTxs to til blocks
401				bn := batch.Batch.BatchNum
402				for k := range blocks {
403					block := &blocks[k]
404					for l := range block.Rollup.L1UserTxs {
405						tx := &block.Rollup.L1UserTxs[l]
406						if *tx.ToForgeL1TxsNum == openToForge {
407							tx.BatchNum = &bn
408						}
409					}
410				}
411				openToForge++
412			}
413
414			batch.Batch.EthBlockNum = block.Block.EthBlockNum
415			batch.Batch.ForgerAddr = bootCoordAddr // til doesn't fill the batch forger addr
416			if batch.L1Batch {
417				toForgeL1TxsNumCpy := toForgeL1TxsNum
418				batch.Batch.ForgeL1TxsNum = &toForgeL1TxsNumCpy // til doesn't fill the ForgeL1TxsNum
419				toForgeL1TxsNum++
420			}
421
422			batchNum := batch.Batch.BatchNum
423			for k := range batch.L1CoordinatorTxs {
424				tx := &batch.L1CoordinatorTxs[k]
425				tx.BatchNum = &batchNum
426				tx.EthBlockNum = batch.Batch.EthBlockNum
427			}
428		}
429	}
430
431	// Fill expected positions in L1CoordinatorTxs and L2Txs
432	for i := range blocks {
433		block := &blocks[i]
434		for j := range block.Rollup.Batches {
435			batch := &block.Rollup.Batches[j]
436			position := 0
437			if batch.L1Batch {
438				position = l1UserTxsLen[*batch.Batch.ForgeL1TxsNum]
439			}
440			for k := range batch.L1CoordinatorTxs {
441				tx := &batch.L1CoordinatorTxs[k]
442				tx.Position = position
443				position++
444				nTx, err := common.NewL1Tx(tx)
445				require.Nil(t, err)
446				*tx = *nTx
447			}
448			for k := range batch.L2Txs {
449				tx := &batch.L2Txs[k]
450				tx.Position = position
451				position++
452				nonces[tx.FromIdx]++
453				tx.Nonce = nonces[tx.FromIdx]
454				nTx, err := common.NewL2Tx(tx)
455				require.Nil(t, err)
456				*tx = *nTx
457			}
458		}
459	}
460
461	// Fill ExitTree (only AccountIdx and Balance)
462	for i := range blocks {
463		block := &blocks[i]
464		for j := range block.Rollup.Batches {
465			batch := &block.Rollup.Batches[j]
466			if batch.L1Batch {
467				for forgeL1TxsNum, exits := range forceExits {
468					if forgeL1TxsNum == *batch.Batch.ForgeL1TxsNum {
469						batch.ExitTree = append(batch.ExitTree, exits...)
470					}
471				}
472			}
473			for k := range batch.L2Txs {
474				tx := &batch.L2Txs[k]
475				if tx.Type == common.TxTypeExit {
476					batch.ExitTree = append(batch.ExitTree, common.ExitInfo{
477						AccountIdx: tx.FromIdx,
478						Balance:    tx.Amount,
479					})
480				}
481			}
482		}
483	}
484
485	//
486	// Sync to synchronize the current state from the test smart contracts,
487	// and check the outcome
488	//
489
490	// Block 2
491
492	syncBlock, discards, err = s.Sync2(ctx, nil)
493	require.Nil(t, err)
494	require.Nil(t, discards)
495	require.NotNil(t, syncBlock)
496	assert.Equal(t, int64(2), syncBlock.Block.EthBlockNum)
497
498	checkSyncBlock(t, s, 2, &blocks[0], syncBlock)
499
500	// Block 3
501
502	syncBlock, discards, err = s.Sync2(ctx, nil)
503	require.Nil(t, err)
504	require.Nil(t, discards)
505	require.NotNil(t, syncBlock)
506	assert.Equal(t, int64(3), syncBlock.Block.EthBlockNum)
507
508	checkSyncBlock(t, s, 3, &blocks[1], syncBlock)
509
510	// TODO: Reorg will be properly tested once we have the mock ethClient implemented
511	/*
512		// Force a Reorg
513		lastSavedBlock, err := historyDB.GetLastBlock()
514		require.Nil(t, err)
515
516		lastSavedBlock.EthBlockNum++
517		err = historyDB.AddBlock(lastSavedBlock)
518		require.Nil(t, err)
519
520		lastSavedBlock.EthBlockNum++
521		err = historyDB.AddBlock(lastSavedBlock)
522		require.Nil(t, err)
523
524		log.Debugf("Wait for the blockchain to generate some blocks...")
525		time.Sleep(40 * time.Second)
526
527
528		err = s.Sync()
529		require.Nil(t, err)
530	*/
531}