Skip to content

Commit 3a2168e

Browse files
authored
fix(sv-api): Fixed pagination cursor (#448)
* fix(sv-api): Fixed pagination cursor * fix(sv-api): Fixed unit tests for query
1 parent 2dc30ac commit 3a2168e

File tree

6 files changed

+39
-26
lines changed

6 files changed

+39
-26
lines changed

crates/domains/src/blocks/queryable.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ impl TryFrom<&str> for TimeRange {
112112
pub enum Blocks {
113113
#[iden = "blocks"]
114114
Table,
115+
#[iden = "id"]
116+
Id,
115117
#[iden = "subject"]
116118
Subject,
117119
#[iden = "value"]
@@ -173,7 +175,7 @@ impl Queryable for BlocksQuery {
173175
}
174176

175177
fn pagination_column() -> Self::PaginationColumn {
176-
Blocks::Height
178+
Blocks::Id
177179
}
178180

179181
fn pagination(&self) -> &QueryPagination {
@@ -252,7 +254,7 @@ mod test {
252254
format!("SELECT * FROM \"blocks\" WHERE \"producer_address\" = '{}' AND \"block_height\" = {}", TEST_PRODUCER_ADDRESS, TEST_BLOCK_HEIGHT)
253255
);
254256

255-
// Test 2: all blocks after a given block_height, first items only
257+
// Test 2: all blocks after a given cursor, first items only
256258
let after_height_query = BlocksQuery {
257259
producer: None,
258260
height: None,
@@ -269,7 +271,7 @@ mod test {
269271

270272
assert_eq!(
271273
after_height_query.query_to_string(),
272-
format!("SELECT * FROM \"blocks\" WHERE \"block_height\" > {} ORDER BY \"block_height\" ASC LIMIT {}", TEST_BLOCK_HEIGHT, FIRST_POINTER)
274+
format!("SELECT * FROM \"blocks\" WHERE \"id\" > {} ORDER BY \"id\" ASC LIMIT {}", TEST_BLOCK_HEIGHT, FIRST_POINTER)
273275
);
274276

275277
// Test 3: all blocks after a given timestamp, first items only
@@ -282,7 +284,7 @@ mod test {
282284
};
283285
assert_eq!(
284286
after_timestamp_query.query_to_string(),
285-
format!("SELECT * FROM \"blocks\" WHERE \"created_at\" >= {} ORDER BY \"block_height\" ASC LIMIT {}", TEST_TIMESTAMP, FIRST_POINTER)
287+
format!("SELECT * FROM \"blocks\" WHERE \"created_at\" >= {} ORDER BY \"id\" ASC LIMIT {}", TEST_TIMESTAMP, FIRST_POINTER)
286288
);
287289

288290
// Test 4: all blocks before a given timestamp, last items only
@@ -295,7 +297,7 @@ mod test {
295297
};
296298
assert_eq!(
297299
before_timestamp_query.query_to_string(),
298-
format!("SELECT * FROM \"blocks\" WHERE \"created_at\" >= {} ORDER BY \"block_height\" DESC LIMIT {}", TEST_TIMESTAMP, LAST_POINTER)
300+
format!("SELECT * FROM \"blocks\" WHERE \"created_at\" >= {} ORDER BY \"id\" DESC LIMIT {}", TEST_TIMESTAMP, LAST_POINTER)
299301
);
300302

301303
// Test 5: all blocks in the last 90 days

crates/domains/src/inputs/queryable.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use crate::queryable::{HasPagination, QueryPagination, Queryable};
1111
pub enum Inputs {
1212
#[iden = "inputs"]
1313
Table,
14+
#[iden = "id"]
15+
Id,
1416
#[iden = "subject"]
1517
Subject,
1618
#[iden = "value"]
@@ -94,7 +96,7 @@ impl Queryable for InputsQuery {
9496
}
9597

9698
fn pagination_column() -> Self::PaginationColumn {
97-
Inputs::BlockHeight
99+
Inputs::Id
98100
}
99101

100102
fn pagination(&self) -> &QueryPagination {
@@ -284,7 +286,7 @@ mod test {
284286

285287
assert_eq!(
286288
coin_query.query_to_string(),
287-
format!("SELECT * FROM \"inputs\" WHERE \"input_type\" = 'coin' AND \"owner_id\" = '{}' AND \"asset_id\" = '{}' ORDER BY \"block_height\" ASC LIMIT {}",
289+
format!("SELECT * FROM \"inputs\" WHERE \"input_type\" = 'coin' AND \"owner_id\" = '{}' AND \"asset_id\" = '{}' ORDER BY \"id\" ASC LIMIT {}",
288290
TEST_OWNER_ID, TEST_ASSET_ID, FIRST_POINTER)
289291
);
290292

@@ -307,7 +309,7 @@ mod test {
307309

308310
assert_eq!(
309311
contract_query.query_to_string(),
310-
format!("SELECT * FROM \"inputs\" WHERE \"input_type\" = 'contract' AND \"contract_id\" = '{}' AND \"block_height\" > {} ORDER BY \"block_height\" DESC LIMIT {}",
312+
format!("SELECT * FROM \"inputs\" WHERE \"input_type\" = 'contract' AND \"contract_id\" = '{}' AND \"id\" > {} ORDER BY \"id\" DESC LIMIT {}",
311313
TEST_CONTRACT_ID, AFTER_POINTER, LAST_POINTER)
312314
);
313315

@@ -330,7 +332,7 @@ mod test {
330332

331333
assert_eq!(
332334
message_query.query_to_string(),
333-
format!("SELECT * FROM \"inputs\" WHERE \"input_type\" = 'message' AND \"sender_address\" = '{}' AND \"recipient_address\" = '{}' AND \"block_height\" < {} ORDER BY \"block_height\" ASC LIMIT {}",
335+
format!("SELECT * FROM \"inputs\" WHERE \"input_type\" = 'message' AND \"sender_address\" = '{}' AND \"recipient_address\" = '{}' AND \"id\" < {} ORDER BY \"id\" ASC LIMIT {}",
334336
TEST_ADDRESS, TEST_ADDRESS, BEFORE_POINTER, FIRST_POINTER)
335337
);
336338

crates/domains/src/outputs/queryable.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use crate::queryable::{HasPagination, QueryPagination, Queryable};
1111
pub enum Outputs {
1212
#[iden = "outputs"]
1313
Table,
14+
#[iden = "id"]
15+
Id,
1416
#[iden = "subject"]
1517
Subject,
1618
#[iden = "value"]
@@ -88,7 +90,7 @@ impl Queryable for OutputsQuery {
8890
}
8991

9092
fn pagination_column() -> Self::PaginationColumn {
91-
Outputs::BlockHeight
93+
Outputs::Id
9294
}
9395

9496
fn pagination(&self) -> &QueryPagination {
@@ -252,7 +254,7 @@ mod test {
252254

253255
assert_eq!(
254256
coin_query.query_to_string(),
255-
format!("SELECT * FROM \"outputs\" WHERE \"output_type\" = 'coin' AND \"to_address\" = '{}' AND \"asset_id\" = '{}' ORDER BY \"block_height\" ASC LIMIT {}",
257+
format!("SELECT * FROM \"outputs\" WHERE \"output_type\" = 'coin' AND \"to_address\" = '{}' AND \"asset_id\" = '{}' ORDER BY \"id\" ASC LIMIT {}",
256258
TEST_ADDRESS, TEST_ASSET_ID, FIRST_POINTER)
257259
);
258260

@@ -273,7 +275,7 @@ mod test {
273275

274276
assert_eq!(
275277
contract_query.query_to_string(),
276-
format!("SELECT * FROM \"outputs\" WHERE \"output_type\" = 'contract' AND \"contract_id\" = '{}' AND \"block_height\" > {} ORDER BY \"block_height\" DESC LIMIT {}",
278+
format!("SELECT * FROM \"outputs\" WHERE \"output_type\" = 'contract' AND \"contract_id\" = '{}' AND \"id\" > {} ORDER BY \"id\" DESC LIMIT {}",
277279
TEST_CONTRACT_ID, AFTER_POINTER, LAST_POINTER)
278280
);
279281

@@ -294,7 +296,7 @@ mod test {
294296

295297
assert_eq!(
296298
change_query.query_to_string(),
297-
format!("SELECT * FROM \"outputs\" WHERE \"output_type\" = 'change' AND \"to_address\" = '{}' AND \"asset_id\" = '{}' AND \"block_height\" < {} ORDER BY \"block_height\" ASC LIMIT {}",
299+
format!("SELECT * FROM \"outputs\" WHERE \"output_type\" = 'change' AND \"to_address\" = '{}' AND \"asset_id\" = '{}' AND \"id\" < {} ORDER BY \"id\" ASC LIMIT {}",
298300
TEST_ADDRESS, TEST_ASSET_ID, BEFORE_POINTER, FIRST_POINTER)
299301
);
300302

crates/domains/src/receipts/queryable.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use crate::queryable::{HasPagination, QueryPagination, Queryable};
1111
pub enum Receipts {
1212
#[iden = "receipts"]
1313
Table,
14+
#[iden = "id"]
15+
Id,
1416
#[iden = "subject"]
1517
Subject,
1618
#[iden = "value"]
@@ -99,7 +101,7 @@ impl Queryable for ReceiptsQuery {
99101
}
100102

101103
fn pagination_column() -> Self::PaginationColumn {
102-
Receipts::BlockHeight
104+
Receipts::Id
103105
}
104106

105107
fn pagination(&self) -> &QueryPagination {
@@ -320,7 +322,7 @@ mod test {
320322

321323
assert_eq!(
322324
contract_query.query_to_string(),
323-
format!("SELECT * FROM \"receipts\" WHERE \"from_contract_id\" = '{}' AND \"to_contract_id\" = '{}' ORDER BY \"block_height\" ASC LIMIT {}",
325+
format!("SELECT * FROM \"receipts\" WHERE \"from_contract_id\" = '{}' AND \"to_contract_id\" = '{}' ORDER BY \"id\" ASC LIMIT {}",
324326
TEST_CONTRACT_ID, TEST_CONTRACT_ID, FIRST_POINTER)
325327
);
326328

@@ -345,7 +347,7 @@ mod test {
345347

346348
assert_eq!(
347349
asset_query.query_to_string(),
348-
format!("SELECT * FROM \"receipts\" WHERE \"asset_id\" = '{}' AND \"block_height\" > {} ORDER BY \"block_height\" DESC LIMIT {}",
350+
format!("SELECT * FROM \"receipts\" WHERE \"asset_id\" = '{}' AND \"id\" > {} ORDER BY \"id\" DESC LIMIT {}",
349351
TEST_ASSET_ID, AFTER_POINTER, LAST_POINTER)
350352
);
351353

@@ -370,7 +372,7 @@ mod test {
370372

371373
assert_eq!(
372374
address_query.query_to_string(),
373-
format!("SELECT * FROM \"receipts\" WHERE \"sender_address\" = '{}' AND \"recipient_address\" = '{}' AND \"block_height\" < {} ORDER BY \"block_height\" ASC LIMIT {}",
375+
format!("SELECT * FROM \"receipts\" WHERE \"sender_address\" = '{}' AND \"recipient_address\" = '{}' AND \"id\" < {} ORDER BY \"id\" ASC LIMIT {}",
374376
TEST_ADDRESS, TEST_ADDRESS, BEFORE_POINTER, FIRST_POINTER)
375377
);
376378

crates/domains/src/transactions/queryable.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use crate::queryable::{HasPagination, QueryPagination, Queryable};
1111
pub enum Transactions {
1212
#[iden = "transactions"]
1313
Table,
14+
#[iden = "id"]
15+
Id,
1416
#[iden = "subject"]
1517
Subject,
1618
#[iden = "value"]
@@ -73,7 +75,7 @@ impl Queryable for TransactionsQuery {
7375
}
7476

7577
fn pagination_column() -> Self::PaginationColumn {
76-
Transactions::BlockHeight
78+
Transactions::Id
7779
}
7880

7981
fn pagination(&self) -> &QueryPagination {
@@ -178,7 +180,7 @@ mod test {
178180

179181
assert_eq!(
180182
type_query.query_to_string(),
181-
format!("SELECT * FROM \"transactions\" WHERE \"tx_index\" = {} AND \"type\" = 'script' ORDER BY \"block_height\" ASC LIMIT {}",
183+
format!("SELECT * FROM \"transactions\" WHERE \"tx_index\" = {} AND \"type\" = 'script' ORDER BY \"id\" ASC LIMIT {}",
182184
TEST_TX_INDEX, FIRST_POINTER)
183185
);
184186

@@ -197,7 +199,7 @@ mod test {
197199

198200
assert_eq!(
199201
range_query.query_to_string(),
200-
format!("SELECT * FROM \"transactions\" WHERE \"block_height\" = {} AND \"block_height\" > {} ORDER BY \"block_height\" DESC LIMIT {}",
202+
format!("SELECT * FROM \"transactions\" WHERE \"block_height\" = {} AND \"id\" > {} ORDER BY \"id\" DESC LIMIT {}",
201203
TEST_BLOCK_HEIGHT, AFTER_POINTER, LAST_POINTER)
202204
);
203205

@@ -216,7 +218,7 @@ mod test {
216218

217219
assert_eq!(
218220
status_type_query.query_to_string(),
219-
format!("SELECT * FROM \"transactions\" WHERE \"type\" = 'create' AND \"tx_status\" = 'failed' AND \"block_height\" < {} ORDER BY \"block_height\" ASC LIMIT {}",
221+
format!("SELECT * FROM \"transactions\" WHERE \"type\" = 'create' AND \"tx_status\" = 'failed' AND \"id\" < {} ORDER BY \"id\" ASC LIMIT {}",
220222
BEFORE_POINTER, FIRST_POINTER)
221223
);
222224

crates/domains/src/utxos/queryable.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use crate::{
1414
pub enum Utxos {
1515
#[iden = "utxos"]
1616
Table,
17+
#[iden = "id"]
18+
Id,
1719
#[iden = "subject"]
1820
Subject,
1921
#[iden = "value"]
@@ -88,7 +90,7 @@ impl Queryable for UtxosQuery {
8890
}
8991

9092
fn pagination_column() -> Self::PaginationColumn {
91-
Utxos::BlockHeight
93+
Utxos::Id
9294
}
9395

9496
fn pagination(&self) -> &QueryPagination {
@@ -217,6 +219,7 @@ mod test {
217219
block_height: None,
218220
utxo_type: None,
219221
tx_index: None,
222+
220223
input_index: None,
221224
utxo_id: Some(HexData::from(TEST_UTXO_ID)),
222225
pagination: (None, None, Some(FIRST_POINTER), None).into(),
@@ -226,7 +229,7 @@ mod test {
226229

227230
assert_eq!(
228231
utxo_id_query.query_to_string(),
229-
format!("SELECT * FROM \"utxos\" WHERE \"utxo_id\" = '{}' ORDER BY \"block_height\" ASC LIMIT {}",
232+
format!("SELECT * FROM \"utxos\" WHERE \"utxo_id\" = '{}' ORDER BY \"id\" ASC LIMIT {}",
230233
TEST_UTXO_ID, FIRST_POINTER)
231234
);
232235

@@ -246,7 +249,7 @@ mod test {
246249

247250
assert_eq!(
248251
indices_query.query_to_string(),
249-
format!("SELECT * FROM \"utxos\" WHERE \"tx_id\" = '{}' AND \"tx_index\" = {} AND \"input_index\" = {} AND \"block_height\" > {} ORDER BY \"block_height\" DESC LIMIT {}",
252+
format!("SELECT * FROM \"utxos\" WHERE \"tx_id\" = '{}' AND \"tx_index\" = {} AND \"input_index\" = {} AND \"id\" > {} ORDER BY \"id\" DESC LIMIT {}",
250253
TEST_TX_ID, TEST_TX_INDEX, TEST_INPUT_INDEX, AFTER_POINTER, LAST_POINTER)
251254
);
252255

@@ -266,7 +269,7 @@ mod test {
266269

267270
assert_eq!(
268271
type_query.query_to_string(),
269-
format!("SELECT * FROM \"utxos\" WHERE \"utxo_type\" = 'message' AND \"block_height\" < {} ORDER BY \"block_height\" ASC LIMIT {}",
272+
format!("SELECT * FROM \"utxos\" WHERE \"utxo_type\" = 'message' AND \"id\" < {} ORDER BY \"id\" ASC LIMIT {}",
270273
BEFORE_POINTER, FIRST_POINTER)
271274
);
272275

@@ -291,7 +294,7 @@ mod test {
291294

292295
assert_eq!(
293296
complex_query.query_to_string(),
294-
format!("SELECT * FROM \"utxos\" WHERE \"block_height\" = {} AND \"tx_id\" = '{}' AND \"tx_index\" = {} AND \"input_index\" = {} AND \"utxo_type\" = 'contract' AND \"utxo_id\" = '{}' AND \"block_height\" > {} AND \"block_height\" < {} ORDER BY \"block_height\" ASC LIMIT {}",
297+
format!("SELECT * FROM \"utxos\" WHERE \"block_height\" = {} AND \"tx_id\" = '{}' AND \"tx_index\" = {} AND \"input_index\" = {} AND \"utxo_type\" = 'contract' AND \"utxo_id\" = '{}' AND \"id\" > {} AND \"id\" < {} ORDER BY \"id\" ASC LIMIT {}",
295298
TEST_BLOCK_HEIGHT, TEST_TX_ID, TEST_TX_INDEX, TEST_INPUT_INDEX, TEST_UTXO_ID, AFTER_POINTER, BEFORE_POINTER, FIRST_POINTER)
296299
);
297300
}

0 commit comments

Comments
 (0)