Skip to content

Commit d5cf306

Browse files
committed
refactor(database, rpc-server): Update read queries related to states to use i64 instead of BigDecimal
1 parent 5c179c1 commit d5cf306

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

database/src/postgres/rpc_server.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl crate::ReaderDbManager for crate::PostgresDBManager {
8888
",
8989
)
9090
.bind(account_id.to_string())
91-
.bind(bigdecimal::BigDecimal::from(block_height))
91+
.bind(block_height as i64) // Convert to i64 for database compatibility
9292
.bind(page_state.last_data_key.clone())
9393
.bind(page_state.page_size)
9494
.fetch(shard_id_pool.pool);
@@ -141,7 +141,7 @@ impl crate::ReaderDbManager for crate::PostgresDBManager {
141141
)
142142
.bind(account_id.to_string())
143143
.bind(format!("{}%", hex::encode(prefix)))
144-
.bind(bigdecimal::BigDecimal::from(block_height))
144+
.bind(block_height as i64) // Convert to i64 for database compatibility
145145
.fetch(shard_id_pool.pool);
146146
while let Some(row) = stream.next().await {
147147
let (key, value): (String, Vec<u8>) = row?;
@@ -177,7 +177,7 @@ impl crate::ReaderDbManager for crate::PostgresDBManager {
177177
",
178178
)
179179
.bind(account_id.to_string())
180-
.bind(bigdecimal::BigDecimal::from(block_height))
180+
.bind(block_height as i64) // Convert to i64 for database compatibility
181181
.fetch(shard_id_pool.pool);
182182
while let Some(row) = stream.next().await {
183183
let (key, value): (String, Vec<u8>) = row?;
@@ -218,7 +218,7 @@ impl crate::ReaderDbManager for crate::PostgresDBManager {
218218
)
219219
.bind(account_id.to_string())
220220
.bind(hex::encode(&key_data).to_string())
221-
.bind(bigdecimal::BigDecimal::from(block_height))
221+
.bind(block_height as i64) // Convert to i64 for database compatibility
222222
.fetch_one(shard_id_pool.pool)
223223
.await?;
224224
Ok((key_data, data_value))
@@ -244,7 +244,7 @@ impl crate::ReaderDbManager for crate::PostgresDBManager {
244244
"state_changes_account",
245245
])
246246
.inc();
247-
let result: (Vec<u8>, bigdecimal::BigDecimal) = sqlx::query_as(
247+
let result: (Vec<u8>, i64) = sqlx::query_as(
248248
"
249249
SELECT data_value, block_height_from
250250
FROM state_changes_account_compact
@@ -256,7 +256,7 @@ impl crate::ReaderDbManager for crate::PostgresDBManager {
256256
",
257257
)
258258
.bind(account_id.to_string())
259-
.bind(bigdecimal::BigDecimal::from(request_block_height))
259+
.bind(request_block_height as i64) // Convert to i64 for database compatibility
260260
.fetch_one(shard_id_pool.pool)
261261
.await?;
262262
tracing::debug!(
@@ -282,7 +282,7 @@ impl crate::ReaderDbManager for crate::PostgresDBManager {
282282
"state_changes_contract",
283283
])
284284
.inc();
285-
let result: (Vec<u8>, bigdecimal::BigDecimal) = sqlx::query_as(
285+
let result: (Vec<u8>, i64) = sqlx::query_as(
286286
"
287287
SELECT data_value, block_height_from
288288
FROM state_changes_contract_compact
@@ -294,7 +294,7 @@ impl crate::ReaderDbManager for crate::PostgresDBManager {
294294
",
295295
)
296296
.bind(account_id.to_string())
297-
.bind(bigdecimal::BigDecimal::from(request_block_height))
297+
.bind(request_block_height as i64) // Convert to i64 for database compatibility
298298
.fetch_one(shard_id_pool.pool)
299299
.await?;
300300

@@ -317,7 +317,7 @@ impl crate::ReaderDbManager for crate::PostgresDBManager {
317317
])
318318
.inc();
319319
let key_data = borsh::to_vec(&public_key)?;
320-
let result: (Vec<u8>, bigdecimal::BigDecimal) = sqlx::query_as(
320+
let result: (Vec<u8>, i64) = sqlx::query_as(
321321
"
322322
SELECT data_value, block_height_from
323323
FROM state_changes_access_key_compact
@@ -331,7 +331,7 @@ impl crate::ReaderDbManager for crate::PostgresDBManager {
331331
)
332332
.bind(account_id.to_string())
333333
.bind(hex::encode(&key_data).to_string())
334-
.bind(bigdecimal::BigDecimal::from(request_block_height))
334+
.bind(request_block_height as i64) // Convert to i64 for database compatibility
335335
.fetch_one(shard_id_pool.pool)
336336
.await?;
337337
readnode_primitives::QueryData::<near_primitives::account::AccessKey>::try_from(result)
@@ -362,7 +362,7 @@ impl crate::ReaderDbManager for crate::PostgresDBManager {
362362
",
363363
)
364364
.bind(account_id.to_string())
365-
.bind(bigdecimal::BigDecimal::from(block_height))
365+
.bind(block_height as i64) // Convert to i64 for database compatibility
366366
.fetch(shard_id_pool.pool);
367367
while let Some(row) = stream.next().await {
368368
let (public_key_hex, access_key): (String, Vec<u8>) = row?;

database/src/postgres/state_indexer/helpers.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ use std::time::Instant;
44
use futures::future::try_join_all;
55
use sqlx::Row;
66

7+
type StateChangeKeyDataAtBlockHeight = (
8+
String, // account_id
9+
String, // data_key
10+
Vec<u8>, // data_value
11+
i64, // block_height_from
12+
);
13+
714
/// PostgreSQL State Indexer Implementation
815
///
916
/// ARCHITECTURAL OVERVIEW:
@@ -66,7 +73,7 @@ impl crate::PostgresDBManager {
6673

6774
// Compute partition assignments for all account_ids using PostgreSQL's hashtext() function
6875
// This ensures consistent partition distribution matching the table partitioning scheme
69-
let partition_map = self.partition_map(&shard_id, &pool, &account_ids).await?;
76+
let partition_map = self.partition_map(&shard_id, pool, &account_ids).await?;
7077

7178
// Group account_ids by their target partition for batch processing
7279
// This reduces the number of database queries by updating entire partitions at once
@@ -183,7 +190,7 @@ impl crate::PostgresDBManager {
183190
])
184191
.inc();
185192

186-
let partition_map = self.partition_map(&shard_id, &pool, &account_ids).await?;
193+
let partition_map = self.partition_map(&shard_id, pool, &account_ids).await?;
187194

188195
// Group updates per partition
189196
let mut updates_per_partition: HashMap<i32, Vec<(String, String, i64)>> = HashMap::new();
@@ -309,10 +316,10 @@ impl crate::PostgresDBManager {
309316
])
310317
.inc();
311318

312-
let partition_map = self.partition_map(&shard_id, &pool, &account_ids).await?;
319+
let partition_map = self.partition_map(&shard_id, pool, &account_ids).await?;
313320

314321
// Group inserts by partition for efficient batch processing
315-
let mut inserts_per_partition: HashMap<i32, Vec<(String, String, Vec<u8>, i64)>> =
322+
let mut inserts_per_partition: HashMap<i32, Vec<StateChangeKeyDataAtBlockHeight>> =
316323
HashMap::new();
317324
for (account_id, data_key, data_value, block_height) in inserts {
318325
if let Some(&partition) = partition_map.get(&account_id) {
@@ -433,7 +440,7 @@ impl crate::PostgresDBManager {
433440
&account_ids.len().to_string(),
434441
])
435442
.inc();
436-
let partition_map = self.partition_map(&shard_id, &pool, &account_ids).await?;
443+
let partition_map = self.partition_map(&shard_id, pool, &account_ids).await?;
437444

438445
// Group inserts per partition
439446
let mut inserts_per_partition: HashMap<i32, Vec<(String, Vec<u8>, i64)>> = HashMap::new();

database/src/postgres/state_indexer/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use std::collections::HashMap;
21
use std::time::Instant;
32

43
use bigdecimal::ToPrimitive;
5-
use futures::{future::try_join_all, FutureExt};
4+
use futures::FutureExt;
65

76
mod helpers;
87

readnode-primitives/src/lib.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,19 +299,16 @@ where
299299
}
300300
}
301301

302-
impl<T, B> TryFrom<(Vec<u8>, B)> for QueryData<T>
302+
impl<T> TryFrom<(Vec<u8>, i64)> for QueryData<T>
303303
where
304304
T: borsh::BorshDeserialize,
305-
B: ToPrimitive,
306305
{
307306
type Error = anyhow::Error;
308307

309-
fn try_from(value: (Vec<u8>, B)) -> Result<Self, Self::Error> {
308+
fn try_from(value: (Vec<u8>, i64)) -> Result<Self, Self::Error> {
310309
let data = T::try_from_slice(&value.0)?;
311-
let block_height = value
312-
.1
313-
.to_u64()
314-
.ok_or_else(|| anyhow::anyhow!("Failed to parse `block_height` to u64"))?;
310+
let block_height = u64::try_from(value.1)
311+
.map_err(|_| anyhow::anyhow!("Failed to cast `block_height` from i64 to u64"))?;
315312
Ok(Self { data, block_height })
316313
}
317314
}

0 commit comments

Comments
 (0)