Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ethpandaops/dora into skylenet/pe…
Browse files Browse the repository at this point in the history
…er-das-view
  • Loading branch information
skylenet committed Sep 23, 2024
2 parents 049e016 + db7786b commit 1f3e96b
Show file tree
Hide file tree
Showing 16 changed files with 1,384 additions and 96 deletions.
2 changes: 2 additions & 0 deletions cmd/dora-explorer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ func startFrontend(webserver *http.Server) {
router.HandleFunc("/validators/included_deposits", handlers.IncludedDeposits).Methods("GET")
router.HandleFunc("/validators/voluntary_exits", handlers.VoluntaryExits).Methods("GET")
router.HandleFunc("/validators/slashings", handlers.Slashings).Methods("GET")
router.HandleFunc("/validators/el_withdrawals", handlers.ElWithdrawals).Methods("GET")
router.HandleFunc("/validators/el_consolidations", handlers.ElConsolidations).Methods("GET")
router.HandleFunc("/validator/{idxOrPubKey}", handlers.Validator).Methods("GET")
router.HandleFunc("/validator/{index}/slots", handlers.ValidatorSlots).Methods("GET")

Expand Down
123 changes: 123 additions & 0 deletions db/consolidation_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,126 @@ func InsertConsolidationRequests(consolidations []*dbtypes.ConsolidationRequest,
}
return nil
}

