Skip to content

Commit 1b4257e

Browse files
committed
Add validator rewards chart service. Dependency updates.
1 parent 12700c7 commit 1b4257e

File tree

14 files changed

+105
-15
lines changed

14 files changed

+105
-15
lines changed

Cargo.lock

+5-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

subvt-network-status-server/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jsonrpsee = { version = "0.16", features = ["full"] }
1717
lazy_static = { workspace = true }
1818
log = { workspace = true }
1919
once_cell = "1"
20-
redis = { version = "0.22.1", features = ["tokio-comp"] }
20+
redis = { version = "0.23", features = ["tokio-comp"] }
2121
serde = { version = "1.0", features = ["derive"] }
2222
serde_json = "1.0"
2323
subvt-config = { path = "../subvt-config" }

subvt-network-status-updater/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ chrono = "0.4"
1111
lazy_static = { workspace = true }
1212
log = { workspace = true }
1313
once_cell = "1"
14-
redis = { version = "0.22.1", features = ["tokio-comp"] }
14+
redis = { version = "0.23", features = ["tokio-comp"] }
1515
serde = { version = "1.0", features = ["derive"] }
1616
serde_json = "1.0"
1717
subvt-config = { path = "../subvt-config" }

subvt-notification-generator/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ futures-util = "0.3"
1414
lazy_static = { workspace = true }
1515
log = { workspace = true }
1616
once_cell = "1"
17-
redis = "0.22"
17+
redis = "0.23"
1818
rustc-hash = "1.1.0"
1919
serde = { version = "1.0", features = ["derive"] }
2020
serde_json = "1.0"

