mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Sync ForgerCommitment and use it in coord
Previously the coordinator was erroneously using Slot.BatchesLen to determine when anyone can forge. The correct behaviour is implmenented with the boolean flag `ForgerCommitment`, that is set to true only when there's a batch before the deadline within a slot. Delete Slot.BatchesLen, and the synchronization code of this value from the Synchronizer, as this is not needed
This commit is contained in:
@@ -293,11 +293,15 @@ func (hdb *HistoryDB) GetBatches(from, to common.BatchNum) ([]common.Batch, erro
|
||||
return db.SlicePtrsToSlice(batches).([]common.Batch), tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetBatchesLen retrieve number of batches from the DB, given a slotNum
|
||||
func (hdb *HistoryDB) GetBatchesLen(slotNum int64) (int, error) {
|
||||
row := hdb.db.QueryRow("SELECT COUNT(*) FROM batch WHERE slot_num = $1;", slotNum)
|
||||
var batchesLen int
|
||||
return batchesLen, tracerr.Wrap(row.Scan(&batchesLen))
|
||||
// GetFirstBatchBlockNumBySlot returns the ethereum block number of the first
|
||||
// batch within a slot
|
||||
func (hdb *HistoryDB) GetFirstBatchBlockNumBySlot(slotNum int64) (int64, error) {
|
||||
row := hdb.db.QueryRow(
|
||||
`SELECT eth_block_num FROM batch
|
||||
WHERE slot_num = $1 ORDER BY batch_num ASC LIMIT 1;`, slotNum,
|
||||
)
|
||||
var blockNum int64
|
||||
return blockNum, tracerr.Wrap(row.Scan(&blockNum))
|
||||
}
|
||||
|
||||
// GetLastBatchNum returns the BatchNum of the latest forged batch
|
||||
|
||||
@@ -1051,6 +1051,70 @@ func TestGetLastTxsPosition(t *testing.T) {
|
||||
assert.Equal(t, sql.ErrNoRows.Error(), err.Error())
|
||||
}
|
||||
|
||||
func TestGetFirstBatchBlockNumBySlot(t *testing.T) {
|
||||
test.WipeDB(historyDB.DB())
|
||||
|
||||
set := `
|
||||
Type: Blockchain
|
||||
|
||||
// Slot = 0
|
||||
|
||||
> block // 2
|
||||
> block // 3
|
||||
> block // 4
|
||||
> block // 5
|
||||
|
||||
// Slot = 1
|
||||
|
||||
> block // 6
|
||||
> block // 7
|
||||
> batch
|
||||
> block // 8
|
||||
> block // 9
|
||||
|
||||
// Slot = 2
|
||||
|
||||
> batch
|
||||
> block // 10
|
||||
> block // 11
|
||||
> block // 12
|
||||
> block // 13
|
||||
|
||||
`
|
||||
tc := til.NewContext(uint16(0), common.RollupConstMaxL1UserTx)
|
||||
blocks, err := tc.GenerateBlocks(set)
|
||||
assert.NoError(t, err)
|
||||
|
||||
tilCfgExtra := til.ConfigExtra{
|
||||
CoordUser: "A",
|
||||
}
|
||||
err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
|
||||
require.NoError(t, err)
|
||||
|
||||
for i := range blocks {
|
||||
for j := range blocks[i].Rollup.Batches {
|
||||
blocks[i].Rollup.Batches[j].Batch.SlotNum = int64(i) / 4
|
||||
}
|
||||
}
|
||||
|
||||
// Add all blocks
|
||||
for i := range blocks {
|
||||
err = historyDB.AddBlockSCData(&blocks[i])
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
_, err = historyDB.GetFirstBatchBlockNumBySlot(0)
|
||||
require.Equal(t, sql.ErrNoRows, tracerr.Unwrap(err))
|
||||
|
||||
bn1, err := historyDB.GetFirstBatchBlockNumBySlot(1)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(8), bn1)
|
||||
|
||||
bn2, err := historyDB.GetFirstBatchBlockNumBySlot(2)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(10), bn2)
|
||||
}
|
||||
|
||||
// setTestBlocks WARNING: this will delete the blocks and recreate them
|
||||
func setTestBlocks(from, to int64) []common.Block {
|
||||
test.WipeDB(historyDB.DB())
|
||||
|
||||
Reference in New Issue
Block a user