Skip to content

Commit

Permalink
Split select coins and get state
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Jan 24, 2024
1 parent 902956d commit b5901e5
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/wallet/coin_store.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, future::Future};

use chia_protocol::CoinState;
use chia_protocol::{Coin, CoinState};
use parking_lot::Mutex;

/// Keeps track of the state of coins in a wallet.
Expand All @@ -9,7 +9,10 @@ pub trait CoinStore {
fn update_coin_state(&self, coin_states: Vec<CoinState>) -> impl Future<Output = ()> + Send;

/// Gets a list of unspent coins.
fn unspent_coins(&self) -> impl Future<Output = Vec<CoinState>> + Send;
fn unspent_coins(&self) -> impl Future<Output = Vec<Coin>> + Send;

/// Gets the current state of a coin.
fn coin_state(&self, coin_id: [u8; 32]) -> impl Future<Output = Option<CoinState>> + Send;

/// Gets coin states for a given puzzle hash.
fn is_used(&self, puzzle_hash: [u8; 32]) -> impl Future<Output = bool> + Send;
Expand Down Expand Up @@ -49,16 +52,25 @@ impl CoinStore for MemoryCoinStore {
}
}

async fn unspent_coins(&self) -> Vec<CoinState> {
async fn unspent_coins(&self) -> Vec<Coin> {
self.coin_states
.lock()
.values()
.flatten()
.filter(|coin_state| coin_state.spent_height.is_none())
.cloned()
.map(|coin_state| coin_state.coin.clone())
.collect()
}

async fn coin_state(&self, coin_id: [u8; 32]) -> Option<CoinState> {
self.coin_states
.lock()
.values()
.flatten()
.find(|coin_state| coin_state.coin.coin_id() == coin_id)
.cloned()
}

async fn is_used(&self, puzzle_hash: [u8; 32]) -> bool {
self.coin_states
.lock()
Expand Down

0 comments on commit b5901e5

Please sign in to comment.