Skip to content
Merged
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
30 changes: 25 additions & 5 deletions src/bus/ctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,31 @@ fn format_keys(keys: &Keys) -> String {
format!("sk: {}, pk: {}", keys.0.display_secret(), keys.1,)
}

#[derive(Clone, Debug, Display, NetworkDecode, NetworkEncode)]
#[cfg_attr(feature = "serde", serde_as)]
#[derive(Clone, Debug, Display, Eq, PartialEq, NetworkDecode, NetworkEncode)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate")
)]
pub enum FundingInfo {
#[display("bitcoin(..)")]
#[display("{0}")]
Bitcoin(BitcoinFundingInfo),
#[display("monero(..)")]
#[display("{0}")]
Monero(MoneroFundingInfo),
}

#[derive(Clone, Debug, NetworkDecode, NetworkEncode)]
#[cfg_attr(feature = "serde", serde_as)]
#[derive(Clone, Debug, Eq, PartialEq, NetworkDecode, NetworkEncode)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate")
)]
pub struct BitcoinFundingInfo {
pub swap_id: SwapId,
pub address: bitcoin::Address,
#[serde(with = "bitcoin::util::amount::serde::as_btc")]
pub amount: bitcoin::Amount,
}

Expand All @@ -274,9 +287,16 @@ impl fmt::Display for BitcoinFundingInfo {
}
}

#[derive(Clone, Debug, NetworkEncode, NetworkDecode)]
#[cfg_attr(feature = "serde", serde_as)]
#[derive(Clone, Debug, Eq, PartialEq, NetworkEncode, NetworkDecode)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate")
)]
pub struct MoneroFundingInfo {
pub swap_id: SwapId,
#[serde(with = "monero::util::amount::serde::as_xmr")]
pub amount: monero::Amount,
pub address: monero::Address,
}
Expand Down
18 changes: 18 additions & 0 deletions src/bus/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use crate::cli::OfferSelector;
use crate::farcasterd::stats::Stats;
use crate::syncerd::runtime::SyncerdTask;

use super::ctl::FundingInfo;

#[derive(Clone, Debug, Display, From, NetworkEncode, NetworkDecode)]
#[non_exhaustive]
pub enum InfoMsg {
Expand Down Expand Up @@ -162,6 +164,8 @@ pub enum InfoMsg {
#[display("checkpoint_entry({0})")]
CheckpointEntry(CheckpointEntry),
// - End GetCheckpointEntry section
#[display("{0}")]
FundingInfos(FundingInfos),
}

#[cfg_attr(feature = "serde", serde_as)]
Expand Down Expand Up @@ -299,6 +303,18 @@ pub struct SwapProgress {
pub progress: Vec<ProgressEvent>,
}

#[cfg_attr(feature = "serde", serde_as)]
#[derive(Clone, PartialEq, Eq, Debug, Display, Default, NetworkEncode, NetworkDecode)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate")
)]
#[display(FundingInfos::to_yaml_string)]
pub struct FundingInfos {
pub swaps_need_funding: Vec<FundingInfo>,
}

#[cfg_attr(feature = "serde", serde_as)]
#[derive(Clone, PartialEq, Eq, Debug, Display, NetworkEncode, NetworkDecode)]
#[cfg_attr(
Expand Down Expand Up @@ -394,3 +410,5 @@ impl ToYamlString for SwapInfo {}
impl ToYamlString for SyncerInfo {}
#[cfg(feature = "serde")]
impl ToYamlString for ProgressEvent {}
#[cfg(feature = "serde")]
impl ToYamlString for FundingInfos {}
53 changes: 14 additions & 39 deletions src/farcasterd/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.

use crate::bus::ctl::{BitcoinFundingInfo, CtlMsg, GetKeys, MoneroFundingInfo};
use crate::bus::p2p::{PeerMsg, TakerCommit};
use crate::bus::ctl::FundingInfo;
use crate::bus::ctl::{CtlMsg, GetKeys};
use crate::bus::info::FundingInfos;
use crate::bus::p2p::PeerMsg;
use crate::bus::p2p::TakerCommit;
use crate::bus::sync::SyncMsg;
use crate::bus::{BusMsg, List, ServiceBus};
use crate::event::StateMachineExecutor;
Expand Down Expand Up @@ -593,46 +596,18 @@ impl Runtime {
// if no swap service exists no subscription need to be removed
}

