Skip to content

Commit

Permalink
Cache maxsize to speed up offer request
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-belcher committed Jan 31, 2022
1 parent 5ac963b commit a5ec8de
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ pub fn run_maker(
let wallet_ptr = Arc::new(RwLock::new(wallet));
let config = maker_protocol::MakerConfig {
port,
rpc_ping_interval: 30,
rpc_ping_interval: 60,
watchtower_ping_interval: 300,
maker_behavior,
kill_flag: if kill_flag.is_none() {
Expand Down
8 changes: 6 additions & 2 deletions src/maker_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ async fn run(
"Running maker with special behavior = {:?}",
config.maker_behavior
);
wallet
.write()
.unwrap()
.refresh_offer_maxsize_cache(Arc::clone(&rpc))?;

log::info!("Pinging watchtowers. . .");
ping_watchtowers().await?;
Expand Down Expand Up @@ -119,7 +123,7 @@ async fn run(
break Err(client_err.unwrap());
},
_ = sleep(Duration::from_secs(config.rpc_ping_interval)) => {
let rpc_ping_success = rpc.get_best_block_hash().is_ok();
let rpc_ping_success = wallet.write().unwrap().refresh_offer_maxsize_cache(Arc::clone(&rpc)).is_ok();
let watchtowers_ping_interval = Duration::from_secs(config.watchtower_ping_interval);
let (watchtowers_ping_success, debug_msg) = if Instant::now()
.saturating_duration_since(last_watchtowers_ping)
Expand Down Expand Up @@ -283,7 +287,7 @@ async fn handle_message(
}
ExpectedMessage::NewlyConnectedTaker => match request {
TakerToMakerMessage::GiveOffer(_) => {
let max_size = wallet.read().unwrap().get_offer_maxsize(rpc)?;
let max_size = wallet.read().unwrap().get_offer_maxsize_cache();
let tweakable_point = wallet.read().unwrap().get_tweakable_keypair().1;
connection_state.allowed_message = ExpectedMessage::SignSendersContractTx;
Some(MakerToTakerMessage::Offer(Offer {
Expand Down
11 changes: 9 additions & 2 deletions src/wallet_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub struct Wallet {
initial_address_import_count: usize,
incoming_swapcoins: HashMap<Script, IncomingSwapCoin>,
outgoing_swapcoins: HashMap<Script, OutgoingSwapCoin>,
offer_maxsize_cache: u64,
}

pub enum WalletSyncAddressAmount {
Expand Down Expand Up @@ -546,6 +547,7 @@ impl Wallet {
.iter()
.map(|sc| (sc.get_multisig_redeemscript(), sc.clone()))
.collect::<HashMap<Script, OutgoingSwapCoin>>(),
offer_maxsize_cache: 0,
};
Ok(wallet)
}
Expand Down Expand Up @@ -1187,10 +1189,15 @@ impl Wallet {
)?)
}

pub fn get_offer_maxsize(&self, rpc: Arc<Client>) -> Result<u64, Error> {
pub fn refresh_offer_maxsize_cache(&mut self, rpc: Arc<Client>) -> Result<(), Error> {
let utxos = self.list_unspent_from_wallet(&rpc, false)?;
let balance: Amount = utxos.iter().fold(Amount::ZERO, |acc, u| acc + u.0.amount);
Ok(balance.as_sat())
self.offer_maxsize_cache = balance.as_sat();
Ok(())
}

pub fn get_offer_maxsize_cache(&self) -> u64 {
self.offer_maxsize_cache
}

pub fn get_tweakable_keypair(&self) -> (SecretKey, PublicKey) {
Expand Down

0 comments on commit a5ec8de

Please sign in to comment.