Skip to content

Commit

Permalink
clean up db.GetSlotsByRoots
Browse files Browse the repository at this point in the history
  • Loading branch information
pk910 committed Aug 23, 2024
1 parent 9fec525 commit 8ded7b5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
25 changes: 13 additions & 12 deletions db/slots.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
}

Expand Down
26 changes: 14 additions & 12 deletions services/chainservice_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand Down Expand Up @@ -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
}
}
}
Expand Down

0 comments on commit 8ded7b5

Please sign in to comment.