Update coordinator, call all api update functions

- Common:
	- Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition
- API:
	- Add UpdateNetworkInfoBlock to update just block information, to be
	  used when the node is not yet synchronized
- Node:
	- Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with
	  configurable time intervals
- Synchronizer:
	- When mapping events by TxHash, use an array to support the possibility
	  of multiple calls of the same function happening in the same
	  transaction (for example, a smart contract in a single transaction
	  could call withdraw with delay twice, which would generate 2 withdraw
	  events, and 2 deposit events).
	- In Stats, keep entire LastBlock instead of just the blockNum
	- In Stats, add lastL1BatchBlock
	- Test Stats and SCVars
- Coordinator:
	- Enable writing the BatchInfo in every step of the pipeline to disk
	  (with JSON text files) for debugging purposes.
	- Move the Pipeline functionality from the Coordinator to its own struct
	  (Pipeline)
	- Implement shouldL1lL2Batch
	- In TxManager, implement logic to perform several attempts when doing
	  ethereum node RPC calls before considering the error. (Both for calls
	  to forgeBatch and transaction receipt)
	- In TxManager, reorganize the flow and note the specific points in
	  which actions are made when err != nil
- HistoryDB:
	- Implement GetLastL1BatchBlockNum: returns the blockNum of the latest
	  forged l1Batch, to help the coordinator decide when to forge an
	  L1Batch.
- EthereumClient and test.Client:
	- Update EthBlockByNumber to return the last block when the passed
	  number is -1.
This commit is contained in:
Eduard S
2020-11-20 14:46:01 +01:00
parent 58c9be3644
commit 8f1cf2f145
26 changed files with 1039 additions and 402 deletions

View File

