From 47bb9e6567ec772e8c3fbabb1b647b185c7be104 Mon Sep 17 00:00:00 2001 From: Matt <98158711+BedrockSquirrel@users.noreply.github.com> Date: Tue, 7 Nov 2023 10:34:45 +0000 Subject: [PATCH] ObscuroScan: serve oldest batches first (#1635) --- go/common/query_types.go | 1 + go/host/db/batches.go | 28 +++++++++----- go/host/db/blocks.go | 37 +++++++++++-------- .../frontend/src/components/BatchDataGrid.vue | 21 ++++------- .../src/components/RotatingRollupsItem.vue | 9 +---- 5 files changed, 52 insertions(+), 44 deletions(-) diff --git a/go/common/query_types.go b/go/common/query_types.go index 229346d6db..596a4e4341 100644 --- a/go/common/query_types.go +++ b/go/common/query_types.go @@ -38,6 +38,7 @@ type PublicTransaction struct { type PublicBatch struct { BatchHeader + TxHashes []TxHash `json:"txHashes"` } type PublicBlock struct { diff --git a/go/host/db/batches.go b/go/host/db/batches.go index 8562903af3..b117d7116c 100644 --- a/go/host/db/batches.go +++ b/go/host/db/batches.go @@ -132,25 +132,35 @@ func (db *DB) GetBatchBySequenceNumber(sequenceNumber *big.Int) (*common.ExtBatc return db.GetBatch(*batchHash) } -// GetBatchListing returns BatchListingResponse given a pagination +// GetBatchListing returns latest batches given a pagination. +// For example, page 0, size 10 will return the latest 10 batches. // todo change this when the db changes - this is not super performant func (db *DB) GetBatchListing(pagination *common.QueryPagination) (*common.BatchListingResponse, error) { - // fetch requested batches + // fetch the total batches so we can paginate + header, err := db.GetHeadBatchHeader() + if err != nil { + return nil, err + } + + batchesFrom := header.SequencerOrderNo.Uint64() - pagination.Offset + batchesToInclusive := int(batchesFrom) - int(pagination.Size) + 1 + // batchesToInclusive can't go below zero + if batchesToInclusive < 0 { + batchesToInclusive = 0 + } + var batches []common.PublicBatch - for i := pagination.Offset; i < pagination.Offset+uint64(pagination.Size); i++ { + // fetch requested batches - looping backwards from the latest batch subtracting any pagination offset + // (e.g. front-end showing latest batches first, page 3 of size 10 would be skipping the 30 most recent batches) + for i := batchesFrom; i >= uint64(batchesToInclusive); i-- { extBatch, err := db.GetBatchBySequenceNumber(big.NewInt(int64(i))) if err != nil && !errors.Is(err, errutil.ErrNotFound) { return nil, err } if extBatch != nil { - batches = append(batches, common.PublicBatch{BatchHeader: *extBatch.Header}) + batches = append(batches, common.PublicBatch{BatchHeader: *extBatch.Header, TxHashes: extBatch.TxHashes}) } } - // fetch the total batches so we can paginate - header, err := db.GetHeadBatchHeader() - if err != nil { - return nil, err - } return &common.BatchListingResponse{ BatchesData: batches, diff --git a/go/host/db/blocks.go b/go/host/db/blocks.go index 75af9c8c8b..0569f19e5f 100644 --- a/go/host/db/blocks.go +++ b/go/host/db/blocks.go @@ -58,13 +58,27 @@ func (db *DB) AddBlock(header *types.Header) error { return nil } -// GetBlockListing returns a list of blocks given the pagination +// GetBlockListing returns latest L1 blocks given the pagination. +// For example, page 0, size 10 will return the latest 10 blocks. func (db *DB) GetBlockListing(pagination *common.QueryPagination) (*common.BlockListingResponse, error) { + // fetch the total blocks so we can paginate + tipHeader, err := db.GetBlockAtTip() + if err != nil { + return nil, err + } + + blocksFrom := tipHeader.Number.Uint64() - pagination.Offset + blocksToInclusive := int(blocksFrom) - int(pagination.Size) + 1 + // if blocksToInclusive would be negative, set it to 0 + if blocksToInclusive < 0 { + blocksToInclusive = 0 + } + // fetch requested batches var blocks []common.PublicBlock - for i := pagination.Offset; i < pagination.Offset+uint64(pagination.Size); i++ { + for i := blocksFrom; i > uint64(blocksToInclusive); i-- { header, err := db.GetBlockByHeight(big.NewInt(int64(i))) - if err != nil && !errors.Is(err, errutil.ErrNotFound) { + if err != nil { return nil, err } @@ -74,19 +88,12 @@ func (db *DB) GetBlockListing(pagination *common.QueryPagination) (*common.Block return nil, err } - if header != nil { - listedBlock := common.PublicBlock{BlockHeader: *header} - if rollup != nil { - listedBlock.RollupHash = rollup.Hash() - fmt.Println("added at block: ", header.Number.Int64(), " - ", listedBlock.RollupHash) - } - blocks = append(blocks, listedBlock) + listedBlock := common.PublicBlock{BlockHeader: *header} + if rollup != nil { + listedBlock.RollupHash = rollup.Hash() + fmt.Println("added at block: ", header.Number.Int64(), " - ", listedBlock.RollupHash) } - } - // fetch the total blocks so we can paginate - tipHeader, err := db.GetBlockAtTip() - if err != nil { - return nil, err + blocks = append(blocks, listedBlock) } return &common.BlockListingResponse{ diff --git a/tools/obscuroscan_v2/frontend/src/components/BatchDataGrid.vue b/tools/obscuroscan_v2/frontend/src/components/BatchDataGrid.vue index 8685406799..608db99a57 100644 --- a/tools/obscuroscan_v2/frontend/src/components/BatchDataGrid.vue +++ b/tools/obscuroscan_v2/frontend/src/components/BatchDataGrid.vue @@ -18,7 +18,8 @@ - + + { - store.startPolling() - }) - - // Ensure to stop polling when component is destroyed or deactivated - onUnmounted(() => { - store.stopPolling() + store.fetch() }) return { @@ -80,21 +76,20 @@ export default { const store = useBatchStore() store.size = newSize store.offset = (this.currentPage - 1) * store.size + // reload data + store.fetch() }, // Called when the current page is changed handleCurrentChange(newPage) { const store = useBatchStore() this.currentPage = newPage store.offset = (newPage - 1) * store.size + // reload data + store.fetch() }, toggleWindow(data) { this.$refs.batchInfoWindowRef.displayData(data.hash); }, - }, - computed: { - tableRowClassName() { - return "hover" - } } } diff --git a/tools/obscuroscan_v2/frontend/src/components/RotatingRollupsItem.vue b/tools/obscuroscan_v2/frontend/src/components/RotatingRollupsItem.vue index d3a822d6d0..3ade490d46 100644 --- a/tools/obscuroscan_v2/frontend/src/components/RotatingRollupsItem.vue +++ b/tools/obscuroscan_v2/frontend/src/components/RotatingRollupsItem.vue @@ -26,14 +26,9 @@ export default { setup() { const rollupsStore = useRollupStore() - // Start polling when the component is mounted + // Reload rollup data onMount onMounted(() => { - rollupsStore.startPolling() - }) - - // Ensure to stop polling when component is destroyed or deactivated - onUnmounted(() => { - rollupsStore.stopPolling() + rollupsStore.fetch() }) return {