From 8ded7b53a4477482583f58bb60e4d3a2811f2f32 Mon Sep 17 00:00:00 2001 From: pk910 Date: Fri, 23 Aug 2024 13:07:53 +0200 Subject: [PATCH] clean up `db.GetSlotsByRoots` --- db/slots.go | 25 +++++++++++++------------ services/chainservice_blocks.go | 26 ++++++++++++++------------ 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/db/slots.go b/db/slots.go index f0f60f33..a140d537 100644 --- a/db/slots.go +++ b/db/slots.go @@ -154,15 +154,6 @@ func GetSlotByRoot(root []byte) *dbtypes.Slot { } func GetSlotsByRoots(roots [][]byte) map[phase0.Root]*dbtypes.Slot { - var sql strings.Builder - fmt.Fprintf(&sql, `SELECT - root, slot, parent_root, state_root, status, proposer, graffiti, graffiti_text, - attestation_count, deposit_count, exit_count, withdraw_count, withdraw_amount, attester_slashing_count, - proposer_slashing_count, bls_change_count, eth_transaction_count, eth_block_number, eth_block_hash, - eth_block_extra, eth_block_extra_text, sync_participation, fork_id - FROM slots - WHERE root IN `) - argIdx := 0 args := make([]any, len(roots)) plcList := make([]string, len(roots)) @@ -171,14 +162,24 @@ func GetSlotsByRoots(roots [][]byte) map[phase0.Root]*dbtypes.Slot { args[argIdx] = root argIdx += 1 } - fmt.Fprintf(&sql, "(%v)", strings.Join(plcList, ", ")) - fmt.Fprintf(&sql, " ORDER BY slot DESC") + var sql strings.Builder + fmt.Fprintf(&sql, + `SELECT + root, slot, parent_root, state_root, status, proposer, graffiti, graffiti_text, + attestation_count, deposit_count, exit_count, withdraw_count, withdraw_amount, attester_slashing_count, + proposer_slashing_count, bls_change_count, eth_transaction_count, eth_block_number, eth_block_hash, + eth_block_extra, eth_block_extra_text, sync_participation, fork_id + FROM slots + WHERE root IN (%v) + ORDER BY slot DESC`, + strings.Join(plcList, ", "), + ) slots := []*dbtypes.Slot{} err := ReaderDb.Select(&slots, sql.String(), args...) if err != nil { - //logger.Errorf("Error while fetching block by root 0x%x: %v", root, err) + logger.Errorf("Error while fetching block by roots: %v", err) return nil } diff --git a/services/chainservice_blocks.go b/services/chainservice_blocks.go index d1e9a566..76a73c18 100644 --- a/services/chainservice_blocks.go +++ b/services/chainservice_blocks.go @@ -407,11 +407,12 @@ func (bs *ChainService) GetDbBlocksForSlots(firstSlot uint64, slotLimit uint32, // load selected blocks from db if len(blockRoots) > 0 { blockMap := db.GetSlotsByRoots(blockRoots) - - for idx, blockRoot := range blockRoots { - if dbBlock, ok := blockMap[phase0.Root(blockRoot)]; ok { - dbBlock.Status = resBlocks[blockRootsIdx[idx]].Status - resBlocks[blockRootsIdx[idx]] = dbBlock + if blockMap != nil { + for idx, blockRoot := range blockRoots { + if dbBlock, ok := blockMap[phase0.Root(blockRoot)]; ok { + dbBlock.Status = resBlocks[blockRootsIdx[idx]].Status + resBlocks[blockRootsIdx[idx]] = dbBlock + } } } } @@ -688,16 +689,17 @@ func (bs *ChainService) GetDbBlocksByFilter(filter *dbtypes.BlockFilter, pageIdx // load pruned blocks from database if len(blockRoots) > 0 { blockMap := db.GetSlotsByRoots(blockRoots) + if blockMap != nil { + for idx, blockRoot := range blockRoots { + if dbBlock, ok := blockMap[phase0.Root(blockRoot)]; ok { - for idx, blockRoot := range blockRoots { - if dbBlock, ok := blockMap[phase0.Root(blockRoot)]; ok { + dbBlock.Status = dbtypes.Canonical + if cachedMatches[blockRootsCachedId[idx]].orphaned { + dbBlock.Status = dbtypes.Orphaned + } - dbBlock.Status = dbtypes.Canonical - if cachedMatches[blockRootsCachedId[idx]].orphaned { - dbBlock.Status = dbtypes.Orphaned + resBlocks[blockRootsIdx[idx]].Block = dbBlock } - - resBlocks[blockRootsIdx[idx]].Block = dbBlock } } }