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	"sort"
 11	"testing"
 12	"time"
 13
 14	ethCommon "github.com/ethereum/go-ethereum/common"
 15	"github.com/hermeznetwork/hermez-node/common"
 16	dbUtils "github.com/hermeznetwork/hermez-node/db"
 17	"github.com/hermeznetwork/hermez-node/db/historydb"
 18	"github.com/hermeznetwork/hermez-node/db/statedb"
 19	"github.com/hermeznetwork/hermez-node/eth"
 20	"github.com/hermeznetwork/hermez-node/test"
 21	"github.com/hermeznetwork/hermez-node/test/til"
 22	"github.com/jinzhu/copier"
 23	"github.com/stretchr/testify/assert"
 24	"github.com/stretchr/testify/require"
 25)
 26
 27var tokenConsts = map[common.TokenID]eth.ERC20Consts{}
 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
 39func accountsCmp(accounts []common.Account) func(i, j int) bool {
 40	return func(i, j int) bool { return accounts[i].Idx < accounts[j].Idx }
 41}
 42
 43// Check Sync output and HistoryDB state against expected values generated by
 44// til
 45func checkSyncBlock(t *testing.T, s *Synchronizer, blockNum int, block, syncBlock *common.BlockData) {
 46	// Check Blocks
 47	dbBlocks, err := s.historyDB.GetAllBlocks()
 48	require.NoError(t, err)
 49	dbBlocks = dbBlocks[1:] // ignore block 0, added by default in the DB
 50	assert.Equal(t, blockNum, len(dbBlocks))
 51	assert.Equal(t, int64(blockNum), dbBlocks[blockNum-1].Num)
 52	assert.NotEqual(t, dbBlocks[blockNum-1].Hash, dbBlocks[blockNum-2].Hash)
 53	assert.Greater(t, dbBlocks[blockNum-1].Timestamp.Unix(), dbBlocks[blockNum-2].Timestamp.Unix())
 54
 55	// Check Tokens
 56	assert.Equal(t, len(block.Rollup.AddedTokens), len(syncBlock.Rollup.AddedTokens))
 57	dbTokens, err := s.historyDB.GetAllTokens()
 58	require.NoError(t, err)
 59	dbTokens = dbTokens[1:] // ignore token 0, added by default in the DB
 60	for i, token := range block.Rollup.AddedTokens {
 61		dbToken := dbTokens[i]
 62		syncToken := syncBlock.Rollup.AddedTokens[i]
 63
 64		assert.Equal(t, block.Block.Num, syncToken.EthBlockNum)
 65		assert.Equal(t, token.TokenID, syncToken.TokenID)
 66		assert.Equal(t, token.EthAddr, syncToken.EthAddr)
 67		tokenConst := tokenConsts[token.TokenID]
 68		assert.Equal(t, tokenConst.Name, syncToken.Name)
 69		assert.Equal(t, tokenConst.Symbol, syncToken.Symbol)
 70		assert.Equal(t, tokenConst.Decimals, syncToken.Decimals)
 71
 72		var tokenCpy historydb.TokenWithUSD
 73		//nolint:gosec
 74		require.Nil(t, copier.Copy(&tokenCpy, &token))      // copy common.Token to historydb.TokenWithUSD
 75		require.Nil(t, copier.Copy(&tokenCpy, &tokenConst)) // copy common.Token to historydb.TokenWithUSD
 76		tokenCpy.ItemID = dbToken.ItemID                    // we don't care about ItemID
 77		assert.Equal(t, tokenCpy, dbToken)
 78	}
 79
 80	// Check submitted L1UserTxs
 81	assert.Equal(t, len(block.Rollup.L1UserTxs), len(syncBlock.Rollup.L1UserTxs))
 82	dbL1UserTxs, err := s.historyDB.GetAllL1UserTxs()
 83	require.NoError(t, err)
 84	// Ignore BatchNum in syncBlock.L1UserTxs because this value is set by
 85	// the HistoryDB. Also ignore EffectiveAmount & EffectiveDepositAmount
 86	// because this value is set by StateDB.ProcessTxs.
 87	for i := range syncBlock.Rollup.L1UserTxs {
 88		syncBlock.Rollup.L1UserTxs[i].BatchNum = block.Rollup.L1UserTxs[i].BatchNum
 89		assert.Nil(t, syncBlock.Rollup.L1UserTxs[i].EffectiveDepositAmount)
 90		assert.Nil(t, syncBlock.Rollup.L1UserTxs[i].EffectiveAmount)
 91	}
 92	assert.Equal(t, block.Rollup.L1UserTxs, syncBlock.Rollup.L1UserTxs)
 93	for _, tx := range block.Rollup.L1UserTxs {
 94		var dbTx *common.L1Tx
 95		// Find tx in DB output
 96		for _, _dbTx := range dbL1UserTxs {
 97			if *tx.ToForgeL1TxsNum == *_dbTx.ToForgeL1TxsNum &&
 98				tx.Position == _dbTx.Position {
 99				dbTx = new(common.L1Tx)
100				*dbTx = _dbTx
101				// NOTE: Overwrite EffectiveFromIdx in L1UserTx
102				// from db because we don't expect
103				// EffectiveFromIdx to be set yet, as this tx
104				// is not in yet forged
105				dbTx.EffectiveFromIdx = 0
106				break
107			}
108		}
109		// If the tx has been forged in this block, this will be
110		// reflected in the DB, and so the Effective values will be
111		// already set
112		if dbTx.BatchNum != nil {
113			tx.EffectiveAmount = tx.Amount
114			tx.EffectiveDepositAmount = tx.DepositAmount
115		}
116		assert.Equal(t, &tx, dbTx) //nolint:gosec
117	}
118
119	// Check Batches
120	assert.Equal(t, len(block.Rollup.Batches), len(syncBlock.Rollup.Batches))
121	dbBatches, err := s.historyDB.GetAllBatches()
122	require.NoError(t, err)
123
124	dbL1CoordinatorTxs, err := s.historyDB.GetAllL1CoordinatorTxs()
125	require.NoError(t, err)
126	dbL2Txs, err := s.historyDB.GetAllL2Txs()
127	require.NoError(t, err)
128	dbExits, err := s.historyDB.GetAllExits()
129	require.NoError(t, err)
130	// dbL1CoordinatorTxs := []common.L1Tx{}
131	for i, batch := range block.Rollup.Batches {
132		var dbBatch *common.Batch
133		// Find batch in DB output
134		for _, _dbBatch := range dbBatches {
135			if batch.Batch.BatchNum == _dbBatch.BatchNum {
136				dbBatch = new(common.Batch)
137				*dbBatch = _dbBatch
138				break
139			}
140		}
141		syncBatch := syncBlock.Rollup.Batches[i]
142
143		// We don't care about TotalFeesUSD.  Use the syncBatch that
144		// has a TotalFeesUSD inserted by the HistoryDB
145		batch.Batch.TotalFeesUSD = syncBatch.Batch.TotalFeesUSD
146		assert.Equal(t, batch.CreatedAccounts, syncBatch.CreatedAccounts)
147		batch.Batch.NumAccounts = len(batch.CreatedAccounts)
148
149		// Test field by field to facilitate debugging of errors
150		assert.Equal(t, len(batch.L1UserTxs), len(syncBatch.L1UserTxs))
151		// NOTE: EffectiveFromIdx is set to til L1UserTxs in
152		// `FillBlocksForgedL1UserTxs` function
153		for j := range syncBatch.L1UserTxs {
154			assert.NotEqual(t, 0, syncBatch.L1UserTxs[j].EffectiveFromIdx)
155		}
156		assert.Equal(t, batch.L1UserTxs, syncBatch.L1UserTxs)
157		// NOTE: EffectiveFromIdx is set to til L1CoordinatorTxs in
158		// `FillBlocksExtra` function
159		for j := range syncBatch.L1CoordinatorTxs {
160			assert.NotEqual(t, 0, syncBatch.L1CoordinatorTxs[j].EffectiveFromIdx)
161		}
162		assert.Equal(t, batch.L1CoordinatorTxs, syncBatch.L1CoordinatorTxs)
163		assert.Equal(t, batch.L2Txs, syncBatch.L2Txs)
164		// In exit tree, we only check AccountIdx and Balance, because
165		// it's what we have precomputed before.
166		require.Equal(t, len(batch.ExitTree), len(syncBatch.ExitTree))
167		for j := range batch.ExitTree {
168			exit := &batch.ExitTree[j]
169			assert.Equal(t, exit.AccountIdx, syncBatch.ExitTree[j].AccountIdx)
170			assert.Equal(t, exit.Balance, syncBatch.ExitTree[j].Balance)
171			*exit = syncBatch.ExitTree[j]
172		}
173		assert.Equal(t, batch.Batch, syncBatch.Batch)
174		assert.Equal(t, batch, syncBatch)
175		assert.Equal(t, &batch.Batch, dbBatch) //nolint:gosec
176
177		// Check forged L1UserTxs from DB, and check effective values
178		// in sync output
179		for j, tx := range batch.L1UserTxs {
180			var dbTx *common.L1Tx
181			// Find tx in DB output
182			for _, _dbTx := range dbL1UserTxs {
183				if *tx.BatchNum == *_dbTx.BatchNum &&
184					tx.Position == _dbTx.Position {
185					dbTx = new(common.L1Tx)
186					*dbTx = _dbTx
187					break
188				}
189			}
190			assert.Equal(t, &tx, dbTx) //nolint:gosec
191
192			syncTx := &syncBlock.Rollup.Batches[i].L1UserTxs[j]
193			assert.Equal(t, syncTx.DepositAmount, syncTx.EffectiveDepositAmount)
194			assert.Equal(t, syncTx.Amount, syncTx.EffectiveAmount)
195		}
196
197		// Check L1CoordinatorTxs from DB
198		for _, tx := range batch.L1CoordinatorTxs {
199			var dbTx *common.L1Tx
200			// Find tx in DB output
201			for _, _dbTx := range dbL1CoordinatorTxs {
202				if *tx.BatchNum == *_dbTx.BatchNum &&
203					tx.Position == _dbTx.Position {
204					dbTx = new(common.L1Tx)
205					*dbTx = _dbTx
206					break
207				}
208			}
209			assert.Equal(t, &tx, dbTx) //nolint:gosec
210		}
211
212		// Check L2Txs from DB
213		for _, tx := range batch.L2Txs {
214			var dbTx *common.L2Tx
215			// Find tx in DB output
216			for _, _dbTx := range dbL2Txs {
217				if tx.BatchNum == _dbTx.BatchNum &&
218					tx.Position == _dbTx.Position {
219					dbTx = new(common.L2Tx)
220					*dbTx = _dbTx
221					break
222				}
223			}
224			assert.Equal(t, &tx, dbTx) //nolint:gosec
225		}
226
227		// Check Exits from DB
228		for _, exit := range batch.ExitTree {
229			var dbExit *common.ExitInfo
230			// Find exit in DB output
231			for _, _dbExit := range dbExits {
232				if exit.BatchNum == _dbExit.BatchNum &&
233					exit.AccountIdx == _dbExit.AccountIdx {
234					dbExit = new(common.ExitInfo)
235					*dbExit = _dbExit
236					break
237				}
238			}
239			// Compare MerkleProof in JSON because unmarshaled 0
240			// big.Int leaves the internal big.Int array at nil,
241			// and gives trouble when comparing big.Int with
242			// internal big.Int array != nil but empty.
243			mtp, err := json.Marshal(exit.MerkleProof)
244			require.NoError(t, err)
245			dbMtp, err := json.Marshal(dbExit.MerkleProof)
246			require.NoError(t, err)
247			assert.Equal(t, mtp, dbMtp)
248			dbExit.MerkleProof = exit.MerkleProof
249			assert.Equal(t, &exit, dbExit) //nolint:gosec
250		}
251	}
252
253	// Compare accounts from HistoryDB with StateDB (they should match)
254	dbAccounts, err := s.historyDB.GetAllAccounts()
255	require.NoError(t, err)
256	sdbAccounts, err := s.stateDB.TestGetAccounts()
257	require.NoError(t, err)
258	assertEqualAccountsHistoryDBStateDB(t, dbAccounts, sdbAccounts)
259}
260
261func assertEqualAccountsHistoryDBStateDB(t *testing.T, hdbAccs, sdbAccs []common.Account) {
262	assert.Equal(t, len(hdbAccs), len(sdbAccs))
263	sort.SliceStable(hdbAccs, accountsCmp(hdbAccs))
264	sort.SliceStable(sdbAccs, accountsCmp(sdbAccs))
265	for i := range hdbAccs {
266		hdbAcc := hdbAccs[i]
267		sdbAcc := sdbAccs[i]
268		assert.Equal(t, hdbAcc.Idx, sdbAcc.Idx)
269		assert.Equal(t, hdbAcc.TokenID, sdbAcc.TokenID)
270		assert.Equal(t, hdbAcc.EthAddr, sdbAcc.EthAddr)
271		assert.Equal(t, hdbAcc.BJJ, sdbAcc.BJJ)
272	}
273}
274
275// ethAddTokens adds the tokens from the blocks to the blockchain
276func ethAddTokens(blocks []common.BlockData, client *test.Client) {
277	for _, block := range blocks {
278		for _, token := range block.Rollup.AddedTokens {
279			consts := eth.ERC20Consts{
280				Name:     fmt.Sprintf("Token %d", token.TokenID),
281				Symbol:   fmt.Sprintf("TK%d", token.TokenID),
282				Decimals: 18,
283			}
284			tokenConsts[token.TokenID] = consts
285			client.CtlAddERC20(token.EthAddr, consts)
286		}
287	}
288}
289
290var chainID uint16 = 0
291var deleteme = []string{}
292
293func TestMain(m *testing.M) {
294	exitVal := m.Run()
295	for _, dir := range deleteme {
296		if err := os.RemoveAll(dir); err != nil {
297			panic(err)
298		}
299	}
300	os.Exit(exitVal)
301}
302
303func newTestModules(t *testing.T) (*statedb.StateDB, *historydb.HistoryDB) {
304	// Int State DB
305	dir, err := ioutil.TempDir("", "tmpdb")
306	require.NoError(t, err)
307	deleteme = append(deleteme, dir)
308
309	stateDB, err := statedb.NewStateDB(statedb.Config{Path: dir, Keep: 128, Type: statedb.TypeSynchronizer, NLevels: 32})
310	require.NoError(t, err)
311
312	// Init History DB
313	pass := os.Getenv("POSTGRES_PASS")
314	db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
315	require.NoError(t, err)
316	historyDB := historydb.NewHistoryDB(db, nil)
317	// Clear DB
318	test.WipeDB(historyDB.DB())
319
320	return stateDB, historyDB
321}
322
323func newBigInt(s string) *big.Int {
324	v, ok := new(big.Int).SetString(s, 10)
325	if !ok {
326		panic(fmt.Errorf("Can't set big.Int from %s", s))
327	}
328	return v
329}
330
331func TestSyncGeneral(t *testing.T) {
332	//
333	// Setup
334	//
335
336	stateDB, historyDB := newTestModules(t)
337
338	// Init eth client
339	var timer timer
340	clientSetup := test.NewClientSetupExample()
341	clientSetup.ChainID = big.NewInt(int64(chainID))
342	bootCoordAddr := clientSetup.AuctionVariables.BootCoordinator
343	client := test.NewClient(true, &timer, &ethCommon.Address{}, clientSetup)
344
345	// Create Synchronizer
346	s, err := NewSynchronizer(client, historyDB, stateDB, Config{
347		StatsRefreshPeriod: 0 * time.Second,
348	})
349	require.NoError(t, err)
350
351	ctx := context.Background()
352
353	//
354	// First Sync from an initial state
355	//
356	stats := s.Stats()
357	assert.Equal(t, false, stats.Synced())
358
359	// Test Sync for rollup genesis block
360	syncBlock, discards, err := s.Sync2(ctx, nil)
361	require.NoError(t, err)
362	require.Nil(t, discards)
363	require.NotNil(t, syncBlock)
364	require.Nil(t, syncBlock.Rollup.Vars)
365	require.Nil(t, syncBlock.Auction.Vars)
366	require.Nil(t, syncBlock.WDelayer.Vars)
367	assert.Equal(t, int64(1), syncBlock.Block.Num)
368	stats = s.Stats()
369	assert.Equal(t, int64(1), stats.Eth.FirstBlockNum)
370	assert.Equal(t, int64(1), stats.Eth.LastBlock.Num)
371	assert.Equal(t, int64(1), stats.Sync.LastBlock.Num)
372	vars := s.SCVars()
373	assert.Equal(t, clientSetup.RollupVariables, vars.Rollup)
374	assert.Equal(t, clientSetup.AuctionVariables, vars.Auction)
375	assert.Equal(t, clientSetup.WDelayerVariables, vars.WDelayer)
376
377	dbBlocks, err := s.historyDB.GetAllBlocks()
378	require.NoError(t, err)
379	assert.Equal(t, 2, len(dbBlocks))
380	assert.Equal(t, int64(1), dbBlocks[1].Num)
381
382	// Sync again and expect no new blocks
383	syncBlock, discards, err = s.Sync2(ctx, nil)
384	require.NoError(t, err)
385	require.Nil(t, discards)
386	require.Nil(t, syncBlock)
387
388	//
389	// Generate blockchain and smart contract data, and fill the test smart contracts
390	//
391
392	// Generate blockchain data with til
393	set1 := `
394		Type: Blockchain
395
396		AddToken(1)
397		AddToken(2)
398		AddToken(3)
399
400		CreateAccountDeposit(1) C: 2000 // Idx=256+2=258
401		CreateAccountDeposit(2) A: 2000 // Idx=256+3=259
402		CreateAccountDeposit(1) D: 500  // Idx=256+4=260
403		CreateAccountDeposit(2) B: 500  // Idx=256+5=261
404		CreateAccountDeposit(2) C: 500  // Idx=256+6=262
405
406		CreateAccountCoordinator(1) A // Idx=256+0=256
407		CreateAccountCoordinator(1) B // Idx=256+1=257
408
409		> batchL1 // forge L1UserTxs{nil}, freeze defined L1UserTxs{5}
410		> batchL1 // forge defined L1UserTxs{5}, freeze L1UserTxs{nil}
411		> block // blockNum=2
412
413		CreateAccountDepositTransfer(1) E-A: 1000, 200 // Idx=256+7=263
414		ForceTransfer(1) C-B: 80
415		ForceExit(1) A: 100
416		ForceExit(1) B: 80
417		ForceTransfer(1) A-D: 100
418
419		Transfer(1) C-A: 100 (126)
420		Exit(1) C: 50 (100)
421		Exit(1) D: 30 (100)
422
423		> batchL1 // forge L1UserTxs{nil}, freeze defined L1UserTxs{3}
424		> batchL1 // forge L1UserTxs{3}, freeze defined L1UserTxs{nil}
425		> block // blockNum=3
426	`
427	tc := til.NewContext(chainID, common.RollupConstMaxL1UserTx)
428	tilCfgExtra := til.ConfigExtra{
429		BootCoordAddr: bootCoordAddr,
430		CoordUser:     "A",
431	}
432	blocks, err := tc.GenerateBlocks(set1)
433	require.NoError(t, err)
434	// Sanity check
435	require.Equal(t, 2, len(blocks))
436	// blocks 0 (blockNum=2)
437	i := 0
438	require.Equal(t, 2, int(blocks[i].Block.Num))
439	require.Equal(t, 3, len(blocks[i].Rollup.AddedTokens))
440	require.Equal(t, 5, len(blocks[i].Rollup.L1UserTxs))
441	require.Equal(t, 2, len(blocks[i].Rollup.Batches))
442	require.Equal(t, 2, len(blocks[i].Rollup.Batches[0].L1CoordinatorTxs))
443	// Set StateRoots for batches manually (til doesn't set it)
444	blocks[i].Rollup.Batches[0].Batch.StateRoot =
445		newBigInt("18906357591508007884273218035694076596537737437965299189312069102730480717391")
446	blocks[i].Rollup.Batches[1].Batch.StateRoot =
447		newBigInt("9513185123401321669660637227182204000277156839501731093239187625486561933297")
448	// blocks 1 (blockNum=3)
449	i = 1
450	require.Equal(t, 3, int(blocks[i].Block.Num))
451	require.Equal(t, 5, len(blocks[i].Rollup.L1UserTxs))
452	require.Equal(t, 2, len(blocks[i].Rollup.Batches))
453	require.Equal(t, 3, len(blocks[i].Rollup.Batches[0].L2Txs))
454	// Set StateRoots for batches manually (til doesn't set it)
455	blocks[i].Rollup.Batches[0].Batch.StateRoot =
456		newBigInt("13060270878200012606074130020925677466793317216609491464427188889005039616594")
457	blocks[i].Rollup.Batches[1].Batch.StateRoot =
458		newBigInt("21427104994652624302859637783375978708867165042357535792408500519060088086054")
459
460	// Generate extra required data
461	ethAddTokens(blocks, client)
462
463	err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
464	require.NoError(t, err)
465	tc.FillBlocksL1UserTxsBatchNum(blocks)
466	err = tc.FillBlocksForgedL1UserTxs(blocks)
467	require.NoError(t, err)
468
469	// Add block data to the smart contracts
470	err = client.CtlAddBlocks(blocks)
471	require.NoError(t, err)
472
473	//
474	// Sync to synchronize the current state from the test smart contracts,
475	// and check the outcome
476	//
477
478	// Block 2
479
480	syncBlock, discards, err = s.Sync2(ctx, nil)
481	require.NoError(t, err)
482	require.Nil(t, discards)
483	require.NotNil(t, syncBlock)
484	assert.Nil(t, syncBlock.Rollup.Vars)
485	assert.Nil(t, syncBlock.Auction.Vars)
486	assert.Nil(t, syncBlock.WDelayer.Vars)
487	assert.Equal(t, int64(2), syncBlock.Block.Num)
488	stats = s.Stats()
489	assert.Equal(t, int64(1), stats.Eth.FirstBlockNum)
490	assert.Equal(t, int64(3), stats.Eth.LastBlock.Num)
491	assert.Equal(t, int64(2), stats.Sync.LastBlock.Num)
492
493	checkSyncBlock(t, s, 2, &blocks[0], syncBlock)
494
495	// Block 3
496
497	syncBlock, discards, err = s.Sync2(ctx, nil)
498	assert.NoError(t, err)
499	require.NoError(t, err)
500	require.Nil(t, discards)
501	require.NotNil(t, syncBlock)
502	assert.Nil(t, syncBlock.Rollup.Vars)
503	assert.Nil(t, syncBlock.Auction.Vars)
504	assert.Nil(t, syncBlock.WDelayer.Vars)
505	assert.Equal(t, int64(3), syncBlock.Block.Num)
506	stats = s.Stats()
507	assert.Equal(t, int64(1), stats.Eth.FirstBlockNum)
508	assert.Equal(t, int64(3), stats.Eth.LastBlock.Num)
509	assert.Equal(t, int64(3), stats.Sync.LastBlock.Num)
510
511	checkSyncBlock(t, s, 3, &blocks[1], syncBlock)
512
513	// Block 4
514	// Generate 2 withdraws manually
515	_, err = client.RollupWithdrawMerkleProof(tc.Users["A"].BJJ.Public().Compress(), 1, 4, 256, big.NewInt(100), []*big.Int{}, true)
516	require.NoError(t, err)
517	_, err = client.RollupWithdrawMerkleProof(tc.Users["C"].BJJ.Public().Compress(), 1, 3, 258, big.NewInt(50), []*big.Int{}, false)
518	require.NoError(t, err)
519	client.CtlMineBlock()
520
521	syncBlock, discards, err = s.Sync2(ctx, nil)
522	require.NoError(t, err)
523	require.Nil(t, discards)
524	require.NotNil(t, syncBlock)
525	assert.Nil(t, syncBlock.Rollup.Vars)
526	assert.Nil(t, syncBlock.Auction.Vars)
527	assert.Nil(t, syncBlock.WDelayer.Vars)
528	assert.Equal(t, int64(4), syncBlock.Block.Num)
529	stats = s.Stats()
530	assert.Equal(t, int64(1), stats.Eth.FirstBlockNum)
531	assert.Equal(t, int64(4), stats.Eth.LastBlock.Num)
532	assert.Equal(t, int64(4), stats.Sync.LastBlock.Num)
533	vars = s.SCVars()
534	assert.Equal(t, clientSetup.RollupVariables, vars.Rollup)
535	assert.Equal(t, clientSetup.AuctionVariables, vars.Auction)
536	assert.Equal(t, clientSetup.WDelayerVariables, vars.WDelayer)
537
538	dbExits, err := s.historyDB.GetAllExits()
539	require.NoError(t, err)
540	foundA1, foundC1 := false, false
541	for _, exit := range dbExits {
542		if exit.AccountIdx == 256 && exit.BatchNum == 4 {
543			foundA1 = true
544			assert.Equal(t, int64(4), *exit.InstantWithdrawn)
545		}
546		if exit.AccountIdx == 258 && exit.BatchNum == 3 {
547			foundC1 = true
548			assert.Equal(t, int64(4), *exit.DelayedWithdrawRequest)
549		}
550	}
551	assert.True(t, foundA1)
552	assert.True(t, foundC1)
553
554	// Block 5
555	// Update variables manually
556	rollupVars, auctionVars, wDelayerVars, err := s.historyDB.GetSCVars()
557	require.NoError(t, err)
558	rollupVars.ForgeL1L2BatchTimeout = 42
559	_, err = client.RollupUpdateForgeL1L2BatchTimeout(rollupVars.ForgeL1L2BatchTimeout)
560	require.NoError(t, err)
561
562	auctionVars.OpenAuctionSlots = 17
563	_, err = client.AuctionSetOpenAuctionSlots(auctionVars.OpenAuctionSlots)
564	require.NoError(t, err)
565
566	wDelayerVars.WithdrawalDelay = 99
567	_, err = client.WDelayerChangeWithdrawalDelay(wDelayerVars.WithdrawalDelay)
568	require.NoError(t, err)
569
570	client.CtlMineBlock()
571
572	syncBlock, discards, err = s.Sync2(ctx, nil)
573	require.NoError(t, err)
574	require.Nil(t, discards)
575	require.NotNil(t, syncBlock)
576	assert.NotNil(t, syncBlock.Rollup.Vars)
577	assert.NotNil(t, syncBlock.Auction.Vars)
578	assert.NotNil(t, syncBlock.WDelayer.Vars)
579	assert.Equal(t, int64(5), syncBlock.Block.Num)
580	stats = s.Stats()
581	assert.Equal(t, int64(1), stats.Eth.FirstBlockNum)
582	assert.Equal(t, int64(5), stats.Eth.LastBlock.Num)
583	assert.Equal(t, int64(5), stats.Sync.LastBlock.Num)
584	vars = s.SCVars()
585	assert.NotEqual(t, clientSetup.RollupVariables, vars.Rollup)
586	assert.NotEqual(t, clientSetup.AuctionVariables, vars.Auction)
587	assert.NotEqual(t, clientSetup.WDelayerVariables, vars.WDelayer)
588
589	dbRollupVars, dbAuctionVars, dbWDelayerVars, err := s.historyDB.GetSCVars()
590	require.NoError(t, err)
591	// Set EthBlockNum for Vars to the blockNum in which they were updated (should be 5)
592	rollupVars.EthBlockNum = syncBlock.Block.Num
593	auctionVars.EthBlockNum = syncBlock.Block.Num
594	wDelayerVars.EthBlockNum = syncBlock.Block.Num
595	assert.Equal(t, rollupVars, dbRollupVars)
596	assert.Equal(t, auctionVars, dbAuctionVars)
597	assert.Equal(t, wDelayerVars, dbWDelayerVars)
598
599	//
600	// Reorg test
601	//
602
603	// Redo blocks 2-5 (as a reorg) only leaving:
604	// - 2 create account transactions
605	// - 2 add tokens
606	// We add a 6th block so that the synchronizer can detect the reorg
607	set2 := `
608		Type: Blockchain
609
610		AddToken(1)
611		AddToken(2)
612
613		CreateAccountDeposit(1) C: 2000 // Idx=256+1=257
614
615		CreateAccountCoordinator(1) A // Idx=256+0=256
616
617		> batchL1 // forge L1UserTxs{nil}, freeze defined L1UserTxs{1}
618		> batchL1 // forge defined L1UserTxs{1}, freeze L1UserTxs{nil}
619		> block // blockNum=2
620		> block // blockNum=3
621		> block // blockNum=4
622		> block // blockNum=5
623		> block // blockNum=6
624	`
625	tc = til.NewContext(chainID, common.RollupConstMaxL1UserTx)
626	tilCfgExtra = til.ConfigExtra{
627		BootCoordAddr: bootCoordAddr,
628		CoordUser:     "A",
629	}
630	blocks, err = tc.GenerateBlocks(set2)
631	require.NoError(t, err)
632
633	// Set StateRoots for batches manually (til doesn't set it)
634	blocks[0].Rollup.Batches[0].Batch.StateRoot =
635		newBigInt("11218510534825843475100588932060366395781087435899915642332104464234485046683")
636	blocks[0].Rollup.Batches[1].Batch.StateRoot =
637		newBigInt("20283020730369146334077598087403837297563965802277806438205710455191646998983")
638
639	for i := 0; i < 4; i++ {
640		client.CtlRollback()
641	}
642	block := client.CtlLastBlock()
643	require.Equal(t, int64(1), block.Num)
644
645	// Generate extra required data
646	ethAddTokens(blocks, client)
647
648	err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
649	require.NoError(t, err)
650	tc.FillBlocksL1UserTxsBatchNum(blocks)
651
652	// Add block data to the smart contracts
653	err = client.CtlAddBlocks(blocks)
654	require.NoError(t, err)
655
656	// First sync detects the reorg and discards 4 blocks
657	syncBlock, discards, err = s.Sync2(ctx, nil)
658	require.NoError(t, err)
659	expetedDiscards := int64(4)
660	require.Equal(t, &expetedDiscards, discards)
661	require.Nil(t, syncBlock)
662	stats = s.Stats()
663	assert.Equal(t, false, stats.Synced())
664	assert.Equal(t, int64(6), stats.Eth.LastBlock.Num)
665	vars = s.SCVars()
666	assert.Equal(t, clientSetup.RollupVariables, vars.Rollup)
667	assert.Equal(t, clientSetup.AuctionVariables, vars.Auction)
668	assert.Equal(t, clientSetup.WDelayerVariables, vars.WDelayer)
669
670	// At this point, the DB only has data up to block 1
671	dbBlock, err := s.historyDB.GetLastBlock()
672	require.NoError(t, err)
673	assert.Equal(t, int64(1), dbBlock.Num)
674
675	// Accounts in HistoryDB and StateDB must be empty
676	dbAccounts, err := s.historyDB.GetAllAccounts()
677	require.NoError(t, err)
678	sdbAccounts, err := s.stateDB.TestGetAccounts()
679	require.NoError(t, err)
680	assert.Equal(t, 0, len(dbAccounts))
681	assertEqualAccountsHistoryDBStateDB(t, dbAccounts, sdbAccounts)
682
683	// Sync blocks 2-6
684	for i := 0; i < 5; i++ {
685		syncBlock, discards, err = s.Sync2(ctx, nil)
686		require.NoError(t, err)
687		require.Nil(t, discards)
688		require.NotNil(t, syncBlock)
689		assert.Nil(t, syncBlock.Rollup.Vars)
690		assert.Nil(t, syncBlock.Auction.Vars)
691		assert.Nil(t, syncBlock.WDelayer.Vars)
692		assert.Equal(t, int64(2+i), syncBlock.Block.Num)
693
694		stats = s.Stats()
695		assert.Equal(t, int64(1), stats.Eth.FirstBlockNum)
696		assert.Equal(t, int64(6), stats.Eth.LastBlock.Num)
697		assert.Equal(t, int64(2+i), stats.Sync.LastBlock.Num)
698		if i == 4 {
699			assert.Equal(t, true, stats.Synced())
700		} else {
701			assert.Equal(t, false, stats.Synced())
702		}
703
704		vars = s.SCVars()
705		assert.Equal(t, clientSetup.RollupVariables, vars.Rollup)
706		assert.Equal(t, clientSetup.AuctionVariables, vars.Auction)
707		assert.Equal(t, clientSetup.WDelayerVariables, vars.WDelayer)
708	}
709
710	dbBlock, err = s.historyDB.GetLastBlock()
711	require.NoError(t, err)
712	assert.Equal(t, int64(6), dbBlock.Num)
713
714	// Accounts in HistoryDB and StateDB is only 2 entries
715	dbAccounts, err = s.historyDB.GetAllAccounts()
716	require.NoError(t, err)
717	sdbAccounts, err = s.stateDB.TestGetAccounts()
718	require.NoError(t, err)
719	assert.Equal(t, 2, len(dbAccounts))
720	assertEqualAccountsHistoryDBStateDB(t, dbAccounts, sdbAccounts)
721}
722
723func TestSyncForgerCommitment(t *testing.T) {
724	stateDB, historyDB := newTestModules(t)
725
726	// Init eth client
727	var timer timer
728	clientSetup := test.NewClientSetupExample()
729	clientSetup.ChainID = big.NewInt(int64(chainID))
730	clientSetup.AuctionConstants.GenesisBlockNum = 2
731	clientSetup.AuctionConstants.BlocksPerSlot = 4
732	clientSetup.AuctionVariables.SlotDeadline = 2
733	bootCoordAddr := clientSetup.AuctionVariables.BootCoordinator
734	client := test.NewClient(true, &timer, &ethCommon.Address{}, clientSetup)
735
736	// Create Synchronizer
737	s, err := NewSynchronizer(client, historyDB, stateDB, Config{
738		StatsRefreshPeriod: 0 * time.Second,
739	})
740	require.NoError(t, err)
741
742	ctx := context.Background()
743
744	set := `
745		Type: Blockchain
746
747		// Slot = 0
748
749		> block // 2
750		> block // 3
751		> block // 4
752		> block // 5
753
754		// Slot = 1
755
756		> block // 6
757		> batch
758		> block // 7
759		> block // 8
760		> block // 9
761
762		// Slot = 2
763
764		> block // 10
765		> block // 11
766		> batch
767		> block // 12
768		> block // 13
769
770	`
771	// For each block, true when the slot that belongs to the following
772	// block has forgerCommitment
773	commitment := map[int64]bool{
774		2: false,
775		3: false,
776		4: false,
777		5: false,
778
779		6: false,
780		7: true,
781		8: true,
782		9: false,
783
784		10: false,
785		11: false,
786		12: false,
787		13: false,
788	}
789	tc := til.NewContext(chainID, common.RollupConstMaxL1UserTx)
790	blocks, err := tc.GenerateBlocks(set)
791	assert.NoError(t, err)
792
793	tilCfgExtra := til.ConfigExtra{
794		BootCoordAddr: bootCoordAddr,
795		CoordUser:     "A",
796	}
797	err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
798	require.NoError(t, err)
799
800	// for i := range blocks {
801	// 	for j := range blocks[i].Rollup.Batches {
802	// 		blocks[i].Rollup.Batches[j].Batch.SlotNum = int64(i) / 4
803	// 	}
804	// }
805
806	// be in sync
807	for {
808		syncBlock, discards, err := s.Sync2(ctx, nil)
809		require.NoError(t, err)
810		require.Nil(t, discards)
811		if syncBlock == nil {
812			break
813		}
814	}
815	stats := s.Stats()
816	require.Equal(t, int64(1), stats.Sync.LastBlock.Num)
817
818	// Store ForgerComitmnent observed at every block by the live synchronizer
819	syncCommitment := map[int64]bool{}
820	// Store ForgerComitmnent observed at every block by a syncrhonizer that is restarted
821	syncRestartedCommitment := map[int64]bool{}
822	for _, block := range blocks {
823		// Add block data to the smart contracts
824		err = client.CtlAddBlocks([]common.BlockData{block})
825		require.NoError(t, err)
826
827		syncBlock, discards, err := s.Sync2(ctx, nil)
828		require.NoError(t, err)
829		require.Nil(t, discards)
830		if syncBlock == nil {
831			break
832		}
833		stats := s.Stats()
834		require.True(t, stats.Synced())
835		syncCommitment[syncBlock.Block.Num] = stats.Sync.Auction.CurrentSlot.ForgerCommitment
836
837		s2, err := NewSynchronizer(client, historyDB, stateDB, Config{
838			StatsRefreshPeriod: 0 * time.Second,
839		})
840		require.NoError(t, err)
841		stats = s2.Stats()
842		require.True(t, stats.Synced())
843		syncRestartedCommitment[syncBlock.Block.Num] = stats.Sync.Auction.CurrentSlot.ForgerCommitment
844	}
845	assert.Equal(t, commitment, syncCommitment)
846	assert.Equal(t, commitment, syncRestartedCommitment)
847}