Skip to content

Commit a10ead2

Browse files
committed
fix paginated state
1 parent c370570 commit a10ead2

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

database/src/postgres/rpc_server.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,43 @@ impl crate::ReaderDbManager for crate::PostgresDBManager {
7474
} else {
7575
crate::postgres::PageState::new(1000)
7676
};
77-
let mut stream = sqlx::query_as::<_, (String, Vec<u8>)>(
78-
"
77+
let mut stream = if let Some(last_data_key) = &page_state.last_data_key {
78+
sqlx::query_as::<_, (String, Vec<u8>)>(
79+
"
7980
SELECT data_key, data_value
8081
FROM state_changes_data_compact
8182
WHERE account_id = $1
8283
AND block_height_from <= $2
8384
AND (block_height_to IS NULL OR block_height_to > $2)
84-
AND ($3 IS NULL OR data_key > $3)
85+
AND data_key > $3
8586
ORDER BY
8687
data_key ASC
8788
LIMIT $4;
8889
",
89-
)
90-
.bind(account_id.to_string())
91-
.bind(block_height as i64) // Convert to i64 for database compatibility
92-
.bind(page_state.last_data_key.clone())
93-
.bind(page_state.page_size)
94-
.fetch(shard_id_pool.pool);
90+
)
91+
.bind(account_id.to_string())
92+
.bind(block_height as i64) // Convert to i64 for database compatibility
93+
.bind(last_data_key.clone())
94+
.bind(page_state.page_size)
95+
.fetch(shard_id_pool.pool)
96+
} else {
97+
sqlx::query_as::<_, (String, Vec<u8>)>(
98+
"
99+
SELECT data_key, data_value
100+
FROM state_changes_data_compact
101+
WHERE account_id = $1
102+
AND block_height_from <= $2
103+
AND (block_height_to IS NULL OR block_height_to > $2)
104+
ORDER BY
105+
data_key ASC
106+
LIMIT $4;
107+
",
108+
)
109+
.bind(account_id.to_string())
110+
.bind(block_height as i64) // Convert to i64 for database compatibility
111+
.bind(page_state.page_size)
112+
.fetch(shard_id_pool.pool)
113+
};
95114
let mut items = std::collections::HashMap::new();
96115
let mut last_data_key = String::new();
97116
while let Some(row) = stream.next().await {

rpc-server/src/modules/state/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub async fn get_state_from_db_paginated(
1717

1818
let account = data
1919
.db_manager
20-
.get_account(account_id, block.header.height, "query_view_state")
20+
.get_account(account_id, block.header.height, "view_state_paginated")
2121
.await
2222
.map_err(
2323
|_err| near_jsonrpc::primitives::types::query::RpcQueryError::UnknownAccount {
@@ -32,7 +32,7 @@ pub async fn get_state_from_db_paginated(
3232
// more details: nearcore/runtime/runtime/src/state_viewer/mod.rs:150
3333
let code_len = data
3434
.db_manager
35-
.get_contract_code(account_id, block.header.height, "query_view_state")
35+
.get_contract_code(account_id, block.header.height, "view_state_paginated")
3636
.await
3737
.map(|code| code.data.len() as u64)
3838
.unwrap_or_default();

0 commit comments

Comments
 (0)