InfoMsg::NeedsFunding(Blockchain::Monero) => {
let funding_infos: Vec<MoneroFundingInfo> = self
// Filter tsm by funding needs by blockchain and return the funding infos
InfoMsg::NeedsFunding(blockchain) => {
let swaps_need_funding: Vec<FundingInfo> = self
.trade_state_machines
.iter()
.filter_map(|tsm| tsm.needs_funding_monero())
.filter_map(|tsm| tsm.needs_funding(blockchain))
.collect();
let len = funding_infos.len();
let res = funding_infos
.iter()
.enumerate()
.map(|(i, funding_info)| {
let mut res = format!("{}", funding_info);
if i < len - 1 {
res.push('\n');
}
res
})
.collect();
self.send_client_info(endpoints, source, InfoMsg::String(res))?;
}

InfoMsg::NeedsFunding(Blockchain::Bitcoin) => {
let funding_infos: Vec<BitcoinFundingInfo> = self
.trade_state_machines
.iter()
.filter_map(|tsm| tsm.needs_funding_bitcoin())
.collect();
let len = funding_infos.len();
let res = funding_infos
.iter()
.enumerate()
.map(|(i, funding_info)| {
let mut res = format!("{}", funding_info);
if i < len - 1 {
res.push('\n');
}
res
})
.collect();
self.send_client_info(endpoints, source, InfoMsg::String(res))?;
self.send_client_info(
endpoints,
source,
InfoMsg::FundingInfos(FundingInfos { swaps_need_funding }),
)?;
}

req => {
Expand Down
9 changes: 9 additions & 0 deletions src/farcasterd/trade_state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,15 @@ impl TradeStateMachine {
}
}

pub fn needs_funding(&self, blockchain: Blockchain) -> Option<FundingInfo> {
match blockchain {
Blockchain::Monero => self.needs_funding_monero().map(|f| FundingInfo::Monero(f)),
Blockchain::Bitcoin => self
.needs_funding_bitcoin()
.map(|f| FundingInfo::Bitcoin(f)),
}
}

pub fn needs_funding_monero(&self) -> Option<MoneroFundingInfo> {
match self {
TradeStateMachine::SwapdRunning(SwapdRunning {
Expand Down
8 changes: 7 additions & 1 deletion src/grpcd/proto/farcaster.proto
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,13 @@ message NeedsFundingRequest {

message NeedsFundingResponse {
uint32 id = 1;
string funding_infos = 2;
repeated FundingInfo funding_infos = 2;
}

message FundingInfo {
string swap_id = 1;
string address = 2;
uint64 amount = 3;
}

message SweepAddressRequest {
Expand Down
20 changes: 18 additions & 2 deletions src/grpcd/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::bus::bridge::BridgeMsg;
use crate::bus::ctl::FundingInfo;
use crate::bus::ctl::ProtoPublicOffer;
use crate::bus::ctl::PubOffer;
use crate::bus::info::Address;
Expand Down Expand Up @@ -664,10 +665,25 @@ impl Farcaster for FarcasterService {
.await?;

match oneshot_rx.await {
Ok(BusMsg::Info(InfoMsg::String(infos))) => {
Ok(BusMsg::Info(InfoMsg::FundingInfos(infos))) => {
let reply = NeedsFundingResponse {
id,
funding_infos: infos,
funding_infos: infos
.swaps_need_funding
.iter()
.map(|info| match info {
FundingInfo::Bitcoin(b_info) => farcaster::FundingInfo {
swap_id: b_info.swap_id.to_string(),
address: b_info.address.to_string(),
amount: b_info.amount.as_sat(),
},
FundingInfo::Monero(m_info) => farcaster::FundingInfo {
swap_id: m_info.swap_id.to_string(),
address: m_info.address.to_string(),
amount: m_info.amount.as_pico(),
},
})
.collect(),
};
Ok(GrpcResponse::new(reply))
}
Expand Down
33 changes: 9 additions & 24 deletions tests/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::farcaster::{
};
use bitcoincore_rpc::RpcApi;
use farcaster::{InfoRequest, MakeResponse, NeedsFundingResponse};
use farcaster_node::bus::ctl::BitcoinFundingInfo;
use std::{str::FromStr, sync::Arc, time};
use tonic::transport::Endpoint;
use utils::fc::*;
Expand Down Expand Up @@ -150,18 +149,11 @@ async fn grpc_server_functional_test() {
let NeedsFundingResponse { id, funding_infos } = response.unwrap().into_inner();
assert_eq!(id, 11);

let funding_info = BitcoinFundingInfo::from_str(&funding_infos).unwrap();
let address = bitcoin::Address::from_str(&funding_infos[0].address).unwrap();
let amount = bitcoin::Amount::from_sat(funding_infos[0].amount);

bitcoin_rpc
.send_to_address(
&funding_info.address,
funding_info.amount,
None,
None,
None,
None,
None,
None,
)
.send_to_address(&address, amount, None, None, None, None, None, None)
.unwrap();

// Test abort swap
Expand All @@ -184,7 +176,7 @@ async fn grpc_server_functional_test() {
let mut farcaster_client_1 = FarcasterClient::new(channel_1);
let request = tonic::Request::new(SweepAddressRequest {
id: 13,
source_address: funding_info.address.to_string(),
source_address: address.to_string(),
destination_address: btc_address.to_string(),
});
let response = farcaster_client_1.sweep_address(request).await;
Expand Down Expand Up @@ -220,18 +212,11 @@ async fn grpc_server_functional_test() {
let response = farcaster_client_1.needs_funding(request).await;
let NeedsFundingResponse { id, funding_infos } = response.unwrap().into_inner();
assert_eq!(id, 11);
let funding_info = BitcoinFundingInfo::from_str(&funding_infos).unwrap();
let address = bitcoin::Address::from_str(&funding_infos[0].address).unwrap();
let amount = bitcoin::Amount::from_sat(funding_infos[0].amount);

bitcoin_rpc
.send_to_address(
&funding_info.address,
funding_info.amount,
None,
None,
None,
None,
None,
None,
)
.send_to_address(&address, amount, None, None, None, None, None, None)
.unwrap();

// test swap info
Expand Down
Loading