Skip to content

Commit ae792f9

Browse files
committed
Address review comments
1 parent 007885a commit ae792f9

File tree

5 files changed

+16
-30
lines changed

5 files changed

+16
-30
lines changed

backend-rust/CHANGELOG.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7-
Database schema version: 3
8-
9-
### Added
10-
11-
- Add database schema version 3 with index over accounts that delegate stake to a given baker pool.
12-
- Add `delegator_count`, `delegated_stake`, `total_stake`, and `total_stake_percentage` to baker pool query.
13-
147
Database schema version: 2
158

169
### Added
1710

11+
- Add `delegator_count`, `delegated_stake`, `total_stake`, and `total_stake_percentage` to baker pool query.
12+
- Add index over accounts that delegate stake to a given baker pool.
1813
- Add `database_schema_version` and `api_supported_database_schema_version` to `versions` endpoint.
1914
- Add database schema version 2 with index over blocks with no `cumulative_finalization_time`, to improve indexing performance.
2015

backend-rust/src/graphql_api/baker.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::{
33
ApiResult, ConnectionQuery,
44
};
55
use crate::{
6-
scalar_types::{Amount, BakerId, DateTime, Decimal, MetadataUrl, UnsignedLong},
6+
scalar_types::{Amount, BakerId, DateTime, Decimal, MetadataUrl},
77
transaction_event::{baker::BakerPoolOpenStatus, Event},
88
transaction_reject::TransactionRejectReason,
99
transaction_type::{
@@ -131,9 +131,8 @@ impl Baker {
131131

132132
let total_stake: i64 =
133133
sqlx::query_scalar!("SELECT total_staked FROM blocks ORDER BY height DESC LIMIT 1")
134-
.fetch_optional(pool)
135-
.await?
136-
.ok_or(ApiError::NotFound)?;
134+
.fetch_one(pool)
135+
.await?;
137136

138137
let row = sqlx::query!(
139138
"
@@ -156,9 +155,11 @@ impl Baker {
156155

157156
// Division by 0 is not possible because `total_staked` is always a positive
158157
// number.
159-
let total_stake_percentage = (total_pool_stake as f64 * 100.0 / total_stake as f64)
160-
.try_into()
161-
.map_err(|e: anyhow::Error| ApiError::InternalError(e.to_string()))?;
158+
let total_stake_percentage = (rust_decimal::Decimal::from(total_pool_stake)
159+
* rust_decimal::Decimal::from(100))
160+
.checked_div(rust_decimal::Decimal::from(total_stake))
161+
.ok_or_else(|| ApiError::InternalError("Division by zero or overflow".to_string()))?
162+
.into();
162163

163164
let out = BakerState::ActiveBakerState(ActiveBakerState {
164165
staked_amount: Amount::try_from(self.staked)?,
@@ -172,8 +173,8 @@ impl Baker {
172173
},
173174
metadata_url: self.metadata_url.as_deref(),
174175
total_stake_percentage,
175-
total_stake: UnsignedLong(total_pool_stake.try_into()?),
176-
delegated_stake: UnsignedLong(delegated_stake.try_into()?),
176+
total_stake: total_pool_stake.try_into()?,
177+
delegated_stake: delegated_stake.try_into()?,
177178
delegator_count: row.delegator_count.unwrap_or(0),
178179
},
179180
pending_change: None, // This is not used starting from P7.

backend-rust/src/migrations.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,6 @@ pub enum SchemaVersion {
171171
performance."
172172
)]
173173
IndexBlocksWithNoCumulativeFinTime,
174-
#[display("0003:Adds index over accounts that delegate stake to a given baker pool.")]
175-
IndexAccountsWithDelegatedStakeToBakerId,
176174
}
177175
impl SchemaVersion {
178176
/// The minimum supported database schema version for the API.
@@ -198,7 +196,6 @@ impl SchemaVersion {
198196
SchemaVersion::Empty => false,
199197
SchemaVersion::InitialFirstHalf => false,
200198
SchemaVersion::IndexBlocksWithNoCumulativeFinTime => false,
201-
SchemaVersion::IndexAccountsWithDelegatedStakeToBakerId => false,
202199
}
203200
}
204201

@@ -222,15 +219,7 @@ impl SchemaVersion {
222219
.await?;
223220
SchemaVersion::IndexBlocksWithNoCumulativeFinTime
224221
}
225-
SchemaVersion::IndexBlocksWithNoCumulativeFinTime => {
226-
tx.as_mut()
227-
.execute(sqlx::raw_sql(include_str!(
228-
"./migrations/m0003-index-accounts-with-delegated-stake-to-baker-id.sql"
229-
)))
230-
.await?;
231-
SchemaVersion::IndexAccountsWithDelegatedStakeToBakerId
232-
}
233-
SchemaVersion::IndexAccountsWithDelegatedStakeToBakerId => unimplemented!(
222+
SchemaVersion::IndexBlocksWithNoCumulativeFinTime => unimplemented!(
234223
"No migration implemented for database schema version {}",
235224
self.as_i64()
236225
),

backend-rust/src/migrations/m0002-block-cumulative-fin-time-index.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
CREATE INDEX blocks_height_null_cumulative_finalization_time ON blocks (height)
44
WHERE blocks.cumulative_finalization_time IS NULL
55
AND blocks.finalization_time IS NOT NULL;
6+
7+
-- Important for quickly calculating the delegated stake to a baker pool.
8+
CREATE INDEX delegated_target_baker_id_index ON accounts(delegated_target_baker_id);

backend-rust/src/migrations/m0003-index-accounts-with-delegated-stake-to-baker-id.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)