subvt-notification-processor/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ lettre = { version = "0.10", default-features = true, features = ["tokio1-native
2121
lettre_email = "0.9.4"
2222
log = { workspace = true }
2323
once_cell = "1"
24-
redis = { version = "0.22", features = ["tokio-comp"] }
24+
redis = { version = "0.23", features = ["tokio-comp"] }
2525
rustc-hash = "1.1.0"
2626
serde = { version = "1.0", features = ["derive"] }
2727
serde_json = "1.0"

subvt-persistence/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ hex = "0.4"
1111
lazy_static = { workspace = true }
1212
log = { workspace = true }
1313
parity-scale-codec = "3.4"
14-
redis = { version = "0.22", features = ["tokio-comp"] }
14+
redis = { version = "0.23", features = ["tokio-comp"] }
1515
rustc-hash = "1.1.0"
1616
serde = { version = "1.0" }
1717
serde_json = "1.0"

subvt-persistence/src/postgres/network/report/rewards.rs

+37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::postgres::network::PostgreSQLNetworkStorage;
2+
use std::str::FromStr;
23
use subvt_types::crypto::AccountId;
4+
use subvt_types::report::ValidatorTotalReward;
35
use subvt_types::substrate::{Balance, Era};
46

57
impl PostgreSQLNetworkStorage {
@@ -39,4 +41,39 @@ impl PostgreSQLNetworkStorage {
3941
}
4042
Ok(result)
4143
}
44+
45+
pub async fn get_validator_total_rewards(
46+
&self,
47+
start_timestamp: u64,
48+
end_timestamp: u64,
49+
) -> anyhow::Result<Vec<ValidatorTotalReward>> {
50+
let validator_total_rewards: Vec<(String, i64)> = sqlx::query_as(
51+
r#"
52+
SELECT ER.rewardee_account_id as validator_account_id, SUM(ER.amount::bigint) AS total_reward
53+
FROM sub_event_rewarded ER
54+
INNER JOIN sub_block B
55+
ON ER.block_hash = B.hash
56+
AND B.timestamp > $1
57+
AND B.timestamp < $2
58+
WHERE EXISTS (
59+
SELECT * FROM sub_era_validator EV
60+
WHERE EV.validator_account_id = ER.rewardee_account_id
61+
)
62+
GROUP BY ER.rewardee_account_id
63+
ORDER BY total_reward DESC;
64+
"#,
65+
)
66+
.bind(start_timestamp as i64)
67+
.bind(end_timestamp as i64)
68+
.fetch_all(&self.connection_pool)
69+
.await?;
70+
let mut result = vec![];
71+
for validator_total_reward in validator_total_rewards {
72+
result.push(ValidatorTotalReward {
73+
validator_account_id: AccountId::from_str(&validator_total_reward.0)?,
74+
total_reward: validator_total_reward.1 as Balance,
75+
})
76+
}
77+
Ok(result)
78+
}
4279
}

subvt-report-service/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ futures-util = "0.3"
1212
lazy_static = { workspace = true }
1313
log = { workspace = true }
1414
once_cell = "1"
15-
redis = { version = "0.22", features = ["tokio-comp"] }
15+
redis = { version = "0.23", features = ["tokio-comp"] }
1616
serde = { version = "1.0", features = ["derive"] }
1717
serde_json = "1.0"
1818
subvt-config = { path = "../subvt-config" }
1919
subvt-metrics = { path = "../subvt-metrics" }
2020
subvt-persistence = { path = "../subvt-persistence" }
2121
subvt-service-common = { path = "../subvt-service-common" }
22+
subvt-substrate-client = { path = "../subvt-substrate-client" }
2223
subvt-types = { path = "../subvt-types" }
2324
subvt-logging = { path = "../subvt-logging" }
2425
tokio = { version = "1.26", features = ["full"] }

subvt-report-service/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use subvt_config::Config;
1212
use subvt_persistence::postgres::network::PostgreSQLNetworkStorage;
1313
use subvt_persistence::redis::Redis;
1414
use subvt_service_common::{err::InternalServerError, Service};
15+
use subvt_substrate_client::SubstrateClient;
1516
use subvt_types::report::BlockSummary;
1617
use subvt_types::subvt::ValidatorSummary;
1718

@@ -31,6 +32,7 @@ pub(crate) type ResultResponse = Result<HttpResponse, InternalServerError>;
3132
pub(crate) struct ServiceState {
3233
postgres: Arc<PostgreSQLNetworkStorage>,
3334
redis: Arc<Redis>,
35+
substrate_client: Arc<SubstrateClient>,
3436
finalized_block_summary: Arc<RwLock<BlockSummary>>,
3537
active_validator_list: Arc<RwLock<Vec<ValidatorSummary>>>,
3638
inactive_validator_list: Arc<RwLock<Vec<ValidatorSummary>>>,
@@ -65,6 +67,7 @@ impl Service for ReportService {
6567
"Cannot connect to Redis at URL {}.",
6668
CONFIG.redis.url
6769
))?;
70+
let substrate_client = Arc::new(SubstrateClient::new(&CONFIG).await?);
6871
let mut pubsub_connection = redis_client.get_async_connection().await?.into_pubsub();
6972
pubsub_connection
7073
.subscribe(format!(
@@ -168,6 +171,7 @@ impl Service for ReportService {
168171
.app_data(web::Data::new(ServiceState {
169172
postgres: postgres.clone(),
170173
redis: redis.clone(),
174+
substrate_client: substrate_client.clone(),
171175
finalized_block_summary: finalized_block_summary.clone(),
172176
active_validator_list: active_validator_list.clone(),
173177
inactive_validator_list: inactive_validator_list.clone(),
@@ -214,6 +218,7 @@ impl Service for ReportService {
214218
.service(validator::validator_search_service)
215219
.service(validator::validator_era_rewards_service)
216220
.service(validator::validator_era_payouts_service)
221+
.service(validator::validator_reward_chart_service)
217222
})
218223
.workers(10)
219224
.disable_signals()

subvt-report-service/src/validator/mod.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use subvt_types::crypto::AccountId;
66
use subvt_types::err::ServiceError;
77
use subvt_types::report::{
88
BlockSummary, EraValidatorPayoutReport, EraValidatorRewardReport, ValidatorDetailsReport,
9-
ValidatorListReport, ValidatorSummaryReport,
9+
ValidatorListReport, ValidatorSummaryReport, ValidatorTotalRewardChartData,
1010
};
1111
use subvt_types::subvt::{ValidatorSearchSummary, ValidatorSummary};
1212

@@ -241,3 +241,35 @@ pub(crate) async fn validator_era_payouts_service(
241241
.collect();
242242
Ok(HttpResponse::Ok().json(era_payouts))
243243
}
244+
245+
#[derive(Deserialize)]
246+
pub(crate) struct ValidatorRewardChartQueryParameters {
247+
start_timestamp: u64,
248+
end_timestamp: u64,
249+
}
250+
251+
#[get("/validator/reward/chart")]
252+
pub(crate) async fn validator_reward_chart_service(
253+
query: web::Query<ValidatorRewardChartQueryParameters>,
254+
data: web::Data<ServiceState>,
255+
) -> ResultResponse {
256+
let rewards = data
257+
.postgres
258+
.get_validator_total_rewards(query.start_timestamp, query.end_timestamp)
259+
.await?;
260+
let block_hash = data.substrate_client.get_current_block_hash().await?;
261+
let account_ids: Vec<AccountId> = rewards
262+
.iter()
263+
.map(|reward| reward.validator_account_id)
264+
.collect();
265+
let accounts = data
266+
.substrate_client
267+
.get_accounts(&account_ids, &block_hash)
268+
.await?;
269+
Ok(HttpResponse::Ok().json(ValidatorTotalRewardChartData {
270+
accounts,
271+
rewards,
272+
start_timestamp: query.start_timestamp,
273+
end_timestamp: query.end_timestamp,
274+
}))
275+
}

subvt-types/src/report.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Report presentation types. Utilized by the `subvt-report-service` crate to server era and
22
//! validator reports.
33
use crate::crypto::AccountId;
4-
use crate::substrate::{Balance, Epoch, Era};
4+
use crate::substrate::{Account, Balance, Epoch, Era};
55
use crate::subvt::{ValidatorDetails, ValidatorSummary};
66
use serde::{Deserialize, Serialize};
77

@@ -193,3 +193,17 @@ impl From<&(Era, Balance)> for EraValidatorPayoutReport {
193193
}
194194
}
195195
}
196+
197+
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
198+
pub struct ValidatorTotalReward {
199+
pub validator_account_id: AccountId,
200+
pub total_reward: Balance,
201+
}
202+
203+
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
204+
pub struct ValidatorTotalRewardChartData {
205+
pub accounts: Vec<Account>,
206+
pub rewards: Vec<ValidatorTotalReward>,
207+
pub start_timestamp: u64,
208+
pub end_timestamp: u64,
209+
}

subvt-validator-details-server/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jsonrpsee-core = "0.16"
1616
lazy_static = { workspace = true }
1717
log = { workspace = true }
1818
once_cell = "1"
19-
redis = { version = "0.22", features = ["tokio-comp"] }
19+
redis = { version = "0.23", features = ["tokio-comp"] }
2020
serde = { version = "1.0", features = ["derive"] }
2121
serde_json = "1.0"
2222
subvt-config = { path = "../subvt-config" }

subvt-validator-list-server/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jsonrpsee = { version = "0.16", features = ["full"] }
1818
lazy_static = { workspace = true }
1919
log = { workspace = true }
2020
once_cell = "1"
21-
redis = { version = "0.22", features = ["tokio-comp"] }
21+
redis = { version = "0.23", features = ["tokio-comp"] }
2222
rustc-hash = "1.1.0"
2323
serde = { version = "1.0", features = ["derive"] }
2424
serde_json = "1.0"

subvt-validator-list-updater/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ hex = "0.4"
1515
lazy_static = { workspace = true }
1616
log = { workspace = true }
1717
once_cell = "1"
18-
redis = { version = "0.22", features = ["tokio-comp"] }
18+
redis = { version = "0.23", features = ["tokio-comp"] }
1919
rustc-hash = "1.1.0"
2020
serde = { version = "1.0", features = ["derive"] }
2121
serde_json = "1.0"

0 commit comments

Comments
 (0)