Skip to content

Commit

Permalink
Merge pull request #1691 from mintlayer/fix/scanner-utxo-unlocking-time
Browse files Browse the repository at this point in the history
fix scanner time of utxo unlocking
  • Loading branch information
TheQuantumPhysicist authored Mar 23, 2024
2 parents 3f5a870 + a51c45a commit 7dc48b1
Show file tree
Hide file tree
Showing 26 changed files with 166 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ use super::ApiServerInMemoryStorageTransactionalRw;

#[async_trait::async_trait]
impl<'t> ApiServerStorageWrite for ApiServerInMemoryStorageTransactionalRw<'t> {
async fn initialize_storage(
&mut self,
chain_config: &ChainConfig,
) -> Result<(), ApiServerStorageError> {
self.transaction.initialize_storage(chain_config)
}

async fn reinitialize_storage(
&mut self,
chain_config: &ChainConfig,
Expand Down
2 changes: 1 addition & 1 deletion api-server/api-server-common/src/storage/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub const CURRENT_STORAGE_VERSION: u32 = 7;
pub const CURRENT_STORAGE_VERSION: u32 = 8;

pub mod in_memory;
pub mod postgres;
200 changes: 105 additions & 95 deletions api-server/api-server-common/src/storage/impls/postgres/queries.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ use super::{ApiServerPostgresTransactionalRw, CONN_ERR};

#[async_trait::async_trait]
impl<'a> ApiServerStorageWrite for ApiServerPostgresTransactionalRw<'a> {
async fn initialize_storage(
&mut self,
chain_config: &ChainConfig,
) -> Result<(), ApiServerStorageError> {
let mut conn = QueryFromConnection::new(self.connection.as_ref().expect(CONN_ERR));
conn.initialize_database(chain_config).await?;

Ok(())
}

async fn reinitialize_storage(
&mut self,
chain_config: &ChainConfig,
Expand Down
5 changes: 0 additions & 5 deletions api-server/api-server-common/src/storage/storage_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,6 @@ pub trait ApiServerStorageRead: Sync {

#[async_trait::async_trait]
pub trait ApiServerStorageWrite: ApiServerStorageRead {
async fn initialize_storage(
&mut self,
chain_config: &ChainConfig,
) -> Result<(), ApiServerStorageError>;

async fn reinitialize_storage(
&mut self,
chain_config: &ChainConfig,
Expand Down
24 changes: 17 additions & 7 deletions api-server/scanner-daemon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub async fn run<S: ApiServerStorage>(
.unwrap_or_else(|e| panic!("Storage initialization checking failed {}", e))
{
db_tx
.initialize_storage(chain_config)
.reinitialize_storage(chain_config)
.await
.unwrap_or_else(|e| panic!("Storage initialization failed {}", e));

Expand All @@ -98,13 +98,23 @@ pub async fn run<S: ApiServerStorage>(
.reinitialize_storage(chain_config)
.await
.unwrap_or_else(|e| panic!("Storage re-initialization failed {}", e));
db_tx
.commit()
.await
.unwrap_or_else(|e| panic!("Storage initialization commit failed {}", e));
let mut local_block = BlockchainState::new(Arc::clone(chain_config), storage);
local_block
.scan_genesis(chain_config.genesis_block().as_ref())
.await
.expect("Can't scan genesis");
local_block
} else {
db_tx
.commit()
.await
.unwrap_or_else(|e| panic!("Storage initialization commit failed {}", e));
BlockchainState::new(Arc::clone(chain_config), storage)
}

db_tx
.commit()
.await
.unwrap_or_else(|e| panic!("Storage initialization commit failed {}", e));
BlockchainState::new(Arc::clone(chain_config), storage)
}
};

Expand Down
4 changes: 2 additions & 2 deletions api-server/scanner-lib/src/sync/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ async fn compare_pool_rewards_with_chainstate_real_state(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down Expand Up @@ -616,7 +616,7 @@ async fn reorg_locked_balance(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
6 changes: 3 additions & 3 deletions api-server/stack-test-suite/tests/v2/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ async fn multiple_outputs_to_single_address(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down Expand Up @@ -490,7 +490,7 @@ async fn test_unlocking_for_locked_utxos(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down Expand Up @@ -713,7 +713,7 @@ async fn ok(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
4 changes: 2 additions & 2 deletions api-server/stack-test-suite/tests/v2/address_all_utxos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ async fn multiple_utxos_to_single_address(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down Expand Up @@ -521,7 +521,7 @@ async fn ok(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ async fn ok(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ async fn multiple_utxos_to_single_address(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down Expand Up @@ -503,7 +503,7 @@ async fn ok(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
2 changes: 1 addition & 1 deletion api-server/stack-test-suite/tests/v2/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ async fn ok(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(chain_config).await.unwrap();
db_tx.reinitialize_storage(chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
2 changes: 1 addition & 1 deletion api-server/stack-test-suite/tests/v2/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async fn ok(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(tf.chain_config());

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(tf.chain_config()).await.unwrap();
db_tx.reinitialize_storage(tf.chain_config()).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
4 changes: 2 additions & 2 deletions api-server/stack-test-suite/tests/v2/block_reward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async fn no_reward(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down Expand Up @@ -200,7 +200,7 @@ async fn has_reward(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async fn ok(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
2 changes: 1 addition & 1 deletion api-server/stack-test-suite/tests/v2/chain_at_height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ async fn height_n(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
2 changes: 1 addition & 1 deletion api-server/stack-test-suite/tests/v2/chain_tip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async fn height_n(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
2 changes: 1 addition & 1 deletion api-server/stack-test-suite/tests/v2/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ async fn ok(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
2 changes: 1 addition & 1 deletion api-server/stack-test-suite/tests/v2/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ async fn ok(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
2 changes: 1 addition & 1 deletion api-server/stack-test-suite/tests/v2/pool_block_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ async fn ok(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
2 changes: 1 addition & 1 deletion api-server/stack-test-suite/tests/v2/pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ async fn ok(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
2 changes: 1 addition & 1 deletion api-server/stack-test-suite/tests/v2/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ async fn ok(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
4 changes: 2 additions & 2 deletions api-server/stack-test-suite/tests/v2/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ async fn multiple_tx_in_same_block(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down Expand Up @@ -361,7 +361,7 @@ async fn ok(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async fn get_block_failed(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down Expand Up @@ -226,7 +226,7 @@ async fn transaction_not_part_of_block(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down Expand Up @@ -344,7 +344,7 @@ async fn cannot_find_transaction_in_block(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down Expand Up @@ -488,7 +488,7 @@ async fn ok(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
2 changes: 1 addition & 1 deletion api-server/stack-test-suite/tests/v2/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ async fn ok(#[case] seed: Seed) {
let mut storage = TransactionalApiServerInMemoryStorage::new(&chain_config);

let mut db_tx = storage.transaction_rw().await.unwrap();
db_tx.initialize_storage(&chain_config).await.unwrap();
db_tx.reinitialize_storage(&chain_config).await.unwrap();
db_tx.commit().await.unwrap();

storage
Expand Down
16 changes: 14 additions & 2 deletions api-server/storage-test-suite/src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ where
let mut storage = storage_maker().await;
let mut tx = storage.transaction_rw().await.unwrap();
let chain_config = create_unit_test_config();
tx.initialize_storage(&chain_config).await.unwrap();
tx.reinitialize_storage(&chain_config).await.unwrap();
tx.commit().await.unwrap();
let tx = storage.transaction_ro().await.unwrap();
assert!(tx.is_initialized().await.unwrap());
Expand All @@ -87,7 +87,7 @@ where
let mut storage = storage_maker().await;
let mut tx = storage.transaction_rw().await.unwrap();
let chain_config = create_unit_test_config();
tx.initialize_storage(&chain_config).await.unwrap();
tx.reinitialize_storage(&chain_config).await.unwrap();
tx.commit().await.unwrap();

let db_tx = storage.transaction_ro().await.unwrap();
Expand Down Expand Up @@ -500,6 +500,18 @@ where
UtxoWithExtraInfo::new(output.clone(), None),
)]
);

let bob_utxos = db_tx
.get_locked_utxos_until_now(
block_height.next_height(),
(
next_block_timestamp,
next_block_timestamp.add_int_seconds(10).unwrap(),
),
)
.await
.unwrap();
assert_eq!(bob_utxos, vec![]);
}

// get all utxos
Expand Down

0 comments on commit 7dc48b1

Please sign in to comment.