Skip to content

Commit 9d66715

Browse files
committed
remove block_hash from queries
1 parent 2894655 commit 9d66715

File tree

2 files changed

+31
-61
lines changed

2 files changed

+31
-61
lines changed

database/src/postgres/rpc_server.rs

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -310,26 +310,20 @@ impl crate::ReaderDbManager for crate::PostgresDBManager {
310310
let data_range_id = shard_id_pool.data_range_id;
311311
let sql_query = format!(
312312
"
313-
SELECT block_height, block_hash, data_value
313+
SELECT data_value, block_height
314314
FROM state_changes_account_{data_range_id}
315315
WHERE account_id = $1
316316
AND block_height <= $2
317317
ORDER BY block_height DESC
318318
LIMIT 1;
319319
"
320320
);
321-
let (block_height, block_hash, data_value): (bigdecimal::BigDecimal, String, Vec<u8>) =
322-
sqlx::query_as(&sql_query)
323-
.bind(account_id.to_string())
324-
.bind(bigdecimal::BigDecimal::from(request_block_height))
325-
.fetch_one(shard_id_pool.pool)
326-
.await?;
327-
let block = readnode_primitives::BlockRecord::try_from((block_hash, block_height))?;
328-
readnode_primitives::QueryData::<near_primitives::account::Account>::try_from((
329-
data_value,
330-
block.height,
331-
block.hash,
332-
))
321+
let result: (Vec<u8>, bigdecimal::BigDecimal) = sqlx::query_as(&sql_query)
322+
.bind(account_id.to_string())
323+
.bind(bigdecimal::BigDecimal::from(request_block_height))
324+
.fetch_one(shard_id_pool.pool)
325+
.await?;
326+
readnode_primitives::QueryData::<near_primitives::account::Account>::try_from(result)
333327
}
334328

335329
async fn get_contract_code(
@@ -351,26 +345,20 @@ impl crate::ReaderDbManager for crate::PostgresDBManager {
351345
let data_range_id = shard_id_pool.data_range_id;
352346
let sql_query = format!(
353347
"
354-
SELECT block_height, block_hash, data_value
348+
SELECT data_value, block_height
355349
FROM state_changes_contract_{data_range_id}
356350
WHERE account_id = $1
357351
AND block_height <= $2
358352
ORDER BY block_height DESC
359353
LIMIT 1;
360354
"
361355
);
362-
let (block_height, block_hash, contract_code): (bigdecimal::BigDecimal, String, Vec<u8>) =
363-
sqlx::query_as(&sql_query)
364-
.bind(account_id.to_string())
365-
.bind(bigdecimal::BigDecimal::from(request_block_height))
366-
.fetch_one(shard_id_pool.pool)
367-
.await?;
368-
let block = readnode_primitives::BlockRecord::try_from((block_hash, block_height))?;
369-
Ok(readnode_primitives::QueryData {
370-
data: contract_code,
371-
block_height: block.height,
372-
block_hash: block.hash,
373-
})
356+
let result: (Vec<u8>, bigdecimal::BigDecimal) = sqlx::query_as(&sql_query)
357+
.bind(account_id.to_string())
358+
.bind(bigdecimal::BigDecimal::from(request_block_height))
359+
.fetch_one(shard_id_pool.pool)
360+
.await?;
361+
readnode_primitives::QueryData::<Vec<u8>>::try_from(result)
374362
}
375363

376364
async fn get_access_key(
@@ -394,7 +382,7 @@ impl crate::ReaderDbManager for crate::PostgresDBManager {
394382
let data_range_id = shard_id_pool.data_range_id;
395383
let sql_query = format!(
396384
"
397-
SELECT block_height, block_hash, data_value
385+
SELECT data_value, block_height
398386
FROM state_changes_access_key_{data_range_id}
399387
WHERE account_id = $1
400388
AND data_key = $2
@@ -403,19 +391,13 @@ impl crate::ReaderDbManager for crate::PostgresDBManager {
403391
LIMIT 1;
404392
"
405393
);
406-
let (block_height, block_hash, data_value): (bigdecimal::BigDecimal, String, Vec<u8>) =
407-
sqlx::query_as(&sql_query)
408-
.bind(account_id.to_string())
409-
.bind(hex::encode(&key_data).to_string())
410-
.bind(bigdecimal::BigDecimal::from(request_block_height))
411-
.fetch_one(shard_id_pool.pool)
412-
.await?;
413-
let block = readnode_primitives::BlockRecord::try_from((block_hash, block_height))?;
414-
readnode_primitives::QueryData::<near_primitives::account::AccessKey>::try_from((
415-
data_value,
416-
block.height,
417-
block.hash,
418-
))
394+
let result: (Vec<u8>, bigdecimal::BigDecimal) = sqlx::query_as(&sql_query)
395+
.bind(account_id.to_string())
396+
.bind(hex::encode(&key_data).to_string())
397+
.bind(bigdecimal::BigDecimal::from(request_block_height))
398+
.fetch_one(shard_id_pool.pool)
399+
.await?;
400+
readnode_primitives::QueryData::<near_primitives::account::AccessKey>::try_from(result)
419401
}
420402

421403
async fn get_account_access_keys(

readnode-primitives/src/lib.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,11 @@ pub type StateValue = Vec<u8>;
220220
pub struct BlockHeightShardId(pub u64, pub u64);
221221
pub struct QueryData<T: borsh::BorshDeserialize> {
222222
pub data: T,
223-
// block_height and block_hash we return here represents the moment
223+
// block_height we return here represents the moment
224224
// when the data was last updated in the database
225225
// We used to return it in the `QueryResponse` but it was replaced with
226226
// the logic that corresponds the logic of the `nearcore` RPC API
227227
pub block_height: near_indexer_primitives::types::BlockHeight,
228-
pub block_hash: CryptoHash,
229228
}
230229

231230
#[derive(Debug, Clone)]
@@ -300,31 +299,20 @@ where
300299
}
301300
}
302301

303-
impl<T>
304-
TryFrom<(
305-
Vec<u8>,
306-
near_indexer_primitives::types::BlockHeight,
307-
CryptoHash,
308-
)> for QueryData<T>
302+
impl<T, B> TryFrom<(Vec<u8>, B)> for QueryData<T>
309303
where
310304
T: borsh::BorshDeserialize,
305+
B: ToPrimitive,
311306
{
312307
type Error = anyhow::Error;
313308

314-
fn try_from(
315-
value: (
316-
Vec<u8>,
317-
near_indexer_primitives::types::BlockHeight,
318-
CryptoHash,
319-
),
320-
) -> Result<Self, Self::Error> {
309+
fn try_from(value: (Vec<u8>, B)) -> Result<Self, Self::Error> {
321310
let data = T::try_from_slice(&value.0)?;
322-
323-
Ok(Self {
324-
data,
325-
block_height: value.1,
326-
block_hash: value.2,
327-
})
311+
let block_height = value
312+
.1
313+
.to_u64()
314+
.ok_or_else(|| anyhow::anyhow!("Failed to parse `block_height` to u64"))?;
315+
Ok(Self { data, block_height })
328316
}
329317
}
330318

0 commit comments

Comments
 (0)