func GetConsolidationRequestsFiltered(offset uint64, limit uint32, finalizedBlock uint64, filter *dbtypes.ConsolidationRequestFilter) ([]*dbtypes.ConsolidationRequest, uint64, error) {
var sql strings.Builder
args := []interface{}{}
fmt.Fprint(&sql, `
WITH cte AS (
SELECT
slot_number, slot_root, slot_index, orphaned, fork_id, source_address, source_index, source_pubkey, target_index, target_pubkey, tx_hash
FROM consolidation_requests
`)

if filter.SrcValidatorName != "" {
fmt.Fprint(&sql, `
LEFT JOIN validator_names AS source_names ON source_names."index" = consolidation_requests.source_index
`)
}
if filter.TgtValidatorName != "" {
fmt.Fprint(&sql, `
LEFT JOIN validator_names AS target_names ON target_names."index" = consolidation_requests.target_index
`)
}

filterOp := "WHERE"
if filter.MinSlot > 0 {
args = append(args, filter.MinSlot)
fmt.Fprintf(&sql, " %v slot_number >= $%v", filterOp, len(args))
filterOp = "AND"
}
if filter.MaxSlot > 0 {
args = append(args, filter.MaxSlot)
fmt.Fprintf(&sql, " %v slot_number <= $%v", filterOp, len(args))
filterOp = "AND"
}
if len(filter.SourceAddress) > 0 {
args = append(args, filter.SourceAddress)
fmt.Fprintf(&sql, " %v source_address = $%v", filterOp, len(args))
filterOp = "AND"
}
if filter.MinSrcIndex > 0 {
args = append(args, filter.MinSrcIndex)
fmt.Fprintf(&sql, " %v source_index >= $%v", filterOp, len(args))
filterOp = "AND"
}
if filter.MaxSrcIndex > 0 {
args = append(args, filter.MaxSrcIndex)
fmt.Fprintf(&sql, " %v source_index <= $%v", filterOp, len(args))
filterOp = "AND"
}
if filter.MinTgtIndex > 0 {
args = append(args, filter.MinTgtIndex)
fmt.Fprintf(&sql, " %v target_index >= $%v", filterOp, len(args))
filterOp = "AND"
}
if filter.MaxTgtIndex > 0 {
args = append(args, filter.MaxTgtIndex)
fmt.Fprintf(&sql, " %v target_index <= $%v", filterOp, len(args))
filterOp = "AND"
}
if filter.SrcValidatorName != "" {
args = append(args, "%"+filter.SrcValidatorName+"%")
fmt.Fprintf(&sql, " %v ", filterOp)
fmt.Fprintf(&sql, EngineQuery(map[dbtypes.DBEngineType]string{
dbtypes.DBEnginePgsql: ` source_names.name ilike $%v `,
dbtypes.DBEngineSqlite: ` source_names.name LIKE $%v `,
}), len(args))
filterOp = "AND"
}
if filter.TgtValidatorName != "" {
args = append(args, "%"+filter.TgtValidatorName+"%")
fmt.Fprintf(&sql, " %v ", filterOp)
fmt.Fprintf(&sql, EngineQuery(map[dbtypes.DBEngineType]string{
dbtypes.DBEnginePgsql: ` target_names.name ilike $%v `,
dbtypes.DBEngineSqlite: ` target_names.name LIKE $%v `,
}), len(args))
filterOp = "AND"
}

if filter.WithOrphaned == 0 {
args = append(args, finalizedBlock)
fmt.Fprintf(&sql, " %v (slot_number > $%v OR orphaned = false)", filterOp, len(args))
filterOp = "AND"
} else if filter.WithOrphaned == 2 {
args = append(args, finalizedBlock)
fmt.Fprintf(&sql, " %v (slot_number > $%v OR orphaned = true)", filterOp, len(args))
filterOp = "AND"
}

args = append(args, limit)
fmt.Fprintf(&sql, `)
SELECT
count(*) AS slot_number,
null AS slot_root,
0 AS slot_index,
false AS orphaned,
0 AS fork_id,
null AS source_address,
0 AS source_index,
null AS source_pubkey,
0 AS target_index,
null AS target_pubkey,
null AS tx_hash
FROM cte
UNION ALL SELECT * FROM (
SELECT * FROM cte
ORDER BY slot_number DESC, slot_index DESC
LIMIT $%v
`, len(args))

if offset > 0 {
args = append(args, offset)
fmt.Fprintf(&sql, " OFFSET $%v ", len(args))
}
fmt.Fprintf(&sql, ") AS t1")

consolidationRequests := []*dbtypes.ConsolidationRequest{}
err := ReaderDb.Select(&consolidationRequests, sql.String(), args...)
if err != nil {
logger.Errorf("Error while fetching filtered consolidation requests: %v", err)
return nil, 0, err
}

return consolidationRequests[1:], consolidationRequests[0].SlotNumber, nil
}
25 changes: 15 additions & 10 deletions db/withdrawal_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func GetWithdrawalRequestsFiltered(offset uint64, limit uint32, finalizedBlock u
FROM withdrawal_requests
`)

if filter.SourceValidatorName != "" {
if filter.ValidatorName != "" {
fmt.Fprint(&sql, `
LEFT JOIN validator_names AS source_names ON source_names."index" = withdrawal_requests.validator_index
`)
Expand All @@ -92,28 +92,33 @@ func GetWithdrawalRequestsFiltered(offset uint64, limit uint32, finalizedBlock u
fmt.Fprintf(&sql, " %v source_address = $%v", filterOp, len(args))
filterOp = "AND"
}
if filter.MinSourceIndex > 0 {
args = append(args, filter.MinSourceIndex)
if filter.MinIndex > 0 {
args = append(args, filter.MinIndex)
fmt.Fprintf(&sql, " %v validator_index >= $%v", filterOp, len(args))
filterOp = "AND"
}
if filter.MaxSourceIndex > 0 {
args = append(args, filter.MaxSourceIndex)
if filter.MaxIndex > 0 {
args = append(args, filter.MaxIndex)
fmt.Fprintf(&sql, " %v validator_index <= $%v", filterOp, len(args))
filterOp = "AND"
}
if filter.SourceValidatorName != "" {
args = append(args, "%"+filter.SourceValidatorName+"%")
if filter.ValidatorName != "" {
args = append(args, "%"+filter.ValidatorName+"%")
fmt.Fprintf(&sql, " %v ", filterOp)
fmt.Fprintf(&sql, EngineQuery(map[dbtypes.DBEngineType]string{
dbtypes.DBEnginePgsql: ` source_names.name ilike $%v `,
dbtypes.DBEngineSqlite: ` source_names.name LIKE $%v `,
}), len(args))
filterOp = "AND"
}
if filter.Amount != nil {
args = append(args, *filter.Amount)
fmt.Fprintf(&sql, " %v amount = $%v", filterOp, len(args))
if filter.MinAmount != nil {
args = append(args, *filter.MinAmount)
fmt.Fprintf(&sql, " %v amount >= $%v", filterOp, len(args))
filterOp = "AND"
}
if filter.MaxAmount != nil {
args = append(args, *filter.MaxAmount)
fmt.Fprintf(&sql, " %v amount <= $%v", filterOp, len(args))
filterOp = "AND"
}

Expand Down
33 changes: 22 additions & 11 deletions dbtypes/other.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,26 @@ type SlashingFilter struct {
}

type WithdrawalRequestFilter struct {
MinSlot uint64
MaxSlot uint64
SourceAddress []byte
MinSourceIndex uint64
MaxSourceIndex uint64
SourceValidatorName string
MinTargetIndex uint64
MaxTargetIndex uint64
TargetValidatorName string
Amount *uint64
WithOrphaned uint8
MinSlot uint64
MaxSlot uint64
SourceAddress []byte
MinIndex uint64
MaxIndex uint64
ValidatorName string
MinAmount *uint64
MaxAmount *uint64
WithOrphaned uint8
}

type ConsolidationRequestFilter struct {
MinSlot uint64
MaxSlot uint64
SourceAddress []byte
MinSrcIndex uint64
MaxSrcIndex uint64
SrcValidatorName string
MinTgtIndex uint64
MaxTgtIndex uint64
TgtValidatorName string
WithOrphaned uint8
}
Loading

0 comments on commit 1f3e96b

Please sign in to comment.