Skip to content

Commit b916d76

Browse files
committed
cleanup and more tests
1 parent 6b3f09c commit b916d76

11 files changed

Lines changed: 786 additions & 781 deletions

File tree

api-server/api-server-common/src/storage/impls/in_memory/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1448,9 +1448,9 @@ impl ApiServerInMemoryStorage {
14481448
&mut self,
14491449
outpoint: UtxoOutPoint,
14501450
utxo: Utxo,
1451-
spent: bool,
14521451
addresses: &[&str],
14531452
) -> Result<(), ApiServerStorageError> {
1453+
let spent = utxo.spent();
14541454
self.mempool_utxo_table.insert(outpoint.clone(), (utxo, spent));
14551455
for address in addresses {
14561456
self.mempool_address_utxos
@@ -1582,6 +1582,7 @@ impl ApiServerInMemoryStorage {
15821582
self.mempool_token_transactions_table.clear();
15831583
self.mempool_fungible_token_data.clear();
15841584
self.mempool_nft_token_issuances.clear();
1585+
self.mempool_orders_table.clear();
15851586
Ok(())
15861587
}
15871588
}

api-server/api-server-common/src/storage/impls/in_memory/transactional/write.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,9 @@ impl ApiServerStorageWrite for ApiServerInMemoryStorageTransactionalRw<'_> {
324324
&mut self,
325325
outpoint: UtxoOutPoint,
326326
utxo: Utxo,
327-
spent: bool,
328327
addresses: &[&str],
329328
) -> Result<(), ApiServerStorageError> {
330-
self.transaction.set_mempool_utxo(outpoint, utxo, spent, addresses)
329+
self.transaction.set_mempool_utxo(outpoint, utxo, addresses)
331330
}
332331

