Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#184 unconfirmed balance pr #191

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ pub async fn initialize(cli_args: cli_args::Args) -> Result<()> {
cli_args.max_mempool_num_tx,
latest_block.hash(),
);

let mut global_state_lock = GlobalStateLock::new(
wallet_state,
blockchain_state,
Expand Down
10 changes: 10 additions & 0 deletions src/locks/tokio/atomic_rw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use futures::future::BoxFuture;
use tokio::sync::RwLock;
use tokio::sync::RwLockReadGuard;
use tokio::sync::RwLockWriteGuard;
use tokio::sync::TryLockError;

use super::LockAcquisition;
use super::LockCallbackFn;
Expand Down Expand Up @@ -240,6 +241,15 @@ impl<T> AtomicRw<T> {
AtomicRwWriteGuard::new(guard, &self.lock_callback_info)
}

/// Attempt to acquire write lock immediately.
///
/// If the lock cannot be acquired without waiting, an error is returned.
pub fn try_lock_guard_mut(&mut self) -> Result<AtomicRwWriteGuard<T>, TryLockError> {
self.try_acquire_write_cb();
let guard = self.inner.try_write()?;
Ok(AtomicRwWriteGuard::new(guard, &self.lock_callback_info))
}

Comment on lines +244 to +252
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not seem to be used in repository. Please use pub(crate) to spot such unused functions.

/// Immutably access the data of type `T` in a closure and possibly return a result of type `R`
///
/// # Examples
Expand Down
12 changes: 7 additions & 5 deletions src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,8 @@ impl MainLoopHandler {

// Insert into mempool
global_state_mut
.mempool
.insert(&pt2m_transaction.transaction);
.mempool_insert(pt2m_transaction.transaction.to_owned())
.await?;

// send notification to peers
let transaction_notification: TransactionNotification =
Expand Down Expand Up @@ -998,7 +998,7 @@ impl MainLoopHandler {
// Handle mempool cleanup, i.e. removing stale/too old txs from mempool
_ = &mut mempool_cleanup_timer => {
debug!("Timer: mempool-cleaner job");
self.global_state_lock.lock_mut(|s| s.mempool.prune_stale_transactions()).await;
self.global_state_lock.lock_guard_mut().await.mempool_prune_stale_transactions().await?;

// Reset the timer to run this branch again in P seconds
mempool_cleanup_timer.as_mut().reset(tokio::time::Instant::now() + mempool_cleanup_timer_interval);
Expand Down Expand Up @@ -1054,8 +1054,10 @@ impl MainLoopHandler {

// insert transaction into mempool
self.global_state_lock
.lock_mut(|s| s.mempool.insert(&transaction))
.await;
.lock_guard_mut()
.await
.mempool_insert(*transaction)
.await?;

// do not shut down
Ok(false)
Expand Down
15 changes: 10 additions & 5 deletions src/mine_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use crate::util_types::mutator_set::mutator_set_accumulator::MutatorSetAccumulat
const MOCK_MAX_BLOCK_SIZE: u32 = 1_000_000;

/// Prepare a Block for mining
fn make_block_template(
pub(crate) fn make_block_template(
previous_block: &Block,
transaction: Transaction,
mut block_timestamp: Timestamp,
Expand Down Expand Up @@ -311,7 +311,7 @@ pub(crate) async fn make_coinbase_transaction(
/// Create the transaction that goes into the block template. The transaction is
/// built from the mempool and from the coinbase transaction. Also returns the
/// "sender randomness" used in the coinbase transaction.
async fn create_block_transaction(
pub(crate) async fn create_block_transaction(
predecessor_block: &Block,
global_state: &GlobalState,
timestamp: Timestamp,
Expand Down Expand Up @@ -545,7 +545,7 @@ mod mine_loop_tests {

#[traced_test]
#[tokio::test]
async fn block_template_is_valid_test() {
async fn block_template_is_valid_test() -> Result<()> {
// Verify that a block template made with transaction from the mempool is a valid block
let network = Network::Main;
let mut alice = mock_genesis_global_state(network, 2, WalletSecret::devnet_wallet()).await;
Expand Down Expand Up @@ -636,8 +636,11 @@ mod mine_loop_tests {
// no need to inform wallet of expected utxos; block template validity
// is what is being tested

alice.lock_guard_mut().await.mempool.insert(&tx_by_preminer);
assert_eq!(1, alice.lock_guard().await.mempool.len());
{
let mut alice_gsm = alice.lock_guard_mut().await;
alice_gsm.mempool_insert(tx_by_preminer).await?;
assert_eq!(1, alice_gsm.mempool.len());
}

// Build transaction for block
let (transaction_non_empty_mempool, _new_coinbase_sender_randomness) = {
Expand Down Expand Up @@ -667,6 +670,8 @@ mod mine_loop_tests {
.is_valid(&genesis_block, in_seven_months + Timestamp::seconds(2)),
"Block template created by miner with non-empty mempool must be valid"
);

Ok(())
}

/// This test mines a single block at height 1 on the regtest network
Expand Down
2 changes: 1 addition & 1 deletion src/models/blockchain/transaction/transaction_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub enum UtxoNotificationMedium {
///
/// see also: [UtxoNotification]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
enum UtxoNotifyMethod {
pub(crate) enum UtxoNotifyMethod {
/// the utxo notification should be transferred to recipient encrypted on the blockchain
OnChain(ReceivingAddress),

Expand Down
Loading
Loading