Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Use enum for handling bid sorting #76

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions crates/common/src/api/data_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ use ethereum_consensus::{
};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Deserialize, Serialize, Clone)]
pub enum BidsOrderBy {
HighToLow,
LowToHigh,
}

#[derive(Debug, Deserialize, Serialize, Default, Clone)]
pub struct BidFilters {
pub slot: Option<u64>,
Expand All @@ -13,7 +19,7 @@ pub struct BidFilters {
pub block_number: Option<u64>,
pub proposer_pubkey: Option<BlsPublicKey>,
pub builder_pubkey: Option<BlsPublicKey>,
pub order_by: Option<i8>,
pub order_by: Option<BidsOrderBy>,
}

#[derive(Debug, Deserialize, Serialize)]
Expand All @@ -40,8 +46,8 @@ impl From<ProposerPayloadDeliveredParams> for BidFilters {
builder_pubkey: value.builder_pubkey,
order_by: match value.order_by.as_ref() {
Some(s) => match s.as_str() {
"value" => Some(1),
"-value" => Some(-1),
"value" => Some(BidsOrderBy::LowToHigh),
"-value" => Some(BidsOrderBy::HighToLow),
_ => None,
},
None => None,
Expand Down
6 changes: 3 additions & 3 deletions crates/database/src/postgres/postgres_db_filters.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use helix_common::api::data_api::BidFilters;
use helix_common::api::data_api::{BidFilters, BidsOrderBy};

#[derive(Clone, Debug)]
pub struct PgBidFilters(BidFilters);
Expand Down Expand Up @@ -40,8 +40,8 @@ impl PgBidFilters {
self.0.block_hash.as_ref().map(|block_hash| block_hash.as_ref())
}

pub fn order(&self) -> Option<i32> {
self.0.order_by.map(|order_by| order_by as i32)
pub fn order(&self) -> Option<&BidsOrderBy> {
self.0.order_by.as_ref()
}

pub fn limit(&self) -> Option<i64> {
Expand Down
7 changes: 5 additions & 2 deletions crates/database/src/postgres/postgres_db_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use ethereum_consensus::{altair::Hash32, primitives::BlsPublicKey, ssz::prelude:

use helix_common::{
api::{
builder_api::BuilderGetValidatorsResponseEntry, data_api::BidFilters,
builder_api::BuilderGetValidatorsResponseEntry,
data_api::{BidFilters, BidsOrderBy},
proposer_api::ValidatorRegistrationInfo,
},
bid_submission::{
Expand Down Expand Up @@ -1585,7 +1586,9 @@ impl DatabaseService for PostgresDatabaseService {

if let Some(order) = filters.order() {
query.push_str(" ORDER BY block_submission.value ");
query.push_str(if order >= 0 { "ASC" } else { "DESC" });
if order == &BidsOrderBy::HighToLow {
query.push_str("DESC");
}
} else {
query.push_str(" ORDER BY block_submission.slot_number DESC");
}
Expand Down
Loading