333332
async fn set_mempool_address_balance(

api-server/api-server-common/src/storage/impls/postgres/queries.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3233,9 +3233,9 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
32333233
&mut self,
32343234
outpoint: UtxoOutPoint,
32353235
utxo: Utxo,
3236-
spent: bool,
32373236
addresses: &[&str],
32383237
) -> Result<(), ApiServerStorageError> {
3238+
let spent = utxo.spent();
32393239
self.tx
32403240
.execute(
32413241
"INSERT INTO ml.mempool_utxo (outpoint, utxo, spent)
@@ -3488,7 +3488,6 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
34883488
SELECT utxo, spent
34893489
FROM ml.mempool_utxo
34903490
WHERE outpoint = ua.outpoint
3491-
ORDER BY block_height DESC
34923491
LIMIT 1
34933492
) u
34943493
WHERE ua.address = $1 AND u.spent = false;"#,
@@ -3581,7 +3580,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
35813580
let coin_or_token_id = CoinOrTokenId::TokenId(token_id);
35823581
self.tx
35833582
.execute(
3584-
"INSERT INTO ml.mempool_coin_or_token_decimals (coin_or_token_id, number_of_decimals) VALUES ($1, $2, $3);",
3583+
"INSERT INTO ml.mempool_coin_or_token_decimals (coin_or_token_id, number_of_decimals) VALUES ($1, $2);",
35853584
&[&coin_or_token_id.encode(), &(issuance.number_of_decimals as i16)],
35863585
)
35873586
.await
@@ -3617,7 +3616,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
36173616
let coin_or_token_id = CoinOrTokenId::TokenId(token_id);
36183617
self.tx
36193618
.execute(
3620-
"INSERT INTO ml.mempool_coin_or_token_decimals (coin_or_token_id, number_of_decimals) VALUES ($1, $2, $3);",
3619+
"INSERT INTO ml.mempool_coin_or_token_decimals (coin_or_token_id, number_of_decimals) VALUES ($1, $2);",
36213620
&[&coin_or_token_id.encode(), &0_i16],
36223621
)
36233622
.await
@@ -3629,11 +3628,12 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
36293628
&self,
36303629
token_id: TokenId,
36313630
) -> Result<Option<u8>, ApiServerStorageError> {
3631+
let coin_or_token_id = CoinOrTokenId::TokenId(token_id);
36323632
let res = self
36333633
.tx
36343634
.query_opt(
36353635
"SELECT number_of_decimals FROM ml.mempool_coin_or_token_decimals WHERE coin_or_token_id = $1",
3636-
&[&token_id.encode()],
3636+
&[&coin_or_token_id.encode()],
36373637
)
36383638
.await
36393639
.map_err(|e| ApiServerStorageError::LowLevelStorageError(e.to_string()))?;
@@ -3685,9 +3685,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
36853685
chain_config: &ChainConfig,
36863686
) -> Result<(), ApiServerStorageError> {
36873687
let order_id_addr = Address::new(chain_config, order_id)
3688-
.map_err(|_| ApiServerStorageError::AddressableError)?
3689-
.as_str()
3690-
.to_string();
3688+
.map_err(|_| ApiServerStorageError::AddressableError)?;
36913689

36923690
self.tx
36933691
.execute(
@@ -3697,12 +3695,12 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
36973695
ON CONFLICT (order_id) DO UPDATE SET initially_asked = EXCLUDED.initially_asked, ask_balance = EXCLUDED.ask_balance, ask_currency = EXCLUDED.ask_currency, initially_given = EXCLUDED.initially_given, give_balance = EXCLUDED.give_balance, give_currency = EXCLUDED.give_currency, conclude_destination = EXCLUDED.conclude_destination, next_nonce = EXCLUDED.next_nonce, frozen = EXCLUDED.frozen;
36983696
"#,
36993697
&[
3700-
&order_id_addr,
3701-
&order.initially_asked.encode(),
3702-
&order.ask_balance.encode(),
3698+
&order_id_addr.as_str(),
3699+
&amount_to_str(order.initially_asked),
3700+
&amount_to_str(order.ask_balance),
37033701
&order.ask_currency.encode(),
3704-
&order.initially_given.encode(),
3705-
&order.give_balance.encode(),
3702+
&amount_to_str(order.initially_given),
3703+
&amount_to_str(order.give_balance),
37063704
&order.give_currency.encode(),
37073705
&order.conclude_destination.encode(),
37083706
&order.next_nonce.encode(),
@@ -3729,6 +3727,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
37293727
ml.mempool_token_transactions,
37303728
ml.mempool_fungible_token,
37313729
ml.mempool_nft_issuance,
3730+
ml.mempool_orders,
37323731
ml.mempool_coin_or_token_decimals
37333732
RESTART IDENTITY CASCADE;",
37343733
)
@@ -3754,7 +3753,7 @@ fn decode_order_from_row(
37543753
let give_currency: Vec<u8> = data.get("give_currency");
37553754
let conclude_destination: Vec<u8> = data.get("conclude_destination");
37563755
let next_nonce: Vec<u8> = data.get("next_nonce");
3757-
let creation_block_height: Option<i64> = data.get("creation_block_height");
3756+
let creation_block_height: Option<i64> = data.try_get("creation_block_height").ok().flatten();
37583757
let is_frozen: bool = data.get("frozen");
37593758

37603759
let order_id = Address::<OrderId>::from_string(chain_config, order_id)

api-server/api-server-common/src/storage/impls/postgres/transactional/write.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,10 @@ impl ApiServerStorageWrite for ApiServerPostgresTransactionalRw<'_> {
419419
&mut self,
420420
outpoint: UtxoOutPoint,
421421
utxo: Utxo,
422-
spent: bool,
423422
addresses: &[&str],
424423
) -> Result<(), ApiServerStorageError> {
425424
let mut conn = QueryFromConnection::new(self.connection.as_ref().expect(CONN_ERR));
426-
conn.set_mempool_utxo(outpoint, utxo, spent, addresses).await?;
425+
conn.set_mempool_utxo(outpoint, utxo, addresses).await?;
427426

428427
Ok(())
429428
}

api-server/api-server-common/src/storage/storage_api/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,13 +418,13 @@ impl LockedUtxo {
418418
}
419419
}
420420

421-
#[derive(Debug, Clone, Copy, Encode, Decode)]
421+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
422422
pub enum UtxoSpent {
423423
AtBlockHeight(BlockHeight),
424424
InMempool,
425425
}
426426

427-
#[derive(Debug, Clone, Encode, Decode)]
427+
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
428428
pub struct Utxo {
429429
utxo: UtxoWithExtraInfo,
430430
spent: Option<UtxoSpent>,
@@ -1068,7 +1068,6 @@ pub trait ApiServerStorageWrite: ApiServerStorageRead {
10681068
&mut self,
10691069
outpoint: UtxoOutPoint,
10701070
utxo: Utxo,
1071-
spent: bool,
10721071
addresses: &[&str],
10731072
) -> Result<(), ApiServerStorageError>;
10741073

api-server/scanner-lib/src/blockchain_state/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ impl<S: ApiServerStorage + Send + Sync> LocalBlockchainState for BlockchainState
295295

296296
// Mark UTXO as spent in mempool
297297
db_tx
298-
.set_mempool_utxo(outpoint.clone(), utxo.clone(), true, &[])
298+
.set_mempool_utxo(outpoint.clone(), utxo.clone(), &[])
299299
.await
300300
.map_err(BlockchainStateError::StorageError)?;
301301

@@ -678,7 +678,7 @@ impl<S: ApiServerStorage + Send + Sync> LocalBlockchainState for BlockchainState
678678
let addresses_refs: Vec<&str> = addresses.iter().map(|s| s.as_str()).collect();
679679

680680
db_tx
681-
.set_mempool_utxo(outpoint, utxo, false, &addresses_refs)
681+
.set_mempool_utxo(outpoint, utxo, &addresses_refs)
682682
.await
683683
.map_err(BlockchainStateError::StorageError)?;
684684
}

0 commit comments

Comments
 (0)