Skip to content

Commit aa0f928

Browse files
authored
Merge pull request #751 from TheCharlatan/needsFundingSerialization
Cli: send encoded funding info
2 parents 2ee4140 + 58cc95d commit aa0f928

8 files changed

Lines changed: 139 additions & 97 deletions

File tree

src/bus/ctl.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,31 @@ fn format_keys(keys: &Keys) -> String {
239239
format!("sk: {}, pk: {}", keys.0.display_secret(), keys.1,)
240240
}
241241

242-
#[derive(Clone, Debug, Display, NetworkDecode, NetworkEncode)]
242+
#[cfg_attr(feature = "serde", serde_as)]
243+
#[derive(Clone, Debug, Display, Eq, PartialEq, NetworkDecode, NetworkEncode)]
244+
#[cfg_attr(
245+
feature = "serde",
246+
derive(Serialize, Deserialize),
247+
serde(crate = "serde_crate")
248+
)]
243249
pub enum FundingInfo {
244-
#[display("bitcoin(..)")]
250+
#[display("{0}")]
245251
Bitcoin(BitcoinFundingInfo),
246-
#[display("monero(..)")]
252+
#[display("{0}")]
247253
Monero(MoneroFundingInfo),
248254
}
249255

250-
#[derive(Clone, Debug, NetworkDecode, NetworkEncode)]
256+
#[cfg_attr(feature = "serde", serde_as)]
257+
#[derive(Clone, Debug, Eq, PartialEq, NetworkDecode, NetworkEncode)]
258+
#[cfg_attr(
259+
feature = "serde",
260+
derive(Serialize, Deserialize),
261+
serde(crate = "serde_crate")
262+
)]
251263
pub struct BitcoinFundingInfo {
252264
pub swap_id: SwapId,
253265
pub address: bitcoin::Address,
266+
#[serde(with = "bitcoin::util::amount::serde::as_btc")]
254267
pub amount: bitcoin::Amount,
255268
}
256269

@@ -277,9 +290,16 @@ impl fmt::Display for BitcoinFundingInfo {
277290
}
278291
}
279292

280-
#[derive(Clone, Debug, NetworkEncode, NetworkDecode)]
293+
#[cfg_attr(feature = "serde", serde_as)]
294+
#[derive(Clone, Debug, Eq, PartialEq, NetworkEncode, NetworkDecode)]
295+
#[cfg_attr(
296+
feature = "serde",
297+
derive(Serialize, Deserialize),
298+
serde(crate = "serde_crate")
299+
)]
281300
pub struct MoneroFundingInfo {
282301
pub swap_id: SwapId,
302+
#[serde(with = "monero::util::amount::serde::as_xmr")]
283303
pub amount: monero::Amount,
284304
pub address: monero::Address,
285305
}

src/bus/info.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use crate::cli::OfferSelector;
1616
use crate::farcasterd::stats::Stats;
1717
use crate::syncerd::runtime::SyncerdTask;
1818

19+
use super::ctl::FundingInfo;
20+
1921
#[derive(Clone, Debug, Display, From, NetworkEncode, NetworkDecode)]
2022
#[non_exhaustive]
2123
pub enum InfoMsg {
@@ -162,6 +164,8 @@ pub enum InfoMsg {
162164
#[display("checkpoint_entry({0})")]
163165
CheckpointEntry(CheckpointEntry),
164166
// - End GetCheckpointEntry section
167+
#[display("{0}")]
168+
FundingInfos(FundingInfos),
165169
}
166170

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

306+
#[cfg_attr(feature = "serde", serde_as)]
307+
#[derive(Clone, PartialEq, Eq, Debug, Display, Default, NetworkEncode, NetworkDecode)]
308+
#[cfg_attr(
309+
feature = "serde",
310+
derive(Serialize, Deserialize),
311+
serde(crate = "serde_crate")
312+
)]
313+
#[display(FundingInfos::to_yaml_string)]
314+
pub struct FundingInfos {
315+
pub swaps_need_funding: Vec<FundingInfo>,
316+
}
317+
302318
#[cfg_attr(feature = "serde", serde_as)]
303319
#[derive(Clone, PartialEq, Eq, Debug, Display, NetworkEncode, NetworkDecode)]
304320
#[cfg_attr(
@@ -394,3 +410,5 @@ impl ToYamlString for SwapInfo {}
394410
impl ToYamlString for SyncerInfo {}
395411
#[cfg(feature = "serde")]
396412
impl ToYamlString for ProgressEvent {}
413+
#[cfg(feature = "serde")]
414+
impl ToYamlString for FundingInfos {}