@@ -304,6 +304,15 @@ func (hdb *HistoryDB) GetLastBatchNum() (common.BatchNum, error) {
return batchNum, row.Scan(&batchNum)
}
// GetLastL1BatchBlockNum returns the blockNum of the latest forged l1Batch
func (hdb *HistoryDB) GetLastL1BatchBlockNum() (int64, error) {
row := hdb.db.QueryRow(`SELECT eth_block_num FROM batch
WHERE forge_l1_txs_num IS NOT NULL
ORDER BY batch_num DESC LIMIT 1;`)
var blockNum int64
return blockNum, row.Scan(&blockNum)
}
// GetLastL1TxsNum returns the greatest ForgeL1TxsNum in the DB from forged
// batches. If there's no batch in the DB (nil, nil) is returned.
func (hdb *HistoryDB) GetLastL1TxsNum() (*int64, error) {
@@ -1410,7 +1419,7 @@ func (hdb *HistoryDB) AddBlockSCData(blockData *common.BlockData) (err error) {
}
}
if err := hdb.updateExitTree(txn, blockData.Block.EthBlockNum,
if err := hdb.updateExitTree(txn, blockData.Block.Num,
blockData.Rollup.Withdrawals, blockData.WDelayer.Withdrawals); err != nil {
return err
}

View File

@@ -107,7 +107,7 @@ func TestBlocks(t *testing.T) {
}
func assertEqualBlock(t *testing.T, expected *common.Block, actual *common.Block) {
assert.Equal(t, expected.EthBlockNum, actual.EthBlockNum)
assert.Equal(t, expected.Num, actual.Num)
assert.Equal(t, expected.Hash, actual.Hash)
assert.Equal(t, expected.Timestamp.Unix(), actual.Timestamp.Unix())
}
@@ -150,6 +150,7 @@ func TestBatches(t *testing.T) {
batches := []common.Batch{}
tokensValue := make(map[common.TokenID]float64)
lastL1TxsNum := new(int64)
lastL1BatchBlockNum := int64(0)
for _, block := range blocks {
// Insert block
assert.NoError(t, historyDB.AddBlock(&block.Block))
@@ -169,6 +170,7 @@ func TestBatches(t *testing.T) {
forgeTxsNum := batch.Batch.ForgeL1TxsNum
if forgeTxsNum != nil && (lastL1TxsNum == nil || *lastL1TxsNum < *forgeTxsNum) {
*lastL1TxsNum = *forgeTxsNum
lastL1BatchBlockNum = batch.Batch.EthBlockNum
}
}
}
@@ -199,6 +201,10 @@ func TestBatches(t *testing.T) {
fetchedLastL1TxsNum, err := historyDB.GetLastL1TxsNum()
assert.NoError(t, err)
assert.Equal(t, lastL1TxsNum, fetchedLastL1TxsNum)
// Test GetLastL1BatchBlockNum
fetchedLastL1BatchBlockNum, err := historyDB.GetLastL1BatchBlockNum()
assert.NoError(t, err)
assert.Equal(t, lastL1BatchBlockNum, fetchedLastL1BatchBlockNum)
}
func TestBids(t *testing.T) {
@@ -735,7 +741,7 @@ func TestUpdateExitTree(t *testing.T) {
// Add withdraws to the second-to-last block, and insert block into the DB
block := &blocks[len(blocks)-2]
require.Equal(t, int64(4), block.Block.EthBlockNum)
require.Equal(t, int64(4), block.Block.Num)
tokenAddr := blocks[0].Rollup.AddedTokens[0].EthAddr
// block.WDelayer.Deposits = append(block.WDelayer.Deposits,
// common.WDelayerTransfer{Owner: tc.UsersByIdx[257].Addr, Token: tokenAddr, Amount: big.NewInt(80)}, // 257
@@ -752,7 +758,7 @@ func TestUpdateExitTree(t *testing.T) {
err = historyDB.addBlock(historyDB.db, &block.Block)
require.Nil(t, err)
err = historyDB.updateExitTree(historyDB.db, block.Block.EthBlockNum,
err = historyDB.updateExitTree(historyDB.db, block.Block.Num,
block.Rollup.Withdrawals, block.WDelayer.Withdrawals)
require.Nil(t, err)
@@ -767,15 +773,15 @@ func TestUpdateExitTree(t *testing.T) {
for _, withdraw := range block.Rollup.Withdrawals {
assert.Equal(t, withdraw.NumExitRoot, dbExitsByIdx[withdraw.Idx].BatchNum)
if withdraw.InstantWithdraw {
assert.Equal(t, &block.Block.EthBlockNum, dbExitsByIdx[withdraw.Idx].InstantWithdrawn)
assert.Equal(t, &block.Block.Num, dbExitsByIdx[withdraw.Idx].InstantWithdrawn)
} else {
assert.Equal(t, &block.Block.EthBlockNum, dbExitsByIdx[withdraw.Idx].DelayedWithdrawRequest)
assert.Equal(t, &block.Block.Num, dbExitsByIdx[withdraw.Idx].DelayedWithdrawRequest)
}
}
// Add delayed withdraw to the last block, and insert block into the DB
block = &blocks[len(blocks)-1]
require.Equal(t, int64(5), block.Block.EthBlockNum)
require.Equal(t, int64(5), block.Block.Num)
block.WDelayer.Withdrawals = append(block.WDelayer.Withdrawals,
common.WDelayerTransfer{
Owner: tc.UsersByIdx[257].Addr,
@@ -785,7 +791,7 @@ func TestUpdateExitTree(t *testing.T) {
err = historyDB.addBlock(historyDB.db, &block.Block)
require.Nil(t, err)
err = historyDB.updateExitTree(historyDB.db, block.Block.EthBlockNum,
err = historyDB.updateExitTree(historyDB.db, block.Block.Num,
block.Rollup.Withdrawals, block.WDelayer.Withdrawals)
require.Nil(t, err)
@@ -795,7 +801,7 @@ func TestUpdateExitTree(t *testing.T) {
for _, dbExit := range dbExits {
dbExitsByIdx[dbExit.AccountIdx] = dbExit
}
require.Equal(t, &block.Block.EthBlockNum, dbExitsByIdx[257].DelayedWithdrawn)
require.Equal(t, &block.Block.Num, dbExitsByIdx[257].DelayedWithdrawn)
}
func TestGetBestBidCoordinator(t *testing.T) {