src/farcasterd/runtime.rs

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
// along with this software.
1414
// If not, see <https://opensource.org/licenses/MIT>.
1515

16-
use crate::bus::ctl::{BitcoinFundingInfo, CtlMsg, GetKeys, MoneroFundingInfo};
17-
use crate::bus::p2p::{PeerMsg, TakerCommit};
16+
use crate::bus::ctl::FundingInfo;
17+
use crate::bus::ctl::{CtlMsg, GetKeys};
18+
use crate::bus::info::FundingInfos;
19+
use crate::bus::p2p::PeerMsg;
20+
use crate::bus::p2p::TakerCommit;
1821
use crate::bus::sync::SyncMsg;
1922
use crate::bus::{BusMsg, List, ServiceBus};
2023
use crate::event::StateMachineExecutor;
@@ -588,46 +591,18 @@ impl Runtime {
588591
// if no swap service exists no subscription need to be removed
589592
}
590593

591-
InfoMsg::NeedsFunding(Blockchain::Monero) => {
592-
let funding_infos: Vec<MoneroFundingInfo> = self
594+
// Filter tsm by funding needs by blockchain and return the funding infos
595+
InfoMsg::NeedsFunding(blockchain) => {
596+
let swaps_need_funding: Vec<FundingInfo> = self
593597
.trade_state_machines
594598
.iter()
595-
.filter_map(|tsm| tsm.needs_funding_monero())
599+
.filter_map(|tsm| tsm.needs_funding(blockchain))
596600
.collect();
597-
let len = funding_infos.len();
598-
let res = funding_infos
599-
.iter()
600-
.enumerate()
601-
.map(|(i, funding_info)| {
602-
let mut res = format!("{}", funding_info);
603-
if i < len - 1 {
604-
res.push('\n');
605-
}
606-
res
607-
})
608-
.collect();
609-
self.send_client_info(endpoints, source, InfoMsg::String(res))?;
610-
}
611-
612-
InfoMsg::NeedsFunding(Blockchain::Bitcoin) => {
613-
let funding_infos: Vec<BitcoinFundingInfo> = self
614-
.trade_state_machines
615-
.iter()
616-
.filter_map(|tsm| tsm.needs_funding_bitcoin())
617-
.collect();
618-
let len = funding_infos.len();
619-
let res = funding_infos
620-
.iter()
621-
.enumerate()
622-
.map(|(i, funding_info)| {
623-
let mut res = format!("{}", funding_info);
624-
if i < len - 1 {
625-
res.push('\n');
626-
}
627-
res
628-
})
629-
.collect();
630-
self.send_client_info(endpoints, source, InfoMsg::String(res))?;
601+
self.send_client_info(
602+
endpoints,
603+
source,
604+
InfoMsg::FundingInfos(FundingInfos { swaps_need_funding }),
605+
)?;
631606
}
632607

633608
req => {

src/farcasterd/trade_state_machine.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,15 @@ impl TradeStateMachine {
275275
}
276276
}
277277

278+
pub fn needs_funding(&self, blockchain: Blockchain) -> Option<FundingInfo> {
279+
match blockchain {
280+
Blockchain::Monero => self.needs_funding_monero().map(|f| FundingInfo::Monero(f)),
281+
Blockchain::Bitcoin => self
282+
.needs_funding_bitcoin()
283+
.map(|f| FundingInfo::Bitcoin(f)),
284+
}
285+
}
286+
278287
pub fn needs_funding_monero(&self) -> Option<MoneroFundingInfo> {
279288
match self {
280289
TradeStateMachine::SwapdRunning(SwapdRunning {

src/grpcd/proto/farcaster.proto

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,13 @@ message NeedsFundingRequest {
173173

174174
message NeedsFundingResponse {
175175
uint32 id = 1;
176-
string funding_infos = 2;
176+
repeated FundingInfo funding_infos = 2;
177+
}
178+
179+
message FundingInfo {
180+
string swap_id = 1;
181+
string address = 2;
182+
uint64 amount = 3;
177183
}
178184

179185
message SweepAddressRequest {

src/grpcd/runtime.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::bus::bridge::BridgeMsg;
2+
use crate::bus::ctl::FundingInfo;
23
use crate::bus::ctl::ProtoPublicOffer;
34
use crate::bus::ctl::PubOffer;
45
use crate::bus::info::Address;
@@ -664,10 +665,25 @@ impl Farcaster for FarcasterService {
664665
.await?;
665666

666667
match oneshot_rx.await {
667-
Ok(BusMsg::Info(InfoMsg::String(infos))) => {
668+
Ok(BusMsg::Info(InfoMsg::FundingInfos(infos))) => {
668669
let reply = NeedsFundingResponse {
669670
id,
670-
funding_infos: infos,
671+
funding_infos: infos
672+
.swaps_need_funding
673+
.iter()
674+
.map(|info| match info {
675+
FundingInfo::Bitcoin(b_info) => farcaster::FundingInfo {
676+
swap_id: b_info.swap_id.to_string(),
677+
address: b_info.address.to_string(),
678+
amount: b_info.amount.as_sat(),
679+
},
680+
FundingInfo::Monero(m_info) => farcaster::FundingInfo {
681+
swap_id: m_info.swap_id.to_string(),
682+
address: m_info.address.to_string(),
683+
amount: m_info.amount.as_pico(),
684+
},
685+
})
686+
.collect(),
671687
};
672688
Ok(GrpcResponse::new(reply))
673689
}

tests/grpc.rs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::farcaster::{
99
};
1010
use bitcoincore_rpc::RpcApi;
1111
use farcaster::{InfoRequest, MakeResponse, NeedsFundingResponse};
12-
use farcaster_node::bus::ctl::BitcoinFundingInfo;
1312
use std::{str::FromStr, sync::Arc, time};
1413
use tonic::transport::Endpoint;
1514
use utils::{config, fc::*};
@@ -154,18 +153,11 @@ async fn grpc_server_functional_test() {
154153
let NeedsFundingResponse { id, funding_infos } = response.unwrap().into_inner();
155154
assert_eq!(id, 11);
156155

157-
let funding_info = BitcoinFundingInfo::from_str(&funding_infos).unwrap();
156+
let address = bitcoin::Address::from_str(&funding_infos[0].address).unwrap();
157+
let amount = bitcoin::Amount::from_sat(funding_infos[0].amount);
158+
158159
bitcoin_rpc
159-
.send_to_address(
160-
&funding_info.address,
161-
funding_info.amount,
162-
None,
163-
None,
164-
None,
165-
None,
166-
None,
167-
None,
168-
)
160+
.send_to_address(&address, amount, None, None, None, None, None, None)
169161
.unwrap();
170162

171163
// Test abort swap
@@ -189,7 +181,7 @@ async fn grpc_server_functional_test() {
189181
let mut farcaster_client_1 = FarcasterClient::new(channel_1);
190182
let request = tonic::Request::new(SweepAddressRequest {
191183
id: 13,
192-
source_address: funding_info.address.to_string(),
184+
source_address: address.to_string(),
193185
destination_address: btc_address.to_string(),
194186
});
195187
let response = farcaster_client_1.sweep_address(request).await;
@@ -225,18 +217,11 @@ async fn grpc_server_functional_test() {
225217
let response = farcaster_client_1.needs_funding(request).await;
226218
let NeedsFundingResponse { id, funding_infos } = response.unwrap().into_inner();
227219
assert_eq!(id, 11);
228-
let funding_info = BitcoinFundingInfo::from_str(&funding_infos).unwrap();
220+
let address = bitcoin::Address::from_str(&funding_infos[0].address).unwrap();
221+
let amount = bitcoin::Amount::from_sat(funding_infos[0].amount);
222+
229223
bitcoin_rpc
230-
.send_to_address(
231-
&funding_info.address,
232-
funding_info.amount,
233-
None,
234-
None,
235-
None,
236-
None,
237-
None,
238-
None,
239-
)
224+
.send_to_address(&address, amount, None, None, None, None, None, None)
240225
.unwrap();
241226

242227
// test swap info

0 commit comments

Comments
 (0)