From 3a0fcea4eb5a5a9614ca5b7d1c1b474d5bb2a63c Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 10:49:18 +0300 Subject: [PATCH 001/248] Commented out everything except the core mutual_credit module. --- components/funder/src/lib.rs | 30 ++++++++++++---------- components/funder/src/mutual_credit/mod.rs | 2 -- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index 9410cc270..f539da76f 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -13,24 +13,26 @@ clippy::new_without_default )] -#[macro_use] -extern crate log; +// #[macro_use] +// extern crate log; #[macro_use] extern crate serde_derive; +#[allow(unused)] mod credit_calc; -mod ephemeral; -mod friend; -mod funder; -mod handler; -mod liveness; +// mod ephemeral; +// mod friend; +// mod funder; +// mod handler; +// mod liveness; +#[allow(unused)] mod mutual_credit; -pub mod report; -mod state; -#[cfg(test)] -mod tests; -mod token_channel; +// pub mod report; +// mod state; +// #[cfg(test)] +// mod tests; +// mod token_channel; pub mod types; -pub use self::funder::{funder_loop, FunderError}; -pub use self::state::{FunderMutation, FunderState}; +// pub use self::funder::{funder_loop, FunderError}; +// pub use self::state::{FunderMutation, FunderState}; diff --git a/components/funder/src/mutual_credit/mod.rs b/components/funder/src/mutual_credit/mod.rs index ff97afa55..a3521f69d 100644 --- a/components/funder/src/mutual_credit/mod.rs +++ b/components/funder/src/mutual_credit/mod.rs @@ -1,5 +1,3 @@ -#![warn(unused)] - pub mod incoming; pub mod outgoing; #[cfg(test)] From 998a7400191aecbcfcbf66f1707373769b5a4a1e Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 12:08:13 +0300 Subject: [PATCH 002/248] Initial work: Changing some data types, keeping offst-funder compiling. --- components/funder/Cargo.toml | 5 +- components/funder/src/credit_calc.rs | 2 - components/funder/src/lib.rs | 2 +- .../funder/src/mutual_credit/incoming.rs | 60 ++++---- components/funder/src/mutual_credit/mod.rs | 4 +- .../funder/src/mutual_credit/outgoing.rs | 52 +++---- components/funder/src/mutual_credit/tests.rs | 11 +- components/funder/src/mutual_credit/types.rs | 134 +++++++++++++----- components/funder/src/types.rs | 18 +-- components/proto/src/funder/messages.rs | 19 ++- components/proto/src/funder/mod.rs | 1 + components/proto/src/funder/serialize.rs | 11 +- components/proto/src/funder/signature_buff.rs | 42 +++--- 13 files changed, 221 insertions(+), 140 deletions(-) diff --git a/components/funder/Cargo.toml b/components/funder/Cargo.toml index 861cdddfa..10d6a890c 100644 --- a/components/funder/Cargo.toml +++ b/components/funder/Cargo.toml @@ -23,8 +23,9 @@ bytes = "0.4" futures-cpupool = "0.1.8" futures-preview = "0.3.0-alpha.16" -num-bigint = "0.2.0" -num-traits = "0.2.4" +num-bigint = "0.2.2" +num-traits = "0.2.6" + serde = "1" serde_derive = "1" diff --git a/components/funder/src/credit_calc.rs b/components/funder/src/credit_calc.rs index 0c5df66f0..d1a74153d 100644 --- a/components/funder/src/credit_calc.rs +++ b/components/funder/src/credit_calc.rs @@ -1,5 +1,3 @@ -#![warn(unused)] - use std::convert::TryFrom; // use utils::int_convert::usize_to_u32; diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index f539da76f..5f7ed7797 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -32,7 +32,7 @@ mod mutual_credit; // #[cfg(test)] // mod tests; // mod token_channel; -pub mod types; +// pub mod types; // pub use self::funder::{funder_loop, FunderError}; // pub use self::state::{FunderMutation, FunderState}; diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index 9a0fb60cd..d3ba76cc4 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -4,32 +4,30 @@ use common::int_convert::usize_to_u32; use common::safe_arithmetic::SafeSignedArithmetic; use proto::funder::messages::{ - FailureSendFunds, FriendTcOp, PendingRequest, RequestSendFunds, RequestsStatus, + FailureSendFunds, FriendTcOp, PendingTransaction, RequestSendFunds, RequestsStatus, ResponseSendFunds, }; use proto::funder::signature_buff::{create_response_signature_buffer, verify_failure_signature}; -use crate::types::create_pending_request; - use crate::credit_calc::CreditCalculator; -use super::types::{McMutation, MutualCredit, MAX_FUNDER_DEBT}; +use super::types::{create_pending_transaction, McMutation, MutualCredit, MAX_FUNDER_DEBT}; /* pub struct IncomingRequestSendFunds { - pub request: PendingRequest, + pub request: PendingTransaction, } */ #[derive(Debug)] pub struct IncomingResponseSendFunds { - pub pending_request: PendingRequest, + pub pending_transaction: PendingTransaction, pub incoming_response: ResponseSendFunds, } #[derive(Debug)] pub struct IncomingFailureSendFunds { - pub pending_request: PendingRequest, + pub pending_transaction: PendingTransaction, pub incoming_failure: FailureSendFunds, } @@ -239,7 +237,7 @@ fn process_request_send_funds( let p_remote_requests = &mutual_credit .state() - .pending_requests + .pending_transactions .pending_remote_requests; // Make sure that we don't have this request as a pending request already: if p_remote_requests.contains_key(&request_send_funds.request_id) { @@ -247,14 +245,14 @@ fn process_request_send_funds( } // Add pending request funds: - let pending_friend_request = create_pending_request(&request_send_funds); + let pending_friend_request = create_pending_transaction(&request_send_funds); let mut op_output = ProcessOperationOutput { incoming_message: Some(IncomingMessage::Request(request_send_funds)), mc_mutations: Vec::new(), }; - let tc_mutation = McMutation::InsertRemotePendingRequest(pending_friend_request); + let tc_mutation = McMutation::InsertRemotePendingTransaction(pending_friend_request); mutual_credit.mutate(&tc_mutation); op_output.mc_mutations.push(tc_mutation); @@ -272,22 +270,22 @@ fn process_response_send_funds( ) -> Result { // Make sure that id exists in local_pending hashmap, // and access saved request details. - let local_pending_requests = &mutual_credit + let local_pending_transactions = &mutual_credit .state() - .pending_requests + .pending_transactions .pending_local_requests; // Obtain pending request: // TODO: Possibly get rid of clone() here for optimization later - let pending_request = local_pending_requests + let pending_transaction = local_pending_transactions .get(&response_send_funds.request_id) .ok_or(ProcessOperationError::RequestDoesNotExist)? .clone(); - let dest_public_key = pending_request.route.public_keys.last().unwrap(); + let dest_public_key = pending_transaction.route.public_keys.last().unwrap(); let response_signature_buffer = - create_response_signature_buffer(&response_send_funds, &pending_request); + create_response_signature_buffer(&response_send_funds, &pending_transaction); // Verify response funds signature: if !verify_signature( @@ -299,12 +297,12 @@ fn process_response_send_funds( } // It should never happen that usize_to_u32 fails here, because we - // checked this when we created the pending_request. - let route_len = usize_to_u32(pending_request.route.len()).unwrap(); - let credit_calc = CreditCalculator::new(route_len, pending_request.dest_payment); + // checked this when we created the pending_transaction. + let route_len = usize_to_u32(pending_transaction.route.len()).unwrap(); + let credit_calc = CreditCalculator::new(route_len, pending_transaction.dest_payment); // Find ourselves on the route. If we are not there, abort. - let local_index = pending_request + let local_index = pending_transaction .route .find_pk_pair( &mutual_credit.state().idents.local_public_key, @@ -315,7 +313,7 @@ fn process_response_send_funds( let mut mc_mutations = Vec::new(); // Remove entry from local_pending hashmap: - let tc_mutation = McMutation::RemoveLocalPendingRequest(response_send_funds.request_id); + let tc_mutation = McMutation::RemoveLocalPendingTransaction(response_send_funds.request_id); mutual_credit.mutate(&tc_mutation); mc_mutations.push(tc_mutation); @@ -347,7 +345,7 @@ fn process_response_send_funds( mc_mutations.push(tc_mutation); let incoming_message = Some(IncomingMessage::Response(IncomingResponseSendFunds { - pending_request, + pending_transaction, incoming_response: response_send_funds, })); @@ -363,20 +361,20 @@ fn process_failure_send_funds( ) -> Result { // Make sure that id exists in local_pending hashmap, // and access saved request details. - let local_pending_requests = &mutual_credit + let local_pending_transactions = &mutual_credit .state() - .pending_requests + .pending_transactions .pending_local_requests; // Obtain pending request: - let pending_request = local_pending_requests + let pending_transaction = local_pending_transactions .get(&failure_send_funds.request_id) .ok_or(ProcessOperationError::RequestDoesNotExist)? .clone(); // TODO: Possibly get rid of clone() here for optimization later // Find ourselves on the route. If we are not there, abort. - let local_index = pending_request + let local_index = pending_transaction .route .find_pk_pair( &mutual_credit.state().idents.local_public_key, @@ -389,7 +387,7 @@ fn process_failure_send_funds( // - After us on the route. // - Not the destination node - let reporting_index = pending_request + let reporting_index = pending_transaction .route .pk_to_index(&failure_send_funds.reporting_public_key) .ok_or(ProcessOperationError::ReportingNodeNonexistent)?; @@ -398,17 +396,17 @@ fn process_failure_send_funds( return Err(ProcessOperationError::InvalidReportingNode); } - verify_failure_signature(&failure_send_funds, &pending_request) + verify_failure_signature(&failure_send_funds, &pending_transaction) .ok_or(ProcessOperationError::InvalidFailureSignature)?; // At this point we believe the failure funds is valid. - let route_len = usize_to_u32(pending_request.route.len()).unwrap(); - let credit_calc = CreditCalculator::new(route_len, pending_request.dest_payment); + let route_len = usize_to_u32(pending_transaction.route.len()).unwrap(); + let credit_calc = CreditCalculator::new(route_len, pending_transaction.dest_payment); let mut mc_mutations = Vec::new(); // Remove entry from local_pending hashmap: - let tc_mutation = McMutation::RemoveLocalPendingRequest(failure_send_funds.request_id); + let tc_mutation = McMutation::RemoveLocalPendingTransaction(failure_send_funds.request_id); mutual_credit.mutate(&tc_mutation); mc_mutations.push(tc_mutation); @@ -444,7 +442,7 @@ fn process_failure_send_funds( // Return Failure funds. let incoming_message = Some(IncomingMessage::Failure(IncomingFailureSendFunds { - pending_request, + pending_transaction, incoming_failure: failure_send_funds, })); diff --git a/components/funder/src/mutual_credit/mod.rs b/components/funder/src/mutual_credit/mod.rs index a3521f69d..2d650d21a 100644 --- a/components/funder/src/mutual_credit/mod.rs +++ b/components/funder/src/mutual_credit/mod.rs @@ -1,5 +1,5 @@ -pub mod incoming; -pub mod outgoing; +// pub mod incoming; +// pub mod outgoing; #[cfg(test)] mod tests; pub mod types; diff --git a/components/funder/src/mutual_credit/outgoing.rs b/components/funder/src/mutual_credit/outgoing.rs index e3a124d25..e41f672be 100644 --- a/components/funder/src/mutual_credit/outgoing.rs +++ b/components/funder/src/mutual_credit/outgoing.rs @@ -8,9 +8,8 @@ use proto::funder::messages::{ }; use proto::funder::signature_buff::{create_response_signature_buffer, verify_failure_signature}; -use super::types::{McMutation, MutualCredit, MAX_FUNDER_DEBT}; +use super::types::{create_pending_transaction, McMutation, MutualCredit, MAX_FUNDER_DEBT}; use crate::credit_calc::CreditCalculator; -use crate::types::create_pending_request; /// Processes outgoing funds for a token channel. /// Used to batch as many funds as possible. @@ -165,7 +164,7 @@ impl OutgoingMc { let p_local_requests = &self .mutual_credit .state() - .pending_requests + .pending_transactions .pending_local_requests; // Make sure that we don't have this request as a pending request already: if p_local_requests.contains_key(&request_send_funds.request_id) { @@ -173,10 +172,10 @@ impl OutgoingMc { } // Add pending request funds: - let pending_friend_request = create_pending_request(&request_send_funds); + let pending_friend_request = create_pending_transaction(&request_send_funds); let mut tc_mutations = Vec::new(); - let tc_mutation = McMutation::InsertLocalPendingRequest(pending_friend_request); + let tc_mutation = McMutation::InsertLocalPendingTransaction(pending_friend_request); self.mutual_credit.mutate(&tc_mutation); tc_mutations.push(tc_mutation); @@ -194,14 +193,14 @@ impl OutgoingMc { ) -> Result, QueueOperationError> { // Make sure that id exists in remote_pending hashmap, // and access saved request details. - let remote_pending_requests = &self + let remote_pending_transactions = &self .mutual_credit .state() - .pending_requests + .pending_transactions .pending_remote_requests; // Obtain pending request: - let pending_request = remote_pending_requests + let pending_transaction = remote_pending_transactions .get(&response_send_funds.request_id) .ok_or(QueueOperationError::RequestDoesNotExist)? .clone(); @@ -209,9 +208,9 @@ impl OutgoingMc { // verify signature: let response_signature_buffer = - create_response_signature_buffer(&response_send_funds, &pending_request); + create_response_signature_buffer(&response_send_funds, &pending_transaction); // The response was signed by the destination node: - let dest_public_key = pending_request.route.public_keys.last().unwrap(); + let dest_public_key = pending_transaction.route.public_keys.last().unwrap(); // Verify response funds signature: if !verify_signature( @@ -223,12 +222,12 @@ impl OutgoingMc { } // Calculate amount of credits to freeze. - let route_len = - usize_to_u32(pending_request.route.len()).ok_or(QueueOperationError::RouteTooLong)?; - let credit_calc = CreditCalculator::new(route_len, pending_request.dest_payment); + let route_len = usize_to_u32(pending_transaction.route.len()) + .ok_or(QueueOperationError::RouteTooLong)?; + let credit_calc = CreditCalculator::new(route_len, pending_transaction.dest_payment); // Find ourselves on the route. If we are not there, abort. - let remote_index = pending_request + let remote_index = pending_transaction .route .find_pk_pair( &self.mutual_credit.state().idents.remote_public_key, @@ -240,7 +239,8 @@ impl OutgoingMc { // Remove entry from remote_pending hashmap: let mut tc_mutations = Vec::new(); - let tc_mutation = McMutation::RemoveRemotePendingRequest(response_send_funds.request_id); + let tc_mutation = + McMutation::RemoveRemotePendingTransaction(response_send_funds.request_id); self.mutual_credit.mutate(&tc_mutation); tc_mutations.push(tc_mutation); @@ -281,19 +281,19 @@ impl OutgoingMc { ) -> Result, QueueOperationError> { // Make sure that id exists in remote_pending hashmap, // and access saved request details. - let remote_pending_requests = &self + let remote_pending_transactions = &self .mutual_credit .state() - .pending_requests + .pending_transactions .pending_remote_requests; // Obtain pending request: - let pending_request = remote_pending_requests + let pending_transaction = remote_pending_transactions .get(&failure_send_funds.request_id) .ok_or(QueueOperationError::RequestDoesNotExist)?; // Find ourselves on the route. If we are not there, abort. - let remote_index = pending_request + let remote_index = pending_transaction .route .find_pk_pair( &self.mutual_credit.state().idents.remote_public_key, @@ -302,7 +302,7 @@ impl OutgoingMc { .unwrap(); let local_index = remote_index.checked_add(1).unwrap(); - if local_index.checked_add(1).unwrap() == pending_request.route.len() { + if local_index.checked_add(1).unwrap() == pending_transaction.route.len() { // Note that we can not be the destination. The destination can not be the sender of a // failure funds. return Err(QueueOperationError::FailureSentFromDest); @@ -313,7 +313,7 @@ impl OutgoingMc { // - After us on the route, or us. // - Not the destination node - let reporting_index = pending_request + let reporting_index = pending_transaction .route .pk_to_index(&failure_send_funds.reporting_public_key) .ok_or(QueueOperationError::ReportingNodeNonexistent)?; @@ -322,18 +322,18 @@ impl OutgoingMc { return Err(QueueOperationError::InvalidReportingNode); } - verify_failure_signature(&failure_send_funds, &pending_request) + verify_failure_signature(&failure_send_funds, &pending_transaction) .ok_or(QueueOperationError::InvalidFailureSignature)?; // At this point we believe the failure funds is valid. - let route_len = - usize_to_u32(pending_request.route.len()).ok_or(QueueOperationError::RouteTooLong)?; - let credit_calc = CreditCalculator::new(route_len, pending_request.dest_payment); + let route_len = usize_to_u32(pending_transaction.route.len()) + .ok_or(QueueOperationError::RouteTooLong)?; + let credit_calc = CreditCalculator::new(route_len, pending_transaction.dest_payment); // Remove entry from remote hashmap: let mut tc_mutations = Vec::new(); - let tc_mutation = McMutation::RemoveRemotePendingRequest(failure_send_funds.request_id); + let tc_mutation = McMutation::RemoveRemotePendingTransaction(failure_send_funds.request_id); self.mutual_credit.mutate(&tc_mutation); tc_mutations.push(tc_mutation); diff --git a/components/funder/src/mutual_credit/tests.rs b/components/funder/src/mutual_credit/tests.rs index 9fa514fe4..0d62dd128 100644 --- a/components/funder/src/mutual_credit/tests.rs +++ b/components/funder/src/mutual_credit/tests.rs @@ -15,8 +15,7 @@ use proto::funder::signature_buff::{ create_failure_signature_buffer, create_response_signature_buffer, }; -use crate::mutual_credit::types::MutualCredit; -use crate::types::create_pending_request; +use crate::mutual_credit::types::{create_pending_transaction, MutualCredit}; use crate::mutual_credit::incoming::{ process_operation, ProcessOperationError, ProcessOperationOutput, @@ -129,7 +128,7 @@ fn test_request_response_send_funds() { invoice_id, }; - let pending_request = create_pending_request(&request_send_funds); + let pending_transaction = create_pending_transaction(&request_send_funds); apply_outgoing( &mut mutual_credit, &FriendTcOp::RequestSendFunds(request_send_funds), @@ -152,7 +151,7 @@ fn test_request_response_send_funds() { signature: Signature::from(&[0; SIGNATURE_LEN]), }; - let sign_buffer = create_response_signature_buffer(&response_send_funds, &pending_request); + let sign_buffer = create_response_signature_buffer(&response_send_funds, &pending_transaction); response_send_funds.signature = identity.sign(&sign_buffer); apply_incoming( @@ -204,7 +203,7 @@ fn test_request_failure_send_funds() { invoice_id, }; - let pending_request = create_pending_request(&request_send_funds); + let pending_transaction = create_pending_transaction(&request_send_funds); apply_outgoing( &mut mutual_credit, &FriendTcOp::RequestSendFunds(request_send_funds), @@ -227,7 +226,7 @@ fn test_request_failure_send_funds() { signature: Signature::from(&[0; SIGNATURE_LEN]), }; - let sign_buffer = create_failure_signature_buffer(&failure_send_funds, &pending_request); + let sign_buffer = create_failure_signature_buffer(&failure_send_funds, &pending_transaction); failure_send_funds.signature = identity.sign(&sign_buffer); apply_incoming( diff --git a/components/funder/src/mutual_credit/types.rs b/components/funder/src/mutual_credit/types.rs index 49e2798c0..dd43a22d4 100644 --- a/components/funder/src/mutual_credit/types.rs +++ b/components/funder/src/mutual_credit/types.rs @@ -1,10 +1,14 @@ use im::hashmap::HashMap as ImHashMap; +use num_bigint::BigUint; +use num_traits::cast::ToPrimitive; use common::safe_arithmetic::SafeSignedArithmetic; use crypto::identity::PublicKey; use crypto::uid::Uid; -use proto::funder::messages::{PendingRequest, RequestsStatus}; +use proto::funder::messages::{ + PendingTransaction, RequestSendFunds, RequestsStatus, TransactionStage, +}; /// The maximum possible funder debt. /// We don't use the full u128 because i128 can not go beyond this value. @@ -53,16 +57,16 @@ impl McBalance { // TODO: Rename pending_local_requests to a shorter name, like local. #[derive(Clone, Serialize, Deserialize, Debug)] -pub struct McPendingRequests { +pub struct McPendingTransactions { /// Pending requests that were opened locally and not yet completed - pub pending_local_requests: ImHashMap, + pub pending_local_requests: ImHashMap, /// Pending requests that were opened remotely and not yet completed - pub pending_remote_requests: ImHashMap, + pub pending_remote_requests: ImHashMap, } -impl McPendingRequests { - fn new() -> McPendingRequests { - McPendingRequests { +impl McPendingTransactions { + fn new() -> McPendingTransactions { + McPendingTransactions { pending_local_requests: ImHashMap::new(), pending_remote_requests: ImHashMap::new(), } @@ -86,11 +90,46 @@ impl McRequestsStatus { } } +/// Rates for forwarding a transaction +/// For a transaction of `x` credits, the amount of fees will be: +/// `(x * mul) / 2^32 + add` +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Rate { + /// Commission + mul: u32, + /// Flat rate + add: u32, +} + +#[derive(Debug)] +struct RateError; + +impl Rate { + pub fn new() -> Self { + Rate { mul: 0, add: 0 } + } + + fn calc_fee(&self, dest_payment: u128) -> Result { + // TODO: Fix this using BigUint + let mul_res = (BigUint::from(dest_payment) * BigUint::from(self.mul)) >> 32; + let res = mul_res + BigUint::from(self.add); + res.to_u128().ok_or(RateError) + } +} + #[derive(Clone, Serialize, Deserialize, Debug)] pub struct MutualCreditState { + /// Public identities of local and remote side pub idents: McIdents, + /// Rate for forwarding new transactions to the remote side + pub rate: Rate, + /// Current credit balance with respect to remote side pub balance: McBalance, - pub pending_requests: McPendingRequests, + /// Requests in progress + pub pending_transactions: McPendingTransactions, + /// Can local or remote side open requests? + /// We can allow or disallow opening new requests from the remote side to our side. + /// The remote side controls the opposite direction. pub requests_status: McRequestsStatus, } @@ -106,10 +145,10 @@ pub enum McMutation { SetLocalMaxDebt(u128), SetRemoteMaxDebt(u128), SetBalance(i128), - InsertLocalPendingRequest(PendingRequest), - RemoveLocalPendingRequest(Uid), - InsertRemotePendingRequest(PendingRequest), - RemoveRemotePendingRequest(Uid), + InsertLocalPendingTransaction(PendingTransaction), + RemoveLocalPendingTransaction(Uid), + InsertRemotePendingTransaction(PendingTransaction), + RemoveRemotePendingTransaction(Uid), SetLocalPendingDebt(u128), SetRemotePendingDebt(u128), } @@ -126,8 +165,9 @@ impl MutualCredit { local_public_key: local_public_key.clone(), remote_public_key: remote_public_key.clone(), }, + rate: Rate::new(), balance: McBalance::new(balance), - pending_requests: McPendingRequests::new(), + pending_transactions: McPendingTransactions::new(), requests_status: McRequestsStatus::new(), }, } @@ -166,17 +206,17 @@ impl MutualCredit { self.set_remote_max_debt(*proposed_max_debt) } McMutation::SetBalance(balance) => self.set_balance(*balance), - McMutation::InsertLocalPendingRequest(pending_friend_request) => { - self.insert_local_pending_request(pending_friend_request) + McMutation::InsertLocalPendingTransaction(pending_friend_request) => { + self.insert_local_pending_transaction(pending_friend_request) } - McMutation::RemoveLocalPendingRequest(request_id) => { - self.remove_local_pending_request(request_id) + McMutation::RemoveLocalPendingTransaction(request_id) => { + self.remove_local_pending_transaction(request_id) } - McMutation::InsertRemotePendingRequest(pending_friend_request) => { - self.insert_remote_pending_request(pending_friend_request) + McMutation::InsertRemotePendingTransaction(pending_friend_request) => { + self.insert_remote_pending_transaction(pending_friend_request) } - McMutation::RemoveRemotePendingRequest(request_id) => { - self.remove_remote_pending_request(request_id) + McMutation::RemoveRemotePendingTransaction(request_id) => { + self.remove_remote_pending_transaction(request_id) } McMutation::SetLocalPendingDebt(local_pending_debt) => { self.set_local_pending_debt(*local_pending_debt) @@ -207,32 +247,38 @@ impl MutualCredit { self.state.balance.balance = balance; } - fn insert_remote_pending_request(&mut self, pending_friend_request: &PendingRequest) { - self.state.pending_requests.pending_remote_requests.insert( - pending_friend_request.request_id, - pending_friend_request.clone(), - ); + fn insert_remote_pending_transaction(&mut self, pending_friend_request: &PendingTransaction) { + self.state + .pending_transactions + .pending_remote_requests + .insert( + pending_friend_request.request_id, + pending_friend_request.clone(), + ); } - fn remove_remote_pending_request(&mut self, request_id: &Uid) { + fn remove_remote_pending_transaction(&mut self, request_id: &Uid) { let _ = self .state - .pending_requests + .pending_transactions .pending_remote_requests .remove(request_id); } - fn insert_local_pending_request(&mut self, pending_friend_request: &PendingRequest) { - self.state.pending_requests.pending_local_requests.insert( - pending_friend_request.request_id, - pending_friend_request.clone(), - ); + fn insert_local_pending_transaction(&mut self, pending_friend_request: &PendingTransaction) { + self.state + .pending_transactions + .pending_local_requests + .insert( + pending_friend_request.request_id, + pending_friend_request.clone(), + ); } - fn remove_local_pending_request(&mut self, request_id: &Uid) { + fn remove_local_pending_transaction(&mut self, request_id: &Uid) { let _ = self .state - .pending_requests + .pending_transactions .pending_local_requests .remove(request_id); } @@ -244,4 +290,22 @@ impl MutualCredit { fn set_local_pending_debt(&mut self, local_pending_debt: u128) { self.state.balance.local_pending_debt = local_pending_debt; } + + /// Keep information from a RequestSendFunds message. + /// This information will be used later to deal with a corresponding {Response,Failure}SendFunds messages, + /// as those messages do not repeat the information sent in the request. + fn create_pending_transaction( + &self, + request_send_funds: &RequestSendFunds, + ) -> Result { + Ok(PendingTransaction { + request_id: request_send_funds.request_id, + route: request_send_funds.route.clone(), + dest_payment: request_send_funds.dest_payment, + invoice_id: request_send_funds.invoice_id.clone(), + fee: self.state.rate.calc_fee(request_send_funds.dest_payment)?, + src_hashed_lock: request_send_funds.src_hashed_lock.clone(), + stage: TransactionStage::Request, + }) + } } diff --git a/components/funder/src/types.rs b/components/funder/src/types.rs index e2eeceb7a..3222dcd1c 100644 --- a/components/funder/src/types.rs +++ b/components/funder/src/types.rs @@ -7,7 +7,7 @@ use crypto::identity::{PublicKey, Signature}; use proto::app_server::messages::RelayAddress; use proto::funder::messages::{ ChannelerUpdateFriend, FailureSendFunds, FriendMessage, FriendTcOp, FunderIncomingControl, - FunderOutgoingControl, MoveToken, PendingRequest, RequestSendFunds, ResponseSendFunds, + FunderOutgoingControl, MoveToken, PendingTransaction, RequestSendFunds, ResponseSendFunds, }; use proto::funder::signature_buff::{ @@ -48,17 +48,17 @@ where } pub async fn create_response_send_funds<'a>( - pending_request: &'a PendingRequest, + pending_transaction: &'a PendingTransaction, rand_nonce: RandValue, identity_client: &'a mut IdentityClient, ) -> ResponseSendFunds { let u_response_send_funds = ResponseSendFunds { - request_id: pending_request.request_id, + request_id: pending_transaction.request_id, rand_nonce, signature: (), }; - let signature_buff = create_response_signature_buffer(&u_response_send_funds, pending_request); + let signature_buff = create_response_signature_buffer(&u_response_send_funds, pending_transaction); let signature = await!(identity_client.request_signature(signature_buff)).unwrap(); ResponseSendFunds { @@ -69,19 +69,19 @@ pub async fn create_response_send_funds<'a>( } pub async fn create_failure_send_funds<'a>( - pending_request: &'a PendingRequest, + pending_transaction: &'a PendingTransaction, local_public_key: &'a PublicKey, rand_nonce: RandValue, identity_client: &'a mut IdentityClient, ) -> FailureSendFunds { let u_failure_send_funds = FailureSendFunds { - request_id: pending_request.request_id, + request_id: pending_transaction.request_id, reporting_public_key: local_public_key.clone(), rand_nonce, signature: (), }; - let signature_buff = create_failure_signature_buffer(&u_failure_send_funds, pending_request); + let signature_buff = create_failure_signature_buffer(&u_failure_send_funds, pending_transaction); let signature = await!(identity_client.request_signature(signature_buff)).unwrap(); FailureSendFunds { @@ -107,8 +107,8 @@ pub enum UnsignedFriendTcOp { /// Keep information from a RequestSendFunds message. /// This information will be used later to deal with a corresponding {Response,Failure}SendFunds messages, /// as those messages do not repeat the information sent in the request. -pub fn create_pending_request(request_send_funds: &RequestSendFunds) -> PendingRequest { - PendingRequest { +pub fn create_pending_transaction(request_send_funds: &RequestSendFunds) -> PendingTransaction { + PendingTransaction { request_id: request_send_funds.request_id, route: request_send_funds.route.clone(), dest_payment: request_send_funds.dest_payment, diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index be7635b60..d4f58b57a 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -62,6 +62,7 @@ pub struct FriendsRoute { #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] pub struct RequestSendFunds { pub request_id: Uid, + pub src_hashed_lock: HashResult, // TODO: Change to bcrypt later pub route: FriendsRoute, pub dest_payment: u128, pub invoice_id: InvoiceId, @@ -146,12 +147,22 @@ pub struct Receipt { // ) } +#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)] +pub enum TransactionStage { + Request, + Response(HashResult), // inner: dest_hashed_lock. TOOD: Change to use bcrypt +} + #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] -pub struct PendingRequest { +pub struct PendingTransaction { pub request_id: Uid, pub route: FriendsRoute, pub dest_payment: u128, pub invoice_id: InvoiceId, + /// Amount of credits taken for forwarding the transaction + pub fee: u128, + pub src_hashed_lock: HashResult, + pub stage: TransactionStage, } // ================================================================== @@ -435,6 +446,7 @@ impl FunderIncomingControl { } impl UserRequestSendFunds { + /* pub fn into_request(self) -> RequestSendFunds { RequestSendFunds { request_id: self.request_id, @@ -444,14 +456,15 @@ impl UserRequestSendFunds { } } - pub fn create_pending_request(&self) -> PendingRequest { - PendingRequest { + pub fn create_pending_transaction(&self) -> PendingTransaction { + PendingTransaction { request_id: self.request_id, route: self.route.clone(), dest_payment: self.dest_payment, invoice_id: self.invoice_id.clone(), } } + */ } #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/components/proto/src/funder/mod.rs b/components/proto/src/funder/mod.rs index 38167ceeb..0600ab16c 100644 --- a/components/proto/src/funder/mod.rs +++ b/components/proto/src/funder/mod.rs @@ -1,4 +1,5 @@ pub mod messages; +#[allow(unused)] pub mod serialize; pub mod signature_buff; // pub mod scheme; diff --git a/components/proto/src/funder/serialize.rs b/components/proto/src/funder/serialize.rs index adc24d168..47cc7cffb 100644 --- a/components/proto/src/funder/serialize.rs +++ b/components/proto/src/funder/serialize.rs @@ -1,8 +1,8 @@ use crate::capnp_common::{ - read_custom_int128, read_custom_u_int128, read_invoice_id, read_public_key, read_rand_nonce, - read_relay_address, read_signature, read_uid, write_custom_int128, write_custom_u_int128, - write_invoice_id, write_public_key, write_rand_nonce, write_relay_address, write_signature, - write_uid, + read_custom_int128, read_custom_u_int128, /*read_invoice_id,*/ read_public_key, + read_rand_nonce, read_relay_address, read_signature, read_uid, write_custom_int128, + write_custom_u_int128, write_invoice_id, write_public_key, write_rand_nonce, + write_relay_address, write_signature, write_uid, }; use capnp; use capnp::serialize_packed; @@ -278,12 +278,15 @@ pub fn deser_friends_route( fn deser_request_send_funds_op( request_send_funds_op_reader: &funder_capnp::request_send_funds_op::Reader, ) -> Result { + unimplemented!(); + /* Ok(RequestSendFunds { request_id: read_uid(&request_send_funds_op_reader.get_request_id()?)?, route: deser_friends_route(&request_send_funds_op_reader.get_route()?)?, dest_payment: read_custom_u_int128(&request_send_funds_op_reader.get_dest_payment()?)?, invoice_id: read_invoice_id(&request_send_funds_op_reader.get_invoice_id()?)?, }) + */ } fn deser_response_send_funds_op( diff --git a/components/proto/src/funder/signature_buff.rs b/components/proto/src/funder/signature_buff.rs index 40bdd2988..62cb8b2f8 100644 --- a/components/proto/src/funder/signature_buff.rs +++ b/components/proto/src/funder/signature_buff.rs @@ -5,7 +5,9 @@ use crypto::identity::{verify_signature, PublicKey}; use common::canonical_serialize::CanonicalSerialize; use common::int_convert::usize_to_u64; -use super::messages::{FailureSendFunds, MoveToken, PendingRequest, Receipt, ResponseSendFunds}; +use super::messages::{ + FailureSendFunds, MoveToken, PendingTransaction, Receipt, ResponseSendFunds, +}; pub const FUND_SUCCESS_PREFIX: &[u8] = b"FUND_SUCCESS"; pub const FUND_FAILURE_PREFIX: &[u8] = b"FUND_FAILURE"; @@ -15,21 +17,21 @@ pub const FUND_FAILURE_PREFIX: &[u8] = b"FUND_FAILURE"; /// contains information from the Request funds. pub fn create_response_signature_buffer( response_send_funds: &ResponseSendFunds, - pending_request: &PendingRequest, + pending_transaction: &PendingTransaction, ) -> Vec { let mut sbuffer = Vec::new(); sbuffer.extend_from_slice(&hash::sha_512_256(FUND_SUCCESS_PREFIX)); let mut inner_blob = Vec::new(); - inner_blob.extend_from_slice(&pending_request.request_id); - inner_blob.extend_from_slice(&pending_request.route.hash()); + inner_blob.extend_from_slice(&pending_transaction.request_id); + inner_blob.extend_from_slice(&pending_transaction.route.hash()); inner_blob.extend_from_slice(&response_send_funds.rand_nonce); sbuffer.extend_from_slice(&hash::sha_512_256(&inner_blob)); - sbuffer.extend_from_slice(&pending_request.invoice_id); + sbuffer.extend_from_slice(&pending_transaction.invoice_id); sbuffer - .write_u128::(pending_request.dest_payment) + .write_u128::(pending_transaction.dest_payment) .unwrap(); sbuffer @@ -42,18 +44,18 @@ pub fn create_response_signature_buffer( /// contains information from the Request funds. pub fn create_failure_signature_buffer( failure_send_funds: &FailureSendFunds, - pending_request: &PendingRequest, + pending_transaction: &PendingTransaction, ) -> Vec { let mut sbuffer = Vec::new(); sbuffer.extend_from_slice(&hash::sha_512_256(FUND_FAILURE_PREFIX)); - sbuffer.extend_from_slice(&pending_request.request_id); - sbuffer.extend_from_slice(&pending_request.route.hash()); + sbuffer.extend_from_slice(&pending_transaction.request_id); + sbuffer.extend_from_slice(&pending_transaction.route.hash()); sbuffer - .write_u128::(pending_request.dest_payment) + .write_u128::(pending_transaction.dest_payment) .unwrap(); - sbuffer.extend_from_slice(&pending_request.invoice_id); + sbuffer.extend_from_slice(&pending_transaction.invoice_id); sbuffer.extend_from_slice(&failure_send_funds.reporting_public_key); sbuffer.extend_from_slice(&failure_send_funds.rand_nonce); @@ -63,14 +65,16 @@ pub fn create_failure_signature_buffer( /// Verify a failure signature pub fn verify_failure_signature( failure_send_funds: &FailureSendFunds, - pending_request: &PendingRequest, + pending_transaction: &PendingTransaction, ) -> Option<()> { let failure_signature_buffer = - create_failure_signature_buffer(&failure_send_funds, &pending_request); + create_failure_signature_buffer(&failure_send_funds, &pending_transaction); let reporting_public_key = &failure_send_funds.reporting_public_key; // Make sure that the reporting_public_key is on the route: // TODO: Should we check that it is after us? Is it checked somewhere else? - let _ = pending_request.route.pk_to_index(&reporting_public_key)?; + let _ = pending_transaction + .route + .pk_to_index(&reporting_public_key)?; if !verify_signature( &failure_signature_buffer, @@ -84,19 +88,19 @@ pub fn verify_failure_signature( pub fn prepare_receipt( response_send_funds: &ResponseSendFunds, - pending_request: &PendingRequest, + pending_transaction: &PendingTransaction, ) -> Receipt { let mut hash_buff = Vec::new(); - hash_buff.extend_from_slice(&pending_request.request_id); - hash_buff.extend_from_slice(&pending_request.route.hash()); + hash_buff.extend_from_slice(&pending_transaction.request_id); + hash_buff.extend_from_slice(&pending_transaction.route.hash()); hash_buff.extend_from_slice(&response_send_funds.rand_nonce); let response_hash = hash::sha_512_256(&hash_buff); // = sha512/256(requestId || sha512/256(route) || randNonce) Receipt { response_hash, - invoice_id: pending_request.invoice_id.clone(), - dest_payment: pending_request.dest_payment, + invoice_id: pending_transaction.invoice_id.clone(), + dest_payment: pending_transaction.dest_payment, signature: response_send_funds.signature.clone(), } } From d7426d68fc008bd4e73a3f0bfe086c1af33b1d23 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 12:18:34 +0300 Subject: [PATCH 003/248] Ranamed FailureSendFunds -> CancelSendFunds. --- components/proto/src/funder/messages.rs | 11 +-- components/proto/src/funder/serialize.rs | 68 +++++++++++-------- components/proto/src/funder/signature_buff.rs | 8 +-- 3 files changed, 49 insertions(+), 38 deletions(-) diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index d4f58b57a..3c39da355 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -71,12 +71,13 @@ pub struct RequestSendFunds { #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] pub struct ResponseSendFunds { pub request_id: Uid, + pub dest_hashed_lock: HashResult, pub rand_nonce: RandValue, pub signature: S, } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] -pub struct FailureSendFunds { +pub struct CancelSendFunds { pub request_id: Uid, pub reporting_public_key: PublicKey, pub rand_nonce: RandValue, @@ -90,7 +91,7 @@ pub enum FriendTcOp { SetRemoteMaxDebt(u128), RequestSendFunds(RequestSendFunds), ResponseSendFunds(ResponseSendFunds), - FailureSendFunds(FailureSendFunds), + CancelSendFunds(CancelSendFunds), } #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] @@ -190,7 +191,7 @@ impl CanonicalSerialize for ResponseSendFunds { } } -impl CanonicalSerialize for FailureSendFunds { +impl CanonicalSerialize for CancelSendFunds { fn canonical_serialize(&self) -> Vec { let mut res_bytes = Vec::new(); res_bytes.extend_from_slice(&self.request_id); @@ -222,9 +223,9 @@ impl CanonicalSerialize for FriendTcOp { res_bytes.push(4u8); res_bytes.append(&mut response_send_funds.canonical_serialize()) } - FriendTcOp::FailureSendFunds(failure_send_funds) => { + FriendTcOp::CancelSendFunds(cancel_send_funds) => { res_bytes.push(5u8); - res_bytes.append(&mut failure_send_funds.canonical_serialize()) + res_bytes.append(&mut cancel_send_funds.canonical_serialize()) } } res_bytes diff --git a/components/proto/src/funder/serialize.rs b/components/proto/src/funder/serialize.rs index 47cc7cffb..1d838823a 100644 --- a/components/proto/src/funder/serialize.rs +++ b/components/proto/src/funder/serialize.rs @@ -12,7 +12,7 @@ use std::io; use funder_capnp; use super::messages::{ - FailureSendFunds, FriendMessage, FriendTcOp, FriendsRoute, MoveToken, MoveTokenRequest, + CancelSendFunds, FriendMessage, FriendTcOp, FriendsRoute, MoveToken, MoveTokenRequest, RequestSendFunds, ResetTerms, ResponseSendFunds, }; @@ -76,34 +76,39 @@ fn ser_response_send_funds_op( ); } -fn ser_failure_send_funds_op( - failure_send_funds: &FailureSendFunds, - failure_send_funds_op_builder: &mut funder_capnp::failure_send_funds_op::Builder, +fn ser_cancel_send_funds_op( + cancel_send_funds: &CancelSendFunds, + cancel_send_funds_op_builder: &mut funder_capnp::failure_send_funds_op::Builder, ) { + unimplemented!(); + /* write_uid( - &failure_send_funds.request_id, - &mut failure_send_funds_op_builder.reborrow().init_request_id(), + &cancel_send_funds.request_id, + &mut cancel_send_funds_op_builder.reborrow().init_request_id(), ); write_public_key( - &failure_send_funds.reporting_public_key, - &mut failure_send_funds_op_builder + &cancel_send_funds.reporting_public_key, + &mut cancel_send_funds_op_builder .reborrow() .init_reporting_public_key(), ); write_rand_nonce( - &failure_send_funds.rand_nonce, - &mut failure_send_funds_op_builder.reborrow().init_rand_nonce(), + &cancel_send_funds.rand_nonce, + &mut cancel_send_funds_op_builder.reborrow().init_rand_nonce(), ); write_signature( - &failure_send_funds.signature, - &mut failure_send_funds_op_builder.reborrow().init_signature(), + &cancel_send_funds.signature, + &mut cancel_send_funds_op_builder.reborrow().init_signature(), ); + */ } fn ser_friend_operation( operation: &FriendTcOp, operation_builder: &mut funder_capnp::friend_operation::Builder, ) { + unimplemented!(); + /* match operation { FriendTcOp::EnableRequests => operation_builder.set_enable_requests(()), FriendTcOp::DisableRequests => operation_builder.set_disable_requests(()), @@ -122,12 +127,13 @@ fn ser_friend_operation( operation_builder.reborrow().init_response_send_funds(); ser_response_send_funds_op(response_send_funds, &mut response_send_funds_builder); } - FriendTcOp::FailureSendFunds(failure_send_funds) => { - let mut failure_send_funds_builder = - operation_builder.reborrow().init_failure_send_funds(); - ser_failure_send_funds_op(failure_send_funds, &mut failure_send_funds_builder); + FriendTcOp::CancelSendFunds(cancel_send_funds) => { + let mut cancel_send_funds_builder = + operation_builder.reborrow().init_cancel_send_funds(); + ser_cancel_send_funds_op(cancel_send_funds, &mut cancel_send_funds_builder); } }; + */ } fn ser_move_token( @@ -292,29 +298,34 @@ fn deser_request_send_funds_op( fn deser_response_send_funds_op( response_send_funds_op_reader: &funder_capnp::response_send_funds_op::Reader, ) -> Result { + unimplemented!(); + /* Ok(ResponseSendFunds { request_id: read_uid(&response_send_funds_op_reader.get_request_id()?)?, rand_nonce: read_rand_nonce(&response_send_funds_op_reader.get_rand_nonce()?)?, signature: read_signature(&response_send_funds_op_reader.get_signature()?)?, }) + */ } -fn deser_failure_send_funds_op( - failure_send_funds_op_reader: &funder_capnp::failure_send_funds_op::Reader, -) -> Result { - Ok(FailureSendFunds { - request_id: read_uid(&failure_send_funds_op_reader.get_request_id()?)?, +fn deser_cancel_send_funds_op( + cancel_send_funds_op_reader: &funder_capnp::failure_send_funds_op::Reader, +) -> Result { + Ok(CancelSendFunds { + request_id: read_uid(&cancel_send_funds_op_reader.get_request_id()?)?, reporting_public_key: read_public_key( - &failure_send_funds_op_reader.get_reporting_public_key()?, + &cancel_send_funds_op_reader.get_reporting_public_key()?, )?, - rand_nonce: read_rand_nonce(&failure_send_funds_op_reader.get_rand_nonce()?)?, - signature: read_signature(&failure_send_funds_op_reader.get_signature()?)?, + rand_nonce: read_rand_nonce(&cancel_send_funds_op_reader.get_rand_nonce()?)?, + signature: read_signature(&cancel_send_funds_op_reader.get_signature()?)?, }) } fn deser_friend_operation( friend_operation_reader: &funder_capnp::friend_operation::Reader, ) -> Result { + unimplemented!(); + /* Ok(match friend_operation_reader.which()? { funder_capnp::friend_operation::EnableRequests(()) => FriendTcOp::EnableRequests, funder_capnp::friend_operation::DisableRequests(()) => FriendTcOp::DisableRequests, @@ -329,10 +340,11 @@ fn deser_friend_operation( &response_send_funds_reader?, )?) } - funder_capnp::friend_operation::FailureSendFunds(failure_send_funds_reader) => { - FriendTcOp::FailureSendFunds(deser_failure_send_funds_op(&failure_send_funds_reader?)?) + funder_capnp::friend_operation::CancelSendFunds(cancel_send_funds_reader) => { + FriendTcOp::CancelSendFunds(deser_cancel_send_funds_op(&cancel_send_funds_reader?)?) } }) + */ } fn deser_move_token( @@ -453,7 +465,7 @@ mod tests { signature: Signature::from(&[3; SIGNATURE_LEN]), }; - let failure_send_funds = FailureSendFunds { + let cancel_send_funds = CancelSendFunds { request_id: Uid::from(&[10; UID_LEN]), reporting_public_key: PublicKey::from(&[0x11; PUBLIC_KEY_LEN]), rand_nonce: RandValue::from(&[0xbb; RAND_VALUE_LEN]), @@ -466,7 +478,7 @@ mod tests { FriendTcOp::SetRemoteMaxDebt(101), FriendTcOp::RequestSendFunds(request_send_funds), FriendTcOp::ResponseSendFunds(response_send_funds), - FriendTcOp::FailureSendFunds(failure_send_funds), + FriendTcOp::CancelSendFunds(cancel_send_funds), ]; let relay_address4 = RelayAddress { diff --git a/components/proto/src/funder/signature_buff.rs b/components/proto/src/funder/signature_buff.rs index 62cb8b2f8..3c9636951 100644 --- a/components/proto/src/funder/signature_buff.rs +++ b/components/proto/src/funder/signature_buff.rs @@ -5,9 +5,7 @@ use crypto::identity::{verify_signature, PublicKey}; use common::canonical_serialize::CanonicalSerialize; use common::int_convert::usize_to_u64; -use super::messages::{ - FailureSendFunds, MoveToken, PendingTransaction, Receipt, ResponseSendFunds, -}; +use super::messages::{CancelSendFunds, MoveToken, PendingTransaction, Receipt, ResponseSendFunds}; pub const FUND_SUCCESS_PREFIX: &[u8] = b"FUND_SUCCESS"; pub const FUND_FAILURE_PREFIX: &[u8] = b"FUND_FAILURE"; @@ -43,7 +41,7 @@ pub fn create_response_signature_buffer( /// Note that the signature is not just over the Response funds bytes. The signed buffer also /// contains information from the Request funds. pub fn create_failure_signature_buffer( - failure_send_funds: &FailureSendFunds, + failure_send_funds: &CancelSendFunds, pending_transaction: &PendingTransaction, ) -> Vec { let mut sbuffer = Vec::new(); @@ -64,7 +62,7 @@ pub fn create_failure_signature_buffer( /// Verify a failure signature pub fn verify_failure_signature( - failure_send_funds: &FailureSendFunds, + failure_send_funds: &CancelSendFunds, pending_transaction: &PendingTransaction, ) -> Option<()> { let failure_signature_buffer = From db584633ff1b945072b730b9231f724513e25bc7 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 12:29:10 +0300 Subject: [PATCH 004/248] Added a basic hash lock (Based of sha512/256). --- components/crypto/src/hash_lock.rs | 31 ++++++++++++++++++++++++++++++ components/crypto/src/lib.rs | 1 + 2 files changed, 32 insertions(+) create mode 100644 components/crypto/src/hash_lock.rs diff --git a/components/crypto/src/hash_lock.rs b/components/crypto/src/hash_lock.rs new file mode 100644 index 000000000..703217a7e --- /dev/null +++ b/components/crypto/src/hash_lock.rs @@ -0,0 +1,31 @@ +use ring::digest::{digest, SHA512_256}; + +// TODO: Use bcrypt instead here + +pub const PLAIN_LOCK_LEN: usize = 32; +pub const HASHED_LOCK_LEN: usize = 32; + +define_fixed_bytes!(PlainLock, PLAIN_LOCK_LEN); +define_fixed_bytes!(HashedLock, HASHED_LOCK_LEN); + +impl PlainLock { + pub fn hash(&self) -> HashedLock { + let mut inner = [0x00; HASHED_LOCK_LEN]; + let digest_res = digest(&SHA512_256, &self.0); + inner.copy_from_slice(digest_res.as_ref()); + HashedLock(inner) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_hash_lock_basic() { + let plain_lock = PlainLock::from(&[1u8; PLAIN_LOCK_LEN]); + let hashed_lock1 = plain_lock.hash(); + let hashed_lock2 = plain_lock.hash(); + assert_eq!(hashed_lock1, hashed_lock2); + } +} diff --git a/components/crypto/src/lib.rs b/components/crypto/src/lib.rs index cf3df2eac..6f33f042e 100644 --- a/components/crypto/src/lib.rs +++ b/components/crypto/src/lib.rs @@ -15,6 +15,7 @@ extern crate serde_derive; pub mod crypto_rand; pub mod dh; pub mod hash; +pub mod hash_lock; pub mod identity; pub mod invoice_id; pub mod nonce_window; From 114d0d4ce103fcb2a39d5037d76ff485bd5186b6 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 12:58:39 +0300 Subject: [PATCH 005/248] Renamed Confirmation -> MultiConfirmation. --- doc/docs/atomic_proposal.md | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/doc/docs/atomic_proposal.md b/doc/docs/atomic_proposal.md index aa85a6558..9e27a073e 100644 --- a/doc/docs/atomic_proposal.md +++ b/doc/docs/atomic_proposal.md @@ -414,7 +414,7 @@ the buyer, and send back (along the same route) a Commit message to collect his credits. ```capnp -struct RequestConfirmation { +struct Confirmation { responseHash @0: Hash; # = sha512/256(requestId || sha512/256(route) || randNonce) destPayment @1: CustomUInt128; @@ -432,26 +432,26 @@ struct RequestConfirmation { # ) } -struct Confirmation { +struct MultiConfirmation { invoiceId @0: InvoiceId; # InvoiceId being paid. - requestConfirmations @1: List(RequestConfirmation); + confirmations @1: List(Confirmation); # A list of confirmations. Each confirmation corresponds to a request # sent along one route. } ``` -Note that the `Confirmation` message may contain multiple -`RequestConfirmation`, each corresponding to one request. This allows +Note that the `MultiConfirmation` message may contain multiple +`Confirmation`-s, each corresponding to one request. This allows fragmented payment along multiple routes. -Verification of a Confirmation message by the seller is done as follows: +Verification of a MultiConfirmation message by the seller is done as follows: - InvoiceId matches an originally issued invoice. -- For every RequestConfirmation: - - The revealed lock is valid for every RequestConfirmation: +- For every Confirmation: + - The revealed lock is valid: `bcrypt(srcPlainLock) == srcHashedLock` - - signature is valid for all RequestConfirmations. + - signature is valid. - Total of `destPayment` is correct (Equal the requested amount at the invoice). @@ -531,7 +531,7 @@ Assume that the node E issued an invoice and handed it to B. B wants to pay the invoice. The payment begins by sending a Request message along the path from B to E. The payment is considered successful when B hands a -Confirmation message to E. +MultiConfirmation message to E. This means that we should examine the possibility of B waiting indefinitely during the sending of Request and Response messages along the route. @@ -539,11 +539,11 @@ during the sending of Request and Response messages along the route. During this time (Request + Response period), B can discard the transaction by walking away. E will not be able to make progress because in order to send the Commit message, the correct srcPlainLock is required, but E does not know it -before B sends the Confirmation message. +before B sends the MultiConfirmation message. -Also note that if B sends a valid Confirmation message to E, the transaction is +Also note that if B sends a valid MultiConfirmation message to E, the transaction is considered successful, and B can not reverse it. This happens because B reveals -srcPlainLock at the Confirmation message sent to E. +srcPlainLock at the MultiConfirmation message sent to E. ## Receipt verifiability @@ -613,9 +613,9 @@ This is done as follows: 3. A ResponseSendFundsOp or a CancelSendFundsOp message is returned. 4. Go back to (2) until the wanted amount of credits is acheived (for paying the invoice). -5. Buyer sends a Confirmation message containing a list of all - RequestConfirmation-s for all the requests that got a valid response. -6. Seller verifies the Confirmation message. If valid, the payment is accepted +5. Buyer sends a MultiConfirmation message containing a list of all + Confirmation-s for all the requests that got a valid response. +6. Seller verifies the MultiConfirmation message. If valid, the payment is accepted and the goods are handed to the buyer. 7. The Seller sends back CommitOp messages for all requests. 8. Any CommitOp message can be used to create a valid Receipt. (Two diferent From 441513f7979e7eb742600f50d7e66cb09efa8000 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 12:58:56 +0300 Subject: [PATCH 006/248] Added Op prefix to names of operations. --- components/funder/src/mutual_credit/types.rs | 4 +-- components/proto/src/funder/messages.rs | 27 ++++++++++--------- components/proto/src/funder/serialize.rs | 24 ++++++++--------- components/proto/src/funder/signature_buff.rs | 12 +++++---- 4 files changed, 35 insertions(+), 32 deletions(-) diff --git a/components/funder/src/mutual_credit/types.rs b/components/funder/src/mutual_credit/types.rs index dd43a22d4..018fc7d2a 100644 --- a/components/funder/src/mutual_credit/types.rs +++ b/components/funder/src/mutual_credit/types.rs @@ -7,7 +7,7 @@ use crypto::identity::PublicKey; use crypto::uid::Uid; use proto::funder::messages::{ - PendingTransaction, RequestSendFunds, RequestsStatus, TransactionStage, + PendingTransaction, RequestSendFundsOp, RequestsStatus, TransactionStage, }; /// The maximum possible funder debt. @@ -296,7 +296,7 @@ impl MutualCredit { /// as those messages do not repeat the information sent in the request. fn create_pending_transaction( &self, - request_send_funds: &RequestSendFunds, + request_send_funds: &RequestSendFundsOp, ) -> Result { Ok(PendingTransaction { request_id: request_send_funds.request_id, diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 3c39da355..36655cd67 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -3,6 +3,7 @@ use std::collections::HashSet; use crypto::crypto_rand::RandValue; use crypto::hash::{self, HashResult}; +use crypto::hash_lock::HashedLock; use crypto::identity::{PublicKey, Signature}; use crypto::invoice_id::InvoiceId; use crypto::uid::Uid; @@ -60,24 +61,24 @@ pub struct FriendsRoute { } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] -pub struct RequestSendFunds { +pub struct RequestSendFundsOp { pub request_id: Uid, - pub src_hashed_lock: HashResult, // TODO: Change to bcrypt later + pub src_hashed_lock: HashedLock, // TODO: Change to bcrypt later pub route: FriendsRoute, pub dest_payment: u128, pub invoice_id: InvoiceId, } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] -pub struct ResponseSendFunds { +pub struct ResponseSendFundsOp { pub request_id: Uid, - pub dest_hashed_lock: HashResult, + pub dest_hashed_lock: HashedLock, pub rand_nonce: RandValue, pub signature: S, } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] -pub struct CancelSendFunds { +pub struct CancelSendFundsOp { pub request_id: Uid, pub reporting_public_key: PublicKey, pub rand_nonce: RandValue, @@ -89,9 +90,9 @@ pub enum FriendTcOp { EnableRequests, DisableRequests, SetRemoteMaxDebt(u128), - RequestSendFunds(RequestSendFunds), - ResponseSendFunds(ResponseSendFunds), - CancelSendFunds(CancelSendFunds), + RequestSendFunds(RequestSendFundsOp), + ResponseSendFunds(ResponseSendFundsOp), + CancelSendFunds(CancelSendFundsOp), } #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] @@ -151,7 +152,7 @@ pub struct Receipt { #[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)] pub enum TransactionStage { Request, - Response(HashResult), // inner: dest_hashed_lock. TOOD: Change to use bcrypt + Response(HashedLock), // inner: dest_hashed_lock. TOOD: Change to use bcrypt } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] @@ -162,14 +163,14 @@ pub struct PendingTransaction { pub invoice_id: InvoiceId, /// Amount of credits taken for forwarding the transaction pub fee: u128, - pub src_hashed_lock: HashResult, + pub src_hashed_lock: HashedLock, pub stage: TransactionStage, } // ================================================================== // ================================================================== -impl CanonicalSerialize for RequestSendFunds { +impl CanonicalSerialize for RequestSendFundsOp { fn canonical_serialize(&self) -> Vec { let mut res_bytes = Vec::new(); res_bytes.extend_from_slice(&self.request_id); @@ -181,7 +182,7 @@ impl CanonicalSerialize for RequestSendFunds { } } -impl CanonicalSerialize for ResponseSendFunds { +impl CanonicalSerialize for ResponseSendFundsOp { fn canonical_serialize(&self) -> Vec { let mut res_bytes = Vec::new(); res_bytes.extend_from_slice(&self.request_id); @@ -191,7 +192,7 @@ impl CanonicalSerialize for ResponseSendFunds { } } -impl CanonicalSerialize for CancelSendFunds { +impl CanonicalSerialize for CancelSendFundsOp { fn canonical_serialize(&self) -> Vec { let mut res_bytes = Vec::new(); res_bytes.extend_from_slice(&self.request_id); diff --git a/components/proto/src/funder/serialize.rs b/components/proto/src/funder/serialize.rs index 1d838823a..4548226a4 100644 --- a/components/proto/src/funder/serialize.rs +++ b/components/proto/src/funder/serialize.rs @@ -12,8 +12,8 @@ use std::io; use funder_capnp; use super::messages::{ - CancelSendFunds, FriendMessage, FriendTcOp, FriendsRoute, MoveToken, MoveTokenRequest, - RequestSendFunds, ResetTerms, ResponseSendFunds, + CancelSendFundsOp, FriendMessage, FriendTcOp, FriendsRoute, MoveToken, MoveTokenRequest, + RequestSendFundsOp, ResetTerms, ResponseSendFundsOp, }; use crate::serialize::SerializeError; @@ -36,7 +36,7 @@ pub fn ser_friends_route( } fn ser_request_send_funds_op( - request_send_funds: &RequestSendFunds, + request_send_funds: &RequestSendFundsOp, request_send_funds_op_builder: &mut funder_capnp::request_send_funds_op::Builder, ) { write_uid( @@ -59,7 +59,7 @@ fn ser_request_send_funds_op( } fn ser_response_send_funds_op( - response_send_funds: &ResponseSendFunds, + response_send_funds: &ResponseSendFundsOp, response_send_funds_op_builder: &mut funder_capnp::response_send_funds_op::Builder, ) { write_uid( @@ -77,7 +77,7 @@ fn ser_response_send_funds_op( } fn ser_cancel_send_funds_op( - cancel_send_funds: &CancelSendFunds, + cancel_send_funds: &CancelSendFundsOp, cancel_send_funds_op_builder: &mut funder_capnp::failure_send_funds_op::Builder, ) { unimplemented!(); @@ -283,7 +283,7 @@ pub fn deser_friends_route( fn deser_request_send_funds_op( request_send_funds_op_reader: &funder_capnp::request_send_funds_op::Reader, -) -> Result { +) -> Result { unimplemented!(); /* Ok(RequestSendFunds { @@ -297,7 +297,7 @@ fn deser_request_send_funds_op( fn deser_response_send_funds_op( response_send_funds_op_reader: &funder_capnp::response_send_funds_op::Reader, -) -> Result { +) -> Result { unimplemented!(); /* Ok(ResponseSendFunds { @@ -310,8 +310,8 @@ fn deser_response_send_funds_op( fn deser_cancel_send_funds_op( cancel_send_funds_op_reader: &funder_capnp::failure_send_funds_op::Reader, -) -> Result { - Ok(CancelSendFunds { +) -> Result { + Ok(CancelSendFundsOp { request_id: read_uid(&cancel_send_funds_op_reader.get_request_id()?)?, reporting_public_key: read_public_key( &cancel_send_funds_op_reader.get_reporting_public_key()?, @@ -453,19 +453,19 @@ mod tests { ], }; - let request_send_funds = RequestSendFunds { + let request_send_funds = RequestSendFundsOp { request_id: Uid::from(&[22; UID_LEN]), route, dest_payment: 48, invoice_id: InvoiceId::from(&[0x99; INVOICE_ID_LEN]), }; - let response_send_funds = ResponseSendFunds { + let response_send_funds = ResponseSendFundsOp { request_id: Uid::from(&[10; UID_LEN]), rand_nonce: RandValue::from(&[0xbb; RAND_VALUE_LEN]), signature: Signature::from(&[3; SIGNATURE_LEN]), }; - let cancel_send_funds = CancelSendFunds { + let cancel_send_funds = CancelSendFundsOp { request_id: Uid::from(&[10; UID_LEN]), reporting_public_key: PublicKey::from(&[0x11; PUBLIC_KEY_LEN]), rand_nonce: RandValue::from(&[0xbb; RAND_VALUE_LEN]), diff --git a/components/proto/src/funder/signature_buff.rs b/components/proto/src/funder/signature_buff.rs index 3c9636951..739d63c66 100644 --- a/components/proto/src/funder/signature_buff.rs +++ b/components/proto/src/funder/signature_buff.rs @@ -5,7 +5,9 @@ use crypto::identity::{verify_signature, PublicKey}; use common::canonical_serialize::CanonicalSerialize; use common::int_convert::usize_to_u64; -use super::messages::{CancelSendFunds, MoveToken, PendingTransaction, Receipt, ResponseSendFunds}; +use super::messages::{ + CancelSendFundsOp, MoveToken, PendingTransaction, Receipt, ResponseSendFundsOp, +}; pub const FUND_SUCCESS_PREFIX: &[u8] = b"FUND_SUCCESS"; pub const FUND_FAILURE_PREFIX: &[u8] = b"FUND_FAILURE"; @@ -14,7 +16,7 @@ pub const FUND_FAILURE_PREFIX: &[u8] = b"FUND_FAILURE"; /// Note that the signature is not just over the Response funds bytes. The signed buffer also /// contains information from the Request funds. pub fn create_response_signature_buffer( - response_send_funds: &ResponseSendFunds, + response_send_funds: &ResponseSendFundsOp, pending_transaction: &PendingTransaction, ) -> Vec { let mut sbuffer = Vec::new(); @@ -41,7 +43,7 @@ pub fn create_response_signature_buffer( /// Note that the signature is not just over the Response funds bytes. The signed buffer also /// contains information from the Request funds. pub fn create_failure_signature_buffer( - failure_send_funds: &CancelSendFunds, + failure_send_funds: &CancelSendFundsOp, pending_transaction: &PendingTransaction, ) -> Vec { let mut sbuffer = Vec::new(); @@ -62,7 +64,7 @@ pub fn create_failure_signature_buffer( /// Verify a failure signature pub fn verify_failure_signature( - failure_send_funds: &CancelSendFunds, + failure_send_funds: &CancelSendFundsOp, pending_transaction: &PendingTransaction, ) -> Option<()> { let failure_signature_buffer = @@ -85,7 +87,7 @@ pub fn verify_failure_signature( } pub fn prepare_receipt( - response_send_funds: &ResponseSendFunds, + response_send_funds: &ResponseSendFundsOp, pending_transaction: &PendingTransaction, ) -> Receipt { let mut hash_buff = Vec::new(); From 239e13d95fbf7d36f80d60020042180eaa767947 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 13:05:16 +0300 Subject: [PATCH 007/248] Added Confirmation, MultiConfirmation, CommitOp. --- components/proto/src/funder/messages.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 36655cd67..4809e38ab 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -3,7 +3,7 @@ use std::collections::HashSet; use crypto::crypto_rand::RandValue; use crypto::hash::{self, HashResult}; -use crypto::hash_lock::HashedLock; +use crypto::hash_lock::{HashedLock, PlainLock}; use crypto::identity::{PublicKey, Signature}; use crypto::invoice_id::InvoiceId; use crypto::uid::Uid; @@ -85,6 +85,27 @@ pub struct CancelSendFundsOp { pub signature: S, } +#[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] +pub struct Confirmation { + response_hash: HashResult, + dest_payment: u128, + src_plain_lock: PlainLock, + signature: S, +} + +#[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] +pub struct MultiConfirmation { + invoice_id: InvoiceId, + confirmations: Vec>, +} + +#[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] +pub struct CommitOp { + pub request_id: Uid, + pub src_plain_lock: PlainLock, + pub dest_plain_lock: PlainLock, +} + #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] pub enum FriendTcOp { EnableRequests, From c1f5c70dfef228457bfe0299bc21b0aad40a0606 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 13:14:29 +0300 Subject: [PATCH 008/248] Added messages to FriendTcOp. --- components/proto/src/funder/messages.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 4809e38ab..ca15ef190 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -63,10 +63,11 @@ pub struct FriendsRoute { #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] pub struct RequestSendFundsOp { pub request_id: Uid, - pub src_hashed_lock: HashedLock, // TODO: Change to bcrypt later + pub src_hashed_lock: HashedLock, pub route: FriendsRoute, pub dest_payment: u128, pub invoice_id: InvoiceId, + pub left_fees: u128, } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] @@ -100,7 +101,7 @@ pub struct MultiConfirmation { } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] -pub struct CommitOp { +pub struct CommitSendFundsOp { pub request_id: Uid, pub src_plain_lock: PlainLock, pub dest_plain_lock: PlainLock, @@ -114,6 +115,7 @@ pub enum FriendTcOp { RequestSendFunds(RequestSendFundsOp), ResponseSendFunds(ResponseSendFundsOp), CancelSendFunds(CancelSendFundsOp), + CommitSendFunds(CommitSendFundsOp), } #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] @@ -195,10 +197,13 @@ impl CanonicalSerialize for RequestSendFundsOp { fn canonical_serialize(&self) -> Vec { let mut res_bytes = Vec::new(); res_bytes.extend_from_slice(&self.request_id); + res_bytes.extend_from_slice(&self.src_hashed_lock); res_bytes.extend_from_slice(&self.route.canonical_serialize()); res_bytes .write_u128::(self.dest_payment) .unwrap(); + res_bytes.extend_from_slice(&self.invoice_id); + res_bytes.write_u128::(self.left_fees).unwrap(); res_bytes } } @@ -207,6 +212,7 @@ impl CanonicalSerialize for ResponseSendFundsOp { fn canonical_serialize(&self) -> Vec { let mut res_bytes = Vec::new(); res_bytes.extend_from_slice(&self.request_id); + res_bytes.extend_from_slice(&self.dest_hashed_lock); res_bytes.extend_from_slice(&self.rand_nonce); res_bytes.extend_from_slice(&self.signature); res_bytes @@ -218,11 +224,22 @@ impl CanonicalSerialize for CancelSendFundsOp { let mut res_bytes = Vec::new(); res_bytes.extend_from_slice(&self.request_id); res_bytes.extend_from_slice(&self.reporting_public_key); + res_bytes.extend_from_slice(&self.rand_nonce); res_bytes.extend_from_slice(&self.signature); res_bytes } } +impl CanonicalSerialize for CommitSendFundsOp { + fn canonical_serialize(&self) -> Vec { + let mut res_bytes = Vec::new(); + res_bytes.extend_from_slice(&self.request_id); + res_bytes.extend_from_slice(&self.src_plain_lock); + res_bytes.extend_from_slice(&self.dest_plain_lock); + res_bytes + } +} + impl CanonicalSerialize for FriendTcOp { fn canonical_serialize(&self) -> Vec { let mut res_bytes = Vec::new(); @@ -249,6 +266,10 @@ impl CanonicalSerialize for FriendTcOp { res_bytes.push(5u8); res_bytes.append(&mut cancel_send_funds.canonical_serialize()) } + FriendTcOp::CommitSendFunds(commit_send_funds) => { + res_bytes.push(6u8); + res_bytes.append(&mut commit_send_funds.canonical_serialize()) + } } res_bytes } From e38abad1f63bdaa037364733ac7077e0d81a9deb Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 13:30:29 +0300 Subject: [PATCH 009/248] PendingTransaction.left_fees. --- components/funder/src/mutual_credit/types.rs | 2 ++ components/proto/src/funder/messages.rs | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/components/funder/src/mutual_credit/types.rs b/components/funder/src/mutual_credit/types.rs index 018fc7d2a..b13e53c99 100644 --- a/components/funder/src/mutual_credit/types.rs +++ b/components/funder/src/mutual_credit/types.rs @@ -291,6 +291,7 @@ impl MutualCredit { self.state.balance.local_pending_debt = local_pending_debt; } + /* /// Keep information from a RequestSendFunds message. /// This information will be used later to deal with a corresponding {Response,Failure}SendFunds messages, /// as those messages do not repeat the information sent in the request. @@ -308,4 +309,5 @@ impl MutualCredit { stage: TransactionStage::Request, }) } + */ } diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index ca15ef190..1d16133f9 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -184,8 +184,7 @@ pub struct PendingTransaction { pub route: FriendsRoute, pub dest_payment: u128, pub invoice_id: InvoiceId, - /// Amount of credits taken for forwarding the transaction - pub fee: u128, + pub left_fees: u128, pub src_hashed_lock: HashedLock, pub stage: TransactionStage, } From a7e9b03cba341b85828ca61f3f401ea65e7a814d Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 13:39:34 +0300 Subject: [PATCH 010/248] mutual_credit: incoming outline. --- .../funder/src/mutual_credit/incoming.rs | 68 +++++++++++-------- components/funder/src/mutual_credit/mod.rs | 2 +- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index d3ba76cc4..3c39913bf 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -4,38 +4,32 @@ use common::int_convert::usize_to_u32; use common::safe_arithmetic::SafeSignedArithmetic; use proto::funder::messages::{ - FailureSendFunds, FriendTcOp, PendingTransaction, RequestSendFunds, RequestsStatus, - ResponseSendFunds, + CancelSendFundsOp, CommitSendFundsOp, FriendTcOp, PendingTransaction, RequestSendFundsOp, + RequestsStatus, ResponseSendFundsOp, }; use proto::funder::signature_buff::{create_response_signature_buffer, verify_failure_signature}; use crate::credit_calc::CreditCalculator; -use super::types::{create_pending_transaction, McMutation, MutualCredit, MAX_FUNDER_DEBT}; - -/* -pub struct IncomingRequestSendFunds { - pub request: PendingTransaction, -} -*/ +use super::types::{McMutation, MutualCredit, MAX_FUNDER_DEBT}; #[derive(Debug)] -pub struct IncomingResponseSendFunds { +pub struct IncomingResponseSendFundsOp { pub pending_transaction: PendingTransaction, - pub incoming_response: ResponseSendFunds, + pub incoming_response: ResponseSendFundsOp, } #[derive(Debug)] -pub struct IncomingFailureSendFunds { +pub struct IncomingCancelSendFundsOp { pub pending_transaction: PendingTransaction, - pub incoming_failure: FailureSendFunds, + pub incoming_cancel: CancelSendFundsOp, } #[derive(Debug)] pub enum IncomingMessage { - Request(RequestSendFunds), - Response(IncomingResponseSendFunds), - Failure(IncomingFailureSendFunds), + Request(RequestSendFundsOp), + Response(IncomingResponseSendFundsOp), + Cancel(IncomingCancelSendFundsOp), } /// Resulting tasks to perform after processing an incoming operation. @@ -112,8 +106,11 @@ pub fn process_operation( FriendTcOp::ResponseSendFunds(response_send_funds) => { process_response_send_funds(mutual_credit, response_send_funds) } - FriendTcOp::FailureSendFunds(failure_send_funds) => { - process_failure_send_funds(mutual_credit, failure_send_funds) + FriendTcOp::CancelSendFunds(cancel_send_funds) => { + process_cancel_send_funds(mutual_credit, cancel_send_funds) + } + FriendTcOp::CommitSendFunds(commit_send_funds) => { + process_commit_send_funds(mutual_credit, commit_send_funds) } } } @@ -172,10 +169,10 @@ fn process_set_remote_max_debt( } } -/// Process an incoming RequestSendFunds +/// Process an incoming RequestSendFundsOp fn process_request_send_funds( mutual_credit: &mut MutualCredit, - request_send_funds: RequestSendFunds, + request_send_funds: RequestSendFundsOp, ) -> Result { if !request_send_funds.route.is_valid() { return Err(ProcessOperationError::InvalidRoute); @@ -244,6 +241,9 @@ fn process_request_send_funds( return Err(ProcessOperationError::RequestAlreadyExists); } + unimplemented!(); + + /* // Add pending request funds: let pending_friend_request = create_pending_transaction(&request_send_funds); @@ -262,11 +262,12 @@ fn process_request_send_funds( op_output.mc_mutations.push(tc_mutation); Ok(op_output) + */ } fn process_response_send_funds( mutual_credit: &mut MutualCredit, - response_send_funds: ResponseSendFunds, + response_send_funds: ResponseSendFundsOp, ) -> Result { // Make sure that id exists in local_pending hashmap, // and access saved request details. @@ -344,7 +345,7 @@ fn process_response_send_funds( mutual_credit.mutate(&tc_mutation); mc_mutations.push(tc_mutation); - let incoming_message = Some(IncomingMessage::Response(IncomingResponseSendFunds { + let incoming_message = Some(IncomingMessage::Response(IncomingResponseSendFundsOp { pending_transaction, incoming_response: response_send_funds, })); @@ -355,9 +356,9 @@ fn process_response_send_funds( }) } -fn process_failure_send_funds( +fn process_cancel_send_funds( mutual_credit: &mut MutualCredit, - failure_send_funds: FailureSendFunds, + cancel_send_funds: CancelSendFundsOp, ) -> Result { // Make sure that id exists in local_pending hashmap, // and access saved request details. @@ -368,7 +369,7 @@ fn process_failure_send_funds( // Obtain pending request: let pending_transaction = local_pending_transactions - .get(&failure_send_funds.request_id) + .get(&cancel_send_funds.request_id) .ok_or(ProcessOperationError::RequestDoesNotExist)? .clone(); // TODO: Possibly get rid of clone() here for optimization later @@ -389,14 +390,14 @@ fn process_failure_send_funds( let reporting_index = pending_transaction .route - .pk_to_index(&failure_send_funds.reporting_public_key) + .pk_to_index(&cancel_send_funds.reporting_public_key) .ok_or(ProcessOperationError::ReportingNodeNonexistent)?; if reporting_index <= local_index { return Err(ProcessOperationError::InvalidReportingNode); } - verify_failure_signature(&failure_send_funds, &pending_transaction) + verify_failure_signature(&cancel_send_funds, &pending_transaction) .ok_or(ProcessOperationError::InvalidFailureSignature)?; // At this point we believe the failure funds is valid. @@ -406,7 +407,7 @@ fn process_failure_send_funds( let mut mc_mutations = Vec::new(); // Remove entry from local_pending hashmap: - let tc_mutation = McMutation::RemoveLocalPendingTransaction(failure_send_funds.request_id); + let tc_mutation = McMutation::RemoveLocalPendingTransaction(cancel_send_funds.request_id); mutual_credit.mutate(&tc_mutation); mc_mutations.push(tc_mutation); @@ -441,9 +442,9 @@ fn process_failure_send_funds( mc_mutations.push(tc_mutation); // Return Failure funds. - let incoming_message = Some(IncomingMessage::Failure(IncomingFailureSendFunds { + let incoming_message = Some(IncomingMessage::Cancel(IncomingCancelSendFundsOp { pending_transaction, - incoming_failure: failure_send_funds, + incoming_cancel: cancel_send_funds, })); Ok(ProcessOperationOutput { @@ -451,3 +452,10 @@ fn process_failure_send_funds( mc_mutations, }) } + +fn process_commit_send_funds( + mutual_credit: &mut MutualCredit, + commit_send_funds: CommitSendFundsOp, +) -> Result { + unimplemented!(); +} diff --git a/components/funder/src/mutual_credit/mod.rs b/components/funder/src/mutual_credit/mod.rs index 2d650d21a..09d1f1a70 100644 --- a/components/funder/src/mutual_credit/mod.rs +++ b/components/funder/src/mutual_credit/mod.rs @@ -1,4 +1,4 @@ -// pub mod incoming; +pub mod incoming; // pub mod outgoing; #[cfg(test)] mod tests; From 649f96a646f57b2fb00af86787c83ebc136f8291 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 13:40:49 +0300 Subject: [PATCH 011/248] Added IncomimgMessage::Commit(...). --- components/funder/src/mutual_credit/incoming.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index 3c39913bf..0e300d8d9 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -25,11 +25,18 @@ pub struct IncomingCancelSendFundsOp { pub incoming_cancel: CancelSendFundsOp, } +#[derive(Debug)] +pub struct IncomingCommitSendFundsOp { + pub pending_transaction: PendingTransaction, + pub incoming_commit: CommitSendFundsOp, +} + #[derive(Debug)] pub enum IncomingMessage { Request(RequestSendFundsOp), Response(IncomingResponseSendFundsOp), Cancel(IncomingCancelSendFundsOp), + Commit(IncomingCommitSendFundsOp), } /// Resulting tasks to perform after processing an incoming operation. From 0859e375b21ab9a3b89695887c6cafcefd69d4d1 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 13:46:52 +0300 Subject: [PATCH 012/248] Added SetRate mutation. --- components/funder/src/mutual_credit/types.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/components/funder/src/mutual_credit/types.rs b/components/funder/src/mutual_credit/types.rs index b13e53c99..05135834d 100644 --- a/components/funder/src/mutual_credit/types.rs +++ b/components/funder/src/mutual_credit/types.rs @@ -93,7 +93,7 @@ impl McRequestsStatus { /// Rates for forwarding a transaction /// For a transaction of `x` credits, the amount of fees will be: /// `(x * mul) / 2^32 + add` -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub struct Rate { /// Commission mul: u32, @@ -151,6 +151,7 @@ pub enum McMutation { RemoveRemotePendingTransaction(Uid), SetLocalPendingDebt(u128), SetRemotePendingDebt(u128), + SetRate(Rate), } impl MutualCredit { @@ -224,6 +225,7 @@ impl MutualCredit { McMutation::SetRemotePendingDebt(remote_pending_debt) => { self.set_remote_pending_debt(*remote_pending_debt) } + McMutation::SetRate(rate) => self.set_rate(rate.clone()), } } @@ -291,6 +293,9 @@ impl MutualCredit { self.state.balance.local_pending_debt = local_pending_debt; } + fn set_rate(&mut self, rate: Rate) { + self.state.rate = rate; + } /* /// Keep information from a RequestSendFunds message. /// This information will be used later to deal with a corresponding {Response,Failure}SendFunds messages, From fdee2b4663ace4d59c4171621184d0da1491ae21 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 13:51:00 +0300 Subject: [PATCH 013/248] Removed old comment. --- components/funder/src/mutual_credit/incoming.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index 0e300d8d9..3aa328615 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -235,10 +235,6 @@ fn process_request_send_funds( return Err(ProcessOperationError::InsufficientTrust); } - // Note that Verifying our own freezing link will be done outside. We don't have enough - // information here to check this. In addition, even if it turns out we can't freeze those - // credits, we don't want to create a token channel inconsistency. - let p_remote_requests = &mutual_credit .state() .pending_transactions From a5816df57d67fe807a04c2a3d5e3dc91b51bf0d2 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 15:02:41 +0300 Subject: [PATCH 014/248] Don't sign over RequestSendFunds.left_fees, because it changes. --- components/proto/src/funder/messages.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 1d16133f9..e85bb6987 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -202,7 +202,9 @@ impl CanonicalSerialize for RequestSendFundsOp { .write_u128::(self.dest_payment) .unwrap(); res_bytes.extend_from_slice(&self.invoice_id); - res_bytes.write_u128::(self.left_fees).unwrap(); + // We do not sign over`left_fees`, because this field changes as the request message is + // forwarded. + // res_bytes.write_u128::(self.left_fees).unwrap(); res_bytes } } From cc63542e1d2c8932d3e82efe6cb0d64360e4a9f7 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 15:03:03 +0300 Subject: [PATCH 015/248] Changed RateError to be public. --- components/funder/src/mutual_credit/types.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/components/funder/src/mutual_credit/types.rs b/components/funder/src/mutual_credit/types.rs index 05135834d..4475dc042 100644 --- a/components/funder/src/mutual_credit/types.rs +++ b/components/funder/src/mutual_credit/types.rs @@ -102,15 +102,14 @@ pub struct Rate { } #[derive(Debug)] -struct RateError; +pub struct RateError; impl Rate { pub fn new() -> Self { Rate { mul: 0, add: 0 } } - fn calc_fee(&self, dest_payment: u128) -> Result { - // TODO: Fix this using BigUint + pub fn calc_fee(&self, dest_payment: u128) -> Result { let mul_res = (BigUint::from(dest_payment) * BigUint::from(self.mul)) >> 32; let res = mul_res + BigUint::from(self.add); res.to_u128().ok_or(RateError) From 0c2a8118acc511e6aa094a12992a118019f7ef91 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 15:03:41 +0300 Subject: [PATCH 016/248] Implemented process_request_send_funds(). --- .../funder/src/mutual_credit/incoming.rs | 44 ++++++++++++++----- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index 3aa328615..fe757c4c8 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -5,7 +5,7 @@ use common::safe_arithmetic::SafeSignedArithmetic; use proto::funder::messages::{ CancelSendFundsOp, CommitSendFundsOp, FriendTcOp, PendingTransaction, RequestSendFundsOp, - RequestsStatus, ResponseSendFundsOp, + RequestsStatus, ResponseSendFundsOp, TransactionStage, }; use proto::funder::signature_buff::{create_response_signature_buffer, verify_failure_signature}; @@ -55,6 +55,7 @@ pub enum ProcessOperationError { RequestsAlreadyDisabled, RouteTooLong, InsufficientTrust, + InsufficientFee, CreditsCalcOverflow, CreditCalculatorFailure, RequestAlreadyExists, @@ -64,6 +65,7 @@ pub enum ProcessOperationError { InvalidReportingNode, InvalidFailureSignature, LocalRequestsClosed, + CalcFeeError, } #[derive(Debug)] @@ -201,17 +203,31 @@ fn process_request_send_funds( let route_len = usize_to_u32(request_send_funds.route.len()).ok_or(ProcessOperationError::RouteTooLong)?; - let credit_calc = CreditCalculator::new(route_len, request_send_funds.dest_payment); let local_index = remote_index .checked_add(1) .ok_or(ProcessOperationError::RouteTooLong)?; let local_index = usize_to_u32(local_index).ok_or(ProcessOperationError::RouteTooLong)?; + // Calculate the amount of credits we want for passing this request. + // (Note: We are for sure not the origin of this request message, because it was received from a + // remote side) + let own_fee = mutual_credit + .state() + .rate + .calc_fee(request_send_funds.dest_payment) + .map_err(|_| ProcessOperationError::CalcFeeError)?; + + let new_left_fees = request_send_funds + .left_fees + .checked_sub(own_fee) + .ok_or(ProcessOperationError::InsufficientFee)?; + // Calculate amount of credits to freeze - let own_freeze_credits = credit_calc - .credits_to_freeze(local_index) - .ok_or(ProcessOperationError::CreditCalculatorFailure)?; + let own_freeze_credits = request_send_funds + .dest_payment + .checked_add(request_send_funds.left_fees) + .ok_or(ProcessOperationError::CreditsCalcOverflow)?; // Make sure we can freeze the credits let balance = &mutual_credit.state().balance; @@ -244,18 +260,25 @@ fn process_request_send_funds( return Err(ProcessOperationError::RequestAlreadyExists); } - unimplemented!(); + // Add pending transaction: + let pending_transaction = PendingTransaction { + request_id: request_send_funds.request_id, + route: request_send_funds.route.clone(), + dest_payment: request_send_funds.dest_payment, + invoice_id: request_send_funds.invoice_id.clone(), + left_fees: new_left_fees, + src_hashed_lock: request_send_funds.src_hashed_lock.clone(), + stage: TransactionStage::Request, + }; - /* - // Add pending request funds: - let pending_friend_request = create_pending_transaction(&request_send_funds); + // let pending_friend_request = create_pending_transaction(&request_send_funds); let mut op_output = ProcessOperationOutput { incoming_message: Some(IncomingMessage::Request(request_send_funds)), mc_mutations: Vec::new(), }; - let tc_mutation = McMutation::InsertRemotePendingTransaction(pending_friend_request); + let tc_mutation = McMutation::InsertRemotePendingTransaction(pending_transaction); mutual_credit.mutate(&tc_mutation); op_output.mc_mutations.push(tc_mutation); @@ -265,7 +288,6 @@ fn process_request_send_funds( op_output.mc_mutations.push(tc_mutation); Ok(op_output) - */ } fn process_response_send_funds( From bd5be65e448ea43c28211200e83c1ef5abde69b0 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 15:22:23 +0300 Subject: [PATCH 017/248] docs: Fixed signature definitions --- doc/docs/atomic_proposal.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/doc/docs/atomic_proposal.md b/doc/docs/atomic_proposal.md index 9e27a073e..5fe5544b2 100644 --- a/doc/docs/atomic_proposal.md +++ b/doc/docs/atomic_proposal.md @@ -337,7 +337,7 @@ struct ResponseSendFundsOp { # sha512/256("FUNDS_RESPONSE") || # sha512/256(requestId || sha512/256(route) || randNonce) || # srcHashedLock || - # receiptHashedLock || + # destHashedLock || # destPayment || # invoiceId # ) @@ -425,8 +425,7 @@ struct Confirmation { # sha512/256("FUNDS_RESPONSE") || # sha512/256(requestId || sha512/256(route) || randNonce) || # srcHashedLock || - # dstHashedLock || - # receiptHashedLock || + # destHashedLock || # destPayment || # invoiceId # ) From 7d2f0f896be3fa2dd05a7435ffb4dbc2b4a23129 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 15:22:50 +0300 Subject: [PATCH 018/248] Added missing destPayment field in Receipt. --- doc/docs/atomic_proposal.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/docs/atomic_proposal.md b/doc/docs/atomic_proposal.md index 5fe5544b2..ce8153b1f 100644 --- a/doc/docs/atomic_proposal.md +++ b/doc/docs/atomic_proposal.md @@ -503,7 +503,8 @@ struct Receipt { invoiceId @1: InvoiceId; srcPlainLock @2: Lock; destPlainLock @3: Lock; - signature @4: Signature; + destPayment @4: CustomUInt128; + signature @5: Signature; # Signature{key=destinationKey}( # sha512/256("FUNDS_RESPONSE") || # sha512/256(requestId || sha512/256(route) || randNonce) || From 26e11dd85aa1d688a495b5c9904fcf39fdd7c7ad Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 15:23:30 +0300 Subject: [PATCH 019/248] Updated some signature buffers. --- components/proto/src/capnp_common.rs | 6 ++++ components/proto/src/funder/messages.rs | 2 ++ components/proto/src/funder/signature_buff.rs | 34 +++++++++++++++---- components/proto/src/lib.rs | 1 + 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/components/proto/src/capnp_common.rs b/components/proto/src/capnp_common.rs index 0d0c126f3..b7b8cb50e 100644 --- a/components/proto/src/capnp_common.rs +++ b/components/proto/src/capnp_common.rs @@ -270,17 +270,23 @@ pub fn write_index_server_address(from: &IndexServerAddress, to: &mut index_serv */ pub fn read_receipt(from: &receipt::Reader) -> Result { + unimplemented!(); + /* Ok(Receipt { response_hash: read_hash(&from.get_response_hash()?)?, invoice_id: read_invoice_id(&from.get_invoice_id()?)?, dest_payment: read_custom_u_int128(&from.get_dest_payment()?)?, signature: read_signature(&from.get_signature()?)?, }) + */ } pub fn write_receipt(from: &Receipt, to: &mut receipt::Builder) { + unimplemented!(); + /* write_hash(&from.response_hash, &mut to.reborrow().init_response_hash()); write_invoice_id(&from.invoice_id, &mut to.reborrow().init_invoice_id()); write_custom_u_int128(from.dest_payment, &mut to.reborrow().init_dest_payment()); write_signature(&from.signature, &mut to.reborrow().init_signature()); + */ } diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index e85bb6987..460f2b709 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -162,6 +162,8 @@ pub struct Receipt { pub response_hash: HashResult, // = sha512/256(requestId || sha512/256(route) || randNonce) pub invoice_id: InvoiceId, + pub src_plain_lock: PlainLock, + pub dest_plain_lock: PlainLock, pub dest_payment: u128, pub signature: Signature, // Signature{key=recipientKey}( diff --git a/components/proto/src/funder/signature_buff.rs b/components/proto/src/funder/signature_buff.rs index 739d63c66..320c1436a 100644 --- a/components/proto/src/funder/signature_buff.rs +++ b/components/proto/src/funder/signature_buff.rs @@ -9,8 +9,8 @@ use super::messages::{ CancelSendFundsOp, MoveToken, PendingTransaction, Receipt, ResponseSendFundsOp, }; -pub const FUND_SUCCESS_PREFIX: &[u8] = b"FUND_SUCCESS"; -pub const FUND_FAILURE_PREFIX: &[u8] = b"FUND_FAILURE"; +pub const FUNDS_RESPONSE_PREFIX: &[u8] = b"FUND_RESPONSE"; +pub const FUNDS_CANCEL_PREFIX: &[u8] = b"FUND_CANCEL"; /// Create the buffer we sign over at the Response funds. /// Note that the signature is not just over the Response funds bytes. The signed buffer also @@ -21,7 +21,7 @@ pub fn create_response_signature_buffer( ) -> Vec { let mut sbuffer = Vec::new(); - sbuffer.extend_from_slice(&hash::sha_512_256(FUND_SUCCESS_PREFIX)); + sbuffer.extend_from_slice(&hash::sha_512_256(FUNDS_RESPONSE_PREFIX)); let mut inner_blob = Vec::new(); inner_blob.extend_from_slice(&pending_transaction.request_id); @@ -29,10 +29,12 @@ pub fn create_response_signature_buffer( inner_blob.extend_from_slice(&response_send_funds.rand_nonce); sbuffer.extend_from_slice(&hash::sha_512_256(&inner_blob)); - sbuffer.extend_from_slice(&pending_transaction.invoice_id); + sbuffer.extend_from_slice(&pending_transaction.src_hashed_lock); + sbuffer.extend_from_slice(&response_send_funds.dest_hashed_lock); sbuffer .write_u128::(pending_transaction.dest_payment) .unwrap(); + sbuffer.extend_from_slice(&pending_transaction.invoice_id); sbuffer } @@ -46,10 +48,23 @@ pub fn create_failure_signature_buffer( failure_send_funds: &CancelSendFundsOp, pending_transaction: &PendingTransaction, ) -> Vec { + /* + # Signature{key=recipientKey}( + # sha512/256("FUNDS_CANCEL") || + # requestId || + # srcHashedLock || + # sha512/256(route) || + # destPayment || + # invoiceId || + # reportingPublicKey || + # randNonce + # ) + */ let mut sbuffer = Vec::new(); - sbuffer.extend_from_slice(&hash::sha_512_256(FUND_FAILURE_PREFIX)); + sbuffer.extend_from_slice(&hash::sha_512_256(FUNDS_CANCEL_PREFIX)); sbuffer.extend_from_slice(&pending_transaction.request_id); + sbuffer.extend_from_slice(&pending_transaction.src_hashed_lock); sbuffer.extend_from_slice(&pending_transaction.route.hash()); sbuffer @@ -86,6 +101,7 @@ pub fn verify_failure_signature( Some(()) } +/* pub fn prepare_receipt( response_send_funds: &ResponseSendFundsOp, pending_transaction: &PendingTransaction, @@ -104,14 +120,18 @@ pub fn prepare_receipt( signature: response_send_funds.signature.clone(), } } +*/ /// Verify that a given receipt's signature is valid pub fn verify_receipt(receipt: &Receipt, public_key: &PublicKey) -> bool { let mut data = Vec::new(); - data.extend_from_slice(&hash::sha_512_256(FUND_SUCCESS_PREFIX)); + + data.extend_from_slice(&hash::sha_512_256(FUNDS_RESPONSE_PREFIX)); data.extend(receipt.response_hash.as_ref()); - data.extend(receipt.invoice_id.as_ref()); + data.extend_from_slice(&receipt.src_plain_lock.hash()); + data.extend_from_slice(&receipt.dest_plain_lock.hash()); data.write_u128::(receipt.dest_payment).unwrap(); + data.extend(receipt.invoice_id.as_ref()); verify_signature(&data, public_key, &receipt.signature) } diff --git a/components/proto/src/lib.rs b/components/proto/src/lib.rs index a205a45dc..20d9ffd48 100644 --- a/components/proto/src/lib.rs +++ b/components/proto/src/lib.rs @@ -33,6 +33,7 @@ extern crate derive_more; #[macro_use] pub mod macros; pub mod app_server; +#[allow(unused)] pub mod capnp_common; pub mod consts; pub mod file; From 9d45011decec2bd7a227bd3e24dd122b63953d83 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 15:43:25 +0300 Subject: [PATCH 020/248] Implemented process_response_send_funds() --- .../funder/src/mutual_credit/incoming.rs | 62 +++++++++++++++++++ components/funder/src/mutual_credit/types.rs | 24 +++++++ 2 files changed, 86 insertions(+) diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index fe757c4c8..c967beb94 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -66,6 +66,7 @@ pub enum ProcessOperationError { InvalidFailureSignature, LocalRequestsClosed, CalcFeeError, + NotExpectingResponse, } #[derive(Debug)] @@ -290,6 +291,66 @@ fn process_request_send_funds( Ok(op_output) } +fn process_response_send_funds( + mutual_credit: &mut MutualCredit, + response_send_funds: ResponseSendFundsOp, +) -> Result { + // Make sure that id exists in local_pending hashmap, + // and access saved request details. + let local_pending_transactions = &mutual_credit + .state() + .pending_transactions + .pending_local_requests; + + // Obtain pending request: + // TODO: Possibly get rid of clone() here for optimization later + let pending_transaction = local_pending_transactions + .get(&response_send_funds.request_id) + .ok_or(ProcessOperationError::RequestDoesNotExist)? + .clone(); + + let dest_public_key = pending_transaction.route.public_keys.last().unwrap(); + + let response_signature_buffer = + create_response_signature_buffer(&response_send_funds, &pending_transaction); + + // Verify response funds signature: + if !verify_signature( + &response_signature_buffer, + dest_public_key, + &response_send_funds.signature, + ) { + return Err(ProcessOperationError::InvalidResponseSignature); + } + + // We expect that the current stage is Request: + if let TransactionStage::Request = pending_transaction.stage { + } else { + return Err(ProcessOperationError::NotExpectingResponse); + } + + let mut mc_mutations = Vec::new(); + + // Set the stage to Response, and remember dest_hashed_lock: + let tc_mutation = McMutation::SetRemotePendingTransactionStage(( + response_send_funds.request_id.clone(), + TransactionStage::Response(response_send_funds.dest_hashed_lock.clone()), + )); + mutual_credit.mutate(&tc_mutation); + mc_mutations.push(tc_mutation); + + let incoming_message = Some(IncomingMessage::Response(IncomingResponseSendFundsOp { + pending_transaction, + incoming_response: response_send_funds, + })); + + Ok(ProcessOperationOutput { + incoming_message, + mc_mutations, + }) +} + +/* fn process_response_send_funds( mutual_credit: &mut MutualCredit, response_send_funds: ResponseSendFundsOp, @@ -380,6 +441,7 @@ fn process_response_send_funds( mc_mutations, }) } +*/ fn process_cancel_send_funds( mutual_credit: &mut MutualCredit, diff --git a/components/funder/src/mutual_credit/types.rs b/components/funder/src/mutual_credit/types.rs index 4475dc042..908c01de7 100644 --- a/components/funder/src/mutual_credit/types.rs +++ b/components/funder/src/mutual_credit/types.rs @@ -146,8 +146,10 @@ pub enum McMutation { SetBalance(i128), InsertLocalPendingTransaction(PendingTransaction), RemoveLocalPendingTransaction(Uid), + SetLocalPendingTransactionStage((Uid, TransactionStage)), InsertRemotePendingTransaction(PendingTransaction), RemoveRemotePendingTransaction(Uid), + SetRemotePendingTransactionStage((Uid, TransactionStage)), SetLocalPendingDebt(u128), SetRemotePendingDebt(u128), SetRate(Rate), @@ -212,12 +214,18 @@ impl MutualCredit { McMutation::RemoveLocalPendingTransaction(request_id) => { self.remove_local_pending_transaction(request_id) } + McMutation::SetLocalPendingTransactionStage((request_id, stage)) => { + self.set_local_pending_transaction_stage(&request_id, stage.clone()) + } McMutation::InsertRemotePendingTransaction(pending_friend_request) => { self.insert_remote_pending_transaction(pending_friend_request) } McMutation::RemoveRemotePendingTransaction(request_id) => { self.remove_remote_pending_transaction(request_id) } + McMutation::SetRemotePendingTransactionStage((request_id, stage)) => { + self.set_remote_pending_transaction_stage(&request_id, stage.clone()) + } McMutation::SetLocalPendingDebt(local_pending_debt) => { self.set_local_pending_debt(*local_pending_debt) } @@ -314,4 +322,20 @@ impl MutualCredit { }) } */ + fn set_local_pending_transaction_stage(&mut self, request_id: &Uid, stage: TransactionStage) { + self.state + .pending_transactions + .pending_local_requests + .get_mut(&request_id) + .unwrap() + .stage = stage; + } + fn set_remote_pending_transaction_stage(&mut self, request_id: &Uid, stage: TransactionStage) { + self.state + .pending_transactions + .pending_remote_requests + .get_mut(&request_id) + .unwrap() + .stage = stage; + } } From 43b953c6227b38e915ff7785534a5f83959c1dc7 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 16:03:10 +0300 Subject: [PATCH 021/248] Renamed CommitOp -> CommitSendFundsOp. --- doc/docs/atomic_proposal.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/docs/atomic_proposal.md b/doc/docs/atomic_proposal.md index ce8153b1f..83881743e 100644 --- a/doc/docs/atomic_proposal.md +++ b/doc/docs/atomic_proposal.md @@ -474,7 +474,7 @@ all the way (along the original route) to the source of the payment. ```capnp -struct CommitOp { +struct CommitSendFundsOp { requestId @0: Uid; srcPlainLock @1: Lock; destPlainLock @2: Lock; @@ -485,7 +485,7 @@ Note that the Commit message can only be sent by the seller after it has received the confirmation, because the confirmation contains the srcPlainLock. -When receiving a CommitOp messages, the following should be verified: +When receiving a CommitSendFundsOp messages, the following should be verified: - `bcrypt(srcPlainLock) = srcHashedLock` - `bcrypt(destPlainLock) = destHashedLock` @@ -516,7 +516,7 @@ struct Receipt { } ``` -The Receipt can be constructed only after the CommitOp message was received. +The Receipt can be constructed only after the CommitSendFundsOp message was received. Note that it is possible that a receipt can be constructed only a long time after the confirmation message was given. @@ -617,8 +617,8 @@ This is done as follows: Confirmation-s for all the requests that got a valid response. 6. Seller verifies the MultiConfirmation message. If valid, the payment is accepted and the goods are handed to the buyer. -7. The Seller sends back CommitOp messages for all requests. -8. Any CommitOp message can be used to create a valid Receipt. (Two diferent +7. The Seller sends back CommitSendFundsOp messages for all requests. +8. Any CommitSendFundsOp message can be used to create a valid Receipt. (Two diferent constructed receipts will have the same invoiceId but different responseHash). From ac3eef493497777d83dc7f628904ad105985a5f0 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 16:03:29 +0300 Subject: [PATCH 022/248] Implemented process_commit_send_funds(). --- .../funder/src/mutual_credit/incoming.rs | 140 ++++++++---------- 1 file changed, 62 insertions(+), 78 deletions(-) diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index c967beb94..78f2abb61 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -67,6 +67,9 @@ pub enum ProcessOperationError { LocalRequestsClosed, CalcFeeError, NotExpectingResponse, + InvalidSrcPlainLock, + InvalidDestPlainLock, + NotExpectingCommit, } #[derive(Debug)] @@ -188,7 +191,7 @@ fn process_request_send_funds( return Err(ProcessOperationError::InvalidRoute); } - // Find ourselves on the route. If we are not there, abort. + // Find ourselves (And remote side) on the route. If we are not there, abort. let remote_index = request_send_funds .route .find_pk_pair( @@ -350,10 +353,9 @@ fn process_response_send_funds( }) } -/* -fn process_response_send_funds( +fn process_cancel_send_funds( mutual_credit: &mut MutualCredit, - response_send_funds: ResponseSendFundsOp, + cancel_send_funds: CancelSendFundsOp, ) -> Result { // Make sure that id exists in local_pending hashmap, // and access saved request details. @@ -363,30 +365,11 @@ fn process_response_send_funds( .pending_local_requests; // Obtain pending request: - // TODO: Possibly get rid of clone() here for optimization later let pending_transaction = local_pending_transactions - .get(&response_send_funds.request_id) + .get(&cancel_send_funds.request_id) .ok_or(ProcessOperationError::RequestDoesNotExist)? .clone(); - - let dest_public_key = pending_transaction.route.public_keys.last().unwrap(); - - let response_signature_buffer = - create_response_signature_buffer(&response_send_funds, &pending_transaction); - - // Verify response funds signature: - if !verify_signature( - &response_signature_buffer, - dest_public_key, - &response_send_funds.signature, - ) { - return Err(ProcessOperationError::InvalidResponseSignature); - } - - // It should never happen that usize_to_u32 fails here, because we - // checked this when we created the pending_transaction. - let route_len = usize_to_u32(pending_transaction.route.len()).unwrap(); - let credit_calc = CreditCalculator::new(route_len, pending_transaction.dest_payment); + // TODO: Possibly get rid of clone() here for optimization later // Find ourselves on the route. If we are not there, abort. let local_index = pending_transaction @@ -397,15 +380,39 @@ fn process_response_send_funds( ) .unwrap(); + // Make sure that reporting node public key is: + // - inside the route + // - After us on the route. + // - Not the destination node + + let reporting_index = pending_transaction + .route + .pk_to_index(&cancel_send_funds.reporting_public_key) + .ok_or(ProcessOperationError::ReportingNodeNonexistent)?; + + if reporting_index <= local_index { + return Err(ProcessOperationError::InvalidReportingNode); + } + + verify_failure_signature(&cancel_send_funds, &pending_transaction) + .ok_or(ProcessOperationError::InvalidFailureSignature)?; + + // At this point we believe the failure funds is valid. + let route_len = usize_to_u32(pending_transaction.route.len()).unwrap(); + let credit_calc = CreditCalculator::new(route_len, pending_transaction.dest_payment); + let mut mc_mutations = Vec::new(); // Remove entry from local_pending hashmap: - let tc_mutation = McMutation::RemoveLocalPendingTransaction(response_send_funds.request_id); + let tc_mutation = McMutation::RemoveLocalPendingTransaction(cancel_send_funds.request_id); mutual_credit.mutate(&tc_mutation); mc_mutations.push(tc_mutation); let remote_index = usize_to_u32(local_index.checked_add(1).unwrap()).unwrap(); - let success_credits = credit_calc.credits_on_success(remote_index).unwrap(); + let reporting_index = usize_to_u32(reporting_index).unwrap(); + let failure_credits = credit_calc + .credits_on_failure(remote_index, reporting_index) + .unwrap(); let freeze_credits = credit_calc.credits_to_freeze(remote_index).unwrap(); // Decrease frozen credits and decrease balance: @@ -424,16 +431,17 @@ fn process_response_send_funds( .state() .balance .balance - .checked_sub_unsigned(success_credits) + .checked_sub_unsigned(failure_credits) .unwrap(); let tc_mutation = McMutation::SetBalance(new_balance); mutual_credit.mutate(&tc_mutation); mc_mutations.push(tc_mutation); - let incoming_message = Some(IncomingMessage::Response(IncomingResponseSendFundsOp { + // Return Failure funds. + let incoming_message = Some(IncomingMessage::Cancel(IncomingCancelSendFundsOp { pending_transaction, - incoming_response: response_send_funds, + incoming_cancel: cancel_send_funds, })); Ok(ProcessOperationOutput { @@ -441,11 +449,10 @@ fn process_response_send_funds( mc_mutations, }) } -*/ -fn process_cancel_send_funds( +fn process_commit_send_funds( mutual_credit: &mut MutualCredit, - cancel_send_funds: CancelSendFundsOp, + commit_send_funds: CommitSendFundsOp, ) -> Result { // Make sure that id exists in local_pending hashmap, // and access saved request details. @@ -455,55 +462,40 @@ fn process_cancel_send_funds( .pending_local_requests; // Obtain pending request: + // TODO: Possibly get rid of clone() here for optimization later let pending_transaction = local_pending_transactions - .get(&cancel_send_funds.request_id) + .get(&commit_send_funds.request_id) .ok_or(ProcessOperationError::RequestDoesNotExist)? .clone(); - // TODO: Possibly get rid of clone() here for optimization later - - // Find ourselves on the route. If we are not there, abort. - let local_index = pending_transaction - .route - .find_pk_pair( - &mutual_credit.state().idents.local_public_key, - &mutual_credit.state().idents.remote_public_key, - ) - .unwrap(); - - // Make sure that reporting node public key is: - // - inside the route - // - After us on the route. - // - Not the destination node - let reporting_index = pending_transaction - .route - .pk_to_index(&cancel_send_funds.reporting_public_key) - .ok_or(ProcessOperationError::ReportingNodeNonexistent)?; + let dest_hashed_lock = match &pending_transaction.stage { + TransactionStage::Response(dest_hashed_lock) => dest_hashed_lock, + _ => return Err(ProcessOperationError::NotExpectingCommit), + }; - if reporting_index <= local_index { - return Err(ProcessOperationError::InvalidReportingNode); + // Verify src_plain_lock and dest_plain_lock: + if commit_send_funds.src_plain_lock.hash() != pending_transaction.src_hashed_lock { + return Err(ProcessOperationError::InvalidSrcPlainLock); } - verify_failure_signature(&cancel_send_funds, &pending_transaction) - .ok_or(ProcessOperationError::InvalidFailureSignature)?; - - // At this point we believe the failure funds is valid. - let route_len = usize_to_u32(pending_transaction.route.len()).unwrap(); - let credit_calc = CreditCalculator::new(route_len, pending_transaction.dest_payment); + if commit_send_funds.dest_plain_lock.hash() != *dest_hashed_lock { + return Err(ProcessOperationError::InvalidDestPlainLock); + } let mut mc_mutations = Vec::new(); // Remove entry from local_pending hashmap: - let tc_mutation = McMutation::RemoveLocalPendingTransaction(cancel_send_funds.request_id); + let tc_mutation = McMutation::RemoveLocalPendingTransaction(commit_send_funds.request_id); mutual_credit.mutate(&tc_mutation); mc_mutations.push(tc_mutation); - let remote_index = usize_to_u32(local_index.checked_add(1).unwrap()).unwrap(); - let reporting_index = usize_to_u32(reporting_index).unwrap(); - let failure_credits = credit_calc - .credits_on_failure(remote_index, reporting_index) + // Calculate amount of credits that were frozen: + let freeze_credits = pending_transaction + .dest_payment + .checked_add(pending_transaction.left_fees) .unwrap(); - let freeze_credits = credit_calc.credits_to_freeze(remote_index).unwrap(); + // Note: The unwrap() above should never fail, because this was already checked during the + // request message processing. // Decrease frozen credits and decrease balance: let new_local_pending_debt = mutual_credit @@ -521,17 +513,16 @@ fn process_cancel_send_funds( .state() .balance .balance - .checked_sub_unsigned(failure_credits) + .checked_sub_unsigned(freeze_credits) .unwrap(); let tc_mutation = McMutation::SetBalance(new_balance); mutual_credit.mutate(&tc_mutation); mc_mutations.push(tc_mutation); - // Return Failure funds. - let incoming_message = Some(IncomingMessage::Cancel(IncomingCancelSendFundsOp { + let incoming_message = Some(IncomingMessage::Commit(IncomingCommitSendFundsOp { pending_transaction, - incoming_cancel: cancel_send_funds, + incoming_commit: commit_send_funds, })); Ok(ProcessOperationOutput { @@ -539,10 +530,3 @@ fn process_cancel_send_funds( mc_mutations, }) } - -fn process_commit_send_funds( - mutual_credit: &mut MutualCredit, - commit_send_funds: CommitSendFundsOp, -) -> Result { - unimplemented!(); -} From a66db11fc375d3fc53ba10912ff4cc17ca45ce8b Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 17:04:13 +0300 Subject: [PATCH 023/248] docs: Removed signature related fields from CancelSendFundsOps --- doc/docs/atomic_proposal.md | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/doc/docs/atomic_proposal.md b/doc/docs/atomic_proposal.md index 83881743e..5c3c3fdce 100644 --- a/doc/docs/atomic_proposal.md +++ b/doc/docs/atomic_proposal.md @@ -380,21 +380,6 @@ B --- C --- D --- E ```capnp struct CancelSendFundsOp { requestId @0: Uid; - reportingPublicKey @1: PublicKey; - # Public key of the reporting node in the route of the corresponding request. - # The reporting node cannot be the destination node. - randNonce @2: RandNonce; - signature @3: Signature; - # Signature{key=recipientKey}( - # sha512/256("FUNDS_CANCEL") || - # requestId || - # srcHashedLock || - # sha512/256(route) || - # destPayment || - # invoiceId || - # reportingPublicKey || - # randNonce - # ) } ``` From 09ca4f3e13f290996779383732539c30e0a2890d Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 17:04:24 +0300 Subject: [PATCH 024/248] Implemented process_cancel_send_funds(). --- .../funder/src/mutual_credit/incoming.rs | 55 +++------------- components/proto/src/funder/messages.rs | 8 +-- components/proto/src/funder/serialize.rs | 3 + components/proto/src/funder/signature_buff.rs | 66 +------------------ 4 files changed, 15 insertions(+), 117 deletions(-) diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index 78f2abb61..374df9e5b 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -7,7 +7,7 @@ use proto::funder::messages::{ CancelSendFundsOp, CommitSendFundsOp, FriendTcOp, PendingTransaction, RequestSendFundsOp, RequestsStatus, ResponseSendFundsOp, TransactionStage, }; -use proto::funder::signature_buff::{create_response_signature_buffer, verify_failure_signature}; +use proto::funder::signature_buff::create_response_signature_buffer; use crate::credit_calc::CreditCalculator; @@ -371,36 +371,6 @@ fn process_cancel_send_funds( .clone(); // TODO: Possibly get rid of clone() here for optimization later - // Find ourselves on the route. If we are not there, abort. - let local_index = pending_transaction - .route - .find_pk_pair( - &mutual_credit.state().idents.local_public_key, - &mutual_credit.state().idents.remote_public_key, - ) - .unwrap(); - - // Make sure that reporting node public key is: - // - inside the route - // - After us on the route. - // - Not the destination node - - let reporting_index = pending_transaction - .route - .pk_to_index(&cancel_send_funds.reporting_public_key) - .ok_or(ProcessOperationError::ReportingNodeNonexistent)?; - - if reporting_index <= local_index { - return Err(ProcessOperationError::InvalidReportingNode); - } - - verify_failure_signature(&cancel_send_funds, &pending_transaction) - .ok_or(ProcessOperationError::InvalidFailureSignature)?; - - // At this point we believe the failure funds is valid. - let route_len = usize_to_u32(pending_transaction.route.len()).unwrap(); - let credit_calc = CreditCalculator::new(route_len, pending_transaction.dest_payment); - let mut mc_mutations = Vec::new(); // Remove entry from local_pending hashmap: @@ -408,14 +378,20 @@ fn process_cancel_send_funds( mutual_credit.mutate(&tc_mutation); mc_mutations.push(tc_mutation); + /* let remote_index = usize_to_u32(local_index.checked_add(1).unwrap()).unwrap(); let reporting_index = usize_to_u32(reporting_index).unwrap(); let failure_credits = credit_calc .credits_on_failure(remote_index, reporting_index) .unwrap(); - let freeze_credits = credit_calc.credits_to_freeze(remote_index).unwrap(); + */ - // Decrease frozen credits and decrease balance: + let freeze_credits = pending_transaction + .dest_payment + .checked_add(pending_transaction.left_fees) + .unwrap(); + + // Decrease frozen credits: let new_local_pending_debt = mutual_credit .state() .balance @@ -427,18 +403,7 @@ fn process_cancel_send_funds( mutual_credit.mutate(&tc_mutation); mc_mutations.push(tc_mutation); - let new_balance = mutual_credit - .state() - .balance - .balance - .checked_sub_unsigned(failure_credits) - .unwrap(); - - let tc_mutation = McMutation::SetBalance(new_balance); - mutual_credit.mutate(&tc_mutation); - mc_mutations.push(tc_mutation); - - // Return Failure funds. + // Return cancel_send_funds: let incoming_message = Some(IncomingMessage::Cancel(IncomingCancelSendFundsOp { pending_transaction, incoming_cancel: cancel_send_funds, diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 460f2b709..24baa48fe 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -79,11 +79,8 @@ pub struct ResponseSendFundsOp { } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] -pub struct CancelSendFundsOp { +pub struct CancelSendFundsOp { pub request_id: Uid, - pub reporting_public_key: PublicKey, - pub rand_nonce: RandValue, - pub signature: S, } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] @@ -226,9 +223,6 @@ impl CanonicalSerialize for CancelSendFundsOp { fn canonical_serialize(&self) -> Vec { let mut res_bytes = Vec::new(); res_bytes.extend_from_slice(&self.request_id); - res_bytes.extend_from_slice(&self.reporting_public_key); - res_bytes.extend_from_slice(&self.rand_nonce); - res_bytes.extend_from_slice(&self.signature); res_bytes } } diff --git a/components/proto/src/funder/serialize.rs b/components/proto/src/funder/serialize.rs index 4548226a4..2fe413008 100644 --- a/components/proto/src/funder/serialize.rs +++ b/components/proto/src/funder/serialize.rs @@ -311,6 +311,8 @@ fn deser_response_send_funds_op( fn deser_cancel_send_funds_op( cancel_send_funds_op_reader: &funder_capnp::failure_send_funds_op::Reader, ) -> Result { + unimplemented!(); + /* Ok(CancelSendFundsOp { request_id: read_uid(&cancel_send_funds_op_reader.get_request_id()?)?, reporting_public_key: read_public_key( @@ -319,6 +321,7 @@ fn deser_cancel_send_funds_op( rand_nonce: read_rand_nonce(&cancel_send_funds_op_reader.get_rand_nonce()?)?, signature: read_signature(&cancel_send_funds_op_reader.get_signature()?)?, }) + */ } fn deser_friend_operation( diff --git a/components/proto/src/funder/signature_buff.rs b/components/proto/src/funder/signature_buff.rs index 320c1436a..66ddd851c 100644 --- a/components/proto/src/funder/signature_buff.rs +++ b/components/proto/src/funder/signature_buff.rs @@ -5,9 +5,7 @@ use crypto::identity::{verify_signature, PublicKey}; use common::canonical_serialize::CanonicalSerialize; use common::int_convert::usize_to_u64; -use super::messages::{ - CancelSendFundsOp, MoveToken, PendingTransaction, Receipt, ResponseSendFundsOp, -}; +use super::messages::{MoveToken, PendingTransaction, Receipt, ResponseSendFundsOp}; pub const FUNDS_RESPONSE_PREFIX: &[u8] = b"FUND_RESPONSE"; pub const FUNDS_CANCEL_PREFIX: &[u8] = b"FUND_CANCEL"; @@ -39,68 +37,6 @@ pub fn create_response_signature_buffer( sbuffer } -// TODO: How to keep in sync with verify_receipt and prepare receipt? -// TODO: Add tests for synchronization between those functions? Possibly share code? -/// Create the buffer we sign over at the Failure funds. -/// Note that the signature is not just over the Response funds bytes. The signed buffer also -/// contains information from the Request funds. -pub fn create_failure_signature_buffer( - failure_send_funds: &CancelSendFundsOp, - pending_transaction: &PendingTransaction, -) -> Vec { - /* - # Signature{key=recipientKey}( - # sha512/256("FUNDS_CANCEL") || - # requestId || - # srcHashedLock || - # sha512/256(route) || - # destPayment || - # invoiceId || - # reportingPublicKey || - # randNonce - # ) - */ - let mut sbuffer = Vec::new(); - - sbuffer.extend_from_slice(&hash::sha_512_256(FUNDS_CANCEL_PREFIX)); - sbuffer.extend_from_slice(&pending_transaction.request_id); - sbuffer.extend_from_slice(&pending_transaction.src_hashed_lock); - sbuffer.extend_from_slice(&pending_transaction.route.hash()); - - sbuffer - .write_u128::(pending_transaction.dest_payment) - .unwrap(); - sbuffer.extend_from_slice(&pending_transaction.invoice_id); - sbuffer.extend_from_slice(&failure_send_funds.reporting_public_key); - sbuffer.extend_from_slice(&failure_send_funds.rand_nonce); - - sbuffer -} - -/// Verify a failure signature -pub fn verify_failure_signature( - failure_send_funds: &CancelSendFundsOp, - pending_transaction: &PendingTransaction, -) -> Option<()> { - let failure_signature_buffer = - create_failure_signature_buffer(&failure_send_funds, &pending_transaction); - let reporting_public_key = &failure_send_funds.reporting_public_key; - // Make sure that the reporting_public_key is on the route: - // TODO: Should we check that it is after us? Is it checked somewhere else? - let _ = pending_transaction - .route - .pk_to_index(&reporting_public_key)?; - - if !verify_signature( - &failure_signature_buffer, - reporting_public_key, - &failure_send_funds.signature, - ) { - return None; - } - Some(()) -} - /* pub fn prepare_receipt( response_send_funds: &ResponseSendFundsOp, From 0852063dab1f761f3f5f1191a6b43b907de01b65 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 17:04:51 +0300 Subject: [PATCH 025/248] Removed old comment. --- components/funder/src/mutual_credit/incoming.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index 374df9e5b..0db185b2e 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -378,14 +378,6 @@ fn process_cancel_send_funds( mutual_credit.mutate(&tc_mutation); mc_mutations.push(tc_mutation); - /* - let remote_index = usize_to_u32(local_index.checked_add(1).unwrap()).unwrap(); - let reporting_index = usize_to_u32(reporting_index).unwrap(); - let failure_credits = credit_calc - .credits_on_failure(remote_index, reporting_index) - .unwrap(); - */ - let freeze_credits = pending_transaction .dest_payment .checked_add(pending_transaction.left_fees) From b535d7aabd0cf54a7f6bce33651b61f09e8e87af Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 17:06:53 +0300 Subject: [PATCH 026/248] Removed some unused ProcessOperationError variants. --- components/funder/src/mutual_credit/incoming.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index 0db185b2e..a1d071a52 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -57,13 +57,9 @@ pub enum ProcessOperationError { InsufficientTrust, InsufficientFee, CreditsCalcOverflow, - CreditCalculatorFailure, RequestAlreadyExists, RequestDoesNotExist, InvalidResponseSignature, - ReportingNodeNonexistent, - InvalidReportingNode, - InvalidFailureSignature, LocalRequestsClosed, CalcFeeError, NotExpectingResponse, From 358bc85e81add6e65a9e6fb549831188bc663062 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 17:11:53 +0300 Subject: [PATCH 027/248] Initial mutual credit outgoing outline. --- components/funder/src/mutual_credit/mod.rs | 2 +- .../funder/src/mutual_credit/outgoing.rs | 47 ++++++++++++++----- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/components/funder/src/mutual_credit/mod.rs b/components/funder/src/mutual_credit/mod.rs index 09d1f1a70..a3521f69d 100644 --- a/components/funder/src/mutual_credit/mod.rs +++ b/components/funder/src/mutual_credit/mod.rs @@ -1,5 +1,5 @@ pub mod incoming; -// pub mod outgoing; +pub mod outgoing; #[cfg(test)] mod tests; pub mod types; diff --git a/components/funder/src/mutual_credit/outgoing.rs b/components/funder/src/mutual_credit/outgoing.rs index e41f672be..520a6bc47 100644 --- a/components/funder/src/mutual_credit/outgoing.rs +++ b/components/funder/src/mutual_credit/outgoing.rs @@ -4,11 +4,12 @@ use common::int_convert::usize_to_u32; use common::safe_arithmetic::SafeSignedArithmetic; use proto::funder::messages::{ - FailureSendFunds, FriendTcOp, RequestSendFunds, RequestsStatus, ResponseSendFunds, + CancelSendFundsOp, CommitSendFundsOp, FriendTcOp, RequestSendFundsOp, RequestsStatus, + ResponseSendFundsOp, }; -use proto::funder::signature_buff::{create_response_signature_buffer, verify_failure_signature}; +use proto::funder::signature_buff::create_response_signature_buffer; -use super::types::{create_pending_transaction, McMutation, MutualCredit, MAX_FUNDER_DEBT}; +use super::types::{McMutation, MutualCredit, MAX_FUNDER_DEBT}; use crate::credit_calc::CreditCalculator; /// Processes outgoing funds for a token channel. @@ -61,8 +62,11 @@ impl OutgoingMc { FriendTcOp::ResponseSendFunds(response_send_funds) => { self.queue_response_send_funds(response_send_funds) } - FriendTcOp::FailureSendFunds(failure_send_funds) => { - self.queue_failure_send_funds(failure_send_funds) + FriendTcOp::CancelSendFunds(cancel_send_funds) => { + self.queue_cancel_send_funds(cancel_send_funds) + } + FriendTcOp::CommitSendFunds(commit_send_funds) => { + self.queue_commit_send_funds(commit_send_funds) } } } @@ -103,7 +107,7 @@ impl OutgoingMc { fn queue_request_send_funds( &mut self, - request_send_funds: RequestSendFunds, + request_send_funds: RequestSendFundsOp, ) -> Result, QueueOperationError> { if !request_send_funds.route.is_valid() { return Err(QueueOperationError::InvalidRoute); @@ -171,6 +175,10 @@ impl OutgoingMc { return Err(QueueOperationError::RequestAlreadyExists); } + unimplemented!(); + + /* + // Add pending request funds: let pending_friend_request = create_pending_transaction(&request_send_funds); @@ -185,11 +193,12 @@ impl OutgoingMc { tc_mutations.push(tc_mutation); Ok(tc_mutations) + */ } fn queue_response_send_funds( &mut self, - response_send_funds: ResponseSendFunds, + response_send_funds: ResponseSendFundsOp, ) -> Result, QueueOperationError> { // Make sure that id exists in remote_pending hashmap, // and access saved request details. @@ -275,9 +284,9 @@ impl OutgoingMc { Ok(tc_mutations) } - fn queue_failure_send_funds( + fn queue_cancel_send_funds( &mut self, - failure_send_funds: FailureSendFunds, + cancel_send_funds: CancelSendFundsOp, ) -> Result, QueueOperationError> { // Make sure that id exists in remote_pending hashmap, // and access saved request details. @@ -289,7 +298,7 @@ impl OutgoingMc { // Obtain pending request: let pending_transaction = remote_pending_transactions - .get(&failure_send_funds.request_id) + .get(&cancel_send_funds.request_id) .ok_or(QueueOperationError::RequestDoesNotExist)?; // Find ourselves on the route. If we are not there, abort. @@ -312,17 +321,21 @@ impl OutgoingMc { // - inside the route // - After us on the route, or us. // - Not the destination node + unimplemented!(); + + /* let reporting_index = pending_transaction .route - .pk_to_index(&failure_send_funds.reporting_public_key) + .pk_to_index(&cancel_send_funds.reporting_public_key) .ok_or(QueueOperationError::ReportingNodeNonexistent)?; if reporting_index < local_index { return Err(QueueOperationError::InvalidReportingNode); } - verify_failure_signature(&failure_send_funds, &pending_transaction) + + verify_failure_signature(&cancel_send_funds, &pending_transaction) .ok_or(QueueOperationError::InvalidFailureSignature)?; // At this point we believe the failure funds is valid. @@ -333,7 +346,7 @@ impl OutgoingMc { // Remove entry from remote hashmap: let mut tc_mutations = Vec::new(); - let tc_mutation = McMutation::RemoveRemotePendingTransaction(failure_send_funds.request_id); + let tc_mutation = McMutation::RemoveRemotePendingTransaction(cancel_send_funds.request_id); self.mutual_credit.mutate(&tc_mutation); tc_mutations.push(tc_mutation); @@ -372,5 +385,13 @@ impl OutgoingMc { tc_mutations.push(tc_mutation); Ok(tc_mutations) + */ + } + + fn queue_commit_send_funds( + &mut self, + commit_send_funds: CommitSendFundsOp, + ) -> Result, QueueOperationError> { + unimplemented!(); } } From cc38432981724268fc8d0e909042e5e071806a0d Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 17:13:46 +0300 Subject: [PATCH 028/248] Removed unused import (CreditCalculator). --- components/funder/src/mutual_credit/incoming.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index a1d071a52..df49f01e8 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -9,8 +9,6 @@ use proto::funder::messages::{ }; use proto::funder::signature_buff::create_response_signature_buffer; -use crate::credit_calc::CreditCalculator; - use super::types::{McMutation, MutualCredit, MAX_FUNDER_DEBT}; #[derive(Debug)] From 399e73115c70c6c6ba55e938e94f1f6a5506e77a Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 17:20:57 +0300 Subject: [PATCH 029/248] Renamed tc_mutation -> mc_mutation. --- .../funder/src/mutual_credit/incoming.rs | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index df49f01e8..97fc2b3e2 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -129,9 +129,9 @@ fn process_enable_requests( incoming_message: None, mc_mutations: Vec::new(), }; - let tc_mutation = McMutation::SetRemoteRequestsStatus(RequestsStatus::Open); - mutual_credit.mutate(&tc_mutation); - op_output.mc_mutations.push(tc_mutation); + let mc_mutation = McMutation::SetRemoteRequestsStatus(RequestsStatus::Open); + mutual_credit.mutate(&mc_mutation); + op_output.mc_mutations.push(mc_mutation); Ok(op_output) } @@ -146,9 +146,9 @@ fn process_disable_requests( match mutual_credit.state().requests_status.remote { RequestsStatus::Open => { - let tc_mutation = McMutation::SetRemoteRequestsStatus(RequestsStatus::Closed); - mutual_credit.mutate(&tc_mutation); - op_output.mc_mutations.push(tc_mutation); + let mc_mutation = McMutation::SetRemoteRequestsStatus(RequestsStatus::Closed); + mutual_credit.mutate(&mc_mutation); + op_output.mc_mutations.push(mc_mutation); Ok(op_output) } RequestsStatus::Closed => Err(ProcessOperationError::RequestsAlreadyDisabled), @@ -169,9 +169,9 @@ fn process_set_remote_max_debt( proposed_max_debt, )) } else { - let tc_mutation = McMutation::SetLocalMaxDebt(proposed_max_debt); - mutual_credit.mutate(&tc_mutation); - op_output.mc_mutations.push(tc_mutation); + let mc_mutation = McMutation::SetLocalMaxDebt(proposed_max_debt); + mutual_credit.mutate(&mc_mutation); + op_output.mc_mutations.push(mc_mutation); Ok(op_output) } } @@ -276,14 +276,14 @@ fn process_request_send_funds( mc_mutations: Vec::new(), }; - let tc_mutation = McMutation::InsertRemotePendingTransaction(pending_transaction); - mutual_credit.mutate(&tc_mutation); - op_output.mc_mutations.push(tc_mutation); + let mc_mutation = McMutation::InsertRemotePendingTransaction(pending_transaction); + mutual_credit.mutate(&mc_mutation); + op_output.mc_mutations.push(mc_mutation); // If we are here, we can freeze the credits: - let tc_mutation = McMutation::SetRemotePendingDebt(new_remote_pending_debt); - mutual_credit.mutate(&tc_mutation); - op_output.mc_mutations.push(tc_mutation); + let mc_mutation = McMutation::SetRemotePendingDebt(new_remote_pending_debt); + mutual_credit.mutate(&mc_mutation); + op_output.mc_mutations.push(mc_mutation); Ok(op_output) } @@ -329,12 +329,12 @@ fn process_response_send_funds( let mut mc_mutations = Vec::new(); // Set the stage to Response, and remember dest_hashed_lock: - let tc_mutation = McMutation::SetRemotePendingTransactionStage(( + let mc_mutation = McMutation::SetRemotePendingTransactionStage(( response_send_funds.request_id.clone(), TransactionStage::Response(response_send_funds.dest_hashed_lock.clone()), )); - mutual_credit.mutate(&tc_mutation); - mc_mutations.push(tc_mutation); + mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); let incoming_message = Some(IncomingMessage::Response(IncomingResponseSendFundsOp { pending_transaction, @@ -368,9 +368,9 @@ fn process_cancel_send_funds( let mut mc_mutations = Vec::new(); // Remove entry from local_pending hashmap: - let tc_mutation = McMutation::RemoveLocalPendingTransaction(cancel_send_funds.request_id); - mutual_credit.mutate(&tc_mutation); - mc_mutations.push(tc_mutation); + let mc_mutation = McMutation::RemoveLocalPendingTransaction(cancel_send_funds.request_id); + mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); let freeze_credits = pending_transaction .dest_payment @@ -385,9 +385,9 @@ fn process_cancel_send_funds( .checked_sub(freeze_credits) .unwrap(); - let tc_mutation = McMutation::SetLocalPendingDebt(new_local_pending_debt); - mutual_credit.mutate(&tc_mutation); - mc_mutations.push(tc_mutation); + let mc_mutation = McMutation::SetLocalPendingDebt(new_local_pending_debt); + mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); // Return cancel_send_funds: let incoming_message = Some(IncomingMessage::Cancel(IncomingCancelSendFundsOp { @@ -436,9 +436,9 @@ fn process_commit_send_funds( let mut mc_mutations = Vec::new(); // Remove entry from local_pending hashmap: - let tc_mutation = McMutation::RemoveLocalPendingTransaction(commit_send_funds.request_id); - mutual_credit.mutate(&tc_mutation); - mc_mutations.push(tc_mutation); + let mc_mutation = McMutation::RemoveLocalPendingTransaction(commit_send_funds.request_id); + mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); // Calculate amount of credits that were frozen: let freeze_credits = pending_transaction @@ -456,9 +456,9 @@ fn process_commit_send_funds( .checked_sub(freeze_credits) .unwrap(); - let tc_mutation = McMutation::SetLocalPendingDebt(new_local_pending_debt); - mutual_credit.mutate(&tc_mutation); - mc_mutations.push(tc_mutation); + let mc_mutation = McMutation::SetLocalPendingDebt(new_local_pending_debt); + mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); let new_balance = mutual_credit .state() @@ -467,9 +467,9 @@ fn process_commit_send_funds( .checked_sub_unsigned(freeze_credits) .unwrap(); - let tc_mutation = McMutation::SetBalance(new_balance); - mutual_credit.mutate(&tc_mutation); - mc_mutations.push(tc_mutation); + let mc_mutation = McMutation::SetBalance(new_balance); + mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); let incoming_message = Some(IncomingMessage::Commit(IncomingCommitSendFundsOp { pending_transaction, From 620ecdc5b4a670affbe3bb76910c668037e51731 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 17:21:17 +0300 Subject: [PATCH 030/248] Implemented queue_request_send_funds(). --- .../funder/src/mutual_credit/outgoing.rs | 132 +++++++++--------- 1 file changed, 63 insertions(+), 69 deletions(-) diff --git a/components/funder/src/mutual_credit/outgoing.rs b/components/funder/src/mutual_credit/outgoing.rs index 520a6bc47..293471e65 100644 --- a/components/funder/src/mutual_credit/outgoing.rs +++ b/components/funder/src/mutual_credit/outgoing.rs @@ -4,8 +4,8 @@ use common::int_convert::usize_to_u32; use common::safe_arithmetic::SafeSignedArithmetic; use proto::funder::messages::{ - CancelSendFundsOp, CommitSendFundsOp, FriendTcOp, RequestSendFundsOp, RequestsStatus, - ResponseSendFundsOp, + CancelSendFundsOp, CommitSendFundsOp, FriendTcOp, PendingTransaction, RequestSendFundsOp, + RequestsStatus, ResponseSendFundsOp, TransactionStage, }; use proto::funder::signature_buff::create_response_signature_buffer; @@ -73,21 +73,21 @@ impl OutgoingMc { fn queue_enable_requests(&mut self) -> Result, QueueOperationError> { // TODO: Should we check first if local requests are already open? - let mut tc_mutations = Vec::new(); - let tc_mutation = McMutation::SetLocalRequestsStatus(RequestsStatus::Open); - self.mutual_credit.mutate(&tc_mutation); - tc_mutations.push(tc_mutation); + let mut mc_mutations = Vec::new(); + let mc_mutation = McMutation::SetLocalRequestsStatus(RequestsStatus::Open); + self.mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); - Ok(tc_mutations) + Ok(mc_mutations) } fn queue_disable_requests(&mut self) -> Result, QueueOperationError> { - let mut tc_mutations = Vec::new(); - let tc_mutation = McMutation::SetLocalRequestsStatus(RequestsStatus::Closed); - self.mutual_credit.mutate(&tc_mutation); - tc_mutations.push(tc_mutation); + let mut mc_mutations = Vec::new(); + let mc_mutation = McMutation::SetLocalRequestsStatus(RequestsStatus::Closed); + self.mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); - Ok(tc_mutations) + Ok(mc_mutations) } fn queue_set_remote_max_debt( @@ -98,11 +98,11 @@ impl OutgoingMc { return Err(QueueOperationError::RemoteMaxDebtTooLarge); } - let mut tc_mutations = Vec::new(); - let tc_mutation = McMutation::SetRemoteMaxDebt(proposed_max_debt); - self.mutual_credit.mutate(&tc_mutation); - tc_mutations.push(tc_mutation); - Ok(tc_mutations) + let mut mc_mutations = Vec::new(); + let mc_mutation = McMutation::SetRemoteMaxDebt(proposed_max_debt); + self.mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); + Ok(mc_mutations) } fn queue_request_send_funds( @@ -127,21 +127,11 @@ impl OutgoingMc { return Err(QueueOperationError::RemoteRequestsClosed); } - // Calculate amount of credits to freeze. - let route_len = usize_to_u32(request_send_funds.route.len()) - .ok_or(QueueOperationError::RouteTooLong)?; - let credit_calc = CreditCalculator::new(route_len, request_send_funds.dest_payment); - - // Get index of remote friend on the route: - let remote_index = local_index - .checked_add(1) - .ok_or(QueueOperationError::RouteTooLong)?; - let remote_index = usize_to_u32(remote_index).ok_or(QueueOperationError::RouteTooLong)?; - // Calculate amount of credits to freeze - let own_freeze_credits = credit_calc - .credits_to_freeze(remote_index) - .ok_or(QueueOperationError::CreditCalculatorFailure)?; + let own_freeze_credits = request_send_funds + .dest_payment + .checked_add(request_send_funds.left_fees) + .ok_or(QueueOperationError::CreditsCalcOverflow)?; let balance = &self.mutual_credit.state().balance; @@ -170,30 +160,34 @@ impl OutgoingMc { .state() .pending_transactions .pending_local_requests; + // Make sure that we don't have this request as a pending request already: if p_local_requests.contains_key(&request_send_funds.request_id) { return Err(QueueOperationError::RequestAlreadyExists); } - unimplemented!(); - - /* - - // Add pending request funds: - let pending_friend_request = create_pending_transaction(&request_send_funds); - - let mut tc_mutations = Vec::new(); - let tc_mutation = McMutation::InsertLocalPendingTransaction(pending_friend_request); - self.mutual_credit.mutate(&tc_mutation); - tc_mutations.push(tc_mutation); + // Add pending transaction: + let pending_transaction = PendingTransaction { + request_id: request_send_funds.request_id, + route: request_send_funds.route.clone(), + dest_payment: request_send_funds.dest_payment, + invoice_id: request_send_funds.invoice_id.clone(), + left_fees: request_send_funds.left_fees, + src_hashed_lock: request_send_funds.src_hashed_lock.clone(), + stage: TransactionStage::Request, + }; + + let mut mc_mutations = Vec::new(); + let mc_mutation = McMutation::InsertLocalPendingTransaction(pending_transaction); + self.mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); // If we are here, we can freeze the credits: - let tc_mutation = McMutation::SetLocalPendingDebt(new_local_pending_debt); - self.mutual_credit.mutate(&tc_mutation); - tc_mutations.push(tc_mutation); + let mc_mutation = McMutation::SetLocalPendingDebt(new_local_pending_debt); + self.mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); - Ok(tc_mutations) - */ + Ok(mc_mutations) } fn queue_response_send_funds( @@ -247,11 +241,11 @@ impl OutgoingMc { let local_index = usize_to_u32(remote_index.checked_add(1).unwrap()).unwrap(); // Remove entry from remote_pending hashmap: - let mut tc_mutations = Vec::new(); - let tc_mutation = + let mut mc_mutations = Vec::new(); + let mc_mutation = McMutation::RemoveRemotePendingTransaction(response_send_funds.request_id); - self.mutual_credit.mutate(&tc_mutation); - tc_mutations.push(tc_mutation); + self.mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); let success_credits = credit_calc.credits_on_success(local_index).unwrap(); let freeze_credits = credit_calc.credits_to_freeze(local_index).unwrap(); @@ -265,9 +259,9 @@ impl OutgoingMc { .checked_sub(freeze_credits) .expect("Insufficient frozen credit!"); - let tc_mutation = McMutation::SetRemotePendingDebt(new_remote_pending_debt); - self.mutual_credit.mutate(&tc_mutation); - tc_mutations.push(tc_mutation); + let mc_mutation = McMutation::SetRemotePendingDebt(new_remote_pending_debt); + self.mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); let new_balance = self .mutual_credit @@ -277,11 +271,11 @@ impl OutgoingMc { .checked_add_unsigned(success_credits) .expect("balance overflow"); - let tc_mutation = McMutation::SetBalance(new_balance); - self.mutual_credit.mutate(&tc_mutation); - tc_mutations.push(tc_mutation); + let mc_mutation = McMutation::SetBalance(new_balance); + self.mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); - Ok(tc_mutations) + Ok(mc_mutations) } fn queue_cancel_send_funds( @@ -344,11 +338,11 @@ impl OutgoingMc { let credit_calc = CreditCalculator::new(route_len, pending_transaction.dest_payment); // Remove entry from remote hashmap: - let mut tc_mutations = Vec::new(); + let mut mc_mutations = Vec::new(); - let tc_mutation = McMutation::RemoveRemotePendingTransaction(cancel_send_funds.request_id); - self.mutual_credit.mutate(&tc_mutation); - tc_mutations.push(tc_mutation); + let mc_mutation = McMutation::RemoveRemotePendingTransaction(cancel_send_funds.request_id); + self.mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); let local_index = usize_to_u32(local_index).unwrap(); let reporting_index = usize_to_u32(reporting_index).unwrap(); @@ -367,9 +361,9 @@ impl OutgoingMc { .checked_sub(freeze_credits) .unwrap(); - let tc_mutation = McMutation::SetRemotePendingDebt(new_remote_pending_debt); - self.mutual_credit.mutate(&tc_mutation); - tc_mutations.push(tc_mutation); + let mc_mutation = McMutation::SetRemotePendingDebt(new_remote_pending_debt); + self.mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); // Add to balance: let new_balance = self @@ -380,11 +374,11 @@ impl OutgoingMc { .checked_add_unsigned(failure_credits) .unwrap(); - let tc_mutation = McMutation::SetBalance(new_balance); - self.mutual_credit.mutate(&tc_mutation); - tc_mutations.push(tc_mutation); + let mc_mutation = McMutation::SetBalance(new_balance); + self.mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); - Ok(tc_mutations) + Ok(mc_mutations) */ } From d2dbacbbb5bb63c1bb74a4f0b6e3e339f727354b Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 17:33:17 +0300 Subject: [PATCH 031/248] incoming mutual credit: Fixed Remote -> Local. --- components/funder/src/mutual_credit/incoming.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index 97fc2b3e2..2f70dcd23 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -329,7 +329,7 @@ fn process_response_send_funds( let mut mc_mutations = Vec::new(); // Set the stage to Response, and remember dest_hashed_lock: - let mc_mutation = McMutation::SetRemotePendingTransactionStage(( + let mc_mutation = McMutation::SetLocalPendingTransactionStage(( response_send_funds.request_id.clone(), TransactionStage::Response(response_send_funds.dest_hashed_lock.clone()), )); From d39c7c01405e779145d62e6c44940c71a21406ab Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 17:33:33 +0300 Subject: [PATCH 032/248] Implemented queue_response_send_funds(). --- .../funder/src/mutual_credit/outgoing.rs | 146 ++++++++++++------ 1 file changed, 101 insertions(+), 45 deletions(-) diff --git a/components/funder/src/mutual_credit/outgoing.rs b/components/funder/src/mutual_credit/outgoing.rs index 293471e65..790dc73b8 100644 --- a/components/funder/src/mutual_credit/outgoing.rs +++ b/components/funder/src/mutual_credit/outgoing.rs @@ -35,6 +35,7 @@ pub enum QueueOperationError { InvalidFailureSignature, FailureSentFromDest, RemoteRequestsClosed, + NotExpectingResponse, } /// A wrapper over a token channel, accumulating funds to be sent as one transaction. @@ -224,54 +225,19 @@ impl OutgoingMc { return Err(QueueOperationError::InvalidResponseSignature); } - // Calculate amount of credits to freeze. - let route_len = usize_to_u32(pending_transaction.route.len()) - .ok_or(QueueOperationError::RouteTooLong)?; - let credit_calc = CreditCalculator::new(route_len, pending_transaction.dest_payment); - - // Find ourselves on the route. If we are not there, abort. - let remote_index = pending_transaction - .route - .find_pk_pair( - &self.mutual_credit.state().idents.remote_public_key, - &self.mutual_credit.state().idents.local_public_key, - ) - .unwrap(); - - let local_index = usize_to_u32(remote_index.checked_add(1).unwrap()).unwrap(); + // We expect that the current stage is Request: + if let TransactionStage::Request = pending_transaction.stage { + } else { + return Err(QueueOperationError::NotExpectingResponse); + } - // Remove entry from remote_pending hashmap: let mut mc_mutations = Vec::new(); - let mc_mutation = - McMutation::RemoveRemotePendingTransaction(response_send_funds.request_id); - self.mutual_credit.mutate(&mc_mutation); - mc_mutations.push(mc_mutation); - let success_credits = credit_calc.credits_on_success(local_index).unwrap(); - let freeze_credits = credit_calc.credits_to_freeze(local_index).unwrap(); - - // Decrease frozen credits and increase balance: - let new_remote_pending_debt = self - .mutual_credit - .state() - .balance - .remote_pending_debt - .checked_sub(freeze_credits) - .expect("Insufficient frozen credit!"); - - let mc_mutation = McMutation::SetRemotePendingDebt(new_remote_pending_debt); - self.mutual_credit.mutate(&mc_mutation); - mc_mutations.push(mc_mutation); - - let new_balance = self - .mutual_credit - .state() - .balance - .balance - .checked_add_unsigned(success_credits) - .expect("balance overflow"); - - let mc_mutation = McMutation::SetBalance(new_balance); + // Set the stage to Response, and remember dest_hashed_lock: + let mc_mutation = McMutation::SetRemotePendingTransactionStage(( + response_send_funds.request_id.clone(), + TransactionStage::Response(response_send_funds.dest_hashed_lock.clone()), + )); self.mutual_credit.mutate(&mc_mutation); mc_mutations.push(mc_mutation); @@ -388,4 +354,94 @@ impl OutgoingMc { ) -> Result, QueueOperationError> { unimplemented!(); } + + /* + fn queue_response_send_funds( + &mut self, + response_send_funds: ResponseSendFundsOp, + ) -> Result, QueueOperationError> { + // Make sure that id exists in remote_pending hashmap, + // and access saved request details. + let remote_pending_transactions = &self + .mutual_credit + .state() + .pending_transactions + .pending_remote_requests; + + // Obtain pending request: + let pending_transaction = remote_pending_transactions + .get(&response_send_funds.request_id) + .ok_or(QueueOperationError::RequestDoesNotExist)? + .clone(); + // TODO: Possibly get rid of clone() here for optimization later + + // verify signature: + let response_signature_buffer = + create_response_signature_buffer(&response_send_funds, &pending_transaction); + // The response was signed by the destination node: + let dest_public_key = pending_transaction.route.public_keys.last().unwrap(); + + // Verify response funds signature: + if !verify_signature( + &response_signature_buffer, + dest_public_key, + &response_send_funds.signature, + ) { + return Err(QueueOperationError::InvalidResponseSignature); + } + + // Calculate amount of credits to freeze. + let route_len = usize_to_u32(pending_transaction.route.len()) + .ok_or(QueueOperationError::RouteTooLong)?; + let credit_calc = CreditCalculator::new(route_len, pending_transaction.dest_payment); + + // Find ourselves on the route. If we are not there, abort. + let remote_index = pending_transaction + .route + .find_pk_pair( + &self.mutual_credit.state().idents.remote_public_key, + &self.mutual_credit.state().idents.local_public_key, + ) + .unwrap(); + + let local_index = usize_to_u32(remote_index.checked_add(1).unwrap()).unwrap(); + + // Remove entry from remote_pending hashmap: + let mut mc_mutations = Vec::new(); + let mc_mutation = + McMutation::RemoveRemotePendingTransaction(response_send_funds.request_id); + self.mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); + + let success_credits = credit_calc.credits_on_success(local_index).unwrap(); + let freeze_credits = credit_calc.credits_to_freeze(local_index).unwrap(); + + // Decrease frozen credits and increase balance: + let new_remote_pending_debt = self + .mutual_credit + .state() + .balance + .remote_pending_debt + .checked_sub(freeze_credits) + .expect("Insufficient frozen credit!"); + + let mc_mutation = McMutation::SetRemotePendingDebt(new_remote_pending_debt); + self.mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); + + let new_balance = self + .mutual_credit + .state() + .balance + .balance + .checked_add_unsigned(success_credits) + .expect("balance overflow"); + + let mc_mutation = McMutation::SetBalance(new_balance); + self.mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); + + Ok(mc_mutations) + } + */ } From c16da19da31591ba1ac32d719c577d4c79b03ad0 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 17:37:31 +0300 Subject: [PATCH 033/248] Implemented queue_cancel_send_funds(). --- .../funder/src/mutual_credit/outgoing.rs | 65 +------------------ 1 file changed, 3 insertions(+), 62 deletions(-) diff --git a/components/funder/src/mutual_credit/outgoing.rs b/components/funder/src/mutual_credit/outgoing.rs index 790dc73b8..933ca58bd 100644 --- a/components/funder/src/mutual_credit/outgoing.rs +++ b/components/funder/src/mutual_credit/outgoing.rs @@ -261,48 +261,11 @@ impl OutgoingMc { .get(&cancel_send_funds.request_id) .ok_or(QueueOperationError::RequestDoesNotExist)?; - // Find ourselves on the route. If we are not there, abort. - let remote_index = pending_transaction - .route - .find_pk_pair( - &self.mutual_credit.state().idents.remote_public_key, - &self.mutual_credit.state().idents.local_public_key, - ) + let freeze_credits = pending_transaction + .dest_payment + .checked_add(pending_transaction.left_fees) .unwrap(); - let local_index = remote_index.checked_add(1).unwrap(); - if local_index.checked_add(1).unwrap() == pending_transaction.route.len() { - // Note that we can not be the destination. The destination can not be the sender of a - // failure funds. - return Err(QueueOperationError::FailureSentFromDest); - } - - // Make sure that reporting node public key is: - // - inside the route - // - After us on the route, or us. - // - Not the destination node - unimplemented!(); - - /* - - let reporting_index = pending_transaction - .route - .pk_to_index(&cancel_send_funds.reporting_public_key) - .ok_or(QueueOperationError::ReportingNodeNonexistent)?; - - if reporting_index < local_index { - return Err(QueueOperationError::InvalidReportingNode); - } - - - verify_failure_signature(&cancel_send_funds, &pending_transaction) - .ok_or(QueueOperationError::InvalidFailureSignature)?; - - // At this point we believe the failure funds is valid. - let route_len = usize_to_u32(pending_transaction.route.len()) - .ok_or(QueueOperationError::RouteTooLong)?; - let credit_calc = CreditCalculator::new(route_len, pending_transaction.dest_payment); - // Remove entry from remote hashmap: let mut mc_mutations = Vec::new(); @@ -310,14 +273,6 @@ impl OutgoingMc { self.mutual_credit.mutate(&mc_mutation); mc_mutations.push(mc_mutation); - let local_index = usize_to_u32(local_index).unwrap(); - let reporting_index = usize_to_u32(reporting_index).unwrap(); - - let failure_credits = credit_calc - .credits_on_failure(local_index, reporting_index) - .unwrap(); - let freeze_credits = credit_calc.credits_to_freeze(local_index).unwrap(); - // Decrease frozen credits: let new_remote_pending_debt = self .mutual_credit @@ -331,21 +286,7 @@ impl OutgoingMc { self.mutual_credit.mutate(&mc_mutation); mc_mutations.push(mc_mutation); - // Add to balance: - let new_balance = self - .mutual_credit - .state() - .balance - .balance - .checked_add_unsigned(failure_credits) - .unwrap(); - - let mc_mutation = McMutation::SetBalance(new_balance); - self.mutual_credit.mutate(&mc_mutation); - mc_mutations.push(mc_mutation); - Ok(mc_mutations) - */ } fn queue_commit_send_funds( From 3014b94c660bdf5e61dcf87e02ad01c56631073c Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 17:39:04 +0300 Subject: [PATCH 034/248] Preparation to implement queue_commit_send_funds(). --- components/funder/src/mutual_credit/outgoing.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/components/funder/src/mutual_credit/outgoing.rs b/components/funder/src/mutual_credit/outgoing.rs index 933ca58bd..5a40d75b1 100644 --- a/components/funder/src/mutual_credit/outgoing.rs +++ b/components/funder/src/mutual_credit/outgoing.rs @@ -294,13 +294,8 @@ impl OutgoingMc { commit_send_funds: CommitSendFundsOp, ) -> Result, QueueOperationError> { unimplemented!(); - } - /* - fn queue_response_send_funds( - &mut self, - response_send_funds: ResponseSendFundsOp, - ) -> Result, QueueOperationError> { + /* // Make sure that id exists in remote_pending hashmap, // and access saved request details. let remote_pending_transactions = &self @@ -383,6 +378,6 @@ impl OutgoingMc { mc_mutations.push(mc_mutation); Ok(mc_mutations) + */ } - */ } From c1d5ecf315dd0f973efbf9a3bb21fb912ae98a8e Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 17:46:06 +0300 Subject: [PATCH 035/248] Implemented queue_commit_send_funds(). --- .../funder/src/mutual_credit/incoming.rs | 14 ++-- .../funder/src/mutual_credit/outgoing.rs | 64 ++++++++----------- 2 files changed, 33 insertions(+), 45 deletions(-) diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index 2f70dcd23..96bf2b7e9 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -433,13 +433,6 @@ fn process_commit_send_funds( return Err(ProcessOperationError::InvalidDestPlainLock); } - let mut mc_mutations = Vec::new(); - - // Remove entry from local_pending hashmap: - let mc_mutation = McMutation::RemoveLocalPendingTransaction(commit_send_funds.request_id); - mutual_credit.mutate(&mc_mutation); - mc_mutations.push(mc_mutation); - // Calculate amount of credits that were frozen: let freeze_credits = pending_transaction .dest_payment @@ -448,6 +441,13 @@ fn process_commit_send_funds( // Note: The unwrap() above should never fail, because this was already checked during the // request message processing. + let mut mc_mutations = Vec::new(); + + // Remove entry from local_pending hashmap: + let mc_mutation = McMutation::RemoveLocalPendingTransaction(commit_send_funds.request_id); + mutual_credit.mutate(&mc_mutation); + mc_mutations.push(mc_mutation); + // Decrease frozen credits and decrease balance: let new_local_pending_debt = mutual_credit .state() diff --git a/components/funder/src/mutual_credit/outgoing.rs b/components/funder/src/mutual_credit/outgoing.rs index 5a40d75b1..54c35753e 100644 --- a/components/funder/src/mutual_credit/outgoing.rs +++ b/components/funder/src/mutual_credit/outgoing.rs @@ -36,6 +36,9 @@ pub enum QueueOperationError { FailureSentFromDest, RemoteRequestsClosed, NotExpectingResponse, + NotExpectingCommit, + InvalidSrcPlainLock, + InvalidDestPlainLock, } /// A wrapper over a token channel, accumulating funds to be sent as one transaction. @@ -293,9 +296,6 @@ impl OutgoingMc { &mut self, commit_send_funds: CommitSendFundsOp, ) -> Result, QueueOperationError> { - unimplemented!(); - - /* // Make sure that id exists in remote_pending hashmap, // and access saved request details. let remote_pending_transactions = &self @@ -306,52 +306,37 @@ impl OutgoingMc { // Obtain pending request: let pending_transaction = remote_pending_transactions - .get(&response_send_funds.request_id) + .get(&commit_send_funds.request_id) .ok_or(QueueOperationError::RequestDoesNotExist)? .clone(); // TODO: Possibly get rid of clone() here for optimization later - // verify signature: - let response_signature_buffer = - create_response_signature_buffer(&response_send_funds, &pending_transaction); - // The response was signed by the destination node: - let dest_public_key = pending_transaction.route.public_keys.last().unwrap(); + let dest_hashed_lock = match &pending_transaction.stage { + TransactionStage::Response(dest_hashed_lock) => dest_hashed_lock, + _ => return Err(QueueOperationError::NotExpectingCommit), + }; - // Verify response funds signature: - if !verify_signature( - &response_signature_buffer, - dest_public_key, - &response_send_funds.signature, - ) { - return Err(QueueOperationError::InvalidResponseSignature); + // Verify src_plain_lock and dest_plain_lock: + if commit_send_funds.src_plain_lock.hash() != pending_transaction.src_hashed_lock { + return Err(QueueOperationError::InvalidSrcPlainLock); } - // Calculate amount of credits to freeze. - let route_len = usize_to_u32(pending_transaction.route.len()) - .ok_or(QueueOperationError::RouteTooLong)?; - let credit_calc = CreditCalculator::new(route_len, pending_transaction.dest_payment); + if commit_send_funds.dest_plain_lock.hash() != *dest_hashed_lock { + return Err(QueueOperationError::InvalidDestPlainLock); + } - // Find ourselves on the route. If we are not there, abort. - let remote_index = pending_transaction - .route - .find_pk_pair( - &self.mutual_credit.state().idents.remote_public_key, - &self.mutual_credit.state().idents.local_public_key, - ) + // Calculate amount of credits that were frozen: + let freeze_credits = pending_transaction + .dest_payment + .checked_add(pending_transaction.left_fees) .unwrap(); - let local_index = usize_to_u32(remote_index.checked_add(1).unwrap()).unwrap(); - // Remove entry from remote_pending hashmap: let mut mc_mutations = Vec::new(); - let mc_mutation = - McMutation::RemoveRemotePendingTransaction(response_send_funds.request_id); + let mc_mutation = McMutation::RemoveRemotePendingTransaction(commit_send_funds.request_id); self.mutual_credit.mutate(&mc_mutation); mc_mutations.push(mc_mutation); - let success_credits = credit_calc.credits_on_success(local_index).unwrap(); - let freeze_credits = credit_calc.credits_to_freeze(local_index).unwrap(); - // Decrease frozen credits and increase balance: let new_remote_pending_debt = self .mutual_credit @@ -359,7 +344,9 @@ impl OutgoingMc { .balance .remote_pending_debt .checked_sub(freeze_credits) - .expect("Insufficient frozen credit!"); + .unwrap(); + // Above unwrap() should never fail. This was already checked when a request message was + // received. let mc_mutation = McMutation::SetRemotePendingDebt(new_remote_pending_debt); self.mutual_credit.mutate(&mc_mutation); @@ -370,14 +357,15 @@ impl OutgoingMc { .state() .balance .balance - .checked_add_unsigned(success_credits) - .expect("balance overflow"); + .checked_add_unsigned(freeze_credits) + .unwrap(); + // Above unwrap() should never fail. This was already checked when a request message was + // received. let mc_mutation = McMutation::SetBalance(new_balance); self.mutual_credit.mutate(&mc_mutation); mc_mutations.push(mc_mutation); Ok(mc_mutations) - */ } } From 27a0c2aeb0b3d0037625e6496e71787b4cf2ec25 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 17:47:33 +0300 Subject: [PATCH 036/248] Removed unused QueueOperationError variants. --- components/funder/src/mutual_credit/outgoing.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/components/funder/src/mutual_credit/outgoing.rs b/components/funder/src/mutual_credit/outgoing.rs index 54c35753e..0148703f0 100644 --- a/components/funder/src/mutual_credit/outgoing.rs +++ b/components/funder/src/mutual_credit/outgoing.rs @@ -23,17 +23,11 @@ pub enum QueueOperationError { RemoteMaxDebtTooLarge, InvalidRoute, PkPairNotInRoute, - RouteTooLong, - CreditCalculatorFailure, CreditsCalcOverflow, InsufficientTrust, RequestAlreadyExists, RequestDoesNotExist, InvalidResponseSignature, - ReportingNodeNonexistent, - InvalidReportingNode, - InvalidFailureSignature, - FailureSentFromDest, RemoteRequestsClosed, NotExpectingResponse, NotExpectingCommit, From 88e665b1a359c7c1d6d3cbfc461a88030b2cd0a8 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 17:49:26 +0300 Subject: [PATCH 037/248] Removed credit_calc. --- components/funder/src/lib.rs | 4 ++-- components/funder/src/mutual_credit/outgoing.rs | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index 5f7ed7797..3b5e07e25 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -18,8 +18,8 @@ #[macro_use] extern crate serde_derive; -#[allow(unused)] -mod credit_calc; +// #[allow(unused)] +// mod credit_calc; // mod ephemeral; // mod friend; // mod funder; diff --git a/components/funder/src/mutual_credit/outgoing.rs b/components/funder/src/mutual_credit/outgoing.rs index 0148703f0..694c693c1 100644 --- a/components/funder/src/mutual_credit/outgoing.rs +++ b/components/funder/src/mutual_credit/outgoing.rs @@ -10,7 +10,6 @@ use proto::funder::messages::{ use proto::funder::signature_buff::create_response_signature_buffer; use super::types::{McMutation, MutualCredit, MAX_FUNDER_DEBT}; -use crate::credit_calc::CreditCalculator; /// Processes outgoing funds for a token channel. /// Used to batch as many funds as possible. From f68428f24764256e87de3585069fae273fb87c69 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 17:57:52 +0300 Subject: [PATCH 038/248] Some work on mutual_credit/tests.rs. Not compiling yet. --- components/funder/src/mutual_credit/tests.rs | 25 +++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/components/funder/src/mutual_credit/tests.rs b/components/funder/src/mutual_credit/tests.rs index 0d62dd128..5abf366dd 100644 --- a/components/funder/src/mutual_credit/tests.rs +++ b/components/funder/src/mutual_credit/tests.rs @@ -9,11 +9,10 @@ use crypto::crypto_rand::{RandValue, RAND_VALUE_LEN}; use crypto::invoice_id::{InvoiceId, INVOICE_ID_LEN}; use proto::funder::messages::{ - FailureSendFunds, FriendTcOp, FriendsRoute, RequestSendFunds, RequestsStatus, ResponseSendFunds, -}; -use proto::funder::signature_buff::{ - create_failure_signature_buffer, create_response_signature_buffer, + CancelSendFundsOp, CommitSendFundsOp, FriendTcOp, FriendsRoute, RequestSendFundsOp, + RequestsStatus, ResponseSendFundsOp, }; +use proto::funder::signature_buff::create_response_signature_buffer; use crate::mutual_credit::types::{create_pending_transaction, MutualCredit}; @@ -121,7 +120,7 @@ fn test_request_response_send_funds() { }; let invoice_id = InvoiceId::from(&[0; INVOICE_ID_LEN]); - let request_send_funds = RequestSendFunds { + let request_send_funds = RequestSendFundsOp { request_id: request_id.clone(), route, dest_payment: 10, @@ -145,7 +144,7 @@ fn test_request_response_send_funds() { let rand_nonce = RandValue::from(&[5; RAND_VALUE_LEN]); // TODO: Add signature here: - let mut response_send_funds = ResponseSendFunds { + let mut response_send_funds = ResponseSendFundsOp { request_id: request_id.clone(), rand_nonce: rand_nonce.clone(), signature: Signature::from(&[0; SIGNATURE_LEN]), @@ -196,7 +195,7 @@ fn test_request_failure_send_funds() { }; let invoice_id = InvoiceId::from(&[0; INVOICE_ID_LEN]); - let request_send_funds = RequestSendFunds { + let request_send_funds = RequestSendFundsOp { request_id: request_id.clone(), route, dest_payment: 10, @@ -219,19 +218,11 @@ fn test_request_failure_send_funds() { let rand_nonce = RandValue::from(&[5; RAND_VALUE_LEN]); - let mut failure_send_funds = FailureSendFunds { - request_id, - reporting_public_key: public_key_b.clone(), - rand_nonce, - signature: Signature::from(&[0; SIGNATURE_LEN]), - }; - - let sign_buffer = create_failure_signature_buffer(&failure_send_funds, &pending_transaction); - failure_send_funds.signature = identity.sign(&sign_buffer); + let mut cancel_send_funds = CancelSendFundsOp { request_id }; apply_incoming( &mut mutual_credit, - FriendTcOp::FailureSendFunds(failure_send_funds), + FriendTcOp::CancelSendFunds(cancel_send_funds), ) .unwrap(); From f1cb967a09f01cba252042795c2925cfe12d6e27 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 19:17:57 +0300 Subject: [PATCH 039/248] Added create_pending_transaction(). --- components/funder/src/mutual_credit/incoming.rs | 14 +++++--------- components/funder/src/mutual_credit/outgoing.rs | 12 ++---------- components/funder/src/mutual_credit/types.rs | 12 ++++++++++++ 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index 96bf2b7e9..516829462 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -9,7 +9,7 @@ use proto::funder::messages::{ }; use proto::funder::signature_buff::create_response_signature_buffer; -use super::types::{McMutation, MutualCredit, MAX_FUNDER_DEBT}; +use super::types::{create_pending_transaction, McMutation, MutualCredit, MAX_FUNDER_DEBT}; #[derive(Debug)] pub struct IncomingResponseSendFundsOp { @@ -259,14 +259,10 @@ fn process_request_send_funds( } // Add pending transaction: - let pending_transaction = PendingTransaction { - request_id: request_send_funds.request_id, - route: request_send_funds.route.clone(), - dest_payment: request_send_funds.dest_payment, - invoice_id: request_send_funds.invoice_id.clone(), - left_fees: new_left_fees, - src_hashed_lock: request_send_funds.src_hashed_lock.clone(), - stage: TransactionStage::Request, + let pending_transaction = { + let mut pending_transaction = create_pending_transaction(&request_send_funds); + pending_transaction.left_fees = new_left_fees; + pending_transaction }; // let pending_friend_request = create_pending_transaction(&request_send_funds); diff --git a/components/funder/src/mutual_credit/outgoing.rs b/components/funder/src/mutual_credit/outgoing.rs index 694c693c1..764ce7e80 100644 --- a/components/funder/src/mutual_credit/outgoing.rs +++ b/components/funder/src/mutual_credit/outgoing.rs @@ -9,7 +9,7 @@ use proto::funder::messages::{ }; use proto::funder::signature_buff::create_response_signature_buffer; -use super::types::{McMutation, MutualCredit, MAX_FUNDER_DEBT}; +use super::types::{create_pending_transaction, McMutation, MutualCredit, MAX_FUNDER_DEBT}; /// Processes outgoing funds for a token channel. /// Used to batch as many funds as possible. @@ -164,15 +164,7 @@ impl OutgoingMc { } // Add pending transaction: - let pending_transaction = PendingTransaction { - request_id: request_send_funds.request_id, - route: request_send_funds.route.clone(), - dest_payment: request_send_funds.dest_payment, - invoice_id: request_send_funds.invoice_id.clone(), - left_fees: request_send_funds.left_fees, - src_hashed_lock: request_send_funds.src_hashed_lock.clone(), - stage: TransactionStage::Request, - }; + let pending_transaction = create_pending_transaction(&request_send_funds); let mut mc_mutations = Vec::new(); let mc_mutation = McMutation::InsertLocalPendingTransaction(pending_transaction); diff --git a/components/funder/src/mutual_credit/types.rs b/components/funder/src/mutual_credit/types.rs index 908c01de7..723a25534 100644 --- a/components/funder/src/mutual_credit/types.rs +++ b/components/funder/src/mutual_credit/types.rs @@ -339,3 +339,15 @@ impl MutualCredit { .stage = stage; } } + +pub fn create_pending_transaction(request_send_funds: &RequestSendFundsOp) -> PendingTransaction { + PendingTransaction { + request_id: request_send_funds.request_id, + route: request_send_funds.route.clone(), + dest_payment: request_send_funds.dest_payment, + invoice_id: request_send_funds.invoice_id.clone(), + left_fees: request_send_funds.left_fees, + src_hashed_lock: request_send_funds.src_hashed_lock.clone(), + stage: TransactionStage::Request, + } +} From a4e4f87b0d98098aa3db6bddda8667ce73c7b075 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 19:19:01 +0300 Subject: [PATCH 040/248] Two basic mutual credit tests pass. --- components/funder/src/mutual_credit/tests.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/components/funder/src/mutual_credit/tests.rs b/components/funder/src/mutual_credit/tests.rs index 5abf366dd..674fda8b2 100644 --- a/components/funder/src/mutual_credit/tests.rs +++ b/components/funder/src/mutual_credit/tests.rs @@ -92,6 +92,8 @@ fn test_outgoing_set_remote_max_debt() { assert_eq!(mutual_credit.state().balance.remote_max_debt, 20); } +/* + #[test] fn test_request_response_send_funds() { let local_public_key = PublicKey::from(&[0xaa; PUBLIC_KEY_LEN]); @@ -232,3 +234,4 @@ fn test_request_failure_send_funds() { assert_eq!(mutual_credit.state().balance.local_pending_debt, 0); assert_eq!(mutual_credit.state().balance.remote_pending_debt, 0); } +*/ From 8decce74368af3befd2be8cc53c20fd5f283c610 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 19:23:33 +0300 Subject: [PATCH 041/248] test_request_response_send_funds() compiles but fails. --- components/funder/src/mutual_credit/tests.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/components/funder/src/mutual_credit/tests.rs b/components/funder/src/mutual_credit/tests.rs index 674fda8b2..ce2ab0c31 100644 --- a/components/funder/src/mutual_credit/tests.rs +++ b/components/funder/src/mutual_credit/tests.rs @@ -1,3 +1,4 @@ +use crypto::hash_lock::{PlainLock, PLAIN_LOCK_LEN}; use crypto::identity::{ generate_pkcs8_key_pair, Identity, Signature, SoftwareEd25519Identity, SIGNATURE_LEN, }; @@ -92,8 +93,6 @@ fn test_outgoing_set_remote_max_debt() { assert_eq!(mutual_credit.state().balance.remote_max_debt, 20); } -/* - #[test] fn test_request_response_send_funds() { let local_public_key = PublicKey::from(&[0xaa; PUBLIC_KEY_LEN]); @@ -121,12 +120,15 @@ fn test_request_response_send_funds() { ], }; let invoice_id = InvoiceId::from(&[0; INVOICE_ID_LEN]); + let src_plain_lock = PlainLock::from(&[1; PLAIN_LOCK_LEN]); let request_send_funds = RequestSendFundsOp { request_id: request_id.clone(), + src_hashed_lock: src_plain_lock.hash(), route, dest_payment: 10, invoice_id, + left_fees: 5, }; let pending_transaction = create_pending_transaction(&request_send_funds); @@ -144,10 +146,12 @@ fn test_request_response_send_funds() { assert_eq!(mutual_credit.state().balance.remote_pending_debt, 0); let rand_nonce = RandValue::from(&[5; RAND_VALUE_LEN]); + let dest_plain_lock = PlainLock::from(&[2; PLAIN_LOCK_LEN]); // TODO: Add signature here: let mut response_send_funds = ResponseSendFundsOp { request_id: request_id.clone(), + dest_hashed_lock: dest_plain_lock.hash(), rand_nonce: rand_nonce.clone(), signature: Signature::from(&[0; SIGNATURE_LEN]), }; @@ -168,6 +172,7 @@ fn test_request_response_send_funds() { assert_eq!(mutual_credit.state().balance.local_pending_debt, 0); assert_eq!(mutual_credit.state().balance.remote_pending_debt, 0); } +/* #[test] fn test_request_failure_send_funds() { From 375e711d3cb9a616c7d0d144675e7a473410061e Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 19:31:00 +0300 Subject: [PATCH 042/248] Fixed asserts in test_request_response_commit_send_funds(). --- components/funder/src/mutual_credit/tests.rs | 23 +++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/components/funder/src/mutual_credit/tests.rs b/components/funder/src/mutual_credit/tests.rs index ce2ab0c31..51010ba96 100644 --- a/components/funder/src/mutual_credit/tests.rs +++ b/components/funder/src/mutual_credit/tests.rs @@ -94,18 +94,24 @@ fn test_outgoing_set_remote_max_debt() { } #[test] -fn test_request_response_send_funds() { +fn test_request_response_commit_send_funds() { let local_public_key = PublicKey::from(&[0xaa; PUBLIC_KEY_LEN]); let remote_public_key = PublicKey::from(&[0xbb; PUBLIC_KEY_LEN]); let balance = 0; let mut mutual_credit = MutualCredit::new(&local_public_key, &remote_public_key, balance); + // -----[SetRemoteMaxDebt]------ + // ----------------------------- // Make enough trust from remote side, so that we will be able to send credits: apply_incoming(&mut mutual_credit, FriendTcOp::SetRemoteMaxDebt(100)).unwrap(); + // -----[EnableRequests]-------- + // ----------------------------- // Remote side should open his requests status: apply_incoming(&mut mutual_credit, FriendTcOp::EnableRequests).unwrap(); + // -----[RequestSendFunds]-------- + // ----------------------------- let rng = DummyRandom::new(&[1u8]); let pkcs8 = generate_pkcs8_key_pair(&rng); let identity = SoftwareEd25519Identity::from_pkcs8(&pkcs8).unwrap(); @@ -141,10 +147,12 @@ fn test_request_response_send_funds() { assert_eq!(mutual_credit.state().balance.balance, 0); assert_eq!(mutual_credit.state().balance.local_max_debt, 100); assert_eq!(mutual_credit.state().balance.remote_max_debt, 0); - let local_pending_debt = mutual_credit.state().balance.local_pending_debt; - assert!(local_pending_debt > 0); + assert_eq!(mutual_credit.state().balance.local_pending_debt, 10 + 5); assert_eq!(mutual_credit.state().balance.remote_pending_debt, 0); + // -----[ResponseSendFunds]-------- + // -------------------------------- + let rand_nonce = RandValue::from(&[5; RAND_VALUE_LEN]); let dest_plain_lock = PlainLock::from(&[2; PLAIN_LOCK_LEN]); @@ -165,12 +173,15 @@ fn test_request_response_send_funds() { ) .unwrap(); - let balance = mutual_credit.state().balance.balance; - assert_eq!(balance, -(local_pending_debt as i128)); + // We expect that no changes to balance happened yet: + assert_eq!(mutual_credit.state().balance.balance, 0); assert_eq!(mutual_credit.state().balance.local_max_debt, 100); assert_eq!(mutual_credit.state().balance.remote_max_debt, 0); - assert_eq!(mutual_credit.state().balance.local_pending_debt, 0); + assert_eq!(mutual_credit.state().balance.local_pending_debt, 10 + 5); assert_eq!(mutual_credit.state().balance.remote_pending_debt, 0); + + // -----[CommitSendFunds]-------- + // -------------------------------- } /* From 8efb5bb43f8361efb863c41ad044456dac7e3e3e Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 19:33:47 +0300 Subject: [PATCH 043/248] Added commit part to test_request_response_commit_send_funds(). --- components/funder/src/mutual_credit/tests.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/components/funder/src/mutual_credit/tests.rs b/components/funder/src/mutual_credit/tests.rs index 51010ba96..68f480b49 100644 --- a/components/funder/src/mutual_credit/tests.rs +++ b/components/funder/src/mutual_credit/tests.rs @@ -182,6 +182,24 @@ fn test_request_response_commit_send_funds() { // -----[CommitSendFunds]-------- // -------------------------------- + let commit_send_funds = CommitSendFundsOp { + request_id, + src_plain_lock, + dest_plain_lock, + }; + + apply_incoming( + &mut mutual_credit, + FriendTcOp::CommitSendFunds(commit_send_funds), + ) + .unwrap(); + + // We expect that no changes to balance happened yet: + assert_eq!(mutual_credit.state().balance.balance, -15); + assert_eq!(mutual_credit.state().balance.local_max_debt, 100); + assert_eq!(mutual_credit.state().balance.remote_max_debt, 0); + assert_eq!(mutual_credit.state().balance.local_pending_debt, 0); + assert_eq!(mutual_credit.state().balance.remote_pending_debt, 0); } /* From 5f1fd55a637185cc3be59d203e4d2a1a99ff15b0 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 19:37:40 +0300 Subject: [PATCH 044/248] Added test_request_cancel_send_funds(). --- components/funder/src/mutual_credit/tests.rs | 21 ++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/components/funder/src/mutual_credit/tests.rs b/components/funder/src/mutual_credit/tests.rs index 68f480b49..9c395bc3e 100644 --- a/components/funder/src/mutual_credit/tests.rs +++ b/components/funder/src/mutual_credit/tests.rs @@ -152,7 +152,6 @@ fn test_request_response_commit_send_funds() { // -----[ResponseSendFunds]-------- // -------------------------------- - let rand_nonce = RandValue::from(&[5; RAND_VALUE_LEN]); let dest_plain_lock = PlainLock::from(&[2; PLAIN_LOCK_LEN]); @@ -201,10 +200,9 @@ fn test_request_response_commit_send_funds() { assert_eq!(mutual_credit.state().balance.local_pending_debt, 0); assert_eq!(mutual_credit.state().balance.remote_pending_debt, 0); } -/* #[test] -fn test_request_failure_send_funds() { +fn test_request_cancel_send_funds() { let rng = DummyRandom::new(&[1u8]); let pkcs8 = generate_pkcs8_key_pair(&rng); let identity = SoftwareEd25519Identity::from_pkcs8(&pkcs8).unwrap(); @@ -215,12 +213,18 @@ fn test_request_failure_send_funds() { let balance = 0; let mut mutual_credit = MutualCredit::new(&local_public_key, &remote_public_key, balance); + // -----[SetRemoteMaxDebt]------ + // ----------------------------- // Make enough trust from remote side, so that we will be able to send credits: apply_incoming(&mut mutual_credit, FriendTcOp::SetRemoteMaxDebt(100)).unwrap(); + // -----[EnableRequests]-------- + // ----------------------------- // Remote side should open his requests status: apply_incoming(&mut mutual_credit, FriendTcOp::EnableRequests).unwrap(); + // -----[RequestSendFunds]-------- + // ----------------------------- let request_id = Uid::from(&[3; UID_LEN]); let route = FriendsRoute { public_keys: vec![ @@ -230,12 +234,15 @@ fn test_request_failure_send_funds() { ], }; let invoice_id = InvoiceId::from(&[0; INVOICE_ID_LEN]); + let src_plain_lock = PlainLock::from(&[1; PLAIN_LOCK_LEN]); let request_send_funds = RequestSendFundsOp { request_id: request_id.clone(), + src_hashed_lock: src_plain_lock.hash(), route, dest_payment: 10, invoice_id, + left_fees: 5, }; let pending_transaction = create_pending_transaction(&request_send_funds); @@ -248,12 +255,11 @@ fn test_request_failure_send_funds() { assert_eq!(mutual_credit.state().balance.balance, 0); assert_eq!(mutual_credit.state().balance.local_max_debt, 100); assert_eq!(mutual_credit.state().balance.remote_max_debt, 0); - let local_pending_debt = mutual_credit.state().balance.local_pending_debt; - assert!(local_pending_debt > 0); + assert_eq!(mutual_credit.state().balance.local_pending_debt, 10 + 5); assert_eq!(mutual_credit.state().balance.remote_pending_debt, 0); - let rand_nonce = RandValue::from(&[5; RAND_VALUE_LEN]); - + // -----[CancelSendFunds]-------- + // ------------------------------ let mut cancel_send_funds = CancelSendFundsOp { request_id }; apply_incoming( @@ -268,4 +274,3 @@ fn test_request_failure_send_funds() { assert_eq!(mutual_credit.state().balance.local_pending_debt, 0); assert_eq!(mutual_credit.state().balance.remote_pending_debt, 0); } -*/ From a67d118271712bf86fc192562d4b68751e2f094e Mon Sep 17 00:00:00 2001 From: real Date: Wed, 15 May 2019 19:45:34 +0300 Subject: [PATCH 045/248] Added test_request_response_cancel_send_funds(). --- components/funder/src/mutual_credit/tests.rs | 103 ++++++++++++++++++- 1 file changed, 102 insertions(+), 1 deletion(-) diff --git a/components/funder/src/mutual_credit/tests.rs b/components/funder/src/mutual_credit/tests.rs index 9c395bc3e..b8e1b3b0b 100644 --- a/components/funder/src/mutual_credit/tests.rs +++ b/components/funder/src/mutual_credit/tests.rs @@ -155,7 +155,6 @@ fn test_request_response_commit_send_funds() { let rand_nonce = RandValue::from(&[5; RAND_VALUE_LEN]); let dest_plain_lock = PlainLock::from(&[2; PLAIN_LOCK_LEN]); - // TODO: Add signature here: let mut response_send_funds = ResponseSendFundsOp { request_id: request_id.clone(), dest_hashed_lock: dest_plain_lock.hash(), @@ -274,3 +273,105 @@ fn test_request_cancel_send_funds() { assert_eq!(mutual_credit.state().balance.local_pending_debt, 0); assert_eq!(mutual_credit.state().balance.remote_pending_debt, 0); } + +#[test] +fn test_request_response_cancel_send_funds() { + let local_public_key = PublicKey::from(&[0xaa; PUBLIC_KEY_LEN]); + let remote_public_key = PublicKey::from(&[0xbb; PUBLIC_KEY_LEN]); + let balance = 0; + let mut mutual_credit = MutualCredit::new(&local_public_key, &remote_public_key, balance); + + // -----[SetRemoteMaxDebt]------ + // ----------------------------- + // Make enough trust from remote side, so that we will be able to send credits: + apply_incoming(&mut mutual_credit, FriendTcOp::SetRemoteMaxDebt(100)).unwrap(); + + // -----[EnableRequests]-------- + // ----------------------------- + // Remote side should open his requests status: + apply_incoming(&mut mutual_credit, FriendTcOp::EnableRequests).unwrap(); + + // -----[RequestSendFunds]-------- + // ----------------------------- + let rng = DummyRandom::new(&[1u8]); + let pkcs8 = generate_pkcs8_key_pair(&rng); + let identity = SoftwareEd25519Identity::from_pkcs8(&pkcs8).unwrap(); + let public_key_c = identity.get_public_key(); + + let request_id = Uid::from(&[3; UID_LEN]); + let route = FriendsRoute { + public_keys: vec![ + PublicKey::from(&[0xaa; PUBLIC_KEY_LEN]), + PublicKey::from(&[0xbb; PUBLIC_KEY_LEN]), + public_key_c.clone(), + ], + }; + let invoice_id = InvoiceId::from(&[0; INVOICE_ID_LEN]); + let src_plain_lock = PlainLock::from(&[1; PLAIN_LOCK_LEN]); + + let request_send_funds = RequestSendFundsOp { + request_id: request_id.clone(), + src_hashed_lock: src_plain_lock.hash(), + route, + dest_payment: 10, + invoice_id, + left_fees: 5, + }; + + let pending_transaction = create_pending_transaction(&request_send_funds); + apply_outgoing( + &mut mutual_credit, + &FriendTcOp::RequestSendFunds(request_send_funds), + ) + .unwrap(); + + assert_eq!(mutual_credit.state().balance.balance, 0); + assert_eq!(mutual_credit.state().balance.local_max_debt, 100); + assert_eq!(mutual_credit.state().balance.remote_max_debt, 0); + assert_eq!(mutual_credit.state().balance.local_pending_debt, 10 + 5); + assert_eq!(mutual_credit.state().balance.remote_pending_debt, 0); + + // -----[ResponseSendFunds]-------- + // -------------------------------- + let rand_nonce = RandValue::from(&[5; RAND_VALUE_LEN]); + let dest_plain_lock = PlainLock::from(&[2; PLAIN_LOCK_LEN]); + + let mut response_send_funds = ResponseSendFundsOp { + request_id: request_id.clone(), + dest_hashed_lock: dest_plain_lock.hash(), + rand_nonce: rand_nonce.clone(), + signature: Signature::from(&[0; SIGNATURE_LEN]), + }; + + let sign_buffer = create_response_signature_buffer(&response_send_funds, &pending_transaction); + response_send_funds.signature = identity.sign(&sign_buffer); + + apply_incoming( + &mut mutual_credit, + FriendTcOp::ResponseSendFunds(response_send_funds), + ) + .unwrap(); + + // We expect that no changes to balance happened yet: + assert_eq!(mutual_credit.state().balance.balance, 0); + assert_eq!(mutual_credit.state().balance.local_max_debt, 100); + assert_eq!(mutual_credit.state().balance.remote_max_debt, 0); + assert_eq!(mutual_credit.state().balance.local_pending_debt, 10 + 5); + assert_eq!(mutual_credit.state().balance.remote_pending_debt, 0); + + // -----[CancelSendFunds]-------- + // ------------------------------ + let mut cancel_send_funds = CancelSendFundsOp { request_id }; + + apply_incoming( + &mut mutual_credit, + FriendTcOp::CancelSendFunds(cancel_send_funds), + ) + .unwrap(); + + assert_eq!(mutual_credit.state().balance.balance, 0); + assert_eq!(mutual_credit.state().balance.local_max_debt, 100); + assert_eq!(mutual_credit.state().balance.remote_max_debt, 0); + assert_eq!(mutual_credit.state().balance.local_pending_debt, 0); + assert_eq!(mutual_credit.state().balance.remote_pending_debt, 0); +} From 6785f29af299a85e6e9f6d728f2242425abf2de4 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 11:13:40 +0300 Subject: [PATCH 046/248] Re-added funder/src/types.rs. --- components/funder/src/lib.rs | 2 +- .../funder/src/mutual_credit/incoming.rs | 4 +- .../funder/src/mutual_credit/outgoing.rs | 4 +- components/funder/src/mutual_credit/tests.rs | 3 +- components/funder/src/mutual_credit/types.rs | 33 +------- components/funder/src/types.rs | 76 +++++++++---------- 6 files changed, 45 insertions(+), 77 deletions(-) diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index 3b5e07e25..be1e80a21 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -32,7 +32,7 @@ mod mutual_credit; // #[cfg(test)] // mod tests; // mod token_channel; -// pub mod types; +pub mod types; // pub use self::funder::{funder_loop, FunderError}; // pub use self::state::{FunderMutation, FunderState}; diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index 516829462..84b0fad5d 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -9,7 +9,9 @@ use proto::funder::messages::{ }; use proto::funder::signature_buff::create_response_signature_buffer; -use super::types::{create_pending_transaction, McMutation, MutualCredit, MAX_FUNDER_DEBT}; +use crate::types::create_pending_transaction; + +use super::types::{McMutation, MutualCredit, MAX_FUNDER_DEBT}; #[derive(Debug)] pub struct IncomingResponseSendFundsOp { diff --git a/components/funder/src/mutual_credit/outgoing.rs b/components/funder/src/mutual_credit/outgoing.rs index 764ce7e80..e1fd317a4 100644 --- a/components/funder/src/mutual_credit/outgoing.rs +++ b/components/funder/src/mutual_credit/outgoing.rs @@ -9,7 +9,9 @@ use proto::funder::messages::{ }; use proto::funder::signature_buff::create_response_signature_buffer; -use super::types::{create_pending_transaction, McMutation, MutualCredit, MAX_FUNDER_DEBT}; +use crate::types::create_pending_transaction; + +use super::types::{McMutation, MutualCredit, MAX_FUNDER_DEBT}; /// Processes outgoing funds for a token channel. /// Used to batch as many funds as possible. diff --git a/components/funder/src/mutual_credit/tests.rs b/components/funder/src/mutual_credit/tests.rs index b8e1b3b0b..4f588dc4a 100644 --- a/components/funder/src/mutual_credit/tests.rs +++ b/components/funder/src/mutual_credit/tests.rs @@ -15,12 +15,13 @@ use proto::funder::messages::{ }; use proto::funder::signature_buff::create_response_signature_buffer; -use crate::mutual_credit::types::{create_pending_transaction, MutualCredit}; +use crate::types::create_pending_transaction; use crate::mutual_credit::incoming::{ process_operation, ProcessOperationError, ProcessOperationOutput, }; use crate::mutual_credit::outgoing::{OutgoingMc, QueueOperationError}; +use crate::mutual_credit::types::MutualCredit; /// Helper function for applying an outgoing operation over a token channel. fn apply_outgoing( diff --git a/components/funder/src/mutual_credit/types.rs b/components/funder/src/mutual_credit/types.rs index 723a25534..bdafaf3f0 100644 --- a/components/funder/src/mutual_credit/types.rs +++ b/components/funder/src/mutual_credit/types.rs @@ -303,25 +303,7 @@ impl MutualCredit { fn set_rate(&mut self, rate: Rate) { self.state.rate = rate; } - /* - /// Keep information from a RequestSendFunds message. - /// This information will be used later to deal with a corresponding {Response,Failure}SendFunds messages, - /// as those messages do not repeat the information sent in the request. - fn create_pending_transaction( - &self, - request_send_funds: &RequestSendFundsOp, - ) -> Result { - Ok(PendingTransaction { - request_id: request_send_funds.request_id, - route: request_send_funds.route.clone(), - dest_payment: request_send_funds.dest_payment, - invoice_id: request_send_funds.invoice_id.clone(), - fee: self.state.rate.calc_fee(request_send_funds.dest_payment)?, - src_hashed_lock: request_send_funds.src_hashed_lock.clone(), - stage: TransactionStage::Request, - }) - } - */ + fn set_local_pending_transaction_stage(&mut self, request_id: &Uid, stage: TransactionStage) { self.state .pending_transactions @@ -330,6 +312,7 @@ impl MutualCredit { .unwrap() .stage = stage; } + fn set_remote_pending_transaction_stage(&mut self, request_id: &Uid, stage: TransactionStage) { self.state .pending_transactions @@ -339,15 +322,3 @@ impl MutualCredit { .stage = stage; } } - -pub fn create_pending_transaction(request_send_funds: &RequestSendFundsOp) -> PendingTransaction { - PendingTransaction { - request_id: request_send_funds.request_id, - route: request_send_funds.route.clone(), - dest_payment: request_send_funds.dest_payment, - invoice_id: request_send_funds.invoice_id.clone(), - left_fees: request_send_funds.left_fees, - src_hashed_lock: request_send_funds.src_hashed_lock.clone(), - stage: TransactionStage::Request, - } -} diff --git a/components/funder/src/types.rs b/components/funder/src/types.rs index 3222dcd1c..473e3404e 100644 --- a/components/funder/src/types.rs +++ b/components/funder/src/types.rs @@ -2,23 +2,24 @@ use common::canonical_serialize::CanonicalSerialize; use crypto::crypto_rand::RandValue; use crypto::hash::HashResult; +use crypto::hash_lock::HashedLock; use crypto::identity::{PublicKey, Signature}; use proto::app_server::messages::RelayAddress; use proto::funder::messages::{ - ChannelerUpdateFriend, FailureSendFunds, FriendMessage, FriendTcOp, FunderIncomingControl, - FunderOutgoingControl, MoveToken, PendingTransaction, RequestSendFunds, ResponseSendFunds, + ChannelerUpdateFriend, CancelSendFundsOp, FriendMessage, FriendTcOp, FunderIncomingControl, + FunderOutgoingControl, MoveToken, PendingTransaction, RequestSendFundsOp, ResponseSendFundsOp, + CommitSendFundsOp, TransactionStage, }; use proto::funder::signature_buff::{ - create_failure_signature_buffer, create_response_signature_buffer, move_token_signature_buff, + create_response_signature_buffer, move_token_signature_buff, prefix_hash, }; use identity::IdentityClient; -pub type UnsignedFailureSendFunds = FailureSendFunds<()>; -pub type UnsignedResponseSendFunds = ResponseSendFunds<()>; +pub type UnsignedResponseSendFundsOp = ResponseSendFundsOp<()>; pub type UnsignedMoveToken = MoveToken; pub async fn sign_move_token<'a, B>( @@ -49,11 +50,14 @@ where pub async fn create_response_send_funds<'a>( pending_transaction: &'a PendingTransaction, + dest_hashed_lock: HashedLock, rand_nonce: RandValue, identity_client: &'a mut IdentityClient, -) -> ResponseSendFunds { - let u_response_send_funds = ResponseSendFunds { +) -> ResponseSendFundsOp { + + let u_response_send_funds = ResponseSendFundsOp { request_id: pending_transaction.request_id, + dest_hashed_lock, rand_nonce, signature: (), }; @@ -61,34 +65,33 @@ pub async fn create_response_send_funds<'a>( let signature_buff = create_response_signature_buffer(&u_response_send_funds, pending_transaction); let signature = await!(identity_client.request_signature(signature_buff)).unwrap(); - ResponseSendFunds { + ResponseSendFundsOp { request_id: u_response_send_funds.request_id, + dest_hashed_lock: u_response_send_funds.dest_hashed_lock, rand_nonce: u_response_send_funds.rand_nonce, signature, } } -pub async fn create_failure_send_funds<'a>( - pending_transaction: &'a PendingTransaction, - local_public_key: &'a PublicKey, - rand_nonce: RandValue, - identity_client: &'a mut IdentityClient, -) -> FailureSendFunds { - let u_failure_send_funds = FailureSendFunds { +pub fn create_cancel_send_funds( + pending_transaction: &PendingTransaction, +) -> CancelSendFundsOp { + + CancelSendFundsOp { request_id: pending_transaction.request_id, - reporting_public_key: local_public_key.clone(), - rand_nonce, - signature: (), - }; + } +} - let signature_buff = create_failure_signature_buffer(&u_failure_send_funds, pending_transaction); - let signature = await!(identity_client.request_signature(signature_buff)).unwrap(); - FailureSendFunds { - request_id: u_failure_send_funds.request_id, - reporting_public_key: u_failure_send_funds.reporting_public_key, - rand_nonce: u_failure_send_funds.rand_nonce, - signature, +pub fn create_pending_transaction(request_send_funds: &RequestSendFundsOp) -> PendingTransaction { + PendingTransaction { + request_id: request_send_funds.request_id, + route: request_send_funds.route.clone(), + dest_payment: request_send_funds.dest_payment, + invoice_id: request_send_funds.invoice_id.clone(), + left_fees: request_send_funds.left_fees, + src_hashed_lock: request_send_funds.src_hashed_lock.clone(), + stage: TransactionStage::Request, } } @@ -97,24 +100,13 @@ pub enum UnsignedFriendTcOp { EnableRequests, DisableRequests, SetRemoteMaxDebt(u128), - RequestSendFunds(RequestSendFunds), - ResponseSendFunds(ResponseSendFunds), - UnsignedResponseSendFunds(UnsignedResponseSendFunds), - FailureSendFunds(FailureSendFunds), - UnsignedFailureSendFunds(UnsignedFailureSendFunds), + RequestSendFunds(RequestSendFundsOp), + ResponseSendFunds(ResponseSendFundsOp), + UnsignedResponseSendFunds(UnsignedResponseSendFundsOp), + CancelSendFunds(CancelSendFundsOp), + CommitSendFunds(CommitSendFundsOp), } -/// Keep information from a RequestSendFunds message. -/// This information will be used later to deal with a corresponding {Response,Failure}SendFunds messages, -/// as those messages do not repeat the information sent in the request. -pub fn create_pending_transaction(request_send_funds: &RequestSendFunds) -> PendingTransaction { - PendingTransaction { - request_id: request_send_funds.request_id, - route: request_send_funds.route.clone(), - dest_payment: request_send_funds.dest_payment, - invoice_id: request_send_funds.invoice_id.clone(), - } -} #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] pub struct MoveTokenHashed { From 3c308cff840fd79f9f75b4c05ab4a772ae15540a Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 11:17:17 +0300 Subject: [PATCH 047/248] Re-added token_channel to funder. --- components/funder/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index be1e80a21..924319542 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -31,7 +31,8 @@ mod mutual_credit; // mod state; // #[cfg(test)] // mod tests; -// mod token_channel; +#[allow(unused)] +mod token_channel; pub mod types; // pub use self::funder::{funder_loop, FunderError}; From 541c64f29f6a925837647681f23b8cbac842a00d Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 11:18:32 +0300 Subject: [PATCH 048/248] Re-added liveness to funder. --- components/funder/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index 924319542..22b5d2281 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -24,7 +24,8 @@ extern crate serde_derive; // mod friend; // mod funder; // mod handler; -// mod liveness; +#[allow(unused)] +mod liveness; #[allow(unused)] mod mutual_credit; // pub mod report; From 88c246413c815693acc3207835b6917790774509 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 11:19:28 +0300 Subject: [PATCH 049/248] Re-added ephemeral to funder. --- components/funder/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index 22b5d2281..6cf4c8807 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -20,7 +20,8 @@ extern crate serde_derive; // #[allow(unused)] // mod credit_calc; -// mod ephemeral; +#[allow(unused)] +mod ephemeral; // mod friend; // mod funder; // mod handler; From 9b05229f1984cbc9dba46c9b7a049be8de84d642 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 11:30:14 +0300 Subject: [PATCH 050/248] Initial re-addition of friend.rs to funder. Not done yet. --- components/funder/src/friend.rs | 37 ++++++++++++++++++++++++--------- components/funder/src/lib.rs | 3 ++- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/components/funder/src/friend.rs b/components/funder/src/friend.rs index 45e4f3dc8..c07c0f7fa 100644 --- a/components/funder/src/friend.rs +++ b/components/funder/src/friend.rs @@ -8,8 +8,8 @@ use common::safe_arithmetic::SafeUnsignedArithmetic; use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ - FailureSendFunds, FriendStatus, PendingRequest, RequestSendFunds, RequestsStatus, ResetTerms, - ResponseSendFunds, + CancelSendFundsOp, CommitSendFundsOp, FriendStatus, PendingTransaction, RequestSendFundsOp, + RequestsStatus, ResetTerms, ResponseSendFundsOp, }; use crate::token_channel::{TcMutation, TokenChannel}; @@ -17,10 +17,10 @@ use crate::types::MoveTokenHashed; #[derive(Clone, Serialize, Deserialize, Debug)] pub enum ResponseOp { - Response(ResponseSendFunds), - UnsignedResponse(PendingRequest), - Failure(FailureSendFunds), - UnsignedFailure(PendingRequest), + Response(ResponseSendFundsOp), + UnsignedResponse(PendingTransaction), + Cancel(CancelSendFundsOp), + Commit(CommitSendFundsOp), } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -71,11 +71,11 @@ pub enum FriendMutation { SetConsistent(TokenChannel), SetWantedRemoteMaxDebt(u128), SetWantedLocalRequestsStatus(RequestsStatus), - PushBackPendingRequest(RequestSendFunds), + PushBackPendingRequest(RequestSendFundsOp), PopFrontPendingRequest, PushBackPendingResponse(ResponseOp), PopFrontPendingResponse, - PushBackPendingUserRequest(RequestSendFunds), + PushBackPendingUserRequest(RequestSendFundsOp), PopFrontPendingUserRequest, SetStatus(FriendStatus), SetRemoteRelays(Vec>), @@ -115,19 +115,36 @@ where #[derive(Clone, Serialize, Deserialize, Debug)] pub struct FriendState { + /// Public key of this node pub local_public_key: PublicKey, + /// Public key of the friend node pub remote_public_key: PublicKey, + /// Relays on which the friend node can be found. + /// This list of relays corresponds to the last report of relays we got from the remote friend. pub remote_relays: Vec>, + /// The last list of our used relays we have sent to the remote friend. + /// We maintain this list to deal with relays drift. pub sent_local_relays: SentLocalRelays, + /// Locally maintained name of the remote friend node. pub name: String, + /// Mutual credit channel information pub channel_status: ChannelStatus, + /// Wanted credit frame for the remote side (Set by the user of this node) + /// It might take a while until this value is applied, as it needs to be communicated to the + /// remote side. pub wanted_remote_max_debt: u128, + /// Can the remote friend send requests through us? This is a value chosen by the user, and it + /// might take some time until it is applied (As it should be communicated to the remote + /// friend). pub wanted_local_requests_status: RequestsStatus, - pub pending_requests: ImVec, + /// A queue of requests that need to be sent to the remote friend + pub pending_requests: ImVec, + /// A queue of non-requests messages (Response, Cancel, Commit) that need to be sent to the remote side + // TODO: Pick a better name for this queue? pub pending_responses: ImVec, // Pending operations to be sent to the token channel. pub status: FriendStatus, - pub pending_user_requests: ImVec, + pub pending_user_requests: ImVec, // Request that the user has sent to this neighbor, // but have not been processed yet. Bounded in size. } diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index 6cf4c8807..a052eea5d 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -22,7 +22,8 @@ extern crate serde_derive; // mod credit_calc; #[allow(unused)] mod ephemeral; -// mod friend; +#[allow(unused)] +mod friend; // mod funder; // mod handler; #[allow(unused)] From 02e46e0348d61e1e3561b3051df479fd78cc1093 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 11:41:58 +0300 Subject: [PATCH 051/248] Updated friend.rs to take new atomic payment design into account. --- components/funder/src/friend.rs | 39 +++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/components/funder/src/friend.rs b/components/funder/src/friend.rs index c07c0f7fa..01beb09ce 100644 --- a/components/funder/src/friend.rs +++ b/components/funder/src/friend.rs @@ -15,9 +15,11 @@ use proto::funder::messages::{ use crate::token_channel::{TcMutation, TokenChannel}; use crate::types::MoveTokenHashed; +/// Any operation that goes backwards (With respect to the initial request) #[derive(Clone, Serialize, Deserialize, Debug)] -pub enum ResponseOp { +pub enum BackwardsOp { Response(ResponseSendFundsOp), + /// A response that was not yet signed (Signing requires an async call) UnsignedResponse(PendingTransaction), Cancel(CancelSendFundsOp), Commit(CommitSendFundsOp), @@ -73,8 +75,8 @@ pub enum FriendMutation { SetWantedLocalRequestsStatus(RequestsStatus), PushBackPendingRequest(RequestSendFundsOp), PopFrontPendingRequest, - PushBackPendingResponse(ResponseOp), - PopFrontPendingResponse, + PushBackPendingBackwardsOp(BackwardsOp), + PopFrontPendingBackwardsOp, PushBackPendingUserRequest(RequestSendFundsOp), PopFrontPendingUserRequest, SetStatus(FriendStatus), @@ -127,6 +129,9 @@ pub struct FriendState { pub sent_local_relays: SentLocalRelays, /// Locally maintained name of the remote friend node. pub name: String, + /// Friend status. If disabled, we don't attempt to connect to this friend. (Friend will think + /// we are offline). + pub status: FriendStatus, /// Mutual credit channel information pub channel_status: ChannelStatus, /// Wanted credit frame for the remote side (Set by the user of this node) @@ -139,14 +144,14 @@ pub struct FriendState { pub wanted_local_requests_status: RequestsStatus, /// A queue of requests that need to be sent to the remote friend pub pending_requests: ImVec, - /// A queue of non-requests messages (Response, Cancel, Commit) that need to be sent to the remote side - // TODO: Pick a better name for this queue? - pub pending_responses: ImVec, - // Pending operations to be sent to the token channel. - pub status: FriendStatus, + /// A queue of backwards operations (Response, Cancel, Commit) that need to be sent to the remote side + /// We keep backwards op on a separate queue because those operations are not supposed to fail + /// (While requests may fail due to lack of trust for example) + pub pending_backwards_ops: ImVec, + /// Pending requests originating from the user. + /// We care more about these requests, because those are payments that our user wants to make. + /// This queue is bounded in size (TODO: Check this) pub pending_user_requests: ImVec, - // Request that the user has sent to this neighbor, - // but have not been processed yet. Bounded in size. } impl FriendState @@ -168,6 +173,7 @@ where remote_relays, sent_local_relays: SentLocalRelays::NeverSent, name, + status: FriendStatus::Disabled, channel_status: ChannelStatus::Consistent(token_channel), // The remote_max_debt we want to have. When possible, this will be sent to the remote @@ -177,12 +183,12 @@ where // The local_send_price we want to have (Or possibly close requests, by having an empty // send price). When possible, this will be updated with the TokenChannel. pending_requests: ImVec::new(), - pending_responses: ImVec::new(), - status: FriendStatus::Disabled, + pending_backwards_ops: ImVec::new(), pending_user_requests: ImVec::new(), } } + /* // TODO: Do we use this function somewhere? /// Find the shared credits we have with this friend. /// This value is used for freeze guard calculations. @@ -209,6 +215,7 @@ where .local_max_debt .saturating_add_signed(balance.balance) } + */ pub fn mutate(&mut self, friend_mutation: &FriendMutation) { match friend_mutation { @@ -236,11 +243,11 @@ where FriendMutation::PopFrontPendingRequest => { let _ = self.pending_requests.pop_front(); } - FriendMutation::PushBackPendingResponse(response_op) => { - self.pending_responses.push_back(response_op.clone()); + FriendMutation::PushBackPendingBackwardsOp(backwards_op) => { + self.pending_backwards_ops.push_back(backwards_op.clone()); } - FriendMutation::PopFrontPendingResponse => { - let _ = self.pending_responses.pop_front(); + FriendMutation::PopFrontPendingBackwardsOp => { + let _ = self.pending_backwards_ops.pop_front(); } FriendMutation::PushBackPendingUserRequest(request_send_funds) => { self.pending_user_requests From 985638301c4f62f825e1baa7fbf81cad7bc3e85b Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 12:12:36 +0300 Subject: [PATCH 052/248] Initial re-adding of funder/src/state.rs. Not done yet. --- components/funder/src/lib.rs | 3 ++- components/funder/src/state.rs | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index a052eea5d..69d75c1d7 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -31,7 +31,8 @@ mod liveness; #[allow(unused)] mod mutual_credit; // pub mod report; -// mod state; +#[allow(unused)] +mod state; // #[cfg(test)] // mod tests; #[allow(unused)] diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index 5486c5ac1..52c713b72 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -12,11 +12,19 @@ use crate::friend::{FriendMutation, FriendState}; #[derive(Clone, Serialize, Deserialize, Debug)] pub struct FunderState { + /// Public key of this node pub local_public_key: PublicKey, /// Address of relay we are going to connect to. /// None means that no address was configured. pub relays: ImVec>, + /// All configured friends and their state pub friends: ImHashMap>, + /// Receipts of completed payments (Generated after a CommitSendFundsOp message was received + /// successfuly). + // TODO: Should we map using InvoiceId or by something else? + // Note that we can not map using requestIds because we might be doing a multi route payment, + // which has multiple requestId-s. Maybe the Uid here can be some internal value that marks the + // transaction, possibly originated from the user? pub ready_receipts: ImHashMap, } From 4f738a8136198256f4787f7623b03d55377f2997 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 12:14:02 +0300 Subject: [PATCH 053/248] Removed old comment. --- components/funder/src/state.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index 52c713b72..9154af79d 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -55,7 +55,6 @@ where ready_receipts: ImHashMap::new(), } } - // TODO: Add code for initialization from database? // TODO: Use MutableState trait instead: pub fn mutate(&mut self, funder_mutation: &FunderMutation) { From d90b62e311880087933a640e789cfd3fbda63458 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 12:15:51 +0300 Subject: [PATCH 054/248] Updated comments. --- components/funder/src/state.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index 9154af79d..fff6ae42f 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -24,7 +24,7 @@ pub struct FunderState { // TODO: Should we map using InvoiceId or by something else? // Note that we can not map using requestIds because we might be doing a multi route payment, // which has multiple requestId-s. Maybe the Uid here can be some internal value that marks the - // transaction, possibly originated from the user? + // transaction, possibly originated from the user? We can call it payment_id. pub ready_receipts: ImHashMap, } @@ -36,7 +36,7 @@ pub enum FunderMutation { RemoveRelay(PublicKey), AddFriend(AddFriend), RemoveFriend(PublicKey), - AddReceipt((Uid, Receipt)), //(request_id, receipt) + AddReceipt((Uid, Receipt)), // (payment_id, receipt) RemoveReceipt(Uid), } From 0e33261949dcc2637092e38ac3694fdd8e4be172 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 12:21:06 +0300 Subject: [PATCH 055/248] Renamed fields of McPendingTransactions. --- .../funder/src/mutual_credit/incoming.rs | 20 ++----- .../funder/src/mutual_credit/outgoing.rs | 24 ++------- components/funder/src/mutual_credit/types.rs | 52 +++++++------------ components/funder/src/report.rs | 2 +- 4 files changed, 27 insertions(+), 71 deletions(-) diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index 84b0fad5d..0a036763f 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -251,10 +251,7 @@ fn process_request_send_funds( return Err(ProcessOperationError::InsufficientTrust); } - let p_remote_requests = &mutual_credit - .state() - .pending_transactions - .pending_remote_requests; + let p_remote_requests = &mutual_credit.state().pending_transactions.remote; // Make sure that we don't have this request as a pending request already: if p_remote_requests.contains_key(&request_send_funds.request_id) { return Err(ProcessOperationError::RequestAlreadyExists); @@ -292,10 +289,7 @@ fn process_response_send_funds( ) -> Result { // Make sure that id exists in local_pending hashmap, // and access saved request details. - let local_pending_transactions = &mutual_credit - .state() - .pending_transactions - .pending_local_requests; + let local_pending_transactions = &mutual_credit.state().pending_transactions.local; // Obtain pending request: // TODO: Possibly get rid of clone() here for optimization later @@ -351,10 +345,7 @@ fn process_cancel_send_funds( ) -> Result { // Make sure that id exists in local_pending hashmap, // and access saved request details. - let local_pending_transactions = &mutual_credit - .state() - .pending_transactions - .pending_local_requests; + let local_pending_transactions = &mutual_credit.state().pending_transactions.local; // Obtain pending request: let pending_transaction = local_pending_transactions @@ -405,10 +396,7 @@ fn process_commit_send_funds( ) -> Result { // Make sure that id exists in local_pending hashmap, // and access saved request details. - let local_pending_transactions = &mutual_credit - .state() - .pending_transactions - .pending_local_requests; + let local_pending_transactions = &mutual_credit.state().pending_transactions.local; // Obtain pending request: // TODO: Possibly get rid of clone() here for optimization later diff --git a/components/funder/src/mutual_credit/outgoing.rs b/components/funder/src/mutual_credit/outgoing.rs index e1fd317a4..d6bf7d0c9 100644 --- a/components/funder/src/mutual_credit/outgoing.rs +++ b/components/funder/src/mutual_credit/outgoing.rs @@ -154,11 +154,7 @@ impl OutgoingMc { return Err(QueueOperationError::InsufficientTrust); } - let p_local_requests = &self - .mutual_credit - .state() - .pending_transactions - .pending_local_requests; + let p_local_requests = &self.mutual_credit.state().pending_transactions.local; // Make sure that we don't have this request as a pending request already: if p_local_requests.contains_key(&request_send_funds.request_id) { @@ -187,11 +183,7 @@ impl OutgoingMc { ) -> Result, QueueOperationError> { // Make sure that id exists in remote_pending hashmap, // and access saved request details. - let remote_pending_transactions = &self - .mutual_credit - .state() - .pending_transactions - .pending_remote_requests; + let remote_pending_transactions = &self.mutual_credit.state().pending_transactions.remote; // Obtain pending request: let pending_transaction = remote_pending_transactions @@ -240,11 +232,7 @@ impl OutgoingMc { ) -> Result, QueueOperationError> { // Make sure that id exists in remote_pending hashmap, // and access saved request details. - let remote_pending_transactions = &self - .mutual_credit - .state() - .pending_transactions - .pending_remote_requests; + let remote_pending_transactions = &self.mutual_credit.state().pending_transactions.remote; // Obtain pending request: let pending_transaction = remote_pending_transactions @@ -285,11 +273,7 @@ impl OutgoingMc { ) -> Result, QueueOperationError> { // Make sure that id exists in remote_pending hashmap, // and access saved request details. - let remote_pending_transactions = &self - .mutual_credit - .state() - .pending_transactions - .pending_remote_requests; + let remote_pending_transactions = &self.mutual_credit.state().pending_transactions.remote; // Obtain pending request: let pending_transaction = remote_pending_transactions diff --git a/components/funder/src/mutual_credit/types.rs b/components/funder/src/mutual_credit/types.rs index bdafaf3f0..13ef966ea 100644 --- a/components/funder/src/mutual_credit/types.rs +++ b/components/funder/src/mutual_credit/types.rs @@ -54,21 +54,19 @@ impl McBalance { } } -// TODO: Rename pending_local_requests to a shorter name, like local. - #[derive(Clone, Serialize, Deserialize, Debug)] pub struct McPendingTransactions { - /// Pending requests that were opened locally and not yet completed - pub pending_local_requests: ImHashMap, - /// Pending requests that were opened remotely and not yet completed - pub pending_remote_requests: ImHashMap, + /// Pending transactions that were opened locally and not yet completed + pub local: ImHashMap, + /// Pending transactions that were opened remotely and not yet completed + pub remote: ImHashMap, } impl McPendingTransactions { fn new() -> McPendingTransactions { McPendingTransactions { - pending_local_requests: ImHashMap::new(), - pending_remote_requests: ImHashMap::new(), + local: ImHashMap::new(), + remote: ImHashMap::new(), } } } @@ -257,39 +255,25 @@ impl MutualCredit { } fn insert_remote_pending_transaction(&mut self, pending_friend_request: &PendingTransaction) { - self.state - .pending_transactions - .pending_remote_requests - .insert( - pending_friend_request.request_id, - pending_friend_request.clone(), - ); + self.state.pending_transactions.remote.insert( + pending_friend_request.request_id, + pending_friend_request.clone(), + ); } fn remove_remote_pending_transaction(&mut self, request_id: &Uid) { - let _ = self - .state - .pending_transactions - .pending_remote_requests - .remove(request_id); + let _ = self.state.pending_transactions.remote.remove(request_id); } fn insert_local_pending_transaction(&mut self, pending_friend_request: &PendingTransaction) { - self.state - .pending_transactions - .pending_local_requests - .insert( - pending_friend_request.request_id, - pending_friend_request.clone(), - ); + self.state.pending_transactions.local.insert( + pending_friend_request.request_id, + pending_friend_request.clone(), + ); } fn remove_local_pending_transaction(&mut self, request_id: &Uid) { - let _ = self - .state - .pending_transactions - .pending_local_requests - .remove(request_id); + let _ = self.state.pending_transactions.local.remove(request_id); } fn set_remote_pending_debt(&mut self, remote_pending_debt: u128) { @@ -307,7 +291,7 @@ impl MutualCredit { fn set_local_pending_transaction_stage(&mut self, request_id: &Uid, stage: TransactionStage) { self.state .pending_transactions - .pending_local_requests + .local .get_mut(&request_id) .unwrap() .stage = stage; @@ -316,7 +300,7 @@ impl MutualCredit { fn set_remote_pending_transaction_stage(&mut self, request_id: &Uid, stage: TransactionStage) { self.state .pending_transactions - .pending_remote_requests + .remote .get_mut(&request_id) .unwrap() .stage = stage; diff --git a/components/funder/src/report.rs b/components/funder/src/report.rs index 701a4b6f1..72349777a 100644 --- a/components/funder/src/report.rs +++ b/components/funder/src/report.rs @@ -86,7 +86,7 @@ where requests_status: McRequestsStatusReport::from(&mutual_credit_state.requests_status), num_local_pending_requests: usize_to_u64( mutual_credit_state - .pending_requests + .pending_transactions .pending_local_requests .len(), ) From cde7804729d04b0c88d6650f3b1e07840288527a Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 12:30:21 +0300 Subject: [PATCH 056/248] Re-added report.rs to funder. --- components/funder/src/lib.rs | 2 +- components/funder/src/report.rs | 24 +++++++++--------------- components/proto/src/report/messages.rs | 10 +++++----- components/proto/src/report/mod.rs | 1 + components/proto/src/report/serialize.rs | 14 ++++++++++++++ 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index 69d75c1d7..4fd187bc6 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -30,7 +30,7 @@ mod friend; mod liveness; #[allow(unused)] mod mutual_credit; -// pub mod report; +pub mod report; #[allow(unused)] mod state; // #[cfg(test)] diff --git a/components/funder/src/report.rs b/components/funder/src/report.rs index 72349777a..685cbba2c 100644 --- a/components/funder/src/report.rs +++ b/components/funder/src/report.rs @@ -85,17 +85,11 @@ where balance: McBalanceReport::from(&mutual_credit_state.balance), requests_status: McRequestsStatusReport::from(&mutual_credit_state.requests_status), num_local_pending_requests: usize_to_u64( - mutual_credit_state - .pending_transactions - .pending_local_requests - .len(), + mutual_credit_state.pending_transactions.local.len(), ) .unwrap(), num_remote_pending_requests: usize_to_u64( - mutual_credit_state - .pending_requests - .pending_remote_requests - .len(), + mutual_credit_state.pending_transactions.remote.len(), ) .unwrap(), } @@ -155,7 +149,7 @@ where &friend_state.wanted_local_requests_status, ), num_pending_requests: usize_to_u64(friend_state.pending_requests.len()).unwrap(), - num_pending_responses: usize_to_u64(friend_state.pending_responses.len()).unwrap(), + num_pending_backwards_ops: usize_to_u64(friend_state.pending_backwards_ops.len()).unwrap(), status: FriendStatusReport::from(&friend_state.status), num_pending_user_requests: usize_to_u64(friend_state.pending_user_requests.len()).unwrap(), } @@ -238,14 +232,14 @@ where usize_to_u64(friend_after.pending_requests.len()).unwrap(), )] } - FriendMutation::PushBackPendingResponse(_response_op) => { - vec![FriendReportMutation::SetNumPendingResponses( - usize_to_u64(friend_after.pending_responses.len()).unwrap(), + FriendMutation::PushBackPendingBackwardsOp(_backwards_op) => { + vec![FriendReportMutation::SetNumPendingBackwardsOps( + usize_to_u64(friend_after.pending_backwards_ops.len()).unwrap(), )] } - FriendMutation::PopFrontPendingResponse => { - vec![FriendReportMutation::SetNumPendingResponses( - usize_to_u64(friend_after.pending_responses.len()).unwrap(), + FriendMutation::PopFrontPendingBackwardsOp => { + vec![FriendReportMutation::SetNumPendingBackwardsOps( + usize_to_u64(friend_after.pending_backwards_ops.len()).unwrap(), )] } FriendMutation::PushBackPendingUserRequest(_request_send_funds) => { diff --git a/components/proto/src/report/messages.rs b/components/proto/src/report/messages.rs index f263a8e7c..d6741e4e0 100644 --- a/components/proto/src/report/messages.rs +++ b/components/proto/src/report/messages.rs @@ -156,7 +156,7 @@ where pub wanted_remote_max_debt: u128, pub wanted_local_requests_status: RequestsStatusReport, pub num_pending_requests: u64, - pub num_pending_responses: u64, + pub num_pending_backwards_ops: u64, // Pending operations to be sent to the token channel. pub status: FriendStatusReport, pub num_pending_user_requests: u64, @@ -191,7 +191,7 @@ where SetWantedRemoteMaxDebt(u128), SetWantedLocalRequestsStatus(RequestsStatusReport), SetNumPendingRequests(u64), - SetNumPendingResponses(u64), + SetNumPendingBackwardsOps(u64), SetStatus(FriendStatusReport), SetNumPendingUserRequests(u64), SetOptLastIncomingMoveToken(Option), @@ -279,8 +279,8 @@ where FriendReportMutation::SetWantedLocalRequestsStatus(wanted_local_requests_status) => { self.wanted_local_requests_status = wanted_local_requests_status.clone(); } - FriendReportMutation::SetNumPendingResponses(num_pending_responses) => { - self.num_pending_responses = *num_pending_responses; + FriendReportMutation::SetNumPendingBackwardsOps(num_pending_backwards_ops) => { + self.num_pending_backwards_ops = *num_pending_backwards_ops; } FriendReportMutation::SetNumPendingRequests(num_pending_requests) => { self.num_pending_requests = *num_pending_requests; @@ -340,7 +340,7 @@ where wanted_local_requests_status: RequestsStatusReport::from( &RequestsStatus::Closed, ), - num_pending_responses: 0, + num_pending_backwards_ops: 0, num_pending_requests: 0, status: FriendStatusReport::from(&FriendStatus::Disabled), num_pending_user_requests: 0, diff --git a/components/proto/src/report/mod.rs b/components/proto/src/report/mod.rs index 1c2e3db58..8bd59dcc8 100644 --- a/components/proto/src/report/mod.rs +++ b/components/proto/src/report/mod.rs @@ -1,4 +1,5 @@ pub mod convert; pub mod messages; +#[allow(unused)] pub mod serialize; pub mod signature_buff; diff --git a/components/proto/src/report/serialize.rs b/components/proto/src/report/serialize.rs index 6ac24e2a7..f1918c0a0 100644 --- a/components/proto/src/report/serialize.rs +++ b/components/proto/src/report/serialize.rs @@ -544,6 +544,9 @@ fn ser_friend_report( friend_report: &FriendReport, friend_report_builder: &mut report_capnp::friend_report::Builder, ) { + unimplemented!(); + + /* friend_report_builder .reborrow() .set_name(&friend_report.name); @@ -603,11 +606,14 @@ fn ser_friend_report( ); friend_report_builder.set_num_pending_user_requests(friend_report.num_pending_user_requests); + */ } fn deser_friend_report( friend_report_reader: &report_capnp::friend_report::Reader, ) -> Result { + unimplemented!(); + /* let mut remote_relays = Vec::new(); for relay_address in friend_report_reader.get_remote_relays()? { remote_relays.push(read_relay_address(&relay_address)?); @@ -635,6 +641,7 @@ fn deser_friend_report( status: deser_friend_status_report(&friend_report_reader.get_status()?)?, num_pending_user_requests: friend_report_reader.get_num_pending_user_requests(), }) + */ } fn ser_pk_friend_report( @@ -774,6 +781,8 @@ fn ser_friend_report_mutation( friend_report_mutation: &FriendReportMutation, friend_report_mutation_builder: &mut report_capnp::friend_report_mutation::Builder, ) { + unimplemented!(); + /* match friend_report_mutation { FriendReportMutation::SetRemoteRelays(relays) => { let relays_len = usize_to_u32(relays.len()).unwrap(); @@ -853,11 +862,15 @@ fn ser_friend_report_mutation( .init_set_liveness(), ), }; + */ } fn deser_friend_report_mutation( friend_report_mutation: &report_capnp::friend_report_mutation::Reader, ) -> Result { + unimplemented!(); + + /* Ok(match friend_report_mutation.which()? { report_capnp::friend_report_mutation::SetRemoteRelays(relays_reader) => { let mut relays = Vec::new(); @@ -914,6 +927,7 @@ fn deser_friend_report_mutation( )?) } }) + */ } fn ser_pk_friend_report_mutation( From a6eba8d28206223c373e0d4d8dd3b1ca6681df84 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 12:31:01 +0300 Subject: [PATCH 057/248] Restored export (FunderMutation, FunderState). --- components/funder/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index 4fd187bc6..95d89f5de 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -40,4 +40,4 @@ mod token_channel; pub mod types; // pub use self::funder::{funder_loop, FunderError}; -// pub use self::state::{FunderMutation, FunderState}; +pub use self::state::{FunderMutation, FunderState}; From 2a763491fed62bae3043d3bc1a725f67c5c4daf4 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 12:44:30 +0300 Subject: [PATCH 058/248] Refactored some code from handler.rs into state_wrap.rs and utils.rs. --- components/funder/src/handler/handler.rs | 117 -------------------- components/funder/src/handler/mod.rs | 24 ++-- components/funder/src/handler/state_wrap.rs | 78 +++++++++++++ components/funder/src/handler/utils.rs | 80 +++++++++++++ components/funder/src/lib.rs | 2 +- 5 files changed, 173 insertions(+), 128 deletions(-) create mode 100644 components/funder/src/handler/state_wrap.rs create mode 100644 components/funder/src/handler/utils.rs diff --git a/components/funder/src/handler/handler.rs b/components/funder/src/handler/handler.rs index fe731193e..40f476650 100644 --- a/components/funder/src/handler/handler.rs +++ b/components/funder/src/handler/handler.rs @@ -25,64 +25,6 @@ use crate::friend::ChannelStatus; use crate::report::{ephemeral_mutation_to_report_mutations, funder_mutation_to_report_mutations}; use crate::types::{ChannelerConfig, FunderIncoming, FunderIncomingComm, FunderOutgoingComm}; -pub struct MutableFunderState { - initial_state: FunderState, - state: FunderState, - mutations: Vec>, -} - -impl MutableFunderState -where - B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, -{ - pub fn new(state: FunderState) -> Self { - MutableFunderState { - initial_state: state.clone(), - state, - mutations: Vec::new(), - } - } - - pub fn mutate(&mut self, mutation: FunderMutation) { - self.state.mutate(&mutation); - self.mutations.push(mutation); - } - - pub fn state(&self) -> &FunderState { - &self.state - } - - pub fn done(self) -> (FunderState, Vec>, FunderState) { - (self.initial_state, self.mutations, self.state) - } -} - -pub struct MutableEphemeral { - ephemeral: Ephemeral, - mutations: Vec, -} - -impl MutableEphemeral { - pub fn new(ephemeral: Ephemeral) -> Self { - MutableEphemeral { - ephemeral, - mutations: Vec::new(), - } - } - pub fn mutate(&mut self, mutation: EphemeralMutation) { - self.ephemeral.mutate(&mutation); - self.mutations.push(mutation); - } - - pub fn ephemeral(&self) -> &Ephemeral { - &self.ephemeral - } - - pub fn done(self) -> (Vec, Ephemeral) { - (self.mutations, self.ephemeral) - } -} - #[derive(Debug)] pub enum FunderHandlerError { // HandleControlError(HandleControlError), @@ -100,65 +42,6 @@ where pub outgoing_control: Vec>, } -/// Find the originator of a pending local request. -/// This should be a pending remote request at some other friend. -/// Returns the public key of a friend. If we are the origin of this request, the function returns None. -/// -/// TODO: We need to change this search to be O(1) in the future. Possibly by maintaining a map -/// between request_id and (friend_public_key, friend). -pub fn find_request_origin<'a, B>( - state: &'a FunderState, - request_id: &Uid, -) -> Option<&'a PublicKey> -where - B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, -{ - for (friend_public_key, friend) in &state.friends { - match &friend.channel_status { - ChannelStatus::Inconsistent(_) => continue, - ChannelStatus::Consistent(token_channel) => { - if token_channel - .get_mutual_credit() - .state() - .pending_requests - .pending_remote_requests - .contains_key(request_id) - { - return Some(friend_public_key); - } - } - } - } - None -} - -pub fn is_friend_ready( - state: &FunderState, - ephemeral: &Ephemeral, - friend_public_key: &PublicKey, -) -> bool -where - B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, -{ - let friend = state.friends.get(friend_public_key).unwrap(); - if !ephemeral.liveness.is_online(friend_public_key) { - return false; - } - - // Make sure that the channel is consistent: - let token_channel = match &friend.channel_status { - ChannelStatus::Inconsistent(_) => return false, - ChannelStatus::Consistent(token_channel) => token_channel, - }; - - // Make sure that the remote side has open requests: - token_channel - .get_mutual_credit() - .state() - .requests_status - .remote - .is_open() -} type FunderHandleIncomingOutput = ( SendCommands, diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index c7d7e25ce..12dbb2d99 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -1,12 +1,16 @@ -mod canceler; -mod handle_control; -mod handle_friend; -mod handle_init; -mod handle_liveness; -mod handler; -mod sender; +// mod canceler; +// mod handle_control; +// mod handle_friend; +// mod handle_init; +// mod handle_liveness; +// mod handler; +#[allow(unused)] +mod state_wrap; +#[allow(unused)] +mod utils; +// mod sender; -#[cfg(test)] -mod tests; +// #[cfg(test)] +// mod tests; -pub use self::handler::{funder_handle_message, FunderHandlerError}; +// pub use self::handler::{funder_handle_message, FunderHandlerError}; diff --git a/components/funder/src/handler/state_wrap.rs b/components/funder/src/handler/state_wrap.rs new file mode 100644 index 000000000..91e01d3d1 --- /dev/null +++ b/components/funder/src/handler/state_wrap.rs @@ -0,0 +1,78 @@ +use std::fmt::Debug; + +use common::canonical_serialize::CanonicalSerialize; + +use crypto::crypto_rand::CryptoRandom; +use crypto::identity::PublicKey; +use crypto::uid::Uid; + +use proto::app_server::messages::RelayAddress; +use proto::funder::messages::FunderOutgoingControl; +use proto::report::messages::{FunderReportMutation, FunderReportMutations}; + +use identity::IdentityClient; + +use crate::state::{FunderMutation, FunderState}; + +use crate::ephemeral::{Ephemeral, EphemeralMutation}; +use crate::friend::ChannelStatus; +use crate::report::{ephemeral_mutation_to_report_mutations, funder_mutation_to_report_mutations}; +use crate::types::{ChannelerConfig, FunderIncoming, FunderIncomingComm, FunderOutgoingComm}; + +pub struct MutableFunderState { + initial_state: FunderState, + state: FunderState, + mutations: Vec>, +} + +impl MutableFunderState +where + B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, +{ + pub fn new(state: FunderState) -> Self { + MutableFunderState { + initial_state: state.clone(), + state, + mutations: Vec::new(), + } + } + + pub fn mutate(&mut self, mutation: FunderMutation) { + self.state.mutate(&mutation); + self.mutations.push(mutation); + } + + pub fn state(&self) -> &FunderState { + &self.state + } + + pub fn done(self) -> (FunderState, Vec>, FunderState) { + (self.initial_state, self.mutations, self.state) + } +} + +pub struct MutableEphemeral { + ephemeral: Ephemeral, + mutations: Vec, +} + +impl MutableEphemeral { + pub fn new(ephemeral: Ephemeral) -> Self { + MutableEphemeral { + ephemeral, + mutations: Vec::new(), + } + } + pub fn mutate(&mut self, mutation: EphemeralMutation) { + self.ephemeral.mutate(&mutation); + self.mutations.push(mutation); + } + + pub fn ephemeral(&self) -> &Ephemeral { + &self.ephemeral + } + + pub fn done(self) -> (Vec, Ephemeral) { + (self.mutations, self.ephemeral) + } +} diff --git a/components/funder/src/handler/utils.rs b/components/funder/src/handler/utils.rs new file mode 100644 index 000000000..842de369b --- /dev/null +++ b/components/funder/src/handler/utils.rs @@ -0,0 +1,80 @@ +use std::fmt::Debug; + +use common::canonical_serialize::CanonicalSerialize; + +use crypto::crypto_rand::CryptoRandom; +use crypto::identity::PublicKey; +use crypto::uid::Uid; + +use proto::app_server::messages::RelayAddress; +use proto::funder::messages::FunderOutgoingControl; +use proto::report::messages::{FunderReportMutation, FunderReportMutations}; + +use identity::IdentityClient; + +use crate::state::{FunderMutation, FunderState}; + +use crate::ephemeral::{Ephemeral, EphemeralMutation}; +use crate::friend::ChannelStatus; +use crate::report::{ephemeral_mutation_to_report_mutations, funder_mutation_to_report_mutations}; +use crate::types::{ChannelerConfig, FunderIncoming, FunderIncomingComm, FunderOutgoingComm}; + +/// Find the originator of a pending local request. +/// This should be a pending remote request at some other friend. +/// Returns the public key of a friend. If we are the origin of this request, the function returns None. +/// +/// TODO: We need to change this search to be O(1) in the future. Possibly by maintaining a map +/// between request_id and (friend_public_key, friend). +pub fn find_request_origin<'a, B>( + state: &'a FunderState, + request_id: &Uid, +) -> Option<&'a PublicKey> +where + B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, +{ + for (friend_public_key, friend) in &state.friends { + match &friend.channel_status { + ChannelStatus::Inconsistent(_) => continue, + ChannelStatus::Consistent(token_channel) => { + if token_channel + .get_mutual_credit() + .state() + .pending_transactions + .remote + .contains_key(request_id) + { + return Some(friend_public_key); + } + } + } + } + None +} + +pub fn is_friend_ready( + state: &FunderState, + ephemeral: &Ephemeral, + friend_public_key: &PublicKey, +) -> bool +where + B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, +{ + let friend = state.friends.get(friend_public_key).unwrap(); + if !ephemeral.liveness.is_online(friend_public_key) { + return false; + } + + // Make sure that the channel is consistent: + let token_channel = match &friend.channel_status { + ChannelStatus::Inconsistent(_) => return false, + ChannelStatus::Consistent(token_channel) => token_channel, + }; + + // Make sure that the remote side has open requests: + token_channel + .get_mutual_credit() + .state() + .requests_status + .remote + .is_open() +} diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index 95d89f5de..e0c50c6a1 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -25,7 +25,7 @@ mod ephemeral; #[allow(unused)] mod friend; // mod funder; -// mod handler; +mod handler; #[allow(unused)] mod liveness; #[allow(unused)] From 46fd807a3355480f56f3f5174c5da66c505916da Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 12:49:13 +0300 Subject: [PATCH 059/248] Restored handle_init.rs --- components/funder/src/handler/handle_init.rs | 4 ++-- components/funder/src/handler/mod.rs | 3 ++- components/funder/src/lib.rs | 4 ++-- components/funder/src/tests/mod.rs | 3 ++- components/funder/src/tests/utils.rs | 7 ++++++- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/components/funder/src/handler/handle_init.rs b/components/funder/src/handler/handle_init.rs index 931b40726..11dd09f83 100644 --- a/components/funder/src/handler/handle_init.rs +++ b/components/funder/src/handler/handle_init.rs @@ -4,7 +4,7 @@ use std::fmt::Debug; use proto::app_server::messages::RelayAddress; use proto::funder::messages::{ChannelerUpdateFriend, FriendStatus}; -use crate::handler::handler::MutableFunderState; +use crate::handler::state_wrap::MutableFunderState; use crate::types::ChannelerConfig; pub fn handle_init( @@ -61,7 +61,7 @@ mod tests { use crate::friend::FriendMutation; use crate::state::{FunderMutation, FunderState}; - use crate::handler::handler::MutableFunderState; + use crate::handler::state_wrap::MutableFunderState; use crate::tests::utils::{dummy_named_relay_address, dummy_relay_address}; #[test] diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index 12dbb2d99..5edd36543 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -1,7 +1,8 @@ // mod canceler; // mod handle_control; // mod handle_friend; -// mod handle_init; +#[allow(unused)] +mod handle_init; // mod handle_liveness; // mod handler; #[allow(unused)] diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index e0c50c6a1..c1e8c5ad9 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -33,8 +33,8 @@ mod mutual_credit; pub mod report; #[allow(unused)] mod state; -// #[cfg(test)] -// mod tests; +#[cfg(test)] +mod tests; #[allow(unused)] mod token_channel; pub mod types; diff --git a/components/funder/src/tests/mod.rs b/components/funder/src/tests/mod.rs index eca0d7c4d..ef41cf74e 100644 --- a/components/funder/src/tests/mod.rs +++ b/components/funder/src/tests/mod.rs @@ -1,2 +1,3 @@ -mod tests; +// mod tests; +#[allow(unused)] pub mod utils; diff --git a/components/funder/src/tests/utils.rs b/components/funder/src/tests/utils.rs index 3fc0b8df6..1ff818f0d 100644 --- a/components/funder/src/tests/utils.rs +++ b/components/funder/src/tests/utils.rs @@ -30,7 +30,8 @@ use database::DatabaseClient; use identity::{create_identity, IdentityClient}; use crate::ephemeral::Ephemeral; -use crate::funder::inner_funder_loop; +// TODO: Add this back later: +// use crate::funder::inner_funder_loop; use crate::report::create_report; use crate::state::FunderState; @@ -399,6 +400,9 @@ where } } +/* + +// TODO: Add this back later: /// Create a few node_controls, together with a router connecting them all. /// This allows having a conversation between any two nodes. /// We use A = u32: @@ -493,3 +497,4 @@ where } node_controls } +*/ From 0d67bfe7ae96fe97460beeca245eaf3ea08336b8 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 13:06:19 +0300 Subject: [PATCH 060/248] Updated import in handle_liveness. --- components/funder/src/handler/handle_liveness.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/funder/src/handler/handle_liveness.rs b/components/funder/src/handler/handle_liveness.rs index d990739d5..2602f5192 100644 --- a/components/funder/src/handler/handle_liveness.rs +++ b/components/funder/src/handler/handle_liveness.rs @@ -9,8 +9,8 @@ use crate::ephemeral::EphemeralMutation; use crate::liveness::LivenessMutation; use crate::handler::canceler::{cancel_pending_requests, cancel_pending_user_requests}; -use crate::handler::handler::{MutableEphemeral, MutableFunderState}; use crate::handler::sender::SendCommands; +use crate::handler::state_wrap::{MutableEphemeral, MutableFunderState}; #[derive(Debug)] pub enum HandleLivenessError { From e559fa1935949bf494da9b7367b64e5edd8deb25 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 13:06:46 +0300 Subject: [PATCH 061/248] Some work on sender.rs. Still commented out. --- components/funder/src/handler/mod.rs | 3 +- components/funder/src/handler/sender.rs | 60 +++++++++++-------------- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index 5edd36543..eb38401de 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -5,11 +5,12 @@ mod handle_init; // mod handle_liveness; // mod handler; +// #[allow(unused)] +// mod sender; #[allow(unused)] mod state_wrap; #[allow(unused)] mod utils; -// mod sender; // #[cfg(test)] // mod tests; diff --git a/components/funder/src/handler/sender.rs b/components/funder/src/handler/sender.rs index 0abd56ac6..dd6d172e7 100644 --- a/components/funder/src/handler/sender.rs +++ b/components/funder/src/handler/sender.rs @@ -16,17 +16,18 @@ use identity::IdentityClient; use crate::mutual_credit::outgoing::{OutgoingMc, QueueOperationError}; use crate::types::{ - create_failure_send_funds, create_pending_request, create_response_send_funds, - create_unsigned_move_token, sign_move_token, ChannelerConfig, + create_pending_transaction, create_response_send_funds, + create_unsigned_move_token, sign_move_token, ChannelerConfig, create_cancel_send_funds, }; use crate::friend::{ - ChannelInconsistent, ChannelStatus, FriendMutation, ResponseOp, SentLocalRelays, + ChannelInconsistent, ChannelStatus, FriendMutation, BackwardsOp, SentLocalRelays, }; use crate::token_channel::{SetDirection, TcDirection, TcMutation, TokenChannel}; use crate::ephemeral::Ephemeral; -use crate::handler::handler::{find_request_origin, MutableFunderState}; +use crate::handler::state_wrap::MutableFunderState; +use crate::handler::utils::find_request_origin; use crate::state::{FunderMutation, FunderState}; #[derive(Debug, Clone)] @@ -470,9 +471,9 @@ where Some(origin_public_key) => { // The friend with public key `origin_public_key` is the origin of this request. // We send him back a failure message: - let pending_request = create_pending_request(request_send_funds); - let u_failure_op = ResponseOp::UnsignedFailure(pending_request); - let friend_mutation = FriendMutation::PushBackPendingResponse(u_failure_op); + let pending_transaction = create_pending_transaction(request_send_funds); + let cancel_send_funds = BackwardsOp::Cancel(create_cancel_send_funds(&pending_transaction)); + let friend_mutation = FriendMutation::PushBackPendingBackwardsOp(cancel_send_funds); let funder_mutation = FunderMutation::FriendMutation((origin_public_key.clone(), friend_mutation)); m_state.mutate(funder_mutation); @@ -527,9 +528,9 @@ where } */ -async fn response_op_to_friend_tc_op<'a, B, R>( +async fn backwards_op_to_friend_tc_op<'a, B, R>( m_state: &'a mut MutableFunderState, - response_op: ResponseOp, + backwards_op: BackwardsOp, mut identity_client: &'a mut IdentityClient, rng: &'a R, ) -> FriendTcOp @@ -537,26 +538,19 @@ where B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, R: CryptoRandom, { - match response_op { - ResponseOp::Response(response) => FriendTcOp::ResponseSendFunds(response), - ResponseOp::UnsignedResponse(pending_request) => { + match backwards_op { + BackwardsOp::Response(response) => FriendTcOp::ResponseSendFunds(response), + BackwardsOp::UnsignedResponse(pending_transaction) => { let rand_nonce = RandValue::new(rng); + // TODO: How can we remember the destPlainLock? + // (Note that we have to supply here a destHashedLock value). FriendTcOp::ResponseSendFunds(await!(create_response_send_funds( - &pending_request, + &pending_transaction, rand_nonce, identity_client ))) } - ResponseOp::Failure(failure) => FriendTcOp::FailureSendFunds(failure), - ResponseOp::UnsignedFailure(pending_request) => { - let rand_nonce = RandValue::new(rng); - FriendTcOp::FailureSendFunds(await!(create_failure_send_funds( - &pending_request, - &(m_state.state().local_public_key), - rand_nonce, - &mut identity_client - ))) - } + BackwardsOp::Cancel(cancel_send_funds) => FriendTcOp::CancelSendFunds(cancel_send_funds), } } @@ -689,11 +683,11 @@ where let friend = m_state.state().friends.get(friend_public_key).unwrap(); // Send pending responses (responses and failures) // TODO: Possibly replace this clone with something more efficient later: - let mut pending_responses = friend.pending_responses.clone(); - while let Some(pending_response) = pending_responses.pop_front() { - let pending_op = await!(response_op_to_friend_tc_op( + let mut pending_backwards_ops = friend.pending_backwards_ops.clone(); + while let Some(pending_backwards_op) = pending_backwards_ops.pop_front() { + let pending_op = await!(backwards_op_to_friend_tc_op( m_state, - pending_response, + pending_backwards_op, identity_client, rng )); @@ -705,7 +699,7 @@ where &pending_op ))?; - let friend_mutation = FriendMutation::PopFrontPendingResponse; + let friend_mutation = FriendMutation::PopFrontPendingBackwardsOp; let funder_mutation = FunderMutation::FriendMutation((friend_public_key.clone(), friend_mutation)); m_state.mutate(funder_mutation); @@ -767,11 +761,11 @@ where // Send pending responses (responses and failures) // TODO: Possibly replace this clone with something more efficient later: - let mut pending_responses = friend.pending_responses.clone(); - while let Some(pending_response) = pending_responses.pop_front() { - let pending_op = await!(response_op_to_friend_tc_op( + let mut pending_backwards_ops = friend.pending_backwards_ops.clone(); + while let Some(pending_backwards_op) = pending_backwards_ops.pop_front() { + let pending_op = await!(backwards_op_to_friend_tc_op( m_state, - pending_response, + pending_backwards_op, identity_client, rng )); @@ -786,7 +780,7 @@ where &pending_op ))?; - let friend_mutation = FriendMutation::PopFrontPendingResponse; + let friend_mutation = FriendMutation::PopFrontPendingBackwardsOp; let funder_mutation = FunderMutation::FriendMutation((friend_public_key.clone(), friend_mutation)); m_state.mutate(funder_mutation); From 4495ca90dd5553b852764f00baecd3eeb60cbe44 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 16:14:51 +0300 Subject: [PATCH 062/248] Added payment_id to offst-crypto. --- components/crypto/src/lib.rs | 1 + components/crypto/src/payment_id.rs | 33 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 components/crypto/src/payment_id.rs diff --git a/components/crypto/src/lib.rs b/components/crypto/src/lib.rs index 6f33f042e..28864799b 100644 --- a/components/crypto/src/lib.rs +++ b/components/crypto/src/lib.rs @@ -19,6 +19,7 @@ pub mod hash_lock; pub mod identity; pub mod invoice_id; pub mod nonce_window; +pub mod payment_id; pub mod sym_encrypt; pub mod test_utils; pub mod uid; diff --git a/components/crypto/src/payment_id.rs b/components/crypto/src/payment_id.rs new file mode 100644 index 000000000..ca5d8c0ce --- /dev/null +++ b/components/crypto/src/payment_id.rs @@ -0,0 +1,33 @@ +use ring::rand::SecureRandom; +use std::fmt; + +pub const PAYMENT_ID_LEN: usize = 16; + +define_fixed_bytes!(PaymentId, PAYMENT_ID_LEN); + +impl Copy for PaymentId {} + +impl PaymentId { + pub fn new(rng: &R) -> PaymentId { + let mut payment_id = PaymentId([0; PAYMENT_ID_LEN]); + rng.fill(&mut payment_id.0).unwrap(); + payment_id + } + + /// Formatting for `Debug` and `Display`. + fn format(&self) -> String { + let upper_hex = self + .as_ref() + .iter() + .map(|byte| format!("{:02X}", byte)) + .collect::>(); + + upper_hex.join("") + } +} + +impl fmt::Display for PaymentId { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.format()) + } +} From c377f921adde3f6efc33970deeff8b16cd8f9f94 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 16:32:37 +0300 Subject: [PATCH 063/248] Added open_invoices, open_transactions to FunderState. --- components/funder/src/state.rs | 51 +++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index fff6ae42f..e2698a4e9 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -2,7 +2,10 @@ use im::hashmap::HashMap as ImHashMap; use im::vector::Vector as ImVec; use common::canonical_serialize::CanonicalSerialize; +use crypto::hash_lock::PlainLock; use crypto::identity::PublicKey; +use crypto::invoice_id::InvoiceId; +use crypto::payment_id::PaymentId; use crypto::uid::Uid; use proto::app_server::messages::NamedRelayAddress; @@ -14,18 +17,38 @@ use crate::friend::{FriendMutation, FriendState}; pub struct FunderState { /// Public key of this node pub local_public_key: PublicKey, - /// Address of relay we are going to connect to. - /// None means that no address was configured. + /// Addresses of relays we are going to connect to. pub relays: ImVec>, /// All configured friends and their state pub friends: ImHashMap>, + /// Locally issued invoices in progress. + // TODO: Add this part in the report? At least a counter of this hash set? + pub open_invoices: ImHashMap, + /// Locally created transaction in progress. (This node is the buyer). + // TODO: Add this part in the report? At least a counter of this hash set? + pub open_transactions: ImHashMap, /// Receipts of completed payments (Generated after a CommitSendFundsOp message was received /// successfuly). - // TODO: Should we map using InvoiceId or by something else? - // Note that we can not map using requestIds because we might be doing a multi route payment, - // which has multiple requestId-s. Maybe the Uid here can be some internal value that marks the - // transaction, possibly originated from the user? We can call it payment_id. - pub ready_receipts: ImHashMap, + /// Note: We use our own randomly generated paymentId to represent a payment and not an + /// InvoiceId. This happens to defend against possible collisions of InvoiceId values (Which + /// are not locally generated). + pub ready_receipts: ImHashMap, +} + +/// A local invoice in progress +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct OpenInvoice { + /// Destination plain locks for all requests related to a single open invoice that was + /// originated for this node. + /// Multiple requests are possible for a single invoice in case of a multi-route payment. + dest_plain_locks: ImHashMap, +} + +/// A local request (Originated from this node) in progress +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct OpenTransaction { + /// The plain part of a hash lock for the generated transaction. + src_plain_lock: PlainLock, } #[allow(clippy::large_enum_variant)] @@ -36,8 +59,8 @@ pub enum FunderMutation { RemoveRelay(PublicKey), AddFriend(AddFriend), RemoveFriend(PublicKey), - AddReceipt((Uid, Receipt)), // (payment_id, receipt) - RemoveReceipt(Uid), + AddReceipt((PaymentId, Receipt)), + RemoveReceipt(PaymentId), } impl FunderState @@ -52,6 +75,8 @@ where local_public_key, relays, friends: ImHashMap::new(), + open_invoices: ImHashMap::new(), + open_transactions: ImHashMap::new(), ready_receipts: ImHashMap::new(), } } @@ -94,12 +119,12 @@ where FunderMutation::RemoveFriend(public_key) => { let _ = self.friends.remove(&public_key); } - FunderMutation::AddReceipt((uid, send_funds_receipt)) => { + FunderMutation::AddReceipt((payment_id, send_funds_receipt)) => { self.ready_receipts - .insert(uid.clone(), send_funds_receipt.clone()); + .insert(payment_id.clone(), send_funds_receipt.clone()); } - FunderMutation::RemoveReceipt(uid) => { - let _ = self.ready_receipts.remove(uid); + FunderMutation::RemoveReceipt(payment_id) => { + let _ = self.ready_receipts.remove(payment_id); } } } From 7303987a52a387ebb98cf0f0077a99b542e70910 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 16:35:50 +0300 Subject: [PATCH 064/248] Added random generation of PlainLock. --- components/crypto/src/hash_lock.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/components/crypto/src/hash_lock.rs b/components/crypto/src/hash_lock.rs index 703217a7e..781bfdf39 100644 --- a/components/crypto/src/hash_lock.rs +++ b/components/crypto/src/hash_lock.rs @@ -1,4 +1,5 @@ use ring::digest::{digest, SHA512_256}; +use ring::rand::SecureRandom; // TODO: Use bcrypt instead here @@ -9,6 +10,13 @@ define_fixed_bytes!(PlainLock, PLAIN_LOCK_LEN); define_fixed_bytes!(HashedLock, HASHED_LOCK_LEN); impl PlainLock { + /// Randomly generate a new PlainLock + pub fn new(rng: &R) -> Self { + let mut plain_lock = PlainLock([0; PLAIN_LOCK_LEN]); + rng.fill(&mut plain_lock.0).unwrap(); + plain_lock + } + pub fn hash(&self) -> HashedLock { let mut inner = [0x00; HASHED_LOCK_LEN]; let digest_res = digest(&SHA512_256, &self.0); From c408766a483c4861d8411394a9d640b3b0ba7553 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 16:41:28 +0300 Subject: [PATCH 065/248] Funder: sender.rs compiles, logic currently incorrect. --- components/funder/src/handler/mod.rs | 4 ++-- components/funder/src/handler/sender.rs | 9 ++++++++- components/funder/src/lib.rs | 4 ++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index eb38401de..fc3041c20 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -5,8 +5,8 @@ mod handle_init; // mod handle_liveness; // mod handler; -// #[allow(unused)] -// mod sender; +#[allow(unused)] +mod sender; #[allow(unused)] mod state_wrap; #[allow(unused)] diff --git a/components/funder/src/handler/sender.rs b/components/funder/src/handler/sender.rs index dd6d172e7..8d216600d 100644 --- a/components/funder/src/handler/sender.rs +++ b/components/funder/src/handler/sender.rs @@ -5,6 +5,7 @@ use common::canonical_serialize::CanonicalSerialize; use crypto::crypto_rand::{CryptoRandom, RandValue}; use crypto::identity::PublicKey; +use crypto::hash_lock::PlainLock; use proto::app_server::messages::RelayAddress; use proto::funder::messages::{ @@ -420,7 +421,7 @@ where ChannelStatus::Inconsistent(_) => {} }; - if !friend.pending_responses.is_empty() { + if !friend.pending_backwards_ops.is_empty() { return true; } @@ -541,16 +542,22 @@ where match backwards_op { BackwardsOp::Response(response) => FriendTcOp::ResponseSendFunds(response), BackwardsOp::UnsignedResponse(pending_transaction) => { + // TODO; How can we remember this value? + let dest_plain_lock = PlainLock::new(rng); let rand_nonce = RandValue::new(rng); + assert!(false); // TODO: How can we remember the destPlainLock? // (Note that we have to supply here a destHashedLock value). FriendTcOp::ResponseSendFunds(await!(create_response_send_funds( &pending_transaction, + // TODO: How can we remember the value of dest_plain_lock here? + dest_plain_lock.hash(), rand_nonce, identity_client ))) } BackwardsOp::Cancel(cancel_send_funds) => FriendTcOp::CancelSendFunds(cancel_send_funds), + BackwardsOp::Commit(commit_send_funds) => FriendTcOp::CommitSendFunds(commit_send_funds), } } diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index c1e8c5ad9..67c4bbc31 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -13,8 +13,8 @@ clippy::new_without_default )] -// #[macro_use] -// extern crate log; +#[macro_use] +extern crate log; #[macro_use] extern crate serde_derive; From f8fa223e52c14e542fca7dd449d0d5dd27791f19 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 16 May 2019 16:58:32 +0300 Subject: [PATCH 066/248] Some tentative work on BackwardsOp::UnsignedResponse structure. Will probably change later. --- components/funder/src/friend.rs | 5 +++-- components/funder/src/handler/sender.rs | 8 +------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/components/funder/src/friend.rs b/components/funder/src/friend.rs index 01beb09ce..1e31da1d7 100644 --- a/components/funder/src/friend.rs +++ b/components/funder/src/friend.rs @@ -1,6 +1,7 @@ use im::vector::Vector as ImVec; use std::fmt::Debug; +use crypto::hash_lock::PlainLock; use crypto::identity::PublicKey; use common::canonical_serialize::CanonicalSerialize; @@ -13,14 +14,14 @@ use proto::funder::messages::{ }; use crate::token_channel::{TcMutation, TokenChannel}; -use crate::types::MoveTokenHashed; +use crate::types::{MoveTokenHashed, UnsignedResponseSendFundsOp}; /// Any operation that goes backwards (With respect to the initial request) #[derive(Clone, Serialize, Deserialize, Debug)] pub enum BackwardsOp { Response(ResponseSendFundsOp), /// A response that was not yet signed (Signing requires an async call) - UnsignedResponse(PendingTransaction), + UnsignedResponse((PendingTransaction, PlainLock)), Cancel(CancelSendFundsOp), Commit(CommitSendFundsOp), } diff --git a/components/funder/src/handler/sender.rs b/components/funder/src/handler/sender.rs index 8d216600d..61dc2497d 100644 --- a/components/funder/src/handler/sender.rs +++ b/components/funder/src/handler/sender.rs @@ -541,16 +541,10 @@ where { match backwards_op { BackwardsOp::Response(response) => FriendTcOp::ResponseSendFunds(response), - BackwardsOp::UnsignedResponse(pending_transaction) => { - // TODO; How can we remember this value? - let dest_plain_lock = PlainLock::new(rng); + BackwardsOp::UnsignedResponse((pending_transaction, dest_plain_lock)) => { let rand_nonce = RandValue::new(rng); - assert!(false); - // TODO: How can we remember the destPlainLock? - // (Note that we have to supply here a destHashedLock value). FriendTcOp::ResponseSendFunds(await!(create_response_send_funds( &pending_transaction, - // TODO: How can we remember the value of dest_plain_lock here? dest_plain_lock.hash(), rand_nonce, identity_client From ea622201a559c5ebcb82c0821a9df994bea54da9 Mon Sep 17 00:00:00 2001 From: real Date: Fri, 17 May 2019 18:47:47 +0300 Subject: [PATCH 067/248] Removed BackwardsOp::UnsignedResponse. --- components/funder/src/friend.rs | 4 ++-- components/funder/src/handler/sender.rs | 9 --------- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/components/funder/src/friend.rs b/components/funder/src/friend.rs index 1e31da1d7..93c10196b 100644 --- a/components/funder/src/friend.rs +++ b/components/funder/src/friend.rs @@ -20,8 +20,8 @@ use crate::types::{MoveTokenHashed, UnsignedResponseSendFundsOp}; #[derive(Clone, Serialize, Deserialize, Debug)] pub enum BackwardsOp { Response(ResponseSendFundsOp), - /// A response that was not yet signed (Signing requires an async call) - UnsignedResponse((PendingTransaction, PlainLock)), + // /// A response that was not yet signed (Signing requires an async call) + // UnsignedResponse((PendingTransaction, PlainLock)), Cancel(CancelSendFundsOp), Commit(CommitSendFundsOp), } diff --git a/components/funder/src/handler/sender.rs b/components/funder/src/handler/sender.rs index 61dc2497d..cfe6ba11e 100644 --- a/components/funder/src/handler/sender.rs +++ b/components/funder/src/handler/sender.rs @@ -541,15 +541,6 @@ where { match backwards_op { BackwardsOp::Response(response) => FriendTcOp::ResponseSendFunds(response), - BackwardsOp::UnsignedResponse((pending_transaction, dest_plain_lock)) => { - let rand_nonce = RandValue::new(rng); - FriendTcOp::ResponseSendFunds(await!(create_response_send_funds( - &pending_transaction, - dest_plain_lock.hash(), - rand_nonce, - identity_client - ))) - } BackwardsOp::Cancel(cancel_send_funds) => FriendTcOp::CancelSendFunds(cancel_send_funds), BackwardsOp::Commit(commit_send_funds) => FriendTcOp::CommitSendFunds(commit_send_funds), } From 21af9e92d9e93b0380aa641a0b93ac2d450da85a Mon Sep 17 00:00:00 2001 From: real Date: Fri, 17 May 2019 18:54:40 +0300 Subject: [PATCH 068/248] Removed some unnecessary async. --- components/funder/src/handler/sender.rs | 48 ++++++++++++------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/components/funder/src/handler/sender.rs b/components/funder/src/handler/sender.rs index cfe6ba11e..470367d7d 100644 --- a/components/funder/src/handler/sender.rs +++ b/components/funder/src/handler/sender.rs @@ -370,7 +370,7 @@ async fn send_friend_iter1<'a, B, R>( ); pending_move_tokens.insert(friend_public_key.clone(), pending_move_token); let pending_move_token = pending_move_tokens.get_mut(friend_public_key).unwrap(); - let _ = await!(collect_outgoing_move_token( + let _ = collect_outgoing_move_token( m_state, outgoing_channeler_config, outgoing_control, @@ -379,7 +379,7 @@ async fn send_friend_iter1<'a, B, R>( pending_move_token, identity_client, rng - )); + ); } /// Do we need to send anything to the remote side? @@ -439,7 +439,7 @@ where /// Queue an operation to a PendingMoveToken. /// On failure, queue a failure to the relevant friend, /// or (if we are the origin of the request): send a failure through the control -async fn queue_operation_or_failure<'a, B>( +fn queue_operation_or_failure<'a, B>( m_state: &'a mut MutableFunderState, pending_move_token: &'a mut PendingMoveToken, failure_public_keys: &'a mut HashSet, @@ -529,7 +529,7 @@ where } */ -async fn backwards_op_to_friend_tc_op<'a, B, R>( +fn backwards_op_to_friend_tc_op<'a, B, R>( m_state: &'a mut MutableFunderState, backwards_op: BackwardsOp, mut identity_client: &'a mut IdentityClient, @@ -549,7 +549,7 @@ where /// Given a friend with an incoming move token state, create the largest possible move token to /// send to the remote side. /// Requests that fail to be processed are moved to the failure queues of the relevant friends. -async fn collect_outgoing_move_token<'a, B, R>( +fn collect_outgoing_move_token<'a, B, R>( m_state: &'a mut MutableFunderState, outgoing_channeler_config: &'a mut Vec>>, outgoing_control: &'a mut Vec>, @@ -635,13 +635,13 @@ where if friend.wanted_remote_max_debt != remote_max_debt { let operation = FriendTcOp::SetRemoteMaxDebt(friend.wanted_remote_max_debt); - await!(queue_operation_or_failure( + queue_operation_or_failure( m_state, pending_move_token, failure_public_keys, outgoing_control, &operation - ))?; + )?; } let friend = m_state.state().friends.get(friend_public_key).unwrap(); @@ -663,13 +663,13 @@ where } else { FriendTcOp::DisableRequests }; - await!(queue_operation_or_failure( + queue_operation_or_failure( m_state, pending_move_token, failure_public_keys, outgoing_control, &friend_op - ))?; + )?; } let friend = m_state.state().friends.get(friend_public_key).unwrap(); @@ -677,19 +677,19 @@ where // TODO: Possibly replace this clone with something more efficient later: let mut pending_backwards_ops = friend.pending_backwards_ops.clone(); while let Some(pending_backwards_op) = pending_backwards_ops.pop_front() { - let pending_op = await!(backwards_op_to_friend_tc_op( + let pending_op = backwards_op_to_friend_tc_op( m_state, pending_backwards_op, identity_client, rng - )); - await!(queue_operation_or_failure( + ); + queue_operation_or_failure( m_state, pending_move_token, failure_public_keys, outgoing_control, &pending_op - ))?; + )?; let friend_mutation = FriendMutation::PopFrontPendingBackwardsOp; let funder_mutation = @@ -704,13 +704,13 @@ where let mut pending_requests = friend.pending_requests.clone(); while let Some(pending_request) = pending_requests.pop_front() { let pending_op = FriendTcOp::RequestSendFunds(pending_request); - await!(queue_operation_or_failure( + queue_operation_or_failure( m_state, pending_move_token, failure_public_keys, outgoing_control, &pending_op - ))?; + )?; let friend_mutation = FriendMutation::PopFrontPendingRequest; let funder_mutation = FunderMutation::FriendMutation((friend_public_key.clone(), friend_mutation)); @@ -723,13 +723,13 @@ where let mut pending_user_requests = friend.pending_user_requests.clone(); while let Some(request_send_funds) = pending_user_requests.pop_front() { let pending_op = FriendTcOp::RequestSendFunds(request_send_funds); - await!(queue_operation_or_failure( + queue_operation_or_failure( m_state, pending_move_token, failure_public_keys, outgoing_control, &pending_op - ))?; + )?; let friend_mutation = FriendMutation::PopFrontPendingUserRequest; let funder_mutation = FunderMutation::FriendMutation((friend_public_key.clone(), friend_mutation)); @@ -738,7 +738,7 @@ where Ok(()) } -async fn append_failures_to_move_token<'a, B, R>( +fn append_failures_to_move_token<'a, B, R>( m_state: &'a mut MutableFunderState, friend_public_key: &'a PublicKey, pending_move_token: &'a mut PendingMoveToken, @@ -755,22 +755,22 @@ where // TODO: Possibly replace this clone with something more efficient later: let mut pending_backwards_ops = friend.pending_backwards_ops.clone(); while let Some(pending_backwards_op) = pending_backwards_ops.pop_front() { - let pending_op = await!(backwards_op_to_friend_tc_op( + let pending_op = backwards_op_to_friend_tc_op( m_state, pending_backwards_op, identity_client, rng - )); + ); // TODO: Find a more elegant way to do this: let mut dummy_failure_public_keys = HashSet::new(); let mut dummy_outgoing_control = Vec::new(); - await!(queue_operation_or_failure( + queue_operation_or_failure( m_state, pending_move_token, &mut dummy_failure_public_keys, &mut dummy_outgoing_control, &pending_op - ))?; + )?; let friend_mutation = FriendMutation::PopFrontPendingBackwardsOp; let funder_mutation = @@ -956,13 +956,13 @@ where // Second iteration (Attempt to queue failures created in the first iteration): for (friend_public_key, pending_move_token) in &mut pending_move_tokens { assert!(ephemeral.liveness.is_online(&friend_public_key)); - let _ = await!(append_failures_to_move_token( + let _ = append_failures_to_move_token( m_state, friend_public_key, pending_move_token, identity_client, rng - )); + ); } // Send all pending move tokens: From 5e02142fe15334617b9bbca5471a611cebe1f5e4 Mon Sep 17 00:00:00 2001 From: real Date: Fri, 17 May 2019 19:07:58 +0300 Subject: [PATCH 069/248] Added MutableFunderState.queue_unsigned_response() --- components/funder/src/handler/state_wrap.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/components/funder/src/handler/state_wrap.rs b/components/funder/src/handler/state_wrap.rs index 91e01d3d1..3547a9291 100644 --- a/components/funder/src/handler/state_wrap.rs +++ b/components/funder/src/handler/state_wrap.rs @@ -17,11 +17,15 @@ use crate::state::{FunderMutation, FunderState}; use crate::ephemeral::{Ephemeral, EphemeralMutation}; use crate::friend::ChannelStatus; use crate::report::{ephemeral_mutation_to_report_mutations, funder_mutation_to_report_mutations}; -use crate::types::{ChannelerConfig, FunderIncoming, FunderIncomingComm, FunderOutgoingComm}; +use crate::types::{ + ChannelerConfig, FunderIncoming, FunderIncomingComm, FunderOutgoingComm, + UnsignedResponseSendFundsOp, +}; pub struct MutableFunderState { initial_state: FunderState, state: FunderState, + unsigned_responses: Vec, mutations: Vec>, } @@ -33,10 +37,18 @@ where MutableFunderState { initial_state: state.clone(), state, + unsigned_responses: Vec::new(), mutations: Vec::new(), } } + /// Push an unsigned response operation. + /// We have a separate queue for these operations because we need an async function call to + /// sign an unsigned response. + pub fn queue_unsigned_response(&mut self, u_response_send_funds: UnsignedResponseSendFundsOp) { + self.unsigned_responses.push(u_response_send_funds); + } + pub fn mutate(&mut self, mutation: FunderMutation) { self.state.mutate(&mutation); self.mutations.push(mutation); From f73b69f61b37dea132d3e77a75c1c6dd4c3901a6 Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 11:30:26 +0300 Subject: [PATCH 070/248] funder.rs: Added TODO comment --- components/funder/src/funder.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/components/funder/src/funder.rs b/components/funder/src/funder.rs index 863c70f6b..dfaaac3be 100644 --- a/components/funder/src/funder.rs +++ b/components/funder/src/funder.rs @@ -109,6 +109,9 @@ where } }; + // TODO: Sign all unsigned responses and then queue them as mutations + assert!(false); + if !handler_output.funder_mutations.is_empty() { // Mutate our funder_state in memory: for mutation in &handler_output.funder_mutations { @@ -124,6 +127,7 @@ where ephemeral.mutate(mutation); } + // Send outgoing communication messages: let mut comm_stream = stream::iter::<_>(handler_output.outgoing_comms); await!(comm_sender.send_all(&mut comm_stream)).map_err(|_| FunderError::SendCommError)?; From 0d81328b4489b0d66cd064ae18c2036c1bf7c103 Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 11:30:48 +0300 Subject: [PATCH 071/248] Restored canceler.rs. Compiling but contains incorrect logic. --- components/funder/src/handler/canceler.rs | 77 +++++++++-------------- components/funder/src/handler/mod.rs | 3 +- 2 files changed, 31 insertions(+), 49 deletions(-) diff --git a/components/funder/src/handler/canceler.rs b/components/funder/src/handler/canceler.rs index 2e8cce9f7..3f404490e 100644 --- a/components/funder/src/handler/canceler.rs +++ b/components/funder/src/handler/canceler.rs @@ -3,53 +3,30 @@ use crypto::identity::PublicKey; use std::fmt::Debug; use proto::funder::messages::{ - FunderOutgoingControl, RequestSendFunds, ResponseReceived, ResponseSendFundsResult, + FunderOutgoingControl, RequestSendFundsOp, ResponseReceived, ResponseSendFundsResult, }; -use crate::handler::handler::{find_request_origin, MutableFunderState}; use crate::handler::sender::SendCommands; +use crate::handler::state_wrap::MutableFunderState; +use crate::handler::utils::find_request_origin; -use crate::friend::{ChannelStatus, FriendMutation, ResponseOp}; +use crate::friend::{BackwardsOp, ChannelStatus, FriendMutation}; use crate::state::FunderMutation; -use crate::types::create_pending_request; +use crate::types::{create_cancel_send_funds, create_pending_transaction}; -/* -A: CanonicalSerialize + Clone + Debug + PartialEq + Eq + 'static, -R: CryptoRandom + 'static, -*/ - -/* -/// Create a (signed) failure message for a given request_id. -/// We are the reporting_public_key for this failure message. -pub fn create_unsigned_failure_message(&self, pending_local_request: &PendingRequest) - -> UnsignedFailureSendFunds { - - let rand_nonce = RandValue::new(&self.rng); - let local_public_key = self.state.local_public_key.clone(); - - let mut u_failure_send_funds = UnsignedFailureSendFunds { - request_id: pending_local_request.request_id.clone(), - reporting_public_key: local_public_key.clone(), - rand_nonce, - signature: (), - }; - - u_failure_send_funds -} -*/ - -/// Reply to a request message with failure. -pub fn reply_with_failure( +/// Reply to a request message with a cancellation. +pub fn reply_with_cancel( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, remote_public_key: &PublicKey, - request_send_funds: &RequestSendFunds, + request_send_funds: &RequestSendFundsOp, ) where B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, { - let pending_request = create_pending_request(request_send_funds); - let u_failure_op = ResponseOp::UnsignedFailure(pending_request); - let friend_mutation = FriendMutation::PushBackPendingResponse(u_failure_op); + let pending_local_transaction = create_pending_transaction(request_send_funds); + let cancel_send_funds = create_cancel_send_funds(&pending_local_transaction); + let friend_mutation = + FriendMutation::PushBackPendingBackwardsOp(BackwardsOp::Cancel(cancel_send_funds)); let funder_mutation = FunderMutation::FriendMutation((remote_public_key.clone(), friend_mutation)); m_state.mutate(funder_mutation); @@ -58,7 +35,7 @@ pub fn reply_with_failure( /// Cancel outgoing local requests that are already inside the token channel (Possibly already /// communicated to the remote side). -pub fn cancel_local_pending_requests( +pub fn cancel_local_pending_transactions( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, outgoing_control: &mut Vec>, @@ -76,23 +53,25 @@ pub fn cancel_local_pending_requests( // Mark all pending requests to this friend as errors. // As the token channel is being reset, we can be sure we will never obtain a response // for those requests. - let pending_local_requests = token_channel + let pending_local_transactions = token_channel .get_mutual_credit() .state() - .pending_requests - .pending_local_requests + .pending_transactions + .local .clone(); // Prepare a list of all remote requests that we need to cancel: - for (local_request_id, pending_local_request) in pending_local_requests { + for (local_request_id, pending_local_transaction) in pending_local_transactions { let opt_origin_public_key = find_request_origin(m_state.state(), &local_request_id).cloned(); match opt_origin_public_key { Some(origin_public_key) => { // We have found the friend that is the origin of this request. - // We send him a failure message. - let u_failure_op = ResponseOp::UnsignedFailure(pending_local_request); - let friend_mutation = FriendMutation::PushBackPendingResponse(u_failure_op); + // We send him a cancel message. + let cancel_send_funds = create_cancel_send_funds(&pending_local_transaction); + let friend_mutation = FriendMutation::PushBackPendingBackwardsOp( + BackwardsOp::Cancel(cancel_send_funds), + ); let funder_mutation = FunderMutation::FriendMutation((origin_public_key.clone(), friend_mutation)); m_state.mutate(funder_mutation); @@ -100,9 +79,9 @@ pub fn cancel_local_pending_requests( } None => { // We are the origin of this request. - // We send a failure response through the control: + // We send a cancel message through the control: let response_received = ResponseReceived { - request_id: pending_local_request.request_id, + request_id: pending_local_transaction.request_id, result: ResponseSendFundsResult::Failure( m_state.state().local_public_key.clone(), ), @@ -134,9 +113,11 @@ pub fn cancel_pending_requests( find_request_origin(m_state.state(), &pending_request.request_id).cloned(); match opt_origin_public_key { Some(origin_public_key) => { - let local_pending_request = create_pending_request(&pending_request); - let u_failure_op = ResponseOp::UnsignedFailure(local_pending_request); - let friend_mutation = FriendMutation::PushBackPendingResponse(u_failure_op); + let pending_local_transaction = create_pending_transaction(&pending_request); + let cancel_send_funds = create_cancel_send_funds(&pending_local_transaction); + let friend_mutation = FriendMutation::PushBackPendingBackwardsOp( + BackwardsOp::Cancel(cancel_send_funds), + ); let funder_mutation = FunderMutation::FriendMutation((origin_public_key.clone(), friend_mutation)); m_state.mutate(funder_mutation); diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index fc3041c20..a7c6cabb7 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -1,4 +1,5 @@ -// mod canceler; +#[allow(unused)] +mod canceler; // mod handle_control; // mod handle_friend; #[allow(unused)] From a851baf1388607ebdca56b717761ba1a612e5aa2 Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 11:32:44 +0300 Subject: [PATCH 072/248] Restored handle_liveness.rs. --- components/funder/src/handler/handle_liveness.rs | 2 +- components/funder/src/handler/mod.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/components/funder/src/handler/handle_liveness.rs b/components/funder/src/handler/handle_liveness.rs index 2602f5192..9bca7e865 100644 --- a/components/funder/src/handler/handle_liveness.rs +++ b/components/funder/src/handler/handle_liveness.rs @@ -95,8 +95,8 @@ mod tests { use crate::friend::{ChannelStatus, FriendMutation}; use crate::state::{FunderMutation, FunderState}; - use crate::handler::handler::{MutableEphemeral, MutableFunderState}; use crate::handler::sender::SendCommands; + use crate::handler::state_wrap::{MutableEphemeral, MutableFunderState}; use crate::tests::utils::{dummy_named_relay_address, dummy_relay_address}; #[test] diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index a7c6cabb7..1d23c9977 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -4,7 +4,8 @@ mod canceler; // mod handle_friend; #[allow(unused)] mod handle_init; -// mod handle_liveness; +#[allow(unused)] +mod handle_liveness; // mod handler; #[allow(unused)] mod sender; From fa1f5623b64cde236ba8cdac2a97ed2c76bf1ab9 Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 11:52:47 +0300 Subject: [PATCH 073/248] handle_friend.rs compiles (but currently incorrect). --- components/funder/src/handler/canceler.rs | 10 +- .../funder/src/handler/handle_friend.rs | 120 +++++++++++------- components/funder/src/handler/mod.rs | 3 +- components/funder/src/handler/sender.rs | 2 +- components/funder/src/handler/state_wrap.rs | 8 +- components/proto/src/app_server/mod.rs | 1 + components/proto/src/app_server/serialize.rs | 15 ++- components/proto/src/funder/messages.rs | 2 +- 8 files changed, 98 insertions(+), 63 deletions(-) diff --git a/components/funder/src/handler/canceler.rs b/components/funder/src/handler/canceler.rs index 3f404490e..dcbd47ca6 100644 --- a/components/funder/src/handler/canceler.rs +++ b/components/funder/src/handler/canceler.rs @@ -82,9 +82,7 @@ pub fn cancel_local_pending_transactions( // We send a cancel message through the control: let response_received = ResponseReceived { request_id: pending_local_transaction.request_id, - result: ResponseSendFundsResult::Failure( - m_state.state().local_public_key.clone(), - ), + result: ResponseSendFundsResult::Failure, }; outgoing_control.push(FunderOutgoingControl::ResponseReceived(response_received)); } @@ -127,9 +125,7 @@ pub fn cancel_pending_requests( // We are the origin of this request: let response_received = ResponseReceived { request_id: pending_request.request_id, - result: ResponseSendFundsResult::Failure( - m_state.state().local_public_key.clone(), - ), + result: ResponseSendFundsResult::Failure, }; outgoing_control.push(FunderOutgoingControl::ResponseReceived(response_received)); } @@ -156,7 +152,7 @@ pub fn cancel_pending_user_requests( // We are the origin of this request: let response_received = ResponseReceived { request_id: pending_user_request.request_id, - result: ResponseSendFundsResult::Failure(m_state.state().local_public_key.clone()), + result: ResponseSendFundsResult::Failure, }; outgoing_control.push(FunderOutgoingControl::ResponseReceived(response_received)); } diff --git a/components/funder/src/handler/handle_friend.rs b/components/funder/src/handler/handle_friend.rs index 8bb3b0e5b..9d2f05800 100644 --- a/components/funder/src/handler/handle_friend.rs +++ b/components/funder/src/handler/handle_friend.rs @@ -6,34 +6,34 @@ use crypto::identity::{PublicKey, Signature, SIGNATURE_LEN}; use proto::app_server::messages::RelayAddress; use proto::funder::messages::{ - ChannelerUpdateFriend, FailureSendFunds, FriendMessage, FunderOutgoingControl, - MoveTokenRequest, PendingRequest, RequestSendFunds, ResetTerms, ResponseReceived, - ResponseSendFunds, ResponseSendFundsResult, + CancelSendFundsOp, ChannelerUpdateFriend, CommitSendFundsOp, FriendMessage, + FunderOutgoingControl, MoveTokenRequest, PendingTransaction, RequestSendFundsOp, ResetTerms, + ResponseReceived, ResponseSendFundsOp, ResponseSendFundsResult, }; -use proto::funder::signature_buff::{prepare_receipt, verify_move_token}; +use proto::funder::signature_buff::verify_move_token; use crate::mutual_credit::incoming::{ - IncomingFailureSendFunds, IncomingMessage, IncomingResponseSendFunds, + IncomingCancelSendFundsOp, IncomingCommitSendFundsOp, IncomingMessage, + IncomingResponseSendFundsOp, }; use crate::token_channel::{MoveTokenReceived, ReceiveMoveTokenOutput, TokenChannel}; -use crate::types::{create_pending_request, ChannelerConfig}; +use crate::types::{create_pending_transaction, ChannelerConfig}; use crate::friend::{ - ChannelInconsistent, ChannelStatus, FriendMutation, ResponseOp, SentLocalRelays, + BackwardsOp, ChannelInconsistent, ChannelStatus, FriendMutation, SentLocalRelays, }; use crate::state::FunderMutation; use crate::ephemeral::Ephemeral; use crate::handler::canceler::{ - cancel_local_pending_requests, cancel_pending_requests, cancel_pending_user_requests, - reply_with_failure, -}; -use crate::handler::handler::{ - find_request_origin, is_friend_ready, MutableEphemeral, MutableFunderState, + cancel_local_pending_transactions, cancel_pending_requests, cancel_pending_user_requests, + reply_with_cancel, }; use crate::handler::sender::SendCommands; +use crate::handler::state_wrap::{MutableEphemeral, MutableFunderState}; +use crate::handler::utils::{find_request_origin, is_friend_ready}; #[derive(Debug)] pub enum HandleFriendError { @@ -120,7 +120,7 @@ pub fn try_reset_channel( fn forward_request( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, - request_send_funds: RequestSendFunds, + request_send_funds: RequestSendFundsOp, ) where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, { @@ -144,7 +144,7 @@ fn handle_request_send_funds( ephemeral: &Ephemeral, send_commands: &mut SendCommands, remote_public_key: &PublicKey, - request_send_funds: RequestSendFunds, + request_send_funds: RequestSendFundsOp, ) where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, { @@ -158,12 +158,15 @@ fn handle_request_send_funds( let next_index = local_index.checked_add(1).unwrap(); if next_index >= request_send_funds.route.len() { // We are the destination of this request. We return a response: - let pending_request = create_pending_request(&request_send_funds); - let u_response_op = ResponseOp::UnsignedResponse(pending_request); + let pending_transaction = create_pending_transaction(&request_send_funds); + m_state.queue_unsigned_response(pending_transaction); + /* + let u_response_op = BackwardsOp::UnsignedResponse(pending_transaction); let friend_mutation = FriendMutation::PushBackPendingResponse(u_response_op); let funder_mutation = - FunderMutation::FriendMutation((remote_public_key.clone(), friend_mutation)); + FunderMutation::FriendMutation((remote_public_key.clone(), friend_mutation)); m_state.mutate(funder_mutation); + */ send_commands.set_try_send(&remote_public_key); return; } @@ -182,7 +185,7 @@ fn handle_request_send_funds( }; if !friend_ready { - reply_with_failure( + reply_with_cancel( m_state, send_commands, remote_public_key, @@ -199,31 +202,35 @@ fn handle_response_send_funds( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, outgoing_control: &mut Vec>, - response_send_funds: ResponseSendFunds, - pending_request: PendingRequest, + response_send_funds: ResponseSendFundsOp, + pending_transaction: PendingTransaction, ) where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, { match find_request_origin(m_state.state(), &response_send_funds.request_id).cloned() { None => { + unimplemented!(); + /* // We are the origin of this request, and we got a response. // We provide a receipt to the user: - let receipt = prepare_receipt(&response_send_funds, &pending_request); + let receipt = prepare_receipt(&response_send_funds, &pending_transaction); let response_send_funds_result = ResponseSendFundsResult::Success(receipt.clone()); outgoing_control.push(FunderOutgoingControl::ResponseReceived(ResponseReceived { - request_id: pending_request.request_id, + request_id: pending_transaction.request_id, result: response_send_funds_result, })); // We make our own copy of the receipt, in case the user abruptly crashes. // In that case the user will be able to obtain the receipt again later. - let funder_mutation = FunderMutation::AddReceipt((pending_request.request_id, receipt)); + let funder_mutation = + FunderMutation::AddReceipt((pending_transaction.request_id, receipt)); m_state.mutate(funder_mutation); + */ } Some(friend_public_key) => { // Queue this response message to another token channel: - let response_op = ResponseOp::Response(response_send_funds); - let friend_mutation = FriendMutation::PushBackPendingResponse(response_op); + let response_op = BackwardsOp::Response(response_send_funds); + let friend_mutation = FriendMutation::PushBackPendingBackwardsOp(response_op); let funder_mutation = FunderMutation::FriendMutation((friend_public_key.clone(), friend_mutation)); m_state.mutate(funder_mutation); @@ -233,31 +240,30 @@ fn handle_response_send_funds( } } -fn handle_failure_send_funds( +fn handle_cancel_send_funds( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, outgoing_control: &mut Vec>, - failure_send_funds: FailureSendFunds, - pending_request: PendingRequest, + cancel_send_funds: CancelSendFundsOp, + pending_transaction: PendingTransaction, ) where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, { - match find_request_origin(m_state.state(), &failure_send_funds.request_id).cloned() { + match find_request_origin(m_state.state(), &cancel_send_funds.request_id).cloned() { None => { - // We are the origin of this request, and we got a failure + // We are the origin of this request, and we got a cancellation. // We should pass it back to encryptor. - let response_send_funds_result = - ResponseSendFundsResult::Failure(failure_send_funds.reporting_public_key); + let response_send_funds_result = ResponseSendFundsResult::Failure; outgoing_control.push(FunderOutgoingControl::ResponseReceived(ResponseReceived { - request_id: pending_request.request_id, + request_id: pending_transaction.request_id, result: response_send_funds_result, })); } Some(friend_public_key) => { // Queue this failure message to another token channel: - let failure_op = ResponseOp::Failure(failure_send_funds); - let friend_mutation = FriendMutation::PushBackPendingResponse(failure_op); + let failure_op = BackwardsOp::Cancel(cancel_send_funds); + let friend_mutation = FriendMutation::PushBackPendingBackwardsOp(failure_op); let funder_mutation = FunderMutation::FriendMutation((friend_public_key.clone(), friend_mutation)); m_state.mutate(funder_mutation); @@ -267,6 +273,18 @@ fn handle_failure_send_funds( }; } +fn handle_commit_send_funds( + m_state: &mut MutableFunderState, + send_commands: &mut SendCommands, + outgoing_control: &mut Vec>, + commit_send_funds: CommitSendFundsOp, + pending_transaction: PendingTransaction, +) where + B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, +{ + unimplemented!(); +} + /// Process valid incoming operations from remote side. fn handle_move_token_output( m_state: &mut MutableFunderState, @@ -289,8 +307,8 @@ fn handle_move_token_output( request_send_funds, ); } - IncomingMessage::Response(IncomingResponseSendFunds { - pending_request, + IncomingMessage::Response(IncomingResponseSendFundsOp { + pending_transaction, incoming_response, }) => { handle_response_send_funds( @@ -298,19 +316,31 @@ fn handle_move_token_output( send_commands, outgoing_control, incoming_response, - pending_request, + pending_transaction, + ); + } + IncomingMessage::Cancel(IncomingCancelSendFundsOp { + pending_transaction, + incoming_cancel, + }) => { + handle_cancel_send_funds( + m_state, + send_commands, + outgoing_control, + incoming_cancel, + pending_transaction, ); } - IncomingMessage::Failure(IncomingFailureSendFunds { - pending_request, - incoming_failure, + IncomingMessage::Commit(IncomingCommitSendFundsOp { + pending_transaction, + incoming_commit, }) => { - handle_failure_send_funds( + handle_commit_send_funds( m_state, send_commands, outgoing_control, - incoming_failure, - pending_request, + incoming_commit, + pending_transaction, ); } } @@ -338,7 +368,7 @@ fn handle_move_token_error( let local_reset_terms = gen_reset_terms(&token_channel, rng); // Cancel all internal pending requests inside token channel: - cancel_local_pending_requests(m_state, send_commands, outgoing_control, remote_public_key); + cancel_local_pending_transactions(m_state, send_commands, outgoing_control, remote_public_key); // Cancel all pending requests to this friend: cancel_pending_requests(m_state, send_commands, outgoing_control, remote_public_key); cancel_pending_user_requests(m_state, outgoing_control, remote_public_key); diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index 1d23c9977..4e070b764 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -1,7 +1,8 @@ #[allow(unused)] mod canceler; // mod handle_control; -// mod handle_friend; +#[allow(unused)] +mod handle_friend; #[allow(unused)] mod handle_init; #[allow(unused)] diff --git a/components/funder/src/handler/sender.rs b/components/funder/src/handler/sender.rs index 470367d7d..8e4c9f4a7 100644 --- a/components/funder/src/handler/sender.rs +++ b/components/funder/src/handler/sender.rs @@ -485,7 +485,7 @@ where // We are the origin of this request let response_received = ResponseReceived { request_id: request_send_funds.request_id, - result: ResponseSendFundsResult::Failure(m_state.state().local_public_key.clone()), + result: ResponseSendFundsResult::Failure, }; outgoing_control.push(FunderOutgoingControl::ResponseReceived(response_received)); } diff --git a/components/funder/src/handler/state_wrap.rs b/components/funder/src/handler/state_wrap.rs index 3547a9291..7e81bad69 100644 --- a/components/funder/src/handler/state_wrap.rs +++ b/components/funder/src/handler/state_wrap.rs @@ -7,7 +7,7 @@ use crypto::identity::PublicKey; use crypto::uid::Uid; use proto::app_server::messages::RelayAddress; -use proto::funder::messages::FunderOutgoingControl; +use proto::funder::messages::{FunderOutgoingControl, PendingTransaction}; use proto::report::messages::{FunderReportMutation, FunderReportMutations}; use identity::IdentityClient; @@ -25,7 +25,7 @@ use crate::types::{ pub struct MutableFunderState { initial_state: FunderState, state: FunderState, - unsigned_responses: Vec, + unsigned_responses: Vec, mutations: Vec>, } @@ -45,8 +45,8 @@ where /// Push an unsigned response operation. /// We have a separate queue for these operations because we need an async function call to /// sign an unsigned response. - pub fn queue_unsigned_response(&mut self, u_response_send_funds: UnsignedResponseSendFundsOp) { - self.unsigned_responses.push(u_response_send_funds); + pub fn queue_unsigned_response(&mut self, pending_transaction: PendingTransaction) { + self.unsigned_responses.push(pending_transaction); } pub fn mutate(&mut self, mutation: FunderMutation) { diff --git a/components/proto/src/app_server/mod.rs b/components/proto/src/app_server/mod.rs index d8fcde7fc..ed4bfd786 100644 --- a/components/proto/src/app_server/mod.rs +++ b/components/proto/src/app_server/mod.rs @@ -1,2 +1,3 @@ pub mod messages; +#[allow(unused)] pub mod serialize; diff --git a/components/proto/src/app_server/serialize.rs b/components/proto/src/app_server/serialize.rs index 58768512e..d5a61edd5 100644 --- a/components/proto/src/app_server/serialize.rs +++ b/components/proto/src/app_server/serialize.rs @@ -2,9 +2,10 @@ use std::io; use crate::capnp_common::{ read_custom_int128, read_custom_u_int128, read_invoice_id, read_named_index_server_address, - read_named_relay_address, read_public_key, read_receipt, read_relay_address, read_signature, - read_uid, write_custom_int128, write_custom_u_int128, write_invoice_id, - write_named_index_server_address, write_named_relay_address, write_public_key, write_receipt, + read_named_relay_address, read_public_key, /*read_receipt,*/ read_relay_address, + read_signature, read_uid, write_custom_int128, write_custom_u_int128, write_invoice_id, + write_named_index_server_address, write_named_relay_address, + write_public_key, /*write_receipt,*/ write_relay_address, write_signature, write_uid, }; use capnp; @@ -24,7 +25,7 @@ use index_server::serialize::{ }; use crate::funder::messages::{ - AddFriend, ReceiptAck, ResetFriendChannel, ResponseReceived, ResponseSendFundsResult, + AddFriend, ReceiptAck, ResetFriendChannel, ResponseReceived, /*ResponseSendFundsResult, */ SetFriendName, SetFriendRelays, SetFriendRemoteMaxDebt, UserRequestSendFunds, }; use crate::funder::serialize::{deser_friends_route, ser_friends_route}; @@ -73,6 +74,8 @@ fn ser_response_received( response_received: &ResponseReceived, response_received_builder: &mut app_server_capnp::response_received::Builder, ) { + unimplemented!(); + /* write_uid( &response_received.request_id, &mut response_received_builder.reborrow().init_request_id(), @@ -89,11 +92,14 @@ fn ser_response_received( write_public_key(public_key, &mut failure_builder); } }; + */ } fn deser_response_received( response_received_reader: &app_server_capnp::response_received::Reader, ) -> Result { + unimplemented!(); + /* let result = match response_received_reader.get_result().which()? { app_server_capnp::response_received::result::Success(receipt_reader) => { let receipt_reader = receipt_reader?; @@ -109,6 +115,7 @@ fn deser_response_received( request_id: read_uid(&response_received_reader.get_request_id()?)?, result, }) + */ } fn ser_receipt_ack( diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 24baa48fe..cae840360 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -511,7 +511,7 @@ impl UserRequestSendFunds { #[derive(Debug, Clone, PartialEq, Eq)] pub enum ResponseSendFundsResult { Success(Receipt), - Failure(PublicKey), // Reporting public key. + Failure, } #[derive(Debug, Clone, PartialEq, Eq)] From b7745b99bfed3060472e1c246d40eeda95f86f8d Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 13:23:40 +0300 Subject: [PATCH 074/248] Added totalDestPayment, renamed Commit -> Collect, Confirmation -> Commit. --- doc/docs/atomic_proposal.md | 148 +++++++++++++++++++----------------- 1 file changed, 80 insertions(+), 68 deletions(-) diff --git a/doc/docs/atomic_proposal.md b/doc/docs/atomic_proposal.md index 5c3c3fdce..a5cbe411f 100644 --- a/doc/docs/atomic_proposal.md +++ b/doc/docs/atomic_proposal.md @@ -158,29 +158,30 @@ Invoice <=====[inv]======== (Out of band) Request ------[req]-------> Response <-----[resp]------- -Confirmation ======[conf]======> (Out of band) +Commit ======[commit]====> (Out of band) Goods <=====[goods]====== (Out of band) -Commit <-----[commit]----- +Collect <-----[collect]---- (Receipt) B --- C --- D --- E ``` + The following operations occur: 1. E hands an Invoice to B. (Out of band) 2. B sends a Request message along a path to E. 3. E sends back a Response message along the same route to B. -4. B prepares a Confirmation message and hands it to E (Out of band). -5. E sends a Commit message along the route to B. -6. B receives the Commit message, prepares a Receipt and keeps it. +4. B prepares a Commit message and hands it to E (Out of band). +5. E sends a Collect message along the route to B. +6. B receives the Collect message, prepares a Receipt and keeps it. From the point of view of Offst users, this is how a transaction looks like: ```text Invoice <=====[inv]======== (Out of band) -Confirmation ======[conf]======> (Out of band) +Commit ======[commit]====> (Out of band) Goods <=====[goods]====== (Out of band) B -- ... ... -- E ``` @@ -191,7 +192,7 @@ that it might take some time until the seller E will be able to collect his credits. Cancellation can happen at any time after the Request message was sent from the -buyer and before the Commit message was sent. +buyer and before the Collect message was sent. During the Request stage a cancellation message could be sent from any node forwarding the Request message. However, after the Request message arrives at @@ -226,7 +227,7 @@ Cancel <-----[cancel]----- ``` -- Confirmation took too long to arrive: +- Commit took too long to arrive: ```text Invoice <=====[inv]======== (Out of band) @@ -291,7 +292,10 @@ struct RequestSendFundsOp { route @2: FriendsRoute; # A route of friends that leads to the destination destPayment @3: CustomUInt128; - # Amount of credits to pay the destination + # Amount of credits to pay the destination over this route. + totalDestPayment @4: CustomUInt128; + # Total amount of credits to be paid (Must match the invoice) + # totalDestPayment > destPayment in cases of multi route-payments. invoiceId @4: InvoiceId; # A 256 bit value representing the invoice this request # intends to pay. @@ -314,7 +318,7 @@ The Request message contains a hash lock: `srcHashedLock`. This value is generated by the buyer by generating a random `srcPlainLock` value and hashing it: `srcHashedLock := bcrypt(srcPlainLock)`. This mechanism is used to ensure transaction atomicity: The seller can not create a -valid Commit message without knowing the secret value `srcPlainLock`. +valid Collect message without knowing the secret value `srcPlainLock`. ### (*) Response message @@ -339,6 +343,7 @@ struct ResponseSendFundsOp { # srcHashedLock || # destHashedLock || # destPayment || + # totalDestPayment || # invoiceId # ) # @@ -350,7 +355,7 @@ struct ResponseSendFundsOp { Note that the response contains a `destHashedLock`. This value is created by hashing a secret generated by the seller: `destHashedLock := -bcrypt(destPlainLock)`. This secret will only be revealed when the Commit +bcrypt(destPlainLock)`. This secret will only be revealed when the Collect message is sent. We have this mechanism to defend against fake Receipt generated by the buyer. (A valid receipt must contain the secret `destPlainLock`). @@ -358,13 +363,13 @@ generated by the buyer. (A valid receipt must contain the secret ### (*) Cancel message A Cancel message may be sent back by any node during the Request period. -After Request message arrived at the seller node and before the Commit +After Request message arrived at the seller node and before the Collect message was sent, only the seller node may send a Cancel message. In addition, any node may send a Cancel message to cancel ongoing transactions -in case of unfriending a node (As long as the Commit message was not yet +in case of unfriending a node (As long as the Collect message was not yet received). -After the Commit message was received, the transaction can not be cancelled. +After the Collect message was received, the transaction can not be cancelled. If any node could not forward the request message, or the destination decided to cancel the transaction, a failure message will be sent back, beginning from @@ -383,56 +388,58 @@ struct CancelSendFundsOp { } ``` -### (*) Confirmation message (Out of band) +### (*) Commit message (Out of band) After receiving a Response message, the source node of the payment creates a -Confirmation message. The Confirmation message is given to the destination, and +Commit message. The Commit message is given to the destination, and at that moment the payment is considered successful. ```text -=======[conf]=====> (Out of band) +======[commit]====> (Out of band) B --- C --- D --- E ``` -Upon receipt of a valid Confirmation message, the seller will give the goods to -the buyer, and send back (along the same route) a Commit message to collect his +Upon receipt of a valid Commit message, the seller will give the goods to +the buyer, and send back (along the same route) a Collect message to collect his credits. ```capnp -struct Confirmation { +struct Commit { responseHash @0: Hash; # = sha512/256(requestId || sha512/256(route) || randNonce) destPayment @1: CustomUInt128; - srcPlainLock: @2: Lock; + totalDestPayment @2: CustomUInt128; + srcPlainLock: @3: Lock; # The preimage of the hashedLock at the request message [256 bits] - signature @3: Signature; + signature @4: Signature; # Signature{key=destinationKey}( # sha512/256("FUNDS_RESPONSE") || # sha512/256(requestId || sha512/256(route) || randNonce) || # srcHashedLock || # destHashedLock || # destPayment || + # totalDestPayment || # invoiceId # ) } -struct MultiConfirmation { +struct MultiCommit { invoiceId @0: InvoiceId; # InvoiceId being paid. - confirmations @1: List(Confirmation); + commits @1: List(Commit); # A list of confirmations. Each confirmation corresponds to a request # sent along one route. } ``` -Note that the `MultiConfirmation` message may contain multiple -`Confirmation`-s, each corresponding to one request. This allows +Note that the `MultiCommit` message may contain multiple +`Commit`-s, each corresponding to one request. This allows fragmented payment along multiple routes. -Verification of a MultiConfirmation message by the seller is done as follows: +Verification of a MultiCommit message by the seller is done as follows: - InvoiceId matches an originally issued invoice. -- For every Confirmation: +- For every Commit: - The revealed lock is valid: `bcrypt(srcPlainLock) == srcHashedLock` - signature is valid. @@ -440,37 +447,37 @@ Verification of a MultiConfirmation message by the seller is done as follows: invoice). -### (*) Commit message +### (*) Collect message After receiving a confirmation message from the buyer, the destination -gives the goods to the buyer and sends back a Commit message to +gives the goods to the buyer and sends back a Collect message to collect his credits. ```text - <----[commit]----- + <----[collect]---- B --- C --- D --- E ``` -A Commit message completes the transaction. For example, when the Commit +A Collect message completes the transaction. For example, when the Collect message is sent from E to D, the credits that were frozen between D and E -become unfrozen, and the payment is irreversible. The Commit messages continues +become unfrozen, and the payment is irreversible. The Collect messages continues all the way (along the original route) to the source of the payment. ```capnp -struct CommitSendFundsOp { +struct CollectSendFundsOp { requestId @0: Uid; srcPlainLock @1: Lock; destPlainLock @2: Lock; } ``` -Note that the Commit message can only be sent by the seller after +Note that the Collect message can only be sent by the seller after it has received the confirmation, because the confirmation contains the srcPlainLock. -When receiving a CommitSendFundsOp messages, the following should be verified: +When receiving a CollectSendFundsOp messages, the following should be verified: - `bcrypt(srcPlainLock) = srcHashedLock` - `bcrypt(destPlainLock) = destHashedLock` @@ -489,6 +496,7 @@ struct Receipt { srcPlainLock @2: Lock; destPlainLock @3: Lock; destPayment @4: CustomUInt128; + destTotalPayment @4: CustomUInt128; signature @5: Signature; # Signature{key=destinationKey}( # sha512/256("FUNDS_RESPONSE") || @@ -496,12 +504,13 @@ struct Receipt { # srcHashedLock || # dstHashedLock || # destPayment || + # destTotalPayment || # invoiceId # ) } ``` -The Receipt can be constructed only after the CommitSendFundsOp message was received. +The Receipt can be constructed only after the CollectSendFundsOp message was received. Note that it is possible that a receipt can be constructed only a long time after the confirmation message was given. @@ -516,19 +525,19 @@ Assume that the node E issued an invoice and handed it to B. B wants to pay the invoice. The payment begins by sending a Request message along the path from B to E. The payment is considered successful when B hands a -MultiConfirmation message to E. +MultiCommit message to E. This means that we should examine the possibility of B waiting indefinitely during the sending of Request and Response messages along the route. During this time (Request + Response period), B can discard the transaction by walking away. E will not be able to make progress because in order to send the -Commit message, the correct srcPlainLock is required, but E does not know it -before B sends the MultiConfirmation message. +Collect message, the correct srcPlainLock is required, but E does not know it +before B sends the MultiCommit message. -Also note that if B sends a valid MultiConfirmation message to E, the transaction is +Also note that if B sends a valid MultiCommit message to E, the transaction is considered successful, and B can not reverse it. This happens because B reveals -srcPlainLock at the MultiConfirmation message sent to E. +srcPlainLock at the MultiCommit message sent to E. ## Receipt verifiability @@ -548,11 +557,11 @@ destination: `destHashedLock`. When the buyer receives a Response message it can not yet create a valid Receipt, because the buyer doesn't yet know `destPlainLock`. -This value is revealed only at the Commit message, when the payment is +This value is revealed only at the Collect message, when the payment is considered to be successful. Note: An alternative solution could be to let the seller sign a new -signature over the Commit message, but instead we chose to use a hash lock, +signature over the Collect message, but instead we chose to use a hash lock, which is a less expensive cryptographic operation. Using a hash lock also does not require access to the identity of the seller. @@ -569,10 +578,10 @@ recognized InvoiceId (TODO: Can this cause any issues?) In addition, cancellation can be issued for a certain `invoiceId` from the application level. Cancellation message should only be sent after the invoice was -issued and before a Confirmation message was received. It might be possible for +issued and before a Commit message was received. It might be possible for applications to cancel `invoiceId`-s after a certain amount of time. -- Sending a Cancel message after a Confirmation message was received is +- Sending a Cancel message after a Commit message was received is considered a bad form for the seller, and can be seen as equivalent to not delivering the goods after a successful payment. @@ -582,7 +591,7 @@ applications to cancel `invoiceId`-s after a certain amount of time. The only way for the buyer to cancel a transaction is by never -sending a Confirmation message to the seller. +sending a Commit message to the seller. ## Fragmented/Multi-Route transactions @@ -598,17 +607,17 @@ This is done as follows: 3. A ResponseSendFundsOp or a CancelSendFundsOp message is returned. 4. Go back to (2) until the wanted amount of credits is acheived (for paying the invoice). -5. Buyer sends a MultiConfirmation message containing a list of all - Confirmation-s for all the requests that got a valid response. -6. Seller verifies the MultiConfirmation message. If valid, the payment is accepted +5. Buyer sends a MultiCommit message containing a list of all + Commit-s for all the requests that got a valid response. +6. Seller verifies the MultiCommit message. If valid, the payment is accepted and the goods are handed to the buyer. -7. The Seller sends back CommitSendFundsOp messages for all requests. -8. Any CommitSendFundsOp message can be used to create a valid Receipt. (Two diferent +7. The Seller sends back CollectSendFundsOp messages for all requests. +8. Any CollectSendFundsOp message can be used to create a valid Receipt. (Two diferent constructed receipts will have the same invoiceId but different responseHash). -## Extra: Non-atomic payment without Confirmation +## Extra: Non-atomic payment without Commit Consider the following route: @@ -629,7 +638,7 @@ Invoice <=====[inv]======== (Out of band) Request ------[req]-------> Response <-----[resp]------- -Commit <-----[commit]----- +Collect <-----[collect]---- (Receipt) B --- C --- D --- E ``` @@ -638,8 +647,8 @@ Commit <-----[commit]----- This can be achieved by having B choose a well known value for `srcPlainLock`, for example, 256 consecutive zero bytes (`'\x00' * 256`). This should allow E -to "guess" `srcPlainLock` and send back a Commit message immediately. -Therefore the out of band Confirmation message sent from B is not required. +to "guess" `srcPlainLock` and send back a Collect message immediately. +Therefore the out of band Commit message sent from B is not required. This construction allows taking donations through a static HTTPS webpage. All one has to do is publish his public key. We are still not though sure if @@ -649,6 +658,8 @@ Idea: Change `srcHashedLock` to be a sum type of None or srcHashedLock? This way guessing will not be required. Might depend on the hash and salt representation. +Note: We can not use this for multi-route payments. + ## Application interface We describe here Offst nodes' interface with an Application. @@ -663,15 +674,16 @@ use this implementation. ```markdown RequestPay [App -> Node] ========== -- Invoice - - invoiceId - - Destination public key - - Amount of credits +- requestId +- invoiceId +- destPayment +- totalDestPayment +- Destination public key ResponsePay [Node -> App] =========== -- Confirmation or Cancelleation +- Commit or Cancellation SendFunds [App -> Node] @@ -688,7 +700,7 @@ RequestReceipt [App -> Node] ResponseReceipt [Node -> App] =============== -- invoiceId or Empty +- Receipt or Empty RemoveReceipt [App -> Node] @@ -703,7 +715,7 @@ To pay an invoice, a buyer can do the following: - Wait for `ResponsePay` to arrive from the Node. If waiting takes too long, forget about the payment. - If `ResponsePay` contains cancel, cancel the payment. - Else, send Confirmation to the seller. + Else, send Commit to the seller. After a while: @@ -726,19 +738,19 @@ CancelInvoice [App -> Node] - invoiceId -Confirm [App -> Node] -======= -- Confirmation +CommitInvoice [App -> Node] +============== +- MultiCommit ``` Suppose that the seller has a website, and a user wants to buy an item on the website. The seller will perform the following: - Generate a new `invoiceId` and send `AddInvoice` to the Seller's Node. -- Send an invoice to the buyer (Out of band), and wait for Confirmation from +- Send an invoice to the buyer (Out of band), and wait for Commit from the buyer. - If waited too long for confirmation, send `CancelInvoice` to node and forget about this transaction. - Receive a confirmation from the buyer (Out of band). -- Send `Confirm` to the Node. +- Send `MultiCommit` to the Node. From 70f8cead1aa784de1571bc34001fe0175bf55bc6 Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 13:24:39 +0300 Subject: [PATCH 075/248] Some work on handle_control.rs. Compiling but not correct. --- .../funder/src/handler/handle_control.rs | 25 ++++++++------- components/funder/src/handler/mod.rs | 1 + components/funder/src/state.rs | 2 +- components/proto/src/app_server/serialize.rs | 6 ++++ components/proto/src/funder/messages.rs | 31 +++++++++++++++---- 5 files changed, 46 insertions(+), 19 deletions(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index e69d87426..f7ea62a21 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -17,10 +17,11 @@ use proto::funder::messages::{ use crate::ephemeral::Ephemeral; use crate::handler::canceler::{ - cancel_local_pending_requests, cancel_pending_requests, cancel_pending_user_requests, + cancel_local_pending_transactions, cancel_pending_requests, cancel_pending_user_requests, }; -use crate::handler::handler::{is_friend_ready, MutableEphemeral, MutableFunderState}; use crate::handler::sender::SendCommands; +use crate::handler::state_wrap::{MutableEphemeral, MutableFunderState}; +use crate::handler::utils::is_friend_ready; use crate::types::ChannelerConfig; @@ -256,7 +257,7 @@ where &remove_friend.friend_public_key, ); - cancel_local_pending_requests( + cancel_local_pending_transactions( m_state, send_commands, outgoing_control, @@ -448,10 +449,10 @@ where if let Some(receipt) = m_state .state() .ready_receipts - .get(&user_request_send_funds.request_id) + .get(&user_request_send_funds.payment_id) { let response_received = ResponseReceived { - request_id: user_request_send_funds.request_id, + request_id: user_request_send_funds.payment_id, result: ResponseSendFundsResult::Success(receipt.clone()), }; outgoing_control.push(FunderOutgoingControl::ResponseReceived(response_received)); @@ -482,10 +483,10 @@ where return Err(HandleControlError::FriendNotReady); } - // If request is already in progress, we do nothing: - // Check if there is already a pending user request with the same request_id: + // If payment is already in progress, we do nothing: + // Check if there is already a pending user payment with the same payment_id: for user_request in &friend.pending_user_requests { - if user_request_send_funds.request_id == user_request.request_id { + if user_request_send_funds.payment_id == user_request.request_id { return Err(HandleControlError::RequestAlreadyInProgress); } } @@ -499,8 +500,8 @@ where if token_channel .get_mutual_credit() .state() - .pending_requests - .pending_local_requests + .pending_transactions + .local .contains_key(&user_request_send_funds.request_id) { return Err(HandleControlError::RequestAlreadyInProgress); @@ -544,8 +545,8 @@ where ) { error!("control_request_send_funds_inner() failed: {:?}", e); let response_received = ResponseReceived { - request_id: user_request_send_funds.request_id, - result: ResponseSendFundsResult::Failure(m_state.state().local_public_key.clone()), + request_id: user_request_send_funds.payment_id, + result: ResponseSendFundsResult::Failure, }; outgoing_control.push(FunderOutgoingControl::ResponseReceived(response_received)); diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index 4e070b764..d8720bc4b 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -1,5 +1,6 @@ #[allow(unused)] mod canceler; +// #[allow(unused)] // mod handle_control; #[allow(unused)] mod handle_friend; diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index e2698a4e9..35e929a67 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -30,7 +30,7 @@ pub struct FunderState { /// Receipts of completed payments (Generated after a CommitSendFundsOp message was received /// successfuly). /// Note: We use our own randomly generated paymentId to represent a payment and not an - /// InvoiceId. This happens to defend against possible collisions of InvoiceId values (Which + /// InvoiceId. We do this to defend against possible collisions of InvoiceId values (Which /// are not locally generated). pub ready_receipts: ImHashMap, } diff --git a/components/proto/src/app_server/serialize.rs b/components/proto/src/app_server/serialize.rs index d5a61edd5..97a1d6dc0 100644 --- a/components/proto/src/app_server/serialize.rs +++ b/components/proto/src/app_server/serialize.rs @@ -38,6 +38,8 @@ fn ser_user_request_send_funds( user_request_send_funds: &UserRequestSendFunds, user_request_send_funds_builder: &mut app_server_capnp::user_request_send_funds::Builder, ) { + unimplemented!(); + /* write_uid( &user_request_send_funds.request_id, &mut user_request_send_funds_builder.reborrow().init_request_id(), @@ -57,17 +59,21 @@ fn ser_user_request_send_funds( &user_request_send_funds.invoice_id, &mut user_request_send_funds_builder.reborrow().init_invoice_id(), ); + */ } fn deser_user_request_send_funds( user_request_send_funds_reader: &app_server_capnp::user_request_send_funds::Reader, ) -> Result { + unimplemented!(); + /* Ok(UserRequestSendFunds { request_id: read_uid(&user_request_send_funds_reader.get_request_id()?)?, route: deser_friends_route(&user_request_send_funds_reader.get_route()?)?, dest_payment: read_custom_u_int128(&user_request_send_funds_reader.get_dest_payment()?)?, invoice_id: read_invoice_id(&user_request_send_funds_reader.get_invoice_id()?)?, }) + */ } fn ser_response_received( diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index cae840360..3ff1c68f5 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -6,6 +6,7 @@ use crypto::hash::{self, HashResult}; use crypto::hash_lock::{HashedLock, PlainLock}; use crypto::identity::{PublicKey, Signature}; use crypto::invoice_id::InvoiceId; +use crypto::payment_id::PaymentId; use crypto::uid::Uid; use crate::app_server::messages::{NamedRelayAddress, RelayAddress}; @@ -84,7 +85,7 @@ pub struct CancelSendFundsOp { } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] -pub struct Confirmation { +pub struct Commit { response_hash: HashResult, dest_payment: u128, src_plain_lock: PlainLock, @@ -92,9 +93,9 @@ pub struct Confirmation { } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] -pub struct MultiConfirmation { +pub struct MultiCommit { invoice_id: InvoiceId, - confirmations: Vec>, + confirmations: Vec>, } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] @@ -440,10 +441,19 @@ pub struct ResetFriendChannel { pub reset_token: Signature, } +/// A friend's route with known capacity +#[derive(Debug, Clone, PartialEq, Eq)] +struct FriendsRouteCapacity { + route: FriendsRoute, + capacity: u128, +} + /// A request to send funds that originates from the user #[derive(Debug, Clone, PartialEq, Eq)] pub struct UserRequestSendFunds { - pub request_id: Uid, + /// payment_id is a randomly generated value (by the user), allowing the user to refer to a + /// certain payment. + pub payment_id: PaymentId, pub route: FriendsRoute, pub invoice_id: InvoiceId, pub dest_payment: u128, @@ -467,8 +477,16 @@ pub enum FunderControl { SetFriendRelays(SetFriendRelays), SetFriendName(SetFriendName), ResetFriendChannel(ResetFriendChannel), - RequestSendFunds(UserRequestSendFunds), - ReceiptAck(ReceiptAck), + // Buyer API: + RequestPay(()), + // SendFunds(()) // TODO: Extra + RequestReceipt(PaymentId), + RemoveReceipt(PaymentId), + // Seller API: + AddInvoice(InvoiceId), + CancelInvoice(InvoiceId), + // TODO: Possibly find a better name for this? + CommitInvoice(InvoiceId), } #[derive(Debug, Clone, PartialEq, Eq)] @@ -511,6 +529,7 @@ impl UserRequestSendFunds { #[derive(Debug, Clone, PartialEq, Eq)] pub enum ResponseSendFundsResult { Success(Receipt), + // TODO: Should we add more information to the failure here? Failure, } From ad72e04b7be2d94b97d3f720c8e312ffc0e02191 Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 13:43:33 +0300 Subject: [PATCH 076/248] Updated interface with node (buyer/seller). --- doc/docs/atomic_proposal.md | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/doc/docs/atomic_proposal.md b/doc/docs/atomic_proposal.md index a5cbe411f..0b7063cc4 100644 --- a/doc/docs/atomic_proposal.md +++ b/doc/docs/atomic_proposal.md @@ -672,30 +672,35 @@ use this implementation. ### Buyer interface ```markdown -RequestPay [App -> Node] -========== -- requestId +CreatePayment [App -> Node] +============= +- paymentId - invoiceId -- destPayment - totalDestPayment - Destination public key +CreateTransaction [App -> Node] +================= +- paymentId +- requestId +- route + capacity + fees +- destPayment -ResponsePay [Node -> App] -=========== -- Commit or Cancellation - +TransactionResult [Node -> App] +================= +- Response or Cancellation + RequestReceipt [App -> Node] ============== -- invoiceId +- paymentId ResponseReceipt [Node -> App] @@ -705,7 +710,7 @@ ResponseReceipt [Node -> App] RemoveReceipt [App -> Node] ============= -- invoiceId +- paymentId ``` From d7cd066ee49897d3faeb8090b4978c34cc548d5a Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 13:43:47 +0300 Subject: [PATCH 077/248] Renamed CommitSendFunds -> CollectSendFunds. --- components/funder/src/friend.rs | 4 +-- .../funder/src/handler/handle_friend.rs | 16 +++++----- components/funder/src/handler/sender.rs | 2 +- .../funder/src/mutual_credit/incoming.rs | 32 +++++++++---------- .../funder/src/mutual_credit/outgoing.rs | 22 ++++++------- components/funder/src/mutual_credit/tests.rs | 10 +++--- components/funder/src/state.rs | 2 +- components/funder/src/types.rs | 4 +-- components/proto/src/funder/messages.rs | 8 ++--- 9 files changed, 50 insertions(+), 50 deletions(-) diff --git a/components/funder/src/friend.rs b/components/funder/src/friend.rs index 93c10196b..d0c291f90 100644 --- a/components/funder/src/friend.rs +++ b/components/funder/src/friend.rs @@ -9,7 +9,7 @@ use common::safe_arithmetic::SafeUnsignedArithmetic; use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ - CancelSendFundsOp, CommitSendFundsOp, FriendStatus, PendingTransaction, RequestSendFundsOp, + CancelSendFundsOp, CollectSendFundsOp, FriendStatus, PendingTransaction, RequestSendFundsOp, RequestsStatus, ResetTerms, ResponseSendFundsOp, }; @@ -23,7 +23,7 @@ pub enum BackwardsOp { // /// A response that was not yet signed (Signing requires an async call) // UnsignedResponse((PendingTransaction, PlainLock)), Cancel(CancelSendFundsOp), - Commit(CommitSendFundsOp), + Collect(CollectSendFundsOp), } #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/components/funder/src/handler/handle_friend.rs b/components/funder/src/handler/handle_friend.rs index 9d2f05800..b1f05c5c7 100644 --- a/components/funder/src/handler/handle_friend.rs +++ b/components/funder/src/handler/handle_friend.rs @@ -6,14 +6,14 @@ use crypto::identity::{PublicKey, Signature, SIGNATURE_LEN}; use proto::app_server::messages::RelayAddress; use proto::funder::messages::{ - CancelSendFundsOp, ChannelerUpdateFriend, CommitSendFundsOp, FriendMessage, + CancelSendFundsOp, ChannelerUpdateFriend, CollectSendFundsOp, FriendMessage, FunderOutgoingControl, MoveTokenRequest, PendingTransaction, RequestSendFundsOp, ResetTerms, ResponseReceived, ResponseSendFundsOp, ResponseSendFundsResult, }; use proto::funder::signature_buff::verify_move_token; use crate::mutual_credit::incoming::{ - IncomingCancelSendFundsOp, IncomingCommitSendFundsOp, IncomingMessage, + IncomingCancelSendFundsOp, IncomingCollectSendFundsOp, IncomingMessage, IncomingResponseSendFundsOp, }; use crate::token_channel::{MoveTokenReceived, ReceiveMoveTokenOutput, TokenChannel}; @@ -273,11 +273,11 @@ fn handle_cancel_send_funds( }; } -fn handle_commit_send_funds( +fn handle_collect_send_funds( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, outgoing_control: &mut Vec>, - commit_send_funds: CommitSendFundsOp, + collect_send_funds: CollectSendFundsOp, pending_transaction: PendingTransaction, ) where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, @@ -331,15 +331,15 @@ fn handle_move_token_output( pending_transaction, ); } - IncomingMessage::Commit(IncomingCommitSendFundsOp { + IncomingMessage::Collect(IncomingCollectSendFundsOp { pending_transaction, - incoming_commit, + incoming_collect, }) => { - handle_commit_send_funds( + handle_collect_send_funds( m_state, send_commands, outgoing_control, - incoming_commit, + incoming_collect, pending_transaction, ); } diff --git a/components/funder/src/handler/sender.rs b/components/funder/src/handler/sender.rs index 8e4c9f4a7..3f9f52a9e 100644 --- a/components/funder/src/handler/sender.rs +++ b/components/funder/src/handler/sender.rs @@ -542,7 +542,7 @@ where match backwards_op { BackwardsOp::Response(response) => FriendTcOp::ResponseSendFunds(response), BackwardsOp::Cancel(cancel_send_funds) => FriendTcOp::CancelSendFunds(cancel_send_funds), - BackwardsOp::Commit(commit_send_funds) => FriendTcOp::CommitSendFunds(commit_send_funds), + BackwardsOp::Collect(collect_send_funds) => FriendTcOp::CollectSendFunds(collect_send_funds), } } diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index 0a036763f..d11a697e4 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -4,7 +4,7 @@ use common::int_convert::usize_to_u32; use common::safe_arithmetic::SafeSignedArithmetic; use proto::funder::messages::{ - CancelSendFundsOp, CommitSendFundsOp, FriendTcOp, PendingTransaction, RequestSendFundsOp, + CancelSendFundsOp, CollectSendFundsOp, FriendTcOp, PendingTransaction, RequestSendFundsOp, RequestsStatus, ResponseSendFundsOp, TransactionStage, }; use proto::funder::signature_buff::create_response_signature_buffer; @@ -26,9 +26,9 @@ pub struct IncomingCancelSendFundsOp { } #[derive(Debug)] -pub struct IncomingCommitSendFundsOp { +pub struct IncomingCollectSendFundsOp { pub pending_transaction: PendingTransaction, - pub incoming_commit: CommitSendFundsOp, + pub incoming_collect: CollectSendFundsOp, } #[derive(Debug)] @@ -36,7 +36,7 @@ pub enum IncomingMessage { Request(RequestSendFundsOp), Response(IncomingResponseSendFundsOp), Cancel(IncomingCancelSendFundsOp), - Commit(IncomingCommitSendFundsOp), + Collect(IncomingCollectSendFundsOp), } /// Resulting tasks to perform after processing an incoming operation. @@ -65,7 +65,7 @@ pub enum ProcessOperationError { NotExpectingResponse, InvalidSrcPlainLock, InvalidDestPlainLock, - NotExpectingCommit, + NotExpectingCollect, } #[derive(Debug)] @@ -118,8 +118,8 @@ pub fn process_operation( FriendTcOp::CancelSendFunds(cancel_send_funds) => { process_cancel_send_funds(mutual_credit, cancel_send_funds) } - FriendTcOp::CommitSendFunds(commit_send_funds) => { - process_commit_send_funds(mutual_credit, commit_send_funds) + FriendTcOp::CollectSendFunds(collect_send_funds) => { + process_collect_send_funds(mutual_credit, collect_send_funds) } } } @@ -390,9 +390,9 @@ fn process_cancel_send_funds( }) } -fn process_commit_send_funds( +fn process_collect_send_funds( mutual_credit: &mut MutualCredit, - commit_send_funds: CommitSendFundsOp, + collect_send_funds: CollectSendFundsOp, ) -> Result { // Make sure that id exists in local_pending hashmap, // and access saved request details. @@ -401,21 +401,21 @@ fn process_commit_send_funds( // Obtain pending request: // TODO: Possibly get rid of clone() here for optimization later let pending_transaction = local_pending_transactions - .get(&commit_send_funds.request_id) + .get(&collect_send_funds.request_id) .ok_or(ProcessOperationError::RequestDoesNotExist)? .clone(); let dest_hashed_lock = match &pending_transaction.stage { TransactionStage::Response(dest_hashed_lock) => dest_hashed_lock, - _ => return Err(ProcessOperationError::NotExpectingCommit), + _ => return Err(ProcessOperationError::NotExpectingCollect), }; // Verify src_plain_lock and dest_plain_lock: - if commit_send_funds.src_plain_lock.hash() != pending_transaction.src_hashed_lock { + if collect_send_funds.src_plain_lock.hash() != pending_transaction.src_hashed_lock { return Err(ProcessOperationError::InvalidSrcPlainLock); } - if commit_send_funds.dest_plain_lock.hash() != *dest_hashed_lock { + if collect_send_funds.dest_plain_lock.hash() != *dest_hashed_lock { return Err(ProcessOperationError::InvalidDestPlainLock); } @@ -430,7 +430,7 @@ fn process_commit_send_funds( let mut mc_mutations = Vec::new(); // Remove entry from local_pending hashmap: - let mc_mutation = McMutation::RemoveLocalPendingTransaction(commit_send_funds.request_id); + let mc_mutation = McMutation::RemoveLocalPendingTransaction(collect_send_funds.request_id); mutual_credit.mutate(&mc_mutation); mc_mutations.push(mc_mutation); @@ -457,9 +457,9 @@ fn process_commit_send_funds( mutual_credit.mutate(&mc_mutation); mc_mutations.push(mc_mutation); - let incoming_message = Some(IncomingMessage::Commit(IncomingCommitSendFundsOp { + let incoming_message = Some(IncomingMessage::Collect(IncomingCollectSendFundsOp { pending_transaction, - incoming_commit: commit_send_funds, + incoming_collect: collect_send_funds, })); Ok(ProcessOperationOutput { diff --git a/components/funder/src/mutual_credit/outgoing.rs b/components/funder/src/mutual_credit/outgoing.rs index d6bf7d0c9..4e35ad022 100644 --- a/components/funder/src/mutual_credit/outgoing.rs +++ b/components/funder/src/mutual_credit/outgoing.rs @@ -4,7 +4,7 @@ use common::int_convert::usize_to_u32; use common::safe_arithmetic::SafeSignedArithmetic; use proto::funder::messages::{ - CancelSendFundsOp, CommitSendFundsOp, FriendTcOp, PendingTransaction, RequestSendFundsOp, + CancelSendFundsOp, CollectSendFundsOp, FriendTcOp, PendingTransaction, RequestSendFundsOp, RequestsStatus, ResponseSendFundsOp, TransactionStage, }; use proto::funder::signature_buff::create_response_signature_buffer; @@ -31,7 +31,7 @@ pub enum QueueOperationError { InvalidResponseSignature, RemoteRequestsClosed, NotExpectingResponse, - NotExpectingCommit, + NotExpectingCollect, InvalidSrcPlainLock, InvalidDestPlainLock, } @@ -64,8 +64,8 @@ impl OutgoingMc { FriendTcOp::CancelSendFunds(cancel_send_funds) => { self.queue_cancel_send_funds(cancel_send_funds) } - FriendTcOp::CommitSendFunds(commit_send_funds) => { - self.queue_commit_send_funds(commit_send_funds) + FriendTcOp::CollectSendFunds(collect_send_funds) => { + self.queue_collect_send_funds(collect_send_funds) } } } @@ -267,9 +267,9 @@ impl OutgoingMc { Ok(mc_mutations) } - fn queue_commit_send_funds( + fn queue_collect_send_funds( &mut self, - commit_send_funds: CommitSendFundsOp, + collect_send_funds: CollectSendFundsOp, ) -> Result, QueueOperationError> { // Make sure that id exists in remote_pending hashmap, // and access saved request details. @@ -277,22 +277,22 @@ impl OutgoingMc { // Obtain pending request: let pending_transaction = remote_pending_transactions - .get(&commit_send_funds.request_id) + .get(&collect_send_funds.request_id) .ok_or(QueueOperationError::RequestDoesNotExist)? .clone(); // TODO: Possibly get rid of clone() here for optimization later let dest_hashed_lock = match &pending_transaction.stage { TransactionStage::Response(dest_hashed_lock) => dest_hashed_lock, - _ => return Err(QueueOperationError::NotExpectingCommit), + _ => return Err(QueueOperationError::NotExpectingCollect), }; // Verify src_plain_lock and dest_plain_lock: - if commit_send_funds.src_plain_lock.hash() != pending_transaction.src_hashed_lock { + if collect_send_funds.src_plain_lock.hash() != pending_transaction.src_hashed_lock { return Err(QueueOperationError::InvalidSrcPlainLock); } - if commit_send_funds.dest_plain_lock.hash() != *dest_hashed_lock { + if collect_send_funds.dest_plain_lock.hash() != *dest_hashed_lock { return Err(QueueOperationError::InvalidDestPlainLock); } @@ -304,7 +304,7 @@ impl OutgoingMc { // Remove entry from remote_pending hashmap: let mut mc_mutations = Vec::new(); - let mc_mutation = McMutation::RemoveRemotePendingTransaction(commit_send_funds.request_id); + let mc_mutation = McMutation::RemoveRemotePendingTransaction(collect_send_funds.request_id); self.mutual_credit.mutate(&mc_mutation); mc_mutations.push(mc_mutation); diff --git a/components/funder/src/mutual_credit/tests.rs b/components/funder/src/mutual_credit/tests.rs index 4f588dc4a..9cd097b1a 100644 --- a/components/funder/src/mutual_credit/tests.rs +++ b/components/funder/src/mutual_credit/tests.rs @@ -10,7 +10,7 @@ use crypto::crypto_rand::{RandValue, RAND_VALUE_LEN}; use crypto::invoice_id::{InvoiceId, INVOICE_ID_LEN}; use proto::funder::messages::{ - CancelSendFundsOp, CommitSendFundsOp, FriendTcOp, FriendsRoute, RequestSendFundsOp, + CancelSendFundsOp, CollectSendFundsOp, FriendTcOp, FriendsRoute, RequestSendFundsOp, RequestsStatus, ResponseSendFundsOp, }; use proto::funder::signature_buff::create_response_signature_buffer; @@ -95,7 +95,7 @@ fn test_outgoing_set_remote_max_debt() { } #[test] -fn test_request_response_commit_send_funds() { +fn test_request_response_collect_send_funds() { let local_public_key = PublicKey::from(&[0xaa; PUBLIC_KEY_LEN]); let remote_public_key = PublicKey::from(&[0xbb; PUBLIC_KEY_LEN]); let balance = 0; @@ -179,9 +179,9 @@ fn test_request_response_commit_send_funds() { assert_eq!(mutual_credit.state().balance.local_pending_debt, 10 + 5); assert_eq!(mutual_credit.state().balance.remote_pending_debt, 0); - // -----[CommitSendFunds]-------- + // -----[CollectSendFunds]-------- // -------------------------------- - let commit_send_funds = CommitSendFundsOp { + let collect_send_funds = CollectSendFundsOp { request_id, src_plain_lock, dest_plain_lock, @@ -189,7 +189,7 @@ fn test_request_response_commit_send_funds() { apply_incoming( &mut mutual_credit, - FriendTcOp::CommitSendFunds(commit_send_funds), + FriendTcOp::CollectSendFunds(collect_send_funds), ) .unwrap(); diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index 35e929a67..123374140 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -27,7 +27,7 @@ pub struct FunderState { /// Locally created transaction in progress. (This node is the buyer). // TODO: Add this part in the report? At least a counter of this hash set? pub open_transactions: ImHashMap, - /// Receipts of completed payments (Generated after a CommitSendFundsOp message was received + /// Receipts of completed payments (Generated after a CollectSendFundsOp message was received /// successfuly). /// Note: We use our own randomly generated paymentId to represent a payment and not an /// InvoiceId. We do this to defend against possible collisions of InvoiceId values (Which diff --git a/components/funder/src/types.rs b/components/funder/src/types.rs index 473e3404e..d73063a7f 100644 --- a/components/funder/src/types.rs +++ b/components/funder/src/types.rs @@ -9,7 +9,7 @@ use proto::app_server::messages::RelayAddress; use proto::funder::messages::{ ChannelerUpdateFriend, CancelSendFundsOp, FriendMessage, FriendTcOp, FunderIncomingControl, FunderOutgoingControl, MoveToken, PendingTransaction, RequestSendFundsOp, ResponseSendFundsOp, - CommitSendFundsOp, TransactionStage, + CollectSendFundsOp, TransactionStage, }; use proto::funder::signature_buff::{ @@ -104,7 +104,7 @@ pub enum UnsignedFriendTcOp { ResponseSendFunds(ResponseSendFundsOp), UnsignedResponseSendFunds(UnsignedResponseSendFundsOp), CancelSendFunds(CancelSendFundsOp), - CommitSendFunds(CommitSendFundsOp), + CollectSendFunds(CollectSendFundsOp), } diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 3ff1c68f5..2c949db4f 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -99,7 +99,7 @@ pub struct MultiCommit { } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] -pub struct CommitSendFundsOp { +pub struct CollectSendFundsOp { pub request_id: Uid, pub src_plain_lock: PlainLock, pub dest_plain_lock: PlainLock, @@ -113,7 +113,7 @@ pub enum FriendTcOp { RequestSendFunds(RequestSendFundsOp), ResponseSendFunds(ResponseSendFundsOp), CancelSendFunds(CancelSendFundsOp), - CommitSendFunds(CommitSendFundsOp), + CollectSendFunds(CollectSendFundsOp), } #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] @@ -228,7 +228,7 @@ impl CanonicalSerialize for CancelSendFundsOp { } } -impl CanonicalSerialize for CommitSendFundsOp { +impl CanonicalSerialize for CollectSendFundsOp { fn canonical_serialize(&self) -> Vec { let mut res_bytes = Vec::new(); res_bytes.extend_from_slice(&self.request_id); @@ -264,7 +264,7 @@ impl CanonicalSerialize for FriendTcOp { res_bytes.push(5u8); res_bytes.append(&mut cancel_send_funds.canonical_serialize()) } - FriendTcOp::CommitSendFunds(commit_send_funds) => { + FriendTcOp::CollectSendFunds(commit_send_funds) => { res_bytes.push(6u8); res_bytes.append(&mut commit_send_funds.canonical_serialize()) } From 0856ec9f535fbf0c781bf9f2adbd42b594ea115f Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 14:00:06 +0300 Subject: [PATCH 078/248] docs: Updated arguments of CreateTransaction and AddInvoice. --- doc/docs/atomic_proposal.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/docs/atomic_proposal.md b/doc/docs/atomic_proposal.md index 0b7063cc4..52d09c798 100644 --- a/doc/docs/atomic_proposal.md +++ b/doc/docs/atomic_proposal.md @@ -683,7 +683,8 @@ CreateTransaction [App -> Node] ================= - paymentId - requestId -- route + capacity + fees +- route +- fees - destPayment TransactionResult [Node -> App] @@ -736,6 +737,7 @@ After a while: AddInvoice [App -> Node] =========== - invoiceId +- totalDestPayment CancelInvoice [App -> Node] From 94497b55ae7e5a47615e1ac9ea33dd0fd05d8fb3 Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 14:00:43 +0300 Subject: [PATCH 079/248] Added CreatePayment, CreateTransaction, AddInvoice structs. --- components/proto/src/funder/messages.rs | 40 +++++++++++++++++++++---- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 2c949db4f..a04d7bdfc 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -451,8 +451,6 @@ struct FriendsRouteCapacity { /// A request to send funds that originates from the user #[derive(Debug, Clone, PartialEq, Eq)] pub struct UserRequestSendFunds { - /// payment_id is a randomly generated value (by the user), allowing the user to refer to a - /// certain payment. pub payment_id: PaymentId, pub route: FriendsRoute, pub invoice_id: InvoiceId, @@ -465,6 +463,39 @@ pub struct ReceiptAck { pub receipt_signature: Signature, } +/// Start a payment, possibly by paying through multiple routes. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct CreatePayment { + /// payment_id is a randomly generated value (by the user), allowing the user to refer to a + /// certain payment. + pub payment_id: PaymentId, + pub invoice_id: InvoiceId, + pub total_dest_payment: u128, + pub dest_public_key: PublicKey, +} + +/// Start a payment, possibly by paying through multiple routes. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct CreateTransaction { + /// A payment id of an existing payment. + pub payment_id: PaymentId, + /// Randomly generated request_id (by the user), + /// allows the user to refer to this request later. + pub request_id: Uid, + pub route: FriendsRoute, + pub dest_payment: u128, + pub fees: u128, +} + +/// Start an invoice (A request for payment). +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct AddInvoice { + /// Randomly generated invoice_id, allows to refer to this invoice. + invoice_id: InvoiceId, + /// Total amount of credits to be paid. + total_dest_payment: u128, +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum FunderControl { AddRelay(NamedRelayAddress), @@ -478,14 +509,13 @@ pub enum FunderControl { SetFriendName(SetFriendName), ResetFriendChannel(ResetFriendChannel), // Buyer API: - RequestPay(()), - // SendFunds(()) // TODO: Extra + CreatePayment(CreatePayment), + CreateTransaction(CreateTransaction), // TODO RequestReceipt(PaymentId), RemoveReceipt(PaymentId), // Seller API: AddInvoice(InvoiceId), CancelInvoice(InvoiceId), - // TODO: Possibly find a better name for this? CommitInvoice(InvoiceId), } From 1418907fb4ac0af71b7b2d7b33c2f5b907a635ba Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 14:08:42 +0300 Subject: [PATCH 080/248] Added total_dest_payment to RequestSendFundsOp. --- components/funder/src/mutual_credit/incoming.rs | 5 +++++ components/funder/src/mutual_credit/outgoing.rs | 5 +++++ components/funder/src/mutual_credit/tests.rs | 3 +++ components/funder/src/types.rs | 1 + components/proto/src/funder/messages.rs | 3 +++ components/proto/src/funder/signature_buff.rs | 5 +++++ 6 files changed, 22 insertions(+) diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index d11a697e4..47f05a3e6 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -66,6 +66,7 @@ pub enum ProcessOperationError { InvalidSrcPlainLock, InvalidDestPlainLock, NotExpectingCollect, + DestPaymentExceedsTotal, } #[derive(Debug)] @@ -187,6 +188,10 @@ fn process_request_send_funds( return Err(ProcessOperationError::InvalidRoute); } + if request_send_funds.dest_payment > request_send_funds.total_dest_payment { + return Err(ProcessOperationError::DestPaymentExceedsTotal); + } + // Find ourselves (And remote side) on the route. If we are not there, abort. let remote_index = request_send_funds .route diff --git a/components/funder/src/mutual_credit/outgoing.rs b/components/funder/src/mutual_credit/outgoing.rs index 4e35ad022..453eae2aa 100644 --- a/components/funder/src/mutual_credit/outgoing.rs +++ b/components/funder/src/mutual_credit/outgoing.rs @@ -34,6 +34,7 @@ pub enum QueueOperationError { NotExpectingCollect, InvalidSrcPlainLock, InvalidDestPlainLock, + DestPaymentExceedsTotal, } /// A wrapper over a token channel, accumulating funds to be sent as one transaction. @@ -112,6 +113,10 @@ impl OutgoingMc { return Err(QueueOperationError::InvalidRoute); } + if request_send_funds.dest_payment > request_send_funds.total_dest_payment { + return Err(QueueOperationError::DestPaymentExceedsTotal); + } + // Find ourselves on the route. If we are not there, abort. let local_index = request_send_funds .route diff --git a/components/funder/src/mutual_credit/tests.rs b/components/funder/src/mutual_credit/tests.rs index 9cd097b1a..c2c60baea 100644 --- a/components/funder/src/mutual_credit/tests.rs +++ b/components/funder/src/mutual_credit/tests.rs @@ -134,6 +134,7 @@ fn test_request_response_collect_send_funds() { src_hashed_lock: src_plain_lock.hash(), route, dest_payment: 10, + total_dest_payment: 10, invoice_id, left_fees: 5, }; @@ -241,6 +242,7 @@ fn test_request_cancel_send_funds() { src_hashed_lock: src_plain_lock.hash(), route, dest_payment: 10, + total_dest_payment: 10, invoice_id, left_fees: 5, }; @@ -315,6 +317,7 @@ fn test_request_response_cancel_send_funds() { src_hashed_lock: src_plain_lock.hash(), route, dest_payment: 10, + total_dest_payment: 10, invoice_id, left_fees: 5, }; diff --git a/components/funder/src/types.rs b/components/funder/src/types.rs index d73063a7f..f61f31def 100644 --- a/components/funder/src/types.rs +++ b/components/funder/src/types.rs @@ -88,6 +88,7 @@ pub fn create_pending_transaction(request_send_funds: &RequestSendFundsOp) -> Pe request_id: request_send_funds.request_id, route: request_send_funds.route.clone(), dest_payment: request_send_funds.dest_payment, + total_dest_payment: request_send_funds.total_dest_payment, invoice_id: request_send_funds.invoice_id.clone(), left_fees: request_send_funds.left_fees, src_hashed_lock: request_send_funds.src_hashed_lock.clone(), diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index a04d7bdfc..5de197846 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -67,6 +67,7 @@ pub struct RequestSendFundsOp { pub src_hashed_lock: HashedLock, pub route: FriendsRoute, pub dest_payment: u128, + pub total_dest_payment: u128, pub invoice_id: InvoiceId, pub left_fees: u128, } @@ -163,6 +164,7 @@ pub struct Receipt { pub src_plain_lock: PlainLock, pub dest_plain_lock: PlainLock, pub dest_payment: u128, + pub total_dest_payment: u128, pub signature: Signature, // Signature{key=recipientKey}( // "FUND_SUCCESS" || @@ -183,6 +185,7 @@ pub struct PendingTransaction { pub request_id: Uid, pub route: FriendsRoute, pub dest_payment: u128, + pub total_dest_payment: u128, pub invoice_id: InvoiceId, pub left_fees: u128, pub src_hashed_lock: HashedLock, diff --git a/components/proto/src/funder/signature_buff.rs b/components/proto/src/funder/signature_buff.rs index 66ddd851c..056e56c48 100644 --- a/components/proto/src/funder/signature_buff.rs +++ b/components/proto/src/funder/signature_buff.rs @@ -32,6 +32,9 @@ pub fn create_response_signature_buffer( sbuffer .write_u128::(pending_transaction.dest_payment) .unwrap(); + sbuffer + .write_u128::(pending_transaction.total_dest_payment) + .unwrap(); sbuffer.extend_from_slice(&pending_transaction.invoice_id); sbuffer @@ -67,6 +70,8 @@ pub fn verify_receipt(receipt: &Receipt, public_key: &PublicKey) -> bool { data.extend_from_slice(&receipt.src_plain_lock.hash()); data.extend_from_slice(&receipt.dest_plain_lock.hash()); data.write_u128::(receipt.dest_payment).unwrap(); + data.write_u128::(receipt.total_dest_payment) + .unwrap(); data.extend(receipt.invoice_id.as_ref()); verify_signature(&data, public_key, &receipt.signature) } From 8e17542b58ec9699f27b592ef736cdab3a262838 Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 16:13:54 +0300 Subject: [PATCH 081/248] Restored handle_control.rs. Compiling but incorrect. --- .../funder/src/handler/handle_control.rs | 20 ++++++++++--------- components/funder/src/handler/handler.rs | 1 + components/funder/src/handler/mod.rs | 5 +++-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index f7ea62a21..5138bfb0f 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -429,6 +429,7 @@ fn check_user_request_valid(user_request_send_funds: &UserRequestSendFunds) -> O Some(()) } +/* fn control_request_send_funds_inner( m_state: &mut MutableFunderState, ephemeral: &Ephemeral, @@ -583,6 +584,7 @@ where Ok(()) } +*/ pub fn handle_control_message( m_state: &mut MutableFunderState, @@ -657,15 +659,15 @@ where control_set_friend_name(m_state, set_friend_name) } - FunderControl::RequestSendFunds(user_request_send_funds) => control_request_send_funds( - m_state, - m_ephemeral.ephemeral(), - outgoing_control, - send_commands, - max_pending_user_requests, - user_request_send_funds, - ), + // Buyer API: + FunderControl::CreatePayment(_create_payment) => unimplemented!(), + FunderControl::CreateTransaction(_create_transaction) => unimplemented!(), + FunderControl::RequestReceipt(_payment_id) => unimplemented!(), + FunderControl::RemoveReceipt(_payment_id) => unimplemented!(), - FunderControl::ReceiptAck(receipt_ack) => control_receipt_ack(m_state, receipt_ack), + // Seller API: + FunderControl::AddInvoice(_invoice_id) => unimplemented!(), + FunderControl::CancelInvoice(invoice_id) => unimplemented!(), + FunderControl::CommitInvoice(_multi_commit) => unimplemented!(), } } diff --git a/components/funder/src/handler/handler.rs b/components/funder/src/handler/handler.rs index 40f476650..fb78d20a5 100644 --- a/components/funder/src/handler/handler.rs +++ b/components/funder/src/handler/handler.rs @@ -19,6 +19,7 @@ use crate::handler::handle_friend::{handle_friend_message, HandleFriendError}; use crate::handler::handle_init::handle_init; use crate::handler::handle_liveness::{handle_liveness_message, HandleLivenessError}; use crate::handler::sender::{create_friend_messages, SendCommands}; +use crate::handler::state_wrap::{MutableFunderState, MutableEphemeral}; use crate::ephemeral::{Ephemeral, EphemeralMutation}; use crate::friend::ChannelStatus; diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index d8720bc4b..4e128d89b 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -1,13 +1,14 @@ #[allow(unused)] mod canceler; -// #[allow(unused)] -// mod handle_control; +#[allow(unused)] +mod handle_control; #[allow(unused)] mod handle_friend; #[allow(unused)] mod handle_init; #[allow(unused)] mod handle_liveness; +// #[allow(unused)] // mod handler; #[allow(unused)] mod sender; From 516a90036b5045360b4adb830afef068a94532c8 Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 16:15:46 +0300 Subject: [PATCH 082/248] Restored handler.rs. Compiles but currently incorrect. --- components/funder/src/handler/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index 4e128d89b..cf5c8a8ea 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -8,8 +8,8 @@ mod handle_friend; mod handle_init; #[allow(unused)] mod handle_liveness; -// #[allow(unused)] -// mod handler; +#[allow(unused)] +mod handler; #[allow(unused)] mod sender; #[allow(unused)] @@ -20,4 +20,4 @@ mod utils; // #[cfg(test)] // mod tests; -// pub use self::handler::{funder_handle_message, FunderHandlerError}; +pub use self::handler::{funder_handle_message, FunderHandlerError}; From a486f4e69caaea2059da393e1d7cf41058a6e684 Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 16:17:58 +0300 Subject: [PATCH 083/248] Moved TODO comment. --- components/funder/src/funder.rs | 3 --- components/funder/src/handler/handler.rs | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/components/funder/src/funder.rs b/components/funder/src/funder.rs index dfaaac3be..864d7c900 100644 --- a/components/funder/src/funder.rs +++ b/components/funder/src/funder.rs @@ -109,9 +109,6 @@ where } }; - // TODO: Sign all unsigned responses and then queue them as mutations - assert!(false); - if !handler_output.funder_mutations.is_empty() { // Mutate our funder_state in memory: for mutation in &handler_output.funder_mutations { diff --git a/components/funder/src/handler/handler.rs b/components/funder/src/handler/handler.rs index fb78d20a5..05b205a6f 100644 --- a/components/funder/src/handler/handler.rs +++ b/components/funder/src/handler/handler.rs @@ -190,6 +190,9 @@ where outgoing_comms.push(FunderOutgoingComm::ChannelerConfig(channeler_config)); } + // TODO: Sign all unsigned responses and then queue them as mutations + assert!(false); + // Send all possible messages according to SendCommands // TODO: Maybe we should output outgoing_comms instead of friend_messages and // outgoing_channeler_config. When we merge the two, we might be out of order! From e95a16d02808e9d8ba723441ed317897f31a5fc0 Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 17:14:42 +0300 Subject: [PATCH 084/248] Some work on MutableFunderState.sign_responses(). Not done yet. --- .../funder/src/handler/handle_friend.rs | 23 +++++-- components/funder/src/handler/handler.rs | 9 ++- components/funder/src/handler/state_wrap.rs | 69 +++++++++++++++++-- components/funder/src/state.rs | 4 +- 4 files changed, 90 insertions(+), 15 deletions(-) diff --git a/components/funder/src/handler/handle_friend.rs b/components/funder/src/handler/handle_friend.rs index b1f05c5c7..40f5deace 100644 --- a/components/funder/src/handler/handle_friend.rs +++ b/components/funder/src/handler/handle_friend.rs @@ -2,6 +2,7 @@ use common::canonical_serialize::CanonicalSerialize; use std::fmt::Debug; use crypto::crypto_rand::CryptoRandom; +use crypto::hash_lock::PlainLock; use crypto::identity::{PublicKey, Signature, SIGNATURE_LEN}; use proto::app_server::messages::RelayAddress; @@ -139,13 +140,15 @@ fn forward_request( send_commands.set_try_send(&next_pk); } -fn handle_request_send_funds( +fn handle_request_send_funds( m_state: &mut MutableFunderState, ephemeral: &Ephemeral, send_commands: &mut SendCommands, remote_public_key: &PublicKey, request_send_funds: RequestSendFundsOp, + rng: &R, ) where + R: CryptoRandom, B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, { // Find ourselves on the route. If we are not there, abort. @@ -159,7 +162,12 @@ fn handle_request_send_funds( if next_index >= request_send_funds.route.len() { // We are the destination of this request. We return a response: let pending_transaction = create_pending_transaction(&request_send_funds); - m_state.queue_unsigned_response(pending_transaction); + let dest_plain_lock = PlainLock::new(rng); + m_state.queue_unsigned_response( + remote_public_key.clone(), + pending_transaction, + dest_plain_lock, + ); /* let u_response_op = BackwardsOp::UnsignedResponse(pending_transaction); let friend_mutation = FriendMutation::PushBackPendingResponse(u_response_op); @@ -286,14 +294,16 @@ fn handle_collect_send_funds( } /// Process valid incoming operations from remote side. -fn handle_move_token_output( +fn handle_move_token_output( m_state: &mut MutableFunderState, m_ephemeral: &mut MutableEphemeral, send_commands: &mut SendCommands, outgoing_control: &mut Vec>, remote_public_key: &PublicKey, incoming_messages: Vec, + rng: &R, ) where + R: CryptoRandom, B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, { for incoming_message in incoming_messages { @@ -305,6 +315,7 @@ fn handle_move_token_output( send_commands, remote_public_key, request_send_funds, + rng, ); } IncomingMessage::Response(IncomingResponseSendFundsOp { @@ -387,7 +398,7 @@ fn handle_move_token_error( } /// Handle success with incoming move token. -fn handle_move_token_success( +fn handle_move_token_success( m_state: &mut MutableFunderState, m_ephemeral: &mut MutableEphemeral, send_commands: &mut SendCommands, @@ -396,7 +407,9 @@ fn handle_move_token_success( remote_public_key: &PublicKey, receive_move_token_output: ReceiveMoveTokenOutput, token_wanted: bool, + rng: &R, ) where + R: CryptoRandom, B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, { match receive_move_token_output { @@ -497,6 +510,7 @@ fn handle_move_token_success( outgoing_control, remote_public_key, incoming_messages, + rng, ); } } @@ -556,6 +570,7 @@ where remote_public_key, receive_move_token_output, token_wanted, + rng, ); } Err(_receive_move_token_error) => { diff --git a/components/funder/src/handler/handler.rs b/components/funder/src/handler/handler.rs index 05b205a6f..ccae2e7c7 100644 --- a/components/funder/src/handler/handler.rs +++ b/components/funder/src/handler/handler.rs @@ -191,7 +191,8 @@ where } // TODO: Sign all unsigned responses and then queue them as mutations - assert!(false); + + await!(m_state.sign_responses(identity_client, rng)); // Send all possible messages according to SendCommands // TODO: Maybe we should output outgoing_comms instead of friend_messages and @@ -214,9 +215,11 @@ where outgoing_comms.push(FunderOutgoingComm::FriendMessage(friend_message)); } - // Add reports: - let (initial_state, funder_mutations, _state) = m_state.done(); + // Sign all unsigned responses (Async): + let (initial_state, funder_mutations, state) = m_state.done(); let (ephemeral_mutations, _ephemeral) = m_ephemeral.done(); + + // Add reports: let report_mutations = create_report_mutations( initial_state, &funder_mutations[..], diff --git a/components/funder/src/handler/state_wrap.rs b/components/funder/src/handler/state_wrap.rs index 7e81bad69..d80473fe4 100644 --- a/components/funder/src/handler/state_wrap.rs +++ b/components/funder/src/handler/state_wrap.rs @@ -2,7 +2,8 @@ use std::fmt::Debug; use common::canonical_serialize::CanonicalSerialize; -use crypto::crypto_rand::CryptoRandom; +use crypto::crypto_rand::{CryptoRandom, RandValue}; +use crypto::hash_lock::PlainLock; use crypto::identity::PublicKey; use crypto::uid::Uid; @@ -18,14 +19,21 @@ use crate::ephemeral::{Ephemeral, EphemeralMutation}; use crate::friend::ChannelStatus; use crate::report::{ephemeral_mutation_to_report_mutations, funder_mutation_to_report_mutations}; use crate::types::{ - ChannelerConfig, FunderIncoming, FunderIncomingComm, FunderOutgoingComm, - UnsignedResponseSendFundsOp, + create_response_send_funds, ChannelerConfig, FunderIncoming, FunderIncomingComm, + FunderOutgoingComm, UnsignedResponseSendFundsOp, }; +#[derive(Debug, Clone)] +pub struct SemiResponse { + friend_public_key: PublicKey, + pending_transaction: PendingTransaction, + dest_plain_lock: PlainLock, +} + pub struct MutableFunderState { initial_state: FunderState, state: FunderState, - unsigned_responses: Vec, + unsigned_responses: Vec, mutations: Vec>, } @@ -45,8 +53,17 @@ where /// Push an unsigned response operation. /// We have a separate queue for these operations because we need an async function call to /// sign an unsigned response. - pub fn queue_unsigned_response(&mut self, pending_transaction: PendingTransaction) { - self.unsigned_responses.push(pending_transaction); + pub fn queue_unsigned_response( + &mut self, + friend_public_key: PublicKey, + pending_transaction: PendingTransaction, + dest_plain_lock: PlainLock, + ) { + self.unsigned_responses.push(SemiResponse { + friend_public_key, + pending_transaction, + dest_plain_lock, + }); } pub fn mutate(&mut self, mutation: FunderMutation) { @@ -58,11 +75,51 @@ where &self.state } + /// Sign all unsigned responses + pub async fn sign_responses<'a, R>( + &'a mut self, + identity_client: &'a mut IdentityClient, + rng: &'a R, + ) where + R: CryptoRandom, + { + while let Some(semi_response) = self.unsigned_responses.pop() { + let SemiResponse { + friend_public_key, + pending_transaction, + dest_plain_lock, + } = semi_response; + + let rand_nonce = RandValue::new(rng); + let response_send_funds = await!(create_response_send_funds( + &pending_transaction, + dest_plain_lock.hash(), + rand_nonce, + identity_client, + )); + + // TODO: + // - Apply mutation to keep dest_plain_lock + // - Apply Mutation to push the new response + unimplemented!(); + } + } + pub fn done(self) -> (FunderState, Vec>, FunderState) { + // TODO: Find out how to change this into compile time guarantee: + assert!(self.unsigned_responses.is_empty()); (self.initial_state, self.mutations, self.state) } } +pub struct MutableFunderStateSigned { + initial_state: FunderState, + state: FunderState, + mutations: Vec>, +} + +impl MutableFunderStateSigned where B: Clone + CanonicalSerialize + PartialEq + Eq + Debug {} + pub struct MutableEphemeral { ephemeral: Ephemeral, mutations: Vec, diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index 123374140..948f6f154 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -41,14 +41,14 @@ pub struct OpenInvoice { /// Destination plain locks for all requests related to a single open invoice that was /// originated for this node. /// Multiple requests are possible for a single invoice in case of a multi-route payment. - dest_plain_locks: ImHashMap, + pub dest_plain_locks: ImHashMap, } /// A local request (Originated from this node) in progress #[derive(Clone, Serialize, Deserialize, Debug)] pub struct OpenTransaction { /// The plain part of a hash lock for the generated transaction. - src_plain_lock: PlainLock, + pub src_plain_lock: PlainLock, } #[allow(clippy::large_enum_variant)] From a4d71d002671f226b7c55c7d135646b743f88c69 Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 17:24:30 +0300 Subject: [PATCH 085/248] Some work on sign_responses(). Not done yet. --- .../funder/src/handler/handle_friend.rs | 23 ++++--------------- components/funder/src/handler/handler.rs | 1 - components/funder/src/handler/sender.rs | 2 +- components/funder/src/handler/state_wrap.rs | 21 ++++++++++------- 4 files changed, 18 insertions(+), 29 deletions(-) diff --git a/components/funder/src/handler/handle_friend.rs b/components/funder/src/handler/handle_friend.rs index 40f5deace..186a5cb35 100644 --- a/components/funder/src/handler/handle_friend.rs +++ b/components/funder/src/handler/handle_friend.rs @@ -2,7 +2,6 @@ use common::canonical_serialize::CanonicalSerialize; use std::fmt::Debug; use crypto::crypto_rand::CryptoRandom; -use crypto::hash_lock::PlainLock; use crypto::identity::{PublicKey, Signature, SIGNATURE_LEN}; use proto::app_server::messages::RelayAddress; @@ -140,15 +139,13 @@ fn forward_request( send_commands.set_try_send(&next_pk); } -fn handle_request_send_funds( +fn handle_request_send_funds( m_state: &mut MutableFunderState, ephemeral: &Ephemeral, send_commands: &mut SendCommands, remote_public_key: &PublicKey, request_send_funds: RequestSendFundsOp, - rng: &R, ) where - R: CryptoRandom, B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, { // Find ourselves on the route. If we are not there, abort. @@ -162,12 +159,7 @@ fn handle_request_send_funds( if next_index >= request_send_funds.route.len() { // We are the destination of this request. We return a response: let pending_transaction = create_pending_transaction(&request_send_funds); - let dest_plain_lock = PlainLock::new(rng); - m_state.queue_unsigned_response( - remote_public_key.clone(), - pending_transaction, - dest_plain_lock, - ); + m_state.queue_unsigned_response(remote_public_key.clone(), pending_transaction); /* let u_response_op = BackwardsOp::UnsignedResponse(pending_transaction); let friend_mutation = FriendMutation::PushBackPendingResponse(u_response_op); @@ -294,16 +286,14 @@ fn handle_collect_send_funds( } /// Process valid incoming operations from remote side. -fn handle_move_token_output( +fn handle_move_token_output( m_state: &mut MutableFunderState, m_ephemeral: &mut MutableEphemeral, send_commands: &mut SendCommands, outgoing_control: &mut Vec>, remote_public_key: &PublicKey, incoming_messages: Vec, - rng: &R, ) where - R: CryptoRandom, B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, { for incoming_message in incoming_messages { @@ -315,7 +305,6 @@ fn handle_move_token_output( send_commands, remote_public_key, request_send_funds, - rng, ); } IncomingMessage::Response(IncomingResponseSendFundsOp { @@ -398,7 +387,7 @@ fn handle_move_token_error( } /// Handle success with incoming move token. -fn handle_move_token_success( +fn handle_move_token_success( m_state: &mut MutableFunderState, m_ephemeral: &mut MutableEphemeral, send_commands: &mut SendCommands, @@ -407,9 +396,7 @@ fn handle_move_token_success( remote_public_key: &PublicKey, receive_move_token_output: ReceiveMoveTokenOutput, token_wanted: bool, - rng: &R, ) where - R: CryptoRandom, B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, { match receive_move_token_output { @@ -510,7 +497,6 @@ fn handle_move_token_success( outgoing_control, remote_public_key, incoming_messages, - rng, ); } } @@ -570,7 +556,6 @@ where remote_public_key, receive_move_token_output, token_wanted, - rng, ); } Err(_receive_move_token_error) => { diff --git a/components/funder/src/handler/handler.rs b/components/funder/src/handler/handler.rs index ccae2e7c7..493dc20de 100644 --- a/components/funder/src/handler/handler.rs +++ b/components/funder/src/handler/handler.rs @@ -191,7 +191,6 @@ where } // TODO: Sign all unsigned responses and then queue them as mutations - await!(m_state.sign_responses(identity_client, rng)); // Send all possible messages according to SendCommands diff --git a/components/funder/src/handler/sender.rs b/components/funder/src/handler/sender.rs index 3f9f52a9e..24a0c8501 100644 --- a/components/funder/src/handler/sender.rs +++ b/components/funder/src/handler/sender.rs @@ -17,7 +17,7 @@ use identity::IdentityClient; use crate::mutual_credit::outgoing::{OutgoingMc, QueueOperationError}; use crate::types::{ - create_pending_transaction, create_response_send_funds, + create_pending_transaction, create_unsigned_move_token, sign_move_token, ChannelerConfig, create_cancel_send_funds, }; diff --git a/components/funder/src/handler/state_wrap.rs b/components/funder/src/handler/state_wrap.rs index d80473fe4..ad634968f 100644 --- a/components/funder/src/handler/state_wrap.rs +++ b/components/funder/src/handler/state_wrap.rs @@ -16,7 +16,7 @@ use identity::IdentityClient; use crate::state::{FunderMutation, FunderState}; use crate::ephemeral::{Ephemeral, EphemeralMutation}; -use crate::friend::ChannelStatus; +use crate::friend::{BackwardsOp, ChannelStatus, FriendMutation}; use crate::report::{ephemeral_mutation_to_report_mutations, funder_mutation_to_report_mutations}; use crate::types::{ create_response_send_funds, ChannelerConfig, FunderIncoming, FunderIncomingComm, @@ -27,7 +27,6 @@ use crate::types::{ pub struct SemiResponse { friend_public_key: PublicKey, pending_transaction: PendingTransaction, - dest_plain_lock: PlainLock, } pub struct MutableFunderState { @@ -57,12 +56,10 @@ where &mut self, friend_public_key: PublicKey, pending_transaction: PendingTransaction, - dest_plain_lock: PlainLock, ) { self.unsigned_responses.push(SemiResponse { friend_public_key, pending_transaction, - dest_plain_lock, }); } @@ -75,7 +72,7 @@ where &self.state } - /// Sign all unsigned responses + /// Sign all unsigned responses and apply them as mutations pub async fn sign_responses<'a, R>( &'a mut self, identity_client: &'a mut IdentityClient, @@ -87,9 +84,12 @@ where let SemiResponse { friend_public_key, pending_transaction, - dest_plain_lock, } = semi_response; + // Randomly generate a dest plain lock: + let dest_plain_lock = PlainLock::new(rng); + + // Mutation to push the new response: let rand_nonce = RandValue::new(rng); let response_send_funds = await!(create_response_send_funds( &pending_transaction, @@ -98,9 +98,14 @@ where identity_client, )); + let backwards_op = BackwardsOp::Response(response_send_funds); + let friend_mutation = FriendMutation::PushBackPendingBackwardsOp(backwards_op); + let funder_mutation = + FunderMutation::FriendMutation((friend_public_key.clone(), friend_mutation)); + self.mutate(funder_mutation); + + // Mutation to keep dest_plain_lock // TODO: - // - Apply mutation to keep dest_plain_lock - // - Apply Mutation to push the new response unimplemented!(); } } From daf8e2b210c625ca69644e995c5d5e9608a50648 Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 17:53:41 +0300 Subject: [PATCH 086/248] Initial implementation of sign_responses(). --- components/funder/src/handler/state_wrap.rs | 10 +++++--- components/funder/src/report.rs | 13 ++++++---- components/funder/src/state.rs | 27 +++++++++++++++++++++ components/proto/src/report/convert.rs | 4 ++- components/proto/src/report/messages.rs | 12 +++++++++ components/proto/src/report/serialize.rs | 6 +++++ 6 files changed, 63 insertions(+), 9 deletions(-) diff --git a/components/funder/src/handler/state_wrap.rs b/components/funder/src/handler/state_wrap.rs index ad634968f..8c4fd0937 100644 --- a/components/funder/src/handler/state_wrap.rs +++ b/components/funder/src/handler/state_wrap.rs @@ -104,9 +104,13 @@ where FunderMutation::FriendMutation((friend_public_key.clone(), friend_mutation)); self.mutate(funder_mutation); - // Mutation to keep dest_plain_lock - // TODO: - unimplemented!(); + // Mutation to add the destination plain lock: + let funder_mutation = FunderMutation::AddDestPlainLock(( + pending_transaction.invoice_id, + pending_transaction.request_id, + dest_plain_lock, + )); + self.mutate(funder_mutation); } } diff --git a/components/funder/src/report.rs b/components/funder/src/report.rs index 685cbba2c..1ac0ace6c 100644 --- a/components/funder/src/report.rs +++ b/components/funder/src/report.rs @@ -174,6 +174,8 @@ where local_public_key: funder_state.local_public_key.clone(), relays: funder_state.relays.clone(), friends, + num_open_invoices: usize_to_u64(funder_state.open_invoices.len()).unwrap(), + num_open_transactions: usize_to_u64(funder_state.open_transactions.len()).unwrap(), num_ready_receipts: usize_to_u64(funder_state.ready_receipts.len()).unwrap(), } } @@ -336,7 +338,7 @@ where friend_public_key.clone(), )] } - FunderMutation::AddReceipt((_uid, _receipt)) => { + FunderMutation::AddReceipt(_) | FunderMutation::RemoveReceipt(_) => { if funder_state_after.ready_receipts.len() != funder_state.ready_receipts.len() { vec![FunderReportMutation::SetNumReadyReceipts( usize_to_u64(funder_state_after.ready_receipts.len()).unwrap(), @@ -345,15 +347,16 @@ where Vec::new() } } - FunderMutation::RemoveReceipt(_uid) => { - if funder_state_after.ready_receipts.len() != funder_state.ready_receipts.len() { - vec![FunderReportMutation::SetNumReadyReceipts( - usize_to_u64(funder_state_after.ready_receipts.len()).unwrap(), + FunderMutation::AddInvoice(_) | FunderMutation::RemoveInvoice(_) => { + if funder_state_after.open_invoices.len() != funder_state.open_invoices.len() { + vec![FunderReportMutation::SetNumOpenInvoices( + usize_to_u64(funder_state_after.open_invoices.len()).unwrap(), )] } else { Vec::new() } } + FunderMutation::AddDestPlainLock(_) => vec![], } } diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index 948f6f154..09830283a 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -38,12 +38,23 @@ pub struct FunderState { /// A local invoice in progress #[derive(Clone, Serialize, Deserialize, Debug)] pub struct OpenInvoice { + /// Total payment required to fulfill this invoice: + pub total_dest_payment: u128, /// Destination plain locks for all requests related to a single open invoice that was /// originated for this node. /// Multiple requests are possible for a single invoice in case of a multi-route payment. pub dest_plain_locks: ImHashMap, } +impl OpenInvoice { + pub fn new(total_dest_payment: u128) -> Self { + OpenInvoice { + total_dest_payment, + dest_plain_locks: ImHashMap::new(), + } + } +} + /// A local request (Originated from this node) in progress #[derive(Clone, Serialize, Deserialize, Debug)] pub struct OpenTransaction { @@ -61,6 +72,9 @@ pub enum FunderMutation { RemoveFriend(PublicKey), AddReceipt((PaymentId, Receipt)), RemoveReceipt(PaymentId), + AddInvoice((InvoiceId, u128)), // (InvoiceId, total_dest_payment) + AddDestPlainLock((InvoiceId, Uid, PlainLock)), // InvoiceId, RequestId, dest_plain_lock + RemoveInvoice(InvoiceId), } impl FunderState @@ -126,6 +140,19 @@ where FunderMutation::RemoveReceipt(payment_id) => { let _ = self.ready_receipts.remove(payment_id); } + FunderMutation::AddInvoice((invoice_id, total_dest_payment)) => { + self.open_invoices + .insert(invoice_id.clone(), OpenInvoice::new(*total_dest_payment)); + } + FunderMutation::AddDestPlainLock((invoice_id, request_id, plain_lock)) => { + let open_invoice = self.open_invoices.get_mut(invoice_id).unwrap(); + open_invoice + .dest_plain_locks + .insert(request_id.clone(), plain_lock.clone()); + } + FunderMutation::RemoveInvoice(invoice_id) => { + let _ = self.open_invoices.remove(invoice_id); + } } } } diff --git a/components/proto/src/report/convert.rs b/components/proto/src/report/convert.rs index 7dc03dc51..65903f3db 100644 --- a/components/proto/src/report/convert.rs +++ b/components/proto/src/report/convert.rs @@ -124,7 +124,9 @@ where match funder_report_mutation { FunderReportMutation::AddRelay(_) | FunderReportMutation::RemoveRelay(_) - | FunderReportMutation::SetNumReadyReceipts(_) => None, + | FunderReportMutation::SetNumReadyReceipts(_) + | FunderReportMutation::SetNumOpenInvoices(_) + | FunderReportMutation::SetNumOpenTransactions(_) => None, FunderReportMutation::AddFriend(add_friend_report) => { create_update_friend(&add_friend_report.friend_public_key) } diff --git a/components/proto/src/report/messages.rs b/components/proto/src/report/messages.rs index d6741e4e0..b2058469b 100644 --- a/components/proto/src/report/messages.rs +++ b/components/proto/src/report/messages.rs @@ -175,6 +175,8 @@ where pub local_public_key: PublicKey, pub relays: ImVec>, pub friends: ImHashMap>, + pub num_open_invoices: u64, + pub num_open_transactions: u64, pub num_ready_receipts: u64, } @@ -219,6 +221,8 @@ where AddFriend(AddFriendReport), RemoveFriend(PublicKey), FriendReportMutation((PublicKey, FriendReportMutation)), + SetNumOpenInvoices(u64), + SetNumOpenTransactions(u64), SetNumReadyReceipts(u64), } @@ -377,6 +381,14 @@ where self.num_ready_receipts = *num_ready_receipts; Ok(()) } + FunderReportMutation::SetNumOpenInvoices(num_open_invoices) => { + self.num_open_invoices = *num_open_invoices; + Ok(()) + } + FunderReportMutation::SetNumOpenTransactions(num_open_transactions) => { + self.num_open_transactions = *num_open_transactions; + Ok(()) + } } } } diff --git a/components/proto/src/report/serialize.rs b/components/proto/src/report/serialize.rs index f1918c0a0..bf396b1ee 100644 --- a/components/proto/src/report/serialize.rs +++ b/components/proto/src/report/serialize.rs @@ -698,6 +698,8 @@ fn ser_funder_report( fn deser_funder_report( funder_report_reader: &report_capnp::funder_report::Reader, ) -> Result { + unimplemented!(); + /* let mut named_relays = Vec::new(); for named_relay_address in funder_report_reader.get_relays()? { named_relays.push(read_named_relay_address(&named_relay_address)?); @@ -715,6 +717,7 @@ fn deser_funder_report( friends, num_ready_receipts: funder_report_reader.get_num_ready_receipts(), }) + */ } fn ser_add_friend_report( @@ -965,6 +968,8 @@ fn ser_funder_report_mutation( funder_report_mutation: &FunderReportMutation, funder_report_mutation_builder: &mut report_capnp::funder_report_mutation::Builder, ) { + unimplemented!(); + /* match funder_report_mutation { FunderReportMutation::AddRelay(named_relay_address) => { write_named_relay_address( @@ -1008,6 +1013,7 @@ fn ser_funder_report_mutation( .set_set_num_ready_receipts(*num_ready_receipts); } } + */ } fn deser_funder_report_mutation( From 8fe469fec047b30b209754d85868ab9c0d812aba Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 17:56:45 +0300 Subject: [PATCH 087/248] Updated comment. --- components/funder/src/handler/handler.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/funder/src/handler/handler.rs b/components/funder/src/handler/handler.rs index 493dc20de..15e87b5b1 100644 --- a/components/funder/src/handler/handler.rs +++ b/components/funder/src/handler/handler.rs @@ -190,7 +190,7 @@ where outgoing_comms.push(FunderOutgoingComm::ChannelerConfig(channeler_config)); } - // TODO: Sign all unsigned responses and then queue them as mutations + // Sign all unsigned responses and then queue them as mutations await!(m_state.sign_responses(identity_client, rng)); // Send all possible messages according to SendCommands From 5b4dd534ecf599432b4bcd2c73fda93f6afabaa3 Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 18:03:30 +0300 Subject: [PATCH 088/248] Created empty function control_create_payment(). --- .../funder/src/handler/handle_control.rs | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 5138bfb0f..42be03cfd 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -9,10 +9,10 @@ use crate::state::FunderMutation; use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ - AddFriend, ChannelerUpdateFriend, FriendStatus, FunderControl, FunderOutgoingControl, - ReceiptAck, RemoveFriend, ResetFriendChannel, ResponseReceived, ResponseSendFundsResult, - SetFriendName, SetFriendRelays, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, - UserRequestSendFunds, + AddFriend, ChannelerUpdateFriend, CreatePayment, FriendStatus, FunderControl, + FunderOutgoingControl, ReceiptAck, RemoveFriend, ResetFriendChannel, ResponseReceived, + ResponseSendFundsResult, SetFriendName, SetFriendRelays, SetFriendRemoteMaxDebt, + SetFriendStatus, SetRequestsStatus, UserRequestSendFunds, }; use crate::ephemeral::Ephemeral; @@ -586,6 +586,17 @@ where } */ +fn control_create_payment( + m_state: &mut MutableFunderState, + create_payment: CreatePayment, +) -> Result<(), HandleControlError> +where + B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, +{ + // TODO: + unimplemented!(); +} + pub fn handle_control_message( m_state: &mut MutableFunderState, m_ephemeral: &mut MutableEphemeral, @@ -660,7 +671,9 @@ where } // Buyer API: - FunderControl::CreatePayment(_create_payment) => unimplemented!(), + FunderControl::CreatePayment(create_payment) => { + control_create_payment(m_state, create_payment) + } FunderControl::CreateTransaction(_create_transaction) => unimplemented!(), FunderControl::RequestReceipt(_payment_id) => unimplemented!(), FunderControl::RemoveReceipt(_payment_id) => unimplemented!(), From 5bc90f62186cf781a42eca43a2c39208e410cb26 Mon Sep 17 00:00:00 2001 From: real Date: Sat, 18 May 2019 18:39:20 +0300 Subject: [PATCH 089/248] Added FunderState.open_payments. --- components/funder/src/state.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index 09830283a..d1640512a 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -21,12 +21,12 @@ pub struct FunderState { pub relays: ImVec>, /// All configured friends and their state pub friends: ImHashMap>, - /// Locally issued invoices in progress. - // TODO: Add this part in the report? At least a counter of this hash set? + /// Locally issued invoices in progress (For which this node is the seller) pub open_invoices: ImHashMap, - /// Locally created transaction in progress. (This node is the buyer). - // TODO: Add this part in the report? At least a counter of this hash set? + /// Locally created transaction in progress. (For which this node is the buyer). pub open_transactions: ImHashMap, + /// Ongoing payments (For which this node is the buyer): + pub open_payments: ImHashMap, /// Receipts of completed payments (Generated after a CollectSendFundsOp message was received /// successfuly). /// Note: We use our own randomly generated paymentId to represent a payment and not an @@ -35,6 +35,16 @@ pub struct FunderState { pub ready_receipts: ImHashMap, } +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct OpenPayment { + /// Remote invoice id being paid + invoice_id: InvoiceId, + /// Total amount of credits we want to pay to seller. + total_dest_payment: u128, + /// Seller's public key: + destination_public_key: PublicKey, +} + /// A local invoice in progress #[derive(Clone, Serialize, Deserialize, Debug)] pub struct OpenInvoice { @@ -58,6 +68,7 @@ impl OpenInvoice { /// A local request (Originated from this node) in progress #[derive(Clone, Serialize, Deserialize, Debug)] pub struct OpenTransaction { + pub payment_id: PaymentId, /// The plain part of a hash lock for the generated transaction. pub src_plain_lock: PlainLock, } @@ -91,6 +102,7 @@ where friends: ImHashMap::new(), open_invoices: ImHashMap::new(), open_transactions: ImHashMap::new(), + open_payments: ImHashMap::new(), ready_receipts: ImHashMap::new(), } } From f2ca396371e8325b09ed40c033fa5dc37edaaa2f Mon Sep 17 00:00:00 2001 From: real Date: Wed, 22 May 2019 14:27:55 +0300 Subject: [PATCH 090/248] Added OpenPayment struct inside FunderState. --- components/funder/src/report.rs | 32 +++++--- components/funder/src/state.rs | 100 ++++++++++++++++++----- components/proto/src/report/convert.rs | 2 +- components/proto/src/report/messages.rs | 12 +-- components/proto/src/report/serialize.rs | 9 +- 5 files changed, 116 insertions(+), 39 deletions(-) diff --git a/components/funder/src/report.rs b/components/funder/src/report.rs index 1ac0ace6c..e35eb8776 100644 --- a/components/funder/src/report.rs +++ b/components/funder/src/report.rs @@ -175,8 +175,8 @@ where relays: funder_state.relays.clone(), friends, num_open_invoices: usize_to_u64(funder_state.open_invoices.len()).unwrap(), + num_open_payments: usize_to_u64(funder_state.open_payments.len()).unwrap(), num_open_transactions: usize_to_u64(funder_state.open_transactions.len()).unwrap(), - num_ready_receipts: usize_to_u64(funder_state.ready_receipts.len()).unwrap(), } } @@ -338,15 +338,6 @@ where friend_public_key.clone(), )] } - FunderMutation::AddReceipt(_) | FunderMutation::RemoveReceipt(_) => { - if funder_state_after.ready_receipts.len() != funder_state.ready_receipts.len() { - vec![FunderReportMutation::SetNumReadyReceipts( - usize_to_u64(funder_state_after.ready_receipts.len()).unwrap(), - )] - } else { - Vec::new() - } - } FunderMutation::AddInvoice(_) | FunderMutation::RemoveInvoice(_) => { if funder_state_after.open_invoices.len() != funder_state.open_invoices.len() { vec![FunderReportMutation::SetNumOpenInvoices( @@ -357,6 +348,27 @@ where } } FunderMutation::AddDestPlainLock(_) => vec![], + FunderMutation::AddTransaction(_) | FunderMutation::RemoveTransaction(_) => { + if funder_state_after.open_transactions.len() != funder_state.open_transactions.len() { + vec![FunderReportMutation::SetNumOpenTransactions( + usize_to_u64(funder_state_after.open_transactions.len()).unwrap(), + )] + } else { + Vec::new() + } + } + FunderMutation::AddPayment(_) | FunderMutation::RemovePayment(_) => { + if funder_state_after.open_payments.len() != funder_state.open_payments.len() { + vec![FunderReportMutation::SetNumOpenPayments( + usize_to_u64(funder_state_after.open_payments.len()).unwrap(), + )] + } else { + Vec::new() + } + } + FunderMutation::SetPaymentReceipt(_) + | FunderMutation::TakePaymentReceipt(_) + | FunderMutation::SetPaymentClosing(_) => vec![], } } diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index d1640512a..5478b8f00 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -27,24 +27,39 @@ pub struct FunderState { pub open_transactions: ImHashMap, /// Ongoing payments (For which this node is the buyer): pub open_payments: ImHashMap, - /// Receipts of completed payments (Generated after a CollectSendFundsOp message was received - /// successfuly). - /// Note: We use our own randomly generated paymentId to represent a payment and not an - /// InvoiceId. We do this to defend against possible collisions of InvoiceId values (Which - /// are not locally generated). - pub ready_receipts: ImHashMap, +} + +#[derive(Clone, Serialize, Deserialize, Debug)] +pub enum ReceiptStatus { + /// Haven't got a receipt yet + Empty, + /// We Have a pending receipt, waiting to be handed to the user. + Pending(Receipt), + /// A receipt was already received and handed over to user. + Taken, } #[derive(Clone, Serialize, Deserialize, Debug)] pub struct OpenPayment { + /// Is in the process of being closed? + /// Can only be removed if (open_transactions == 0) && opt_receipt.is_none() + pub is_closing: bool, /// Remote invoice id being paid - invoice_id: InvoiceId, + pub invoice_id: InvoiceId, + /// The amount of ongoing transactions for this payment. + pub open_transactions: usize, /// Total amount of credits we want to pay to seller. - total_dest_payment: u128, + pub total_dest_payment: u128, /// Seller's public key: - destination_public_key: PublicKey, + pub dest_public_key: PublicKey, + /// The first receipt that was received for this payment. (Generated after a CollectSendFundsOp + /// message was received successfuly). + pub receipt_status: ReceiptStatus, } +// TODO: If a receipt is requested and OpenPayment.open_transactions == 0, it should reported that no receipt +// exists and the payment should be removed. + /// A local invoice in progress #[derive(Clone, Serialize, Deserialize, Debug)] pub struct OpenInvoice { @@ -81,11 +96,16 @@ pub enum FunderMutation { RemoveRelay(PublicKey), AddFriend(AddFriend), RemoveFriend(PublicKey), - AddReceipt((PaymentId, Receipt)), - RemoveReceipt(PaymentId), AddInvoice((InvoiceId, u128)), // (InvoiceId, total_dest_payment) AddDestPlainLock((InvoiceId, Uid, PlainLock)), // InvoiceId, RequestId, dest_plain_lock RemoveInvoice(InvoiceId), + AddTransaction((Uid, PaymentId, PlainLock)), // (transaction_id, payment_id,src_plain_lock) + RemoveTransaction(Uid), // transaction_id + AddPayment((PaymentId, InvoiceId, u128, PublicKey)), // (payment_id, invoice_id, total_dest_payment, destination) + SetPaymentReceipt((PaymentId, Receipt)), + TakePaymentReceipt(PaymentId), + SetPaymentClosing(PaymentId), + RemovePayment(PaymentId), } impl FunderState @@ -103,7 +123,6 @@ where open_invoices: ImHashMap::new(), open_transactions: ImHashMap::new(), open_payments: ImHashMap::new(), - ready_receipts: ImHashMap::new(), } } @@ -145,13 +164,6 @@ where FunderMutation::RemoveFriend(public_key) => { let _ = self.friends.remove(&public_key); } - FunderMutation::AddReceipt((payment_id, send_funds_receipt)) => { - self.ready_receipts - .insert(payment_id.clone(), send_funds_receipt.clone()); - } - FunderMutation::RemoveReceipt(payment_id) => { - let _ = self.ready_receipts.remove(payment_id); - } FunderMutation::AddInvoice((invoice_id, total_dest_payment)) => { self.open_invoices .insert(invoice_id.clone(), OpenInvoice::new(*total_dest_payment)); @@ -165,6 +177,56 @@ where FunderMutation::RemoveInvoice(invoice_id) => { let _ = self.open_invoices.remove(invoice_id); } + FunderMutation::AddTransaction((transaction_id, payment_id, src_plain_lock)) => { + let open_transaction = OpenTransaction { + payment_id: payment_id.clone(), + src_plain_lock: src_plain_lock.clone(), + }; + let _ = self + .open_transactions + .insert(transaction_id.clone(), open_transaction); + } + FunderMutation::RemoveTransaction(transaction_id) => { + let _ = self.open_transactions.remove(transaction_id); + } + FunderMutation::AddPayment(( + payment_id, + invoice_id, + total_dest_payment, + dest_public_key, + )) => { + let open_payment = OpenPayment { + is_closing: false, + invoice_id: invoice_id.clone(), + open_transactions: 0, + total_dest_payment: *total_dest_payment, + dest_public_key: dest_public_key.clone(), + receipt_status: ReceiptStatus::Empty, + }; + let _ = self.open_payments.insert(payment_id.clone(), open_payment); + } + FunderMutation::SetPaymentReceipt((payment_id, receipt)) => { + let open_payment = self.open_payments.get_mut(payment_id).unwrap(); + if let ReceiptStatus::Empty = open_payment.receipt_status { + open_payment.receipt_status = ReceiptStatus::Pending(receipt.clone()); + } else { + unreachable!(); + } + } + FunderMutation::TakePaymentReceipt(payment_id) => { + let open_payment = self.open_payments.get_mut(payment_id).unwrap(); + if let ReceiptStatus::Pending(_) = &open_payment.receipt_status { + open_payment.receipt_status = ReceiptStatus::Taken; + } else { + unreachable!(); + } + } + FunderMutation::SetPaymentClosing(payment_id) => { + self.open_payments.get_mut(payment_id).unwrap().is_closing = true; + } + FunderMutation::RemovePayment(payment_id) => { + self.open_payments.remove(payment_id); + } } } } diff --git a/components/proto/src/report/convert.rs b/components/proto/src/report/convert.rs index 65903f3db..6a44f41c8 100644 --- a/components/proto/src/report/convert.rs +++ b/components/proto/src/report/convert.rs @@ -124,8 +124,8 @@ where match funder_report_mutation { FunderReportMutation::AddRelay(_) | FunderReportMutation::RemoveRelay(_) - | FunderReportMutation::SetNumReadyReceipts(_) | FunderReportMutation::SetNumOpenInvoices(_) + | FunderReportMutation::SetNumOpenPayments(_) | FunderReportMutation::SetNumOpenTransactions(_) => None, FunderReportMutation::AddFriend(add_friend_report) => { create_update_friend(&add_friend_report.friend_public_key) diff --git a/components/proto/src/report/messages.rs b/components/proto/src/report/messages.rs index b2058469b..7b1e5a573 100644 --- a/components/proto/src/report/messages.rs +++ b/components/proto/src/report/messages.rs @@ -176,8 +176,8 @@ where pub relays: ImVec>, pub friends: ImHashMap>, pub num_open_invoices: u64, + pub num_open_payments: u64, pub num_open_transactions: u64, - pub num_ready_receipts: u64, } #[allow(clippy::large_enum_variant)] @@ -222,8 +222,8 @@ where RemoveFriend(PublicKey), FriendReportMutation((PublicKey, FriendReportMutation)), SetNumOpenInvoices(u64), + SetNumOpenPayments(u64), SetNumOpenTransactions(u64), - SetNumReadyReceipts(u64), } #[derive(Debug, Clone, PartialEq, Eq)] @@ -377,14 +377,14 @@ where friend.mutate(friend_report_mutation)?; Ok(()) } - FunderReportMutation::SetNumReadyReceipts(num_ready_receipts) => { - self.num_ready_receipts = *num_ready_receipts; - Ok(()) - } FunderReportMutation::SetNumOpenInvoices(num_open_invoices) => { self.num_open_invoices = *num_open_invoices; Ok(()) } + FunderReportMutation::SetNumOpenPayments(num_open_payments) => { + self.num_open_payments = *num_open_payments; + Ok(()) + } FunderReportMutation::SetNumOpenTransactions(num_open_transactions) => { self.num_open_transactions = *num_open_transactions; Ok(()) diff --git a/components/proto/src/report/serialize.rs b/components/proto/src/report/serialize.rs index bf396b1ee..c44dd056f 100644 --- a/components/proto/src/report/serialize.rs +++ b/components/proto/src/report/serialize.rs @@ -672,6 +672,8 @@ fn ser_funder_report( funder_report: &FunderReport, funder_report_builder: &mut report_capnp::funder_report::Builder, ) { + unimplemented!(); + /* write_public_key( &funder_report.local_public_key, &mut funder_report_builder.reborrow().init_local_public_key(), @@ -693,6 +695,7 @@ fn ser_funder_report( } funder_report_builder.set_num_ready_receipts(funder_report.num_ready_receipts); + */ } fn deser_funder_report( @@ -1019,6 +1022,8 @@ fn ser_funder_report_mutation( fn deser_funder_report_mutation( funder_report_mutation_reader: &report_capnp::funder_report_mutation::Reader, ) -> Result { + unimplemented!(); + /* Ok(match funder_report_mutation_reader.which()? { report_capnp::funder_report_mutation::AddRelay(named_relay_address_reader) => { FunderReportMutation::AddRelay(read_named_relay_address(&named_relay_address_reader?)?) @@ -1037,10 +1042,8 @@ fn deser_funder_report_mutation( ) => FunderReportMutation::FriendReportMutation(deser_pk_friend_report_mutation( &pk_friend_report_mutation_reader?, )?), - report_capnp::funder_report_mutation::SetNumReadyReceipts(num_ready_receipts) => { - FunderReportMutation::SetNumReadyReceipts(num_ready_receipts) - } }) + */ } fn ser_index_client_report( From 04660596b69015646a46d3108e3058cbe3692b06 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 22 May 2019 14:29:04 +0300 Subject: [PATCH 091/248] Updated comment. --- components/funder/src/state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index 5478b8f00..3f88e7bda 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -42,7 +42,7 @@ pub enum ReceiptStatus { #[derive(Clone, Serialize, Deserialize, Debug)] pub struct OpenPayment { /// Is in the process of being closed? - /// Can only be removed if (open_transactions == 0) && opt_receipt.is_none() + /// Can only be removed if (open_transactions == 0) && receipt_status != ReceiptStatus::Pending(Receipt) pub is_closing: bool, /// Remote invoice id being paid pub invoice_id: InvoiceId, From 99c5995e8af7c3304339c82c4bb2bd964c6d4f7f Mon Sep 17 00:00:00 2001 From: real Date: Wed, 22 May 2019 15:50:30 +0300 Subject: [PATCH 092/248] Added SetFriendRate to FunderControl. --- .../funder/src/handler/handle_control.rs | 1 + .../funder/src/mutual_credit/incoming.rs | 9 ++---- components/funder/src/mutual_credit/types.rs | 31 ++++--------------- components/proto/src/funder/messages.rs | 24 ++++++++++++++ 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 42be03cfd..7a23f4739 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -669,6 +669,7 @@ where FunderControl::SetFriendName(set_friend_name) => { control_set_friend_name(m_state, set_friend_name) } + FunderControl::SetFriendRate(_friend_rate) => unimplemented!(), // Buyer API: FunderControl::CreatePayment(create_payment) => { diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index 47f05a3e6..dac6afa7a 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -11,7 +11,7 @@ use proto::funder::signature_buff::create_response_signature_buffer; use crate::types::create_pending_transaction; -use super::types::{McMutation, MutualCredit, MAX_FUNDER_DEBT}; +use super::types::{calc_fee, McMutation, MutualCredit, MAX_FUNDER_DEBT}; #[derive(Debug)] pub struct IncomingResponseSendFundsOp { @@ -217,11 +217,8 @@ fn process_request_send_funds( // Calculate the amount of credits we want for passing this request. // (Note: We are for sure not the origin of this request message, because it was received from a // remote side) - let own_fee = mutual_credit - .state() - .rate - .calc_fee(request_send_funds.dest_payment) - .map_err(|_| ProcessOperationError::CalcFeeError)?; + let own_fee = calc_fee(&mutual_credit.state().rate, request_send_funds.dest_payment) + .ok_or(ProcessOperationError::CalcFeeError)?; let new_left_fees = request_send_funds .left_fees diff --git a/components/funder/src/mutual_credit/types.rs b/components/funder/src/mutual_credit/types.rs index 13ef966ea..5a5d5b4c3 100644 --- a/components/funder/src/mutual_credit/types.rs +++ b/components/funder/src/mutual_credit/types.rs @@ -7,7 +7,7 @@ use crypto::identity::PublicKey; use crypto::uid::Uid; use proto::funder::messages::{ - PendingTransaction, RequestSendFundsOp, RequestsStatus, TransactionStage, + PendingTransaction, Rate, RequestSendFundsOp, RequestsStatus, TransactionStage, }; /// The maximum possible funder debt. @@ -88,30 +88,11 @@ impl McRequestsStatus { } } -/// Rates for forwarding a transaction -/// For a transaction of `x` credits, the amount of fees will be: -/// `(x * mul) / 2^32 + add` -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct Rate { - /// Commission - mul: u32, - /// Flat rate - add: u32, -} - -#[derive(Debug)] -pub struct RateError; - -impl Rate { - pub fn new() -> Self { - Rate { mul: 0, add: 0 } - } - - pub fn calc_fee(&self, dest_payment: u128) -> Result { - let mul_res = (BigUint::from(dest_payment) * BigUint::from(self.mul)) >> 32; - let res = mul_res + BigUint::from(self.add); - res.to_u128().ok_or(RateError) - } +/// Calculate fee for forwarding a transaction given a `rate` and `dest_payment`. +pub fn calc_fee(rate: &Rate, dest_payment: u128) -> Option { + let mul_res = (BigUint::from(dest_payment) * BigUint::from(rate.mul)) >> 32; + let res = mul_res + BigUint::from(rate.add); + res.to_u128() } #[derive(Clone, Serialize, Deserialize, Debug)] diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 5de197846..a46e0105a 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -395,6 +395,23 @@ impl RequestsStatus { } } +/// Rates for forwarding a transaction +/// For a transaction of `x` credits, the amount of fees will be: +/// `(x * mul) / 2^32 + add` +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct Rate { + /// Commission + pub mul: u32, + /// Flat rate + pub add: u32, +} + +impl Rate { + pub fn new() -> Self { + Rate { mul: 0, add: 0 } + } +} + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct AddFriend { pub friend_public_key: PublicKey, @@ -444,6 +461,12 @@ pub struct ResetFriendChannel { pub reset_token: Signature, } +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct SetFriendRate { + pub friend_public_key: PublicKey, + pub rate: Rate, +} + /// A friend's route with known capacity #[derive(Debug, Clone, PartialEq, Eq)] struct FriendsRouteCapacity { @@ -510,6 +533,7 @@ pub enum FunderControl { SetFriendRemoteMaxDebt(SetFriendRemoteMaxDebt), SetFriendRelays(SetFriendRelays), SetFriendName(SetFriendName), + SetFriendRate(SetFriendRate), ResetFriendChannel(ResetFriendChannel), // Buyer API: CreatePayment(CreatePayment), From e8d8f4ed7ac35374755f20560462d6b1bd80699a Mon Sep 17 00:00:00 2001 From: real Date: Wed, 22 May 2019 16:14:49 +0300 Subject: [PATCH 093/248] Removed rate considerations from mutual_credit. --- .../funder/src/mutual_credit/incoming.rs | 20 ++++++------------- components/funder/src/mutual_credit/types.rs | 13 ++---------- 2 files changed, 8 insertions(+), 25 deletions(-) diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index dac6afa7a..0720aacbd 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -11,7 +11,7 @@ use proto::funder::signature_buff::create_response_signature_buffer; use crate::types::create_pending_transaction; -use super::types::{calc_fee, McMutation, MutualCredit, MAX_FUNDER_DEBT}; +use super::types::{McMutation, MutualCredit, MAX_FUNDER_DEBT}; #[derive(Debug)] pub struct IncomingResponseSendFundsOp { @@ -206,17 +206,12 @@ fn process_request_send_funds( return Err(ProcessOperationError::LocalRequestsClosed); } - let route_len = - usize_to_u32(request_send_funds.route.len()).ok_or(ProcessOperationError::RouteTooLong)?; - - let local_index = remote_index - .checked_add(1) - .ok_or(ProcessOperationError::RouteTooLong)?; - let local_index = usize_to_u32(local_index).ok_or(ProcessOperationError::RouteTooLong)?; - // Calculate the amount of credits we want for passing this request. // (Note: We are for sure not the origin of this request message, because it was received from a // remote side) + // + // TODO: Should be done outside of MutualCredit: + /* let own_fee = calc_fee(&mutual_credit.state().rate, request_send_funds.dest_payment) .ok_or(ProcessOperationError::CalcFeeError)?; @@ -224,6 +219,7 @@ fn process_request_send_funds( .left_fees .checked_sub(own_fee) .ok_or(ProcessOperationError::InsufficientFee)?; + */ // Calculate amount of credits to freeze let own_freeze_credits = request_send_funds @@ -260,11 +256,7 @@ fn process_request_send_funds( } // Add pending transaction: - let pending_transaction = { - let mut pending_transaction = create_pending_transaction(&request_send_funds); - pending_transaction.left_fees = new_left_fees; - pending_transaction - }; + let pending_transaction = create_pending_transaction(&request_send_funds); // let pending_friend_request = create_pending_transaction(&request_send_funds); diff --git a/components/funder/src/mutual_credit/types.rs b/components/funder/src/mutual_credit/types.rs index 5a5d5b4c3..594a736d2 100644 --- a/components/funder/src/mutual_credit/types.rs +++ b/components/funder/src/mutual_credit/types.rs @@ -99,8 +99,6 @@ pub fn calc_fee(rate: &Rate, dest_payment: u128) -> Option { pub struct MutualCreditState { /// Public identities of local and remote side pub idents: McIdents, - /// Rate for forwarding new transactions to the remote side - pub rate: Rate, /// Current credit balance with respect to remote side pub balance: McBalance, /// Requests in progress @@ -131,7 +129,6 @@ pub enum McMutation { SetRemotePendingTransactionStage((Uid, TransactionStage)), SetLocalPendingDebt(u128), SetRemotePendingDebt(u128), - SetRate(Rate), } impl MutualCredit { @@ -146,7 +143,6 @@ impl MutualCredit { local_public_key: local_public_key.clone(), remote_public_key: remote_public_key.clone(), }, - rate: Rate::new(), balance: McBalance::new(balance), pending_transactions: McPendingTransactions::new(), requests_status: McRequestsStatus::new(), @@ -172,8 +168,8 @@ impl MutualCredit { &self.state } - pub fn mutate(&mut self, tc_mutation: &McMutation) { - match tc_mutation { + pub fn mutate(&mut self, mc_mutation: &McMutation) { + match mc_mutation { McMutation::SetLocalRequestsStatus(requests_status) => { self.set_local_requests_status(requests_status.clone()) } @@ -211,7 +207,6 @@ impl MutualCredit { McMutation::SetRemotePendingDebt(remote_pending_debt) => { self.set_remote_pending_debt(*remote_pending_debt) } - McMutation::SetRate(rate) => self.set_rate(rate.clone()), } } @@ -265,10 +260,6 @@ impl MutualCredit { self.state.balance.local_pending_debt = local_pending_debt; } - fn set_rate(&mut self, rate: Rate) { - self.state.rate = rate; - } - fn set_local_pending_transaction_stage(&mut self, request_id: &Uid, stage: TransactionStage) { self.state .pending_transactions From aa9562ee4c0d204af766384954eab45628ce42f7 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 22 May 2019 16:22:35 +0300 Subject: [PATCH 094/248] Added rate field to FriendState. --- components/funder/src/friend.rs | 49 +++++++++++++++++---------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/components/funder/src/friend.rs b/components/funder/src/friend.rs index d0c291f90..21b8985fc 100644 --- a/components/funder/src/friend.rs +++ b/components/funder/src/friend.rs @@ -9,8 +9,8 @@ use common::safe_arithmetic::SafeUnsignedArithmetic; use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ - CancelSendFundsOp, CollectSendFundsOp, FriendStatus, PendingTransaction, RequestSendFundsOp, - RequestsStatus, ResetTerms, ResponseSendFundsOp, + CancelSendFundsOp, CollectSendFundsOp, FriendStatus, PendingTransaction, Rate, + RequestSendFundsOp, RequestsStatus, ResetTerms, ResponseSendFundsOp, }; use crate::token_channel::{TcMutation, TokenChannel}; @@ -65,27 +65,6 @@ where } } } - -#[allow(clippy::large_enum_variant)] -#[derive(Debug, Clone, Serialize, Deserialize)] -pub enum FriendMutation { - TcMutation(TcMutation), - SetInconsistent(ChannelInconsistent), - SetConsistent(TokenChannel), - SetWantedRemoteMaxDebt(u128), - SetWantedLocalRequestsStatus(RequestsStatus), - PushBackPendingRequest(RequestSendFundsOp), - PopFrontPendingRequest, - PushBackPendingBackwardsOp(BackwardsOp), - PopFrontPendingBackwardsOp, - PushBackPendingUserRequest(RequestSendFundsOp), - PopFrontPendingUserRequest, - SetStatus(FriendStatus), - SetRemoteRelays(Vec>), - SetName(String), - SetSentLocalRelays(SentLocalRelays), -} - #[derive(PartialEq, Eq, Clone, Serialize, Deserialize, Debug)] pub struct ChannelInconsistent { pub opt_last_incoming_move_token: Option, @@ -130,6 +109,8 @@ pub struct FriendState { pub sent_local_relays: SentLocalRelays, /// Locally maintained name of the remote friend node. pub name: String, + /// Rate of forwarding transactions that arrived from this friend to any other friend. + pub rate: Rate, /// Friend status. If disabled, we don't attempt to connect to this friend. (Friend will think /// we are offline). pub status: FriendStatus, @@ -155,6 +136,26 @@ pub struct FriendState { pub pending_user_requests: ImVec, } +#[allow(clippy::large_enum_variant)] +#[derive(Debug, Clone, Serialize, Deserialize)] +pub enum FriendMutation { + TcMutation(TcMutation), + SetInconsistent(ChannelInconsistent), + SetConsistent(TokenChannel), + SetWantedRemoteMaxDebt(u128), + SetWantedLocalRequestsStatus(RequestsStatus), + PushBackPendingRequest(RequestSendFundsOp), + PopFrontPendingRequest, + PushBackPendingBackwardsOp(BackwardsOp), + PopFrontPendingBackwardsOp, + PushBackPendingUserRequest(RequestSendFundsOp), + PopFrontPendingUserRequest, + SetStatus(FriendStatus), + SetRemoteRelays(Vec>), + SetName(String), + SetSentLocalRelays(SentLocalRelays), +} + impl FriendState where B: Clone + CanonicalSerialize, @@ -174,6 +175,8 @@ where remote_relays, sent_local_relays: SentLocalRelays::NeverSent, name, + // Initial rate is 0 for a new friend: + rate: Rate::new(), status: FriendStatus::Disabled, channel_status: ChannelStatus::Consistent(token_channel), From 19d3696bd623e51294ef7ab164b0c88306b741ff Mon Sep 17 00:00:00 2001 From: real Date: Wed, 22 May 2019 16:33:23 +0300 Subject: [PATCH 095/248] Added control_set_friend_rate(). --- components/funder/src/friend.rs | 4 +++ .../funder/src/handler/handle_control.rs | 35 +++++++++++++++++-- components/funder/src/report.rs | 2 ++ components/proto/src/report/messages.rs | 8 ++++- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/components/funder/src/friend.rs b/components/funder/src/friend.rs index 21b8985fc..e37d32793 100644 --- a/components/funder/src/friend.rs +++ b/components/funder/src/friend.rs @@ -153,6 +153,7 @@ pub enum FriendMutation { SetStatus(FriendStatus), SetRemoteRelays(Vec>), SetName(String), + SetRate(Rate), SetSentLocalRelays(SentLocalRelays), } @@ -269,6 +270,9 @@ where FriendMutation::SetName(friend_name) => { self.name = friend_name.clone(); } + FriendMutation::SetRate(friend_rate) => { + self.rate = friend_rate.clone(); + } FriendMutation::SetSentLocalRelays(sent_local_relays) => { self.sent_local_relays = sent_local_relays.clone(); } diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 7a23f4739..df5830444 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -11,7 +11,7 @@ use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ AddFriend, ChannelerUpdateFriend, CreatePayment, FriendStatus, FunderControl, FunderOutgoingControl, ReceiptAck, RemoveFriend, ResetFriendChannel, ResponseReceived, - ResponseSendFundsResult, SetFriendName, SetFriendRelays, SetFriendRemoteMaxDebt, + ResponseSendFundsResult, SetFriendName, SetFriendRate, SetFriendRelays, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, UserRequestSendFunds, }; @@ -422,6 +422,35 @@ where Ok(()) } +fn control_set_friend_rate( + m_state: &mut MutableFunderState, + set_friend_rate: SetFriendRate, +) -> Result<(), HandleControlError> +where + B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, +{ + // Make sure that friend exists: + let friend = m_state + .state() + .friends + .get(&set_friend_rate.friend_public_key) + .ok_or(HandleControlError::FriendDoesNotExist)?; + + // If the newly proposed rate is the same as the old one, we do nothing: + if friend.rate == set_friend_rate.rate { + return Ok(()); + } + + let friend_mutation = FriendMutation::SetRate(set_friend_rate.rate); + let funder_mutation = FunderMutation::FriendMutation(( + set_friend_rate.friend_public_key.clone(), + friend_mutation, + )); + m_state.mutate(funder_mutation); + + Ok(()) +} + fn check_user_request_valid(user_request_send_funds: &UserRequestSendFunds) -> Option<()> { if !user_request_send_funds.route.is_valid() { return None; @@ -669,7 +698,9 @@ where FunderControl::SetFriendName(set_friend_name) => { control_set_friend_name(m_state, set_friend_name) } - FunderControl::SetFriendRate(_friend_rate) => unimplemented!(), + FunderControl::SetFriendRate(set_friend_rate) => { + control_set_friend_rate(m_state, set_friend_rate) + } // Buyer API: FunderControl::CreatePayment(create_payment) => { diff --git a/components/funder/src/report.rs b/components/funder/src/report.rs index e35eb8776..5ad3ca489 100644 --- a/components/funder/src/report.rs +++ b/components/funder/src/report.rs @@ -136,6 +136,7 @@ where FriendReport { name: friend_state.name.clone(), + rate: friend_state.rate.clone(), remote_relays: friend_state.remote_relays.clone(), sent_local_relays: (&friend_state.sent_local_relays).into(), opt_last_incoming_move_token: friend_state @@ -261,6 +262,7 @@ where vec![FriendReportMutation::SetRemoteRelays(remote_relays.clone())] } FriendMutation::SetName(name) => vec![FriendReportMutation::SetName(name.clone())], + FriendMutation::SetRate(rate) => vec![FriendReportMutation::SetRate(rate.clone())], FriendMutation::SetSentLocalRelays(sent_local_relays) => { vec![FriendReportMutation::SetSentLocalRelays( sent_local_relays.into(), diff --git a/components/proto/src/report/messages.rs b/components/proto/src/report/messages.rs index 7b1e5a573..a10dbd69c 100644 --- a/components/proto/src/report/messages.rs +++ b/components/proto/src/report/messages.rs @@ -9,7 +9,7 @@ use crypto::identity::{PublicKey, Signature}; use crypto::uid::Uid; use crate::app_server::messages::{NamedRelayAddress, RelayAddress}; -use crate::funder::messages::{FriendStatus, RequestsStatus}; +use crate::funder::messages::{FriendStatus, Rate, RequestsStatus}; use crate::net::messages::NetAddress; #[derive(Clone, Debug, PartialEq, Eq)] @@ -144,6 +144,7 @@ where B: Clone, { pub name: String, + pub rate: Rate, pub remote_relays: Vec>, pub sent_local_relays: SentLocalRelaysReport, // Last message signed by the remote side. @@ -188,6 +189,7 @@ where { SetRemoteRelays(Vec>), SetName(String), + SetRate(Rate), SetSentLocalRelays(SentLocalRelaysReport), SetChannelStatus(ChannelStatusReport), SetWantedRemoteMaxDebt(u128), @@ -268,6 +270,9 @@ where FriendReportMutation::SetName(name) => { self.name = name.clone(); } + FriendReportMutation::SetRate(rate) => { + self.rate = rate.clone(); + } FriendReportMutation::SetRemoteRelays(remote_relays) => { self.remote_relays = remote_relays.clone(); } @@ -333,6 +338,7 @@ where FunderReportMutation::AddFriend(add_friend_report) => { let friend_report = FriendReport { name: add_friend_report.name.clone(), + rate: Rate::new(), remote_relays: add_friend_report.relays.clone(), sent_local_relays: SentLocalRelaysReport::NeverSent, opt_last_incoming_move_token: add_friend_report From 93037c8ff7bb42bb15c1a7f71860cbe124287fb9 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 22 May 2019 16:40:54 +0300 Subject: [PATCH 096/248] Implemented control_create_payment(). --- .../funder/src/handler/handle_control.rs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index df5830444..58b50beff 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -39,6 +39,7 @@ pub enum HandleControlError { UserRequestInvalid, FriendNotReady, MaxNodeRelaysReached, + PaymentAlreadyOpen, } fn control_set_friend_remote_max_debt( @@ -622,8 +623,25 @@ fn control_create_payment( where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, { - // TODO: - unimplemented!(); + // Make sure that a payment with the same payment_id doesn't exist: + if m_state + .state() + .open_payments + .contains_key(&create_payment.payment_id) + { + return Err(HandleControlError::PaymentAlreadyOpen); + } + + // Add a new payment entry: + let m_mutation = FunderMutation::AddPayment(( + create_payment.payment_id, + create_payment.invoice_id, + create_payment.total_dest_payment, + create_payment.dest_public_key, + )); + + m_state.mutate(m_mutation); + Ok(()) } pub fn handle_control_message( From c3ceeffce0affe63e52737d4f41cff9898fe2500 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 22 May 2019 17:52:58 +0300 Subject: [PATCH 097/248] Implemented control_create_transaction_inner(). --- .../funder/src/handler/handle_control.rs | 185 +++++++++++++++++- components/funder/src/handler/handler.rs | 1 + components/funder/src/report.rs | 1 + components/funder/src/state.rs | 15 +- 4 files changed, 191 insertions(+), 11 deletions(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 58b50beff..2c586b2d7 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -2,17 +2,20 @@ use std::fmt::Debug; use common::canonical_serialize::CanonicalSerialize; +use crypto::crypto_rand::CryptoRandom; +use crypto::hash_lock::PlainLock; use crypto::identity::PublicKey; use crate::friend::{ChannelStatus, FriendMutation}; -use crate::state::FunderMutation; +use crate::state::{FunderMutation, ReceiptStatus}; use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ - AddFriend, ChannelerUpdateFriend, CreatePayment, FriendStatus, FunderControl, - FunderOutgoingControl, ReceiptAck, RemoveFriend, ResetFriendChannel, ResponseReceived, - ResponseSendFundsResult, SetFriendName, SetFriendRate, SetFriendRelays, SetFriendRemoteMaxDebt, - SetFriendStatus, SetRequestsStatus, UserRequestSendFunds, + AddFriend, ChannelerUpdateFriend, CreatePayment, CreateTransaction, FriendStatus, + FunderControl, FunderOutgoingControl, ReceiptAck, RemoveFriend, RequestSendFundsOp, + ResetFriendChannel, ResponseReceived, ResponseSendFundsResult, SetFriendName, SetFriendRate, + SetFriendRelays, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, + UserRequestSendFunds, }; use crate::ephemeral::Ephemeral; @@ -40,6 +43,8 @@ pub enum HandleControlError { FriendNotReady, MaxNodeRelaysReached, PaymentAlreadyOpen, + OpenPaymentNotFound, + PaymentReceiptNotEmpty, } fn control_set_friend_remote_max_debt( @@ -644,18 +649,176 @@ where Ok(()) } -pub fn handle_control_message( +fn control_create_transaction_inner( + m_state: &mut MutableFunderState, + ephemeral: &Ephemeral, + outgoing_control: &mut Vec>, + send_commands: &mut SendCommands, + rng: &R, + max_pending_user_requests: usize, + create_transaction: CreateTransaction, +) -> Result<(), HandleControlError> +where + B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, + R: CryptoRandom, +{ + // Make sure that a corresponding payment is open + let open_payment = m_state + .state() + .open_payments + .get(&create_transaction.payment_id) + .ok_or(HandleControlError::OpenPaymentNotFound)?; + + // If we already have a receipt or the payment is closed, do not allow creating a new + // transaction: + if let ReceiptStatus::Empty = open_payment.receipt_status { + // Good case + } else { + return Err(HandleControlError::PaymentReceiptNotEmpty)?; + } + + let route = &create_transaction.route; + + // We have to be the first on the route: + match route.public_keys.first() { + Some(first) if *first == m_state.state().local_public_key => Ok(()), + _ => Err(HandleControlError::NotFirstInRoute), + }?; + + // We want to have at least two public keys on the route (source and destination). + // We also want that the public keys on the route are unique. + if !route.is_valid() { + return Err(HandleControlError::InvalidRoute); + } + let friend_public_key = route.public_keys[1].clone(); + + let friend = match m_state.state().friends.get(&friend_public_key) { + Some(friend) => Ok(friend), + None => Err(HandleControlError::FriendDoesNotExist), + }?; + + if !is_friend_ready(m_state.state(), ephemeral, &friend_public_key) { + return Err(HandleControlError::FriendNotReady); + } + + // If payment is already in progress, we do nothing: + // Check if there is already a pending user payment with the same payment_id: + for user_request in &friend.pending_user_requests { + if create_transaction.request_id == user_request.request_id { + return Err(HandleControlError::RequestAlreadyInProgress); + } + } + + let token_channel = match &friend.channel_status { + ChannelStatus::Inconsistent(_) => { + // It is impossible that the Channel is Inconsistent, because we know that this friend is + // in ready state: + unreachable!(); + } + ChannelStatus::Consistent(token_channel) => token_channel, + }; + + // Check if there is an ongoing request with the same request_id with this specific friend: + if token_channel + .get_mutual_credit() + .state() + .pending_transactions + .local + .contains_key(&create_transaction.request_id) + { + return Err(HandleControlError::RequestAlreadyInProgress); + } + + // Check if we have room to push this message: + if friend.pending_user_requests.len() >= max_pending_user_requests { + return Err(HandleControlError::PendingUserRequestsFull); + } + + // Randomly generate a new PlainLock: + let src_plain_lock = PlainLock::new(rng); + + // Keep PlainLock: + let funder_mutation = FunderMutation::AddTransaction(( + create_transaction.request_id, + create_transaction.payment_id, + src_plain_lock.clone(), + )); + m_state.mutate(funder_mutation); + + let open_payment = m_state + .state() + .open_payments + .get(&create_transaction.payment_id) + .unwrap(); + + // Update OpenPayment (Increase open transactions count): + let new_num_transactions = open_payment.num_transactions.checked_add(1).unwrap(); + let funder_mutation = FunderMutation::SetPaymentNumTransactions(( + create_transaction.payment_id, + new_num_transactions, + )); + m_state.mutate(funder_mutation); + + let open_payment = m_state + .state() + .open_payments + .get(&create_transaction.payment_id) + .unwrap(); + + // Push the request: + let request_send_funds = RequestSendFundsOp { + request_id: create_transaction.request_id, + src_hashed_lock: src_plain_lock.hash(), + route: create_transaction.route, + dest_payment: create_transaction.dest_payment, + total_dest_payment: open_payment.total_dest_payment, + invoice_id: open_payment.invoice_id.clone(), + left_fees: create_transaction.fees, + }; + + let friend_mutation = FriendMutation::PushBackPendingUserRequest(request_send_funds); + let funder_mutation = + FunderMutation::FriendMutation((friend_public_key.clone(), friend_mutation)); + m_state.mutate(funder_mutation); + + // Signal the sender to attempt to send: + send_commands.set_try_send(&friend_public_key); + unimplemented!(); +} + +fn control_create_transaction( + m_state: &mut MutableFunderState, + ephemeral: &Ephemeral, + outgoing_control: &mut Vec>, + send_commands: &mut SendCommands, + rng: &R, + max_pending_user_requests: usize, + create_transaction: CreateTransaction, +) -> Result<(), HandleControlError> +where + B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, + R: CryptoRandom, +{ + // TODO: + // - Call inner version of the function + // - Send outgoing control with error if any error occurs in any of the above + unimplemented!(); +} + +pub fn handle_control_message( m_state: &mut MutableFunderState, m_ephemeral: &mut MutableEphemeral, send_commands: &mut SendCommands, outgoing_control: &mut Vec>, outgoing_channeler_config: &mut Vec>>, + rng: &R, max_node_relays: usize, max_pending_user_requests: usize, incoming_control: FunderControl, ) -> Result<(), HandleControlError> where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, + R: CryptoRandom, { match incoming_control { FunderControl::SetFriendRemoteMaxDebt(set_friend_remote_max_debt) => { @@ -724,7 +887,15 @@ where FunderControl::CreatePayment(create_payment) => { control_create_payment(m_state, create_payment) } - FunderControl::CreateTransaction(_create_transaction) => unimplemented!(), + FunderControl::CreateTransaction(create_transaction) => control_create_transaction( + m_state, + m_ephemeral.ephemeral(), + outgoing_control, + send_commands, + rng, + max_pending_user_requests, + create_transaction, + ), FunderControl::RequestReceipt(_payment_id) => unimplemented!(), FunderControl::RemoveReceipt(_payment_id) => unimplemented!(), diff --git a/components/funder/src/handler/handler.rs b/components/funder/src/handler/handler.rs index 15e87b5b1..3a6853584 100644 --- a/components/funder/src/handler/handler.rs +++ b/components/funder/src/handler/handler.rs @@ -81,6 +81,7 @@ where &mut send_commands, &mut outgoing_control, &mut outgoing_channeler_config, + rng, max_node_relays, max_pending_user_requests, funder_incoming_control.funder_control, diff --git a/components/funder/src/report.rs b/components/funder/src/report.rs index 5ad3ca489..4527de3c4 100644 --- a/components/funder/src/report.rs +++ b/components/funder/src/report.rs @@ -369,6 +369,7 @@ where } } FunderMutation::SetPaymentReceipt(_) + | FunderMutation::SetPaymentNumTransactions(_) | FunderMutation::TakePaymentReceipt(_) | FunderMutation::SetPaymentClosing(_) => vec![], } diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index 3f88e7bda..a88cb6e58 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -42,12 +42,12 @@ pub enum ReceiptStatus { #[derive(Clone, Serialize, Deserialize, Debug)] pub struct OpenPayment { /// Is in the process of being closed? - /// Can only be removed if (open_transactions == 0) && receipt_status != ReceiptStatus::Pending(Receipt) + /// Can only be removed if (num_transactions == 0) && receipt_status != ReceiptStatus::Pending(Receipt) pub is_closing: bool, /// Remote invoice id being paid pub invoice_id: InvoiceId, /// The amount of ongoing transactions for this payment. - pub open_transactions: usize, + pub num_transactions: u64, /// Total amount of credits we want to pay to seller. pub total_dest_payment: u128, /// Seller's public key: @@ -57,7 +57,7 @@ pub struct OpenPayment { pub receipt_status: ReceiptStatus, } -// TODO: If a receipt is requested and OpenPayment.open_transactions == 0, it should reported that no receipt +// TODO: If a receipt is requested and OpenPayment.num_transactions == 0, it should reported that no receipt // exists and the payment should be removed. /// A local invoice in progress @@ -105,6 +105,7 @@ pub enum FunderMutation { SetPaymentReceipt((PaymentId, Receipt)), TakePaymentReceipt(PaymentId), SetPaymentClosing(PaymentId), + SetPaymentNumTransactions((PaymentId, u64)), // (payment_id, num_transactions) RemovePayment(PaymentId), } @@ -198,7 +199,7 @@ where let open_payment = OpenPayment { is_closing: false, invoice_id: invoice_id.clone(), - open_transactions: 0, + num_transactions: 0, total_dest_payment: *total_dest_payment, dest_public_key: dest_public_key.clone(), receipt_status: ReceiptStatus::Empty, @@ -224,6 +225,12 @@ where FunderMutation::SetPaymentClosing(payment_id) => { self.open_payments.get_mut(payment_id).unwrap().is_closing = true; } + FunderMutation::SetPaymentNumTransactions((payment_id, num_transactions)) => { + self.open_payments + .get_mut(payment_id) + .unwrap() + .num_transactions = *num_transactions; + } FunderMutation::RemovePayment(payment_id) => { self.open_payments.remove(payment_id); } From e99f7c42461ecda2476abb7c43f166cdcddf1fc2 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 22 May 2019 17:54:17 +0300 Subject: [PATCH 098/248] Removed last unimplemented macro line in control_create_transaction_inner(). --- components/funder/src/handler/handle_control.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 2c586b2d7..7414ef001 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -783,7 +783,7 @@ where // Signal the sender to attempt to send: send_commands.set_try_send(&friend_public_key); - unimplemented!(); + Ok(()) } fn control_create_transaction( From b6e7c574817b0c745675df1b55b7be6b408a0d98 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 22 May 2019 19:28:20 +0300 Subject: [PATCH 099/248] Renamed ResponseReceived -> TransactionResult. --- components/funder/src/handler/canceler.rs | 20 +++++------ .../funder/src/handler/handle_control.rs | 33 +++++++++++++++---- .../funder/src/handler/handle_friend.rs | 16 +++++---- components/funder/src/handler/sender.rs | 8 ++--- components/funder/src/tests/utils.rs | 14 ++++---- components/proto/src/app_server/messages.rs | 6 ++-- components/proto/src/app_server/serialize.rs | 17 +++++++--- components/proto/src/funder/messages.rs | 9 ++--- 8 files changed, 76 insertions(+), 47 deletions(-) diff --git a/components/funder/src/handler/canceler.rs b/components/funder/src/handler/canceler.rs index dcbd47ca6..15906eebc 100644 --- a/components/funder/src/handler/canceler.rs +++ b/components/funder/src/handler/canceler.rs @@ -3,7 +3,7 @@ use crypto::identity::PublicKey; use std::fmt::Debug; use proto::funder::messages::{ - FunderOutgoingControl, RequestSendFundsOp, ResponseReceived, ResponseSendFundsResult, + FunderOutgoingControl, RequestResult, RequestSendFundsOp, TransactionResult, }; use crate::handler::sender::SendCommands; @@ -80,11 +80,11 @@ pub fn cancel_local_pending_transactions( None => { // We are the origin of this request. // We send a cancel message through the control: - let response_received = ResponseReceived { + let transaction_result = TransactionResult { request_id: pending_local_transaction.request_id, - result: ResponseSendFundsResult::Failure, + result: RequestResult::Failure, }; - outgoing_control.push(FunderOutgoingControl::ResponseReceived(response_received)); + outgoing_control.push(FunderOutgoingControl::TransactionResult(transaction_result)); } }; } @@ -123,11 +123,11 @@ pub fn cancel_pending_requests( } None => { // We are the origin of this request: - let response_received = ResponseReceived { + let transaction_result = TransactionResult { request_id: pending_request.request_id, - result: ResponseSendFundsResult::Failure, + result: RequestResult::Failure, }; - outgoing_control.push(FunderOutgoingControl::ResponseReceived(response_received)); + outgoing_control.push(FunderOutgoingControl::TransactionResult(transaction_result)); } }; } @@ -150,10 +150,10 @@ pub fn cancel_pending_user_requests( m_state.mutate(funder_mutation); // We are the origin of this request: - let response_received = ResponseReceived { + let transaction_result = TransactionResult { request_id: pending_user_request.request_id, - result: ResponseSendFundsResult::Failure, + result: RequestResult::Failure, }; - outgoing_control.push(FunderOutgoingControl::ResponseReceived(response_received)); + outgoing_control.push(FunderOutgoingControl::TransactionResult(transaction_result)); } } diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 7414ef001..d2274300e 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -12,9 +12,9 @@ use crate::state::{FunderMutation, ReceiptStatus}; use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ AddFriend, ChannelerUpdateFriend, CreatePayment, CreateTransaction, FriendStatus, - FunderControl, FunderOutgoingControl, ReceiptAck, RemoveFriend, RequestSendFundsOp, - ResetFriendChannel, ResponseReceived, ResponseSendFundsResult, SetFriendName, SetFriendRate, - SetFriendRelays, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, + FunderControl, FunderOutgoingControl, ReceiptAck, RemoveFriend, RequestResult, + RequestSendFundsOp, ResetFriendChannel, SetFriendName, SetFriendRate, SetFriendRelays, + SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, TransactionResult, UserRequestSendFunds, }; @@ -799,10 +799,29 @@ where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, R: CryptoRandom, { - // TODO: - // - Call inner version of the function - // - Send outgoing control with error if any error occurs in any of the above - unimplemented!(); + // If we managed to push the message, we return an Ok(()). + // Otherwise, we return the internal error and return a response failure message. + if let Err(e) = control_create_transaction_inner( + m_state, + ephemeral, + outgoing_control, + send_commands, + rng, + max_pending_user_requests, + create_transaction.clone(), + ) { + error!("control_create_transaction_inner() failed: {:?}", e); + let transaction_result = TransactionResult { + request_id: create_transaction.request_id, + result: RequestResult::Failure, + }; + + outgoing_control.push(FunderOutgoingControl::TransactionResult(transaction_result)); + } + + // Every CreateTransaction must have a matching response. Therefore we don't return an error + // here. We have to make sure the response arrives back to the user. + Ok(()) } pub fn handle_control_message( diff --git a/components/funder/src/handler/handle_friend.rs b/components/funder/src/handler/handle_friend.rs index 186a5cb35..955cd3b4d 100644 --- a/components/funder/src/handler/handle_friend.rs +++ b/components/funder/src/handler/handle_friend.rs @@ -7,8 +7,8 @@ use crypto::identity::{PublicKey, Signature, SIGNATURE_LEN}; use proto::app_server::messages::RelayAddress; use proto::funder::messages::{ CancelSendFundsOp, ChannelerUpdateFriend, CollectSendFundsOp, FriendMessage, - FunderOutgoingControl, MoveTokenRequest, PendingTransaction, RequestSendFundsOp, ResetTerms, - ResponseReceived, ResponseSendFundsOp, ResponseSendFundsResult, + FunderOutgoingControl, MoveTokenRequest, PendingTransaction, RequestResult, RequestSendFundsOp, + ResetTerms, ResponseSendFundsOp, TransactionResult, }; use proto::funder::signature_buff::verify_move_token; @@ -254,11 +254,13 @@ fn handle_cancel_send_funds( // We are the origin of this request, and we got a cancellation. // We should pass it back to encryptor. - let response_send_funds_result = ResponseSendFundsResult::Failure; - outgoing_control.push(FunderOutgoingControl::ResponseReceived(ResponseReceived { - request_id: pending_transaction.request_id, - result: response_send_funds_result, - })); + let request_result = RequestResult::Failure; + outgoing_control.push(FunderOutgoingControl::TransactionResult( + TransactionResult { + request_id: pending_transaction.request_id, + result: request_result, + }, + )); } Some(friend_public_key) => { // Queue this failure message to another token channel: diff --git a/components/funder/src/handler/sender.rs b/components/funder/src/handler/sender.rs index 24a0c8501..aa42abee1 100644 --- a/components/funder/src/handler/sender.rs +++ b/components/funder/src/handler/sender.rs @@ -10,7 +10,7 @@ use crypto::hash_lock::PlainLock; use proto::app_server::messages::RelayAddress; use proto::funder::messages::{ ChannelerUpdateFriend, FriendMessage, FriendTcOp, FunderOutgoingControl, MoveTokenRequest, - RequestsStatus, ResponseReceived, ResponseSendFundsResult, + RequestsStatus, TransactionResult, RequestResult }; use identity::IdentityClient; @@ -483,11 +483,11 @@ where } None => { // We are the origin of this request - let response_received = ResponseReceived { + let transaction_result = TransactionResult { request_id: request_send_funds.request_id, - result: ResponseSendFundsResult::Failure, + result: RequestResult::Failure, }; - outgoing_control.push(FunderOutgoingControl::ResponseReceived(response_received)); + outgoing_control.push(FunderOutgoingControl::TransactionResult(transaction_result)); } } diff --git a/components/funder/src/tests/utils.rs b/components/funder/src/tests/utils.rs index 1ff818f0d..bf2564839 100644 --- a/components/funder/src/tests/utils.rs +++ b/components/funder/src/tests/utils.rs @@ -22,7 +22,7 @@ use proto::report::messages::{ use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ AddFriend, FriendStatus, FunderControl, FunderIncomingControl, FunderOutgoingControl, - RequestsStatus, ResponseReceived, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, + RequestsStatus, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, TransactionResult }; use database::DatabaseClient; @@ -206,7 +206,7 @@ pub struct NodeControl { #[derive(Debug)] pub enum NodeRecv { ReportMutations(FunderReportMutations), - ResponseReceived(ResponseReceived), + TransactionResult(TransactionResult), } impl NodeControl @@ -226,8 +226,8 @@ where } Some(NodeRecv::ReportMutations(funder_report_mutations)) } - FunderOutgoingControl::ResponseReceived(response_received) => { - Some(NodeRecv::ResponseReceived(response_received)) + FunderOutgoingControl::TransactionResult(transaction_result) => { + Some(NodeRecv::TransactionResult(transaction_result)) } } } @@ -239,16 +239,16 @@ where while !predicate(&self.report) { match await!(self.recv()).unwrap() { NodeRecv::ReportMutations(_) => {} - NodeRecv::ResponseReceived(_) => unreachable!(), + NodeRecv::TransactionResult(_) => unreachable!(), }; } } - pub async fn recv_until_response(&mut self) -> Option { + pub async fn recv_until_response(&mut self) -> Option { loop { match await!(self.recv())? { NodeRecv::ReportMutations(_) => {} - NodeRecv::ResponseReceived(response_received) => return Some(response_received), + NodeRecv::TransactionResult(transaction_result) => return Some(transaction_result), }; } } diff --git a/components/proto/src/app_server/messages.rs b/components/proto/src/app_server/messages.rs index adc5e97e8..42ca20fcf 100644 --- a/components/proto/src/app_server/messages.rs +++ b/components/proto/src/app_server/messages.rs @@ -4,8 +4,8 @@ use crypto::identity::PublicKey; use crypto::uid::Uid; use crate::funder::messages::{ - AddFriend, ReceiptAck, ResetFriendChannel, ResponseReceived, SetFriendName, SetFriendRelays, - SetFriendRemoteMaxDebt, UserRequestSendFunds, + AddFriend, ReceiptAck, ResetFriendChannel, SetFriendName, SetFriendRelays, + SetFriendRemoteMaxDebt, TransactionResult, UserRequestSendFunds, }; use crate::index_client::messages::{ ClientResponseRoutes, IndexClientReport, IndexClientReportMutation, @@ -82,7 +82,7 @@ where B: Clone, { /// Funds: - ResponseReceived(ResponseReceived), + TransactionResult(TransactionResult), /// Reports about current state: Report(NodeReport), ReportMutations(ReportMutations), diff --git a/components/proto/src/app_server/serialize.rs b/components/proto/src/app_server/serialize.rs index 97a1d6dc0..65b747063 100644 --- a/components/proto/src/app_server/serialize.rs +++ b/components/proto/src/app_server/serialize.rs @@ -25,7 +25,8 @@ use index_server::serialize::{ }; use crate::funder::messages::{ - AddFriend, ReceiptAck, ResetFriendChannel, ResponseReceived, /*ResponseSendFundsResult, */ + AddFriend, ReceiptAck, + ResetFriendChannel, /* ResponseReceived, ResponseSendFundsResult, */ SetFriendName, SetFriendRelays, SetFriendRemoteMaxDebt, UserRequestSendFunds, }; use crate::funder::serialize::{deser_friends_route, ser_friends_route}; @@ -76,12 +77,12 @@ fn deser_user_request_send_funds( */ } +/* fn ser_response_received( response_received: &ResponseReceived, response_received_builder: &mut app_server_capnp::response_received::Builder, ) { unimplemented!(); - /* write_uid( &response_received.request_id, &mut response_received_builder.reborrow().init_request_id(), @@ -98,14 +99,14 @@ fn ser_response_received( write_public_key(public_key, &mut failure_builder); } }; - */ } +*/ +/* fn deser_response_received( response_received_reader: &app_server_capnp::response_received::Reader, ) -> Result { unimplemented!(); - /* let result = match response_received_reader.get_result().which()? { app_server_capnp::response_received::result::Success(receipt_reader) => { let receipt_reader = receipt_reader?; @@ -121,8 +122,8 @@ fn deser_response_received( request_id: read_uid(&response_received_reader.get_request_id()?)?, result, }) - */ } +*/ fn ser_receipt_ack( receipt_ack: &ReceiptAck, @@ -458,6 +459,8 @@ fn ser_app_server_to_app( app_server_to_app: &AppServerToApp, app_server_to_app_builder: &mut app_server_capnp::app_server_to_app::Builder, ) { + unimplemented!(); + /* match app_server_to_app { AppServerToApp::ResponseReceived(response_received) => ser_response_received( response_received, @@ -478,11 +481,14 @@ fn ser_app_server_to_app( &mut app_server_to_app_builder.reborrow().init_response_routes(), ), } + */ } fn deser_app_server_to_app( app_server_to_app_reader: &app_server_capnp::app_server_to_app::Reader, ) -> Result { + unimplemented!(); + /* Ok(match app_server_to_app_reader.which()? { app_server_capnp::app_server_to_app::ResponseReceived(response_received_reader) => { AppServerToApp::ResponseReceived(deser_response_received(&response_received_reader?)?) @@ -499,6 +505,7 @@ fn deser_app_server_to_app( )?) } }) + */ } fn ser_app_request( diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index a46e0105a..af76dd3ea 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -584,20 +584,21 @@ impl UserRequestSendFunds { } #[derive(Debug, Clone, PartialEq, Eq)] -pub enum ResponseSendFundsResult { +pub enum RequestResult { + // TODO: Should not contain Receipt. Success(Receipt), // TODO: Should we add more information to the failure here? Failure, } #[derive(Debug, Clone, PartialEq, Eq)] -pub struct ResponseReceived { +pub struct TransactionResult { pub request_id: Uid, - pub result: ResponseSendFundsResult, + pub result: RequestResult, } #[derive(Debug)] pub enum FunderOutgoingControl { - ResponseReceived(ResponseReceived), + TransactionResult(TransactionResult), ReportMutations(FunderReportMutations), } From 932a766c849eaa4c48e1fbc0909c4b092f32b6be Mon Sep 17 00:00:00 2001 From: real Date: Thu, 23 May 2019 12:09:11 +0300 Subject: [PATCH 100/248] docs: Refactored MultiCommit to contain repeating values from Commit. --- doc/docs/atomic_proposal.md | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/doc/docs/atomic_proposal.md b/doc/docs/atomic_proposal.md index 52d09c798..495ea7e36 100644 --- a/doc/docs/atomic_proposal.md +++ b/doc/docs/atomic_proposal.md @@ -287,7 +287,7 @@ This is the structure of the request message: struct RequestSendFundsOp { requestId @0: Uid; # Randomly generated reqeustId [128 bits] - srcHashedLock @1: Hash; + srcHashedLock @1: HashedLock; # bcrypt(srcPlainLock), where srcPlainLock is of size 256 bits. route @2: FriendsRoute; # A route of friends that leads to the destination @@ -334,7 +334,7 @@ B --- C --- D --- E ```capnp struct ResponseSendFundsOp { requestId @0: Uid; - destHashedLock @1: Lock; + destHashedLock @1: HashedLock; randNonce @2: RandNonce; signature @3: Signature; # Signature{key=destinationKey}( @@ -408,10 +408,11 @@ struct Commit { responseHash @0: Hash; # = sha512/256(requestId || sha512/256(route) || randNonce) destPayment @1: CustomUInt128; - totalDestPayment @2: CustomUInt128; - srcPlainLock: @3: Lock; + # Amount of credits paid in this Transaction. + srcPlainLock @2: PlainLock; # The preimage of the hashedLock at the request message [256 bits] - signature @4: Signature; + destHashedLock @3: HashedLock; + signature @3: Signature; # Signature{key=destinationKey}( # sha512/256("FUNDS_RESPONSE") || # sha512/256(requestId || sha512/256(route) || randNonce) || @@ -426,7 +427,9 @@ struct Commit { struct MultiCommit { invoiceId @0: InvoiceId; # InvoiceId being paid. - commits @1: List(Commit); + totalDestPayment @1: CustomUInt128; + # The total amount being paid + commits @2: List(Commit); # A list of confirmations. Each confirmation corresponds to a request # sent along one route. } @@ -468,8 +471,8 @@ all the way (along the original route) to the source of the payment. ```capnp struct CollectSendFundsOp { requestId @0: Uid; - srcPlainLock @1: Lock; - destPlainLock @2: Lock; + srcPlainLock @1: PlainLock; + destPlainLock @2: PlainLock; } ``` @@ -493,8 +496,8 @@ struct Receipt { responseHash @0: Hash; # = sha512/256(requestId || sha512/256(route) || randNonce) invoiceId @1: InvoiceId; - srcPlainLock @2: Lock; - destPlainLock @3: Lock; + srcPlainLock @2: PlainLock; + destPlainLock @3: PlainLock; destPayment @4: CustomUInt128; destTotalPayment @4: CustomUInt128; signature @5: Signature; From 1e62b587ade849f1f9b6c10c54b2f9c85bb161b7 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 23 May 2019 12:11:29 +0300 Subject: [PATCH 101/248] RequestResult returns a (single) Commit on success. --- components/proto/src/funder/messages.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index af76dd3ea..5f469dc41 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -86,17 +86,19 @@ pub struct CancelSendFundsOp { } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] -pub struct Commit { +pub struct Commit { response_hash: HashResult, dest_payment: u128, src_plain_lock: PlainLock, - signature: S, + dest_hashed_lock: HashedLock, + signature: Signature, } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] -pub struct MultiCommit { +pub struct MultiCommit { invoice_id: InvoiceId, - confirmations: Vec>, + total_dest_payment: u128, + commits: Vec, } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] @@ -585,8 +587,7 @@ impl UserRequestSendFunds { #[derive(Debug, Clone, PartialEq, Eq)] pub enum RequestResult { - // TODO: Should not contain Receipt. - Success(Receipt), + Success(Commit), // TODO: Should we add more information to the failure here? Failure, } From 2bd2f6bfdd0ced37ff76433babbfeac13d694c74 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 23 May 2019 12:56:32 +0300 Subject: [PATCH 102/248] Updated payments closing related Funder API. --- components/funder/src/handler/handle_control.rs | 5 +++-- components/funder/src/tests/utils.rs | 8 +++++++- components/proto/src/funder/messages.rs | 12 ++++++++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index d2274300e..18ebe7354 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -5,6 +5,7 @@ use common::canonical_serialize::CanonicalSerialize; use crypto::crypto_rand::CryptoRandom; use crypto::hash_lock::PlainLock; use crypto::identity::PublicKey; +use crypto::payment_id::PaymentId; use crate::friend::{ChannelStatus, FriendMutation}; use crate::state::{FunderMutation, ReceiptStatus}; @@ -915,8 +916,8 @@ where max_pending_user_requests, create_transaction, ), - FunderControl::RequestReceipt(_payment_id) => unimplemented!(), - FunderControl::RemoveReceipt(_payment_id) => unimplemented!(), + FunderControl::RequestClosePayment(_payment_id) => unimplemented!(), + FunderControl::AckClosePayment((_payment_id, _uid)) => unimplemented!(), // Seller API: FunderControl::AddInvoice(_invoice_id) => unimplemented!(), diff --git a/components/funder/src/tests/utils.rs b/components/funder/src/tests/utils.rs index bf2564839..96ccf991c 100644 --- a/components/funder/src/tests/utils.rs +++ b/components/funder/src/tests/utils.rs @@ -22,7 +22,7 @@ use proto::report::messages::{ use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ AddFriend, FriendStatus, FunderControl, FunderIncomingControl, FunderOutgoingControl, - RequestsStatus, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, TransactionResult + RequestsStatus, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, TransactionResult, ResponseClosePayment, }; use database::DatabaseClient; @@ -206,6 +206,7 @@ pub struct NodeControl { #[derive(Debug)] pub enum NodeRecv { ReportMutations(FunderReportMutations), + ResponseClosePayment(ResponseClosePayment), TransactionResult(TransactionResult), } @@ -226,6 +227,9 @@ where } Some(NodeRecv::ReportMutations(funder_report_mutations)) } + FunderOutgoingControl::ResponseClosePayment(response_close_payment) => { + Some(NodeRecv::ResponseClosePayment(response_close_payment)) + } FunderOutgoingControl::TransactionResult(transaction_result) => { Some(NodeRecv::TransactionResult(transaction_result)) } @@ -240,6 +244,7 @@ where match await!(self.recv()).unwrap() { NodeRecv::ReportMutations(_) => {} NodeRecv::TransactionResult(_) => unreachable!(), + NodeRecv::ResponseClosePayment(_) => unreachable!(), }; } } @@ -249,6 +254,7 @@ where match await!(self.recv())? { NodeRecv::ReportMutations(_) => {} NodeRecv::TransactionResult(transaction_result) => return Some(transaction_result), + NodeRecv::ResponseClosePayment(_) => {} }; } } diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 5f469dc41..2b7ddb6d4 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -540,8 +540,8 @@ pub enum FunderControl { // Buyer API: CreatePayment(CreatePayment), CreateTransaction(CreateTransaction), // TODO - RequestReceipt(PaymentId), - RemoveReceipt(PaymentId), + RequestClosePayment(PaymentId), + AckClosePayment((PaymentId, Uid)), // (payment_id, ack_id) // Seller API: AddInvoice(InvoiceId), CancelInvoice(InvoiceId), @@ -598,8 +598,16 @@ pub struct TransactionResult { pub result: RequestResult, } +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum ResponseClosePayment { + InProgress, // Can not be acked + Success((Receipt, Uid)), // (Receipt, ack_id) + Canceled(Uid), // ack_id +} + #[derive(Debug)] pub enum FunderOutgoingControl { TransactionResult(TransactionResult), + ResponseClosePayment(ResponseClosePayment), ReportMutations(FunderReportMutations), } From b4ce646013d87a20fe462ae8112b288d04268e32 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 23 May 2019 13:39:55 +0300 Subject: [PATCH 103/248] An idea for Payment design. Not compiling yet. --- components/funder/src/state.rs | 86 +++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 21 deletions(-) diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index a88cb6e58..a81b7c954 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -26,7 +26,7 @@ pub struct FunderState { /// Locally created transaction in progress. (For which this node is the buyer). pub open_transactions: ImHashMap, /// Ongoing payments (For which this node is the buyer): - pub open_payments: ImHashMap, + pub payments: ImHashMap, } #[derive(Clone, Serialize, Deserialize, Debug)] @@ -39,22 +39,38 @@ pub enum ReceiptStatus { Taken, } +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct Payment { + /// The amount of ongoing transactions for this payment. + pub num_transactions: u64, + pub payment_status: PaymentStatus, +} + +#[derive(Clone, Serialize, Deserialize, Debug)] +pub enum PaymentStatus { + /// User can add new transactions + Open(OpenPayment), + /// User can not add new transactions + /// (Either user has called request called, or a receipt was received) + Closed(ClosedPayment), +} + #[derive(Clone, Serialize, Deserialize, Debug)] pub struct OpenPayment { - /// Is in the process of being closed? - /// Can only be removed if (num_transactions == 0) && receipt_status != ReceiptStatus::Pending(Receipt) - pub is_closing: bool, /// Remote invoice id being paid pub invoice_id: InvoiceId, - /// The amount of ongoing transactions for this payment. - pub num_transactions: u64, /// Total amount of credits we want to pay to seller. pub total_dest_payment: u128, /// Seller's public key: pub dest_public_key: PublicKey, - /// The first receipt that was received for this payment. (Generated after a CollectSendFundsOp - /// message was received successfuly). - pub receipt_status: ReceiptStatus, +} + +pub enum ClosePayment { + InProgress, + Success((Receipt, Uid)), // (Receipt, ack_uid) + Canceled(Uid), // ack_uid + AfterAck, // User already acked. + // We now wait for the remaining transactions to finish. } // TODO: If a receipt is requested and OpenPayment.num_transactions == 0, it should reported that no receipt @@ -123,7 +139,7 @@ where friends: ImHashMap::new(), open_invoices: ImHashMap::new(), open_transactions: ImHashMap::new(), - open_payments: ImHashMap::new(), + payments: ImHashMap::new(), } } @@ -197,42 +213,70 @@ where dest_public_key, )) => { let open_payment = OpenPayment { - is_closing: false, invoice_id: invoice_id.clone(), num_transactions: 0, total_dest_payment: *total_dest_payment, dest_public_key: dest_public_key.clone(), - receipt_status: ReceiptStatus::Empty, }; - let _ = self.open_payments.insert(payment_id.clone(), open_payment); + let _ = self + .payments + .insert(payment_id.clone(), Payment::Open(open_payment)); } FunderMutation::SetPaymentReceipt((payment_id, receipt)) => { - let open_payment = self.open_payments.get_mut(payment_id).unwrap(); - if let ReceiptStatus::Empty = open_payment.receipt_status { - open_payment.receipt_status = ReceiptStatus::Pending(receipt.clone()); - } else { - unreachable!(); - } + let payment = self.payments.remove(payment_id).unwrap(); + let closed_payment = match payment { + Payment::Open(open_payment) => ClosedPayment { + opt_ack_uid: None, + num_transactions: closed_payment.num_transactions, + receipt_status: ReceiptStatus::Pending(receipt.clone()), + }, + Payment::Closed(closed_payment) => { + if let ReceiptStatus::Empty = closed_payment.receipt_status { + closed_payment.receipt_status = ReceiptStatus::Pending(receipt.clone()); + } else { + unreachable!(); + } + closed_payment + } + }; + let _ = self + .payments + .insert(payment_id.clone(), Payment::Closed(closed_payment)); } FunderMutation::TakePaymentReceipt(payment_id) => { - let open_payment = self.open_payments.get_mut(payment_id).unwrap(); + unimplemented!(); + /* + let payment = self.payments.get_mut(payment_id).unwrap(); + let open_payment = if let Payment::Open(open_payment) = payment { + open_payment + } else { + unreachable!(); + }; if let ReceiptStatus::Pending(_) = &open_payment.receipt_status { open_payment.receipt_status = ReceiptStatus::Taken; } else { unreachable!(); } + */ } FunderMutation::SetPaymentClosing(payment_id) => { - self.open_payments.get_mut(payment_id).unwrap().is_closing = true; + unimplemented!(); + // self.open_payments.get_mut(payment_id).unwrap().is_closing = true; } FunderMutation::SetPaymentNumTransactions((payment_id, num_transactions)) => { + unimplemented!(); + /* self.open_payments .get_mut(payment_id) .unwrap() .num_transactions = *num_transactions; + */ } FunderMutation::RemovePayment(payment_id) => { + unimplemented!(); + /* self.open_payments.remove(payment_id); + */ } } } From 5ec37bdc52ed5dbf74acafe5676daefb39532faa Mon Sep 17 00:00:00 2001 From: real Date: Thu, 23 May 2019 13:46:07 +0300 Subject: [PATCH 104/248] Updated comment. Funder not yet compiling. --- components/funder/src/state.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index a81b7c954..3dc8651ab 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -51,7 +51,8 @@ pub enum PaymentStatus { /// User can add new transactions Open(OpenPayment), /// User can not add new transactions - /// (Either user has called request called, or a receipt was received) + /// (Either user has called RequestClosePayment, or a receipt was received from a Collect + /// message along the route) Closed(ClosedPayment), } From c8ff1e3e8d590f0210f529578941d9397623687c Mon Sep 17 00:00:00 2001 From: real Date: Thu, 23 May 2019 13:57:16 +0300 Subject: [PATCH 105/248] Some work on Payment design. Not done yet. Not compiling. --- components/funder/src/state.rs | 49 +++++++--------------------------- 1 file changed, 9 insertions(+), 40 deletions(-) diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index 3dc8651ab..a1fca2308 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -30,48 +30,17 @@ pub struct FunderState { } #[derive(Clone, Serialize, Deserialize, Debug)] -pub enum ReceiptStatus { - /// Haven't got a receipt yet - Empty, - /// We Have a pending receipt, waiting to be handed to the user. - Pending(Receipt), - /// A receipt was already received and handed over to user. - Taken, -} - -#[derive(Clone, Serialize, Deserialize, Debug)] -pub struct Payment { - /// The amount of ongoing transactions for this payment. - pub num_transactions: u64, - pub payment_status: PaymentStatus, -} - -#[derive(Clone, Serialize, Deserialize, Debug)] -pub enum PaymentStatus { +pub enum Payment { /// User can add new transactions - Open(OpenPayment), - /// User can not add new transactions - /// (Either user has called RequestClosePayment, or a receipt was received from a Collect - /// message along the route) - Closed(ClosedPayment), -} - -#[derive(Clone, Serialize, Deserialize, Debug)] -pub struct OpenPayment { - /// Remote invoice id being paid - pub invoice_id: InvoiceId, - /// Total amount of credits we want to pay to seller. - pub total_dest_payment: u128, - /// Seller's public key: - pub dest_public_key: PublicKey, -} - -pub enum ClosePayment { - InProgress, - Success((Receipt, Uid)), // (Receipt, ack_uid) + Transactions((u64, InvoiceId, u128, PublicKey)) // (num_transactions, invoice_id, total_dest_payment, dest_public_key) + /// User can no longer add new transactions (user sent a RequestClosePayment) + InProgress(u64), // num_transactions + /// A receipt was received: + Success((u64, Receipt, Uid)), // (num_transactions, Receipt, ack_uid) + /// The payment will not complete, because all transactions were canceled: Canceled(Uid), // ack_uid - AfterAck, // User already acked. - // We now wait for the remaining transactions to finish. + /// User already acked, We now wait for the remaining transactions to finish. + AfterSuccessAck(u64), // num_transactions } // TODO: If a receipt is requested and OpenPayment.num_transactions == 0, it should reported that no receipt From 62850a689ae79d470990beb2deb8de147e5fde4a Mon Sep 17 00:00:00 2001 From: real Date: Thu, 23 May 2019 15:30:18 +0300 Subject: [PATCH 106/248] Updated Payment to be an enum of states. --- .../funder/src/handler/handle_control.rs | 58 +++++------- components/funder/src/report.rs | 14 +-- components/funder/src/state.rs | 92 +++---------------- components/proto/src/report/convert.rs | 2 +- components/proto/src/report/messages.rs | 8 +- 5 files changed, 50 insertions(+), 124 deletions(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 18ebe7354..5d28e5144 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -8,7 +8,7 @@ use crypto::identity::PublicKey; use crypto::payment_id::PaymentId; use crate::friend::{ChannelStatus, FriendMutation}; -use crate::state::{FunderMutation, ReceiptStatus}; +use crate::state::{FunderMutation, NewTransactions, Payment}; use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ @@ -46,6 +46,7 @@ pub enum HandleControlError { PaymentAlreadyOpen, OpenPaymentNotFound, PaymentReceiptNotEmpty, + NewTransactionsNotAllowed, } fn control_set_friend_remote_max_debt( @@ -632,19 +633,21 @@ where // Make sure that a payment with the same payment_id doesn't exist: if m_state .state() - .open_payments + .payments .contains_key(&create_payment.payment_id) { return Err(HandleControlError::PaymentAlreadyOpen); } + let payment = Payment::NewTransactions(NewTransactions { + num_transactions: 0, + invoice_id: create_payment.invoice_id.clone(), + total_dest_payment: create_payment.total_dest_payment, + dest_public_key: create_payment.dest_public_key.clone(), + }); + // Add a new payment entry: - let m_mutation = FunderMutation::AddPayment(( - create_payment.payment_id, - create_payment.invoice_id, - create_payment.total_dest_payment, - create_payment.dest_public_key, - )); + let m_mutation = FunderMutation::UpdatePayment((create_payment.payment_id, payment)); m_state.mutate(m_mutation); Ok(()) @@ -664,19 +667,17 @@ where R: CryptoRandom, { // Make sure that a corresponding payment is open - let open_payment = m_state + let payment = m_state .state() - .open_payments + .payments .get(&create_transaction.payment_id) .ok_or(HandleControlError::OpenPaymentNotFound)?; - // If we already have a receipt or the payment is closed, do not allow creating a new - // transaction: - if let ReceiptStatus::Empty = open_payment.receipt_status { - // Good case + let new_transactions = if let Payment::NewTransactions(new_transactions) = payment { + new_transactions.clone() } else { - return Err(HandleControlError::PaymentReceiptNotEmpty)?; - } + return Err(HandleControlError::NewTransactionsNotAllowed)?; + }; let route = &create_transaction.route; @@ -746,34 +747,25 @@ where )); m_state.mutate(funder_mutation); - let open_payment = m_state - .state() - .open_payments - .get(&create_transaction.payment_id) - .unwrap(); - // Update OpenPayment (Increase open transactions count): - let new_num_transactions = open_payment.num_transactions.checked_add(1).unwrap(); - let funder_mutation = FunderMutation::SetPaymentNumTransactions(( + let mut updated_new_transactions = new_transactions.clone(); + updated_new_transactions.num_transactions = + new_transactions.num_transactions.checked_add(1).unwrap(); + + let funder_mutation = FunderMutation::UpdatePayment(( create_transaction.payment_id, - new_num_transactions, + Payment::NewTransactions(updated_new_transactions), )); m_state.mutate(funder_mutation); - let open_payment = m_state - .state() - .open_payments - .get(&create_transaction.payment_id) - .unwrap(); - // Push the request: let request_send_funds = RequestSendFundsOp { request_id: create_transaction.request_id, src_hashed_lock: src_plain_lock.hash(), route: create_transaction.route, dest_payment: create_transaction.dest_payment, - total_dest_payment: open_payment.total_dest_payment, - invoice_id: open_payment.invoice_id.clone(), + total_dest_payment: new_transactions.total_dest_payment, + invoice_id: new_transactions.invoice_id.clone(), left_fees: create_transaction.fees, }; diff --git a/components/funder/src/report.rs b/components/funder/src/report.rs index 4527de3c4..23dd7e4fd 100644 --- a/components/funder/src/report.rs +++ b/components/funder/src/report.rs @@ -176,7 +176,7 @@ where relays: funder_state.relays.clone(), friends, num_open_invoices: usize_to_u64(funder_state.open_invoices.len()).unwrap(), - num_open_payments: usize_to_u64(funder_state.open_payments.len()).unwrap(), + num_payments: usize_to_u64(funder_state.payments.len()).unwrap(), num_open_transactions: usize_to_u64(funder_state.open_transactions.len()).unwrap(), } } @@ -359,19 +359,15 @@ where Vec::new() } } - FunderMutation::AddPayment(_) | FunderMutation::RemovePayment(_) => { - if funder_state_after.open_payments.len() != funder_state.open_payments.len() { - vec![FunderReportMutation::SetNumOpenPayments( - usize_to_u64(funder_state_after.open_payments.len()).unwrap(), + FunderMutation::UpdatePayment(_) | FunderMutation::RemovePayment(_) => { + if funder_state_after.payments.len() != funder_state.payments.len() { + vec![FunderReportMutation::SetNumPayments( + usize_to_u64(funder_state_after.payments.len()).unwrap(), )] } else { Vec::new() } } - FunderMutation::SetPaymentReceipt(_) - | FunderMutation::SetPaymentNumTransactions(_) - | FunderMutation::TakePaymentReceipt(_) - | FunderMutation::SetPaymentClosing(_) => vec![], } } diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index a1fca2308..6deea9588 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -29,18 +29,26 @@ pub struct FunderState { pub payments: ImHashMap, } +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct NewTransactions { + pub num_transactions: u64, + pub invoice_id: InvoiceId, + pub total_dest_payment: u128, + pub dest_public_key: PublicKey, +} + #[derive(Clone, Serialize, Deserialize, Debug)] pub enum Payment { /// User can add new transactions - Transactions((u64, InvoiceId, u128, PublicKey)) // (num_transactions, invoice_id, total_dest_payment, dest_public_key) + NewTransactions(NewTransactions), /// User can no longer add new transactions (user sent a RequestClosePayment) InProgress(u64), // num_transactions /// A receipt was received: Success((u64, Receipt, Uid)), // (num_transactions, Receipt, ack_uid) /// The payment will not complete, because all transactions were canceled: - Canceled(Uid), // ack_uid + Canceled(Uid), // ack_uid /// User already acked, We now wait for the remaining transactions to finish. - AfterSuccessAck(u64), // num_transactions + AfterSuccessAck(u64), // num_transactions } // TODO: If a receipt is requested and OpenPayment.num_transactions == 0, it should reported that no receipt @@ -87,11 +95,7 @@ pub enum FunderMutation { RemoveInvoice(InvoiceId), AddTransaction((Uid, PaymentId, PlainLock)), // (transaction_id, payment_id,src_plain_lock) RemoveTransaction(Uid), // transaction_id - AddPayment((PaymentId, InvoiceId, u128, PublicKey)), // (payment_id, invoice_id, total_dest_payment, destination) - SetPaymentReceipt((PaymentId, Receipt)), - TakePaymentReceipt(PaymentId), - SetPaymentClosing(PaymentId), - SetPaymentNumTransactions((PaymentId, u64)), // (payment_id, num_transactions) + UpdatePayment((PaymentId, Payment)), RemovePayment(PaymentId), } @@ -176,77 +180,11 @@ where FunderMutation::RemoveTransaction(transaction_id) => { let _ = self.open_transactions.remove(transaction_id); } - FunderMutation::AddPayment(( - payment_id, - invoice_id, - total_dest_payment, - dest_public_key, - )) => { - let open_payment = OpenPayment { - invoice_id: invoice_id.clone(), - num_transactions: 0, - total_dest_payment: *total_dest_payment, - dest_public_key: dest_public_key.clone(), - }; - let _ = self - .payments - .insert(payment_id.clone(), Payment::Open(open_payment)); - } - FunderMutation::SetPaymentReceipt((payment_id, receipt)) => { - let payment = self.payments.remove(payment_id).unwrap(); - let closed_payment = match payment { - Payment::Open(open_payment) => ClosedPayment { - opt_ack_uid: None, - num_transactions: closed_payment.num_transactions, - receipt_status: ReceiptStatus::Pending(receipt.clone()), - }, - Payment::Closed(closed_payment) => { - if let ReceiptStatus::Empty = closed_payment.receipt_status { - closed_payment.receipt_status = ReceiptStatus::Pending(receipt.clone()); - } else { - unreachable!(); - } - closed_payment - } - }; - let _ = self - .payments - .insert(payment_id.clone(), Payment::Closed(closed_payment)); - } - FunderMutation::TakePaymentReceipt(payment_id) => { - unimplemented!(); - /* - let payment = self.payments.get_mut(payment_id).unwrap(); - let open_payment = if let Payment::Open(open_payment) = payment { - open_payment - } else { - unreachable!(); - }; - if let ReceiptStatus::Pending(_) = &open_payment.receipt_status { - open_payment.receipt_status = ReceiptStatus::Taken; - } else { - unreachable!(); - } - */ - } - FunderMutation::SetPaymentClosing(payment_id) => { - unimplemented!(); - // self.open_payments.get_mut(payment_id).unwrap().is_closing = true; - } - FunderMutation::SetPaymentNumTransactions((payment_id, num_transactions)) => { - unimplemented!(); - /* - self.open_payments - .get_mut(payment_id) - .unwrap() - .num_transactions = *num_transactions; - */ + FunderMutation::UpdatePayment((payment_id, payment)) => { + let _ = self.payments.insert(payment_id.clone(), payment.clone()); } FunderMutation::RemovePayment(payment_id) => { - unimplemented!(); - /* - self.open_payments.remove(payment_id); - */ + let _ = self.payments.remove(payment_id); } } } diff --git a/components/proto/src/report/convert.rs b/components/proto/src/report/convert.rs index 6a44f41c8..49472d433 100644 --- a/components/proto/src/report/convert.rs +++ b/components/proto/src/report/convert.rs @@ -125,7 +125,7 @@ where FunderReportMutation::AddRelay(_) | FunderReportMutation::RemoveRelay(_) | FunderReportMutation::SetNumOpenInvoices(_) - | FunderReportMutation::SetNumOpenPayments(_) + | FunderReportMutation::SetNumPayments(_) | FunderReportMutation::SetNumOpenTransactions(_) => None, FunderReportMutation::AddFriend(add_friend_report) => { create_update_friend(&add_friend_report.friend_public_key) diff --git a/components/proto/src/report/messages.rs b/components/proto/src/report/messages.rs index a10dbd69c..ad5cb2804 100644 --- a/components/proto/src/report/messages.rs +++ b/components/proto/src/report/messages.rs @@ -177,7 +177,7 @@ where pub relays: ImVec>, pub friends: ImHashMap>, pub num_open_invoices: u64, - pub num_open_payments: u64, + pub num_payments: u64, pub num_open_transactions: u64, } @@ -224,7 +224,7 @@ where RemoveFriend(PublicKey), FriendReportMutation((PublicKey, FriendReportMutation)), SetNumOpenInvoices(u64), - SetNumOpenPayments(u64), + SetNumPayments(u64), SetNumOpenTransactions(u64), } @@ -387,8 +387,8 @@ where self.num_open_invoices = *num_open_invoices; Ok(()) } - FunderReportMutation::SetNumOpenPayments(num_open_payments) => { - self.num_open_payments = *num_open_payments; + FunderReportMutation::SetNumPayments(num_payments) => { + self.num_payments = *num_payments; Ok(()) } FunderReportMutation::SetNumOpenTransactions(num_open_transactions) => { From 6504ff48cc5b3c30ccac0becaacc95e6cab63aac Mon Sep 17 00:00:00 2001 From: real Date: Thu, 23 May 2019 15:30:38 +0300 Subject: [PATCH 107/248] Removed old comment. --- components/funder/src/state.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index 6deea9588..41cc8cf6f 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -51,9 +51,6 @@ pub enum Payment { AfterSuccessAck(u64), // num_transactions } -// TODO: If a receipt is requested and OpenPayment.num_transactions == 0, it should reported that no receipt -// exists and the payment should be removed. - /// A local invoice in progress #[derive(Clone, Serialize, Deserialize, Debug)] pub struct OpenInvoice { From 7b664a0d4c854a5d083f691aaefbd8066cc1be95 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 23 May 2019 15:31:07 +0300 Subject: [PATCH 108/248] Added a TODO comment. --- components/funder/src/state.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index 41cc8cf6f..d2c89bdc8 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -40,6 +40,7 @@ pub struct NewTransactions { #[derive(Clone, Serialize, Deserialize, Debug)] pub enum Payment { /// User can add new transactions + // TODO: Think about a better name for this? NewTransactions(NewTransactions), /// User can no longer add new transactions (user sent a RequestClosePayment) InProgress(u64), // num_transactions From 8d6d2c49cfeeeea10f463f09640351bc036de68e Mon Sep 17 00:00:00 2001 From: real Date: Thu, 23 May 2019 15:58:30 +0300 Subject: [PATCH 109/248] Implemented control_request_close_payment(). --- .../funder/src/handler/handle_control.rs | 79 ++++++++++++++++++- components/funder/src/state.rs | 4 +- components/proto/src/funder/messages.rs | 1 + 3 files changed, 78 insertions(+), 6 deletions(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 5d28e5144..c37b05120 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -14,8 +14,8 @@ use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ AddFriend, ChannelerUpdateFriend, CreatePayment, CreateTransaction, FriendStatus, FunderControl, FunderOutgoingControl, ReceiptAck, RemoveFriend, RequestResult, - RequestSendFundsOp, ResetFriendChannel, SetFriendName, SetFriendRate, SetFriendRelays, - SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, TransactionResult, + RequestSendFundsOp, ResetFriendChannel, ResponseClosePayment, SetFriendName, SetFriendRate, + SetFriendRelays, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, TransactionResult, UserRequestSendFunds, }; @@ -45,8 +45,8 @@ pub enum HandleControlError { MaxNodeRelaysReached, PaymentAlreadyOpen, OpenPaymentNotFound, - PaymentReceiptNotEmpty, NewTransactionsNotAllowed, + PaymentDoesNotExist, } fn control_set_friend_remote_max_debt( @@ -817,6 +817,75 @@ where Ok(()) } +fn control_request_close_payment( + m_state: &mut MutableFunderState, + outgoing_control: &mut Vec>, + payment_id: PaymentId, +) -> Result<(), HandleControlError> +where + B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, +{ + let payment = if let Some(payment) = m_state.state().payments.get(&payment_id) { + payment + } else { + // Payment not found: + outgoing_control.push(FunderOutgoingControl::ResponseClosePayment( + ResponseClosePayment::InProgress, + )); + return Ok(()); + }; + + let (opt_new_payment, response_close_payment) = match payment { + Payment::NewTransactions(new_transactions) => ( + Some(Payment::InProgress(new_transactions.num_transactions)), + ResponseClosePayment::InProgress, + ), + Payment::InProgress(num_transactions) => ( + Some(Payment::InProgress(*num_transactions)), + ResponseClosePayment::InProgress, + ), + Payment::Success((num_transactions, receipt, ack_uid)) => ( + Some(Payment::Success(( + *num_transactions, + receipt.clone(), + *ack_uid, + ))), + ResponseClosePayment::Success((receipt.clone(), *ack_uid)), + ), + Payment::Canceled(ack_uid) => ( + Some(Payment::Canceled(*ack_uid)), + ResponseClosePayment::Canceled(*ack_uid), + ), + Payment::AfterSuccessAck(num_transactions) => ( + Some(Payment::AfterSuccessAck(*num_transactions)), + ResponseClosePayment::PaymentNotFound, + ), + }; + + // Send back a ResponseClosePayment: + outgoing_control.push(FunderOutgoingControl::ResponseClosePayment( + response_close_payment, + )); + + // Update or remove payment record: + let new_payment = if let Some(new_payment) = opt_new_payment { + new_payment + } else { + let funder_mutation = FunderMutation::RemovePayment(payment_id); + m_state.mutate(funder_mutation); + return Ok(()); + }; + + // We perform no mutations if no changes happened: + if new_payment == *payment { + return Ok(()); + } + + let funder_mutation = FunderMutation::UpdatePayment((payment_id, new_payment)); + m_state.mutate(funder_mutation); + return Ok(()); +} + pub fn handle_control_message( m_state: &mut MutableFunderState, m_ephemeral: &mut MutableEphemeral, @@ -908,7 +977,9 @@ where max_pending_user_requests, create_transaction, ), - FunderControl::RequestClosePayment(_payment_id) => unimplemented!(), + FunderControl::RequestClosePayment(payment_id) => { + control_request_close_payment(m_state, outgoing_control, payment_id) + } FunderControl::AckClosePayment((_payment_id, _uid)) => unimplemented!(), // Seller API: diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index d2c89bdc8..b5d77c695 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -29,7 +29,7 @@ pub struct FunderState { pub payments: ImHashMap, } -#[derive(Clone, Serialize, Deserialize, Debug)] +#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] pub struct NewTransactions { pub num_transactions: u64, pub invoice_id: InvoiceId, @@ -37,7 +37,7 @@ pub struct NewTransactions { pub dest_public_key: PublicKey, } -#[derive(Clone, Serialize, Deserialize, Debug)] +#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] pub enum Payment { /// User can add new transactions // TODO: Think about a better name for this? diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 2b7ddb6d4..7afbfa5b8 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -603,6 +603,7 @@ pub enum ResponseClosePayment { InProgress, // Can not be acked Success((Receipt, Uid)), // (Receipt, ack_id) Canceled(Uid), // ack_id + PaymentNotFound, } #[derive(Debug)] From e824801ddeda19e01585cc1a0212a433614163f7 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 10:37:58 +0300 Subject: [PATCH 110/248] Implemented control_ack_close_payment() --- .../funder/src/handler/handle_control.rs | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index c37b05120..db379c5b9 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -6,6 +6,7 @@ use crypto::crypto_rand::CryptoRandom; use crypto::hash_lock::PlainLock; use crypto::identity::PublicKey; use crypto::payment_id::PaymentId; +use crypto::uid::Uid; use crate::friend::{ChannelStatus, FriendMutation}; use crate::state::{FunderMutation, NewTransactions, Payment}; @@ -47,6 +48,8 @@ pub enum HandleControlError { OpenPaymentNotFound, NewTransactionsNotAllowed, PaymentDoesNotExist, + AckStateInvalid, + AckMismatch, } fn control_set_friend_remote_max_debt( @@ -886,6 +889,56 @@ where return Ok(()); } +fn control_ack_close_payment( + m_state: &mut MutableFunderState, + payment_id: PaymentId, + user_ack_uid: Uid, +) -> Result<(), HandleControlError> +where + B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, +{ + let payment = m_state + .state() + .payments + .get(&payment_id) + .ok_or(HandleControlError::PaymentDoesNotExist)? + .clone(); + + let new_payment = match payment { + Payment::NewTransactions(_) | Payment::InProgress(_) | Payment::AfterSuccessAck(_) => { + return Err(HandleControlError::AckStateInvalid) + } + Payment::Success((num_transactions, receipt, ack_uid)) => { + // Make sure that ack matches: + if user_ack_uid != ack_uid { + return Err(HandleControlError::AckMismatch); + } + + if num_transactions > 0 { + // Update payment to be `AfterSuccessAck`: + let new_payment = Payment::AfterSuccessAck(num_transactions); + let funder_mutation = FunderMutation::UpdatePayment((payment_id, new_payment)); + m_state.mutate(funder_mutation); + } else { + // Remove payment (no pending transactions): + let funder_mutation = FunderMutation::RemovePayment(payment_id); + m_state.mutate(funder_mutation); + } + } + Payment::Canceled(ack_uid) => { + // Make sure that ack matches: + if user_ack_uid != ack_uid { + return Err(HandleControlError::AckMismatch); + } + + // Remove payment: + let funder_mutation = FunderMutation::RemovePayment(payment_id); + m_state.mutate(funder_mutation); + } + }; + Ok(()) +} + pub fn handle_control_message( m_state: &mut MutableFunderState, m_ephemeral: &mut MutableEphemeral, @@ -980,7 +1033,9 @@ where FunderControl::RequestClosePayment(payment_id) => { control_request_close_payment(m_state, outgoing_control, payment_id) } - FunderControl::AckClosePayment((_payment_id, _uid)) => unimplemented!(), + FunderControl::AckClosePayment((payment_id, ack_uid)) => { + control_ack_close_payment(m_state, payment_id, ack_uid) + } // Seller API: FunderControl::AddInvoice(_invoice_id) => unimplemented!(), From 0b5e886ca92ad3c7d253506e25a32fb89f0ffa15 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 10:48:34 +0300 Subject: [PATCH 111/248] Changed AddInvoice fields to be public. --- components/proto/src/funder/messages.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 7afbfa5b8..7ac920a7d 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -519,9 +519,9 @@ pub struct CreateTransaction { #[derive(Debug, Clone, PartialEq, Eq)] pub struct AddInvoice { /// Randomly generated invoice_id, allows to refer to this invoice. - invoice_id: InvoiceId, + pub invoice_id: InvoiceId, /// Total amount of credits to be paid. - total_dest_payment: u128, + pub total_dest_payment: u128, } #[derive(Debug, Clone, PartialEq, Eq)] From 9cbb1ebe64ce5eee41d6bcf7b47c7058749f6065 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 10:48:58 +0300 Subject: [PATCH 112/248] Fixed RequestClosePayment::AddInvoice to also take total_dest_amount. --- components/proto/src/funder/messages.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 7ac920a7d..6c3a1d616 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -543,7 +543,7 @@ pub enum FunderControl { RequestClosePayment(PaymentId), AckClosePayment((PaymentId, Uid)), // (payment_id, ack_id) // Seller API: - AddInvoice(InvoiceId), + AddInvoice(AddInvoice), CancelInvoice(InvoiceId), CommitInvoice(InvoiceId), } From 8cfc37e02bdd2229ec3a7b05b27d9196282993ff Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 10:49:13 +0300 Subject: [PATCH 113/248] Implemented control_add_invoice(). --- .../funder/src/handler/handle_control.rs | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index db379c5b9..cedb1884b 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -5,6 +5,7 @@ use common::canonical_serialize::CanonicalSerialize; use crypto::crypto_rand::CryptoRandom; use crypto::hash_lock::PlainLock; use crypto::identity::PublicKey; +use crypto::invoice_id::InvoiceId; use crypto::payment_id::PaymentId; use crypto::uid::Uid; @@ -13,7 +14,7 @@ use crate::state::{FunderMutation, NewTransactions, Payment}; use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ - AddFriend, ChannelerUpdateFriend, CreatePayment, CreateTransaction, FriendStatus, + AddFriend, AddInvoice, ChannelerUpdateFriend, CreatePayment, CreateTransaction, FriendStatus, FunderControl, FunderOutgoingControl, ReceiptAck, RemoveFriend, RequestResult, RequestSendFundsOp, ResetFriendChannel, ResponseClosePayment, SetFriendName, SetFriendRate, SetFriendRelays, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, TransactionResult, @@ -50,6 +51,7 @@ pub enum HandleControlError { PaymentDoesNotExist, AckStateInvalid, AckMismatch, + InvoiceAlreadyExists, } fn control_set_friend_remote_max_debt( @@ -939,6 +941,29 @@ where Ok(()) } +fn control_add_invoice( + m_state: &mut MutableFunderState, + add_invoice: AddInvoice, +) -> Result<(), HandleControlError> +where + B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, +{ + if m_state + .state() + .open_invoices + .contains_key(&add_invoice.invoice_id) + { + return Err(HandleControlError::InvoiceAlreadyExists); + } + + // Add new invoice: + let funder_mutation = + FunderMutation::AddInvoice((add_invoice.invoice_id, add_invoice.total_dest_payment)); + m_state.mutate(funder_mutation); + + Ok(()) +} + pub fn handle_control_message( m_state: &mut MutableFunderState, m_ephemeral: &mut MutableEphemeral, @@ -1038,7 +1063,7 @@ where } // Seller API: - FunderControl::AddInvoice(_invoice_id) => unimplemented!(), + FunderControl::AddInvoice(add_invoice) => control_add_invoice(m_state, add_invoice), FunderControl::CancelInvoice(invoice_id) => unimplemented!(), FunderControl::CommitInvoice(_multi_commit) => unimplemented!(), } From 810b664e9af0c18f48976f0ce26a2e2aed9f2d4f Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 11:13:01 +0300 Subject: [PATCH 114/248] Implemented control_cancel_invoice(). --- components/funder/src/handler/canceler.rs | 16 +++++--- .../funder/src/handler/handle_control.rs | 41 ++++++++++++++++++- .../funder/src/handler/handle_friend.rs | 2 +- components/funder/src/handler/sender.rs | 2 +- components/funder/src/types.rs | 5 ++- 5 files changed, 54 insertions(+), 12 deletions(-) diff --git a/components/funder/src/handler/canceler.rs b/components/funder/src/handler/canceler.rs index 15906eebc..c1050b2b0 100644 --- a/components/funder/src/handler/canceler.rs +++ b/components/funder/src/handler/canceler.rs @@ -1,6 +1,9 @@ +use std::fmt::Debug; + use common::canonical_serialize::CanonicalSerialize; + use crypto::identity::PublicKey; -use std::fmt::Debug; +use crypto::uid::Uid; use proto::funder::messages::{ FunderOutgoingControl, RequestResult, RequestSendFundsOp, TransactionResult, @@ -19,12 +22,11 @@ pub fn reply_with_cancel( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, remote_public_key: &PublicKey, - request_send_funds: &RequestSendFundsOp, + request_id: &Uid, ) where B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, { - let pending_local_transaction = create_pending_transaction(request_send_funds); - let cancel_send_funds = create_cancel_send_funds(&pending_local_transaction); + let cancel_send_funds = create_cancel_send_funds(request_id.clone()); let friend_mutation = FriendMutation::PushBackPendingBackwardsOp(BackwardsOp::Cancel(cancel_send_funds)); let funder_mutation = @@ -68,7 +70,8 @@ pub fn cancel_local_pending_transactions( Some(origin_public_key) => { // We have found the friend that is the origin of this request. // We send him a cancel message. - let cancel_send_funds = create_cancel_send_funds(&pending_local_transaction); + let cancel_send_funds = + create_cancel_send_funds(pending_local_transaction.request_id.clone()); let friend_mutation = FriendMutation::PushBackPendingBackwardsOp( BackwardsOp::Cancel(cancel_send_funds), ); @@ -112,7 +115,8 @@ pub fn cancel_pending_requests( match opt_origin_public_key { Some(origin_public_key) => { let pending_local_transaction = create_pending_transaction(&pending_request); - let cancel_send_funds = create_cancel_send_funds(&pending_local_transaction); + let cancel_send_funds = + create_cancel_send_funds(pending_local_transaction.request_id.clone()); let friend_mutation = FriendMutation::PushBackPendingBackwardsOp( BackwardsOp::Cancel(cancel_send_funds), ); diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index cedb1884b..58f0a9669 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -24,10 +24,11 @@ use proto::funder::messages::{ use crate::ephemeral::Ephemeral; use crate::handler::canceler::{ cancel_local_pending_transactions, cancel_pending_requests, cancel_pending_user_requests, + reply_with_cancel, }; use crate::handler::sender::SendCommands; use crate::handler::state_wrap::{MutableEphemeral, MutableFunderState}; -use crate::handler::utils::is_friend_ready; +use crate::handler::utils::{find_request_origin, is_friend_ready}; use crate::types::ChannelerConfig; @@ -52,6 +53,7 @@ pub enum HandleControlError { AckStateInvalid, AckMismatch, InvoiceAlreadyExists, + InvoiceDoesNotExist, } fn control_set_friend_remote_max_debt( @@ -964,6 +966,39 @@ where Ok(()) } +fn control_cancel_invoice( + m_state: &mut MutableFunderState, + send_commands: &mut SendCommands, + invoice_id: InvoiceId, +) -> Result<(), HandleControlError> +where + B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, +{ + let open_invoice = m_state + .state() + .open_invoices + .get(&invoice_id) + .ok_or(HandleControlError::InvoiceDoesNotExist)? + .clone(); + + // Cancel all pending transactions related to this invoice + for (request_id, _) in open_invoice.dest_plain_locks { + // Explaining the unwrap() below: + // We expect that the origin of this request must be from an existing friend. + // We can not be the originator of this request. + let friend_public_key = find_request_origin(m_state.state(), &request_id) + .unwrap() + .clone(); + reply_with_cancel(m_state, send_commands, &friend_public_key, &request_id); + } + + // Remove invoice: + let funder_mutation = FunderMutation::RemoveInvoice(invoice_id); + m_state.mutate(funder_mutation); + + Ok(()) +} + pub fn handle_control_message( m_state: &mut MutableFunderState, m_ephemeral: &mut MutableEphemeral, @@ -1064,7 +1099,9 @@ where // Seller API: FunderControl::AddInvoice(add_invoice) => control_add_invoice(m_state, add_invoice), - FunderControl::CancelInvoice(invoice_id) => unimplemented!(), + FunderControl::CancelInvoice(invoice_id) => { + control_cancel_invoice(m_state, send_commands, invoice_id) + } FunderControl::CommitInvoice(_multi_commit) => unimplemented!(), } } diff --git a/components/funder/src/handler/handle_friend.rs b/components/funder/src/handler/handle_friend.rs index 955cd3b4d..7c531dd4f 100644 --- a/components/funder/src/handler/handle_friend.rs +++ b/components/funder/src/handler/handle_friend.rs @@ -189,7 +189,7 @@ fn handle_request_send_funds( m_state, send_commands, remote_public_key, - &request_send_funds, + &request_send_funds.request_id, ); return; } diff --git a/components/funder/src/handler/sender.rs b/components/funder/src/handler/sender.rs index aa42abee1..403514d64 100644 --- a/components/funder/src/handler/sender.rs +++ b/components/funder/src/handler/sender.rs @@ -473,7 +473,7 @@ where // The friend with public key `origin_public_key` is the origin of this request. // We send him back a failure message: let pending_transaction = create_pending_transaction(request_send_funds); - let cancel_send_funds = BackwardsOp::Cancel(create_cancel_send_funds(&pending_transaction)); + let cancel_send_funds = BackwardsOp::Cancel(create_cancel_send_funds(pending_transaction.request_id.clone())); let friend_mutation = FriendMutation::PushBackPendingBackwardsOp(cancel_send_funds); let funder_mutation = FunderMutation::FriendMutation((origin_public_key.clone(), friend_mutation)); diff --git a/components/funder/src/types.rs b/components/funder/src/types.rs index f61f31def..05a269617 100644 --- a/components/funder/src/types.rs +++ b/components/funder/src/types.rs @@ -4,6 +4,7 @@ use crypto::crypto_rand::RandValue; use crypto::hash::HashResult; use crypto::hash_lock::HashedLock; use crypto::identity::{PublicKey, Signature}; +use crypto::uid::Uid; use proto::app_server::messages::RelayAddress; use proto::funder::messages::{ @@ -74,11 +75,11 @@ pub async fn create_response_send_funds<'a>( } pub fn create_cancel_send_funds( - pending_transaction: &PendingTransaction, + request_id: Uid, ) -> CancelSendFundsOp { CancelSendFundsOp { - request_id: pending_transaction.request_id, + request_id: request_id, } } From a74797685a03dfa9c91d7d9a5f9fcf0dc81a1405 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 11:46:33 +0300 Subject: [PATCH 115/248] Fixed capnp fields in atomic_proposal.md. --- doc/docs/atomic_proposal.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/docs/atomic_proposal.md b/doc/docs/atomic_proposal.md index 495ea7e36..bbfff0fa5 100644 --- a/doc/docs/atomic_proposal.md +++ b/doc/docs/atomic_proposal.md @@ -412,7 +412,7 @@ struct Commit { srcPlainLock @2: PlainLock; # The preimage of the hashedLock at the request message [256 bits] destHashedLock @3: HashedLock; - signature @3: Signature; + signature @4: Signature; # Signature{key=destinationKey}( # sha512/256("FUNDS_RESPONSE") || # sha512/256(requestId || sha512/256(route) || randNonce) || From beb1cfa7a3f31dab6da38cc14107e64874892717 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 11:47:11 +0300 Subject: [PATCH 116/248] Changed fields in Commit, MultiCommit to be public. --- components/proto/src/funder/messages.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 6c3a1d616..516910409 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -87,18 +87,18 @@ pub struct CancelSendFundsOp { #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] pub struct Commit { - response_hash: HashResult, - dest_payment: u128, - src_plain_lock: PlainLock, - dest_hashed_lock: HashedLock, - signature: Signature, + pub response_hash: HashResult, + pub dest_payment: u128, + pub src_plain_lock: PlainLock, + pub dest_hashed_lock: HashedLock, + pub signature: Signature, } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] pub struct MultiCommit { - invoice_id: InvoiceId, - total_dest_payment: u128, - commits: Vec, + pub invoice_id: InvoiceId, + pub total_dest_payment: u128, + pub commits: Vec, } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] From 9edccf7b88e65143aebdac166045ab2b8e95ca1a Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 11:47:46 +0300 Subject: [PATCH 117/248] Updated FunderControl::CommitInvoice content to be MultiCommit. --- components/proto/src/funder/messages.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 516910409..881770438 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -545,7 +545,7 @@ pub enum FunderControl { // Seller API: AddInvoice(AddInvoice), CancelInvoice(InvoiceId), - CommitInvoice(InvoiceId), + CommitInvoice(MultiCommit), } #[derive(Debug, Clone, PartialEq, Eq)] From 2a4907d716201c962ce3e94995de746e8c463d87 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 11:48:29 +0300 Subject: [PATCH 118/248] Implemented verify_multi_commit(). --- components/proto/src/funder/signature_buff.rs | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/components/proto/src/funder/signature_buff.rs b/components/proto/src/funder/signature_buff.rs index 056e56c48..de967acf3 100644 --- a/components/proto/src/funder/signature_buff.rs +++ b/components/proto/src/funder/signature_buff.rs @@ -1,11 +1,15 @@ use byteorder::{BigEndian, WriteBytesExt}; + use crypto::hash::{self, sha_512_256, HashResult}; use crypto::identity::{verify_signature, PublicKey}; +use crypto::invoice_id::InvoiceId; use common::canonical_serialize::CanonicalSerialize; use common::int_convert::usize_to_u64; -use super::messages::{MoveToken, PendingTransaction, Receipt, ResponseSendFundsOp}; +use super::messages::{ + Commit, MoveToken, MultiCommit, PendingTransaction, Receipt, ResponseSendFundsOp, +}; pub const FUNDS_RESPONSE_PREFIX: &[u8] = b"FUND_RESPONSE"; pub const FUNDS_CANCEL_PREFIX: &[u8] = b"FUND_CANCEL"; @@ -76,6 +80,57 @@ pub fn verify_receipt(receipt: &Receipt, public_key: &PublicKey) -> bool { verify_signature(&data, public_key, &receipt.signature) } +/// Verify that a given Commit signature is valid +fn verify_commit( + commit: &Commit, + invoice_id: &InvoiceId, + total_dest_payment: u128, + local_public_key: &PublicKey, +) -> bool { + let mut data = Vec::new(); + + data.extend_from_slice(&hash::sha_512_256(FUNDS_RESPONSE_PREFIX)); + data.extend(commit.response_hash.as_ref()); + data.extend_from_slice(&commit.src_plain_lock.hash()); + data.extend_from_slice(&commit.dest_hashed_lock); + data.write_u128::(commit.dest_payment).unwrap(); + data.write_u128::(total_dest_payment).unwrap(); + data.extend(invoice_id.as_ref()); + verify_signature(&data, local_public_key, &commit.signature) +} + +// TODO: Possibly split nicely into two functions? +/// Verify that all the Commit-s inside a MultiCommit are valid +pub fn verify_multi_commit(multi_commit: &MultiCommit, local_public_key: &PublicKey) -> bool { + let mut is_sig_valid = true; + for commit in &multi_commit.commits { + // We don't exit immediately on verification failure to get a constant time verification. + // (Not sure if this is really important here) + is_sig_valid &= verify_commit( + commit, + &multi_commit.invoice_id, + multi_commit.total_dest_payment, + local_public_key, + ); + } + if !is_sig_valid { + return false; + } + + // Check if the credits add up: + let mut sum_credits = 0u128; + for commit in &multi_commit.commits { + sum_credits = if let Some(sum_credits) = sum_credits.checked_add(commit.dest_payment) { + sum_credits + } else { + return false; + } + } + + // Require that the multi_commit.total_dest_payment matches the sum of all commit.dest_payment: + sum_credits == multi_commit.total_dest_payment +} + // Prefix used for chain hashing of token channel funds. // NEXT is used for hashing for the next move token funds. pub const TOKEN_NEXT: &[u8] = b"NEXT"; From 2e887339538d7cfe5654c20644d36a241f1abd21 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 12:24:14 +0300 Subject: [PATCH 119/248] Some perparations for implementation of control_commit_invoice(). --- .../funder/src/handler/handle_control.rs | 55 ++++++++++++++++++- components/funder/src/handler/state_wrap.rs | 2 +- components/funder/src/report.rs | 2 +- components/funder/src/state.rs | 30 ++++++---- 4 files changed, 74 insertions(+), 15 deletions(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 58f0a9669..8359daeba 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -15,11 +15,12 @@ use crate::state::{FunderMutation, NewTransactions, Payment}; use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ AddFriend, AddInvoice, ChannelerUpdateFriend, CreatePayment, CreateTransaction, FriendStatus, - FunderControl, FunderOutgoingControl, ReceiptAck, RemoveFriend, RequestResult, + FunderControl, FunderOutgoingControl, MultiCommit, ReceiptAck, RemoveFriend, RequestResult, RequestSendFundsOp, ResetFriendChannel, ResponseClosePayment, SetFriendName, SetFriendRate, SetFriendRelays, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, TransactionResult, UserRequestSendFunds, }; +use proto::funder::signature_buff::verify_multi_commit; use crate::ephemeral::Ephemeral; use crate::handler::canceler::{ @@ -54,6 +55,7 @@ pub enum HandleControlError { AckMismatch, InvoiceAlreadyExists, InvoiceDoesNotExist, + InvalidMultiCommit, } fn control_set_friend_remote_max_debt( @@ -982,7 +984,8 @@ where .clone(); // Cancel all pending transactions related to this invoice - for (request_id, _) in open_invoice.dest_plain_locks { + for (_, incoming_transaction) in open_invoice.incoming_transactions { + let request_id = &incoming_transaction.request_id; // Explaining the unwrap() below: // We expect that the origin of this request must be from an existing friend. // We can not be the originator of this request. @@ -999,6 +1002,50 @@ where Ok(()) } +fn control_commit_invoice( + m_state: &mut MutableFunderState, + multi_commit: &MultiCommit, +) -> Result<(), HandleControlError> +where + B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, +{ + // Find matching open invoice: + let open_invoice = m_state + .state() + .open_invoices + .get(&multi_commit.invoice_id) + .ok_or(HandleControlError::InvoiceDoesNotExist)? + .clone(); + + if !verify_multi_commit(multi_commit, &m_state.state().local_public_key) { + return Err(HandleControlError::InvalidMultiCommit); + } + /* + + // TODO: + // - Push collect messages for all pending requests + for commit in multi_commit.commits { + let opt_uid_open_invoice.transactions.get(&commit.dest_hashed_lock) + } + + let friend_mutation = FriendMutation::PushBackPendingUserRequest(request_send_funds); + let funder_mutation = + FunderMutation::FriendMutation((friend_public_key.clone(), friend_mutation)); + m_state.mutate(funder_mutation); + + // Signal the sender to attempt to send: + send_commands.set_try_send(&friend_public_key); + assert!(false); + + // Remove invoice: + let funder_mutation = FunderMutation::RemoveInvoice(multi_commit.invoice_id.clone()); + m_state.mutate(funder_mutation); + + Ok(()) + */ + unimplemented!(); +} + pub fn handle_control_message( m_state: &mut MutableFunderState, m_ephemeral: &mut MutableEphemeral, @@ -1102,6 +1149,8 @@ where FunderControl::CancelInvoice(invoice_id) => { control_cancel_invoice(m_state, send_commands, invoice_id) } - FunderControl::CommitInvoice(_multi_commit) => unimplemented!(), + FunderControl::CommitInvoice(multi_commit) => { + control_commit_invoice(m_state, &multi_commit) + } } } diff --git a/components/funder/src/handler/state_wrap.rs b/components/funder/src/handler/state_wrap.rs index 8c4fd0937..1d808e0ae 100644 --- a/components/funder/src/handler/state_wrap.rs +++ b/components/funder/src/handler/state_wrap.rs @@ -105,7 +105,7 @@ where self.mutate(funder_mutation); // Mutation to add the destination plain lock: - let funder_mutation = FunderMutation::AddDestPlainLock(( + let funder_mutation = FunderMutation::AddIncomingTransaction(( pending_transaction.invoice_id, pending_transaction.request_id, dest_plain_lock, diff --git a/components/funder/src/report.rs b/components/funder/src/report.rs index 23dd7e4fd..75accbd94 100644 --- a/components/funder/src/report.rs +++ b/components/funder/src/report.rs @@ -349,7 +349,7 @@ where Vec::new() } } - FunderMutation::AddDestPlainLock(_) => vec![], + FunderMutation::AddIncomingTransaction(_) => vec![], FunderMutation::AddTransaction(_) | FunderMutation::RemoveTransaction(_) => { if funder_state_after.open_transactions.len() != funder_state.open_transactions.len() { vec![FunderReportMutation::SetNumOpenTransactions( diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index b5d77c695..86bcda2dd 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -2,7 +2,7 @@ use im::hashmap::HashMap as ImHashMap; use im::vector::Vector as ImVec; use common::canonical_serialize::CanonicalSerialize; -use crypto::hash_lock::PlainLock; +use crypto::hash_lock::{HashedLock, PlainLock}; use crypto::identity::PublicKey; use crypto::invoice_id::InvoiceId; use crypto::payment_id::PaymentId; @@ -52,22 +52,28 @@ pub enum Payment { AfterSuccessAck(u64), // num_transactions } +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct IncomingTransaction { + pub request_id: Uid, + /// The lock we used on our ResponseSendFundsOp message. + /// We have to keep it, otherwise we will not be able to send a valid CollectSendFundsOp later. + pub dest_plain_lock: PlainLock, +} + /// A local invoice in progress #[derive(Clone, Serialize, Deserialize, Debug)] pub struct OpenInvoice { /// Total payment required to fulfill this invoice: pub total_dest_payment: u128, - /// Destination plain locks for all requests related to a single open invoice that was - /// originated for this node. - /// Multiple requests are possible for a single invoice in case of a multi-route payment. - pub dest_plain_locks: ImHashMap, + /// Multiple transactions are possible for a single invoice in case of a multi-route payment. + pub incoming_transactions: ImHashMap, } impl OpenInvoice { pub fn new(total_dest_payment: u128) -> Self { OpenInvoice { total_dest_payment, - dest_plain_locks: ImHashMap::new(), + incoming_transactions: ImHashMap::new(), } } } @@ -89,7 +95,7 @@ pub enum FunderMutation { AddFriend(AddFriend), RemoveFriend(PublicKey), AddInvoice((InvoiceId, u128)), // (InvoiceId, total_dest_payment) - AddDestPlainLock((InvoiceId, Uid, PlainLock)), // InvoiceId, RequestId, dest_plain_lock + AddIncomingTransaction((InvoiceId, Uid, PlainLock)), // (invoice_id, request_id, dest_plain_lock) RemoveInvoice(InvoiceId), AddTransaction((Uid, PaymentId, PlainLock)), // (transaction_id, payment_id,src_plain_lock) RemoveTransaction(Uid), // transaction_id @@ -157,11 +163,15 @@ where self.open_invoices .insert(invoice_id.clone(), OpenInvoice::new(*total_dest_payment)); } - FunderMutation::AddDestPlainLock((invoice_id, request_id, plain_lock)) => { + FunderMutation::AddIncomingTransaction((invoice_id, request_id, dest_plain_lock)) => { let open_invoice = self.open_invoices.get_mut(invoice_id).unwrap(); + let incoming_transaction = IncomingTransaction { + request_id: request_id.clone(), + dest_plain_lock: dest_plain_lock.clone(), + }; open_invoice - .dest_plain_locks - .insert(request_id.clone(), plain_lock.clone()); + .incoming_transactions + .insert(dest_plain_lock.hash().clone(), incoming_transaction); } FunderMutation::RemoveInvoice(invoice_id) => { let _ = self.open_invoices.remove(invoice_id); From a7696c78995650bd10288be51d6de62a99afbfe7 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 12:34:46 +0300 Subject: [PATCH 120/248] Implemented control_commit_invoice(). --- .../funder/src/handler/handle_control.rs | 61 +++++++++++++------ 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 8359daeba..a57227a62 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -9,16 +9,16 @@ use crypto::invoice_id::InvoiceId; use crypto::payment_id::PaymentId; use crypto::uid::Uid; -use crate::friend::{ChannelStatus, FriendMutation}; +use crate::friend::{BackwardsOp, ChannelStatus, FriendMutation}; use crate::state::{FunderMutation, NewTransactions, Payment}; use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ - AddFriend, AddInvoice, ChannelerUpdateFriend, CreatePayment, CreateTransaction, FriendStatus, - FunderControl, FunderOutgoingControl, MultiCommit, ReceiptAck, RemoveFriend, RequestResult, - RequestSendFundsOp, ResetFriendChannel, ResponseClosePayment, SetFriendName, SetFriendRate, - SetFriendRelays, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, TransactionResult, - UserRequestSendFunds, + AddFriend, AddInvoice, ChannelerUpdateFriend, CollectSendFundsOp, CreatePayment, + CreateTransaction, FriendStatus, FunderControl, FunderOutgoingControl, MultiCommit, ReceiptAck, + RemoveFriend, RequestResult, RequestSendFundsOp, ResetFriendChannel, ResponseClosePayment, + SetFriendName, SetFriendRate, SetFriendRelays, SetFriendRemoteMaxDebt, SetFriendStatus, + SetRequestsStatus, TransactionResult, UserRequestSendFunds, }; use proto::funder::signature_buff::verify_multi_commit; @@ -1004,6 +1004,7 @@ where fn control_commit_invoice( m_state: &mut MutableFunderState, + send_commands: &mut SendCommands, multi_commit: &MultiCommit, ) -> Result<(), HandleControlError> where @@ -1020,30 +1021,50 @@ where if !verify_multi_commit(multi_commit, &m_state.state().local_public_key) { return Err(HandleControlError::InvalidMultiCommit); } - /* // TODO: // - Push collect messages for all pending requests - for commit in multi_commit.commits { - let opt_uid_open_invoice.transactions.get(&commit.dest_hashed_lock) - } + for commit in &multi_commit.commits { + let incoming_transaction = if let Some(incoming_transaction) = open_invoice + .incoming_transactions + .get(&commit.dest_hashed_lock) + { + incoming_transaction + } else { + warn!("control_commit_invoice(): Failed to find matching incoming transaction."); + continue; + }; - let friend_mutation = FriendMutation::PushBackPendingUserRequest(request_send_funds); - let funder_mutation = - FunderMutation::FriendMutation((friend_public_key.clone(), friend_mutation)); - m_state.mutate(funder_mutation); + let friend_public_key = if let Some(friend_public_key) = + find_request_origin(m_state.state(), &incoming_transaction.request_id) + { + friend_public_key.clone() + } else { + warn!("control_commit_invoice(): Failed to find request origin"); + continue; + }; - // Signal the sender to attempt to send: - send_commands.set_try_send(&friend_public_key); - assert!(false); + let collect_send_funds = CollectSendFundsOp { + request_id: incoming_transaction.request_id.clone(), + src_plain_lock: commit.src_plain_lock.clone(), + dest_plain_lock: incoming_transaction.dest_plain_lock.clone(), + }; + + let friend_mutation = + FriendMutation::PushBackPendingBackwardsOp(BackwardsOp::Collect(collect_send_funds)); + let funder_mutation = + FunderMutation::FriendMutation((friend_public_key.clone(), friend_mutation)); + m_state.mutate(funder_mutation); + + // Signal the sender to attempt to send: + send_commands.set_try_send(&friend_public_key); + } // Remove invoice: let funder_mutation = FunderMutation::RemoveInvoice(multi_commit.invoice_id.clone()); m_state.mutate(funder_mutation); Ok(()) - */ - unimplemented!(); } pub fn handle_control_message( @@ -1150,7 +1171,7 @@ where control_cancel_invoice(m_state, send_commands, invoice_id) } FunderControl::CommitInvoice(multi_commit) => { - control_commit_invoice(m_state, &multi_commit) + control_commit_invoice(m_state, send_commands, &multi_commit) } } } From 6cd2282561d28dbb0f7e21df0752cc15c7d7f379 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 12:35:21 +0300 Subject: [PATCH 121/248] Removed TODO comment. --- components/funder/src/handler/handle_control.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index a57227a62..8b9a555e2 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -1022,8 +1022,7 @@ where return Err(HandleControlError::InvalidMultiCommit); } - // TODO: - // - Push collect messages for all pending requests + // Push collect messages for all pending requests for commit in &multi_commit.commits { let incoming_transaction = if let Some(incoming_transaction) = open_invoice .incoming_transactions From 87bd5941a527231594047ac2e6942711846b3039 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 12:55:35 +0300 Subject: [PATCH 122/248] Updated comments. --- components/funder/src/handler/canceler.rs | 9 +- .../funder/src/handler/handle_control.rs | 157 ------------------ components/funder/src/state.rs | 1 + 3 files changed, 8 insertions(+), 159 deletions(-) diff --git a/components/funder/src/handler/canceler.rs b/components/funder/src/handler/canceler.rs index c1050b2b0..ad7faedad 100644 --- a/components/funder/src/handler/canceler.rs +++ b/components/funder/src/handler/canceler.rs @@ -17,7 +17,7 @@ use crate::friend::{BackwardsOp, ChannelStatus, FriendMutation}; use crate::state::FunderMutation; use crate::types::{create_cancel_send_funds, create_pending_transaction}; -/// Reply to a request message with a cancellation. +/// Reply to a single request message with a cancellation. pub fn reply_with_cancel( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, @@ -36,7 +36,8 @@ pub fn reply_with_cancel( } /// Cancel outgoing local requests that are already inside the token channel (Possibly already -/// communicated to the remote side). +/// communicated to the remote side). This is a violent operation, as we break our promises for +/// forwarded requests. This should only be done during unfriending. pub fn cancel_local_pending_transactions( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, @@ -93,6 +94,8 @@ pub fn cancel_local_pending_transactions( } } +/// Cancel all pending request messages at the pending_requests queue. +/// These are requests that were forwarded from other nodes. pub fn cancel_pending_requests( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, @@ -137,6 +140,8 @@ pub fn cancel_pending_requests( } } +/// Cancel all pending request messages at the user_pending_requests queue. +/// These are requests that were created by the local user of this node. pub fn cancel_pending_user_requests( m_state: &mut MutableFunderState, outgoing_control: &mut Vec>, diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 8b9a555e2..76eefd2a9 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -475,163 +475,6 @@ fn check_user_request_valid(user_request_send_funds: &UserRequestSendFunds) -> O Some(()) } -/* -fn control_request_send_funds_inner( - m_state: &mut MutableFunderState, - ephemeral: &Ephemeral, - outgoing_control: &mut Vec>, - send_commands: &mut SendCommands, - max_pending_user_requests: usize, - user_request_send_funds: UserRequestSendFunds, -) -> Result<(), HandleControlError> -where - B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, -{ - check_user_request_valid(&user_request_send_funds) - .ok_or(HandleControlError::UserRequestInvalid)?; - - // If we already have a receipt for this request, we return the receipt immediately and - // exit. Note that we don't erase the receipt yet. This will only be done when a receipt - // ack is received. - if let Some(receipt) = m_state - .state() - .ready_receipts - .get(&user_request_send_funds.payment_id) - { - let response_received = ResponseReceived { - request_id: user_request_send_funds.payment_id, - result: ResponseSendFundsResult::Success(receipt.clone()), - }; - outgoing_control.push(FunderOutgoingControl::ResponseReceived(response_received)); - return Ok(()); - } - - let route = &user_request_send_funds.route; - - // We have to be the first on the route: - match route.public_keys.first() { - Some(first) if *first == m_state.state().local_public_key => Ok(()), - _ => Err(HandleControlError::NotFirstInRoute), - }?; - - // We want to have at least two public keys on the route (source and destination). - // We also want that the public keys on the route are unique. - if !route.is_valid() { - return Err(HandleControlError::InvalidRoute); - } - let friend_public_key = route.public_keys[1].clone(); - - let friend = match m_state.state().friends.get(&friend_public_key) { - Some(friend) => Ok(friend), - None => Err(HandleControlError::FriendDoesNotExist), - }?; - - if !is_friend_ready(m_state.state(), ephemeral, &friend_public_key) { - return Err(HandleControlError::FriendNotReady); - } - - // If payment is already in progress, we do nothing: - // Check if there is already a pending user payment with the same payment_id: - for user_request in &friend.pending_user_requests { - if user_request_send_funds.payment_id == user_request.request_id { - return Err(HandleControlError::RequestAlreadyInProgress); - } - } - - let token_channel = match &friend.channel_status { - ChannelStatus::Inconsistent(_) => unreachable!(), - ChannelStatus::Consistent(token_channel) => token_channel, - }; - - // Check if there is an ongoing request with the same request_id with this specific friend: - if token_channel - .get_mutual_credit() - .state() - .pending_transactions - .local - .contains_key(&user_request_send_funds.request_id) - { - return Err(HandleControlError::RequestAlreadyInProgress); - } - - // Check if we have room to push this message: - if friend.pending_user_requests.len() >= max_pending_user_requests { - return Err(HandleControlError::PendingUserRequestsFull); - } - - let request_send_funds = user_request_send_funds.into_request(); - let friend_mutation = FriendMutation::PushBackPendingUserRequest(request_send_funds); - let funder_mutation = - FunderMutation::FriendMutation((friend_public_key.clone(), friend_mutation)); - m_state.mutate(funder_mutation); - send_commands.set_try_send(&friend_public_key); - - Ok(()) -} - -fn control_request_send_funds( - m_state: &mut MutableFunderState, - ephemeral: &Ephemeral, - outgoing_control: &mut Vec>, - send_commands: &mut SendCommands, - max_pending_user_requests: usize, - user_request_send_funds: UserRequestSendFunds, -) -> Result<(), HandleControlError> -where - B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, -{ - // If we managed to push the message, we return an Ok(()). - // Otherwise, we return the internal error and return a response failure message. - if let Err(e) = control_request_send_funds_inner( - m_state, - ephemeral, - outgoing_control, - send_commands, - max_pending_user_requests, - user_request_send_funds.clone(), - ) { - error!("control_request_send_funds_inner() failed: {:?}", e); - let response_received = ResponseReceived { - request_id: user_request_send_funds.payment_id, - result: ResponseSendFundsResult::Failure, - }; - - outgoing_control.push(FunderOutgoingControl::ResponseReceived(response_received)); - } - - // Every RequestSendFunds must have a matching response. Therefore we don't return an error - // here. We have to make sure the response arrives back to the user. - Ok(()) -} - -/// Handle an incoming receipt ack message -fn control_receipt_ack( - m_state: &mut MutableFunderState, - receipt_ack: ReceiptAck, -) -> Result<(), HandleControlError> -where - B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, -{ - let receipt = m_state - .state() - .ready_receipts - .get(&receipt_ack.request_id) - .ok_or(HandleControlError::ReceiptDoesNotExist)?; - - // Make sure that the provided signature matches the one we have at the ready receipt. - // We do this to make sure the user doesn't send a receipt ack before he actually got the - // receipt (the user can not predict the receipt_signature ahead of time) - if receipt_ack.receipt_signature != receipt.signature { - return Err(HandleControlError::ReceiptSignatureMismatch); - } - - let funder_mutation = FunderMutation::RemoveReceipt(receipt_ack.request_id); - m_state.mutate(funder_mutation); - - Ok(()) -} -*/ - fn control_create_payment( m_state: &mut MutableFunderState, create_payment: CreatePayment, diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index 86bcda2dd..91e9ef18f 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -29,6 +29,7 @@ pub struct FunderState { pub payments: ImHashMap, } +/// A state of a Payment where new transactions may still be added. #[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] pub struct NewTransactions { pub num_transactions: u64, From f144ce8520c1b79e660a3542ac29c558cb2e7826 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 16:05:35 +0300 Subject: [PATCH 123/248] Canceler now updates transaction records when removing a request. --- components/funder/src/handler/canceler.rs | 92 ++++++++++++++++++- .../funder/src/handler/handle_control.rs | 27 +++++- .../funder/src/handler/handle_friend.rs | 36 ++++++-- .../funder/src/handler/handle_liveness.rs | 20 +++- components/funder/src/handler/handler.rs | 3 +- 5 files changed, 157 insertions(+), 21 deletions(-) diff --git a/components/funder/src/handler/canceler.rs b/components/funder/src/handler/canceler.rs index ad7faedad..2cf1d63da 100644 --- a/components/funder/src/handler/canceler.rs +++ b/components/funder/src/handler/canceler.rs @@ -2,6 +2,7 @@ use std::fmt::Debug; use common::canonical_serialize::CanonicalSerialize; +use crypto::crypto_rand::CryptoRandom; use crypto::identity::PublicKey; use crypto::uid::Uid; @@ -14,7 +15,7 @@ use crate::handler::state_wrap::MutableFunderState; use crate::handler::utils::find_request_origin; use crate::friend::{BackwardsOp, ChannelStatus, FriendMutation}; -use crate::state::FunderMutation; +use crate::state::{FunderMutation, Payment}; use crate::types::{create_cancel_send_funds, create_pending_transaction}; /// Reply to a single request message with a cancellation. @@ -35,16 +36,92 @@ pub fn reply_with_cancel( send_commands.set_try_send(remote_public_key); } +/// Remove a local transaction +fn remove_transaction(m_state: &mut MutableFunderState, rng: &R, request_id: &Uid) +where + B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, + R: CryptoRandom, +{ + // TODO: Can this unwrap() ever panic? + let open_transaction = m_state + .state() + .open_transactions + .get(request_id) + .unwrap() + .clone(); + + // TODO: Can this unwrap() ever panic? + let payment = m_state + .state() + .payments + .get(&open_transaction.payment_id) + .unwrap() + .clone(); + + // Remove transaction: + let funder_mutation = FunderMutation::RemoveTransaction(request_id.clone()); + m_state.mutate(funder_mutation); + + // Update payment: + // - Decrease num_transactions + // - Possibly remove payment + let opt_new_payment = match payment { + Payment::NewTransactions(new_transactions) => { + let mut new_new_transactions = new_transactions.clone(); + new_new_transactions.num_transactions = + new_transactions.num_transactions.checked_sub(1).unwrap(); + Some(Payment::NewTransactions(new_new_transactions)) + } + Payment::InProgress(num_transactions) => { + let new_num_transactions = num_transactions.checked_sub(1).unwrap(); + if new_num_transactions > 0 { + Some(Payment::InProgress(new_num_transactions)) + } else { + let ack_uid = Uid::new(rng); + Some(Payment::Canceled(ack_uid)) + } + } + Payment::Success((num_transactions, receipt, request_id)) => { + let new_num_transactions = num_transactions.checked_sub(1).unwrap(); + Some(Payment::Success(( + new_num_transactions, + receipt.clone(), + request_id.clone(), + ))) + } + Payment::Canceled(_) => { + unreachable!(); + } + Payment::AfterSuccessAck(num_transactions) => { + let new_num_transactions = num_transactions.checked_sub(1).unwrap(); + if new_num_transactions > 0 { + Some(Payment::AfterSuccessAck(new_num_transactions)) + } else { + None + } + } + }; + + let funder_mutation = if let Some(new_payment) = opt_new_payment { + FunderMutation::UpdatePayment((open_transaction.payment_id.clone(), new_payment)) + } else { + FunderMutation::RemovePayment(open_transaction.payment_id.clone()) + }; + m_state.mutate(funder_mutation); +} + /// Cancel outgoing local requests that are already inside the token channel (Possibly already /// communicated to the remote side). This is a violent operation, as we break our promises for /// forwarded requests. This should only be done during unfriending. -pub fn cancel_local_pending_transactions( +pub fn cancel_local_pending_transactions( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, outgoing_control: &mut Vec>, + rng: &R, friend_public_key: &PublicKey, ) where B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, + R: CryptoRandom, { let friend = m_state.state().friends.get(friend_public_key).unwrap(); @@ -89,6 +166,7 @@ pub fn cancel_local_pending_transactions( result: RequestResult::Failure, }; outgoing_control.push(FunderOutgoingControl::TransactionResult(transaction_result)); + remove_transaction(m_state, rng, &pending_local_transaction.request_id); } }; } @@ -96,13 +174,15 @@ pub fn cancel_local_pending_transactions( /// Cancel all pending request messages at the pending_requests queue. /// These are requests that were forwarded from other nodes. -pub fn cancel_pending_requests( +pub fn cancel_pending_requests( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, outgoing_control: &mut Vec>, + rng: &R, friend_public_key: &PublicKey, ) where B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, + R: CryptoRandom, { let friend = m_state.state().friends.get(friend_public_key).unwrap(); let mut pending_requests = friend.pending_requests.clone(); @@ -135,6 +215,7 @@ pub fn cancel_pending_requests( result: RequestResult::Failure, }; outgoing_control.push(FunderOutgoingControl::TransactionResult(transaction_result)); + remove_transaction(m_state, rng, &pending_request.request_id); } }; } @@ -142,12 +223,14 @@ pub fn cancel_pending_requests( /// Cancel all pending request messages at the user_pending_requests queue. /// These are requests that were created by the local user of this node. -pub fn cancel_pending_user_requests( +pub fn cancel_pending_user_requests( m_state: &mut MutableFunderState, outgoing_control: &mut Vec>, + rng: &R, friend_public_key: &PublicKey, ) where B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, + R: CryptoRandom, { let friend = m_state.state().friends.get(&friend_public_key).unwrap(); let mut pending_user_requests = friend.pending_user_requests.clone(); @@ -164,5 +247,6 @@ pub fn cancel_pending_user_requests( result: RequestResult::Failure, }; outgoing_control.push(FunderOutgoingControl::TransactionResult(transaction_result)); + remove_transaction(m_state, rng, &pending_user_request.request_id); } } diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 76eefd2a9..7ac8753cf 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -150,19 +150,27 @@ fn enable_friend( outgoing_channeler_config.push(channeler_config); } -fn disable_friend( +fn disable_friend( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, outgoing_control: &mut Vec>, outgoing_channeler_config: &mut Vec>>, + rng: &R, friend_public_key: &PublicKey, ) where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, + R: CryptoRandom, { // Cancel all pending requests to this friend: - cancel_pending_requests(m_state, send_commands, outgoing_control, friend_public_key); + cancel_pending_requests( + m_state, + send_commands, + outgoing_control, + rng, + friend_public_key, + ); - cancel_pending_user_requests(m_state, outgoing_control, friend_public_key); + cancel_pending_user_requests(m_state, outgoing_control, rng, friend_public_key); // Notify Channeler: let channeler_config = ChannelerConfig::RemoveFriend(friend_public_key.clone()); @@ -249,15 +257,17 @@ where /// This is a violent operation, as it removes all the known state with the remote friend. /// An inconsistency will occur if the friend is added again. -fn control_remove_friend( +fn control_remove_friend( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, outgoing_control: &mut Vec>, outgoing_channeler_config: &mut Vec>>, + rng: &R, remove_friend: RemoveFriend, ) -> Result<(), HandleControlError> where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, + R: CryptoRandom, { // Make sure that friend exists: let _friend = m_state @@ -271,6 +281,7 @@ where send_commands, outgoing_control, outgoing_channeler_config, + rng, &remove_friend.friend_public_key, ); @@ -278,6 +289,7 @@ where m_state, send_commands, outgoing_control, + rng, &remove_friend.friend_public_key, ); @@ -287,15 +299,17 @@ where Ok(()) } -fn control_set_friend_status( +fn control_set_friend_status( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, outgoing_control: &mut Vec>, outgoing_channeler_config: &mut Vec>>, + rng: &R, set_friend_status: SetFriendStatus, ) -> Result<(), HandleControlError> where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, + R: CryptoRandom, { // Make sure that friend exists: let _ = m_state @@ -332,6 +346,7 @@ where send_commands, outgoing_control, outgoing_channeler_config, + rng, &friend_public_key, ), }; @@ -961,6 +976,7 @@ where send_commands, outgoing_control, outgoing_channeler_config, + rng, remove_friend, ), @@ -969,6 +985,7 @@ where send_commands, outgoing_control, outgoing_channeler_config, + rng, set_friend_status, ), diff --git a/components/funder/src/handler/handle_friend.rs b/components/funder/src/handler/handle_friend.rs index 7c531dd4f..e87145687 100644 --- a/components/funder/src/handler/handle_friend.rs +++ b/components/funder/src/handler/handle_friend.rs @@ -370,10 +370,22 @@ fn handle_move_token_error( let local_reset_terms = gen_reset_terms(&token_channel, rng); // Cancel all internal pending requests inside token channel: - cancel_local_pending_transactions(m_state, send_commands, outgoing_control, remote_public_key); + cancel_local_pending_transactions( + m_state, + send_commands, + outgoing_control, + rng, + remote_public_key, + ); // Cancel all pending requests to this friend: - cancel_pending_requests(m_state, send_commands, outgoing_control, remote_public_key); - cancel_pending_user_requests(m_state, outgoing_control, remote_public_key); + cancel_pending_requests( + m_state, + send_commands, + outgoing_control, + rng, + remote_public_key, + ); + cancel_pending_user_requests(m_state, outgoing_control, rng, remote_public_key); // Keep outgoing InconsistencyError message details in memory: let channel_inconsistent = ChannelInconsistent { @@ -389,17 +401,19 @@ fn handle_move_token_error( } /// Handle success with incoming move token. -fn handle_move_token_success( +fn handle_move_token_success( m_state: &mut MutableFunderState, m_ephemeral: &mut MutableEphemeral, send_commands: &mut SendCommands, outgoing_control: &mut Vec>, outgoing_channeler_config: &mut Vec>>, + rng: &R, remote_public_key: &PublicKey, receive_move_token_output: ReceiveMoveTokenOutput, token_wanted: bool, ) where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, + R: CryptoRandom, { match receive_move_token_output { ReceiveMoveTokenOutput::Duplicate => {} @@ -487,9 +501,10 @@ fn handle_move_token_success( m_state, send_commands, outgoing_control, + rng, remote_public_key, ); - cancel_pending_user_requests(m_state, outgoing_control, remote_public_key); + cancel_pending_user_requests(m_state, outgoing_control, rng, remote_public_key); } handle_move_token_output( @@ -555,6 +570,7 @@ where send_commands, outgoing_control, outgoing_channeler_config, + rng, remote_public_key, receive_move_token_output, token_wanted, @@ -592,8 +608,14 @@ where }?; // Cancel all pending requests to this friend: - cancel_pending_requests(m_state, send_commands, outgoing_control, remote_public_key); - cancel_pending_user_requests(m_state, outgoing_control, remote_public_key); + cancel_pending_requests( + m_state, + send_commands, + outgoing_control, + rng, + remote_public_key, + ); + cancel_pending_user_requests(m_state, outgoing_control, rng, remote_public_key); // Save remote incoming inconsistency details: let new_remote_reset_terms = remote_reset_terms; diff --git a/components/funder/src/handler/handle_liveness.rs b/components/funder/src/handler/handle_liveness.rs index 9bca7e865..ef6a99a05 100644 --- a/components/funder/src/handler/handle_liveness.rs +++ b/components/funder/src/handler/handle_liveness.rs @@ -1,6 +1,9 @@ -use common::canonical_serialize::CanonicalSerialize; use std::fmt::Debug; +use common::canonical_serialize::CanonicalSerialize; + +use crypto::crypto_rand::CryptoRandom; + use proto::funder::messages::{FriendStatus, FunderOutgoingControl}; use crate::types::IncomingLivenessMessage; @@ -19,15 +22,17 @@ pub enum HandleLivenessError { FriendAlreadyOnline, } -pub fn handle_liveness_message( +pub fn handle_liveness_message( m_state: &mut MutableFunderState, m_ephemeral: &mut MutableEphemeral, send_commands: &mut SendCommands, outgoing_control: &mut Vec>, + rng: &R, liveness_message: IncomingLivenessMessage, ) -> Result<(), HandleLivenessError> where B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, + R: CryptoRandom, { match liveness_message { IncomingLivenessMessage::Online(friend_public_key) => { @@ -72,8 +77,14 @@ where } // Cancel all messages pending for this friend: - cancel_pending_requests(m_state, send_commands, outgoing_control, &friend_public_key); - cancel_pending_user_requests(m_state, outgoing_control, &friend_public_key); + cancel_pending_requests( + m_state, + send_commands, + outgoing_control, + rng, + &friend_public_key, + ); + cancel_pending_user_requests(m_state, outgoing_control, rng, &friend_public_key); } }; Ok(()) @@ -158,6 +169,7 @@ mod tests { &mut m_ephemeral, &mut send_commands, &mut outgoing_control, + &rng1, liveness_message, ) .unwrap(); diff --git a/components/funder/src/handler/handler.rs b/components/funder/src/handler/handler.rs index 3a6853584..8bc912a62 100644 --- a/components/funder/src/handler/handler.rs +++ b/components/funder/src/handler/handler.rs @@ -93,11 +93,12 @@ where FunderIncoming::Comm(incoming_comm) => { match incoming_comm { - FunderIncomingComm::Liveness(liveness_message) => handle_liveness_message::( + FunderIncomingComm::Liveness(liveness_message) => handle_liveness_message::( &mut m_state, &mut m_ephemeral, &mut send_commands, &mut outgoing_control, + rng, liveness_message, ) .map_err(FunderHandlerError::HandleLivenessError)?, From 7e8a3e44ad93bd157a7d88572eaa1c8a98434816 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 16:07:16 +0300 Subject: [PATCH 124/248] Updated comment. --- components/funder/src/handler/canceler.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/funder/src/handler/canceler.rs b/components/funder/src/handler/canceler.rs index 2cf1d63da..4876b3912 100644 --- a/components/funder/src/handler/canceler.rs +++ b/components/funder/src/handler/canceler.rs @@ -36,7 +36,7 @@ pub fn reply_with_cancel( send_commands.set_try_send(remote_public_key); } -/// Remove a local transaction +/// Remove a local transaction (Where this node is the buyer side) fn remove_transaction(m_state: &mut MutableFunderState, rng: &R, request_id: &Uid) where B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, From 0d051f5c3eb4616cd2e6546c3b52ed4d57e4e621 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 16:40:40 +0300 Subject: [PATCH 125/248] Updated handle_request_send_funds(): taking our fee. --- .../funder/src/handler/handle_friend.rs | 41 ++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/components/funder/src/handler/handle_friend.rs b/components/funder/src/handler/handle_friend.rs index e87145687..8b3130b23 100644 --- a/components/funder/src/handler/handle_friend.rs +++ b/components/funder/src/handler/handle_friend.rs @@ -27,6 +27,8 @@ use crate::state::FunderMutation; use crate::ephemeral::Ephemeral; +use crate::mutual_credit::types::calc_fee; + use crate::handler::canceler::{ cancel_local_pending_transactions, cancel_pending_requests, cancel_pending_user_requests, reply_with_cancel, @@ -144,7 +146,7 @@ fn handle_request_send_funds( ephemeral: &Ephemeral, send_commands: &mut SendCommands, remote_public_key: &PublicKey, - request_send_funds: RequestSendFundsOp, + mut request_send_funds: RequestSendFundsOp, ) where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, { @@ -171,6 +173,7 @@ fn handle_request_send_funds( return; } + // We are not the destination of this request. // The node on the route has to be one of our friends: let next_public_key = request_send_funds.route.index_to_pk(next_index).unwrap(); let friend_exists = m_state.state().friends.contains_key(next_public_key); @@ -184,15 +187,33 @@ fn handle_request_send_funds( false }; - if !friend_ready { - reply_with_cancel( - m_state, - send_commands, - remote_public_key, - &request_send_funds.request_id, - ); - return; - } + // Attempt to take our fee for forwarding the request. + // Note that the rate is determined by the rate we set with the node that sent us the request + // (And **not** with the node that we forward the request to). + let rate = &m_state.state().friends.get(remote_public_key).unwrap().rate; + let opt_local_fee = calc_fee(rate, request_send_funds.dest_payment); + + let request_id = request_send_funds.request_id.clone(); + + // Make sure that calc_fee() worked, and that we can take this amount of credits: + let opt_request_send_funds = if let Some(local_fee) = opt_local_fee { + if let Some(new_left_fees) = request_send_funds.left_fees.checked_sub(local_fee) { + request_send_funds.left_fees = new_left_fees; + Some(request_send_funds) + } else { + None + } + } else { + None + }; + + let request_send_funds = match (opt_request_send_funds, friend_ready) { + (Some(request_send_funds), true) => request_send_funds, + _ => { + reply_with_cancel(m_state, send_commands, remote_public_key, &request_id); + return; + } + }; // Queue message to the next node. forward_request(m_state, send_commands, request_send_funds); From 5db927130994928d142675f100ca89a1d20285a0 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 17:00:42 +0300 Subject: [PATCH 126/248] Updated handle_response_send_funds() for the case of response heading locally. --- .../funder/src/handler/handle_friend.rs | 36 +++++++++---------- components/proto/src/funder/signature_buff.rs | 23 ++++++++++++ 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/components/funder/src/handler/handle_friend.rs b/components/funder/src/handler/handle_friend.rs index 8b3130b23..4ecd09875 100644 --- a/components/funder/src/handler/handle_friend.rs +++ b/components/funder/src/handler/handle_friend.rs @@ -10,7 +10,7 @@ use proto::funder::messages::{ FunderOutgoingControl, MoveTokenRequest, PendingTransaction, RequestResult, RequestSendFundsOp, ResetTerms, ResponseSendFundsOp, TransactionResult, }; -use proto::funder::signature_buff::verify_move_token; +use proto::funder::signature_buff::{prepare_commit, verify_move_token}; use crate::mutual_credit::incoming::{ IncomingCancelSendFundsOp, IncomingCollectSendFundsOp, IncomingMessage, @@ -230,23 +230,23 @@ fn handle_response_send_funds( { match find_request_origin(m_state.state(), &response_send_funds.request_id).cloned() { None => { - unimplemented!(); - /* - // We are the origin of this request, and we got a response. - // We provide a receipt to the user: - let receipt = prepare_receipt(&response_send_funds, &pending_transaction); - - let response_send_funds_result = ResponseSendFundsResult::Success(receipt.clone()); - outgoing_control.push(FunderOutgoingControl::ResponseReceived(ResponseReceived { - request_id: pending_transaction.request_id, - result: response_send_funds_result, - })); - // We make our own copy of the receipt, in case the user abruptly crashes. - // In that case the user will be able to obtain the receipt again later. - let funder_mutation = - FunderMutation::AddReceipt((pending_transaction.request_id, receipt)); - m_state.mutate(funder_mutation); - */ + // Send transaction result to user: + let src_plain_lock = m_state + .state() + .open_transactions + .get(&response_send_funds.request_id) + .unwrap() + .src_plain_lock + .clone(); + + let commit = prepare_commit(&response_send_funds, &pending_transaction, src_plain_lock); + + let transaction_result = TransactionResult { + request_id: response_send_funds.request_id.clone(), + result: RequestResult::Success(commit), + }; + let transaction_result = + outgoing_control.push(FunderOutgoingControl::TransactionResult(transaction_result)); } Some(friend_public_key) => { // Queue this response message to another token channel: diff --git a/components/proto/src/funder/signature_buff.rs b/components/proto/src/funder/signature_buff.rs index de967acf3..3c9d4be8b 100644 --- a/components/proto/src/funder/signature_buff.rs +++ b/components/proto/src/funder/signature_buff.rs @@ -1,6 +1,7 @@ use byteorder::{BigEndian, WriteBytesExt}; use crypto::hash::{self, sha_512_256, HashResult}; +use crypto::hash_lock::PlainLock; use crypto::identity::{verify_signature, PublicKey}; use crypto::invoice_id::InvoiceId; @@ -80,6 +81,28 @@ pub fn verify_receipt(receipt: &Receipt, public_key: &PublicKey) -> bool { verify_signature(&data, public_key, &receipt.signature) } +/// Create a Commit (out of band) message given a ResponseSendFunds +pub fn prepare_commit( + response_send_funds: &ResponseSendFundsOp, + pending_transaction: &PendingTransaction, + src_plain_lock: PlainLock, +) -> Commit { + let mut hash_buff = Vec::new(); + hash_buff.extend_from_slice(&pending_transaction.request_id); + hash_buff.extend_from_slice(&pending_transaction.route.hash()); + hash_buff.extend_from_slice(&response_send_funds.rand_nonce); + let response_hash = hash::sha_512_256(&hash_buff); + // = sha512/256(requestId || sha512/256(route) || randNonce) + + Commit { + response_hash, + dest_payment: pending_transaction.dest_payment, + src_plain_lock, + dest_hashed_lock: response_send_funds.dest_hashed_lock.clone(), + signature: response_send_funds.signature.clone(), + } +} + /// Verify that a given Commit signature is valid fn verify_commit( commit: &Commit, From ade3cb24fe5a7d9f02a336c308bde29f491f2e36 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 17:05:50 +0300 Subject: [PATCH 127/248] Changed remove_transaction() to be public. --- components/funder/src/handler/canceler.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/funder/src/handler/canceler.rs b/components/funder/src/handler/canceler.rs index 4876b3912..d82935cca 100644 --- a/components/funder/src/handler/canceler.rs +++ b/components/funder/src/handler/canceler.rs @@ -37,7 +37,7 @@ pub fn reply_with_cancel( } /// Remove a local transaction (Where this node is the buyer side) -fn remove_transaction(m_state: &mut MutableFunderState, rng: &R, request_id: &Uid) +pub fn remove_transaction(m_state: &mut MutableFunderState, rng: &R, request_id: &Uid) where B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, R: CryptoRandom, From f213a075ceda51cc65d098d0edbef89e1e4d8d36 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 17:06:33 +0300 Subject: [PATCH 128/248] Updated handle_cancel_send_funds(): Calling remove_transaction(). --- .../funder/src/handler/handle_friend.rs | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/components/funder/src/handler/handle_friend.rs b/components/funder/src/handler/handle_friend.rs index 4ecd09875..536456a8f 100644 --- a/components/funder/src/handler/handle_friend.rs +++ b/components/funder/src/handler/handle_friend.rs @@ -31,7 +31,7 @@ use crate::mutual_credit::types::calc_fee; use crate::handler::canceler::{ cancel_local_pending_transactions, cancel_pending_requests, cancel_pending_user_requests, - reply_with_cancel, + remove_transaction, reply_with_cancel, }; use crate::handler::sender::SendCommands; use crate::handler::state_wrap::{MutableEphemeral, MutableFunderState}; @@ -261,25 +261,29 @@ fn handle_response_send_funds( } } -fn handle_cancel_send_funds( +fn handle_cancel_send_funds( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, outgoing_control: &mut Vec>, + rng: &R, cancel_send_funds: CancelSendFundsOp, pending_transaction: PendingTransaction, ) where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, + R: CryptoRandom, { match find_request_origin(m_state.state(), &cancel_send_funds.request_id).cloned() { None => { // We are the origin of this request, and we got a cancellation. - // We should pass it back to encryptor. - let request_result = RequestResult::Failure; + // Update buyer transactions (requests that were originated by us): + remove_transaction(m_state, rng, &cancel_send_funds.request_id); + + // Inform user about the transaction failure: outgoing_control.push(FunderOutgoingControl::TransactionResult( TransactionResult { request_id: pending_transaction.request_id, - result: request_result, + result: RequestResult::Failure, }, )); } @@ -309,15 +313,17 @@ fn handle_collect_send_funds( } /// Process valid incoming operations from remote side. -fn handle_move_token_output( +fn handle_move_token_output( m_state: &mut MutableFunderState, m_ephemeral: &mut MutableEphemeral, send_commands: &mut SendCommands, outgoing_control: &mut Vec>, + rng: &R, remote_public_key: &PublicKey, incoming_messages: Vec, ) where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, + R: CryptoRandom, { for incoming_message in incoming_messages { match incoming_message { @@ -350,6 +356,7 @@ fn handle_move_token_output( m_state, send_commands, outgoing_control, + rng, incoming_cancel, pending_transaction, ); @@ -533,6 +540,7 @@ fn handle_move_token_success( m_ephemeral, send_commands, outgoing_control, + rng, remote_public_key, incoming_messages, ); From 48e43ecf1a756342db51198bf0ea08281d8123bc Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 17:11:39 +0300 Subject: [PATCH 129/248] Some work on implementing handle_collect_send_funds(). Not done yet. --- .../funder/src/handler/handle_friend.rs | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/components/funder/src/handler/handle_friend.rs b/components/funder/src/handler/handle_friend.rs index 536456a8f..e7d5127f4 100644 --- a/components/funder/src/handler/handle_friend.rs +++ b/components/funder/src/handler/handle_friend.rs @@ -288,9 +288,9 @@ fn handle_cancel_send_funds( )); } Some(friend_public_key) => { - // Queue this failure message to another token channel: - let failure_op = BackwardsOp::Cancel(cancel_send_funds); - let friend_mutation = FriendMutation::PushBackPendingBackwardsOp(failure_op); + // Queue this Cancel message to another token channel: + let cancel_op = BackwardsOp::Cancel(cancel_send_funds); + let friend_mutation = FriendMutation::PushBackPendingBackwardsOp(cancel_op); let funder_mutation = FunderMutation::FriendMutation((friend_public_key.clone(), friend_mutation)); m_state.mutate(funder_mutation); @@ -309,7 +309,29 @@ fn handle_collect_send_funds( ) where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, { - unimplemented!(); + // Check if we are the origin of this transaction (Did we send the RequestSendFundsOp + // message?): + match find_request_origin(m_state.state(), &collect_send_funds.request_id).cloned() { + None => { + // We are the origin of this request, and we got a Collect message + // TODO: + // - Create a receipt. + // - Update payment status + // - Remove transaction + // - Send the receipt to the user + unimplemented!(); + } + Some(friend_public_key) => { + // Queue this Collect message to another token channel: + let collect_op = BackwardsOp::Collect(collect_send_funds); + let friend_mutation = FriendMutation::PushBackPendingBackwardsOp(collect_op); + let funder_mutation = + FunderMutation::FriendMutation((friend_public_key.clone(), friend_mutation)); + m_state.mutate(funder_mutation); + + send_commands.set_try_send(&friend_public_key); + } + }; } /// Process valid incoming operations from remote side. From 90e92bb0a21b693dd2c1927ddc763235e1885699 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 17:32:35 +0300 Subject: [PATCH 130/248] Keeping received response in OpenTransaction. --- components/funder/src/report.rs | 1 + components/funder/src/state.rs | 24 +++++++++++++++++------- components/proto/src/funder/messages.rs | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/components/funder/src/report.rs b/components/funder/src/report.rs index 75accbd94..46941ec60 100644 --- a/components/funder/src/report.rs +++ b/components/funder/src/report.rs @@ -359,6 +359,7 @@ where Vec::new() } } + FunderMutation::SetTransactionResponse(_) => vec![], FunderMutation::UpdatePayment(_) | FunderMutation::RemovePayment(_) => { if funder_state_after.payments.len() != funder_state.payments.len() { vec![FunderReportMutation::SetNumPayments( diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index 91e9ef18f..ccd3abaea 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -9,7 +9,7 @@ use crypto::payment_id::PaymentId; use crypto::uid::Uid; use proto::app_server::messages::NamedRelayAddress; -use proto::funder::messages::{AddFriend, Receipt}; +use proto::funder::messages::{AddFriend, Receipt, ResponseSendFundsOp}; use crate::friend::{FriendMutation, FriendState}; @@ -85,6 +85,8 @@ pub struct OpenTransaction { pub payment_id: PaymentId, /// The plain part of a hash lock for the generated transaction. pub src_plain_lock: PlainLock, + /// A response (if we got one): + pub opt_response: Option, } #[allow(clippy::large_enum_variant)] @@ -98,8 +100,9 @@ pub enum FunderMutation { AddInvoice((InvoiceId, u128)), // (InvoiceId, total_dest_payment) AddIncomingTransaction((InvoiceId, Uid, PlainLock)), // (invoice_id, request_id, dest_plain_lock) RemoveInvoice(InvoiceId), - AddTransaction((Uid, PaymentId, PlainLock)), // (transaction_id, payment_id,src_plain_lock) - RemoveTransaction(Uid), // transaction_id + AddTransaction((Uid, PaymentId, PlainLock)), // (request_id, payment_id,src_plain_lock) + SetTransactionResponse((Uid, ResponseSendFundsOp)), // (request_id, response_send_funds) + RemoveTransaction(Uid), // request_id UpdatePayment((PaymentId, Payment)), RemovePayment(PaymentId), } @@ -177,17 +180,24 @@ where FunderMutation::RemoveInvoice(invoice_id) => { let _ = self.open_invoices.remove(invoice_id); } - FunderMutation::AddTransaction((transaction_id, payment_id, src_plain_lock)) => { + FunderMutation::AddTransaction((request_id, payment_id, src_plain_lock)) => { let open_transaction = OpenTransaction { payment_id: payment_id.clone(), src_plain_lock: src_plain_lock.clone(), + opt_response: None, }; let _ = self .open_transactions - .insert(transaction_id.clone(), open_transaction); + .insert(request_id.clone(), open_transaction); } - FunderMutation::RemoveTransaction(transaction_id) => { - let _ = self.open_transactions.remove(transaction_id); + FunderMutation::SetTransactionResponse((request_id, response_send_funds)) => { + let open_transaction = self.open_transactions.get_mut(&request_id).unwrap(); + // We assert that no response was received so far: + assert!(open_transaction.opt_response.take().is_none()); + open_transaction.opt_response = Some(response_send_funds.clone()); + } + FunderMutation::RemoveTransaction(request_id) => { + let _ = self.open_transactions.remove(request_id); } FunderMutation::UpdatePayment((payment_id, payment)) => { let _ = self.payments.insert(payment_id.clone(), payment.clone()); diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 881770438..ba54799de 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -179,7 +179,7 @@ pub struct Receipt { #[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)] pub enum TransactionStage { Request, - Response(HashedLock), // inner: dest_hashed_lock. TOOD: Change to use bcrypt + Response(HashedLock), // inner: dest_hashed_lock. } #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)] From 1f12cef17cb97c13cc2818e5e4a8e13fa6018753 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 17:37:43 +0300 Subject: [PATCH 131/248] Keeping response_send_funds in handle_response_send_funds(). --- components/funder/src/handler/handle_friend.rs | 5 +++++ components/funder/src/state.rs | 9 ++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/components/funder/src/handler/handle_friend.rs b/components/funder/src/handler/handle_friend.rs index e7d5127f4..f4082702d 100644 --- a/components/funder/src/handler/handle_friend.rs +++ b/components/funder/src/handler/handle_friend.rs @@ -230,6 +230,11 @@ fn handle_response_send_funds( { match find_request_origin(m_state.state(), &response_send_funds.request_id).cloned() { None => { + // Keep the response: + let funder_mutation = + FunderMutation::SetTransactionResponse(response_send_funds.clone()); + m_state.mutate(funder_mutation); + // Send transaction result to user: let src_plain_lock = m_state .state() diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index ccd3abaea..8185a1ad3 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -101,7 +101,7 @@ pub enum FunderMutation { AddIncomingTransaction((InvoiceId, Uid, PlainLock)), // (invoice_id, request_id, dest_plain_lock) RemoveInvoice(InvoiceId), AddTransaction((Uid, PaymentId, PlainLock)), // (request_id, payment_id,src_plain_lock) - SetTransactionResponse((Uid, ResponseSendFundsOp)), // (request_id, response_send_funds) + SetTransactionResponse(ResponseSendFundsOp), // (request_id, response_send_funds) RemoveTransaction(Uid), // request_id UpdatePayment((PaymentId, Payment)), RemovePayment(PaymentId), @@ -190,8 +190,11 @@ where .open_transactions .insert(request_id.clone(), open_transaction); } - FunderMutation::SetTransactionResponse((request_id, response_send_funds)) => { - let open_transaction = self.open_transactions.get_mut(&request_id).unwrap(); + FunderMutation::SetTransactionResponse(response_send_funds) => { + let open_transaction = self + .open_transactions + .get_mut(&response_send_funds.request_id) + .unwrap(); // We assert that no response was received so far: assert!(open_transaction.opt_response.take().is_none()); open_transaction.opt_response = Some(response_send_funds.clone()); From 1663320e4a98452d059e95552ec9d53e01435ffe Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 17:44:22 +0300 Subject: [PATCH 132/248] Fixed destTotalPayment -> totalDestPayment. --- doc/docs/atomic_proposal.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/docs/atomic_proposal.md b/doc/docs/atomic_proposal.md index bbfff0fa5..a31e1d878 100644 --- a/doc/docs/atomic_proposal.md +++ b/doc/docs/atomic_proposal.md @@ -499,7 +499,7 @@ struct Receipt { srcPlainLock @2: PlainLock; destPlainLock @3: PlainLock; destPayment @4: CustomUInt128; - destTotalPayment @4: CustomUInt128; + totalDestPayment @4: CustomUInt128; signature @5: Signature; # Signature{key=destinationKey}( # sha512/256("FUNDS_RESPONSE") || @@ -507,7 +507,7 @@ struct Receipt { # srcHashedLock || # dstHashedLock || # destPayment || - # destTotalPayment || + # totalDestPayment || # invoiceId # ) } From f845a28d8b5ead9e644fc5b4e2bf5cbe81cf57d4 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 17:44:42 +0300 Subject: [PATCH 133/248] Updated signature comment for Receipt. --- components/proto/src/funder/messages.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index ba54799de..4eac43d31 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -168,12 +168,17 @@ pub struct Receipt { pub dest_payment: u128, pub total_dest_payment: u128, pub signature: Signature, - // Signature{key=recipientKey}( - // "FUND_SUCCESS" || - // sha512/256(requestId || sha512/256(route) || randNonce) || - // invoiceId || - // destPayment - // ) + /* + # Signature{key=destinationKey}( + # sha512/256("FUNDS_RESPONSE") || + # sha512/256(requestId || sha512/256(route) || randNonce) || + # srcHashedLock || + # dstHashedLock || + # destPayment || + # totalDestPayment || + # invoiceId + # ) + */ } #[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)] From e6685c9656cee53ce885e2afe7f6dd28dcd7e35f Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 17:45:16 +0300 Subject: [PATCH 134/248] Implemented prepare_receipt() at signature_buff.rs. --- components/proto/src/funder/signature_buff.rs | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/components/proto/src/funder/signature_buff.rs b/components/proto/src/funder/signature_buff.rs index 3c9d4be8b..528db3d2f 100644 --- a/components/proto/src/funder/signature_buff.rs +++ b/components/proto/src/funder/signature_buff.rs @@ -9,7 +9,8 @@ use common::canonical_serialize::CanonicalSerialize; use common::int_convert::usize_to_u64; use super::messages::{ - Commit, MoveToken, MultiCommit, PendingTransaction, Receipt, ResponseSendFundsOp, + CollectSendFundsOp, Commit, MoveToken, MultiCommit, PendingTransaction, Receipt, + ResponseSendFundsOp, }; pub const FUNDS_RESPONSE_PREFIX: &[u8] = b"FUND_RESPONSE"; @@ -66,6 +67,30 @@ pub fn prepare_receipt( } */ +pub fn prepare_receipt( + collect_send_funds: &CollectSendFundsOp, + response_send_funds: &ResponseSendFundsOp, + pending_transaction: &PendingTransaction, +) -> Receipt { + let mut hash_buff = Vec::new(); + hash_buff.extend_from_slice(&pending_transaction.request_id); + hash_buff.extend_from_slice(&pending_transaction.route.hash()); + hash_buff.extend_from_slice(&response_send_funds.rand_nonce); + let response_hash = hash::sha_512_256(&hash_buff); + // = sha512/256(requestId || sha512/256(route) || randNonce) + + Receipt { + response_hash, + // = sha512/256(requestId || sha512/256(route) || randNonce) + invoice_id: pending_transaction.invoice_id.clone(), + src_plain_lock: collect_send_funds.src_plain_lock.clone(), + dest_plain_lock: collect_send_funds.dest_plain_lock.clone(), + dest_payment: pending_transaction.dest_payment, + total_dest_payment: pending_transaction.total_dest_payment, + signature: response_send_funds.signature.clone(), + } +} + /// Verify that a given receipt's signature is valid pub fn verify_receipt(receipt: &Receipt, public_key: &PublicKey) -> bool { let mut data = Vec::new(); From e960ab03dc06559f98f39cc7d1a3a0de47123e18 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 18:06:52 +0300 Subject: [PATCH 135/248] Implemented handle_collect_send_funds(). --- .../funder/src/handler/handle_friend.rs | 87 +++++++++++++++++-- 1 file changed, 78 insertions(+), 9 deletions(-) diff --git a/components/funder/src/handler/handle_friend.rs b/components/funder/src/handler/handle_friend.rs index f4082702d..1cf474f86 100644 --- a/components/funder/src/handler/handle_friend.rs +++ b/components/funder/src/handler/handle_friend.rs @@ -3,6 +3,7 @@ use std::fmt::Debug; use crypto::crypto_rand::CryptoRandom; use crypto::identity::{PublicKey, Signature, SIGNATURE_LEN}; +use crypto::uid::Uid; use proto::app_server::messages::RelayAddress; use proto::funder::messages::{ @@ -10,7 +11,7 @@ use proto::funder::messages::{ FunderOutgoingControl, MoveTokenRequest, PendingTransaction, RequestResult, RequestSendFundsOp, ResetTerms, ResponseSendFundsOp, TransactionResult, }; -use proto::funder::signature_buff::{prepare_commit, verify_move_token}; +use proto::funder::signature_buff::{prepare_commit, prepare_receipt, verify_move_token}; use crate::mutual_credit::incoming::{ IncomingCancelSendFundsOp, IncomingCollectSendFundsOp, IncomingMessage, @@ -23,7 +24,7 @@ use crate::types::{create_pending_transaction, ChannelerConfig}; use crate::friend::{ BackwardsOp, ChannelInconsistent, ChannelStatus, FriendMutation, SentLocalRelays, }; -use crate::state::FunderMutation; +use crate::state::{FunderMutation, Payment}; use crate::ephemeral::Ephemeral; @@ -305,26 +306,93 @@ fn handle_cancel_send_funds( }; } -fn handle_collect_send_funds( +fn handle_collect_send_funds( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, outgoing_control: &mut Vec>, + rng: &R, collect_send_funds: CollectSendFundsOp, pending_transaction: PendingTransaction, ) where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, + R: CryptoRandom, { // Check if we are the origin of this transaction (Did we send the RequestSendFundsOp // message?): match find_request_origin(m_state.state(), &collect_send_funds.request_id).cloned() { None => { // We are the origin of this request, and we got a Collect message - // TODO: - // - Create a receipt. - // - Update payment status - // - Remove transaction - // - Send the receipt to the user - unimplemented!(); + let open_transaction = m_state + .state() + .open_transactions + .get(&collect_send_funds.request_id) + .unwrap(); + + let payment = m_state + .state() + .payments + .get(&open_transaction.payment_id) + .unwrap(); + + // Update payment status: + let opt_new_payment = match payment { + Payment::NewTransactions(new_transactions) => { + // Create a Receipt: + let receipt = prepare_receipt( + &collect_send_funds, + open_transaction.opt_response.as_ref().unwrap(), + &pending_transaction, + ); + let ack_uid = Uid::new(rng); + Some(Payment::Success(( + new_transactions.num_transactions.checked_sub(1).unwrap(), + receipt, + ack_uid, + ))) + } + Payment::InProgress(num_transactions) => { + // Create a Receipt: + let receipt = prepare_receipt( + &collect_send_funds, + open_transaction.opt_response.as_ref().unwrap(), + &pending_transaction, + ); + let ack_uid = Uid::new(rng); + Some(Payment::Success(( + num_transactions.checked_sub(1).unwrap(), + receipt, + ack_uid, + ))) + } + Payment::Success((num_transactions, receipt, ack_uid)) => Some(Payment::Success(( + num_transactions.checked_sub(1).unwrap(), + receipt.clone(), + ack_uid.clone(), + ))), + Payment::Canceled(_) => unreachable!(), + Payment::AfterSuccessAck(num_transactions) => { + let new_num_transactions = num_transactions.checked_sub(1).unwrap(); + if new_num_transactions > 0 { + Some(Payment::AfterSuccessAck(new_num_transactions)) + } else { + None + } + } + }; + + let funder_mutation = if let Some(new_payment) = opt_new_payment { + // Update payment: + FunderMutation::UpdatePayment((open_transaction.payment_id.clone(), new_payment)) + } else { + FunderMutation::RemovePayment(open_transaction.payment_id.clone()) + // Remove payment: + }; + m_state.mutate(funder_mutation); + + // Remove transaction: + let funder_mutation = + FunderMutation::RemoveTransaction(collect_send_funds.request_id.clone()); + m_state.mutate(funder_mutation); } Some(friend_public_key) => { // Queue this Collect message to another token channel: @@ -396,6 +464,7 @@ fn handle_move_token_output( m_state, send_commands, outgoing_control, + rng, incoming_collect, pending_transaction, ); From a39e3cee8814a19324f879fd5a090b472163dc2e Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 19:07:25 +0300 Subject: [PATCH 136/248] Funder: Renamed failure -> cancel in sender.rs. --- components/funder/src/handler/sender.rs | 112 ++++++++---------------- 1 file changed, 38 insertions(+), 74 deletions(-) diff --git a/components/funder/src/handler/sender.rs b/components/funder/src/handler/sender.rs index 403514d64..f205ca994 100644 --- a/components/funder/src/handler/sender.rs +++ b/components/funder/src/handler/sender.rs @@ -278,7 +278,7 @@ async fn send_friend_iter1<'a, B, R>( identity_client: &'a mut IdentityClient, rng: &'a R, max_operations_in_batch: usize, - failure_public_keys: &'a mut HashSet, + cancel_public_keys: &'a mut HashSet, mut outgoing_messages: &'a mut Vec>, outgoing_control: &'a mut Vec>, outgoing_channeler_config: &'a mut Vec>>, @@ -374,7 +374,7 @@ async fn send_friend_iter1<'a, B, R>( m_state, outgoing_channeler_config, outgoing_control, - failure_public_keys, + cancel_public_keys, friend_public_key, pending_move_token, identity_client, @@ -437,12 +437,12 @@ where } /// Queue an operation to a PendingMoveToken. -/// On failure, queue a failure to the relevant friend, -/// or (if we are the origin of the request): send a failure through the control -fn queue_operation_or_failure<'a, B>( +/// On cancel, queue a Cancel message to the relevant friend, +/// or (if we are the origin of the request): send a failure notification through the control. +fn queue_operation_or_cancel<'a, B>( m_state: &'a mut MutableFunderState, pending_move_token: &'a mut PendingMoveToken, - failure_public_keys: &'a mut HashSet, + cancel_public_keys: &'a mut HashSet, outgoing_control: &'a mut Vec>, operation: &'a FriendTcOp, ) -> Result<(), CollectOutgoingError> @@ -471,7 +471,7 @@ where match find_request_origin(m_state.state(), &request_send_funds.request_id).cloned() { Some(origin_public_key) => { // The friend with public key `origin_public_key` is the origin of this request. - // We send him back a failure message: + // We send him back a Cancel message: let pending_transaction = create_pending_transaction(request_send_funds); let cancel_send_funds = BackwardsOp::Cancel(create_cancel_send_funds(pending_transaction.request_id.clone())); let friend_mutation = FriendMutation::PushBackPendingBackwardsOp(cancel_send_funds); @@ -479,7 +479,7 @@ where FunderMutation::FriendMutation((origin_public_key.clone(), friend_mutation)); m_state.mutate(funder_mutation); - failure_public_keys.insert(origin_public_key.clone()); + cancel_public_keys.insert(origin_public_key.clone()); } None => { // We are the origin of this request @@ -494,41 +494,6 @@ where Ok(()) } -/* -/// Queue a user created request send funds message. -/// This is different from forwarding a request_send_funds message, because there is no -/// pending request we need to cancel in case of failure, and no failure to be queued. -/// Instead, we return a failure message back to the user. -async fn queue_user_request_send_funds<'a,B>(m_state: &'a mut MutableFunderState, - pending_move_token: &'a mut PendingMoveToken, - request_send_funds: &'a RequestSendFunds, - outgoing_control: &'a mut Vec>) - -> Result<(), CollectOutgoingError> -where - B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, -{ - - let operation = FriendTcOp::RequestSendFunds(request_send_funds.clone()); - match pending_move_token.queue_operation(&operation, m_state) { - Ok(()) => return Ok(()), - Err(PendingQueueError::MaxOperationsReached) => { - pending_move_token.token_wanted = true; - // We will send this message next time we have the token: - return Err(CollectOutgoingError::MaxOperationsReached); - } - Err(PendingQueueError::InsufficientTrust) => {}, - }; - - let response_received = ResponseReceived { - request_id: request_send_funds.request_id, - result: ResponseSendFundsResult::Failure(m_state.state().local_public_key.clone()), - }; - outgoing_control.push(FunderOutgoingControl::ResponseReceived(response_received)); - - Ok(()) -} -*/ - fn backwards_op_to_friend_tc_op<'a, B, R>( m_state: &'a mut MutableFunderState, backwards_op: BackwardsOp, @@ -548,12 +513,12 @@ where /// Given a friend with an incoming move token state, create the largest possible move token to /// send to the remote side. -/// Requests that fail to be processed are moved to the failure queues of the relevant friends. +/// Requests that fail to be processed are moved to the cancel queues of the relevant friends. fn collect_outgoing_move_token<'a, B, R>( m_state: &'a mut MutableFunderState, outgoing_channeler_config: &'a mut Vec>>, outgoing_control: &'a mut Vec>, - failure_public_keys: &'a mut HashSet, + cancel_public_keys: &'a mut HashSet, friend_public_key: &'a PublicKey, pending_move_token: &'a mut PendingMoveToken, identity_client: &'a mut IdentityClient, @@ -566,14 +531,12 @@ where /* - Check if last sent local address is up to date. - Collect as many operations as possible (Not more than max ops per batch) - 1. Responses (response, failure) + 1. Responses (response, cancel, collect) 2. Pending requests 3. User pending requests - When adding requests, check the following: - - Valid by freeze guard. - Valid from credits point of view. - - If a request is not valid, Pass it as a failure message to - relevant friend. + - If a request is not valid, queue a Cancel message to relevant friend. */ // Send update about local address if needed: @@ -635,10 +598,10 @@ where if friend.wanted_remote_max_debt != remote_max_debt { let operation = FriendTcOp::SetRemoteMaxDebt(friend.wanted_remote_max_debt); - queue_operation_or_failure( + queue_operation_or_cancel( m_state, pending_move_token, - failure_public_keys, + cancel_public_keys, outgoing_control, &operation )?; @@ -663,17 +626,17 @@ where } else { FriendTcOp::DisableRequests }; - queue_operation_or_failure( + queue_operation_or_cancel( m_state, pending_move_token, - failure_public_keys, + cancel_public_keys, outgoing_control, &friend_op )?; } let friend = m_state.state().friends.get(friend_public_key).unwrap(); - // Send pending responses (responses and failures) + // Send pending responses (Response, Cancel, Collect) // TODO: Possibly replace this clone with something more efficient later: let mut pending_backwards_ops = friend.pending_backwards_ops.clone(); while let Some(pending_backwards_op) = pending_backwards_ops.pop_front() { @@ -683,10 +646,11 @@ where identity_client, rng ); - queue_operation_or_failure( + queue_operation_or_cancel( m_state, pending_move_token, - failure_public_keys, + // Not required here, as no requests are being sent. + cancel_public_keys, outgoing_control, &pending_op )?; @@ -704,10 +668,10 @@ where let mut pending_requests = friend.pending_requests.clone(); while let Some(pending_request) = pending_requests.pop_front() { let pending_op = FriendTcOp::RequestSendFunds(pending_request); - queue_operation_or_failure( + queue_operation_or_cancel( m_state, pending_move_token, - failure_public_keys, + cancel_public_keys, outgoing_control, &pending_op )?; @@ -723,10 +687,10 @@ where let mut pending_user_requests = friend.pending_user_requests.clone(); while let Some(request_send_funds) = pending_user_requests.pop_front() { let pending_op = FriendTcOp::RequestSendFunds(request_send_funds); - queue_operation_or_failure( + queue_operation_or_cancel( m_state, pending_move_token, - failure_public_keys, + cancel_public_keys, outgoing_control, &pending_op )?; @@ -738,7 +702,7 @@ where Ok(()) } -fn append_failures_to_move_token<'a, B, R>( +fn append_cancels_to_move_token<'a, B, R>( m_state: &'a mut MutableFunderState, friend_public_key: &'a PublicKey, pending_move_token: &'a mut PendingMoveToken, @@ -751,7 +715,7 @@ where { let friend = m_state.state().friends.get(friend_public_key).unwrap(); - // Send pending responses (responses and failures) + // Send pending responses (Response, Cancel, Collect): // TODO: Possibly replace this clone with something more efficient later: let mut pending_backwards_ops = friend.pending_backwards_ops.clone(); while let Some(pending_backwards_op) = pending_backwards_ops.pop_front() { @@ -762,12 +726,12 @@ where rng ); // TODO: Find a more elegant way to do this: - let mut dummy_failure_public_keys = HashSet::new(); + let mut dummy_cancel_public_keys = HashSet::new(); let mut dummy_outgoing_control = Vec::new(); - queue_operation_or_failure( + queue_operation_or_cancel( m_state, pending_move_token, - &mut dummy_failure_public_keys, + &mut dummy_cancel_public_keys, &mut dummy_outgoing_control, &pending_op )?; @@ -854,17 +818,17 @@ async fn send_move_token<'a, B, R>( )); } -fn init_failure_pending_move_token( +fn init_cancel_pending_move_token( m_state: &mut MutableFunderState, ephemeral: &Ephemeral, max_operations_in_batch: usize, - failure_public_keys: &HashSet, + cancel_public_keys: &HashSet, pending_move_tokens: &mut HashMap>, ) where B: Clone + Eq + CanonicalSerialize + Debug, { let pending_move_token_keys = pending_move_tokens.keys().cloned().collect::>(); - for friend_public_key in failure_public_keys { + for friend_public_key in cancel_public_keys { // Make sure that this friend is ready, // and that it doesn't already have a PendingMoveToken: @@ -923,7 +887,7 @@ where let mut pending_move_tokens: HashMap> = HashMap::new(); // First iteration: - let mut failure_public_keys = HashSet::new(); + let mut cancel_public_keys = HashSet::new(); for (friend_public_key, friend_send_commands) in &send_commands.send_commands { if !ephemeral.liveness.is_online(friend_public_key) { continue; @@ -936,7 +900,7 @@ where identity_client, rng, max_operations_in_batch, - &mut failure_public_keys, + &mut cancel_public_keys, &mut outgoing_messages, &mut outgoing_control, &mut outgoing_channeler_config @@ -945,18 +909,18 @@ where // Create PendingMoveToken-s for all the friends that were queued // new pending messages during `send_friend_iter1`: - init_failure_pending_move_token( + init_cancel_pending_move_token( m_state, ephemeral, max_operations_in_batch, - &failure_public_keys, + &cancel_public_keys, &mut pending_move_tokens, ); - // Second iteration (Attempt to queue failures created in the first iteration): + // Second iteration (Attempt to queue Cancel-s created in the first iteration): for (friend_public_key, pending_move_token) in &mut pending_move_tokens { assert!(ephemeral.liveness.is_online(&friend_public_key)); - let _ = append_failures_to_move_token( + let _ = append_cancels_to_move_token( m_state, friend_public_key, pending_move_token, From 72d0a87185444d55d2190f6eab4ab5667ee05e37 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 19:09:55 +0300 Subject: [PATCH 137/248] Fixed unused warnings for funder/src/handler/utils.rs. --- components/funder/src/handler/mod.rs | 1 - components/funder/src/handler/utils.rs | 13 ++----------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index cf5c8a8ea..3602b4103 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -14,7 +14,6 @@ mod handler; mod sender; #[allow(unused)] mod state_wrap; -#[allow(unused)] mod utils; // #[cfg(test)] diff --git a/components/funder/src/handler/utils.rs b/components/funder/src/handler/utils.rs index 842de369b..0fdeca9bd 100644 --- a/components/funder/src/handler/utils.rs +++ b/components/funder/src/handler/utils.rs @@ -2,22 +2,13 @@ use std::fmt::Debug; use common::canonical_serialize::CanonicalSerialize; -use crypto::crypto_rand::CryptoRandom; use crypto::identity::PublicKey; use crypto::uid::Uid; -use proto::app_server::messages::RelayAddress; -use proto::funder::messages::FunderOutgoingControl; -use proto::report::messages::{FunderReportMutation, FunderReportMutations}; +use crate::state::FunderState; -use identity::IdentityClient; - -use crate::state::{FunderMutation, FunderState}; - -use crate::ephemeral::{Ephemeral, EphemeralMutation}; +use crate::ephemeral::Ephemeral; use crate::friend::ChannelStatus; -use crate::report::{ephemeral_mutation_to_report_mutations, funder_mutation_to_report_mutations}; -use crate::types::{ChannelerConfig, FunderIncoming, FunderIncomingComm, FunderOutgoingComm}; /// Find the originator of a pending local request. /// This should be a pending remote request at some other friend. From 44b353d0d8a476686cf1039462a2ab048936bbaa Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 19:12:28 +0300 Subject: [PATCH 138/248] state_wrap.rs: Fixed unused warnings. --- components/funder/src/handler/mod.rs | 1 - components/funder/src/handler/state_wrap.rs | 15 +++++---------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index 3602b4103..843e692b8 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -12,7 +12,6 @@ mod handle_liveness; mod handler; #[allow(unused)] mod sender; -#[allow(unused)] mod state_wrap; mod utils; diff --git a/components/funder/src/handler/state_wrap.rs b/components/funder/src/handler/state_wrap.rs index 1d808e0ae..286fd8fc8 100644 --- a/components/funder/src/handler/state_wrap.rs +++ b/components/funder/src/handler/state_wrap.rs @@ -5,23 +5,16 @@ use common::canonical_serialize::CanonicalSerialize; use crypto::crypto_rand::{CryptoRandom, RandValue}; use crypto::hash_lock::PlainLock; use crypto::identity::PublicKey; -use crypto::uid::Uid; -use proto::app_server::messages::RelayAddress; -use proto::funder::messages::{FunderOutgoingControl, PendingTransaction}; -use proto::report::messages::{FunderReportMutation, FunderReportMutations}; +use proto::funder::messages::PendingTransaction; use identity::IdentityClient; use crate::state::{FunderMutation, FunderState}; use crate::ephemeral::{Ephemeral, EphemeralMutation}; -use crate::friend::{BackwardsOp, ChannelStatus, FriendMutation}; -use crate::report::{ephemeral_mutation_to_report_mutations, funder_mutation_to_report_mutations}; -use crate::types::{ - create_response_send_funds, ChannelerConfig, FunderIncoming, FunderIncomingComm, - FunderOutgoingComm, UnsignedResponseSendFundsOp, -}; +use crate::friend::{BackwardsOp, FriendMutation}; +use crate::types::create_response_send_funds; #[derive(Debug, Clone)] pub struct SemiResponse { @@ -121,6 +114,7 @@ where } } +/* pub struct MutableFunderStateSigned { initial_state: FunderState, state: FunderState, @@ -128,6 +122,7 @@ pub struct MutableFunderStateSigned { } impl MutableFunderStateSigned where B: Clone + CanonicalSerialize + PartialEq + Eq + Debug {} +*/ pub struct MutableEphemeral { ephemeral: Ephemeral, From ad2c2441c96bb80ee7ca45b278b778d10ee6a9b7 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 19:12:54 +0300 Subject: [PATCH 139/248] Removed unused comment. --- components/funder/src/handler/state_wrap.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/components/funder/src/handler/state_wrap.rs b/components/funder/src/handler/state_wrap.rs index 286fd8fc8..c9c25b3a5 100644 --- a/components/funder/src/handler/state_wrap.rs +++ b/components/funder/src/handler/state_wrap.rs @@ -114,16 +114,6 @@ where } } -/* -pub struct MutableFunderStateSigned { - initial_state: FunderState, - state: FunderState, - mutations: Vec>, -} - -impl MutableFunderStateSigned where B: Clone + CanonicalSerialize + PartialEq + Eq + Debug {} -*/ - pub struct MutableEphemeral { ephemeral: Ephemeral, mutations: Vec, From a53e2a8f71a37fcf56d8b6537148680faef5f8ca Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 19:17:13 +0300 Subject: [PATCH 140/248] sender.rs: Fixed unused warnings. --- components/funder/src/handler/mod.rs | 1 - components/funder/src/handler/sender.rs | 40 +++++-------------------- 2 files changed, 8 insertions(+), 33 deletions(-) diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index 843e692b8..0b11ac486 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -10,7 +10,6 @@ mod handle_init; mod handle_liveness; #[allow(unused)] mod handler; -#[allow(unused)] mod sender; mod state_wrap; mod utils; diff --git a/components/funder/src/handler/sender.rs b/components/funder/src/handler/sender.rs index f205ca994..26c5095b1 100644 --- a/components/funder/src/handler/sender.rs +++ b/components/funder/src/handler/sender.rs @@ -5,7 +5,6 @@ use common::canonical_serialize::CanonicalSerialize; use crypto::crypto_rand::{CryptoRandom, RandValue}; use crypto::identity::PublicKey; -use crypto::hash_lock::PlainLock; use proto::app_server::messages::RelayAddress; use proto::funder::messages::{ @@ -377,8 +376,6 @@ async fn send_friend_iter1<'a, B, R>( cancel_public_keys, friend_public_key, pending_move_token, - identity_client, - rng ); } @@ -494,18 +491,11 @@ where Ok(()) } -fn backwards_op_to_friend_tc_op<'a, B, R>( - m_state: &'a mut MutableFunderState, +fn backwards_op_to_friend_tc_op( backwards_op: BackwardsOp, - mut identity_client: &'a mut IdentityClient, - rng: &'a R, -) -> FriendTcOp -where - B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, - R: CryptoRandom, -{ +) -> FriendTcOp { match backwards_op { - BackwardsOp::Response(response) => FriendTcOp::ResponseSendFunds(response), + BackwardsOp::Response(response_send_funds) => FriendTcOp::ResponseSendFunds(response_send_funds), BackwardsOp::Cancel(cancel_send_funds) => FriendTcOp::CancelSendFunds(cancel_send_funds), BackwardsOp::Collect(collect_send_funds) => FriendTcOp::CollectSendFunds(collect_send_funds), } @@ -514,19 +504,16 @@ where /// Given a friend with an incoming move token state, create the largest possible move token to /// send to the remote side. /// Requests that fail to be processed are moved to the cancel queues of the relevant friends. -fn collect_outgoing_move_token<'a, B, R>( +fn collect_outgoing_move_token<'a, B>( m_state: &'a mut MutableFunderState, outgoing_channeler_config: &'a mut Vec>>, outgoing_control: &'a mut Vec>, cancel_public_keys: &'a mut HashSet, friend_public_key: &'a PublicKey, pending_move_token: &'a mut PendingMoveToken, - identity_client: &'a mut IdentityClient, - rng: &'a R, ) -> Result<(), CollectOutgoingError> where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, - R: CryptoRandom, { /* - Check if last sent local address is up to date. @@ -641,10 +628,7 @@ where let mut pending_backwards_ops = friend.pending_backwards_ops.clone(); while let Some(pending_backwards_op) = pending_backwards_ops.pop_front() { let pending_op = backwards_op_to_friend_tc_op( - m_state, pending_backwards_op, - identity_client, - rng ); queue_operation_or_cancel( m_state, @@ -702,16 +686,13 @@ where Ok(()) } -fn append_cancels_to_move_token<'a, B, R>( - m_state: &'a mut MutableFunderState, - friend_public_key: &'a PublicKey, - pending_move_token: &'a mut PendingMoveToken, - identity_client: &'a mut IdentityClient, - rng: &'a R, +fn append_cancels_to_move_token( + m_state: &mut MutableFunderState, + friend_public_key: &PublicKey, + pending_move_token: &mut PendingMoveToken, ) -> Result<(), CollectOutgoingError> where B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, - R: CryptoRandom, { let friend = m_state.state().friends.get(friend_public_key).unwrap(); @@ -720,10 +701,7 @@ where let mut pending_backwards_ops = friend.pending_backwards_ops.clone(); while let Some(pending_backwards_op) = pending_backwards_ops.pop_front() { let pending_op = backwards_op_to_friend_tc_op( - m_state, pending_backwards_op, - identity_client, - rng ); // TODO: Find a more elegant way to do this: let mut dummy_cancel_public_keys = HashSet::new(); @@ -924,8 +902,6 @@ where m_state, friend_public_key, pending_move_token, - identity_client, - rng ); } From fa064928b0401749a1c91a9203c55640bb2e07cf Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 19:19:57 +0300 Subject: [PATCH 141/248] handler.rs: Fixed some unused warnings, not done yet. --- components/funder/src/handler/handler.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/components/funder/src/handler/handler.rs b/components/funder/src/handler/handler.rs index 8bc912a62..80b453eb5 100644 --- a/components/funder/src/handler/handler.rs +++ b/components/funder/src/handler/handler.rs @@ -3,7 +3,6 @@ use std::fmt::Debug; use common::canonical_serialize::CanonicalSerialize; use crypto::crypto_rand::CryptoRandom; -use crypto::identity::PublicKey; use crypto::uid::Uid; use proto::app_server::messages::RelayAddress; @@ -22,7 +21,6 @@ use crate::handler::sender::{create_friend_messages, SendCommands}; use crate::handler::state_wrap::{MutableFunderState, MutableEphemeral}; use crate::ephemeral::{Ephemeral, EphemeralMutation}; -use crate::friend::ChannelStatus; use crate::report::{ephemeral_mutation_to_report_mutations, funder_mutation_to_report_mutations}; use crate::types::{ChannelerConfig, FunderIncoming, FunderIncomingComm, FunderOutgoingComm}; @@ -216,8 +214,7 @@ where outgoing_comms.push(FunderOutgoingComm::FriendMessage(friend_message)); } - // Sign all unsigned responses (Async): - let (initial_state, funder_mutations, state) = m_state.done(); + let (initial_state, funder_mutations, _state) = m_state.done(); let (ephemeral_mutations, _ephemeral) = m_ephemeral.done(); // Add reports: From c255f8d616415ff68214a09f8036dbdd4628bd3c Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 19:21:25 +0300 Subject: [PATCH 142/248] Restored mod funder. --- components/funder/src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index 67c4bbc31..6566d3ac8 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -24,7 +24,7 @@ extern crate serde_derive; mod ephemeral; #[allow(unused)] mod friend; -// mod funder; +mod funder; mod handler; #[allow(unused)] mod liveness; @@ -35,9 +35,8 @@ pub mod report; mod state; #[cfg(test)] mod tests; -#[allow(unused)] mod token_channel; pub mod types; -// pub use self::funder::{funder_loop, FunderError}; +pub use self::funder::{funder_loop, FunderError}; pub use self::state::{FunderMutation, FunderState}; From e23b998ca54cfbb6934726ddf4beff159a9bf6a1 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 19:25:54 +0300 Subject: [PATCH 143/248] Funder: Removed some unused warnings. --- components/funder/src/lib.rs | 2 -- .../funder/src/mutual_credit/incoming.rs | 21 +------------------ .../funder/src/mutual_credit/outgoing.rs | 7 +++---- components/funder/src/mutual_credit/tests.rs | 5 ++--- components/funder/src/mutual_credit/types.rs | 4 +--- 5 files changed, 7 insertions(+), 32 deletions(-) diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index 6566d3ac8..facd14a3a 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -28,10 +28,8 @@ mod funder; mod handler; #[allow(unused)] mod liveness; -#[allow(unused)] mod mutual_credit; pub mod report; -#[allow(unused)] mod state; #[cfg(test)] mod tests; diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index 0720aacbd..367f01c51 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -1,6 +1,5 @@ use crypto::identity::verify_signature; -use common::int_convert::usize_to_u32; use common::safe_arithmetic::SafeSignedArithmetic; use proto::funder::messages::{ @@ -53,15 +52,12 @@ pub enum ProcessOperationError { /// The Route contains some public key twice. InvalidRoute, RequestsAlreadyDisabled, - RouteTooLong, InsufficientTrust, - InsufficientFee, CreditsCalcOverflow, RequestAlreadyExists, RequestDoesNotExist, InvalidResponseSignature, LocalRequestsClosed, - CalcFeeError, NotExpectingResponse, InvalidSrcPlainLock, InvalidDestPlainLock, @@ -193,7 +189,7 @@ fn process_request_send_funds( } // Find ourselves (And remote side) on the route. If we are not there, abort. - let remote_index = request_send_funds + let _remote_index = request_send_funds .route .find_pk_pair( &mutual_credit.state().idents.remote_public_key, @@ -206,21 +202,6 @@ fn process_request_send_funds( return Err(ProcessOperationError::LocalRequestsClosed); } - // Calculate the amount of credits we want for passing this request. - // (Note: We are for sure not the origin of this request message, because it was received from a - // remote side) - // - // TODO: Should be done outside of MutualCredit: - /* - let own_fee = calc_fee(&mutual_credit.state().rate, request_send_funds.dest_payment) - .ok_or(ProcessOperationError::CalcFeeError)?; - - let new_left_fees = request_send_funds - .left_fees - .checked_sub(own_fee) - .ok_or(ProcessOperationError::InsufficientFee)?; - */ - // Calculate amount of credits to freeze let own_freeze_credits = request_send_funds .dest_payment diff --git a/components/funder/src/mutual_credit/outgoing.rs b/components/funder/src/mutual_credit/outgoing.rs index 453eae2aa..7d42c1452 100644 --- a/components/funder/src/mutual_credit/outgoing.rs +++ b/components/funder/src/mutual_credit/outgoing.rs @@ -1,11 +1,10 @@ use crypto::identity::verify_signature; -use common::int_convert::usize_to_u32; use common::safe_arithmetic::SafeSignedArithmetic; use proto::funder::messages::{ - CancelSendFundsOp, CollectSendFundsOp, FriendTcOp, PendingTransaction, RequestSendFundsOp, - RequestsStatus, ResponseSendFundsOp, TransactionStage, + CancelSendFundsOp, CollectSendFundsOp, FriendTcOp, RequestSendFundsOp, RequestsStatus, + ResponseSendFundsOp, TransactionStage, }; use proto::funder::signature_buff::create_response_signature_buffer; @@ -118,7 +117,7 @@ impl OutgoingMc { } // Find ourselves on the route. If we are not there, abort. - let local_index = request_send_funds + let _local_index = request_send_funds .route .find_pk_pair( &self.mutual_credit.state().idents.local_public_key, diff --git a/components/funder/src/mutual_credit/tests.rs b/components/funder/src/mutual_credit/tests.rs index c2c60baea..9b88c8cbb 100644 --- a/components/funder/src/mutual_credit/tests.rs +++ b/components/funder/src/mutual_credit/tests.rs @@ -247,7 +247,6 @@ fn test_request_cancel_send_funds() { left_fees: 5, }; - let pending_transaction = create_pending_transaction(&request_send_funds); apply_outgoing( &mut mutual_credit, &FriendTcOp::RequestSendFunds(request_send_funds), @@ -262,7 +261,7 @@ fn test_request_cancel_send_funds() { // -----[CancelSendFunds]-------- // ------------------------------ - let mut cancel_send_funds = CancelSendFundsOp { request_id }; + let cancel_send_funds = CancelSendFundsOp { request_id }; apply_incoming( &mut mutual_credit, @@ -365,7 +364,7 @@ fn test_request_response_cancel_send_funds() { // -----[CancelSendFunds]-------- // ------------------------------ - let mut cancel_send_funds = CancelSendFundsOp { request_id }; + let cancel_send_funds = CancelSendFundsOp { request_id }; apply_incoming( &mut mutual_credit, diff --git a/components/funder/src/mutual_credit/types.rs b/components/funder/src/mutual_credit/types.rs index 594a736d2..cb5ed32ce 100644 --- a/components/funder/src/mutual_credit/types.rs +++ b/components/funder/src/mutual_credit/types.rs @@ -6,9 +6,7 @@ use common::safe_arithmetic::SafeSignedArithmetic; use crypto::identity::PublicKey; use crypto::uid::Uid; -use proto::funder::messages::{ - PendingTransaction, Rate, RequestSendFundsOp, RequestsStatus, TransactionStage, -}; +use proto::funder::messages::{PendingTransaction, Rate, RequestsStatus, TransactionStage}; /// The maximum possible funder debt. /// We don't use the full u128 because i128 can not go beyond this value. From 5de26bcb6ea01f584f0bff8aafd7c1cc7897c78c Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 19:26:42 +0300 Subject: [PATCH 144/248] Funder: Removed some allow(unused) hints. --- components/funder/src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index facd14a3a..79f08b29e 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -20,13 +20,11 @@ extern crate serde_derive; // #[allow(unused)] // mod credit_calc; -#[allow(unused)] mod ephemeral; #[allow(unused)] mod friend; mod funder; mod handler; -#[allow(unused)] mod liveness; mod mutual_credit; pub mod report; From 31a10ed4f1b9d3799297eb13b7f0ba29f6c5a5c6 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 19:27:51 +0300 Subject: [PATCH 145/248] friend.rs: Fixed unused warnings. --- components/funder/src/friend.rs | 10 +++------- components/funder/src/lib.rs | 1 - 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/components/funder/src/friend.rs b/components/funder/src/friend.rs index e37d32793..b85ffeafd 100644 --- a/components/funder/src/friend.rs +++ b/components/funder/src/friend.rs @@ -1,27 +1,23 @@ use im::vector::Vector as ImVec; use std::fmt::Debug; -use crypto::hash_lock::PlainLock; use crypto::identity::PublicKey; use common::canonical_serialize::CanonicalSerialize; -use common::safe_arithmetic::SafeUnsignedArithmetic; use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ - CancelSendFundsOp, CollectSendFundsOp, FriendStatus, PendingTransaction, Rate, - RequestSendFundsOp, RequestsStatus, ResetTerms, ResponseSendFundsOp, + CancelSendFundsOp, CollectSendFundsOp, FriendStatus, Rate, RequestSendFundsOp, RequestsStatus, + ResetTerms, ResponseSendFundsOp, }; use crate::token_channel::{TcMutation, TokenChannel}; -use crate::types::{MoveTokenHashed, UnsignedResponseSendFundsOp}; +use crate::types::MoveTokenHashed; /// Any operation that goes backwards (With respect to the initial request) #[derive(Clone, Serialize, Deserialize, Debug)] pub enum BackwardsOp { Response(ResponseSendFundsOp), - // /// A response that was not yet signed (Signing requires an async call) - // UnsignedResponse((PendingTransaction, PlainLock)), Cancel(CancelSendFundsOp), Collect(CollectSendFundsOp), } diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index 79f08b29e..b6c64783a 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -21,7 +21,6 @@ extern crate serde_derive; // #[allow(unused)] // mod credit_calc; mod ephemeral; -#[allow(unused)] mod friend; mod funder; mod handler; From 87f549c7732cbe18ecdf251f76d76b191c0a03f4 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 19:28:07 +0300 Subject: [PATCH 146/248] Removed comment. --- components/funder/src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/funder/src/lib.rs b/components/funder/src/lib.rs index b6c64783a..ef7f61779 100644 --- a/components/funder/src/lib.rs +++ b/components/funder/src/lib.rs @@ -18,8 +18,6 @@ extern crate log; #[macro_use] extern crate serde_derive; -// #[allow(unused)] -// mod credit_calc; mod ephemeral; mod friend; mod funder; From 8d0f56535cd3c97c2007441efc1a8495b71d2725 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 19:30:28 +0300 Subject: [PATCH 147/248] Funder handler: fixed some unused warnings. --- components/funder/src/handler/handle_friend.rs | 5 +---- components/funder/src/handler/mod.rs | 3 --- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/components/funder/src/handler/handle_friend.rs b/components/funder/src/handler/handle_friend.rs index 1cf474f86..6b007706e 100644 --- a/components/funder/src/handler/handle_friend.rs +++ b/components/funder/src/handler/handle_friend.rs @@ -251,8 +251,7 @@ fn handle_response_send_funds( request_id: response_send_funds.request_id.clone(), result: RequestResult::Success(commit), }; - let transaction_result = - outgoing_control.push(FunderOutgoingControl::TransactionResult(transaction_result)); + outgoing_control.push(FunderOutgoingControl::TransactionResult(transaction_result)); } Some(friend_public_key) => { // Queue this response message to another token channel: @@ -309,7 +308,6 @@ fn handle_cancel_send_funds( fn handle_collect_send_funds( m_state: &mut MutableFunderState, send_commands: &mut SendCommands, - outgoing_control: &mut Vec>, rng: &R, collect_send_funds: CollectSendFundsOp, pending_transaction: PendingTransaction, @@ -463,7 +461,6 @@ fn handle_move_token_output( handle_collect_send_funds( m_state, send_commands, - outgoing_control, rng, incoming_collect, pending_transaction, diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index 0b11ac486..283bffad5 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -2,11 +2,8 @@ mod canceler; #[allow(unused)] mod handle_control; -#[allow(unused)] mod handle_friend; -#[allow(unused)] mod handle_init; -#[allow(unused)] mod handle_liveness; #[allow(unused)] mod handler; From 2c94bcc09268382525ca9af75b121fb87fe10ffa Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 19:35:20 +0300 Subject: [PATCH 148/248] Fixed unused warnings for handle_control.rs. --- .../funder/src/handler/handle_control.rs | 21 +++++-------------- components/funder/src/handler/mod.rs | 1 - 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 7ac8753cf..9da97cca3 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -15,10 +15,10 @@ use crate::state::{FunderMutation, NewTransactions, Payment}; use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ AddFriend, AddInvoice, ChannelerUpdateFriend, CollectSendFundsOp, CreatePayment, - CreateTransaction, FriendStatus, FunderControl, FunderOutgoingControl, MultiCommit, ReceiptAck, + CreateTransaction, FriendStatus, FunderControl, FunderOutgoingControl, MultiCommit, RemoveFriend, RequestResult, RequestSendFundsOp, ResetFriendChannel, ResponseClosePayment, SetFriendName, SetFriendRate, SetFriendRelays, SetFriendRemoteMaxDebt, SetFriendStatus, - SetRequestsStatus, TransactionResult, UserRequestSendFunds, + SetRequestsStatus, TransactionResult, }; use proto::funder::signature_buff::verify_multi_commit; @@ -42,9 +42,6 @@ pub enum HandleControlError { InvalidRoute, RequestAlreadyInProgress, PendingUserRequestsFull, - ReceiptDoesNotExist, - ReceiptSignatureMismatch, - UserRequestInvalid, FriendNotReady, MaxNodeRelaysReached, PaymentAlreadyOpen, @@ -483,13 +480,6 @@ where Ok(()) } -fn check_user_request_valid(user_request_send_funds: &UserRequestSendFunds) -> Option<()> { - if !user_request_send_funds.route.is_valid() { - return None; - } - Some(()) -} - fn control_create_payment( m_state: &mut MutableFunderState, create_payment: CreatePayment, @@ -523,7 +513,6 @@ where fn control_create_transaction_inner( m_state: &mut MutableFunderState, ephemeral: &Ephemeral, - outgoing_control: &mut Vec>, send_commands: &mut SendCommands, rng: &R, max_pending_user_requests: usize, @@ -664,7 +653,6 @@ where if let Err(e) = control_create_transaction_inner( m_state, ephemeral, - outgoing_control, send_commands, rng, max_pending_user_requests, @@ -768,11 +756,11 @@ where .ok_or(HandleControlError::PaymentDoesNotExist)? .clone(); - let new_payment = match payment { + match payment { Payment::NewTransactions(_) | Payment::InProgress(_) | Payment::AfterSuccessAck(_) => { return Err(HandleControlError::AckStateInvalid) } - Payment::Success((num_transactions, receipt, ack_uid)) => { + Payment::Success((num_transactions, _receipt, ack_uid)) => { // Make sure that ack matches: if user_ack_uid != ack_uid { return Err(HandleControlError::AckMismatch); @@ -800,6 +788,7 @@ where m_state.mutate(funder_mutation); } }; + Ok(()) } diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index 283bffad5..9f527fe9b 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -1,6 +1,5 @@ #[allow(unused)] mod canceler; -#[allow(unused)] mod handle_control; mod handle_friend; mod handle_init; From 455dcca4172b666cd147bd1ac6901011677e623e Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 19:35:55 +0300 Subject: [PATCH 149/248] Fixed unused warnings for canceler.rs. --- components/funder/src/handler/canceler.rs | 4 +--- components/funder/src/handler/mod.rs | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/components/funder/src/handler/canceler.rs b/components/funder/src/handler/canceler.rs index d82935cca..7656255de 100644 --- a/components/funder/src/handler/canceler.rs +++ b/components/funder/src/handler/canceler.rs @@ -6,9 +6,7 @@ use crypto::crypto_rand::CryptoRandom; use crypto::identity::PublicKey; use crypto::uid::Uid; -use proto::funder::messages::{ - FunderOutgoingControl, RequestResult, RequestSendFundsOp, TransactionResult, -}; +use proto::funder::messages::{FunderOutgoingControl, RequestResult, TransactionResult}; use crate::handler::sender::SendCommands; use crate::handler::state_wrap::MutableFunderState; diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index 9f527fe9b..67b8f66a6 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -1,4 +1,3 @@ -#[allow(unused)] mod canceler; mod handle_control; mod handle_friend; From 55f0202b14008f9c58e88782596fc47e9b7d9ea2 Mon Sep 17 00:00:00 2001 From: real Date: Sun, 26 May 2019 19:37:30 +0300 Subject: [PATCH 150/248] Funder: Removed allow(unused) hint from handler module. --- components/funder/src/handler/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index 67b8f66a6..88767dc19 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -3,7 +3,6 @@ mod handle_control; mod handle_friend; mod handle_init; mod handle_liveness; -#[allow(unused)] mod handler; mod sender; mod state_wrap; From ac879ee20f711d1f190e48a28781a349abcb2182 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 10:16:05 +0300 Subject: [PATCH 151/248] First part of pair_basic.rs test passes. --- components/funder/src/handler/mod.rs | 4 ++-- components/funder/src/handler/tests/mod.rs | 5 +++-- components/funder/src/handler/tests/pair_basic.rs | 5 +++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/components/funder/src/handler/mod.rs b/components/funder/src/handler/mod.rs index 88767dc19..d649f9141 100644 --- a/components/funder/src/handler/mod.rs +++ b/components/funder/src/handler/mod.rs @@ -8,7 +8,7 @@ mod sender; mod state_wrap; mod utils; -// #[cfg(test)] -// mod tests; +#[cfg(test)] +mod tests; pub use self::handler::{funder_handle_message, FunderHandlerError}; diff --git a/components/funder/src/handler/tests/mod.rs b/components/funder/src/handler/tests/mod.rs index a74abda5f..a0c14fcd9 100644 --- a/components/funder/src/handler/tests/mod.rs +++ b/components/funder/src/handler/tests/mod.rs @@ -1,4 +1,5 @@ -mod change_address; +// mod change_address; +#[allow(unused)] mod pair_basic; -mod pair_inconsistency; +// mod pair_inconsistency; mod utils; diff --git a/components/funder/src/handler/tests/pair_basic.rs b/components/funder/src/handler/tests/pair_basic.rs index 485b21ede..5aa89286d 100644 --- a/components/funder/src/handler/tests/pair_basic.rs +++ b/components/funder/src/handler/tests/pair_basic.rs @@ -413,6 +413,9 @@ async fn task_handler_pair_basic<'a>( }; assert_eq!(local_max_debt, 100); + /* + + // Node2 receives control message to send funds to Node1: // But Node1's requests are not open, therefore Node2 will return a ResponseReceived with // failure through the outgoing control: @@ -443,6 +446,7 @@ async fn task_handler_pair_basic<'a>( assert_eq!(outgoing_comms.len(), 0); assert_eq!(outgoing_control.len(), 2); + // Checking the current requests status on the mutual credit: let friend2 = state1.friends.get(&pk2).unwrap(); let mutual_credit_state = match &friend2.channel_status { @@ -650,6 +654,7 @@ async fn task_handler_pair_basic<'a>( assert_eq!(mutual_credit_state.balance.balance, -20); assert_eq!(mutual_credit_state.balance.remote_pending_debt, 0); assert_eq!(mutual_credit_state.balance.local_pending_debt, 0); + */ } #[test] From 534ef0a09f5aaf13883b298ee0621a5be0d97f22 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 11:09:40 +0300 Subject: [PATCH 152/248] Funder: Reject incoming Requests without a matching open invoice. --- .../funder/src/handler/handle_friend.rs | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/components/funder/src/handler/handle_friend.rs b/components/funder/src/handler/handle_friend.rs index 6b007706e..b2de928f7 100644 --- a/components/funder/src/handler/handle_friend.rs +++ b/components/funder/src/handler/handle_friend.rs @@ -160,7 +160,36 @@ fn handle_request_send_funds( let local_index = remote_index.checked_add(1).unwrap(); let next_index = local_index.checked_add(1).unwrap(); if next_index >= request_send_funds.route.len() { - // We are the destination of this request. We return a response: + // We are the destination of this request. + + // First make sure that we have a matching open invoice for this transaction: + let is_invoice_match = if let Some(open_invoice) = m_state + .state() + .open_invoices + .get(&request_send_funds.invoice_id) + { + if (open_invoice.total_dest_payment == request_send_funds.total_dest_payment) + && (request_send_funds.dest_payment <= request_send_funds.total_dest_payment) + { + true + } else { + false + } + } else { + false + }; + + if !is_invoice_match { + reply_with_cancel( + m_state, + send_commands, + remote_public_key, + &request_send_funds.request_id, + ); + return; + }; + + // We return a response: let pending_transaction = create_pending_transaction(&request_send_funds); m_state.queue_unsigned_response(remote_public_key.clone(), pending_transaction); /* From e049676f68a8e4cb58d2170da523665a6d8a4446 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 12:11:40 +0300 Subject: [PATCH 153/248] Fixed bug in handle_control.rs (Returning InProgress instead of PaymentNotFound). --- components/funder/src/handler/handle_control.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 9da97cca3..1ce888592 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -685,7 +685,7 @@ where } else { // Payment not found: outgoing_control.push(FunderOutgoingControl::ResponseClosePayment( - ResponseClosePayment::InProgress, + ResponseClosePayment::PaymentNotFound, )); return Ok(()); }; From 5e8bd1dd6e61e03047d2b51e5a093c3f5cf7e496 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 12:12:01 +0300 Subject: [PATCH 154/248] Reordered variants in ResponseClosePayment enum. --- components/proto/src/funder/messages.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 4eac43d31..708b44128 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -605,10 +605,10 @@ pub struct TransactionResult { #[derive(Debug, Clone, PartialEq, Eq)] pub enum ResponseClosePayment { + PaymentNotFound, InProgress, // Can not be acked Success((Receipt, Uid)), // (Receipt, ack_id) Canceled(Uid), // ack_id - PaymentNotFound, } #[derive(Debug)] From 2bad489e77c693acacb87dbc55a496e86e3808d0 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 12:12:17 +0300 Subject: [PATCH 155/248] Removed old comment. --- components/proto/src/funder/signature_buff.rs | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/components/proto/src/funder/signature_buff.rs b/components/proto/src/funder/signature_buff.rs index 528db3d2f..0a440d7b2 100644 --- a/components/proto/src/funder/signature_buff.rs +++ b/components/proto/src/funder/signature_buff.rs @@ -46,27 +46,6 @@ pub fn create_response_signature_buffer( sbuffer } -/* -pub fn prepare_receipt( - response_send_funds: &ResponseSendFundsOp, - pending_transaction: &PendingTransaction, -) -> Receipt { - let mut hash_buff = Vec::new(); - hash_buff.extend_from_slice(&pending_transaction.request_id); - hash_buff.extend_from_slice(&pending_transaction.route.hash()); - hash_buff.extend_from_slice(&response_send_funds.rand_nonce); - let response_hash = hash::sha_512_256(&hash_buff); - // = sha512/256(requestId || sha512/256(route) || randNonce) - - Receipt { - response_hash, - invoice_id: pending_transaction.invoice_id.clone(), - dest_payment: pending_transaction.dest_payment, - signature: response_send_funds.signature.clone(), - } -} -*/ - pub fn prepare_receipt( collect_send_funds: &CollectSendFundsOp, response_send_funds: &ResponseSendFundsOp, From 0e0ae6d4ce909b59ed91c8a4d569ccc5313379f4 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 12:12:40 +0300 Subject: [PATCH 156/248] Funder: Fixed pair_basic.rs test. --- .../funder/src/handler/tests/pair_basic.rs | 334 ++++++++++++++++-- 1 file changed, 309 insertions(+), 25 deletions(-) diff --git a/components/funder/src/handler/tests/pair_basic.rs b/components/funder/src/handler/tests/pair_basic.rs index 5aa89286d..61e3cc93e 100644 --- a/components/funder/src/handler/tests/pair_basic.rs +++ b/components/funder/src/handler/tests/pair_basic.rs @@ -13,11 +13,12 @@ use crypto::identity::{compare_public_key, generate_pkcs8_key_pair, SoftwareEd25 use crypto::invoice_id::{InvoiceId, INVOICE_ID_LEN}; use crypto::test_utils::DummyRandom; use crypto::uid::{Uid, UID_LEN}; +use crypto::payment_id::{PaymentId, PAYMENT_ID_LEN}; use proto::funder::messages::{ AddFriend, FriendMessage, FriendStatus, FriendsRoute, FunderControl, FunderIncomingControl, RequestsStatus, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, - UserRequestSendFunds, + UserRequestSendFunds, CreatePayment, AddInvoice, CreateTransaction, RequestResult, FunderOutgoingControl, MultiCommit, ResponseClosePayment, }; use crate::ephemeral::Ephemeral; @@ -413,23 +414,73 @@ async fn task_handler_pair_basic<'a>( }; assert_eq!(local_max_debt, 100); - /* + + // Node1 opens an invoice (To get payment from Node2): + let add_invoice = AddInvoice { + invoice_id: InvoiceId::from(&[1u8; INVOICE_ID_LEN]), + total_dest_payment: 16, + }; + + let incoming_control_message = FunderIncomingControl::new( + Uid::from(&[16; UID_LEN]), + FunderControl::AddInvoice(add_invoice), + ); + let funder_incoming = FunderIncoming::Control(incoming_control_message); + let (outgoing_comms, outgoing_control) = await!(Box::pin(apply_funder_incoming( + funder_incoming, + &mut state1, + &mut ephemeral1, + &mut rng, + identity_client1 + ))) + .unwrap(); + + assert_eq!(outgoing_comms.len(), 0); + // SetNumOpenInvoices(1): + assert_eq!(outgoing_control.len(), 1); + + + // Node2 opens payment (to Node1) according to the invoice from Node1: + let create_payment = CreatePayment { + payment_id: PaymentId::from(&[3u8; PAYMENT_ID_LEN]), + invoice_id: InvoiceId::from(&[1u8; INVOICE_ID_LEN]), + total_dest_payment: 16, + dest_public_key: pk1.clone(), + }; + + let incoming_control_message = FunderIncomingControl::new( + Uid::from(&[17; UID_LEN]), + FunderControl::CreatePayment(create_payment), + ); + let funder_incoming = FunderIncoming::Control(incoming_control_message); + let (outgoing_comms, outgoing_control) = await!(Box::pin(apply_funder_incoming( + funder_incoming, + &mut state2, + &mut ephemeral2, + &mut rng, + identity_client2 + ))) + .unwrap(); + + assert_eq!(outgoing_comms.len(), 0); + // SetNumPayments(1): + assert_eq!(outgoing_control.len(), 1); - // Node2 receives control message to send funds to Node1: - // But Node1's requests are not open, therefore Node2 will return a ResponseReceived with - // failure through the outgoing control: - let user_request_send_funds = UserRequestSendFunds { - request_id: Uid::from(&[3; UID_LEN]), + // Node2 creates a transaction to send funds to Node1: + let create_transaction = CreateTransaction { + payment_id: PaymentId::from(&[3u8; PAYMENT_ID_LEN]), + request_id: Uid::from(&[0; UID_LEN]), route: FriendsRoute { public_keys: vec![pk2.clone(), pk1.clone()], }, - invoice_id: InvoiceId::from(&[1; INVOICE_ID_LEN]), - dest_payment: 20, + dest_payment: 16, + fees: 4, }; + let incoming_control_message = FunderIncomingControl::new( - Uid::from(&[16; UID_LEN]), - FunderControl::RequestSendFunds(user_request_send_funds), + Uid::from(&[18; UID_LEN]), + FunderControl::CreateTransaction(create_transaction), ); let funder_incoming = FunderIncoming::Control(incoming_control_message); let (outgoing_comms, outgoing_control) = await!(Box::pin(apply_funder_incoming( @@ -441,11 +492,21 @@ async fn task_handler_pair_basic<'a>( ))) .unwrap(); - // Node2 will not send the RequestFunds message to Node1, because he knows Node1 is - // not ready: assert_eq!(outgoing_comms.len(), 0); + // SetNumPayments(1): assert_eq!(outgoing_control.len(), 2); + let outgoing = &outgoing_control[1]; + let transaction_result = match outgoing { + FunderOutgoingControl::TransactionResult(transaction_result) => transaction_result, + _ => unreachable!(), + }; + // We expect failure, because remote side is not ready: + assert_eq!(transaction_result.request_id, Uid::from(&[0; UID_LEN])); + match transaction_result.result { + RequestResult::Failure => {}, + _ => unreachable!(), + }; // Checking the current requests status on the mutual credit: let friend2 = state1.friends.get(&pk2).unwrap(); @@ -462,6 +523,7 @@ async fn task_handler_pair_basic<'a>( RequestsStatus::Closed ); + // Node1 gets a control message to declare his requests are open, // However, Node1 doesn't have the token at this moment. let set_requests_status = SetRequestsStatus { @@ -469,7 +531,7 @@ async fn task_handler_pair_basic<'a>( status: RequestsStatus::Open, }; let incoming_control_message = FunderIncomingControl::new( - Uid::from(&[17; UID_LEN]), + Uid::from(&[19; UID_LEN]), FunderControl::SetRequestsStatus(set_requests_status), ); let funder_incoming = FunderIncoming::Control(incoming_control_message); @@ -482,6 +544,7 @@ async fn task_handler_pair_basic<'a>( ))) .unwrap(); + // Node1 will request the token: assert_eq!(outgoing_comms.len(), 1); let friend_message = @@ -491,6 +554,7 @@ async fn task_handler_pair_basic<'a>( unreachable!(); }; + // Node2 receives the request_token message: let funder_incoming = FunderIncoming::Comm(FunderIncomingComm::Friend((pk1.clone(), friend_message))); @@ -503,6 +567,7 @@ async fn task_handler_pair_basic<'a>( ))) .unwrap(); + assert_eq!(outgoing_comms.len(), 1); let friend_message = if let FunderOutgoingComm::FriendMessage((_pk, friend_message)) = &outgoing_comms[0] { @@ -511,6 +576,7 @@ async fn task_handler_pair_basic<'a>( unreachable!(); }; + // Node1 receives the token from Node2: let funder_incoming = FunderIncoming::Comm(FunderIncomingComm::Friend((pk2.clone(), friend_message))); @@ -531,6 +597,7 @@ async fn task_handler_pair_basic<'a>( unreachable!(); }; + // Node2 receives the set requests open message: let funder_incoming = FunderIncoming::Comm(FunderIncomingComm::Friend((pk1.clone(), friend_message))); @@ -543,6 +610,7 @@ async fn task_handler_pair_basic<'a>( ))) .unwrap(); + // Checking the current requests status on the mutual credit for Node1: let friend2 = state1.friends.get(&pk2).unwrap(); let mutual_credit_state = match &friend2.channel_status { @@ -552,6 +620,7 @@ async fn task_handler_pair_basic<'a>( assert!(mutual_credit_state.requests_status.local.is_open()); assert!(!mutual_credit_state.requests_status.remote.is_open()); + // Checking the current requests status on the mutual credit for Node2: let friend1 = state2.friends.get(&pk1).unwrap(); let mutual_credit_state = match &friend1.channel_status { @@ -561,21 +630,23 @@ async fn task_handler_pair_basic<'a>( assert!(!mutual_credit_state.requests_status.local.is_open()); assert!(mutual_credit_state.requests_status.remote.is_open()); - // Node2 receives control message to send funds to Node1: - let user_request_send_funds = UserRequestSendFunds { - request_id: Uid::from(&[3; UID_LEN]), + // Node2 again creates a transaction to send funds to Node1: + let create_transaction = CreateTransaction { + payment_id: PaymentId::from(&[3u8; PAYMENT_ID_LEN]), + request_id: Uid::from(&[1; UID_LEN]), route: FriendsRoute { public_keys: vec![pk2.clone(), pk1.clone()], }, - invoice_id: InvoiceId::from(&[1; INVOICE_ID_LEN]), - dest_payment: 20, + dest_payment: 16, + fees: 4, }; + let incoming_control_message = FunderIncomingControl::new( - Uid::from(&[18; UID_LEN]), - FunderControl::RequestSendFunds(user_request_send_funds), + Uid::from(&[20; UID_LEN]), + FunderControl::CreateTransaction(create_transaction), ); let funder_incoming = FunderIncoming::Control(incoming_control_message); - let (outgoing_comms, _outgoing_control) = await!(Box::pin(apply_funder_incoming( + let (outgoing_comms, outgoing_control) = await!(Box::pin(apply_funder_incoming( funder_incoming, &mut state2, &mut ephemeral2, @@ -584,6 +655,11 @@ async fn task_handler_pair_basic<'a>( ))) .unwrap(); + assert_eq!(outgoing_comms.len(), 1); + // Report mutations: + assert_eq!(outgoing_control.len(), 1); + + // Node2 will send a RequestFunds message to Node1 assert_eq!(outgoing_comms.len(), 1); let friend_message = @@ -605,8 +681,140 @@ async fn task_handler_pair_basic<'a>( ))) .unwrap(); + // Node1 sends a ResponseSendFunds to Node2: assert_eq!(outgoing_comms.len(), 1); + let friend_message = match &outgoing_comms[0] { + FunderOutgoingComm::FriendMessage((pk, friend_message)) => { + if let FriendMessage::MoveTokenRequest(move_token_request) = friend_message { + assert_eq!(pk, &pk2); + let friend_move_token = &move_token_request.friend_move_token; + assert_eq!(friend_move_token.balance, 0); + assert_eq!(friend_move_token.local_pending_debt, 0); + assert_eq!(friend_move_token.remote_pending_debt, 20); + } else { + unreachable!(); + } + friend_message.clone() + } + _ => unreachable!(), + }; + + + // Node2 receives ResponseSendFunds from Node1: + let funder_incoming = + FunderIncoming::Comm(FunderIncomingComm::Friend((pk1.clone(), friend_message))); + let (_outgoing_comms, outgoing_control) = await!(Box::pin(apply_funder_incoming( + funder_incoming, + &mut state2, + &mut ephemeral2, + &mut rng, + identity_client2 + ))) + .unwrap(); + + assert_eq!(outgoing_control.len(), 2); + let outgoing = &outgoing_control[1]; + let transaction_result = match outgoing { + FunderOutgoingControl::TransactionResult(transaction_result) => transaction_result, + _ => unreachable!(), + }; + + // We expect success: + assert_eq!(transaction_result.request_id, Uid::from(&[1; UID_LEN])); + let commit = match &transaction_result.result { + RequestResult::Success(commit) => commit.clone(), + _ => unreachable!(), + }; + + // Node2: Compose a MultiCommit message: + let multi_commit = MultiCommit { + invoice_id: InvoiceId::from(&[1u8; INVOICE_ID_LEN]), + total_dest_payment: 16u128, + commits: vec![commit], + }; + + // Node1: Apply MultiCommit message received from Node2 (Received out of band): + let incoming_control_message = FunderIncomingControl::new( + Uid::from(&[21; UID_LEN]), + FunderControl::CommitInvoice(multi_commit), + ); + let funder_incoming = FunderIncoming::Control(incoming_control_message); + let (outgoing_comms, outgoing_control) = await!(Box::pin(apply_funder_incoming( + funder_incoming, + &mut state1, + &mut ephemeral1, + &mut rng, + identity_client1 + ))) + .unwrap(); + + + assert_eq!(outgoing_comms.len(), 1); + assert_eq!(outgoing_control.len(), 1); + + let friend_message = match &outgoing_comms[0] { + FunderOutgoingComm::FriendMessage((pk, friend_message)) => { + if let FriendMessage::MoveTokenRequest(move_token_request) = friend_message { + assert_eq!(pk, &pk2); + let friend_move_token = &move_token_request.friend_move_token; + assert_eq!(friend_move_token.balance, 0); + assert_eq!(friend_move_token.local_pending_debt, 0); + assert_eq!(friend_move_token.remote_pending_debt, 20); + } else { + unreachable!(); + } + friend_message.clone() + } + _ => unreachable!(), + }; + + // Node2 receives request token message from Node1: + let funder_incoming = + FunderIncoming::Comm(FunderIncomingComm::Friend((pk1.clone(), friend_message))); + let (outgoing_comms, _outgoing_control) = await!(Box::pin(apply_funder_incoming( + funder_incoming, + &mut state2, + &mut ephemeral2, + &mut rng, + identity_client2 + ))) + .unwrap(); + + assert_eq!(outgoing_comms.len(), 1); + + // Node2 gives token to Node1: + let friend_message = match &outgoing_comms[0] { + FunderOutgoingComm::FriendMessage((pk, friend_message)) => { + if let FriendMessage::MoveTokenRequest(move_token_request) = friend_message { + assert_eq!(pk, &pk1); + let friend_move_token = &move_token_request.friend_move_token; + assert_eq!(friend_move_token.balance, 0); + assert_eq!(friend_move_token.local_pending_debt, 20); + assert_eq!(friend_move_token.remote_pending_debt, 0); + } else { + unreachable!(); + } + friend_message.clone() + } + _ => unreachable!(), + }; + + // Node1 receives token from Node2: + let funder_incoming = + FunderIncoming::Comm(FunderIncomingComm::Friend((pk2.clone(), friend_message))); + let (outgoing_comms, _outgoing_control) = await!(Box::pin(apply_funder_incoming( + funder_incoming, + &mut state1, + &mut ephemeral1, + &mut rng, + identity_client1 + ))) + .unwrap(); + + assert_eq!(outgoing_comms.len(), 1); + + // Node1 sends a Collect message to Node2: let friend_message = match &outgoing_comms[0] { FunderOutgoingComm::FriendMessage((pk, friend_message)) => { if let FriendMessage::MoveTokenRequest(move_token_request) = friend_message { @@ -623,10 +831,10 @@ async fn task_handler_pair_basic<'a>( _ => unreachable!(), }; - // Node2 receives ResponseSendFunds from Node1: + // Node2 receives Collect message from node1: let funder_incoming = FunderIncoming::Comm(FunderIncomingComm::Friend((pk1.clone(), friend_message))); - let (_outgoing_comms, _outgoing_control) = await!(Box::pin(apply_funder_incoming( + let (outgoing_comms, _outgoing_control) = await!(Box::pin(apply_funder_incoming( funder_incoming, &mut state2, &mut ephemeral2, @@ -654,7 +862,83 @@ async fn task_handler_pair_basic<'a>( assert_eq!(mutual_credit_state.balance.balance, -20); assert_eq!(mutual_credit_state.balance.remote_pending_debt, 0); assert_eq!(mutual_credit_state.balance.local_pending_debt, 0); - */ + + // After a while... + // Node2: Close the payment (To check the payment results) + let incoming_control_message = FunderIncomingControl::new( + Uid::from(&[21; UID_LEN]), + FunderControl::RequestClosePayment(PaymentId::from(&[3u8; PAYMENT_ID_LEN])), + ); + let funder_incoming = FunderIncoming::Control(incoming_control_message); + let (outgoing_comms, outgoing_control) = await!(Box::pin(apply_funder_incoming( + funder_incoming, + &mut state2, + &mut ephemeral2, + &mut rng, + identity_client2 + ))) + .unwrap(); + + assert_eq!(outgoing_comms.len(), 0); + assert_eq!(outgoing_control.len(), 2); + + let response_close_payment = match &outgoing_control[1] { + FunderOutgoingControl::ResponseClosePayment(response_close_payment) => response_close_payment, + _ => unreachable!(), + }; + + let (receipt, ack_uid) = match response_close_payment { + ResponseClosePayment::Success((receipt, ack_uid)) => (receipt, ack_uid), + _ => unreachable!(), + }; + + assert_eq!(receipt.invoice_id, InvoiceId::from(&[1u8; INVOICE_ID_LEN])); + assert_eq!(receipt.dest_payment, 16); + assert_eq!(receipt.total_dest_payment, 16); + + // Node2: Send ack for closing the payment: + let incoming_control_message = FunderIncomingControl::new( + Uid::from(&[22; UID_LEN]), + FunderControl::AckClosePayment((PaymentId::from(&[3u8; PAYMENT_ID_LEN]), ack_uid.clone())), + ); + let funder_incoming = FunderIncoming::Control(incoming_control_message); + let (outgoing_comms, outgoing_control) = await!(Box::pin(apply_funder_incoming( + funder_incoming, + &mut state2, + &mut ephemeral2, + &mut rng, + identity_client2 + ))) + .unwrap(); + + + // Node2: Request for closing payment again: + let incoming_control_message = FunderIncomingControl::new( + Uid::from(&[23; UID_LEN]), + FunderControl::RequestClosePayment(PaymentId::from(&[3u8; PAYMENT_ID_LEN])), + ); + let funder_incoming = FunderIncoming::Control(incoming_control_message); + let (outgoing_comms, outgoing_control) = await!(Box::pin(apply_funder_incoming( + funder_incoming, + &mut state2, + &mut ephemeral2, + &mut rng, + identity_client2 + ))) + .unwrap(); + + assert_eq!(outgoing_comms.len(), 0); + assert_eq!(outgoing_control.len(), 2); + + let response_close_payment = match &outgoing_control[1] { + FunderOutgoingControl::ResponseClosePayment(response_close_payment) => response_close_payment, + _ => unreachable!(), + }; + + match response_close_payment { + ResponseClosePayment::PaymentNotFound => {}, + _ => unreachable!(), + } } #[test] From 41c4092c60e5cba40d1216f7e14c214b5c08b121 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 12:13:49 +0300 Subject: [PATCH 157/248] Removed unused warnings from tests. --- components/funder/src/handler/tests/mod.rs | 1 - components/funder/src/handler/tests/pair_basic.rs | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/components/funder/src/handler/tests/mod.rs b/components/funder/src/handler/tests/mod.rs index a0c14fcd9..4df37bc43 100644 --- a/components/funder/src/handler/tests/mod.rs +++ b/components/funder/src/handler/tests/mod.rs @@ -1,5 +1,4 @@ // mod change_address; -#[allow(unused)] mod pair_basic; // mod pair_inconsistency; mod utils; diff --git a/components/funder/src/handler/tests/pair_basic.rs b/components/funder/src/handler/tests/pair_basic.rs index 61e3cc93e..a1389d36f 100644 --- a/components/funder/src/handler/tests/pair_basic.rs +++ b/components/funder/src/handler/tests/pair_basic.rs @@ -18,7 +18,7 @@ use crypto::payment_id::{PaymentId, PAYMENT_ID_LEN}; use proto::funder::messages::{ AddFriend, FriendMessage, FriendStatus, FriendsRoute, FunderControl, FunderIncomingControl, RequestsStatus, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, - UserRequestSendFunds, CreatePayment, AddInvoice, CreateTransaction, RequestResult, FunderOutgoingControl, MultiCommit, ResponseClosePayment, + CreatePayment, AddInvoice, CreateTransaction, RequestResult, FunderOutgoingControl, MultiCommit, ResponseClosePayment, }; use crate::ephemeral::Ephemeral; @@ -834,7 +834,7 @@ async fn task_handler_pair_basic<'a>( // Node2 receives Collect message from node1: let funder_incoming = FunderIncoming::Comm(FunderIncomingComm::Friend((pk1.clone(), friend_message))); - let (outgoing_comms, _outgoing_control) = await!(Box::pin(apply_funder_incoming( + let (_outgoing_comms, _outgoing_control) = await!(Box::pin(apply_funder_incoming( funder_incoming, &mut state2, &mut ephemeral2, @@ -902,7 +902,7 @@ async fn task_handler_pair_basic<'a>( FunderControl::AckClosePayment((PaymentId::from(&[3u8; PAYMENT_ID_LEN]), ack_uid.clone())), ); let funder_incoming = FunderIncoming::Control(incoming_control_message); - let (outgoing_comms, outgoing_control) = await!(Box::pin(apply_funder_incoming( + let (_outgoing_comms, _outgoing_control) = await!(Box::pin(apply_funder_incoming( funder_incoming, &mut state2, &mut ephemeral2, From 7aaff899acc9dd7ab2c3cf8bea152ac90670b7fb Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 12:15:20 +0300 Subject: [PATCH 158/248] Funder: Restored pair_inconsistency.rs test. --- components/funder/src/handler/tests/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/funder/src/handler/tests/mod.rs b/components/funder/src/handler/tests/mod.rs index 4df37bc43..8caccdc8d 100644 --- a/components/funder/src/handler/tests/mod.rs +++ b/components/funder/src/handler/tests/mod.rs @@ -1,4 +1,4 @@ // mod change_address; mod pair_basic; -// mod pair_inconsistency; +mod pair_inconsistency; mod utils; From b7b0fdae7a91ed5b4d03cd5afe12ccc76ee8906d Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 12:15:53 +0300 Subject: [PATCH 159/248] Funder: Restored change_address.rs test. --- components/funder/src/handler/tests/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/funder/src/handler/tests/mod.rs b/components/funder/src/handler/tests/mod.rs index 8caccdc8d..a74abda5f 100644 --- a/components/funder/src/handler/tests/mod.rs +++ b/components/funder/src/handler/tests/mod.rs @@ -1,4 +1,4 @@ -// mod change_address; +mod change_address; mod pair_basic; mod pair_inconsistency; mod utils; From b9db91f009c90f3f45b6b0d18e856372f8fdcc8e Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 12:18:46 +0300 Subject: [PATCH 160/248] Restored some code in funder/src/tests/utils.rs. --- components/funder/src/tests/tests.rs | 1 + components/funder/src/tests/utils.rs | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/components/funder/src/tests/tests.rs b/components/funder/src/tests/tests.rs index 4ea31dc93..64f637e4b 100644 --- a/components/funder/src/tests/tests.rs +++ b/components/funder/src/tests/tests.rs @@ -13,6 +13,7 @@ use proto::report::messages::{ChannelStatusReport, FunderReport}; use super::utils::{create_node_controls, dummy_named_relay_address, dummy_relay_address}; + async fn task_funder_basic(spawner: impl Spawn + Clone + Send + 'static) { let num_nodes = 2; let mut node_controls = await!(create_node_controls(num_nodes, spawner)); diff --git a/components/funder/src/tests/utils.rs b/components/funder/src/tests/utils.rs index 96ccf991c..204747d1b 100644 --- a/components/funder/src/tests/utils.rs +++ b/components/funder/src/tests/utils.rs @@ -30,8 +30,7 @@ use database::DatabaseClient; use identity::{create_identity, IdentityClient}; use crate::ephemeral::Ephemeral; -// TODO: Add this back later: -// use crate::funder::inner_funder_loop; +use crate::funder::inner_funder_loop; use crate::report::create_report; use crate::state::FunderState; @@ -406,7 +405,6 @@ where } } -/* // TODO: Add this back later: /// Create a few node_controls, together with a router connecting them all. @@ -503,4 +501,3 @@ where } node_controls } -*/ From 3a2d4085212076f0fb21b6eb456fd892233c5c37 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 13:07:08 +0300 Subject: [PATCH 161/248] Restored test_funder_basic() in funder/src/tests/tests.rs --- components/funder/src/tests/mod.rs | 3 +- components/funder/src/tests/tests.rs | 94 +++++++++++++++++++--------- components/funder/src/tests/utils.rs | 85 +++++++++++++++---------- 3 files changed, 120 insertions(+), 62 deletions(-) diff --git a/components/funder/src/tests/mod.rs b/components/funder/src/tests/mod.rs index ef41cf74e..ca0a03159 100644 --- a/components/funder/src/tests/mod.rs +++ b/components/funder/src/tests/mod.rs @@ -1,3 +1,4 @@ -// mod tests; +#[allow(unused)] +mod tests; #[allow(unused)] pub mod utils; diff --git a/components/funder/src/tests/tests.rs b/components/funder/src/tests/tests.rs index 64f637e4b..c936d90cb 100644 --- a/components/funder/src/tests/tests.rs +++ b/components/funder/src/tests/tests.rs @@ -4,10 +4,12 @@ use futures::task::Spawn; use crypto::identity::PublicKey; use crypto::invoice_id::{InvoiceId, INVOICE_ID_LEN}; use crypto::uid::{Uid, UID_LEN}; +use crypto::payment_id::{PaymentId, PAYMENT_ID_LEN}; use proto::funder::messages::{ FriendStatus, FriendsRoute, FunderControl, FunderIncomingControl, ReceiptAck, RequestsStatus, - ResetFriendChannel, ResponseSendFundsResult, UserRequestSendFunds, + ResetFriendChannel, AddInvoice, CreatePayment, CreateTransaction, TransactionResult, RequestResult, MultiCommit, + ResponseClosePayment, }; use proto::report::messages::{ChannelStatusReport, FunderReport}; @@ -45,43 +47,76 @@ async fn task_funder_basic(spawner: impl Spawn + Clone + Send + 'static) { await!(node_controls[0].wait_until_ready(&public_keys[1])); await!(node_controls[1].wait_until_ready(&public_keys[0])); - // Send credits 0 --> 1 - let user_request_send_funds = UserRequestSendFunds { - request_id: Uid::from(&[3; UID_LEN]), + + // Let node 1 open an invoice: + let add_invoice = AddInvoice { + invoice_id: InvoiceId::from(&[1u8; INVOICE_ID_LEN]), + total_dest_payment: 4, + }; + await!(node_controls[1].send(FunderControl::AddInvoice(add_invoice))); + + // Create payment 0 --> 1 + let create_payment = CreatePayment { + payment_id: PaymentId::from(&[2u8; PAYMENT_ID_LEN]), + invoice_id: InvoiceId::from(&[1u8; INVOICE_ID_LEN]), + total_dest_payment: 4, + dest_public_key: node_controls[1].public_key.clone(), + }; + await!(node_controls[0].send(FunderControl::CreatePayment(create_payment))); + + // Create transaction 0 --> 1: + let create_transaction = CreateTransaction { + payment_id: PaymentId::from(&[2u8; PAYMENT_ID_LEN]), + request_id: Uid::from(&[5u8; UID_LEN]), route: FriendsRoute { public_keys: vec![ - node_controls[0].public_key.clone(), - node_controls[1].public_key.clone(), + public_keys[0].clone(), + public_keys[1].clone(), ], }, - invoice_id: InvoiceId::from(&[1; INVOICE_ID_LEN]), - dest_payment: 5, + dest_payment: 4, + fees: 1, }; - let incoming_control_message = FunderIncomingControl::new( - Uid::from(&[40; UID_LEN]), - FunderControl::RequestSendFunds(user_request_send_funds), - ); - await!(node_controls[0].send(incoming_control_message)).unwrap(); - let response_received = await!(node_controls[0].recv_until_response()).unwrap(); - assert_eq!(response_received.request_id, Uid::from(&[3; UID_LEN])); - let receipt = match response_received.result { - ResponseSendFundsResult::Failure(_) => unreachable!(), - ResponseSendFundsResult::Success(send_funds_receipt) => send_funds_receipt, + await!(node_controls[0].send(FunderControl::CreateTransaction(create_transaction))); + let transaction_result = await!(node_controls[0].recv_until_transaction_result()).unwrap(); + + let commit = match transaction_result.result { + RequestResult::Success(commit) => commit, + _ => unreachable!(), }; - let receipt_ack = ReceiptAck { - request_id: Uid::from(&[3; UID_LEN]), - receipt_signature: receipt.signature.clone(), + // 0: Create multi commit: + let multi_commit = MultiCommit { + invoice_id: InvoiceId::from(&[1u8; INVOICE_ID_LEN]), + total_dest_payment: 4, + commits: vec![commit], }; - let incoming_control_message = FunderIncomingControl::new( - Uid::from(&[41; UID_LEN]), - FunderControl::ReceiptAck(receipt_ack), - ); - await!(node_controls[0].send(incoming_control_message)).unwrap(); - let pred = |report: &FunderReport<_>| report.num_ready_receipts == 0; - await!(node_controls[0].recv_until(pred)); + // MultiCommit: 0 --> 1 (Out of band) + + // 1: Apply MultiCommit: + await!(node_controls[1].send(FunderControl::CommitInvoice(multi_commit))); + + // 0: Expect a receipt: + let (receipt, ack_uid) = loop { + await!(node_controls[0].send( + FunderControl::RequestClosePayment(PaymentId::from(&[2u8; PAYMENT_ID_LEN])))); + let response_close_payment = await!(node_controls[0].recv_until_response_close_payment()).unwrap(); + match response_close_payment { + ResponseClosePayment::Success((receipt, ack_uid)) => break (receipt, ack_uid), + _ => {}, + } + }; + + // 0: Acknowledge response close: + await!(node_controls[0].send( + FunderControl::AckClosePayment((PaymentId::from(&[2u8; PAYMENT_ID_LEN]), ack_uid)))); + + assert_eq!(receipt.invoice_id, InvoiceId::from(&[1u8; INVOICE_ID_LEN])); + assert_eq!(receipt.dest_payment, 4); + assert_eq!(receipt.total_dest_payment, 4); + // Verify expected balances: let pred = |report: &FunderReport<_>| { @@ -111,6 +146,8 @@ fn test_funder_basic() { thread_pool.run(task_funder_basic(thread_pool.clone())); } +/* + async fn task_funder_forward_payment(spawner: impl Spawn + Clone + Send + 'static) { /* * 0 -- 1 -- 2 @@ -434,3 +471,4 @@ fn test_funder_add_relay() { let mut thread_pool = ThreadPool::new().unwrap(); thread_pool.run(task_funder_add_relay(thread_pool.clone())); } +*/ diff --git a/components/funder/src/tests/utils.rs b/components/funder/src/tests/utils.rs index 204747d1b..6f6c47641 100644 --- a/components/funder/src/tests/utils.rs +++ b/components/funder/src/tests/utils.rs @@ -13,6 +13,8 @@ use crypto::identity::{ }; use crypto::test_utils::DummyRandom; use crypto::uid::{Uid, UID_LEN}; +use crypto::payment_id::{PaymentId, PAYMENT_ID_LEN}; + use proto::report::messages::{ ChannelStatusReport, FriendLivenessReport, FriendStatusReport, FunderReport, @@ -200,6 +202,7 @@ pub struct NodeControl { send_control: mpsc::Sender>, recv_control: mpsc::Receiver>, pub report: FunderReport, + next_app_request_id: u64, } #[derive(Debug)] @@ -213,8 +216,37 @@ impl NodeControl where B: Clone + PartialEq + Eq + CanonicalSerialize, { - pub async fn send(&mut self, msg: FunderIncomingControl) -> Option<()> { - await!(self.send_control.send(msg)).ok().map(|_| ()) + pub async fn send(&mut self, funder_control: FunderControl) { + + // Convert self.next_app_request_id to a Uid: + let mut app_request_id_inner = [0u8; UID_LEN]; + let mut next_app_request_id = self.next_app_request_id; + for j in 0 .. 8 { + app_request_id_inner[j] = (next_app_request_id & 0xff) as u8; + next_app_request_id >>= 8; + } + + // Advance self.next_app_request_id: + self.next_app_request_id = self.next_app_request_id.checked_add(1).unwrap(); + + + let app_request_id = Uid::from(&app_request_id_inner); + let funder_incoming_control = FunderIncomingControl { + app_request_id: app_request_id.clone(), + funder_control, + }; + await!(self.send_control.send(funder_incoming_control)).unwrap(); + + loop { + match await!(self.recv()).unwrap() { + NodeRecv::ReportMutations(funder_report_mutations) => { + if funder_report_mutations.opt_app_request_id == Some(app_request_id) { + break; + } + } + _ => {}, + }; + } } pub async fn recv(&mut self) -> Option> { @@ -248,7 +280,7 @@ where } } - pub async fn recv_until_response(&mut self) -> Option { + pub async fn recv_until_transaction_result(&mut self) -> Option { loop { match await!(self.recv())? { NodeRecv::ReportMutations(_) => {} @@ -258,22 +290,24 @@ where } } + pub async fn recv_until_response_close_payment(&mut self) -> Option { + loop { + match await!(self.recv())? { + NodeRecv::ReportMutations(_) => {} + NodeRecv::TransactionResult(transaction_result) => {}, + NodeRecv::ResponseClosePayment(response_close_payment) => return Some(response_close_payment) + }; + } + } + pub async fn add_relay<'a>(&'a mut self, named_relay_address: NamedRelayAddress) { - let incoming_control_message = FunderIncomingControl::new( - Uid::from(&[33; UID_LEN]), - FunderControl::AddRelay(named_relay_address.clone()), - ); - await!(self.send(incoming_control_message)).unwrap(); + await!(self.send(FunderControl::AddRelay(named_relay_address.clone()))); let pred = |report: &FunderReport<_>| report.relays.contains(&named_relay_address); await!(self.recv_until(pred)); } pub async fn remove_relay<'a>(&'a mut self, public_key: PublicKey) { - let incoming_control_message = FunderIncomingControl::new( - Uid::from(&[34; UID_LEN]), - FunderControl::RemoveRelay(public_key.clone()), - ); - await!(self.send(incoming_control_message)).unwrap(); + await!(self.send(FunderControl::RemoveRelay(public_key.clone()))); let pred = |report: &FunderReport<_>| { for relay in &report.relays { if relay.public_key == public_key { @@ -298,11 +332,7 @@ where name: name.into(), balance, }; - let incoming_control_message = FunderIncomingControl::new( - Uid::from(&[35; UID_LEN]), - FunderControl::AddFriend(add_friend), - ); - await!(self.send(incoming_control_message)).unwrap(); + await!(self.send(FunderControl::AddFriend(add_friend))); let pred = |report: &FunderReport<_>| report.friends.contains_key(&friend_public_key); await!(self.recv_until(pred)); } @@ -316,11 +346,7 @@ where friend_public_key: friend_public_key.clone(), status: status.clone(), }; - let incoming_control_message = FunderIncomingControl::new( - Uid::from(&[36; UID_LEN]), - FunderControl::SetFriendStatus(set_friend_status), - ); - await!(self.send(incoming_control_message)).unwrap(); + await!(self.send(FunderControl::SetFriendStatus(set_friend_status))); let pred = |report: &FunderReport<_>| match report.friends.get(&friend_public_key) { None => false, Some(friend) => friend.status == FriendStatusReport::from(&status), @@ -337,11 +363,7 @@ where friend_public_key: friend_public_key.clone(), remote_max_debt: remote_max_debt, }; - let incoming_control_message = FunderIncomingControl::new( - Uid::from(&[36; UID_LEN]), - FunderControl::SetFriendRemoteMaxDebt(set_remote_max_debt), - ); - await!(self.send(incoming_control_message)).unwrap(); + await!(self.send(FunderControl::SetFriendRemoteMaxDebt(set_remote_max_debt))); let pred = |report: &FunderReport<_>| { let friend = match report.friends.get(&friend_public_key) { @@ -366,11 +388,7 @@ where friend_public_key: friend_public_key.clone(), status: requests_status.clone(), }; - let incoming_control_message = FunderIncomingControl::new( - Uid::from(&[37; UID_LEN]), - FunderControl::SetRequestsStatus(set_requests_status), - ); - await!(self.send(incoming_control_message)).unwrap(); + await!(self.send(FunderControl::SetRequestsStatus(set_requests_status))); let pred = |report: &FunderReport<_>| { let friend = match report.friends.get(&friend_public_key) { @@ -497,6 +515,7 @@ where send_control, recv_control, report: base_report, + next_app_request_id: 0, }); } node_controls From 71457cb6d7ed7a7f61a6e5529d83e1b4e1608f20 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 13:23:08 +0300 Subject: [PATCH 162/248] Restored test_funder_forward_payment() test: Added mediator fees. --- components/funder/src/tests/tests.rs | 96 +++++++++++++++++++--------- components/funder/src/tests/utils.rs | 55 ++++------------ 2 files changed, 79 insertions(+), 72 deletions(-) diff --git a/components/funder/src/tests/tests.rs b/components/funder/src/tests/tests.rs index c936d90cb..dde684369 100644 --- a/components/funder/src/tests/tests.rs +++ b/components/funder/src/tests/tests.rs @@ -9,7 +9,7 @@ use crypto::payment_id::{PaymentId, PAYMENT_ID_LEN}; use proto::funder::messages::{ FriendStatus, FriendsRoute, FunderControl, FunderIncomingControl, ReceiptAck, RequestsStatus, ResetFriendChannel, AddInvoice, CreatePayment, CreateTransaction, TransactionResult, RequestResult, MultiCommit, - ResponseClosePayment, + ResponseClosePayment, Rate, }; use proto::report::messages::{ChannelStatusReport, FunderReport}; @@ -146,7 +146,6 @@ fn test_funder_basic() { thread_pool.run(task_funder_basic(thread_pool.clone())); } -/* async fn task_funder_forward_payment(spawner: impl Spawn + Clone + Send + 'static) { /* @@ -177,6 +176,10 @@ async fn task_funder_forward_payment(spawner: impl Spawn + Clone + Send + 'stati await!(node_controls[1].set_friend_status(&public_keys[2], FriendStatus::Enabled)); await!(node_controls[2].set_friend_status(&public_keys[1], FriendStatus::Enabled)); + // Set rate: + // This is the amount of credits node 1 takes from node 0 for forwarding messages. + await!(node_controls[1].set_friend_rate(&public_keys[0], Rate {mul: 0, add: 5})); + // Set remote max debt: await!(node_controls[0].set_remote_max_debt(&public_keys[1], 200)); await!(node_controls[1].set_remote_max_debt(&public_keys[0], 100)); @@ -193,9 +196,27 @@ async fn task_funder_forward_payment(spawner: impl Spawn + Clone + Send + 'stati await!(node_controls[0].wait_until_ready(&public_keys[1])); await!(node_controls[1].wait_until_ready(&public_keys[2])); - // Send credits 0 --> 2 - let user_request_send_funds = UserRequestSendFunds { - request_id: Uid::from(&[3; UID_LEN]), + + // Let node 2 open an invoice: + let add_invoice = AddInvoice { + invoice_id: InvoiceId::from(&[1u8; INVOICE_ID_LEN]), + total_dest_payment: 15, + }; + await!(node_controls[2].send(FunderControl::AddInvoice(add_invoice))); + + // Create payment 0 --> 2 + let create_payment = CreatePayment { + payment_id: PaymentId::from(&[2u8; PAYMENT_ID_LEN]), + invoice_id: InvoiceId::from(&[1u8; INVOICE_ID_LEN]), + total_dest_payment: 15, + dest_public_key: node_controls[0].public_key.clone(), + }; + await!(node_controls[0].send(FunderControl::CreatePayment(create_payment))); + + // Create transaction 0 --> 2: + let create_transaction = CreateTransaction { + payment_id: PaymentId::from(&[2u8; PAYMENT_ID_LEN]), + request_id: Uid::from(&[5u8; UID_LEN]), route: FriendsRoute { public_keys: vec![ public_keys[0].clone(), @@ -203,34 +224,49 @@ async fn task_funder_forward_payment(spawner: impl Spawn + Clone + Send + 'stati public_keys[2].clone(), ], }, - invoice_id: InvoiceId::from(&[1; INVOICE_ID_LEN]), - dest_payment: 20, + dest_payment: 15, + fees: 5, }; - let incoming_control_message = FunderIncomingControl::new( - Uid::from(&[42; UID_LEN]), - FunderControl::RequestSendFunds(user_request_send_funds), - ); - await!(node_controls[0].send(incoming_control_message)).unwrap(); - let response_received = await!(node_controls[0].recv_until_response()).unwrap(); - assert_eq!(response_received.request_id, Uid::from(&[3; UID_LEN])); - let receipt = match response_received.result { - ResponseSendFundsResult::Failure(_) => unreachable!(), - ResponseSendFundsResult::Success(send_funds_receipt) => send_funds_receipt, + await!(node_controls[0].send(FunderControl::CreateTransaction(create_transaction))); + let transaction_result = await!(node_controls[0].recv_until_transaction_result()).unwrap(); + + let commit = match transaction_result.result { + RequestResult::Success(commit) => commit, + _ => unreachable!(), }; - // Send ReceiptAck: - let receipt_ack = ReceiptAck { - request_id: Uid::from(&[3; UID_LEN]), - receipt_signature: receipt.signature.clone(), + + // 0: Create multi commit: + let multi_commit = MultiCommit { + invoice_id: InvoiceId::from(&[1u8; INVOICE_ID_LEN]), + total_dest_payment: 15, + commits: vec![commit], }; - let incoming_control_message = FunderIncomingControl::new( - Uid::from(&[43; UID_LEN]), - FunderControl::ReceiptAck(receipt_ack), - ); - await!(node_controls[0].send(incoming_control_message)).unwrap(); - let pred = |report: &FunderReport<_>| report.num_ready_receipts == 0; - await!(node_controls[0].recv_until(pred)); + // MultiCommit: 0 ==> 2 (Out of band) + + // 2: Apply MultiCommit: + await!(node_controls[2].send(FunderControl::CommitInvoice(multi_commit))); + + // 0: Expect a receipt: + let (receipt, ack_uid) = loop { + await!(node_controls[0].send( + FunderControl::RequestClosePayment(PaymentId::from(&[2u8; PAYMENT_ID_LEN])))); + let response_close_payment = await!(node_controls[0].recv_until_response_close_payment()).unwrap(); + match response_close_payment { + ResponseClosePayment::Success((receipt, ack_uid)) => break (receipt, ack_uid), + _ => {}, + } + }; + + // 0: Acknowledge response close: + await!(node_controls[0].send( + FunderControl::AckClosePayment((PaymentId::from(&[2u8; PAYMENT_ID_LEN]), ack_uid)))); + + assert_eq!(receipt.invoice_id, InvoiceId::from(&[1u8; INVOICE_ID_LEN])); + assert_eq!(receipt.dest_payment, 15); + assert_eq!(receipt.total_dest_payment, 15); + // Make sure that node2 got the credits: let pred = |report: &FunderReport<_>| { @@ -242,7 +278,7 @@ async fn task_funder_forward_payment(spawner: impl Spawn + Clone + Send + 'stati ChannelStatusReport::Consistent(tc_report) => tc_report, _ => return false, }; - tc_report.balance.balance == -6 + 20 + tc_report.balance.balance == -6 + 15 }; await!(node_controls[2].recv_until(pred)); } @@ -253,6 +289,8 @@ fn test_funder_forward_payment() { thread_pool.run(task_funder_forward_payment(thread_pool.clone())); } +/* + async fn task_funder_payment_failure(spawner: impl Spawn + Clone + Send + 'static) { /* * 0 -- 1 -- 2 diff --git a/components/funder/src/tests/utils.rs b/components/funder/src/tests/utils.rs index 6f6c47641..49e52c7f9 100644 --- a/components/funder/src/tests/utils.rs +++ b/components/funder/src/tests/utils.rs @@ -24,7 +24,8 @@ use proto::report::messages::{ use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ AddFriend, FriendStatus, FunderControl, FunderIncomingControl, FunderOutgoingControl, - RequestsStatus, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, TransactionResult, ResponseClosePayment, + RequestsStatus, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, TransactionResult, + ResponseClosePayment, SetFriendRate, Rate, }; use database::DatabaseClient; @@ -302,21 +303,10 @@ where pub async fn add_relay<'a>(&'a mut self, named_relay_address: NamedRelayAddress) { await!(self.send(FunderControl::AddRelay(named_relay_address.clone()))); - let pred = |report: &FunderReport<_>| report.relays.contains(&named_relay_address); - await!(self.recv_until(pred)); } pub async fn remove_relay<'a>(&'a mut self, public_key: PublicKey) { await!(self.send(FunderControl::RemoveRelay(public_key.clone()))); - let pred = |report: &FunderReport<_>| { - for relay in &report.relays { - if relay.public_key == public_key { - return true; - } - } - false - }; - await!(self.recv_until(pred)); } pub async fn add_friend<'a>( @@ -333,8 +323,6 @@ where balance, }; await!(self.send(FunderControl::AddFriend(add_friend))); - let pred = |report: &FunderReport<_>| report.friends.contains_key(&friend_public_key); - await!(self.recv_until(pred)); } pub async fn set_friend_status<'a>( @@ -347,11 +335,6 @@ where status: status.clone(), }; await!(self.send(FunderControl::SetFriendStatus(set_friend_status))); - let pred = |report: &FunderReport<_>| match report.friends.get(&friend_public_key) { - None => false, - Some(friend) => friend.status == FriendStatusReport::from(&status), - }; - await!(self.recv_until(pred)); } pub async fn set_remote_max_debt<'a>( @@ -364,19 +347,6 @@ where remote_max_debt: remote_max_debt, }; await!(self.send(FunderControl::SetFriendRemoteMaxDebt(set_remote_max_debt))); - - let pred = |report: &FunderReport<_>| { - let friend = match report.friends.get(&friend_public_key) { - Some(friend) => friend, - None => return false, - }; - let tc_report = match &friend.channel_status { - ChannelStatusReport::Consistent(tc_report) => tc_report, - _ => return false, - }; - tc_report.balance.remote_max_debt == remote_max_debt - }; - await!(self.recv_until(pred)); } pub async fn set_requests_status<'a>( @@ -389,19 +359,18 @@ where status: requests_status.clone(), }; await!(self.send(FunderControl::SetRequestsStatus(set_requests_status))); + } - let pred = |report: &FunderReport<_>| { - let friend = match report.friends.get(&friend_public_key) { - Some(friend) => friend, - None => return false, - }; - let tc_report = match &friend.channel_status { - ChannelStatusReport::Consistent(tc_report) => tc_report, - _ => return false, - }; - tc_report.requests_status.local == RequestsStatusReport::from(&requests_status) + pub async fn set_friend_rate<'a>( + &'a mut self, + friend_public_key: &'a PublicKey, + rate: Rate, + ) { + let set_friend_rate = SetFriendRate { + friend_public_key: friend_public_key.clone(), + rate, }; - await!(self.recv_until(pred)); + await!(self.send(FunderControl::SetFriendRate(set_friend_rate))); } pub async fn wait_until_ready<'a>(&'a mut self, friend_public_key: &'a PublicKey) { From 016f28b9ed86f551077e7c6987442b4d00f4a611 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 13:29:07 +0300 Subject: [PATCH 163/248] Added assertion for mediator fees in test_funder_forward_payment(). --- components/funder/src/tests/tests.rs | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/components/funder/src/tests/tests.rs b/components/funder/src/tests/tests.rs index dde684369..f698d1592 100644 --- a/components/funder/src/tests/tests.rs +++ b/components/funder/src/tests/tests.rs @@ -281,6 +281,35 @@ async fn task_funder_forward_payment(spawner: impl Spawn + Clone + Send + 'stati tc_report.balance.balance == -6 + 15 }; await!(node_controls[2].recv_until(pred)); + + // Make sure that node1 got his fees: + let pred = |report: &FunderReport<_>| { + // Balance with node 0: + let friend = match report.friends.get(&public_keys[0]) { + None => return false, + Some(friend) => friend, + }; + let tc_report = match &friend.channel_status { + ChannelStatusReport::Consistent(tc_report) => tc_report, + _ => return false, + }; + + if tc_report.balance.balance != -8 + 20 { + return false; + } + + // Balance with node 2: + let friend = match report.friends.get(&public_keys[2]) { + None => return false, + Some(friend) => friend, + }; + let tc_report = match &friend.channel_status { + ChannelStatusReport::Consistent(tc_report) => tc_report, + _ => return false, + }; + tc_report.balance.balance == 6 - 15 + }; + await!(node_controls[1].recv_until(pred)); } #[test] From 2d7e2df6947643f9e0280000aa69c9e80106414b Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 15:20:17 +0300 Subject: [PATCH 164/248] Added TODO comment. --- components/funder/src/tests/tests.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/funder/src/tests/tests.rs b/components/funder/src/tests/tests.rs index f698d1592..7cee47577 100644 --- a/components/funder/src/tests/tests.rs +++ b/components/funder/src/tests/tests.rs @@ -539,3 +539,5 @@ fn test_funder_add_relay() { thread_pool.run(task_funder_add_relay(thread_pool.clone())); } */ + +// TODO: Add a test for multi-route payment From 4a3fabbb5fdd8049e9e8f70bc45621a5114aaa31 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 15:41:14 +0300 Subject: [PATCH 165/248] Fixed case of Payment::InProgress(_) at control_request_close_payment(). --- .../funder/src/handler/handle_control.rs | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 1ce888592..b47aa41e2 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -672,13 +672,15 @@ where Ok(()) } -fn control_request_close_payment( +fn control_request_close_payment( m_state: &mut MutableFunderState, outgoing_control: &mut Vec>, + rng: &R, payment_id: PaymentId, ) -> Result<(), HandleControlError> where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, + R: CryptoRandom, { let payment = if let Some(payment) = m_state.state().payments.get(&payment_id) { payment @@ -695,10 +697,20 @@ where Some(Payment::InProgress(new_transactions.num_transactions)), ResponseClosePayment::InProgress, ), - Payment::InProgress(num_transactions) => ( - Some(Payment::InProgress(*num_transactions)), - ResponseClosePayment::InProgress, - ), + Payment::InProgress(num_transactions) => { + (if *num_transactions == 0 { + let ack_uid = Uid::new(rng); + ( + Some(Payment::Canceled(ack_uid.clone())), + ResponseClosePayment::Canceled(ack_uid), + ) + } else { + ( + Some(Payment::InProgress(*num_transactions)), + ResponseClosePayment::InProgress, + ) + }) + } Payment::Success((num_transactions, receipt, ack_uid)) => ( Some(Payment::Success(( *num_transactions, @@ -1007,7 +1019,7 @@ where create_transaction, ), FunderControl::RequestClosePayment(payment_id) => { - control_request_close_payment(m_state, outgoing_control, payment_id) + control_request_close_payment(m_state, outgoing_control, rng, payment_id) } FunderControl::AckClosePayment((payment_id, ack_uid)) => { control_ack_close_payment(m_state, payment_id, ack_uid) From d6b799c212f56bfae1284703f7ea2ae7cfc2b229 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 15:41:42 +0300 Subject: [PATCH 166/248] Restored test_funder_payment_failure(). --- components/funder/src/tests/tests.rs | 91 +++++++++++++++++++--------- 1 file changed, 61 insertions(+), 30 deletions(-) diff --git a/components/funder/src/tests/tests.rs b/components/funder/src/tests/tests.rs index 7cee47577..4e5164cf4 100644 --- a/components/funder/src/tests/tests.rs +++ b/components/funder/src/tests/tests.rs @@ -209,7 +209,7 @@ async fn task_funder_forward_payment(spawner: impl Spawn + Clone + Send + 'stati payment_id: PaymentId::from(&[2u8; PAYMENT_ID_LEN]), invoice_id: InvoiceId::from(&[1u8; INVOICE_ID_LEN]), total_dest_payment: 15, - dest_public_key: node_controls[0].public_key.clone(), + dest_public_key: node_controls[2].public_key.clone(), }; await!(node_controls[0].send(FunderControl::CreatePayment(create_payment))); @@ -318,7 +318,6 @@ fn test_funder_forward_payment() { thread_pool.run(task_funder_forward_payment(thread_pool.clone())); } -/* async fn task_funder_payment_failure(spawner: impl Spawn + Clone + Send + 'static) { /* @@ -340,10 +339,10 @@ async fn task_funder_payment_failure(spawner: impl Spawn + Clone + Send + 'stati let relays0 = vec![dummy_relay_address(0)]; let relays1 = vec![dummy_relay_address(1)]; let relays2 = vec![dummy_relay_address(2)]; - await!(node_controls[0].add_friend(&public_keys[1], relays1.clone(), "node1", 8)); - await!(node_controls[1].add_friend(&public_keys[0], relays0, "node0", -8)); + await!(node_controls[0].add_friend(&public_keys[1], relays1, "node1", 8)); + await!(node_controls[1].add_friend(&public_keys[0], relays0.clone(), "node0", -8)); await!(node_controls[1].add_friend(&public_keys[2], relays2, "node2", 6)); - await!(node_controls[2].add_friend(&public_keys[1], relays1, "node0", -6)); + await!(node_controls[2].add_friend(&public_keys[1], relays0, "node0", -6)); // Enable friends: await!(node_controls[0].set_friend_status(&public_keys[1], FriendStatus::Enabled)); @@ -351,6 +350,10 @@ async fn task_funder_payment_failure(spawner: impl Spawn + Clone + Send + 'stati await!(node_controls[1].set_friend_status(&public_keys[2], FriendStatus::Enabled)); await!(node_controls[2].set_friend_status(&public_keys[1], FriendStatus::Enabled)); + // Set rate: + // This is the amount of credits node 1 takes from node 0 for forwarding messages. + await!(node_controls[1].set_friend_rate(&public_keys[0], Rate {mul: 0, add: 5})); + // Set remote max debt: await!(node_controls[0].set_remote_max_debt(&public_keys[1], 200)); await!(node_controls[1].set_remote_max_debt(&public_keys[0], 100)); @@ -367,9 +370,21 @@ async fn task_funder_payment_failure(spawner: impl Spawn + Clone + Send + 'stati await!(node_controls[0].wait_until_ready(&public_keys[1])); await!(node_controls[1].wait_until_ready(&public_keys[2])); - // Send credits 0 --> 2 - let user_request_send_funds = UserRequestSendFunds { - request_id: Uid::from(&[3; UID_LEN]), + + + // Create payment 0 --> 3 (Where 3 does not exist) + let create_payment = CreatePayment { + payment_id: PaymentId::from(&[2u8; PAYMENT_ID_LEN]), + invoice_id: InvoiceId::from(&[1u8; INVOICE_ID_LEN]), + total_dest_payment: 15, + dest_public_key: node_controls[3].public_key.clone(), + }; + await!(node_controls[0].send(FunderControl::CreatePayment(create_payment))); + + // Create transaction 0 --> 3 (3 does not exist): + let create_transaction = CreateTransaction { + payment_id: PaymentId::from(&[2u8; PAYMENT_ID_LEN]), + request_id: Uid::from(&[5u8; UID_LEN]), route: FriendsRoute { public_keys: vec![ public_keys[0].clone(), @@ -378,33 +393,47 @@ async fn task_funder_payment_failure(spawner: impl Spawn + Clone + Send + 'stati public_keys[3].clone(), ], }, - invoice_id: InvoiceId::from(&[1; INVOICE_ID_LEN]), - dest_payment: 20, + dest_payment: 15, + fees: 5, }; - let incoming_control_message = FunderIncomingControl::new( - Uid::from(&[44; UID_LEN]), - FunderControl::RequestSendFunds(user_request_send_funds), - ); - await!(node_controls[0].send(incoming_control_message)).unwrap(); - let response_received = await!(node_controls[0].recv_until_response()).unwrap(); - assert_eq!(response_received.request_id, Uid::from(&[3; UID_LEN])); - let reporting_public_key = match response_received.result { - ResponseSendFundsResult::Failure(reporting_public_key) => reporting_public_key, - ResponseSendFundsResult::Success(_) => unreachable!(), + await!(node_controls[0].send(FunderControl::CreateTransaction(create_transaction))); + let transaction_result = await!(node_controls[0].recv_until_transaction_result()).unwrap(); + + // We expect failure: + match transaction_result.result { + RequestResult::Failure => {}, + _ => unreachable!(), + } + + + // 0: Expect that the payment was canceled: + let ack_uid = loop { + await!(node_controls[0].send( + FunderControl::RequestClosePayment(PaymentId::from(&[2u8; PAYMENT_ID_LEN])))); + let response_close_payment = await!(node_controls[0].recv_until_response_close_payment()).unwrap(); + match response_close_payment { + ResponseClosePayment::Canceled(ack_uid) => break ack_uid, + _ => {}, + } }; - assert_eq!(reporting_public_key, public_keys[2]); + // 0: Acknowledge response close: + await!(node_controls[0].send( + FunderControl::AckClosePayment((PaymentId::from(&[2u8; PAYMENT_ID_LEN]), ack_uid)))); - let friend = node_controls[2] - .report - .friends - .get(&public_keys[1]) - .unwrap(); - let tc_report = match &friend.channel_status { - ChannelStatusReport::Consistent(tc_report) => tc_report, - _ => unreachable!(), + // Make sure that node0's balance is left unchanged: + let pred = |report: &FunderReport<_>| { + let friend = match report.friends.get(&public_keys[1]) { + None => return false, + Some(friend) => friend, + }; + let tc_report = match &friend.channel_status { + ChannelStatusReport::Consistent(tc_report) => tc_report, + _ => return false, + }; + tc_report.balance.balance == 8 }; - assert_eq!(tc_report.balance.balance, -6); + await!(node_controls[0].recv_until(pred)); } #[test] @@ -413,6 +442,8 @@ fn test_funder_payment_failure() { thread_pool.run(task_funder_payment_failure(thread_pool.clone())); } +/* + /// Test a basic inconsistency between two adjacent nodes async fn task_funder_inconsistency_basic(spawner: S) where From f7539455d779a02615258b038bd66cdf9a6ba9e1 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 15:45:38 +0300 Subject: [PATCH 167/248] Restored test_funder_inconsistency_basic(). --- components/funder/src/tests/tests.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/components/funder/src/tests/tests.rs b/components/funder/src/tests/tests.rs index 4e5164cf4..19847123a 100644 --- a/components/funder/src/tests/tests.rs +++ b/components/funder/src/tests/tests.rs @@ -442,7 +442,6 @@ fn test_funder_payment_failure() { thread_pool.run(task_funder_payment_failure(thread_pool.clone())); } -/* /// Test a basic inconsistency between two adjacent nodes async fn task_funder_inconsistency_basic(spawner: S) @@ -510,11 +509,7 @@ where friend_public_key: public_keys[1].clone(), reset_token: reset_terms_report.reset_token.clone(), // TODO: Rename reset_token to reset_token? }; - let incoming_control_message = FunderIncomingControl::new( - Uid::from(&[45; UID_LEN]), - FunderControl::ResetFriendChannel(reset_friend_channel), - ); - await!(node_controls[0].send(incoming_control_message)).unwrap(); + await!(node_controls[0].send(FunderControl::ResetFriendChannel(reset_friend_channel))); // Wait until channel is consistent with the correct balance: let pred = |report: &FunderReport<_>| { @@ -550,6 +545,8 @@ fn test_funder_inconsistency_basic() { thread_pool.run(task_funder_inconsistency_basic(thread_pool.clone())); } +/* + /// Test setting relay address for local node async fn task_funder_add_relay(spawner: impl Spawn + Clone + Send + 'static) { let num_nodes = 1; From 53eda41a7e5eee468c22771d28c2c6e70acaf268 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 15:46:07 +0300 Subject: [PATCH 168/248] Fixed failing channel send()-s during teardown. --- components/funder/src/tests/utils.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/funder/src/tests/utils.rs b/components/funder/src/tests/utils.rs index 49e52c7f9..51661c6d1 100644 --- a/components/funder/src/tests/utils.rs +++ b/components/funder/src/tests/utils.rs @@ -96,7 +96,7 @@ async fn router_handle_outgoing_comm<'a, B: 'a>( assert!(node.friends.contains(&src_public_key)); let incoming_comm_message = FunderIncomingComm::Friend((src_public_key.clone(), friend_message)); - await!(node.comm_out.send(incoming_comm_message)).unwrap(); + let _ = await!(node.comm_out.send(incoming_comm_message)); } FunderOutgoingComm::ChannelerConfig(channeler_config) => { match channeler_config { @@ -113,13 +113,13 @@ async fn router_handle_outgoing_comm<'a, B: 'a>( let incoming_comm_message = FunderIncomingComm::Liveness( IncomingLivenessMessage::Online(src_public_key.clone()), ); - await!(remote_node_comm_out.send(incoming_comm_message)).unwrap(); + let _ = await!(remote_node_comm_out.send(incoming_comm_message)); let incoming_comm_message = FunderIncomingComm::Liveness(IncomingLivenessMessage::Online( channeler_add_friend.friend_public_key.clone(), )); - await!(comm_out.send(incoming_comm_message)).unwrap(); + let _ = await!(comm_out.send(incoming_comm_message)); } } ChannelerConfig::RemoveFriend(friend_public_key) => { @@ -136,7 +136,7 @@ async fn router_handle_outgoing_comm<'a, B: 'a>( let incoming_comm_message = FunderIncomingComm::Liveness( IncomingLivenessMessage::Offline(friend_public_key.clone()), ); - await!(comm_out.send(incoming_comm_message)).unwrap(); + let _ = await!(comm_out.send(incoming_comm_message)); } } ChannelerConfig::SetRelays(_) => { From f4b6608dc251d164bcb7a9fa7404d45b88e56817 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 15:47:12 +0300 Subject: [PATCH 169/248] Restored test_funder_add_relay(). --- components/funder/src/tests/tests.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/funder/src/tests/tests.rs b/components/funder/src/tests/tests.rs index 19847123a..473597b13 100644 --- a/components/funder/src/tests/tests.rs +++ b/components/funder/src/tests/tests.rs @@ -545,7 +545,6 @@ fn test_funder_inconsistency_basic() { thread_pool.run(task_funder_inconsistency_basic(thread_pool.clone())); } -/* /// Test setting relay address for local node async fn task_funder_add_relay(spawner: impl Spawn + Clone + Send + 'static) { @@ -566,6 +565,5 @@ fn test_funder_add_relay() { let mut thread_pool = ThreadPool::new().unwrap(); thread_pool.run(task_funder_add_relay(thread_pool.clone())); } -*/ // TODO: Add a test for multi-route payment From b08bf6367c4b515de57db90e10c1cbb8ad673a58 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 16:40:54 +0300 Subject: [PATCH 170/248] control_create_transaction_inner(): dest_public_key must be last in route. --- components/funder/src/handler/handle_control.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index b47aa41e2..d57bed179 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -39,6 +39,7 @@ pub enum HandleControlError { NotInvitedToReset, ResetTokenMismatch, NotFirstInRoute, + PaymentDestNotLastInRoute, InvalidRoute, RequestAlreadyInProgress, PendingUserRequestsFull, @@ -543,6 +544,11 @@ where _ => Err(HandleControlError::NotFirstInRoute), }?; + match route.public_keys.last() { + Some(last) if *last == new_transactions.dest_public_key => Ok(()), + _ => Err(HandleControlError::PaymentDestNotLastInRoute), + }?; + // We want to have at least two public keys on the route (source and destination). // We also want that the public keys on the route are unique. if !route.is_valid() { From e08d41b3389792ba2094e2c88c24375c91422295 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 16:43:05 +0300 Subject: [PATCH 171/248] Removed unused warnings from mod utils. --- components/funder/src/tests/mod.rs | 1 - components/funder/src/tests/utils.rs | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/components/funder/src/tests/mod.rs b/components/funder/src/tests/mod.rs index ca0a03159..3c84fb1f7 100644 --- a/components/funder/src/tests/mod.rs +++ b/components/funder/src/tests/mod.rs @@ -1,4 +1,3 @@ #[allow(unused)] mod tests; -#[allow(unused)] pub mod utils; diff --git a/components/funder/src/tests/utils.rs b/components/funder/src/tests/utils.rs index 51661c6d1..e4a0948ca 100644 --- a/components/funder/src/tests/utils.rs +++ b/components/funder/src/tests/utils.rs @@ -13,11 +13,10 @@ use crypto::identity::{ }; use crypto::test_utils::DummyRandom; use crypto::uid::{Uid, UID_LEN}; -use crypto::payment_id::{PaymentId, PAYMENT_ID_LEN}; use proto::report::messages::{ - ChannelStatusReport, FriendLivenessReport, FriendStatusReport, FunderReport, + ChannelStatusReport, FriendLivenessReport, FunderReport, FunderReportMutations, RequestsStatusReport, }; @@ -295,7 +294,7 @@ where loop { match await!(self.recv())? { NodeRecv::ReportMutations(_) => {} - NodeRecv::TransactionResult(transaction_result) => {}, + NodeRecv::TransactionResult(_) => {}, NodeRecv::ResponseClosePayment(response_close_payment) => return Some(response_close_payment) }; } From 8e44766afcc2075090344371728eafcea4122e31 Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 16:44:39 +0300 Subject: [PATCH 172/248] Fixed unused warnings in mod tests. --- components/funder/src/tests/mod.rs | 1 - components/funder/src/tests/tests.rs | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/components/funder/src/tests/mod.rs b/components/funder/src/tests/mod.rs index 3c84fb1f7..eca0d7c4d 100644 --- a/components/funder/src/tests/mod.rs +++ b/components/funder/src/tests/mod.rs @@ -1,3 +1,2 @@ -#[allow(unused)] mod tests; pub mod utils; diff --git a/components/funder/src/tests/tests.rs b/components/funder/src/tests/tests.rs index 473597b13..85d0d4b65 100644 --- a/components/funder/src/tests/tests.rs +++ b/components/funder/src/tests/tests.rs @@ -7,8 +7,8 @@ use crypto::uid::{Uid, UID_LEN}; use crypto::payment_id::{PaymentId, PAYMENT_ID_LEN}; use proto::funder::messages::{ - FriendStatus, FriendsRoute, FunderControl, FunderIncomingControl, ReceiptAck, RequestsStatus, - ResetFriendChannel, AddInvoice, CreatePayment, CreateTransaction, TransactionResult, RequestResult, MultiCommit, + FriendStatus, FriendsRoute, FunderControl, RequestsStatus, + ResetFriendChannel, AddInvoice, CreatePayment, CreateTransaction, RequestResult, MultiCommit, ResponseClosePayment, Rate, }; use proto::report::messages::{ChannelStatusReport, FunderReport}; From c475d7c0354707f81ac03115893136062359f35e Mon Sep 17 00:00:00 2001 From: real Date: Mon, 27 May 2019 17:36:24 +0300 Subject: [PATCH 173/248] Dealing with the case of already having an outgoing transaction. --- .../funder/src/handler/handle_control.rs | 37 ++++++++++++++++++- components/funder/src/handler/utils.rs | 29 +++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index d57bed179..3757b8ef0 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -20,7 +20,7 @@ use proto::funder::messages::{ SetFriendName, SetFriendRate, SetFriendRelays, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, TransactionResult, }; -use proto::funder::signature_buff::verify_multi_commit; +use proto::funder::signature_buff::{prepare_commit, verify_multi_commit}; use crate::ephemeral::Ephemeral; use crate::handler::canceler::{ @@ -29,7 +29,7 @@ use crate::handler::canceler::{ }; use crate::handler::sender::SendCommands; use crate::handler::state_wrap::{MutableEphemeral, MutableFunderState}; -use crate::handler::utils::{find_request_origin, is_friend_ready}; +use crate::handler::utils::{find_local_pending_transaction, find_request_origin, is_friend_ready}; use crate::types::ChannelerConfig; @@ -654,6 +654,39 @@ where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, R: CryptoRandom, { + // If we already have this transaction: + // - If we have a ready response, we return a Commit message. + // - Else, we do nothing. + // + // This could happen if the user has disconnected before managing to obtain the + // `RequestResult::Success` message. + if let Some(open_transaction) = m_state + .state() + .open_transactions + .get(&create_transaction.request_id) + { + match ( + &open_transaction.opt_response, + find_local_pending_transaction(m_state.state(), &create_transaction.request_id), + ) { + (Some(response_send_funds), Some(pending_transaction)) => { + let commit = prepare_commit( + response_send_funds, + pending_transaction, + open_transaction.src_plain_lock.clone(), + ); + + let transaction_result = TransactionResult { + request_id: response_send_funds.request_id.clone(), + result: RequestResult::Success(commit), + }; + outgoing_control.push(FunderOutgoingControl::TransactionResult(transaction_result)); + } + _ => {} + } + return Ok(()); + } + // If we managed to push the message, we return an Ok(()). // Otherwise, we return the internal error and return a response failure message. if let Err(e) = control_create_transaction_inner( diff --git a/components/funder/src/handler/utils.rs b/components/funder/src/handler/utils.rs index 0fdeca9bd..0dfe43fcd 100644 --- a/components/funder/src/handler/utils.rs +++ b/components/funder/src/handler/utils.rs @@ -2,6 +2,8 @@ use std::fmt::Debug; use common::canonical_serialize::CanonicalSerialize; +use proto::funder::messages::PendingTransaction; + use crypto::identity::PublicKey; use crypto::uid::Uid; @@ -42,6 +44,33 @@ where None } +/// Find an outgoing pending transaction +pub fn find_local_pending_transaction<'a, B>( + state: &'a FunderState, + request_id: &Uid, +) -> Option<&'a PendingTransaction> +where + B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, +{ + for (_friend_public_key, friend) in &state.friends { + match &friend.channel_status { + ChannelStatus::Inconsistent(_) => continue, + ChannelStatus::Consistent(token_channel) => { + if let Some(pending_transaction) = token_channel + .get_mutual_credit() + .state() + .pending_transactions + .local + .get(request_id) + { + return Some(pending_transaction); + } + } + } + } + None +} + pub fn is_friend_ready( state: &FunderState, ephemeral: &Ephemeral, From 49cec5cc221927bc4d5036602e156d937f12ac85 Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 10:45:46 +0300 Subject: [PATCH 174/248] Refactored AckClosePayment into a struct (Instead of anonymous tuple). --- components/app_server/src/lib.rs | 4 +-- components/app_server/src/server.rs | 8 ++--- .../funder/src/handler/handle_control.rs | 34 +++++++++++-------- .../funder/src/handler/tests/pair_basic.rs | 8 +++-- components/funder/src/tests/tests.rs | 20 ++++++++--- components/proto/src/app_server/messages.rs | 15 +++++--- components/proto/src/app_server/serialize.rs | 6 ++++ components/proto/src/funder/messages.rs | 9 ++++- 8 files changed, 71 insertions(+), 33 deletions(-) diff --git a/components/app_server/src/lib.rs b/components/app_server/src/lib.rs index 2dc289598..57fd7157f 100644 --- a/components/app_server/src/lib.rs +++ b/components/app_server/src/lib.rs @@ -20,7 +20,7 @@ extern crate common; mod server; -#[cfg(test)] -mod tests; +// #[cfg(test)] +// mod tests; pub use self::server::{app_server_loop, AppServerError, IncomingAppConnection}; diff --git a/components/app_server/src/server.rs b/components/app_server/src/server.rs index 8eab12cf1..7bbe13f63 100644 --- a/components/app_server/src/server.rs +++ b/components/app_server/src/server.rs @@ -199,20 +199,20 @@ where funder_message: FunderOutgoingControl, ) -> Result<(), AppServerError> { match funder_message { - FunderOutgoingControl::ResponseReceived(response_received) => { + FunderOutgoingControl::TransactionResult(transaction_result) => { // Find the app that issued the request, and forward the response to this app: // TODO: Should we break the loop if found? for app in self.apps.values_mut() { if app .open_send_funds_requests - .remove(&response_received.request_id) + .remove(&transaction_result.request_id) { await!( - app.send(AppServerToApp::ResponseReceived(response_received.clone())) + app.send(AppServerToApp::TransactionResult(transaction_result.clone())) ); } } - } + }, FunderOutgoingControl::ReportMutations(funder_report_mutations) => { let mut index_mutations = Vec::new(); for funder_report_mutation in &funder_report_mutations.mutations { diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 3757b8ef0..268a3f9f2 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -14,11 +14,11 @@ use crate::state::{FunderMutation, NewTransactions, Payment}; use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ - AddFriend, AddInvoice, ChannelerUpdateFriend, CollectSendFundsOp, CreatePayment, - CreateTransaction, FriendStatus, FunderControl, FunderOutgoingControl, MultiCommit, - RemoveFriend, RequestResult, RequestSendFundsOp, ResetFriendChannel, ResponseClosePayment, - SetFriendName, SetFriendRate, SetFriendRelays, SetFriendRemoteMaxDebt, SetFriendStatus, - SetRequestsStatus, TransactionResult, + AckClosePayment, AddFriend, AddInvoice, ChannelerUpdateFriend, CollectSendFundsOp, + CreatePayment, CreateTransaction, FriendStatus, FunderControl, FunderOutgoingControl, + MultiCommit, RemoveFriend, RequestResult, RequestSendFundsOp, ResetFriendChannel, + ResponseClosePayment, SetFriendName, SetFriendRate, SetFriendRelays, SetFriendRemoteMaxDebt, + SetFriendStatus, SetRequestsStatus, TransactionResult, }; use proto::funder::signature_buff::{prepare_commit, verify_multi_commit}; @@ -794,8 +794,7 @@ where fn control_ack_close_payment( m_state: &mut MutableFunderState, - payment_id: PaymentId, - user_ack_uid: Uid, + ack_close_payment: AckClosePayment, ) -> Result<(), HandleControlError> where B: Clone + PartialEq + Eq + CanonicalSerialize + Debug, @@ -803,7 +802,7 @@ where let payment = m_state .state() .payments - .get(&payment_id) + .get(&ack_close_payment.payment_id) .ok_or(HandleControlError::PaymentDoesNotExist)? .clone(); @@ -813,29 +812,34 @@ where } Payment::Success((num_transactions, _receipt, ack_uid)) => { // Make sure that ack matches: - if user_ack_uid != ack_uid { + if ack_close_payment.ack_uid != ack_uid { return Err(HandleControlError::AckMismatch); } if num_transactions > 0 { // Update payment to be `AfterSuccessAck`: let new_payment = Payment::AfterSuccessAck(num_transactions); - let funder_mutation = FunderMutation::UpdatePayment((payment_id, new_payment)); + let funder_mutation = FunderMutation::UpdatePayment(( + ack_close_payment.payment_id.clone(), + new_payment, + )); m_state.mutate(funder_mutation); } else { // Remove payment (no pending transactions): - let funder_mutation = FunderMutation::RemovePayment(payment_id); + let funder_mutation = + FunderMutation::RemovePayment(ack_close_payment.payment_id.clone()); m_state.mutate(funder_mutation); } } Payment::Canceled(ack_uid) => { // Make sure that ack matches: - if user_ack_uid != ack_uid { + if ack_close_payment.ack_uid != ack_uid { return Err(HandleControlError::AckMismatch); } // Remove payment: - let funder_mutation = FunderMutation::RemovePayment(payment_id); + let funder_mutation = + FunderMutation::RemovePayment(ack_close_payment.payment_id.clone()); m_state.mutate(funder_mutation); } }; @@ -1060,8 +1064,8 @@ where FunderControl::RequestClosePayment(payment_id) => { control_request_close_payment(m_state, outgoing_control, rng, payment_id) } - FunderControl::AckClosePayment((payment_id, ack_uid)) => { - control_ack_close_payment(m_state, payment_id, ack_uid) + FunderControl::AckClosePayment(ack_close_payment) => { + control_ack_close_payment(m_state, ack_close_payment) } // Seller API: diff --git a/components/funder/src/handler/tests/pair_basic.rs b/components/funder/src/handler/tests/pair_basic.rs index a1389d36f..b9f22d79c 100644 --- a/components/funder/src/handler/tests/pair_basic.rs +++ b/components/funder/src/handler/tests/pair_basic.rs @@ -18,7 +18,7 @@ use crypto::payment_id::{PaymentId, PAYMENT_ID_LEN}; use proto::funder::messages::{ AddFriend, FriendMessage, FriendStatus, FriendsRoute, FunderControl, FunderIncomingControl, RequestsStatus, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, - CreatePayment, AddInvoice, CreateTransaction, RequestResult, FunderOutgoingControl, MultiCommit, ResponseClosePayment, + CreatePayment, AddInvoice, CreateTransaction, RequestResult, FunderOutgoingControl, MultiCommit, ResponseClosePayment, AckClosePayment, }; use crate::ephemeral::Ephemeral; @@ -896,10 +896,14 @@ async fn task_handler_pair_basic<'a>( assert_eq!(receipt.dest_payment, 16); assert_eq!(receipt.total_dest_payment, 16); + let ack_close_payment = AckClosePayment { + payment_id: PaymentId::from(&[3u8; PAYMENT_ID_LEN]), + ack_uid: ack_uid.clone(), + }; // Node2: Send ack for closing the payment: let incoming_control_message = FunderIncomingControl::new( Uid::from(&[22; UID_LEN]), - FunderControl::AckClosePayment((PaymentId::from(&[3u8; PAYMENT_ID_LEN]), ack_uid.clone())), + FunderControl::AckClosePayment(ack_close_payment), ); let funder_incoming = FunderIncoming::Control(incoming_control_message); let (_outgoing_comms, _outgoing_control) = await!(Box::pin(apply_funder_incoming( diff --git a/components/funder/src/tests/tests.rs b/components/funder/src/tests/tests.rs index 85d0d4b65..86259f1ee 100644 --- a/components/funder/src/tests/tests.rs +++ b/components/funder/src/tests/tests.rs @@ -9,7 +9,7 @@ use crypto::payment_id::{PaymentId, PAYMENT_ID_LEN}; use proto::funder::messages::{ FriendStatus, FriendsRoute, FunderControl, RequestsStatus, ResetFriendChannel, AddInvoice, CreatePayment, CreateTransaction, RequestResult, MultiCommit, - ResponseClosePayment, Rate, + ResponseClosePayment, Rate, AckClosePayment, }; use proto::report::messages::{ChannelStatusReport, FunderReport}; @@ -110,8 +110,12 @@ async fn task_funder_basic(spawner: impl Spawn + Clone + Send + 'static) { }; // 0: Acknowledge response close: + let ack_close_payment = AckClosePayment { + payment_id: PaymentId::from(&[2u8; PAYMENT_ID_LEN]), + ack_uid, + }; await!(node_controls[0].send( - FunderControl::AckClosePayment((PaymentId::from(&[2u8; PAYMENT_ID_LEN]), ack_uid)))); + FunderControl::AckClosePayment(ack_close_payment))); assert_eq!(receipt.invoice_id, InvoiceId::from(&[1u8; INVOICE_ID_LEN])); assert_eq!(receipt.dest_payment, 4); @@ -260,8 +264,12 @@ async fn task_funder_forward_payment(spawner: impl Spawn + Clone + Send + 'stati }; // 0: Acknowledge response close: + let ack_close_payment = AckClosePayment { + payment_id: PaymentId::from(&[2u8; PAYMENT_ID_LEN]), + ack_uid, + }; await!(node_controls[0].send( - FunderControl::AckClosePayment((PaymentId::from(&[2u8; PAYMENT_ID_LEN]), ack_uid)))); + FunderControl::AckClosePayment(ack_close_payment))); assert_eq!(receipt.invoice_id, InvoiceId::from(&[1u8; INVOICE_ID_LEN])); assert_eq!(receipt.dest_payment, 15); @@ -418,8 +426,12 @@ async fn task_funder_payment_failure(spawner: impl Spawn + Clone + Send + 'stati }; // 0: Acknowledge response close: + let ack_close_payment = AckClosePayment { + payment_id: PaymentId::from(&[2u8; PAYMENT_ID_LEN]), + ack_uid, + }; await!(node_controls[0].send( - FunderControl::AckClosePayment((PaymentId::from(&[2u8; PAYMENT_ID_LEN]), ack_uid)))); + FunderControl::AckClosePayment(ack_close_payment))); // Make sure that node0's balance is left unchanged: let pred = |report: &FunderReport<_>| { diff --git a/components/proto/src/app_server/messages.rs b/components/proto/src/app_server/messages.rs index 42ca20fcf..8a2173deb 100644 --- a/components/proto/src/app_server/messages.rs +++ b/components/proto/src/app_server/messages.rs @@ -1,11 +1,12 @@ use common::canonical_serialize::CanonicalSerialize; use common::mutable_state::MutableState; use crypto::identity::PublicKey; +use crypto::payment_id::PaymentId; use crypto::uid::Uid; use crate::funder::messages::{ - AddFriend, ReceiptAck, ResetFriendChannel, SetFriendName, SetFriendRelays, - SetFriendRemoteMaxDebt, TransactionResult, UserRequestSendFunds, + AddFriend, CreatePayment, CreateTransaction, ReceiptAck, ResetFriendChannel, SetFriendName, + SetFriendRate, SetFriendRelays, SetFriendRemoteMaxDebt, TransactionResult, }; use crate::index_client::messages::{ ClientResponseRoutes, IndexClientReport, IndexClientReportMutation, @@ -100,9 +101,6 @@ pub enum AppRequest { /// Manage locally used relays: AddRelay(NamedRelayAddress), RemoveRelay(PublicKey), - /// Sending funds: - RequestSendFunds(UserRequestSendFunds), - ReceiptAck(ReceiptAck), /// Friend management: AddFriend(AddFriend), SetFriendRelays(SetFriendRelays), @@ -113,7 +111,14 @@ pub enum AppRequest { OpenFriend(PublicKey), CloseFriend(PublicKey), SetFriendRemoteMaxDebt(SetFriendRemoteMaxDebt), + SetFriendRate(SetFriendRate), ResetFriendChannel(ResetFriendChannel), + /// Sending funds: + CreatePayment(CreatePayment), + CreateTransaction(CreateTransaction), + RequestClosePayment(PaymentId), + AckClosePayment(PaymentId), + ReceiptAck(ReceiptAck), /// Request routes from one node to another: RequestRoutes(RequestRoutes), /// Manage index servers: diff --git a/components/proto/src/app_server/serialize.rs b/components/proto/src/app_server/serialize.rs index 65b747063..96031d445 100644 --- a/components/proto/src/app_server/serialize.rs +++ b/components/proto/src/app_server/serialize.rs @@ -512,6 +512,8 @@ fn ser_app_request( app_request: &AppRequest, app_request_builder: &mut app_server_capnp::app_request::Builder, ) { + unimplemented!(); + /* match app_request { AppRequest::AddRelay(named_relay_address) => write_named_relay_address( named_relay_address, @@ -588,11 +590,14 @@ fn ser_app_request( &mut app_request_builder.reborrow().init_remove_index_server(), ), } + */ } fn deser_app_request( app_request: &app_server_capnp::app_request::Reader, ) -> Result { + unimplemented!(); + /* Ok(match app_request.which()? { app_server_capnp::app_request::AddRelay(named_relay_address_reader) => { AppRequest::AddRelay(read_named_relay_address(&named_relay_address_reader?)?) @@ -652,6 +657,7 @@ fn deser_app_request( AppRequest::RemoveIndexServer(read_public_key(&public_key_reader?)?) } }) + */ } fn ser_app_to_app_server( diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 708b44128..ea7cc16dd 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -529,6 +529,13 @@ pub struct AddInvoice { pub total_dest_payment: u128, } +/// Start an invoice (A request for payment). +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct AckClosePayment { + pub payment_id: PaymentId, + pub ack_uid: Uid, +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum FunderControl { AddRelay(NamedRelayAddress), @@ -546,7 +553,7 @@ pub enum FunderControl { CreatePayment(CreatePayment), CreateTransaction(CreateTransaction), // TODO RequestClosePayment(PaymentId), - AckClosePayment((PaymentId, Uid)), // (payment_id, ack_id) + AckClosePayment(AckClosePayment), // Seller API: AddInvoice(AddInvoice), CancelInvoice(InvoiceId), From 5729e37716f147f9b06e64104a4097d533ab6efe Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 10:57:24 +0300 Subject: [PATCH 175/248] Some updates to offst-app-server to allow atomic payments. Not done yet. --- components/app_server/src/server.rs | 36 +++++++++++++++++---- components/proto/src/app_server/messages.rs | 7 ++-- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/components/app_server/src/server.rs b/components/app_server/src/server.rs index 7bbe13f63..50df16380 100644 --- a/components/app_server/src/server.rs +++ b/components/app_server/src/server.rs @@ -99,8 +99,10 @@ fn check_permissions(app_permissions: &AppPermissions, app_request: &AppReque match app_request { AppRequest::AddRelay(_) => app_permissions.config, AppRequest::RemoveRelay(_) => app_permissions.config, - AppRequest::RequestSendFunds(_) => app_permissions.send_funds, - AppRequest::ReceiptAck(_) => app_permissions.send_funds, + AppRequest::CreatePayment(_) => app_permissions.send_funds, + AppRequest::CreateTransaction(_) => app_permissions.send_funds, + AppRequest::RequestClosePayment(_) => app_permissions.send_funds, + AppRequest::AckClosePayment(_) => app_permissions.send_funds, AppRequest::AddFriend(_) => app_permissions.config, AppRequest::SetFriendRelays(_) => app_permissions.config, AppRequest::SetFriendName(_) => app_permissions.config, @@ -110,6 +112,7 @@ fn check_permissions(app_permissions: &AppPermissions, app_request: &AppReque AppRequest::OpenFriend(_) => app_permissions.config, AppRequest::CloseFriend(_) => app_permissions.config, AppRequest::SetFriendRemoteMaxDebt(_) => app_permissions.config, + AppRequest::SetFriendRate(_) => app_permissions.config, AppRequest::ResetFriendChannel(_) => app_permissions.config, AppRequest::RequestRoutes(_) => app_permissions.routes, AppRequest::AddIndexServer(_) => app_permissions.config, @@ -213,6 +216,10 @@ where } } }, + FunderOutgoingControl::ResponseClosePayment(_response_close_payment) => { + // TODO: + unimplemented!(); + }, FunderOutgoingControl::ReportMutations(funder_report_mutations) => { let mut index_mutations = Vec::new(); for funder_report_mutation in &funder_report_mutations.mutations { @@ -327,18 +334,26 @@ where FunderIncomingControl::new(app_request_id, FunderControl::RemoveRelay(public_key)) )) .map_err(|_| AppServerError::SendToFunderError), - AppRequest::RequestSendFunds(user_request_send_funds) => { + AppRequest::CreatePayment(create_payment) => await!(self.to_funder.send( + FunderIncomingControl::new(app_request_id, FunderControl::CreatePayment(create_payment)) + )) + .map_err(|_| AppServerError::SendToFunderError), + AppRequest::CreateTransaction(create_transaction) => { // Keep track of which application issued this request: app.open_send_funds_requests - .insert(user_request_send_funds.request_id); + .insert(create_transaction.request_id); await!(self.to_funder.send(FunderIncomingControl::new( app_request_id, - FunderControl::RequestSendFunds(user_request_send_funds) + FunderControl::CreateTransaction(create_transaction) ))) .map_err(|_| AppServerError::SendToFunderError) } - AppRequest::ReceiptAck(receipt_ack) => await!(self.to_funder.send( - FunderIncomingControl::new(app_request_id, FunderControl::ReceiptAck(receipt_ack)) + AppRequest::RequestClosePayment(request_close_payment) => await!(self.to_funder.send( + FunderIncomingControl::new(app_request_id, FunderControl::RequestClosePayment(request_close_payment)) + )) + .map_err(|_| AppServerError::SendToFunderError), + AppRequest::AckClosePayment(ack_close_payment) => await!(self.to_funder.send( + FunderIncomingControl::new(app_request_id, FunderControl::AckClosePayment(ack_close_payment)) )) .map_err(|_| AppServerError::SendToFunderError), AppRequest::AddFriend(add_friend) => await!(self.to_funder.send( @@ -418,6 +433,13 @@ where ))) .map_err(|_| AppServerError::SendToFunderError) } + AppRequest::SetFriendRate(set_friend_rate) => { + await!(self.to_funder.send(FunderIncomingControl::new( + app_request_id, + FunderControl::SetFriendRate(set_friend_rate) + ))) + .map_err(|_| AppServerError::SendToFunderError) + } AppRequest::ResetFriendChannel(reset_friend_channel) => { await!(self.to_funder.send(FunderIncomingControl::new( app_request_id, diff --git a/components/proto/src/app_server/messages.rs b/components/proto/src/app_server/messages.rs index 8a2173deb..dee15aa6b 100644 --- a/components/proto/src/app_server/messages.rs +++ b/components/proto/src/app_server/messages.rs @@ -5,8 +5,8 @@ use crypto::payment_id::PaymentId; use crypto::uid::Uid; use crate::funder::messages::{ - AddFriend, CreatePayment, CreateTransaction, ReceiptAck, ResetFriendChannel, SetFriendName, - SetFriendRate, SetFriendRelays, SetFriendRemoteMaxDebt, TransactionResult, + AckClosePayment, AddFriend, CreatePayment, CreateTransaction, ResetFriendChannel, + SetFriendName, SetFriendRate, SetFriendRelays, SetFriendRemoteMaxDebt, TransactionResult, }; use crate::index_client::messages::{ ClientResponseRoutes, IndexClientReport, IndexClientReportMutation, @@ -117,8 +117,7 @@ pub enum AppRequest { CreatePayment(CreatePayment), CreateTransaction(CreateTransaction), RequestClosePayment(PaymentId), - AckClosePayment(PaymentId), - ReceiptAck(ReceiptAck), + AckClosePayment(AckClosePayment), /// Request routes from one node to another: RequestRoutes(RequestRoutes), /// Manage index servers: From 10ec87b8d0e2a7e96349ac3a2da41aee42e323db Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 11:23:02 +0300 Subject: [PATCH 176/248] ResponseClosePayment now contains PaymentId (Helps matching request/response). --- components/app_server/src/server.rs | 41 +++++++++++-------- .../funder/src/handler/handle_control.rs | 30 +++++++++----- .../funder/src/handler/tests/pair_basic.rs | 12 +++--- components/funder/src/tests/tests.rs | 14 +++---- components/proto/src/funder/messages.rs | 8 +++- 5 files changed, 63 insertions(+), 42 deletions(-) diff --git a/components/app_server/src/server.rs b/components/app_server/src/server.rs index 50df16380..dbcfad06b 100644 --- a/components/app_server/src/server.rs +++ b/components/app_server/src/server.rs @@ -54,8 +54,6 @@ pub enum AppServerEvent { pub struct App { permissions: AppPermissions, opt_sender: Option>>, - open_route_requests: HashSet, - open_send_funds_requests: HashSet, } impl App @@ -91,6 +89,11 @@ pub struct AppServer { /// Required because an app (with one public key) might have multiple connections. app_counter: u128, apps: HashMap>, + /// Data structures to track open requests. + /// This allows us to multiplex requests/responses to multiple apps: + open_route_requests: HashMap, + open_payments: HashMap, + open_transactions: HashMap, spawner: S, } @@ -303,21 +306,24 @@ where app_message: AppToAppServer, ) -> Result<(), AppServerError> { // Get the relevant application: - let app = match self.apps.get_mut(&app_id) { - Some(app) => app, - None => { - warn!("App {:?} does not exist!", app_id); + + { + let app = match self.apps.get(&app_id) { + Some(app) => app, + None => { + warn!("App {:?} does not exist!", app_id); + return Ok(()); + } + }; + + // Make sure this message is allowed for this application: + if !check_permissions(&app.permissions, &app_message.app_request) { + warn!( + "App {:?} does not have permissions for {:?}", + app_id, app_message + ); return Ok(()); } - }; - - // Make sure this message is allowed for this application: - if !check_permissions(&app.permissions, &app_message.app_request) { - warn!( - "App {:?} does not have permissions for {:?}", - app_id, app_message - ); - return Ok(()); } let app_request_id = app_message.app_request_id; @@ -340,8 +346,7 @@ where .map_err(|_| AppServerError::SendToFunderError), AppRequest::CreateTransaction(create_transaction) => { // Keep track of which application issued this request: - app.open_send_funds_requests - .insert(create_transaction.request_id); + self.open_transactions.insert(create_transaction.request_id.clone(), app_uid); await!(self.to_funder.send(FunderIncomingControl::new( app_request_id, FunderControl::CreateTransaction(create_transaction) @@ -449,7 +454,7 @@ where } AppRequest::RequestRoutes(request_routes) => { // Keep track of which application issued this request: - app.open_route_requests.insert(request_routes.request_id); + self.open_route_requests.insert(request_routes.request_id.clone(), app_id) await!(self .to_index_client .send(AppServerToIndexClient::AppRequest(( diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 268a3f9f2..05ff3ec08 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -16,9 +16,9 @@ use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ AckClosePayment, AddFriend, AddInvoice, ChannelerUpdateFriend, CollectSendFundsOp, CreatePayment, CreateTransaction, FriendStatus, FunderControl, FunderOutgoingControl, - MultiCommit, RemoveFriend, RequestResult, RequestSendFundsOp, ResetFriendChannel, - ResponseClosePayment, SetFriendName, SetFriendRate, SetFriendRelays, SetFriendRemoteMaxDebt, - SetFriendStatus, SetRequestsStatus, TransactionResult, + MultiCommit, PaymentStatus, RemoveFriend, RequestResult, RequestSendFundsOp, + ResetFriendChannel, ResponseClosePayment, SetFriendName, SetFriendRate, SetFriendRelays, + SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, TransactionResult, }; use proto::funder::signature_buff::{prepare_commit, verify_multi_commit}; @@ -725,28 +725,32 @@ where payment } else { // Payment not found: + let response_close_payment = ResponseClosePayment { + payment_id: payment_id.clone(), + status: PaymentStatus::PaymentNotFound, + }; outgoing_control.push(FunderOutgoingControl::ResponseClosePayment( - ResponseClosePayment::PaymentNotFound, + response_close_payment, )); return Ok(()); }; - let (opt_new_payment, response_close_payment) = match payment { + let (opt_new_payment, payment_status) = match payment { Payment::NewTransactions(new_transactions) => ( Some(Payment::InProgress(new_transactions.num_transactions)), - ResponseClosePayment::InProgress, + PaymentStatus::InProgress, ), Payment::InProgress(num_transactions) => { (if *num_transactions == 0 { let ack_uid = Uid::new(rng); ( Some(Payment::Canceled(ack_uid.clone())), - ResponseClosePayment::Canceled(ack_uid), + PaymentStatus::Canceled(ack_uid), ) } else { ( Some(Payment::InProgress(*num_transactions)), - ResponseClosePayment::InProgress, + PaymentStatus::InProgress, ) }) } @@ -756,19 +760,23 @@ where receipt.clone(), *ack_uid, ))), - ResponseClosePayment::Success((receipt.clone(), *ack_uid)), + PaymentStatus::Success((receipt.clone(), *ack_uid)), ), Payment::Canceled(ack_uid) => ( Some(Payment::Canceled(*ack_uid)), - ResponseClosePayment::Canceled(*ack_uid), + PaymentStatus::Canceled(*ack_uid), ), Payment::AfterSuccessAck(num_transactions) => ( Some(Payment::AfterSuccessAck(*num_transactions)), - ResponseClosePayment::PaymentNotFound, + PaymentStatus::PaymentNotFound, ), }; // Send back a ResponseClosePayment: + let response_close_payment = ResponseClosePayment { + payment_id: payment_id.clone(), + status: payment_status, + }; outgoing_control.push(FunderOutgoingControl::ResponseClosePayment( response_close_payment, )); diff --git a/components/funder/src/handler/tests/pair_basic.rs b/components/funder/src/handler/tests/pair_basic.rs index b9f22d79c..d6444892b 100644 --- a/components/funder/src/handler/tests/pair_basic.rs +++ b/components/funder/src/handler/tests/pair_basic.rs @@ -18,7 +18,8 @@ use crypto::payment_id::{PaymentId, PAYMENT_ID_LEN}; use proto::funder::messages::{ AddFriend, FriendMessage, FriendStatus, FriendsRoute, FunderControl, FunderIncomingControl, RequestsStatus, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, - CreatePayment, AddInvoice, CreateTransaction, RequestResult, FunderOutgoingControl, MultiCommit, ResponseClosePayment, AckClosePayment, + CreatePayment, AddInvoice, CreateTransaction, RequestResult, FunderOutgoingControl, + MultiCommit, AckClosePayment, PaymentStatus, }; use crate::ephemeral::Ephemeral; @@ -887,8 +888,9 @@ async fn task_handler_pair_basic<'a>( _ => unreachable!(), }; - let (receipt, ack_uid) = match response_close_payment { - ResponseClosePayment::Success((receipt, ack_uid)) => (receipt, ack_uid), + assert_eq!(response_close_payment.payment_id, PaymentId::from(&[3u8; PAYMENT_ID_LEN])); + let (receipt, ack_uid) = match &response_close_payment.status { + PaymentStatus::Success((receipt, ack_uid)) => (receipt, ack_uid), _ => unreachable!(), }; @@ -939,8 +941,8 @@ async fn task_handler_pair_basic<'a>( _ => unreachable!(), }; - match response_close_payment { - ResponseClosePayment::PaymentNotFound => {}, + match response_close_payment.status { + PaymentStatus::PaymentNotFound => {}, _ => unreachable!(), } } diff --git a/components/funder/src/tests/tests.rs b/components/funder/src/tests/tests.rs index 86259f1ee..2cc3a7b7e 100644 --- a/components/funder/src/tests/tests.rs +++ b/components/funder/src/tests/tests.rs @@ -9,7 +9,7 @@ use crypto::payment_id::{PaymentId, PAYMENT_ID_LEN}; use proto::funder::messages::{ FriendStatus, FriendsRoute, FunderControl, RequestsStatus, ResetFriendChannel, AddInvoice, CreatePayment, CreateTransaction, RequestResult, MultiCommit, - ResponseClosePayment, Rate, AckClosePayment, + Rate, AckClosePayment, PaymentStatus, }; use proto::report::messages::{ChannelStatusReport, FunderReport}; @@ -103,8 +103,8 @@ async fn task_funder_basic(spawner: impl Spawn + Clone + Send + 'static) { await!(node_controls[0].send( FunderControl::RequestClosePayment(PaymentId::from(&[2u8; PAYMENT_ID_LEN])))); let response_close_payment = await!(node_controls[0].recv_until_response_close_payment()).unwrap(); - match response_close_payment { - ResponseClosePayment::Success((receipt, ack_uid)) => break (receipt, ack_uid), + match response_close_payment.status { + PaymentStatus::Success((receipt, ack_uid)) => break (receipt, ack_uid), _ => {}, } }; @@ -257,8 +257,8 @@ async fn task_funder_forward_payment(spawner: impl Spawn + Clone + Send + 'stati await!(node_controls[0].send( FunderControl::RequestClosePayment(PaymentId::from(&[2u8; PAYMENT_ID_LEN])))); let response_close_payment = await!(node_controls[0].recv_until_response_close_payment()).unwrap(); - match response_close_payment { - ResponseClosePayment::Success((receipt, ack_uid)) => break (receipt, ack_uid), + match response_close_payment.status { + PaymentStatus::Success((receipt, ack_uid)) => break (receipt, ack_uid), _ => {}, } }; @@ -419,8 +419,8 @@ async fn task_funder_payment_failure(spawner: impl Spawn + Clone + Send + 'stati await!(node_controls[0].send( FunderControl::RequestClosePayment(PaymentId::from(&[2u8; PAYMENT_ID_LEN])))); let response_close_payment = await!(node_controls[0].recv_until_response_close_payment()).unwrap(); - match response_close_payment { - ResponseClosePayment::Canceled(ack_uid) => break ack_uid, + match response_close_payment.status { + PaymentStatus::Canceled(ack_uid) => break ack_uid, _ => {}, } }; diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index ea7cc16dd..854644404 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -611,13 +611,19 @@ pub struct TransactionResult { } #[derive(Debug, Clone, PartialEq, Eq)] -pub enum ResponseClosePayment { +pub enum PaymentStatus { PaymentNotFound, InProgress, // Can not be acked Success((Receipt, Uid)), // (Receipt, ack_id) Canceled(Uid), // ack_id } +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ResponseClosePayment { + pub payment_id: PaymentId, + pub status: PaymentStatus, +} + #[derive(Debug)] pub enum FunderOutgoingControl { TransactionResult(TransactionResult), From 03148904e4929ead7b7b3b227a3b27705c8b9b1e Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 11:37:19 +0300 Subject: [PATCH 177/248] Updated offst-app-server for new atomic payments design. --- components/app_server/src/server.rs | 79 ++++++++++++--------- components/proto/src/app_server/messages.rs | 4 +- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/components/app_server/src/server.rs b/components/app_server/src/server.rs index dbcfad06b..cd4c3cfa7 100644 --- a/components/app_server/src/server.rs +++ b/components/app_server/src/server.rs @@ -1,4 +1,4 @@ -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; use std::fmt::Debug; use std::marker::Unpin; @@ -10,6 +10,7 @@ use common::conn::ConnPair; use common::select_streams::{select_streams, BoxStream}; // use common::mutable_state::MutableState; use crypto::uid::Uid; +use crypto::payment_id::PaymentId; use proto::funder::messages::{ FriendStatus, FunderControl, FunderIncomingControl, FunderOutgoingControl, RemoveFriend, @@ -64,8 +65,6 @@ where App { permissions, opt_sender: Some(sender), - open_route_requests: HashSet::new(), - open_send_funds_requests: HashSet::new(), } } @@ -89,11 +88,11 @@ pub struct AppServer { /// Required because an app (with one public key) might have multiple connections. app_counter: u128, apps: HashMap>, - /// Data structures to track open requests. + /// Data structures to track ongoing requests. /// This allows us to multiplex requests/responses to multiple apps: - open_route_requests: HashMap, - open_payments: HashMap, - open_transactions: HashMap, + route_requests: HashMap, + close_payment_requests: HashMap, + transactions: HashMap, spawner: S, } @@ -145,6 +144,9 @@ where incoming_connections_closed: false, app_counter: 0, apps: HashMap::new(), + route_requests: HashMap::new(), + close_payment_requests: HashMap::new(), + transactions: HashMap::new(), spawner, } } @@ -207,21 +209,31 @@ where match funder_message { FunderOutgoingControl::TransactionResult(transaction_result) => { // Find the app that issued the request, and forward the response to this app: - // TODO: Should we break the loop if found? - for app in self.apps.values_mut() { - if app - .open_send_funds_requests - .remove(&transaction_result.request_id) - { - await!( - app.send(AppServerToApp::TransactionResult(transaction_result.clone())) - ); - } + let app_id = if let Some(app_id) = self.transactions.remove(&transaction_result.request_id) { + app_id + } else { + warn!("TransactionResult: Could not find app that initiated CreateTransaction"); + return Ok(()); + }; + if let Some(app) = self.apps.get_mut(&app_id) { + await!( + app.send(AppServerToApp::TransactionResult(transaction_result.clone())) + ); } }, - FunderOutgoingControl::ResponseClosePayment(_response_close_payment) => { - // TODO: - unimplemented!(); + FunderOutgoingControl::ResponseClosePayment(response_close_payment) => { + // Find the app that issued the request, and forward the response to this app: + let app_id = if let Some(app_id) = self.close_payment_requests.remove(&response_close_payment.payment_id) { + app_id + } else { + warn!("ResponseClosePayment: Could not find app that initiated RequestClosePayment"); + return Ok(()); + }; + if let Some(app) = self.apps.get_mut(&app_id) { + await!( + app.send(AppServerToApp::ResponseClosePayment(response_close_payment.clone())) + ); + } }, FunderOutgoingControl::ReportMutations(funder_report_mutations) => { let mut index_mutations = Vec::new(); @@ -284,16 +296,17 @@ where } IndexClientToAppServer::ResponseRoutes(client_response_routes) => { // We search for the app that issued the request, and send it the response. - // TODO: Should we break the loop if we found one originating app? - for app in self.apps.values_mut() { - if app - .open_route_requests - .remove(&client_response_routes.request_id) - { - await!(app.send(AppServerToApp::ResponseRoutes( - client_response_routes.clone() - ))); - } + let app_id = if let Some(app_id) = self.route_requests.remove(&client_response_routes.request_id) { + app_id + } else { + warn!("ResponseRoutes: Could not find the app that issued RequestRoutes request"); + return Ok(()); + }; + + if let Some(app) = self.apps.get_mut(&app_id) { + await!(app.send(AppServerToApp::ResponseRoutes( + client_response_routes.clone() + ))); } } }; @@ -346,7 +359,7 @@ where .map_err(|_| AppServerError::SendToFunderError), AppRequest::CreateTransaction(create_transaction) => { // Keep track of which application issued this request: - self.open_transactions.insert(create_transaction.request_id.clone(), app_uid); + self.transactions.insert(create_transaction.request_id.clone(), app_id); await!(self.to_funder.send(FunderIncomingControl::new( app_request_id, FunderControl::CreateTransaction(create_transaction) @@ -454,7 +467,9 @@ where } AppRequest::RequestRoutes(request_routes) => { // Keep track of which application issued this request: - self.open_route_requests.insert(request_routes.request_id.clone(), app_id) + if let Some(_) = self.route_requests.insert(request_routes.request_id.clone(), app_id) { + warn!("RequestRoutes: request_id clash."); + } await!(self .to_index_client .send(AppServerToIndexClient::AppRequest(( diff --git a/components/proto/src/app_server/messages.rs b/components/proto/src/app_server/messages.rs index dee15aa6b..51261d431 100644 --- a/components/proto/src/app_server/messages.rs +++ b/components/proto/src/app_server/messages.rs @@ -6,7 +6,8 @@ use crypto::uid::Uid; use crate::funder::messages::{ AckClosePayment, AddFriend, CreatePayment, CreateTransaction, ResetFriendChannel, - SetFriendName, SetFriendRate, SetFriendRelays, SetFriendRemoteMaxDebt, TransactionResult, + ResponseClosePayment, SetFriendName, SetFriendRate, SetFriendRelays, SetFriendRemoteMaxDebt, + TransactionResult, }; use crate::index_client::messages::{ ClientResponseRoutes, IndexClientReport, IndexClientReportMutation, @@ -84,6 +85,7 @@ where { /// Funds: TransactionResult(TransactionResult), + ResponseClosePayment(ResponseClosePayment), /// Reports about current state: Report(NodeReport), ReportMutations(ReportMutations), From b6667008ecf4ab8ecffa605e707b1424907f90fe Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 11:39:06 +0300 Subject: [PATCH 178/248] Restored app_server/src/tests/utils.rs. --- components/app_server/src/lib.rs | 4 ++-- components/app_server/src/tests/mod.rs | 13 +++++++------ components/app_server/src/tests/utils.rs | 4 +++- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/components/app_server/src/lib.rs b/components/app_server/src/lib.rs index 57fd7157f..2dc289598 100644 --- a/components/app_server/src/lib.rs +++ b/components/app_server/src/lib.rs @@ -20,7 +20,7 @@ extern crate common; mod server; -// #[cfg(test)] -// mod tests; +#[cfg(test)] +mod tests; pub use self::server::{app_server_loop, AppServerError, IncomingAppConnection}; diff --git a/components/app_server/src/tests/mod.rs b/components/app_server/src/tests/mod.rs index d86208bb2..5a023908e 100644 --- a/components/app_server/src/tests/mod.rs +++ b/components/app_server/src/tests/mod.rs @@ -1,7 +1,8 @@ -mod all_apps_closed; -mod funder_command; -mod index_client_command; -mod request_routes; -mod request_send_funds; -mod two_apps; +// mod all_apps_closed; +// mod funder_command; +// mod index_client_command; +// mod request_routes; +// mod request_send_funds; +// mod two_apps; +#[allow(unused)] mod utils; diff --git a/components/app_server/src/tests/utils.rs b/components/app_server/src/tests/utils.rs index 3f681d3d0..124a57a63 100644 --- a/components/app_server/src/tests/utils.rs +++ b/components/app_server/src/tests/utils.rs @@ -63,7 +63,9 @@ where .into_iter() .collect(), friends: ImHashMap::new(), - num_ready_receipts: 0, + num_open_invoices: 0, + num_payments: 0, + num_open_transactions: 0, }; let server100 = NamedIndexServerAddress { From 4278d6a56ad251215d4dcc90bc62d018c5b08266 Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 11:40:12 +0300 Subject: [PATCH 179/248] offst-app-server tests: Restored some tests. --- components/app_server/src/tests/mod.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/components/app_server/src/tests/mod.rs b/components/app_server/src/tests/mod.rs index 5a023908e..5b202ec3d 100644 --- a/components/app_server/src/tests/mod.rs +++ b/components/app_server/src/tests/mod.rs @@ -1,8 +1,7 @@ -// mod all_apps_closed; -// mod funder_command; -// mod index_client_command; -// mod request_routes; +mod all_apps_closed; +mod funder_command; +mod index_client_command; +mod request_routes; // mod request_send_funds; -// mod two_apps; -#[allow(unused)] +mod two_apps; mod utils; From f7c1a0a47da189ca9f0e04870ee040eb248b80d2 Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 11:53:50 +0300 Subject: [PATCH 180/248] offst-app-server: Restored request_send_funds test. All tests restored. --- components/app_server/src/tests/mod.rs | 2 +- .../src/tests/request_send_funds.rs | 74 +++++++++++++------ 2 files changed, 51 insertions(+), 25 deletions(-) diff --git a/components/app_server/src/tests/mod.rs b/components/app_server/src/tests/mod.rs index 5b202ec3d..d86208bb2 100644 --- a/components/app_server/src/tests/mod.rs +++ b/components/app_server/src/tests/mod.rs @@ -2,6 +2,6 @@ mod all_apps_closed; mod funder_command; mod index_client_command; mod request_routes; -// mod request_send_funds; +mod request_send_funds; mod two_apps; mod utils; diff --git a/components/app_server/src/tests/request_send_funds.rs b/components/app_server/src/tests/request_send_funds.rs index d075dbf26..a22c793a9 100644 --- a/components/app_server/src/tests/request_send_funds.rs +++ b/components/app_server/src/tests/request_send_funds.rs @@ -6,11 +6,12 @@ use futures::{SinkExt, StreamExt}; use crypto::identity::{PublicKey, PUBLIC_KEY_LEN}; use crypto::invoice_id::{InvoiceId, INVOICE_ID_LEN}; use crypto::uid::{Uid, UID_LEN}; +use crypto::payment_id::{PaymentId, PAYMENT_ID_LEN}; use proto::app_server::messages::{AppPermissions, AppRequest, AppServerToApp, AppToAppServer}; use proto::funder::messages::{ - FriendsRoute, FunderControl, FunderOutgoingControl, ResponseReceived, ResponseSendFundsResult, - UserRequestSendFunds, + FriendsRoute, FunderControl, FunderOutgoingControl, CreatePayment, CreateTransaction, + TransactionResult, RequestResult, }; use super::utils::spawn_dummy_app_server; @@ -56,59 +57,84 @@ where let pk_e = PublicKey::from(&[0xee; PUBLIC_KEY_LEN]); let pk_f = PublicKey::from(&[0xff; PUBLIC_KEY_LEN]); - let user_request_send_funds = UserRequestSendFunds { + let create_payment = CreatePayment { + payment_id: PaymentId::from(&[1; PAYMENT_ID_LEN]), + invoice_id: InvoiceId::from(&[2; INVOICE_ID_LEN]), + total_dest_payment: 20, + dest_public_key: pk_f.clone(), + }; + let to_app_server = AppToAppServer::new( + Uid::from(&[22; UID_LEN]), + AppRequest::CreatePayment(create_payment.clone()), + ); + await!(app_sender0.send(to_app_server)).unwrap(); + + // CreatePayment command should be forwarded to the Funder: + let funder_incoming_control = await!(funder_receiver.next()).unwrap(); + assert_eq!( + funder_incoming_control.app_request_id, + Uid::from(&[22; UID_LEN]) + ); + match funder_incoming_control.funder_control { + FunderControl::CreatePayment(received_create_payment) => { + assert_eq!(received_create_payment, create_payment) + } + _ => unreachable!(), + }; + + let create_transaction = CreateTransaction { + payment_id: PaymentId::from(&[1; PAYMENT_ID_LEN]), request_id: Uid::from(&[3; UID_LEN]), route: FriendsRoute { public_keys: vec![pk_e.clone(), pk_f.clone()], }, - invoice_id: InvoiceId::from(&[1; INVOICE_ID_LEN]), dest_payment: 20, + fees: 4, }; - let to_app_server = AppToAppServer::new( - Uid::from(&[22; UID_LEN]), - AppRequest::RequestSendFunds(user_request_send_funds.clone()), + Uid::from(&[23; UID_LEN]), + AppRequest::CreateTransaction(create_transaction.clone()), ); await!(app_sender0.send(to_app_server)).unwrap(); - // RequestRoutes command should be forwarded to IndexClient: + // CreateTransaction command should be forwarded to the Funder: let funder_incoming_control = await!(funder_receiver.next()).unwrap(); assert_eq!( funder_incoming_control.app_request_id, - Uid::from(&[22; UID_LEN]) + Uid::from(&[23; UID_LEN]) ); match funder_incoming_control.funder_control { - FunderControl::RequestSendFunds(received_user_request_send_funds) => { - assert_eq!(received_user_request_send_funds, user_request_send_funds) + FunderControl::CreateTransaction(received_create_transaction) => { + assert_eq!(received_create_transaction, create_transaction) } _ => unreachable!(), }; - // Funder returns a response that is not related to any open request. - let response_received = ResponseReceived { + // Funder returns a TransactionResult that is not related to any open request. + let transaction_result = TransactionResult { request_id: Uid::from(&[2; UID_LEN]), - result: ResponseSendFundsResult::Failure(pk_e.clone()), + result: RequestResult::Failure, }; - await!(funder_sender.send(FunderOutgoingControl::ResponseReceived(response_received))).unwrap(); + await!(funder_sender.send(FunderOutgoingControl::TransactionResult(transaction_result))).unwrap(); // We shouldn't get an message at any of the apps: assert!(app_receiver0.try_next().is_err()); assert!(app_receiver1.try_next().is_err()); // Funder returns a response that corresponds to the open request: - let response_received = ResponseReceived { + let transaction_result = TransactionResult { request_id: Uid::from(&[3; UID_LEN]), - result: ResponseSendFundsResult::Failure(pk_e.clone()), + result: RequestResult::Failure, }; - await!(funder_sender.send(FunderOutgoingControl::ResponseReceived( - response_received.clone() + await!(funder_sender.send(FunderOutgoingControl::TransactionResult( + transaction_result.clone() ))) .unwrap(); let to_app_message = await!(app_receiver0.next()).unwrap(); match to_app_message { - AppServerToApp::ResponseReceived(obtained_response_received) => { - assert_eq!(obtained_response_received, response_received); + AppServerToApp::TransactionResult(received_transaction_result) => { + assert_eq!(received_transaction_result, transaction_result); } _ => unreachable!(), } @@ -118,11 +144,11 @@ where // Funder again returns the same response, // however, this time it will be discarded, because no open request // has a matching id: - let response_received = ResponseReceived { + let transaction_result = TransactionResult { request_id: Uid::from(&[3; UID_LEN]), - result: ResponseSendFundsResult::Failure(pk_e), + result: RequestResult::Failure, }; - await!(funder_sender.send(FunderOutgoingControl::ResponseReceived(response_received))).unwrap(); + await!(funder_sender.send(FunderOutgoingControl::TransactionResult(transaction_result))).unwrap(); // We shouldn't get an message at any of the apps: assert!(app_receiver0.try_next().is_err()); From 2cbdab90333ff3d9ba521882ea0a93e6b7d344cc Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 12:41:25 +0300 Subject: [PATCH 181/248] Updated offst-index-client code for new atomic payments design. --- components/index_client/src/lib.rs | 4 +-- components/index_client/src/seq_friends.rs | 17 ++++++---- components/proto/src/index_client/messages.rs | 10 +++++- components/proto/src/index_server/messages.rs | 10 +++++- .../proto/src/index_server/serialize.rs | 5 ++- components/proto/src/report/convert.rs | 33 +++++++++++++------ 6 files changed, 58 insertions(+), 21 deletions(-) diff --git a/components/index_client/src/lib.rs b/components/index_client/src/lib.rs index e571c1fbc..0b6236b10 100644 --- a/components/index_client/src/lib.rs +++ b/components/index_client/src/lib.rs @@ -28,8 +28,8 @@ mod seq_map; mod single_client; mod spawn; -#[cfg(test)] -mod tests; +// #[cfg(test)] +// mod tests; pub use self::index_client::{IndexClientConfig, IndexClientConfigMutation, IndexClientError}; pub use self::spawn::{spawn_index_client, SpawnIndexClientError}; diff --git a/components/index_client/src/seq_friends.rs b/components/index_client/src/seq_friends.rs index 839fd6164..56af86e6d 100644 --- a/components/index_client/src/seq_friends.rs +++ b/components/index_client/src/seq_friends.rs @@ -3,11 +3,11 @@ use futures::task::{Spawn, SpawnError, SpawnExt}; use futures::{SinkExt, StreamExt}; use crypto::identity::PublicKey; -use proto::index_client::messages::{IndexMutation, UpdateFriend}; +use proto::index_client::messages::{IndexMutation, UpdateFriend, FriendInfo}; use crate::seq_map::SeqMap; -pub type SeqFriends = SeqMap; +pub type SeqFriends = SeqMap; pub enum SeqFriendsRequest { Mutate(IndexMutation, oneshot::Sender<()>), @@ -29,8 +29,12 @@ pub struct SeqFriendsClient { fn apply_index_mutation(seq_friends: &mut SeqFriends, index_mutation: &IndexMutation) { match index_mutation { IndexMutation::UpdateFriend(update_friend) => { - let capacity_pair = (update_friend.send_capacity, update_friend.recv_capacity); - let _ = seq_friends.update(update_friend.public_key.clone(), capacity_pair); + let friend_info = FriendInfo { + send_capacity: update_friend.send_capacity, + recv_capacity: update_friend.recv_capacity, + rate: update_friend.rate.clone(), + }; + let _ = seq_friends.update(update_friend.public_key.clone(), friend_info); } IndexMutation::RemoveFriend(public_key) => { let _ = seq_friends.remove(public_key); @@ -56,12 +60,13 @@ async fn seq_friends_loop( let update_friend = seq_friends .next() - .map(|(cycle_countdown, (public_key, capacities))| { - let (send_capacity, recv_capacity) = capacities; + .map(|(cycle_countdown, (public_key, friend_info))| { + let FriendInfo {send_capacity, recv_capacity, rate} = friend_info; let update_friend = UpdateFriend { public_key, send_capacity, recv_capacity, + rate, }; (cycle_countdown, update_friend) }); diff --git a/components/proto/src/index_client/messages.rs b/components/proto/src/index_client/messages.rs index ceb2d8100..22da6146c 100644 --- a/components/proto/src/index_client/messages.rs +++ b/components/proto/src/index_client/messages.rs @@ -3,12 +3,20 @@ use std::collections::HashMap; use crypto::identity::PublicKey; use crypto::uid::Uid; +use crate::funder::messages::Rate; pub use crate::index_server::messages::{IndexMutation, RequestRoutes, UpdateFriend}; use crate::index_server::messages::{NamedIndexServerAddress, RouteWithCapacity}; +#[derive(Debug, Clone)] +pub struct FriendInfo { + pub send_capacity: u128, + pub recv_capacity: u128, + pub rate: Rate, +} + #[derive(Debug, Clone)] pub struct IndexClientState { - pub friends: HashMap, + pub friends: HashMap, } // --------------------------------------------------- diff --git a/components/proto/src/index_server/messages.rs b/components/proto/src/index_server/messages.rs index 5ef475b7b..bea5a3525 100644 --- a/components/proto/src/index_server/messages.rs +++ b/components/proto/src/index_server/messages.rs @@ -3,7 +3,7 @@ use crypto::hash::HashResult; use crypto::identity::{PublicKey, Signature}; use crypto::uid::Uid; -use crate::funder::messages::FriendsRoute; +use crate::funder::messages::{FriendsRoute, Rate}; use crate::net::messages::NetAddress; /// IndexClient -> IndexServer @@ -41,6 +41,14 @@ pub struct UpdateFriend { pub send_capacity: u128, /// To denote local requests closed, assign 0 to recvCapacity pub recv_capacity: u128, + /// The rate we charge for forwarding messages to another friend from this friend. + /// For example, in the following diagram we are X and A is the friend we are updating: + /// A -- X -- B + /// \ + /// --- C + /// We can set how much we charge A for forwarding funds. The same rate applies either when A + /// sends funds to B or to C. + pub rate: Rate, } /// IndexClient -> IndexServer diff --git a/components/proto/src/index_server/serialize.rs b/components/proto/src/index_server/serialize.rs index c2e95a120..16fc6a79b 100644 --- a/components/proto/src/index_server/serialize.rs +++ b/components/proto/src/index_server/serialize.rs @@ -155,13 +155,16 @@ fn ser_update_friend( } fn deser_update_friend( - update_friend_reader: &index_capnp::update_friend::Reader, + _update_friend_reader: &index_capnp::update_friend::Reader, ) -> Result { + unimplemented!(); + /* Ok(UpdateFriend { public_key: read_public_key(&update_friend_reader.get_public_key()?)?, send_capacity: read_custom_u_int128(&update_friend_reader.get_send_capacity()?)?, recv_capacity: read_custom_u_int128(&update_friend_reader.get_recv_capacity()?)?, }) + */ } fn ser_index_mutation( diff --git a/components/proto/src/report/convert.rs b/components/proto/src/report/convert.rs index 49472d433..ca83f7186 100644 --- a/components/proto/src/report/convert.rs +++ b/components/proto/src/report/convert.rs @@ -6,7 +6,7 @@ use common::safe_arithmetic::{SafeSignedArithmetic, SafeUnsignedArithmetic}; use crypto::identity::PublicKey; -use crate::index_client::messages::IndexClientState; +use crate::index_client::messages::{FriendInfo, IndexClientState}; use crate::index_server::messages::{IndexMutation, UpdateFriend}; use crate::report::messages::{ @@ -76,13 +76,18 @@ where .friends .iter() .map(|(friend_public_key, friend_report)| { + let (send_capacity, recv_capacity) = calc_friend_capacities(friend_report); ( friend_public_key.clone(), - calc_friend_capacities(friend_report), + FriendInfo { + send_capacity, + recv_capacity, + rate: friend_report.rate.clone(), + }, ) }) - .filter(|(_, (send_capacity, recv_capacity))| *send_capacity != 0 || *recv_capacity != 0) - .collect::>(); + .filter(|(_, friend_info)| friend_info.send_capacity != 0 || friend_info.recv_capacity != 0) + .collect::>(); IndexClientState { friends } } @@ -95,10 +100,16 @@ where B: Clone + Debug, { let create_update_friend = |public_key: &PublicKey| { - let opt_old_capacities = funder_report - .friends - .get(public_key) - .map(|old_friend_report| calc_friend_capacities(&old_friend_report)); + let opt_old_capacities_rate = + funder_report + .friends + .get(public_key) + .map(|old_friend_report| { + ( + calc_friend_capacities(&old_friend_report), + old_friend_report.rate.clone(), + ) + }); let mut new_funder_report = funder_report.clone(); new_funder_report.mutate(funder_report_mutation).unwrap(); @@ -106,14 +117,16 @@ where let new_friend_report = new_funder_report.friends.get(public_key).unwrap(); // We assert that a new friend was added let new_capacities = calc_friend_capacities(new_friend_report); + let new_rate = new_friend_report.rate.clone(); - // Return UpdateFriend if the new capacities are different than the old ones: - if opt_old_capacities != Some(new_capacities) { + // Return UpdateFriend if something has changed: + if opt_old_capacities_rate != Some((new_capacities, new_rate.clone())) { let (send_capacity, recv_capacity) = new_capacities; let update_friend = UpdateFriend { public_key: public_key.clone(), send_capacity, recv_capacity, + rate: new_rate, }; Some(IndexMutation::UpdateFriend(update_friend)) } else { From 509d585a6071efca0309299bfe9806c472a4fab4 Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 12:43:16 +0300 Subject: [PATCH 182/248] Restored offst-index-client tests. --- components/index_client/src/lib.rs | 4 ++-- components/index_client/src/tests.rs | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/components/index_client/src/lib.rs b/components/index_client/src/lib.rs index 0b6236b10..e571c1fbc 100644 --- a/components/index_client/src/lib.rs +++ b/components/index_client/src/lib.rs @@ -28,8 +28,8 @@ mod seq_map; mod single_client; mod spawn; -// #[cfg(test)] -// mod tests; +#[cfg(test)] +mod tests; pub use self::index_client::{IndexClientConfig, IndexClientConfigMutation, IndexClientError}; pub use self::spawn::{spawn_index_client, SpawnIndexClientError}; diff --git a/components/index_client/src/tests.rs b/components/index_client/src/tests.rs index 6fcaa6514..42ecf754d 100644 --- a/components/index_client/src/tests.rs +++ b/components/index_client/src/tests.rs @@ -12,6 +12,7 @@ use proto::index_client::messages::{ IndexMutation, RequestRoutes, ResponseRoutesResult, UpdateFriend, }; use proto::index_server::messages::{IndexServerAddress, NamedIndexServerAddress}; +use proto::funder::messages::Rate; use database::{DatabaseClient, DatabaseRequest}; @@ -154,6 +155,7 @@ where public_key: PublicKey::from(PublicKey::from(&[0xaa; PUBLIC_KEY_LEN])), send_capacity: 100, recv_capacity: 50, + rate: Rate {mul: 0, add: 1}, }; response_sender.send(Some((0, update_friend))).unwrap(); } @@ -320,6 +322,7 @@ where public_key: PublicKey::from(PublicKey::from(&[0xbb; PUBLIC_KEY_LEN])), send_capacity: 200, recv_capacity: 100, + rate: Rate {mul: 0, add: 1}, }; let index_mutation = IndexMutation::UpdateFriend(update_friend); let mutations = vec![index_mutation.clone()]; @@ -343,6 +346,7 @@ where public_key: PublicKey::from(PublicKey::from(&[0xcc; PUBLIC_KEY_LEN])), send_capacity: 20, recv_capacity: 30, + rate: Rate {mul: 0, add: 1}, }; match await!(icc.seq_friends_receiver.next()).unwrap() { @@ -464,6 +468,7 @@ where public_key: PublicKey::from(PublicKey::from(&[0xbb; PUBLIC_KEY_LEN])), send_capacity: 200, recv_capacity: 100, + rate: Rate {mul: 0, add: 1}, }; let index_mutation = IndexMutation::UpdateFriend(update_friend); let mutations = vec![index_mutation.clone()]; From e3a271ba0b21c7a4aed306fd04371352dd6536c3 Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 12:46:54 +0300 Subject: [PATCH 183/248] index_server: Showing friend rate in debug print. --- components/index_server/src/server.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/components/index_server/src/server.rs b/components/index_server/src/server.rs index d7af1272a..75db42c87 100644 --- a/components/index_server/src/server.rs +++ b/components/index_server/src/server.rs @@ -294,10 +294,11 @@ where match index_mutation { IndexMutation::UpdateFriend(update_friend) => { info!( - "pk: {}, send: {}, recv: {}", + "pk: {}, send: {}, recv: {}, rate: {:?}", update_friend.public_key[0], update_friend.send_capacity, - update_friend.recv_capacity + update_friend.recv_capacity, + update_friend.rate, ); await!(self.graph_client.update_edge( From 4a3ed50f2ee95ece5991939727bb2b3cf134df7c Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 13:05:19 +0300 Subject: [PATCH 184/248] Updated offst-index-server messages (proto). Updated offst-index-client accordingly. --- components/index_client/src/single_client.rs | 16 ++++++++-------- components/proto/src/app_server/serialize.rs | 10 +++++++--- components/proto/src/index_client/messages.rs | 4 ++-- components/proto/src/index_server/messages.rs | 18 +++++++++++++++--- components/proto/src/index_server/mod.rs | 1 + components/proto/src/index_server/serialize.rs | 15 +++++++++++++-- 6 files changed, 46 insertions(+), 18 deletions(-) diff --git a/components/index_client/src/single_client.rs b/components/index_client/src/single_client.rs index 5fedfc080..4dce215dc 100644 --- a/components/index_client/src/single_client.rs +++ b/components/index_client/src/single_client.rs @@ -15,14 +15,14 @@ use identity::IdentityClient; use proto::index_server::messages::{ IndexClientToServer, IndexMutation, IndexServerToClient, MutationsUpdate, RequestRoutes, - ResponseRoutes, RouteWithCapacity, + ResponseRoutes, MultiRoute, }; pub type ServerConn = ConnPair; #[derive(Debug)] pub enum SingleClientControl { - RequestRoutes((RequestRoutes, oneshot::Sender>)), + RequestRoutes((RequestRoutes, oneshot::Sender>)), SendMutations(Vec), } @@ -54,7 +54,7 @@ struct SingleClient { /// We use this value to prove that our signatures are recent server_time_hash: HashResult, /// Unanswered requests, waiting for a response from the server - open_requests: HashMap>>, + open_requests: HashMap>>, } impl SingleClient @@ -90,7 +90,7 @@ where match index_server_to_client { IndexServerToClient::TimeHash(time_hash) => self.server_time_hash = time_hash, IndexServerToClient::ResponseRoutes(response_routes) => { - let ResponseRoutes { request_id, routes } = response_routes; + let ResponseRoutes { request_id, multi_routes } = response_routes; let request_sender = match self.open_requests.remove(&request_id) { Some(request_sender) => request_sender, None => { @@ -101,7 +101,7 @@ where return Ok(()); } }; - if request_sender.send(routes).is_err() { + if request_sender.send(multi_routes).is_err() { warn!( "Failed to return response for request_id: {:?} ", &request_id @@ -348,15 +348,15 @@ mod tests { // Server sends response routes: let response_routes = ResponseRoutes { request_id: Uid::from(&[3; UID_LEN]), - routes: vec![], // No suitable routes were found + multi_routes: vec![], // No suitable routes were found }; await!(server_sender.send(IndexServerToClient::ResponseRoutes(response_routes.clone()))) .unwrap(); // Client receives response routes: - let routes = await!(response_receiver).unwrap(); - assert_eq!(routes, vec![]); + let multi_routes = await!(response_receiver).unwrap(); + assert_eq!(multi_routes, vec![]); for iter in 0..3 { // Counter should increment every time diff --git a/components/proto/src/app_server/serialize.rs b/components/proto/src/app_server/serialize.rs index 96031d445..dd0315530 100644 --- a/components/proto/src/app_server/serialize.rs +++ b/components/proto/src/app_server/serialize.rs @@ -20,9 +20,7 @@ use crate::index_client::messages::{ClientResponseRoutes, ResponseRoutesResult}; use crate::report::serialize::{ deser_node_report, deser_node_report_mutation, ser_node_report, ser_node_report_mutation, }; -use index_server::serialize::{ - deser_request_routes, deser_route_with_capacity, ser_request_routes, ser_route_with_capacity, -}; +use index_server::serialize::{deser_request_routes, ser_request_routes}; use crate::funder::messages::{ AddFriend, ReceiptAck, @@ -305,6 +303,8 @@ fn ser_response_routes_result( response_routes_result: &ResponseRoutesResult, response_routes_result_builder: &mut app_server_capnp::response_routes_result::Builder, ) { + unimplemented!(); + /* match response_routes_result { ResponseRoutesResult::Success(routes_with_capacity) => { let routes_len = usize_to_u32(routes_with_capacity.len()).unwrap(); @@ -320,11 +320,14 @@ fn ser_response_routes_result( } ResponseRoutesResult::Failure => response_routes_result_builder.reborrow().set_failure(()), } + */ } fn deser_response_routes_result( response_routes_result_reader: &app_server_capnp::response_routes_result::Reader, ) -> Result { + unimplemented!(); + /* Ok(match response_routes_result_reader.which()? { app_server_capnp::response_routes_result::Success(routes_with_capacity_reader) => { let mut routes_with_capacity = Vec::new(); @@ -335,6 +338,7 @@ fn deser_response_routes_result( } app_server_capnp::response_routes_result::Failure(()) => ResponseRoutesResult::Failure, }) + */ } fn ser_client_response_routes( diff --git a/components/proto/src/index_client/messages.rs b/components/proto/src/index_client/messages.rs index 22da6146c..95b1673bb 100644 --- a/components/proto/src/index_client/messages.rs +++ b/components/proto/src/index_client/messages.rs @@ -5,7 +5,7 @@ use crypto::uid::Uid; use crate::funder::messages::Rate; pub use crate::index_server::messages::{IndexMutation, RequestRoutes, UpdateFriend}; -use crate::index_server::messages::{NamedIndexServerAddress, RouteWithCapacity}; +use crate::index_server::messages::{MultiRoute, NamedIndexServerAddress}; #[derive(Debug, Clone)] pub struct FriendInfo { @@ -48,7 +48,7 @@ pub enum IndexClientReportMutation { #[derive(Debug, Clone, PartialEq, Eq)] pub enum ResponseRoutesResult { - Success(Vec), + Success(Vec), Failure, } diff --git a/components/proto/src/index_server/messages.rs b/components/proto/src/index_server/messages.rs index bea5a3525..e1166f5e1 100644 --- a/components/proto/src/index_server/messages.rs +++ b/components/proto/src/index_server/messages.rs @@ -15,22 +15,34 @@ pub struct RequestRoutes { pub capacity: u128, pub source: PublicKey, pub destination: PublicKey, - /// This directed edge must not show up in the route. + /// This directed edge must not show up any any route inside the multi-route. /// Useful for finding non trivial directed loops. pub opt_exclude: Option<(PublicKey, PublicKey)>, } #[derive(Debug, PartialEq, Eq, Clone)] -pub struct RouteWithCapacity { +pub struct RouteCapacityRate { pub route: FriendsRoute, + /// How many credits we can push along this route? pub capacity: u128, + /// Combined rate of pushing credits along this route. + pub rate: Rate, +} + +/// Multiple routes that together allow to pass a certain amount of credits to a destination. +/// All routes must have the same beginning and the same end. +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct MultiRoute { + pub routes: Vec, } /// IndexServer -> IndexClient #[derive(Debug, Clone)] pub struct ResponseRoutes { pub request_id: Uid, - pub routes: Vec, + /// A few separate multi routes that allow to send the wanted amount of credits to the + /// requested destination: + pub multi_routes: Vec, } #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/components/proto/src/index_server/mod.rs b/components/proto/src/index_server/mod.rs index 77b8ff74c..0e17f28c4 100644 --- a/components/proto/src/index_server/mod.rs +++ b/components/proto/src/index_server/mod.rs @@ -1,3 +1,4 @@ pub mod messages; +#[allow(unused)] pub mod serialize; pub mod signature_buff; diff --git a/components/proto/src/index_server/serialize.rs b/components/proto/src/index_server/serialize.rs index 16fc6a79b..0d88248bb 100644 --- a/components/proto/src/index_server/serialize.rs +++ b/components/proto/src/index_server/serialize.rs @@ -11,8 +11,8 @@ use index_capnp; use super::messages::{ ForwardMutationsUpdate, IndexClientToServer, IndexMutation, IndexServerToClient, - IndexServerToServer, MutationsUpdate, RequestRoutes, ResponseRoutes, RouteWithCapacity, - TimeProofLink, UpdateFriend, + IndexServerToServer, MutationsUpdate, RequestRoutes, ResponseRoutes, TimeProofLink, + UpdateFriend, }; use crate::funder::serialize::{deser_friends_route, ser_friends_route}; @@ -81,10 +81,13 @@ pub fn deser_request_routes( }) } +/* pub fn ser_route_with_capacity( route_with_capacity: &RouteWithCapacity, route_with_capacity_builder: &mut index_capnp::route_with_capacity::Builder, ) { + unimplemented!(); + /* ser_friends_route( &route_with_capacity.route, &mut route_with_capacity_builder.reborrow().init_route(), @@ -93,6 +96,7 @@ pub fn ser_route_with_capacity( route_with_capacity.capacity, &mut route_with_capacity_builder.reborrow().init_capacity(), ); + */ } pub fn deser_route_with_capacity( @@ -103,11 +107,14 @@ pub fn deser_route_with_capacity( capacity: read_custom_u_int128(&route_with_capacity_reader.get_capacity()?)?, }) } +*/ fn ser_response_routes( response_routes: &ResponseRoutes, response_routes_builder: &mut index_capnp::response_routes::Builder, ) { + unimplemented!(); + /* write_uid( &response_routes.request_id, &mut response_routes_builder.reborrow().init_request_id(), @@ -120,11 +127,14 @@ fn ser_response_routes( routes_builder.reborrow().get(usize_to_u32(index).unwrap()); ser_route_with_capacity(&route, &mut route_with_capacity_builder); } + */ } fn deser_response_routes( response_routes_reader: &index_capnp::response_routes::Reader, ) -> Result { + unimplemented!(); + /* let mut routes = Vec::new(); for route_with_capacity in response_routes_reader.get_routes()? { routes.push(deser_route_with_capacity(&route_with_capacity)?); @@ -134,6 +144,7 @@ fn deser_response_routes( request_id: read_uid(&response_routes_reader.get_request_id()?)?, routes, }) + */ } fn ser_update_friend( From 2978cb7407b68414e7561a670394c60e88a069a2 Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 13:17:18 +0300 Subject: [PATCH 185/248] Omitted some modules from offst-index-server. Starting to gradually add them. --- components/index_server/src/lib.rs | 11 ++++++----- components/index_server/src/server.rs | 19 +++++++++++-------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/components/index_server/src/lib.rs b/components/index_server/src/lib.rs index 286754404..739a97008 100644 --- a/components/index_server/src/lib.rs +++ b/components/index_server/src/lib.rs @@ -16,13 +16,14 @@ #[macro_use] extern crate log; -#[macro_use] extern crate common; +#[allow(unused)] mod backoff_connector; +#[allow(unused)] mod graph; -mod net_server; -mod server; -mod verifier; +// mod net_server; +// mod server; +// mod verifier; -pub use net_server::{net_index_server, NetIndexServerError}; +// pub use net_server::{net_index_server, NetIndexServerError}; diff --git a/components/index_server/src/server.rs b/components/index_server/src/server.rs index 75db42c87..c93d808c3 100644 --- a/components/index_server/src/server.rs +++ b/components/index_server/src/server.rs @@ -14,7 +14,7 @@ use crypto::uid::Uid; use proto::index_server::messages::{ ForwardMutationsUpdate, IndexClientToServer, IndexMutation, IndexServerToClient, - IndexServerToServer, MutationsUpdate, ResponseRoutes, RouteWithCapacity, TimeProofLink, + IndexServerToServer, MutationsUpdate, ResponseRoutes, MultiRoute, TimeProofLink, }; use proto::funder::messages::FriendsRoute; @@ -384,17 +384,20 @@ async fn client_handler( .map_err(|_| ServerLoopError::ClientEventSenderError)?; } IndexClientToServer::RequestRoutes(request_routes) => { - let route_tuples = await!(graph_client.get_routes( + let graph_multi_routes = await!(graph_client.get_routes( request_routes.source.clone(), request_routes.destination.clone(), request_routes.capacity, request_routes.opt_exclude.clone() ))?; - let routes = route_tuples + let routes = graph_multi_routes .into_iter() - .map(|(route, capacity)| RouteWithCapacity { - route: FriendsRoute { public_keys: route }, - capacity, + .map(|graph_multi_route| MultiRoute { + routes: graph_multi_route.routes.map(|graph_route| RouteCapacityRate { + route: FriendRoute {public_keys: graph_route.route}, + capacity: graph_route.capacity, + rate: graph_route.rate, + }), }) .collect::>(); @@ -741,7 +744,7 @@ mod tests { match await!(client_receiver.next()).unwrap() { IndexServerToClient::ResponseRoutes(response_routes) => { assert_eq!(response_routes.request_id, request_id); - assert!(response_routes.routes.is_empty()); + assert!(response_routes.multi_routes.is_empty()); } _ => unreachable!(), }; @@ -1006,7 +1009,7 @@ mod tests { match await!(client_receiver.next()).unwrap() { IndexServerToClient::ResponseRoutes(response_routes) => { assert_eq!(response_routes.request_id, request_id); - assert!(response_routes.routes.is_empty()); + assert!(response_routes.multi_routes.is_empty()); } _ => unreachable!(), }; From 0e8f5d59c6999e69431de788c095dc6e74ed1cb4 Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 14:31:46 +0300 Subject: [PATCH 186/248] Updated simple_capacity_graph. --- .../index_server/src/graph/capacity_graph.rs | 44 ++- components/index_server/src/graph/mod.rs | 2 +- .../src/graph/simple_capacity_graph.rs | 265 ++++++++++++------ components/index_server/src/lib.rs | 1 + 4 files changed, 213 insertions(+), 99 deletions(-) diff --git a/components/index_server/src/graph/capacity_graph.rs b/components/index_server/src/graph/capacity_graph.rs index 9ff49c027..91688c221 100644 --- a/components/index_server/src/graph/capacity_graph.rs +++ b/components/index_server/src/graph/capacity_graph.rs @@ -1,15 +1,47 @@ +use std::ops::Add; + pub type CapacityEdge = (C, C); pub type CapacityRoute = (Vec, C); +pub trait LinearRate +where + Self: std::marker::Sized, +{ + /// Type used to count credits + type K; + + /// The zero LinearRate: + fn zero() -> Self; + /// Calculate the fee for forwarding a certain amount of credits being passed. + /// The resulting fee is also an amount of credits. + fn calc_fee(self, k: Self::K) -> Self::K; + /// Attempt to add two rates. + fn checked_add(self, other: &Self) -> Option; +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct GraphRoute { + pub route: Vec, + pub capacity: C, + pub rate: T, +} + +#[derive(Debug, Eq, PartialEq)] +pub struct GraphMultiRoute { + pub routes: Vec>, +} + pub trait CapacityGraph { type Node; // Node type type Capacity; // Directed capacity between two neighboring nodes + type Rate; // A type for the rate of sending credits along an edge/route. /// Add or update edge fn update_edge( &mut self, a: Self::Node, b: Self::Node, + rate: Self::Rate, edge: CapacityEdge, ) -> Option>; @@ -25,18 +57,20 @@ pub trait CapacityGraph { /// Returns true if the node `a` was present, false otherwise fn remove_node(&mut self, a: &Self::Node) -> bool; - /// Get a route with capacity at least `capacity`. - /// Returns the route together with the capacity it is possible to send through the route. + /// Get a multi routes with capacity at least `capacity`. + /// Returns every route in the multi route with the following additional information: + /// - Capacity (Amount of credits we can push along that route) + /// - Rate: Aggregated rate of how much it costs to send credits along that route. /// - /// opt_exclude is an optional edge to exclude (The returned route must not go through this + /// opt_exclude is an optional edge to exclude (All of the returned routes must not go through this /// edge). This can be useful for finding non trivial loops. - fn get_routes( + fn get_multi_routes( &self, a: &Self::Node, b: &Self::Node, capacity: Self::Capacity, opt_exclude: Option<(&Self::Node, &Self::Node)>, - ) -> Vec>; + ) -> Vec>; /// Simulate advancement of time. Used to remove old edges. fn tick(&mut self, a: &Self::Node); diff --git a/components/index_server/src/graph/mod.rs b/components/index_server/src/graph/mod.rs index 01f4d1992..5384d94fa 100644 --- a/components/index_server/src/graph/mod.rs +++ b/components/index_server/src/graph/mod.rs @@ -1,5 +1,5 @@ mod bfs; mod capacity_graph; -pub mod graph_service; +// pub mod graph_service; pub mod simple_capacity_graph; mod utils; diff --git a/components/index_server/src/graph/simple_capacity_graph.rs b/components/index_server/src/graph/simple_capacity_graph.rs index 192f74cd2..f7c54f52e 100644 --- a/components/index_server/src/graph/simple_capacity_graph.rs +++ b/components/index_server/src/graph/simple_capacity_graph.rs @@ -2,29 +2,35 @@ use std::collections::HashMap; use std::{cmp, hash}; use super::bfs::bfs; -use super::capacity_graph::{CapacityEdge, CapacityGraph}; +use super::capacity_graph::{CapacityEdge, CapacityGraph, GraphMultiRoute, GraphRoute, LinearRate}; use super::utils::{option_to_vec, OptionIterator}; /// Amount of ticks an edge could live regardless of coupon collector's approximation. /// This is useful to allow the first edges build (n*log(n) is very small for small n). const BASE_MAX_EDGE_AGE: u128 = 16; -struct Edge { +#[derive(Debug, Clone)] +struct Edge { capacity: CapacityEdge, + rate: T, age: u128, } -impl Edge { - fn new(capacity: CapacityEdge) -> Self { - Edge { capacity, age: 0 } +impl Edge { + fn new(capacity: CapacityEdge, rate: T) -> Self { + Edge { + capacity, + rate, + age: 0, + } } } -struct NodeEdges { - edges: HashMap, +struct NodeEdges { + edges: HashMap>, } -impl NodeEdges +impl NodeEdges where N: cmp::Eq + hash::Hash + Clone + std::fmt::Debug, { @@ -45,7 +51,7 @@ fn max_edge_age(num_edges: usize) -> u128 { BASE_MAX_EDGE_AGE + 3 * (num_edges as u128) } -impl NodeEdges +impl NodeEdges where N: cmp::Eq + hash::Hash + Clone + std::fmt::Debug, { @@ -59,27 +65,28 @@ where } } -pub struct SimpleCapacityGraph { - nodes: HashMap>, +pub struct SimpleCapacityGraph { + nodes: HashMap>, } -impl SimpleCapacityGraph +impl SimpleCapacityGraph where N: cmp::Eq + hash::Hash + Clone + std::fmt::Debug, + T: LinearRate + Clone, { - pub fn new() -> SimpleCapacityGraph { - SimpleCapacityGraph { + pub fn new() -> SimpleCapacityGraph { + Self { nodes: HashMap::new(), } } /// Get a directed edge (if exists) - fn get_edge(&self, a: &N, b: &N) -> Option> { + fn get_edge(&self, a: &N, b: &N) -> Option> { match self.nodes.get(a) { None => None, Some(a_edges) => match a_edges.edges.get(b) { None => None, - Some(a_b_edge) => Some(a_b_edge.capacity), + Some(a_b_edge) => Some(a_b_edge.clone()), }, } } @@ -89,13 +96,13 @@ where /// capacity reported by `b`. fn get_send_capacity(&self, a: &N, b: &N) -> u128 { let a_b_edge = if let Some(a_b_edge) = self.get_edge(&a, &b) { - a_b_edge + a_b_edge.capacity } else { return 0; }; let b_a_edge = if let Some(b_a_edge) = self.get_edge(&b, &a) { - b_a_edge + b_a_edge.capacity } else { return 0; }; @@ -130,18 +137,32 @@ where .min() } + /// Calculate the total rate of sending credits along a given route + fn get_route_rate(&self, route: &[N]) -> Option { + let mut total_rate = T::zero(); + for i in 0..route.len().checked_sub(1)? { + let edge = self.get_edge(&route[i], &route[i + 1])?; + total_rate = total_rate.checked_add(&edge.rate)?; + } + Some(total_rate) + } + /// Get a route with capacity at least `capacity`. /// Returns the route together with the capacity it is possible to send through the route. /// /// opt_exclude is an optional edge to exclude (The returned route must not go through this /// edge). This can be useful for finding non trivial loops. - fn get_route( + fn get_multi_route( &self, a: &N, b: &N, capacity: u128, opt_exclude: Option<(&N, &N)>, - ) -> Option<(Vec, u128)> { + ) -> Option> { + // TODO: Update this implementation: + // Currently get_route does not attemp to find the cheapest route (according to rate) + // It only finds the shortest route and then calculates the rate. + let (opt_e_start, opt_e_end) = match opt_exclude { Some((e_start, e_end)) => (Some(e_start), Some(e_end)), None => (None, None), @@ -155,23 +176,41 @@ where // We assert that we will always have valid capacity here: let capacity = self.get_route_capacity(&route).unwrap(); - Some((route, capacity)) + let rate = self.get_route_rate(&route)?; + + let graph_route = GraphRoute { + route, + capacity, + rate, + }; + + Some(GraphMultiRoute { + routes: vec![graph_route], + }) } } -impl CapacityGraph for SimpleCapacityGraph +impl CapacityGraph for SimpleCapacityGraph where N: cmp::Eq + hash::Hash + Clone + std::fmt::Debug, + T: LinearRate + Clone, { type Node = N; type Capacity = u128; + type Rate = T; /// Add or update edge - fn update_edge(&mut self, a: N, b: N, edge: CapacityEdge) -> Option> { + fn update_edge( + &mut self, + a: N, + b: N, + rate: T, + capacity_edge: CapacityEdge, + ) -> Option> { let a_entry = self.nodes.entry(a).or_insert_with(NodeEdges::new); a_entry .edges - .insert(b, Edge::new(edge)) + .insert(b, Edge::new(capacity_edge, rate)) .map(|edge| edge.capacity) } @@ -200,14 +239,14 @@ where self.nodes.remove(a).is_some() } - fn get_routes( + fn get_multi_routes( &self, a: &N, b: &N, capacity: u128, opt_exclude: Option<(&N, &N)>, - ) -> Vec<(Vec, u128)> { - option_to_vec(self.get_route(a, b, capacity, opt_exclude)) + ) -> Vec> { + option_to_vec(self.get_multi_route(a, b, capacity, opt_exclude)) } fn tick(&mut self, a: &N) { @@ -221,11 +260,31 @@ where mod tests { use super::*; + /// A contant rate, used for testing + #[derive(Debug, Clone)] + struct ConstRate(pub u32); + + impl LinearRate for ConstRate { + type K = u32; + + fn zero() -> Self { + ConstRate(0) + } + + fn calc_fee(self, k: Self::K) -> Self::K { + self.0 + } + + fn checked_add(self, other: &Self) -> Option { + Some(ConstRate(self.0.checked_add(other.0)?)) + } + } + #[test] fn test_get_send_capacity_basic() { - let mut cg = SimpleCapacityGraph::::new(); - cg.update_edge(0, 1, (10, 20)); - cg.update_edge(1, 0, (15, 5)); + let mut cg = SimpleCapacityGraph::::new(); + cg.update_edge(0, 1, ConstRate(1), (10, 20)); + cg.update_edge(1, 0, ConstRate(1), (15, 5)); assert_eq!(cg.get_send_capacity(&0, &1), cmp::min(5, 10)); assert_eq!(cg.get_send_capacity(&1, &0), cmp::min(15, 20)); @@ -233,8 +292,8 @@ mod tests { #[test] fn test_get_send_capacity_one_sided() { - let mut cg = SimpleCapacityGraph::::new(); - cg.update_edge(0, 1, (10, 20)); + let mut cg = SimpleCapacityGraph::::new(); + cg.update_edge(0, 1, ConstRate(1), (10, 20)); assert_eq!(cg.get_send_capacity(&0, &1), 0); assert_eq!(cg.get_send_capacity(&1, &0), 0); @@ -242,21 +301,21 @@ mod tests { #[test] fn test_add_remove_edge() { - let mut cg = SimpleCapacityGraph::::new(); + let mut cg = SimpleCapacityGraph::::new(); assert_eq!(cg.remove_edge(&0, &1), None); - cg.update_edge(0, 1, (10, 20)); + cg.update_edge(0, 1, ConstRate(1), (10, 20)); assert_eq!(cg.nodes.len(), 1); assert_eq!(cg.remove_edge(&0, &1), Some((10, 20))); assert_eq!(cg.nodes.len(), 0); - cg.update_edge(0, 1, (10, 20)); + cg.update_edge(0, 1, ConstRate(1), (10, 20)); assert_eq!(cg.nodes.len(), 1); cg.remove_node(&1); assert_eq!(cg.nodes.len(), 1); } - fn example_capacity_graph() -> SimpleCapacityGraph { + fn example_capacity_graph() -> SimpleCapacityGraph { /* * Example graph: * @@ -267,96 +326,116 @@ mod tests { * */ - let mut cg = SimpleCapacityGraph::::new(); + let mut cg = SimpleCapacityGraph::::new(); - cg.update_edge(0, 1, (30, 10)); - cg.update_edge(1, 0, (10, 30)); + cg.update_edge(0, 1, ConstRate(1), (30, 10)); + cg.update_edge(1, 0, ConstRate(1), (10, 30)); - cg.update_edge(1, 2, (10, 10)); - cg.update_edge(2, 1, (10, 10)); + cg.update_edge(1, 2, ConstRate(1), (10, 10)); + cg.update_edge(2, 1, ConstRate(1), (10, 10)); - cg.update_edge(2, 5, (30, 5)); - cg.update_edge(5, 2, (5, 30)); + cg.update_edge(2, 5, ConstRate(1), (30, 5)); + cg.update_edge(5, 2, ConstRate(1), (5, 30)); - cg.update_edge(1, 3, (30, 8)); - cg.update_edge(3, 1, (8, 30)); + cg.update_edge(1, 3, ConstRate(1), (30, 8)); + cg.update_edge(3, 1, ConstRate(1), (8, 30)); - cg.update_edge(3, 4, (30, 6)); - cg.update_edge(4, 3, (6, 30)); + cg.update_edge(3, 4, ConstRate(1), (30, 6)); + cg.update_edge(4, 3, ConstRate(1), (6, 30)); - cg.update_edge(4, 2, (30, 18)); - cg.update_edge(2, 4, (18, 30)); + cg.update_edge(4, 2, ConstRate(1), (30, 18)); + cg.update_edge(2, 4, ConstRate(1), (18, 30)); cg } #[test] - fn test_get_route() { + fn test_get_multi_route() { let cg = example_capacity_graph(); - assert_eq!(cg.get_route(&2, &5, 29, None), Some((vec![2, 5], 30))); - assert_eq!(cg.get_route(&2, &5, 30, None), Some((vec![2, 5], 30))); - assert_eq!(cg.get_route(&2, &5, 31, None), None); - - assert_eq!( - cg.get_route(&0, &5, 25, None), - Some((vec![0, 1, 3, 4, 2, 5], 30)) - ); - assert_eq!( - cg.get_route(&0, &5, 29, None), - Some((vec![0, 1, 3, 4, 2, 5], 30)) - ); - assert_eq!( - cg.get_route(&0, &5, 30, None), - Some((vec![0, 1, 3, 4, 2, 5], 30)) - ); - assert_eq!(cg.get_route(&0, &5, 31, None), None); + let multi_route = cg.get_multi_route(&2, &5, 29, None).unwrap(); + assert_eq!(multi_route.routes[0].route, vec![2, 5]); + assert_eq!(multi_route.routes[0].capacity, 30); + + let multi_route = cg.get_multi_route(&2, &5, 30, None).unwrap(); + assert_eq!(multi_route.routes[0].route, vec![2, 5]); + assert_eq!(multi_route.routes[0].capacity, 30); + + assert!(cg.get_multi_route(&2, &5, 31, None).is_none()); + + let multi_route = cg.get_multi_route(&0, &5, 25, None).unwrap(); + assert_eq!(multi_route.routes[0].route, vec![0, 1, 3, 4, 2, 5]); + assert_eq!(multi_route.routes[0].capacity, 30); + + let multi_route = cg.get_multi_route(&0, &5, 29, None).unwrap(); + assert_eq!(multi_route.routes[0].route, vec![0, 1, 3, 4, 2, 5]); + assert_eq!(multi_route.routes[0].capacity, 30); + + let multi_route = cg.get_multi_route(&0, &5, 30, None).unwrap(); + assert_eq!(multi_route.routes[0].route, vec![0, 1, 3, 4, 2, 5]); + assert_eq!(multi_route.routes[0].capacity, 30); + + assert!(cg.get_multi_route(&0, &5, 31, None).is_none()); // Block an essential edge: - assert_eq!(cg.get_route(&0, &5, 25, Some((&3, &4))), None); + assert!(cg.get_multi_route(&0, &5, 25, Some((&3, &4))).is_none()); + // Block an essential edge but the at the reversed direction: - assert_eq!( - cg.get_route(&0, &5, 25, Some((&4, &3))), - Some((vec![0, 1, 3, 4, 2, 5], 30)) - ); + let multi_route = cg.get_multi_route(&0, &5, 25, Some((&4, &3))).unwrap(); + assert_eq!(multi_route.routes[0].route, vec![0, 1, 3, 4, 2, 5]); + assert_eq!(multi_route.routes[0].capacity, 30); + // Block an edge not used for the route: - assert_eq!( - cg.get_route(&0, &5, 25, Some((&1, &2))), - Some((vec![0, 1, 3, 4, 2, 5], 30)) - ); + let multi_route = cg.get_multi_route(&0, &5, 25, Some((&1, &2))).unwrap(); + assert_eq!(multi_route.routes[0].route, vec![0, 1, 3, 4, 2, 5]); + assert_eq!(multi_route.routes[0].capacity, 30); // Use excluded edge to find a loop from 1 to 1: - assert_eq!( - cg.get_route(&2, &1, 6, Some((&2, &1))), - Some((vec![2, 4, 3, 1], 6)) - ); - // Require too much capacity: - assert_eq!(cg.get_route(&2, &1, 7, Some((&2, &1))), None); + let multi_route = cg.get_multi_route(&2, &1, 6, Some((&2, &1))).unwrap(); + assert_eq!(multi_route.routes[0].route, vec![2, 4, 3, 1]); + assert_eq!(multi_route.routes[0].capacity, 6); + + // Request for too much capacity: + assert!(cg.get_multi_route(&2, &1, 7, Some((&2, &1))).is_none()); } #[test] fn test_simple_capacity_graph_tick() { - let mut cg = SimpleCapacityGraph::::new(); + let mut cg = SimpleCapacityGraph::::new(); + + cg.update_edge(0, 1, ConstRate(1), (30, 10)); + cg.update_edge(1, 0, ConstRate(1), (10, 30)); - cg.update_edge(0, 1, (30, 10)); - cg.update_edge(1, 0, (10, 30)); + cg.update_edge(2, 3, ConstRate(1), (30, 10)); + cg.update_edge(3, 2, ConstRate(1), (10, 30)); - cg.update_edge(2, 3, (30, 10)); - cg.update_edge(3, 2, (10, 30)); + let multi_route = cg.get_multi_route(&0, &1, 30, None).unwrap(); + assert_eq!(multi_route.routes[0].route, vec![0, 1]); + assert_eq!(multi_route.routes[0].capacity, 30); - assert_eq!(cg.get_route(&0, &1, 30, None), Some((vec![0, 1], 30))); - assert_eq!(cg.get_route(&2, &3, 30, None), Some((vec![2, 3], 30))); + let multi_route = cg.get_multi_route(&2, &3, 30, None).unwrap(); + assert_eq!(multi_route.routes[0].route, vec![2, 3]); + assert_eq!(multi_route.routes[0].capacity, 30); let max_edge_age = max_edge_age(1); for _ in 0..max_edge_age - 1 { cg.tick(&0); - assert_eq!(cg.get_route(&0, &1, 30, None), Some((vec![0, 1], 30))); - assert_eq!(cg.get_route(&2, &3, 30, None), Some((vec![2, 3], 30))); + + let multi_route = cg.get_multi_route(&0, &1, 30, None).unwrap(); + assert_eq!(multi_route.routes[0].route, vec![0, 1]); + assert_eq!(multi_route.routes[0].capacity, 30); + + let multi_route = cg.get_multi_route(&2, &3, 30, None).unwrap(); + assert_eq!(multi_route.routes[0].route, vec![2, 3]); + assert_eq!(multi_route.routes[0].capacity, 30); } // At this point 0->1 and 1->0 should expire, but 2->3 and 3->2 don't expire: cg.tick(&0); - assert_eq!(cg.get_route(&0, &1, 30, None), None); - assert_eq!(cg.get_route(&2, &3, 30, None), Some((vec![2, 3], 30))); + assert!(cg.get_multi_route(&0, &1, 30, None).is_none()); + + let multi_route = cg.get_multi_route(&2, &3, 30, None).unwrap(); + assert_eq!(multi_route.routes[0].route, vec![2, 3]); + assert_eq!(multi_route.routes[0].capacity, 30); } } diff --git a/components/index_server/src/lib.rs b/components/index_server/src/lib.rs index 739a97008..b70d80ff9 100644 --- a/components/index_server/src/lib.rs +++ b/components/index_server/src/lib.rs @@ -13,6 +13,7 @@ clippy::new_without_default )] +#[allow(unused)] #[macro_use] extern crate log; From 425fe2a9a5e632bad8ec3809894e6081d09ea43d Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 15:03:53 +0300 Subject: [PATCH 187/248] offst-index-server: Refactored to use new style CapacityEdge. --- .../index_server/src/graph/capacity_graph.rs | 30 ++++--- .../index_server/src/graph/graph_service.rs | 38 ++++---- .../src/graph/simple_capacity_graph.rs | 88 ++++++++++--------- 3 files changed, 86 insertions(+), 70 deletions(-) diff --git a/components/index_server/src/graph/capacity_graph.rs b/components/index_server/src/graph/capacity_graph.rs index 91688c221..43b12f062 100644 --- a/components/index_server/src/graph/capacity_graph.rs +++ b/components/index_server/src/graph/capacity_graph.rs @@ -1,7 +1,6 @@ use std::ops::Add; -pub type CapacityEdge = (C, C); -pub type CapacityRoute = (Vec, C); +pub type CapacityPair = (C, C); pub trait LinearRate where @@ -20,15 +19,27 @@ where } #[derive(Debug, Clone, PartialEq, Eq)] -pub struct GraphRoute { +pub struct CapacityEdge { + pub capacity: CapacityPair, + pub rate: T, +} + +impl CapacityEdge { + pub fn new(capacity: CapacityPair, rate: T) -> Self { + Self { capacity, rate } + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct CapacityRoute { pub route: Vec, pub capacity: C, pub rate: T, } #[derive(Debug, Eq, PartialEq)] -pub struct GraphMultiRoute { - pub routes: Vec>, +pub struct CapacityMultiRoute { + pub routes: Vec>, } pub trait CapacityGraph { @@ -41,16 +52,15 @@ pub trait CapacityGraph { &mut self, a: Self::Node, b: Self::Node, - rate: Self::Rate, - edge: CapacityEdge, - ) -> Option>; + capacity_edge: CapacityEdge, + ) -> Option>; /// Remove an edge from the graph fn remove_edge( &mut self, a: &Self::Node, b: &Self::Node, - ) -> Option>; + ) -> Option>; /// Remove a node and all related edges known from him. /// Note: This method will not remove an edge from another node b pointing to a. @@ -70,7 +80,7 @@ pub trait CapacityGraph { b: &Self::Node, capacity: Self::Capacity, opt_exclude: Option<(&Self::Node, &Self::Node)>, - ) -> Vec>; + ) -> Vec>; /// Simulate advancement of time. Used to remove old edges. fn tick(&mut self, a: &Self::Node); diff --git a/components/index_server/src/graph/graph_service.rs b/components/index_server/src/graph/graph_service.rs index f1461cd65..9fd5fbd9b 100644 --- a/components/index_server/src/graph/graph_service.rs +++ b/components/index_server/src/graph/graph_service.rs @@ -4,11 +4,12 @@ use futures::{FutureExt, SinkExt, StreamExt, TryFutureExt}; use super::capacity_graph::{CapacityEdge, CapacityGraph, CapacityRoute}; -pub enum GraphRequest { +pub enum GraphRequest { /// Change capacities on a directed edge: UpdateEdge( N, N, + T, CapacityEdge, oneshot::Sender>>, ), @@ -38,13 +39,13 @@ pub enum GraphServiceError { /// Process one GraphRequest, and send the response through the provided sender. /// This function might perform a long computation and take a long time to complete. -fn process_request(capacity_graph: &mut CG, graph_request: GraphRequest) +fn process_request(capacity_graph: &mut CG, graph_request: GraphRequest) where - CG: CapacityGraph, + CG: CapacityGraph, { match graph_request { - GraphRequest::UpdateEdge(a, b, capacity_edge, sender) => { - let _ = sender.send(capacity_graph.update_edge(a, b, capacity_edge)); + GraphRequest::UpdateEdge(a, b, rate, capacity_edge, sender) => { + let _ = sender.send(capacity_graph.update_edge(a, b, rate, capacity_edge)); } GraphRequest::RemoveEdge(a, b, sender) => { let _ = sender.send(capacity_graph.remove_edge(&a, &b)); @@ -54,8 +55,8 @@ where } GraphRequest::GetRoutes(a, b, capacity, opt_exclude, sender) => { let routes = match opt_exclude { - Some((c, d)) => capacity_graph.get_routes(&a, &b, capacity, Some((&c, &d))), - None => capacity_graph.get_routes(&a, &b, capacity, None), + Some((c, d)) => capacity_graph.get_multi_routes(&a, &b, capacity, Some((&c, &d))), + None => capacity_graph.get_multi_routes(&a, &b, capacity, None), }; let _ = sender.send(routes); } @@ -66,15 +67,16 @@ where } } -async fn graph_service_loop( +async fn graph_service_loop( mut capacity_graph: CG, - mut incoming_requests: mpsc::Receiver>, + mut incoming_requests: mpsc::Receiver>, mut graph_service_spawner: GS, ) -> Result<(), GraphServiceError> where N: Send + 'static, C: Send + 'static, - CG: CapacityGraph + Send + 'static, + T: Send + 'static, + CG: CapacityGraph + Send + 'static, GS: Spawn, { // We use a separate spawner to be used for long graph computations. @@ -114,12 +116,12 @@ impl From for GraphClientError { } #[derive(Clone)] -pub struct GraphClient { - requests_sender: mpsc::Sender>, +pub struct GraphClient { + requests_sender: mpsc::Sender>, } -impl GraphClient { - pub fn new(requests_sender: mpsc::Sender>) -> Self { +impl GraphClient { + pub fn new(requests_sender: mpsc::Sender>) -> Self { GraphClient { requests_sender } } @@ -128,12 +130,13 @@ impl GraphClient { &mut self, a: N, b: N, + rate: T, edge: CapacityEdge, ) -> Result>, GraphClientError> { let (sender, receiver) = oneshot::channel::>>(); await!(self .requests_sender - .send(GraphRequest::UpdateEdge(a, b, edge, sender)))?; + .send(GraphRequest::UpdateEdge(a, b, rate, edge, sender)))?; Ok(await!(receiver)?) } @@ -194,14 +197,15 @@ impl GraphClient { /// Spawn a graph service, returning a GraphClient on success. /// GraphClient can be cloned to allow multiple clients. -pub fn create_graph_service( +pub fn create_graph_service( capacity_graph: CG, graph_service_spawner: GS, mut spawner: S, -) -> Result, SpawnError> +) -> Result, SpawnError> where N: Send + 'static, C: Send + 'static, + T: Send + 'static, CG: CapacityGraph + Send + 'static, GS: Spawn + Send + 'static, S: Spawn, diff --git a/components/index_server/src/graph/simple_capacity_graph.rs b/components/index_server/src/graph/simple_capacity_graph.rs index f7c54f52e..135e75916 100644 --- a/components/index_server/src/graph/simple_capacity_graph.rs +++ b/components/index_server/src/graph/simple_capacity_graph.rs @@ -2,7 +2,9 @@ use std::collections::HashMap; use std::{cmp, hash}; use super::bfs::bfs; -use super::capacity_graph::{CapacityEdge, CapacityGraph, GraphMultiRoute, GraphRoute, LinearRate}; +use super::capacity_graph::{ + CapacityEdge, CapacityGraph, CapacityMultiRoute, CapacityPair, CapacityRoute, LinearRate, +}; use super::utils::{option_to_vec, OptionIterator}; /// Amount of ticks an edge could live regardless of coupon collector's approximation. @@ -11,16 +13,14 @@ const BASE_MAX_EDGE_AGE: u128 = 16; #[derive(Debug, Clone)] struct Edge { - capacity: CapacityEdge, - rate: T, + capacity_edge: CapacityEdge, age: u128, } impl Edge { - fn new(capacity: CapacityEdge, rate: T) -> Self { + fn new(capacity_edge: CapacityEdge) -> Self { Edge { - capacity, - rate, + capacity_edge, age: 0, } } @@ -96,13 +96,13 @@ where /// capacity reported by `b`. fn get_send_capacity(&self, a: &N, b: &N) -> u128 { let a_b_edge = if let Some(a_b_edge) = self.get_edge(&a, &b) { - a_b_edge.capacity + a_b_edge.capacity_edge.capacity } else { return 0; }; let b_a_edge = if let Some(b_a_edge) = self.get_edge(&b, &a) { - b_a_edge.capacity + b_a_edge.capacity_edge.capacity } else { return 0; }; @@ -142,7 +142,7 @@ where let mut total_rate = T::zero(); for i in 0..route.len().checked_sub(1)? { let edge = self.get_edge(&route[i], &route[i + 1])?; - total_rate = total_rate.checked_add(&edge.rate)?; + total_rate = total_rate.checked_add(&edge.capacity_edge.rate)?; } Some(total_rate) } @@ -158,7 +158,7 @@ where b: &N, capacity: u128, opt_exclude: Option<(&N, &N)>, - ) -> Option> { + ) -> Option> { // TODO: Update this implementation: // Currently get_route does not attemp to find the cheapest route (according to rate) // It only finds the shortest route and then calculates the rate. @@ -178,13 +178,13 @@ where let rate = self.get_route_rate(&route)?; - let graph_route = GraphRoute { + let graph_route = CapacityRoute { route, capacity, rate, }; - Some(GraphMultiRoute { + Some(CapacityMultiRoute { routes: vec![graph_route], }) } @@ -204,18 +204,17 @@ where &mut self, a: N, b: N, - rate: T, - capacity_edge: CapacityEdge, - ) -> Option> { + capacity_edge: CapacityEdge, + ) -> Option> { let a_entry = self.nodes.entry(a).or_insert_with(NodeEdges::new); a_entry .edges - .insert(b, Edge::new(capacity_edge, rate)) - .map(|edge| edge.capacity) + .insert(b, Edge::new(capacity_edge)) + .map(|edge| edge.capacity_edge) } /// Remove an edge from the graph - fn remove_edge(&mut self, a: &N, b: &N) -> Option> { + fn remove_edge(&mut self, a: &N, b: &N) -> Option> { let a_edges = match self.nodes.get_mut(a) { Some(a_edges) => a_edges, None => return None, @@ -230,7 +229,7 @@ where self.nodes.remove(a); } - Some(old_edge.capacity) + Some(old_edge.capacity_edge) } /// Remove a node and all related edges known from him. @@ -245,7 +244,7 @@ where b: &N, capacity: u128, opt_exclude: Option<(&N, &N)>, - ) -> Vec> { + ) -> Vec> { option_to_vec(self.get_multi_route(a, b, capacity, opt_exclude)) } @@ -261,7 +260,7 @@ mod tests { use super::*; /// A contant rate, used for testing - #[derive(Debug, Clone)] + #[derive(Debug, Clone, PartialEq, Eq)] struct ConstRate(pub u32); impl LinearRate for ConstRate { @@ -283,8 +282,8 @@ mod tests { #[test] fn test_get_send_capacity_basic() { let mut cg = SimpleCapacityGraph::::new(); - cg.update_edge(0, 1, ConstRate(1), (10, 20)); - cg.update_edge(1, 0, ConstRate(1), (15, 5)); + cg.update_edge(0, 1, CapacityEdge::new((10, 20), ConstRate(1))); + cg.update_edge(1, 0, CapacityEdge::new((15, 5), ConstRate(1))); assert_eq!(cg.get_send_capacity(&0, &1), cmp::min(5, 10)); assert_eq!(cg.get_send_capacity(&1, &0), cmp::min(15, 20)); @@ -293,7 +292,7 @@ mod tests { #[test] fn test_get_send_capacity_one_sided() { let mut cg = SimpleCapacityGraph::::new(); - cg.update_edge(0, 1, ConstRate(1), (10, 20)); + cg.update_edge(0, 1, CapacityEdge::new((10, 20), ConstRate(1))); assert_eq!(cg.get_send_capacity(&0, &1), 0); assert_eq!(cg.get_send_capacity(&1, &0), 0); @@ -303,13 +302,16 @@ mod tests { fn test_add_remove_edge() { let mut cg = SimpleCapacityGraph::::new(); assert_eq!(cg.remove_edge(&0, &1), None); - cg.update_edge(0, 1, ConstRate(1), (10, 20)); + cg.update_edge(0, 1, CapacityEdge::new((10, 20), ConstRate(1))); assert_eq!(cg.nodes.len(), 1); - assert_eq!(cg.remove_edge(&0, &1), Some((10, 20))); + assert_eq!( + cg.remove_edge(&0, &1), + Some(CapacityEdge::new((10, 20), ConstRate(1))) + ); assert_eq!(cg.nodes.len(), 0); - cg.update_edge(0, 1, ConstRate(1), (10, 20)); + cg.update_edge(0, 1, CapacityEdge::new((10, 20), ConstRate(1))); assert_eq!(cg.nodes.len(), 1); cg.remove_node(&1); assert_eq!(cg.nodes.len(), 1); @@ -328,23 +330,23 @@ mod tests { let mut cg = SimpleCapacityGraph::::new(); - cg.update_edge(0, 1, ConstRate(1), (30, 10)); - cg.update_edge(1, 0, ConstRate(1), (10, 30)); + cg.update_edge(0, 1, CapacityEdge::new((30, 10), ConstRate(1))); + cg.update_edge(1, 0, CapacityEdge::new((10, 30), ConstRate(1))); - cg.update_edge(1, 2, ConstRate(1), (10, 10)); - cg.update_edge(2, 1, ConstRate(1), (10, 10)); + cg.update_edge(1, 2, CapacityEdge::new((10, 10), ConstRate(1))); + cg.update_edge(2, 1, CapacityEdge::new((10, 10), ConstRate(1))); - cg.update_edge(2, 5, ConstRate(1), (30, 5)); - cg.update_edge(5, 2, ConstRate(1), (5, 30)); + cg.update_edge(2, 5, CapacityEdge::new((30, 5), ConstRate(1))); + cg.update_edge(5, 2, CapacityEdge::new((5, 30), ConstRate(1))); - cg.update_edge(1, 3, ConstRate(1), (30, 8)); - cg.update_edge(3, 1, ConstRate(1), (8, 30)); + cg.update_edge(1, 3, CapacityEdge::new((30, 8), ConstRate(1))); + cg.update_edge(3, 1, CapacityEdge::new((8, 30), ConstRate(1))); - cg.update_edge(3, 4, ConstRate(1), (30, 6)); - cg.update_edge(4, 3, ConstRate(1), (6, 30)); + cg.update_edge(3, 4, CapacityEdge::new((30, 6), ConstRate(1))); + cg.update_edge(4, 3, CapacityEdge::new((6, 30), ConstRate(1))); - cg.update_edge(4, 2, ConstRate(1), (30, 18)); - cg.update_edge(2, 4, ConstRate(1), (18, 30)); + cg.update_edge(4, 2, CapacityEdge::new((30, 18), ConstRate(1))); + cg.update_edge(2, 4, CapacityEdge::new((18, 30), ConstRate(1))); cg } @@ -403,11 +405,11 @@ mod tests { fn test_simple_capacity_graph_tick() { let mut cg = SimpleCapacityGraph::::new(); - cg.update_edge(0, 1, ConstRate(1), (30, 10)); - cg.update_edge(1, 0, ConstRate(1), (10, 30)); + cg.update_edge(0, 1, CapacityEdge::new((30, 30), ConstRate(1))); + cg.update_edge(1, 0, CapacityEdge::new((10, 30), ConstRate(1))); - cg.update_edge(2, 3, ConstRate(1), (30, 10)); - cg.update_edge(3, 2, ConstRate(1), (10, 30)); + cg.update_edge(2, 3, CapacityEdge::new((30, 10), ConstRate(1))); + cg.update_edge(3, 2, CapacityEdge::new((10, 30), ConstRate(1))); let multi_route = cg.get_multi_route(&0, &1, 30, None).unwrap(); assert_eq!(multi_route.routes[0].route, vec![0, 1]); From e57e204b05b2d574d2312ce9f688d59807598a5a Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 17:21:17 +0300 Subject: [PATCH 188/248] offst-index-server: Restored graph_service.rs --- .../index_server/src/graph/graph_service.rs | 74 +++++++++++-------- components/index_server/src/graph/mod.rs | 5 +- .../src/graph/simple_capacity_graph.rs | 20 +---- .../index_server/src/graph/test_utils.rs | 22 ++++++ 4 files changed, 72 insertions(+), 49 deletions(-) create mode 100644 components/index_server/src/graph/test_utils.rs diff --git a/components/index_server/src/graph/graph_service.rs b/components/index_server/src/graph/graph_service.rs index 9fd5fbd9b..324df932d 100644 --- a/components/index_server/src/graph/graph_service.rs +++ b/components/index_server/src/graph/graph_service.rs @@ -2,30 +2,29 @@ use futures::channel::{mpsc, oneshot}; use futures::task::{Spawn, SpawnError, SpawnExt}; use futures::{FutureExt, SinkExt, StreamExt, TryFutureExt}; -use super::capacity_graph::{CapacityEdge, CapacityGraph, CapacityRoute}; +use super::capacity_graph::{CapacityEdge, CapacityGraph, CapacityRoute, CapacityMultiRoute}; pub enum GraphRequest { /// Change capacities on a directed edge: UpdateEdge( N, N, - T, - CapacityEdge, - oneshot::Sender>>, + CapacityEdge, + oneshot::Sender>>, ), /// Remove a directed edge: - RemoveEdge(N, N, oneshot::Sender>>), + RemoveEdge(N, N, oneshot::Sender>>), /// Remove a node and all edges starting from this node. /// Note: This will not remove edges going to this node. RemoveNode(N, oneshot::Sender), /// Get some routes from one node to another of at least certain capacity. /// If an exclude directed edge is provided, the routes must not contain this directed edge. - GetRoutes( + GetMultiRoutes( N, N, C, Option<(N, N)>, - oneshot::Sender>>, + oneshot::Sender>>, ), // (from, to, capacity, opt_exclude) /// Expire old outgoing edges for the specified node Tick(N, oneshot::Sender<()>), @@ -44,8 +43,8 @@ where CG: CapacityGraph, { match graph_request { - GraphRequest::UpdateEdge(a, b, rate, capacity_edge, sender) => { - let _ = sender.send(capacity_graph.update_edge(a, b, rate, capacity_edge)); + GraphRequest::UpdateEdge(a, b, capacity_edge, sender) => { + let _ = sender.send(capacity_graph.update_edge(a, b, capacity_edge)); } GraphRequest::RemoveEdge(a, b, sender) => { let _ = sender.send(capacity_graph.remove_edge(&a, &b)); @@ -53,7 +52,7 @@ where GraphRequest::RemoveNode(a, sender) => { let _ = sender.send(capacity_graph.remove_node(&a)); } - GraphRequest::GetRoutes(a, b, capacity, opt_exclude, sender) => { + GraphRequest::GetMultiRoutes(a, b, capacity, opt_exclude, sender) => { let routes = match opt_exclude { Some((c, d)) => capacity_graph.get_multi_routes(&a, &b, capacity, Some((&c, &d))), None => capacity_graph.get_multi_routes(&a, &b, capacity, None), @@ -130,13 +129,12 @@ impl GraphClient { &mut self, a: N, b: N, - rate: T, - edge: CapacityEdge, - ) -> Result>, GraphClientError> { - let (sender, receiver) = oneshot::channel::>>(); + capacity_edge: CapacityEdge, + ) -> Result>, GraphClientError> { + let (sender, receiver) = oneshot::channel::>>(); await!(self .requests_sender - .send(GraphRequest::UpdateEdge(a, b, rate, edge, sender)))?; + .send(GraphRequest::UpdateEdge(a, b, capacity_edge, sender)))?; Ok(await!(receiver)?) } @@ -145,7 +143,7 @@ impl GraphClient { &mut self, a: N, b: N, - ) -> Result>, GraphClientError> { + ) -> Result>, GraphClientError> { let (sender, receiver) = oneshot::channel(); await!(self .requests_sender @@ -169,15 +167,15 @@ impl GraphClient { /// /// opt_exclude is an optional edge to exclude (The returned route must not go through this /// edge). This can be useful for finding non trivial loops. - pub async fn get_routes( + pub async fn get_multi_routes( &mut self, a: N, b: N, capacity: C, opt_exclude: Option<(N, N)>, - ) -> Result>, GraphClientError> { + ) -> Result>, GraphClientError> { let (sender, receiver) = oneshot::channel(); - await!(self.requests_sender.send(GraphRequest::GetRoutes( + await!(self.requests_sender.send(GraphRequest::GetMultiRoutes( a, b, capacity, @@ -206,7 +204,7 @@ where N: Send + 'static, C: Send + 'static, T: Send + 'static, - CG: CapacityGraph + Send + 'static, + CG: CapacityGraph + Send + 'static, GS: Spawn + Send + 'static, S: Spawn, { @@ -223,10 +221,12 @@ where #[cfg(test)] mod tests { - use super::super::simple_capacity_graph::SimpleCapacityGraph; use super::*; + use super::super::simple_capacity_graph::SimpleCapacityGraph; + use super::super::test_utils::ConstRate; use futures::executor::ThreadPool; + async fn task_create_graph_service_basic(spawner: S) where S: Spawn, @@ -236,19 +236,35 @@ mod tests { let mut graph_client = create_graph_service(capacity_graph, graph_service_spawner, spawner).unwrap(); - await!(graph_client.update_edge(2u32, 5u32, (30, 5))).unwrap(); - await!(graph_client.update_edge(5, 2, (5, 30))).unwrap(); + await!(graph_client.update_edge(2u32, 5u32, CapacityEdge::new((30, 5), ConstRate(1)))).unwrap(); + await!(graph_client.update_edge(5, 2, CapacityEdge::new((5, 30), ConstRate(1)))).unwrap(); assert_eq!( - await!(graph_client.get_routes(2, 5, 29, None)).unwrap(), - vec![(vec![2, 5], 30)] + await!(graph_client.get_multi_routes(2, 5, 29, None)).unwrap(), + vec![CapacityMultiRoute { + routes: vec![ + CapacityRoute { + route: vec![2,5], + capacity: 30, + rate: ConstRate(1), + } + ], + }] ); assert_eq!( - await!(graph_client.get_routes(2, 5, 30, None)).unwrap(), - vec![(vec![2, 5], 30)] + await!(graph_client.get_multi_routes(2, 5, 30, None)).unwrap(), + vec![CapacityMultiRoute { + routes: vec![ + CapacityRoute { + route: vec![2,5], + capacity: 30, + rate: ConstRate(1), + } + ], + }] ); assert_eq!( - await!(graph_client.get_routes(2, 5, 31, None)).unwrap(), + await!(graph_client.get_multi_routes(2, 5, 31, None)).unwrap(), vec![] ); @@ -256,7 +272,7 @@ mod tests { assert_eq!( await!(graph_client.remove_edge(2, 5)).unwrap(), - Some((30, 5)) + Some(CapacityEdge::new((30, 5), ConstRate(1))) ); assert_eq!(await!(graph_client.remove_node(2)).unwrap(), false); assert_eq!(await!(graph_client.remove_node(5)).unwrap(), true); diff --git a/components/index_server/src/graph/mod.rs b/components/index_server/src/graph/mod.rs index 5384d94fa..053df1509 100644 --- a/components/index_server/src/graph/mod.rs +++ b/components/index_server/src/graph/mod.rs @@ -1,5 +1,8 @@ mod bfs; mod capacity_graph; -// pub mod graph_service; +pub mod graph_service; pub mod simple_capacity_graph; mod utils; + +#[cfg(test)] +mod test_utils; diff --git a/components/index_server/src/graph/simple_capacity_graph.rs b/components/index_server/src/graph/simple_capacity_graph.rs index 135e75916..6639e0c28 100644 --- a/components/index_server/src/graph/simple_capacity_graph.rs +++ b/components/index_server/src/graph/simple_capacity_graph.rs @@ -259,25 +259,7 @@ where mod tests { use super::*; - /// A contant rate, used for testing - #[derive(Debug, Clone, PartialEq, Eq)] - struct ConstRate(pub u32); - - impl LinearRate for ConstRate { - type K = u32; - - fn zero() -> Self { - ConstRate(0) - } - - fn calc_fee(self, k: Self::K) -> Self::K { - self.0 - } - - fn checked_add(self, other: &Self) -> Option { - Some(ConstRate(self.0.checked_add(other.0)?)) - } - } + use super::super::test_utils::ConstRate; #[test] fn test_get_send_capacity_basic() { diff --git a/components/index_server/src/graph/test_utils.rs b/components/index_server/src/graph/test_utils.rs new file mode 100644 index 000000000..a08c43945 --- /dev/null +++ b/components/index_server/src/graph/test_utils.rs @@ -0,0 +1,22 @@ +use crate::graph::capacity_graph::LinearRate; + +/// A contant rate, used for testing +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ConstRate(pub u32); + +#[cfg(test)] +impl LinearRate for ConstRate { + type K = u32; + + fn zero() -> Self { + ConstRate(0) + } + + fn calc_fee(self, k: Self::K) -> Self::K { + self.0 + } + + fn checked_add(self, other: &Self) -> Option { + Some(ConstRate(self.0.checked_add(other.0)?)) + } +} From 27ccfeda428b5013fdb675d58238cc47256f6773 Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 17:22:05 +0300 Subject: [PATCH 189/248] offst-index-server: Restored verifier module. --- components/index_server/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/index_server/src/lib.rs b/components/index_server/src/lib.rs index b70d80ff9..3b1962aef 100644 --- a/components/index_server/src/lib.rs +++ b/components/index_server/src/lib.rs @@ -25,6 +25,6 @@ mod backoff_connector; mod graph; // mod net_server; // mod server; -// mod verifier; +mod verifier; // pub use net_server::{net_index_server, NetIndexServerError}; From ea44955f53614d24b0a187631c09458b8f60885e Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 17:45:43 +0300 Subject: [PATCH 190/248] offst-index-server: Restored server module. --- Cargo.lock | 4 +- components/funder/Cargo.toml | 3 - .../funder/src/handler/handle_friend.rs | 4 +- components/funder/src/mutual_credit/types.rs | 11 +-- .../index_server/src/graph/capacity_graph.rs | 4 +- components/index_server/src/graph/mod.rs | 2 +- .../index_server/src/graph/test_utils.rs | 6 +- components/index_server/src/lib.rs | 4 +- components/index_server/src/server.rs | 67 ++++++++++++++----- components/proto/Cargo.toml | 3 + components/proto/src/funder/messages.rs | 9 +++ components/proto/src/lib.rs | 3 + 12 files changed, 77 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d731935fd..0047f1d46 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -818,8 +818,6 @@ dependencies = [ "futures-preview 0.3.0-alpha.16 (registry+https://github.com/rust-lang/crates.io-index)", "im 12.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "num-bigint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "offst-common 0.1.0", "offst-crypto 0.1.0", "offst-database 0.1.0", @@ -938,6 +936,8 @@ dependencies = [ "capnpc 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)", "derive_more 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", "im 12.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "num-bigint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "offst-common 0.1.0", "offst-crypto 0.1.0", "serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/components/funder/Cargo.toml b/components/funder/Cargo.toml index 10d6a890c..f64b4c576 100644 --- a/components/funder/Cargo.toml +++ b/components/funder/Cargo.toml @@ -23,9 +23,6 @@ bytes = "0.4" futures-cpupool = "0.1.8" futures-preview = "0.3.0-alpha.16" -num-bigint = "0.2.2" -num-traits = "0.2.6" - serde = "1" serde_derive = "1" diff --git a/components/funder/src/handler/handle_friend.rs b/components/funder/src/handler/handle_friend.rs index b2de928f7..0589077d2 100644 --- a/components/funder/src/handler/handle_friend.rs +++ b/components/funder/src/handler/handle_friend.rs @@ -28,8 +28,6 @@ use crate::state::{FunderMutation, Payment}; use crate::ephemeral::Ephemeral; -use crate::mutual_credit::types::calc_fee; - use crate::handler::canceler::{ cancel_local_pending_transactions, cancel_pending_requests, cancel_pending_user_requests, remove_transaction, reply_with_cancel, @@ -221,7 +219,7 @@ fn handle_request_send_funds( // Note that the rate is determined by the rate we set with the node that sent us the request // (And **not** with the node that we forward the request to). let rate = &m_state.state().friends.get(remote_public_key).unwrap().rate; - let opt_local_fee = calc_fee(rate, request_send_funds.dest_payment); + let opt_local_fee = rate.calc_fee(request_send_funds.dest_payment); let request_id = request_send_funds.request_id.clone(); diff --git a/components/funder/src/mutual_credit/types.rs b/components/funder/src/mutual_credit/types.rs index cb5ed32ce..888114207 100644 --- a/components/funder/src/mutual_credit/types.rs +++ b/components/funder/src/mutual_credit/types.rs @@ -1,12 +1,10 @@ use im::hashmap::HashMap as ImHashMap; -use num_bigint::BigUint; -use num_traits::cast::ToPrimitive; use common::safe_arithmetic::SafeSignedArithmetic; use crypto::identity::PublicKey; use crypto::uid::Uid; -use proto::funder::messages::{PendingTransaction, Rate, RequestsStatus, TransactionStage}; +use proto::funder::messages::{PendingTransaction, RequestsStatus, TransactionStage}; /// The maximum possible funder debt. /// We don't use the full u128 because i128 can not go beyond this value. @@ -86,13 +84,6 @@ impl McRequestsStatus { } } -/// Calculate fee for forwarding a transaction given a `rate` and `dest_payment`. -pub fn calc_fee(rate: &Rate, dest_payment: u128) -> Option { - let mul_res = (BigUint::from(dest_payment) * BigUint::from(rate.mul)) >> 32; - let res = mul_res + BigUint::from(rate.add); - res.to_u128() -} - #[derive(Clone, Serialize, Deserialize, Debug)] pub struct MutualCreditState { /// Public identities of local and remote side diff --git a/components/index_server/src/graph/capacity_graph.rs b/components/index_server/src/graph/capacity_graph.rs index 43b12f062..f8fd844d9 100644 --- a/components/index_server/src/graph/capacity_graph.rs +++ b/components/index_server/src/graph/capacity_graph.rs @@ -13,9 +13,9 @@ where fn zero() -> Self; /// Calculate the fee for forwarding a certain amount of credits being passed. /// The resulting fee is also an amount of credits. - fn calc_fee(self, k: Self::K) -> Self::K; + fn calc_fee(&self, k: Self::K) -> Option; /// Attempt to add two rates. - fn checked_add(self, other: &Self) -> Option; + fn checked_add(&self, other: &Self) -> Option; } #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/components/index_server/src/graph/mod.rs b/components/index_server/src/graph/mod.rs index 053df1509..a4607083d 100644 --- a/components/index_server/src/graph/mod.rs +++ b/components/index_server/src/graph/mod.rs @@ -1,5 +1,5 @@ mod bfs; -mod capacity_graph; +pub mod capacity_graph; pub mod graph_service; pub mod simple_capacity_graph; mod utils; diff --git a/components/index_server/src/graph/test_utils.rs b/components/index_server/src/graph/test_utils.rs index a08c43945..56a1f1c7a 100644 --- a/components/index_server/src/graph/test_utils.rs +++ b/components/index_server/src/graph/test_utils.rs @@ -12,11 +12,11 @@ impl LinearRate for ConstRate { ConstRate(0) } - fn calc_fee(self, k: Self::K) -> Self::K { - self.0 + fn calc_fee(&self, k: Self::K) -> Option { + Some(self.0) } - fn checked_add(self, other: &Self) -> Option { + fn checked_add(&self, other: &Self) -> Option { Some(ConstRate(self.0.checked_add(other.0)?)) } } diff --git a/components/index_server/src/lib.rs b/components/index_server/src/lib.rs index 3b1962aef..c649e5c24 100644 --- a/components/index_server/src/lib.rs +++ b/components/index_server/src/lib.rs @@ -17,6 +17,7 @@ #[macro_use] extern crate log; +#[macro_use] extern crate common; #[allow(unused)] @@ -24,7 +25,8 @@ mod backoff_connector; #[allow(unused)] mod graph; // mod net_server; -// mod server; +#[allow(unused)] +mod server; mod verifier; // pub use net_server::{net_index_server, NetIndexServerError}; diff --git a/components/index_server/src/server.rs b/components/index_server/src/server.rs index c93d808c3..baee282f0 100644 --- a/components/index_server/src/server.rs +++ b/components/index_server/src/server.rs @@ -14,17 +14,40 @@ use crypto::uid::Uid; use proto::index_server::messages::{ ForwardMutationsUpdate, IndexClientToServer, IndexMutation, IndexServerToClient, - IndexServerToServer, MutationsUpdate, ResponseRoutes, MultiRoute, TimeProofLink, + IndexServerToServer, MutationsUpdate, ResponseRoutes, MultiRoute, TimeProofLink, RouteCapacityRate, }; -use proto::funder::messages::FriendsRoute; +use proto::funder::messages::{FriendsRoute, Rate}; use crate::graph::graph_service::{GraphClient, GraphClientError}; +use crate::graph::capacity_graph::{LinearRate, CapacityEdge}; + use crate::verifier::Verifier; pub type ServerConn = ConnPair; pub type ClientConn = ConnPair; +impl LinearRate for Rate { + /// Type used to count credits + type K = u128; + + fn zero() -> Self { + Rate { mul: 0, add: 0 } + } + + fn calc_fee(&self, k: Self::K) -> Option { + self.calc_fee(k) + } + + fn checked_add(&self, other: &Self) -> Option { + // Entry wise addition: + Some(Rate { + mul: self.mul.checked_add(other.mul)?, + add: self.add.checked_add(other.add)?, + }) + } +} + #[derive(Debug)] pub enum ServerLoopError { SpawnError, @@ -89,7 +112,7 @@ struct RemoteServer { struct IndexServer { local_public_key: PublicKey, server_connector: SC, - graph_client: GraphClient, + graph_client: GraphClient, verifier: V, compare_public_key: CMP, remote_servers: HashMap>, @@ -143,7 +166,7 @@ where local_public_key: PublicKey, trusted_servers: HashMap, server_connector: SC, - graph_client: GraphClient, + graph_client: GraphClient, compare_public_key: CMP, verifier: V, event_sender: mpsc::Sender, @@ -301,10 +324,15 @@ where update_friend.rate, ); + let capacity_edge = CapacityEdge { + capacity: (update_friend.send_capacity, update_friend.recv_capacity), + rate: update_friend.rate.clone(), + }; + await!(self.graph_client.update_edge( mutations_update.node_public_key.clone(), update_friend.public_key.clone(), - (update_friend.send_capacity, update_friend.recv_capacity) + capacity_edge, ))?; } IndexMutation::RemoveFriend(friend_public_key) => { @@ -369,7 +397,7 @@ where } async fn client_handler( - mut graph_client: GraphClient, + mut graph_client: GraphClient, _public_key: PublicKey, // TODO: unused? client_conn: ClientConn, mut event_sender: mpsc::Sender, @@ -384,26 +412,29 @@ async fn client_handler( .map_err(|_| ServerLoopError::ClientEventSenderError)?; } IndexClientToServer::RequestRoutes(request_routes) => { - let graph_multi_routes = await!(graph_client.get_routes( + let graph_multi_routes = await!(graph_client.get_multi_routes( request_routes.source.clone(), request_routes.destination.clone(), request_routes.capacity, request_routes.opt_exclude.clone() ))?; - let routes = graph_multi_routes + let multi_routes = graph_multi_routes .into_iter() .map(|graph_multi_route| MultiRoute { - routes: graph_multi_route.routes.map(|graph_route| RouteCapacityRate { - route: FriendRoute {public_keys: graph_route.route}, - capacity: graph_route.capacity, - rate: graph_route.rate, - }), + routes: graph_multi_route.routes + .into_iter() + .map(|graph_route| RouteCapacityRate { + route: FriendsRoute {public_keys: graph_route.route}, + capacity: graph_route.capacity, + rate: graph_route.rate, + }) + .collect(), }) .collect::>(); let response_routes = ResponseRoutes { request_id: request_routes.request_id, - routes, + multi_routes, }; let message = IndexServerToClient::ResponseRoutes(response_routes); await!(sender.send(message)).map_err(|_| ServerLoopError::ClientSenderError)?; @@ -419,7 +450,7 @@ pub async fn server_loop( incoming_server_connections: IS, incoming_client_connections: IC, server_connector: SC, - graph_client: GraphClient, + graph_client: GraphClient, compare_public_key: CMP, verifier: V, timer_stream: TS, @@ -731,7 +762,7 @@ mod tests { // Handle the graph request: match await!(graph_requests_receiver.next()).unwrap() { - GraphRequest::GetRoutes(src, dest, capacity, opt_exclude, response_sender) => { + GraphRequest::GetMultiRoutes(src, dest, capacity, opt_exclude, response_sender) => { assert_eq!(src, PublicKey::from(&[8; PUBLIC_KEY_LEN])); assert_eq!(dest, PublicKey::from(&[9; PUBLIC_KEY_LEN])); assert_eq!(capacity, 100); @@ -813,7 +844,7 @@ mod tests { tick_sender: mpsc::Sender<()>, server_connections_sender: mpsc::Sender<(PublicKey, ServerConn)>, client_connections_sender: mpsc::Sender<(PublicKey, ClientConn)>, - graph_requests_receiver: mpsc::Receiver>, + graph_requests_receiver: mpsc::Receiver>, server_conn_request_receiver: mpsc::Receiver>>, debug_event_receiver: mpsc::Receiver<()>, @@ -996,7 +1027,7 @@ mod tests { // Handle the graph request: match await!(test_servers[0].graph_requests_receiver.next()).unwrap() { - GraphRequest::GetRoutes(src, dest, capacity, opt_exclude, response_sender) => { + GraphRequest::GetMultiRoutes(src, dest, capacity, opt_exclude, response_sender) => { assert_eq!(src, PublicKey::from(&[8; PUBLIC_KEY_LEN])); assert_eq!(dest, PublicKey::from(&[9; PUBLIC_KEY_LEN])); assert_eq!(capacity, 100); diff --git a/components/proto/Cargo.toml b/components/proto/Cargo.toml index 3c5afc102..1a0542893 100644 --- a/components/proto/Cargo.toml +++ b/components/proto/Cargo.toml @@ -23,6 +23,9 @@ im = {version = "12.0.0", features = ["serde"]} derive_more = "0.14.0" +num-bigint = "0.2.2" +num-traits = "0.2.6" + [dev-dependencies] tempfile = "3.0.5" diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index 854644404..e6be8316d 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -1,6 +1,9 @@ use byteorder::{BigEndian, WriteBytesExt}; use std::collections::HashSet; +use num_bigint::BigUint; +use num_traits::cast::ToPrimitive; + use crypto::crypto_rand::RandValue; use crypto::hash::{self, HashResult}; use crypto::hash_lock::{HashedLock, PlainLock}; @@ -417,6 +420,12 @@ impl Rate { pub fn new() -> Self { Rate { mul: 0, add: 0 } } + + pub fn calc_fee(&self, dest_payment: u128) -> Option { + let mul_res = (BigUint::from(dest_payment) * BigUint::from(self.mul)) >> 32; + let res = mul_res + BigUint::from(self.add); + res.to_u128() + } } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] diff --git a/components/proto/src/lib.rs b/components/proto/src/lib.rs index 20d9ffd48..5aafae96c 100644 --- a/components/proto/src/lib.rs +++ b/components/proto/src/lib.rs @@ -30,6 +30,9 @@ extern crate tempfile; #[macro_use] extern crate derive_more; +extern crate num_bigint; +extern crate num_traits; + #[macro_use] pub mod macros; pub mod app_server; From 60ecea66a5d9421faf71fed7e8d19fd358bea653 Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 17:46:43 +0300 Subject: [PATCH 191/248] offst-index-server: Restored net_server module. --- components/index_server/src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/components/index_server/src/lib.rs b/components/index_server/src/lib.rs index c649e5c24..a73379196 100644 --- a/components/index_server/src/lib.rs +++ b/components/index_server/src/lib.rs @@ -13,7 +13,6 @@ clippy::new_without_default )] -#[allow(unused)] #[macro_use] extern crate log; @@ -24,9 +23,9 @@ extern crate common; mod backoff_connector; #[allow(unused)] mod graph; -// mod net_server; +mod net_server; #[allow(unused)] mod server; mod verifier; -// pub use net_server::{net_index_server, NetIndexServerError}; +pub use net_server::{net_index_server, NetIndexServerError}; From da985eb9b65ac01ef83b285cdb7f8fa8f51a4fdc Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 17:49:52 +0300 Subject: [PATCH 192/248] Removed unused warnings. --- components/index_server/src/graph/capacity_graph.rs | 3 +-- components/index_server/src/graph/graph_service.rs | 3 ++- components/index_server/src/graph/simple_capacity_graph.rs | 2 +- components/index_server/src/graph/test_utils.rs | 2 +- components/index_server/src/lib.rs | 3 --- 5 files changed, 5 insertions(+), 8 deletions(-) diff --git a/components/index_server/src/graph/capacity_graph.rs b/components/index_server/src/graph/capacity_graph.rs index f8fd844d9..88137b5e1 100644 --- a/components/index_server/src/graph/capacity_graph.rs +++ b/components/index_server/src/graph/capacity_graph.rs @@ -1,5 +1,3 @@ -use std::ops::Add; - pub type CapacityPair = (C, C); pub trait LinearRate @@ -24,6 +22,7 @@ pub struct CapacityEdge { pub rate: T, } +#[allow(unused)] impl CapacityEdge { pub fn new(capacity: CapacityPair, rate: T) -> Self { Self { capacity, rate } diff --git a/components/index_server/src/graph/graph_service.rs b/components/index_server/src/graph/graph_service.rs index 324df932d..9e3582079 100644 --- a/components/index_server/src/graph/graph_service.rs +++ b/components/index_server/src/graph/graph_service.rs @@ -2,7 +2,7 @@ use futures::channel::{mpsc, oneshot}; use futures::task::{Spawn, SpawnError, SpawnExt}; use futures::{FutureExt, SinkExt, StreamExt, TryFutureExt}; -use super::capacity_graph::{CapacityEdge, CapacityGraph, CapacityRoute, CapacityMultiRoute}; +use super::capacity_graph::{CapacityEdge, CapacityGraph, CapacityMultiRoute}; pub enum GraphRequest { /// Change capacities on a directed edge: @@ -224,6 +224,7 @@ mod tests { use super::*; use super::super::simple_capacity_graph::SimpleCapacityGraph; use super::super::test_utils::ConstRate; + use super::super::capacity_graph::CapacityRoute; use futures::executor::ThreadPool; diff --git a/components/index_server/src/graph/simple_capacity_graph.rs b/components/index_server/src/graph/simple_capacity_graph.rs index 6639e0c28..693871106 100644 --- a/components/index_server/src/graph/simple_capacity_graph.rs +++ b/components/index_server/src/graph/simple_capacity_graph.rs @@ -3,7 +3,7 @@ use std::{cmp, hash}; use super::bfs::bfs; use super::capacity_graph::{ - CapacityEdge, CapacityGraph, CapacityMultiRoute, CapacityPair, CapacityRoute, LinearRate, + CapacityEdge, CapacityGraph, CapacityMultiRoute, CapacityRoute, LinearRate, }; use super::utils::{option_to_vec, OptionIterator}; diff --git a/components/index_server/src/graph/test_utils.rs b/components/index_server/src/graph/test_utils.rs index 56a1f1c7a..018d033c5 100644 --- a/components/index_server/src/graph/test_utils.rs +++ b/components/index_server/src/graph/test_utils.rs @@ -12,7 +12,7 @@ impl LinearRate for ConstRate { ConstRate(0) } - fn calc_fee(&self, k: Self::K) -> Option { + fn calc_fee(&self, _k: Self::K) -> Option { Some(self.0) } diff --git a/components/index_server/src/lib.rs b/components/index_server/src/lib.rs index a73379196..286754404 100644 --- a/components/index_server/src/lib.rs +++ b/components/index_server/src/lib.rs @@ -19,12 +19,9 @@ extern crate log; #[macro_use] extern crate common; -#[allow(unused)] mod backoff_connector; -#[allow(unused)] mod graph; mod net_server; -#[allow(unused)] mod server; mod verifier; From 970b976bd959a52877461a95a525cedca1b64c7b Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 17:59:27 +0300 Subject: [PATCH 193/248] Temporarily commented out node modules (To make compilation work). --- .../node/src/connect/node_connection/routes.rs | 2 +- components/node/src/lib.rs | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/components/node/src/connect/node_connection/routes.rs b/components/node/src/connect/node_connection/routes.rs index 206b12158..e5ace8827 100644 --- a/components/node/src/connect/node_connection/routes.rs +++ b/components/node/src/connect/node_connection/routes.rs @@ -9,7 +9,7 @@ use crypto::uid::Uid; use proto::app_server::messages::{AppRequest, AppToAppServer}; use proto::index_client::messages::{ClientResponseRoutes, ResponseRoutesResult}; -use proto::index_server::messages::{RequestRoutes, RouteWithCapacity}; +use proto::index_server::messages::{MultiRoute, RequestRoutes}; #[derive(Debug)] pub struct AppRoutesError; diff --git a/components/node/src/lib.rs b/components/node/src/lib.rs index 1f1980f31..b7f4b32ae 100644 --- a/components/node/src/lib.rs +++ b/components/node/src/lib.rs @@ -12,18 +12,22 @@ clippy::new_without_default )] +#[allow(unused)] #[macro_use] extern crate log; +#[allow(unused)] #[macro_use] extern crate serde_derive; +#[allow(unused)] mod adapters; -pub mod connect; -mod net_node; -mod node; +// pub mod connect; +// mod net_node; +// mod node; +#[allow(unused)] mod types; -pub use self::net_node::{net_node, NetNodeError}; +// pub use self::net_node::{net_node, NetNodeError}; pub use self::types::{NodeConfig, NodeState}; -pub use app_server::IncomingAppConnection; +// pub use app_server::IncomingAppConnection; From a598b60addc476476c2a5d2c5c8845b6483dd2ad Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 18:04:52 +0300 Subject: [PATCH 194/248] node config: Implemented set_friend_rate(). --- components/node/src/connect/mod.rs | 7 +++++-- .../node/src/connect/node_connection/config.rs | 14 +++++++++++++- components/node/src/connect/node_connection/mod.rs | 10 +++++----- .../node/src/connect/node_connection/send_funds.rs | 5 +---- components/node/src/lib.rs | 2 +- 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/components/node/src/connect/mod.rs b/components/node/src/connect/mod.rs index 95dccf55e..5a1c94998 100644 --- a/components/node/src/connect/mod.rs +++ b/components/node/src/connect/mod.rs @@ -1,8 +1,11 @@ -mod connect; +// mod connect; +#[allow(unused)] mod node_connection; -pub use self::connect::{node_connect, NodeConnection}; +// pub use self::connect::{node_connect, NodeConnection}; +/* pub use self::node_connection::{ config::AppConfig, report::AppReport, routes::AppRoutes, send_funds::AppSendFunds, }; +*/ diff --git a/components/node/src/connect/node_connection/config.rs b/components/node/src/connect/node_connection/config.rs index 6d87a918b..916a40f0c 100644 --- a/components/node/src/connect/node_connection/config.rs +++ b/components/node/src/connect/node_connection/config.rs @@ -9,7 +9,7 @@ use crypto::uid::Uid; use proto::app_server::messages::{AppRequest, AppToAppServer, NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ - AddFriend, ResetFriendChannel, SetFriendRelays, SetFriendRemoteMaxDebt, + AddFriend, Rate, ResetFriendChannel, SetFriendRate, SetFriendRelays, SetFriendRemoteMaxDebt, }; use proto::index_server::messages::NamedIndexServerAddress; @@ -151,6 +151,18 @@ where ))) } + pub async fn set_friend_rate( + &mut self, + friend_public_key: PublicKey, + rate: Rate, + ) -> Result<(), AppConfigError> { + let set_friend_rate = SetFriendRate { + friend_public_key, + rate, + }; + await!(self.send_request(AppRequest::SetFriendRate(set_friend_rate))) + } + pub async fn reset_friend_channel( &mut self, friend_public_key: PublicKey, diff --git a/components/node/src/connect/node_connection/mod.rs b/components/node/src/connect/node_connection/mod.rs index ad02e0aa3..c2a48bd91 100644 --- a/components/node/src/connect/node_connection/mod.rs +++ b/components/node/src/connect/node_connection/mod.rs @@ -1,8 +1,8 @@ pub mod config; -pub mod report; -pub mod routes; -pub mod send_funds; +// pub mod report; +// pub mod routes; +// pub mod send_funds; -mod node_connection; +// mod node_connection; -pub use self::node_connection::{NodeConnection, NodeConnectionError}; +// pub use self::node_connection::{NodeConnection, NodeConnectionError}; diff --git a/components/node/src/connect/node_connection/send_funds.rs b/components/node/src/connect/node_connection/send_funds.rs index 2c9966498..9f6784826 100644 --- a/components/node/src/connect/node_connection/send_funds.rs +++ b/components/node/src/connect/node_connection/send_funds.rs @@ -8,10 +8,7 @@ use crypto::invoice_id::InvoiceId; use crypto::uid::Uid; use proto::app_server::messages::{AppRequest, AppToAppServer}; -use proto::funder::messages::{ - FriendsRoute, Receipt, ReceiptAck, ResponseReceived, ResponseSendFundsResult, - UserRequestSendFunds, -}; +use proto::funder::messages::{FriendsRoute, Receipt, ReceiptAck, UserRequestSendFunds}; // TODO: Different in naming convention from AppConfigError and AppRoutesError: #[derive(Debug)] diff --git a/components/node/src/lib.rs b/components/node/src/lib.rs index b7f4b32ae..6a299b4ed 100644 --- a/components/node/src/lib.rs +++ b/components/node/src/lib.rs @@ -22,7 +22,7 @@ extern crate serde_derive; #[allow(unused)] mod adapters; -// pub mod connect; +pub mod connect; // mod net_node; // mod node; #[allow(unused)] From e85738c85640f2c550a3e2bde1219ffc71a3d8b6 Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 18:06:35 +0300 Subject: [PATCH 195/248] node connect: Restored report and routes modules. --- components/node/src/connect/node_connection/mod.rs | 4 ++-- components/node/src/connect/node_connection/routes.rs | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/components/node/src/connect/node_connection/mod.rs b/components/node/src/connect/node_connection/mod.rs index c2a48bd91..eeed71c74 100644 --- a/components/node/src/connect/node_connection/mod.rs +++ b/components/node/src/connect/node_connection/mod.rs @@ -1,6 +1,6 @@ pub mod config; -// pub mod report; -// pub mod routes; +pub mod report; +pub mod routes; // pub mod send_funds; // mod node_connection; diff --git a/components/node/src/connect/node_connection/routes.rs b/components/node/src/connect/node_connection/routes.rs index e5ace8827..8c85bcd15 100644 --- a/components/node/src/connect/node_connection/routes.rs +++ b/components/node/src/connect/node_connection/routes.rs @@ -70,7 +70,7 @@ where source: PublicKey, destination: PublicKey, opt_exclude: Option<(PublicKey, PublicKey)>, - ) -> Result, AppRoutesError> { + ) -> Result, AppRoutesError> { let request_routes_id = Uid::new(&self.rng); let request_routes = RequestRoutes { request_id: request_routes_id, @@ -96,9 +96,7 @@ where continue; } match client_response_routes.result { - ResponseRoutesResult::Success(routes_with_capacity) => { - return Ok(routes_with_capacity) - } + ResponseRoutesResult::Success(multi_routes) => return Ok(multi_routes), ResponseRoutesResult::Failure => return Err(AppRoutesError), } } From 5e7e1982cab5d893b2d093aae1eec6dd50229fd7 Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 18:42:13 +0300 Subject: [PATCH 196/248] node_connection: Restored send_funds module --- .../node/src/connect/node_connection/mod.rs | 2 +- .../src/connect/node_connection/send_funds.rs | 151 +++++++++++++----- 2 files changed, 115 insertions(+), 38 deletions(-) diff --git a/components/node/src/connect/node_connection/mod.rs b/components/node/src/connect/node_connection/mod.rs index eeed71c74..7550697e9 100644 --- a/components/node/src/connect/node_connection/mod.rs +++ b/components/node/src/connect/node_connection/mod.rs @@ -1,7 +1,7 @@ pub mod config; pub mod report; pub mod routes; -// pub mod send_funds; +pub mod send_funds; // mod node_connection; diff --git a/components/node/src/connect/node_connection/send_funds.rs b/components/node/src/connect/node_connection/send_funds.rs index 9f6784826..c77f4178e 100644 --- a/components/node/src/connect/node_connection/send_funds.rs +++ b/components/node/src/connect/node_connection/send_funds.rs @@ -5,32 +5,40 @@ use futures::{SinkExt, StreamExt}; use crypto::crypto_rand::{CryptoRandom, OffstSystemRandom}; use crypto::identity::PublicKey; use crypto::invoice_id::InvoiceId; +use crypto::payment_id::PaymentId; use crypto::uid::Uid; use proto::app_server::messages::{AppRequest, AppToAppServer}; -use proto::funder::messages::{FriendsRoute, Receipt, ReceiptAck, UserRequestSendFunds}; +use proto::funder::messages::{ + AckClosePayment, Commit, CreatePayment, CreateTransaction, FriendsRoute, PaymentStatus, + Receipt, ReceiptAck, RequestResult, ResponseClosePayment, TransactionResult, +}; // TODO: Different in naming convention from AppConfigError and AppRoutesError: #[derive(Debug)] pub enum SendFundsError { /// A local error occurred when trying to send funds. /// (Connectivity error) - LocalError, + ConnectivityError, /// A remote error occurred when trying to send funds. /// (Not enough credits, Some node cancelled along the route) - RemoteError(PublicKey), + NodeError, /// The request was issued, but no response was received. /// The request should be saved (By the caller) and resent at another time. NoResponse, } +#[derive(Debug)] +pub struct CreatePaymentError; + #[derive(Debug)] pub struct ReceiptAckError; #[derive(Clone)] pub struct AppSendFunds { sender: mpsc::Sender, - send_funds_mc: MultiConsumerClient, + transaction_result_mc: MultiConsumerClient, + response_close_payment_mc: MultiConsumerClient, done_app_requests_mc: MultiConsumerClient, rng: R, } @@ -41,79 +49,146 @@ where { pub(super) fn new( sender: mpsc::Sender, - send_funds_mc: MultiConsumerClient, + transaction_result_mc: MultiConsumerClient, + response_close_payment_mc: MultiConsumerClient, done_app_requests_mc: MultiConsumerClient, rng: R, ) -> Self { AppSendFunds { sender, - send_funds_mc, + transaction_result_mc, + response_close_payment_mc, done_app_requests_mc, rng, } } - pub async fn request_send_funds( + pub async fn create_payment( &mut self, + payment_id: PaymentId, + invoice_id: InvoiceId, + total_dest_payment: u128, + dest_public_key: PublicKey, + ) -> Result<(), SendFundsError> { + let create_payment = CreatePayment { + payment_id, + invoice_id, + total_dest_payment, + dest_public_key, + }; + + let app_request_id = Uid::new(&self.rng); + let to_app_server = + AppToAppServer::new(app_request_id, AppRequest::CreatePayment(create_payment)); + + // Start listening to done requests: + let mut incoming_done_requests = await!(self.done_app_requests_mc.request_stream()) + .map_err(|_| SendFundsError::ConnectivityError)?; + + // Send CreatePayment: + await!(self.sender.send(to_app_server)).map_err(|_| SendFundsError::ConnectivityError)?; + + // Wait for a sign that our request was received: + while let Some(done_request_id) = await!(incoming_done_requests.next()) { + if app_request_id == done_request_id { + return Ok(()); + } + } + // We lost connectivity before we got any response: + Err(SendFundsError::NoResponse) + } + + pub async fn create_transaction( + &mut self, + payment_id: PaymentId, request_id: Uid, route: FriendsRoute, - invoice_id: InvoiceId, dest_payment: u128, - ) -> Result { - let user_request_send_funds = UserRequestSendFunds { + fees: u128, + ) -> Result { + let create_transaction = CreateTransaction { + payment_id, request_id, route, - invoice_id, dest_payment, + fees, }; let app_request_id = Uid::new(&self.rng); let to_app_server = AppToAppServer::new( app_request_id, - AppRequest::RequestSendFunds(user_request_send_funds), + AppRequest::CreateTransaction(create_transaction), ); - let mut incoming_send_funds = - await!(self.send_funds_mc.request_stream()).map_err(|_| SendFundsError::LocalError)?; + let mut incoming_transaction_results = await!(self.transaction_result_mc.request_stream()) + .map_err(|_| SendFundsError::ConnectivityError)?; - await!(self.sender.send(to_app_server)).map_err(|_| SendFundsError::LocalError)?; + await!(self.sender.send(to_app_server)).map_err(|_| SendFundsError::ConnectivityError)?; - while let Some(response_received) = await!(incoming_send_funds.next()) { - if response_received.request_id != request_id { + while let Some(transaction_result) = await!(incoming_transaction_results.next()) { + if transaction_result.request_id != request_id { // This is not our request continue; } - match response_received.result { - ResponseSendFundsResult::Success(receipt) => return Ok(receipt), - ResponseSendFundsResult::Failure(public_key) => { - return Err(SendFundsError::RemoteError(public_key)) - } + match transaction_result.result { + RequestResult::Success(commit) => return Ok(commit), + RequestResult::Failure => return Err(SendFundsError::NodeError), } } - // We lost connectivity before we got any response for the request to send funds. + // We lost connectivity before we got any response: Err(SendFundsError::NoResponse) } - pub async fn receipt_ack( + pub async fn request_close_payment( &mut self, - request_id: Uid, - receipt: Receipt, - ) -> Result<(), ReceiptAckError> { - let receipt_ack = ReceiptAck { - request_id, - receipt_signature: receipt.signature, - }; + payment_id: PaymentId, + ) -> Result { + let app_request_id = Uid::new(&self.rng); + let to_app_server = AppToAppServer::new( + app_request_id, + AppRequest::RequestClosePayment(payment_id.clone()), + ); + + let mut incoming_response_close_payment = + await!(self.response_close_payment_mc.request_stream()) + .map_err(|_| SendFundsError::ConnectivityError)?; + + await!(self.sender.send(to_app_server)).map_err(|_| SendFundsError::ConnectivityError)?; + + while let Some(response_close_payment) = await!(incoming_response_close_payment.next()) { + if response_close_payment.payment_id != payment_id { + // This is not our close request + continue; + } + return Ok(response_close_payment.status); + } + // We lost connectivity before we got any response: + Err(SendFundsError::NoResponse) + } + + pub async fn ack_close_payment( + &mut self, + payment_id: PaymentId, + ack_uid: Uid, + ) -> Result<(), SendFundsError> { let app_request_id = Uid::new(&self.rng); - let to_app_server = - AppToAppServer::new(app_request_id, AppRequest::ReceiptAck(receipt_ack)); + let ack_close_payment = AckClosePayment { + payment_id: payment_id, + ack_uid: ack_uid, + }; + + let to_app_server = AppToAppServer::new( + app_request_id, + AppRequest::AckClosePayment(ack_close_payment), + ); // Start listening to done requests: - let mut incoming_done_requests = - await!(self.done_app_requests_mc.request_stream()).map_err(|_| ReceiptAckError)?; + let mut incoming_done_requests = await!(self.done_app_requests_mc.request_stream()) + .map_err(|_| SendFundsError::ConnectivityError)?; // Send ReceiptAck: - await!(self.sender.send(to_app_server)).map_err(|_| ReceiptAckError)?; + await!(self.sender.send(to_app_server)).map_err(|_| SendFundsError::ConnectivityError)?; // Wait for a sign that our request was received: while let Some(done_request_id) = await!(incoming_done_requests.next()) { @@ -121,6 +196,8 @@ where return Ok(()); } } - Err(ReceiptAckError) + + // We lost connectivity before we got any response: + Err(SendFundsError::NoResponse) } } From 2b582a2329556837e7afb3f5e79bd97f24c929d9 Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 19:07:21 +0300 Subject: [PATCH 197/248] node_connection: Restored node_connection module. --- .../node/src/connect/node_connection/mod.rs | 2 +- .../node_connection/node_connection.rs | 39 ++++++++++++++----- .../src/connect/node_connection/send_funds.rs | 16 ++++---- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/components/node/src/connect/node_connection/mod.rs b/components/node/src/connect/node_connection/mod.rs index 7550697e9..ecb46b6a6 100644 --- a/components/node/src/connect/node_connection/mod.rs +++ b/components/node/src/connect/node_connection/mod.rs @@ -3,6 +3,6 @@ pub mod report; pub mod routes; pub mod send_funds; -// mod node_connection; +mod node_connection; // pub use self::node_connection::{NodeConnection, NodeConnectionError}; diff --git a/components/node/src/connect/node_connection/node_connection.rs b/components/node/src/connect/node_connection/node_connection.rs index fb27ce8ff..1d9ec0a05 100644 --- a/components/node/src/connect/node_connection/node_connection.rs +++ b/components/node/src/connect/node_connection/node_connection.rs @@ -2,20 +2,27 @@ use futures::channel::mpsc; use futures::task::{Spawn, SpawnExt}; use futures::{FutureExt, SinkExt, StreamExt, TryFutureExt}; -use proto::app_server::messages::AppServerToApp; +use proto::app_server::messages::{AppPermissions, AppServerToApp, AppToAppServer, NodeReport}; use crypto::crypto_rand::{CryptoRandom, OffstSystemRandom}; use common::multi_consumer::{multi_consumer_service, MultiConsumerClient}; use common::mutable_state::BatchMutable; use common::state_service::{state_service, StateClient}; +use common::conn::{ConnPair}; + use super::config::AppConfig; use super::report::AppReport; use super::routes::AppRoutes; use super::send_funds::AppSendFunds; -use crate::connect::connect::NodeConnectionTuple; + +pub type NodeConnectionTuple = ( + AppPermissions, + NodeReport, + ConnPair, +); #[derive(Debug)] pub enum NodeConnectionError { @@ -71,14 +78,24 @@ where .spawn(routes_fut) .map_err(|_| NodeConnectionError::SpawnError)?; - let (mut incoming_send_funds_sender, incoming_send_funds) = mpsc::channel(0); + let (mut incoming_transaction_results_sender, incoming_transaction_results) = mpsc::channel(0); let (requests_sender, incoming_requests) = mpsc::channel(0); - let send_funds_mc = MultiConsumerClient::new(requests_sender); - let send_funds_fut = multi_consumer_service(incoming_send_funds, incoming_requests) + let transaction_results_mc = MultiConsumerClient::new(requests_sender); + let transaction_results_fut = multi_consumer_service(incoming_transaction_results, incoming_requests) .map_err(|e| error!("SendFunds multi_consumer_service() error: {:?}", e)) .map(|_| ()); spawner - .spawn(send_funds_fut) + .spawn(transaction_results_fut) + .map_err(|_| NodeConnectionError::SpawnError)?; + + let (mut incoming_response_close_payments_sender, incoming_response_close_payments) = mpsc::channel(0); + let (requests_sender, incoming_requests) = mpsc::channel(0); + let response_close_payments_mc = MultiConsumerClient::new(requests_sender); + let response_close_payments_fut = multi_consumer_service(incoming_response_close_payments, incoming_requests) + .map_err(|e| error!("SendFunds multi_consumer_service() error: {:?}", e)) + .map(|_| ()); + spawner + .spawn(response_close_payments_fut) .map_err(|_| NodeConnectionError::SpawnError)?; let (mut incoming_done_app_requests_sender, incoming_done_app_requests) = mpsc::channel(0); @@ -96,8 +113,11 @@ where .spawn(async move { while let Some(message) = await!(receiver.next()) { match message { - AppServerToApp::ResponseReceived(response_received) => { - let _ = await!(incoming_send_funds_sender.send(response_received)); + AppServerToApp::TransactionResult(transaction_result) => { + let _ = await!(incoming_transaction_results_sender.send(transaction_result)); + } + AppServerToApp::ResponseClosePayment(response_close_payment) => { + let _ = await!(incoming_response_close_payments_sender.send(response_close_payment)); } AppServerToApp::Report(_node_report) => { // TODO: Maybe somehow redesign the type AppServerToApp @@ -145,7 +165,8 @@ where let opt_send_funds = if app_permissions.send_funds { Some(AppSendFunds::new( sender.clone(), - send_funds_mc.clone(), + transaction_results_mc.clone(), + response_close_payments_mc.clone(), done_app_requests_mc.clone(), rng.clone(), )) diff --git a/components/node/src/connect/node_connection/send_funds.rs b/components/node/src/connect/node_connection/send_funds.rs index c77f4178e..78d311a09 100644 --- a/components/node/src/connect/node_connection/send_funds.rs +++ b/components/node/src/connect/node_connection/send_funds.rs @@ -37,8 +37,8 @@ pub struct ReceiptAckError; #[derive(Clone)] pub struct AppSendFunds { sender: mpsc::Sender, - transaction_result_mc: MultiConsumerClient, - response_close_payment_mc: MultiConsumerClient, + transaction_results_mc: MultiConsumerClient, + response_close_payments_mc: MultiConsumerClient, done_app_requests_mc: MultiConsumerClient, rng: R, } @@ -49,15 +49,15 @@ where { pub(super) fn new( sender: mpsc::Sender, - transaction_result_mc: MultiConsumerClient, - response_close_payment_mc: MultiConsumerClient, + transaction_results_mc: MultiConsumerClient, + response_close_payments_mc: MultiConsumerClient, done_app_requests_mc: MultiConsumerClient, rng: R, ) -> Self { AppSendFunds { sender, - transaction_result_mc, - response_close_payment_mc, + transaction_results_mc, + response_close_payments_mc, done_app_requests_mc, rng, } @@ -119,7 +119,7 @@ where AppRequest::CreateTransaction(create_transaction), ); - let mut incoming_transaction_results = await!(self.transaction_result_mc.request_stream()) + let mut incoming_transaction_results = await!(self.transaction_results_mc.request_stream()) .map_err(|_| SendFundsError::ConnectivityError)?; await!(self.sender.send(to_app_server)).map_err(|_| SendFundsError::ConnectivityError)?; @@ -150,7 +150,7 @@ where ); let mut incoming_response_close_payment = - await!(self.response_close_payment_mc.request_stream()) + await!(self.response_close_payments_mc.request_stream()) .map_err(|_| SendFundsError::ConnectivityError)?; await!(self.sender.send(to_app_server)).map_err(|_| SendFundsError::ConnectivityError)?; From 1b9101aec5b4c6d80007d8cd7ad762545865084d Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 19:16:01 +0300 Subject: [PATCH 198/248] node_connections: Restored exports. --- components/node/src/connect/node_connection/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/node/src/connect/node_connection/mod.rs b/components/node/src/connect/node_connection/mod.rs index ecb46b6a6..ad02e0aa3 100644 --- a/components/node/src/connect/node_connection/mod.rs +++ b/components/node/src/connect/node_connection/mod.rs @@ -5,4 +5,4 @@ pub mod send_funds; mod node_connection; -// pub use self::node_connection::{NodeConnection, NodeConnectionError}; +pub use self::node_connection::{NodeConnection, NodeConnectionError}; From c9ad92af5975e471dc3c69e5e86dc6eb9c370c04 Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 19:20:26 +0300 Subject: [PATCH 199/248] Restored node/src/connect --- components/node/src/connect/connect.rs | 12 +++++------- components/node/src/connect/mod.rs | 7 ++----- components/node/src/connect/node_connection/mod.rs | 2 +- .../node/src/connect/node_connection/send_funds.rs | 8 +------- 4 files changed, 9 insertions(+), 20 deletions(-) diff --git a/components/node/src/connect/connect.rs b/components/node/src/connect/connect.rs index 959aa0c70..fccc653c7 100644 --- a/components/node/src/connect/connect.rs +++ b/components/node/src/connect/connect.rs @@ -2,9 +2,9 @@ use futures::channel::mpsc; use futures::task::{Spawn, SpawnExt}; use futures::{SinkExt, StreamExt}; -use common::conn::{ConnPair, ConnPairVec, FutTransform}; +use common::conn::{ConnPairVec, FutTransform}; -use proto::app_server::messages::{AppPermissions, AppServerToApp, AppToAppServer, NodeReport}; +use proto::app_server::messages::AppServerToApp; use proto::app_server::serialize::{ deserialize_app_permissions, deserialize_app_server_to_app, serialize_app_to_app_server, }; @@ -17,17 +17,15 @@ use crypto::crypto_rand::CryptoRandom; use crypto::identity::PublicKey; use identity::IdentityClient; + pub use super::node_connection::NodeConnection; +use super::node_connection::NodeConnectionTuple; + use keepalive::KeepAliveChannel; use secure_channel::SecureChannel; use version::VersionPrefix; -pub type NodeConnectionTuple = ( - AppPermissions, - NodeReport, - ConnPair, -); #[derive(Debug)] pub enum SetupConnectionError { diff --git a/components/node/src/connect/mod.rs b/components/node/src/connect/mod.rs index 5a1c94998..95dccf55e 100644 --- a/components/node/src/connect/mod.rs +++ b/components/node/src/connect/mod.rs @@ -1,11 +1,8 @@ -// mod connect; -#[allow(unused)] +mod connect; mod node_connection; -// pub use self::connect::{node_connect, NodeConnection}; +pub use self::connect::{node_connect, NodeConnection}; -/* pub use self::node_connection::{ config::AppConfig, report::AppReport, routes::AppRoutes, send_funds::AppSendFunds, }; -*/ diff --git a/components/node/src/connect/node_connection/mod.rs b/components/node/src/connect/node_connection/mod.rs index ad02e0aa3..7bd79ec37 100644 --- a/components/node/src/connect/node_connection/mod.rs +++ b/components/node/src/connect/node_connection/mod.rs @@ -5,4 +5,4 @@ pub mod send_funds; mod node_connection; -pub use self::node_connection::{NodeConnection, NodeConnectionError}; +pub use self::node_connection::{NodeConnection, NodeConnectionError, NodeConnectionTuple}; diff --git a/components/node/src/connect/node_connection/send_funds.rs b/components/node/src/connect/node_connection/send_funds.rs index 78d311a09..47a1b3040 100644 --- a/components/node/src/connect/node_connection/send_funds.rs +++ b/components/node/src/connect/node_connection/send_funds.rs @@ -11,7 +11,7 @@ use crypto::uid::Uid; use proto::app_server::messages::{AppRequest, AppToAppServer}; use proto::funder::messages::{ AckClosePayment, Commit, CreatePayment, CreateTransaction, FriendsRoute, PaymentStatus, - Receipt, ReceiptAck, RequestResult, ResponseClosePayment, TransactionResult, + RequestResult, ResponseClosePayment, TransactionResult, }; // TODO: Different in naming convention from AppConfigError and AppRoutesError: @@ -28,12 +28,6 @@ pub enum SendFundsError { NoResponse, } -#[derive(Debug)] -pub struct CreatePaymentError; - -#[derive(Debug)] -pub struct ReceiptAckError; - #[derive(Clone)] pub struct AppSendFunds { sender: mpsc::Sender, From 247b292fd4aa41f4bc5141432bb5e7f32ad2b399 Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 19:21:58 +0300 Subject: [PATCH 200/248] Fully restored offst-node. --- components/node/src/lib.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/components/node/src/lib.rs b/components/node/src/lib.rs index 6a299b4ed..1f1980f31 100644 --- a/components/node/src/lib.rs +++ b/components/node/src/lib.rs @@ -12,22 +12,18 @@ clippy::new_without_default )] -#[allow(unused)] #[macro_use] extern crate log; -#[allow(unused)] #[macro_use] extern crate serde_derive; -#[allow(unused)] mod adapters; pub mod connect; -// mod net_node; -// mod node; -#[allow(unused)] +mod net_node; +mod node; mod types; -// pub use self::net_node::{net_node, NetNodeError}; +pub use self::net_node::{net_node, NetNodeError}; pub use self::types::{NodeConfig, NodeState}; -// pub use app_server::IncomingAppConnection; +pub use app_server::IncomingAppConnection; From b539e4cbbbf70af83e23cb12cb45e50a24cfde03 Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 19:24:19 +0300 Subject: [PATCH 201/248] Restored offst-app. --- Cargo.lock | 1 - components/app/Cargo.toml | 1 - components/app/src/lib.rs | 2 +- 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0047f1d46..6d79d4029 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -696,7 +696,6 @@ version = "0.1.0" dependencies = [ "futures-preview 0.3.0-alpha.16 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "offst-bin 0.1.0", "offst-common 0.1.0", "offst-crypto 0.1.0", "offst-identity 0.1.0", diff --git a/components/app/Cargo.toml b/components/app/Cargo.toml index 71da0cf51..67e01d483 100644 --- a/components/app/Cargo.toml +++ b/components/app/Cargo.toml @@ -13,7 +13,6 @@ identity = { path = "../identity", version = "0.1.0" , package = "offst-identity timer = { path = "../timer", version = "0.1.0" , package = "offst-timer" } proto = { path = "../proto", version = "0.1.0" , package = "offst-proto" } net = { path = "../net", version = "0.1.0" , package = "offst-net" } -bin = { path = "../bin", version = "0.1.0" , package = "offst-bin" } # TODO: How can we avoid taking all of node? # This is a very heavy dependency: node = { path = "../node", version = "0.1.0" , package = "offst-node" } diff --git a/components/app/src/lib.rs b/components/app/src/lib.rs index 916c21066..b8bb1647b 100644 --- a/components/app/src/lib.rs +++ b/components/app/src/lib.rs @@ -58,7 +58,7 @@ pub mod invoice { pub mod route { pub use proto::funder::messages::FriendsRoute; - pub use proto::index_server::messages::RouteWithCapacity; + pub use proto::index_server::messages::MultiRoute; } From e15aac3450eba9a746676aa2b7b55bdd4fe1bbb4 Mon Sep 17 00:00:00 2001 From: real Date: Tue, 28 May 2019 19:46:20 +0300 Subject: [PATCH 202/248] Some initial work on stctrl (atomic payments). Not done yet. --- components/stctrl/src/funds.rs | 66 +++++++++++++++++----------------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/components/stctrl/src/funds.rs b/components/stctrl/src/funds.rs index 4c0872b6f..b852f3eb8 100644 --- a/components/stctrl/src/funds.rs +++ b/components/stctrl/src/funds.rs @@ -8,7 +8,7 @@ use structopt::StructOpt; use app::gen::gen_uid; use app::invoice::{InvoiceId, INVOICE_ID_LEN}; -use app::route::{FriendsRoute, RouteWithCapacity}; +use app::route::{FriendsRoute, MultiRoute}; use crate::file::invoice::load_invoice_from_file; use crate::file::receipt::store_receipt_to_file; @@ -66,40 +66,38 @@ pub enum FundsError { WriteError, } +// TODO: The algorithm here might not be accurate in all cases. +// Fix later. +/// Can we push the given amount of credits through this multi route? +fn is_good_multi_route( + multi_route: &MultiRoute, + mut amount: u128) -> bool { + + let mut credit_count = 0u128; + let mut fees_count = 0u128; + + for route_capacity_rate in multi_route.routes { + let route_fees = route_capacity_rate.rate.calc_fees(route_capacity_rate.capacity)?; + fees_count = fees_count.checked_add(route_fees)?; + // Amount of capacity we are left with for actual payment: + let left_capacity = route_capacity_rate.capacity.checked_sub(route_fees)?; + credit_count = credit_count.checked_add(left_capacity)?; + } + + credit_count >= amount + +} + /// Choose a route for pushing `amount` credits -fn choose_route( - routes_with_capacity: Vec, +fn choose_multi_route( + multi_routes: Vec, amount: u128, -) -> Result { - // We naively select the first route we find suitable: +) -> Result { + // We naively select the first multi-route we find suitable: // TODO: Possibly improve this later: - for route_with_capacity in routes_with_capacity { - // TODO: Is this dangerous? How can we do this safely? - let length = route_with_capacity.route.len() as u128; - - // For route of length 2 we pay 0. (source and destination are included) - // For route of length 3 we pay 1. - // ... - let extra: u128 = if let Some(extra) = length.checked_sub(2) { - extra - } else { - // This is an invalid route - warn!( - "Received invalid route of length: {}. Skipping route", - route_with_capacity.route.len() - ); - continue; - }; - - let total: u128 = if let Some(total) = extra.checked_add(amount) { - total - } else { - warn!("Overflow when calculating total payment. Skipping route"); - continue; - }; - - if total <= route_with_capacity.capacity { - return Ok(route_with_capacity.route); + for multi_route in multi_routes { + if is_good_multi_route(&multi_route, amount) { + return Ok(multi_route) } } Err(FundsError::NoSuitableRoute) @@ -144,8 +142,8 @@ async fn funds_send_funds( )) // No exclusion of edges .map_err(|_| FundsError::AppRoutesError)?; - let route = choose_route(routes_with_capacity, dest_payment)?; - let fees = route.len().checked_sub(2).unwrap(); + let multi_route = choose_multi_route(routes_with_capacity, dest_payment)?; + // TODO: Calculate fees? // A trivial invoice: let request_id = gen_uid(); From 1756b03454ea8b4555fa2e8b56f0d2d2767f943a Mon Sep 17 00:00:00 2001 From: real Date: Wed, 29 May 2019 10:36:50 +0300 Subject: [PATCH 203/248] get_route_rate(): Do not add the rate of the last hop. --- components/index_server/src/graph/graph_service.rs | 4 ++-- components/index_server/src/graph/simple_capacity_graph.rs | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/components/index_server/src/graph/graph_service.rs b/components/index_server/src/graph/graph_service.rs index 9e3582079..441e7e5a1 100644 --- a/components/index_server/src/graph/graph_service.rs +++ b/components/index_server/src/graph/graph_service.rs @@ -247,7 +247,7 @@ mod tests { CapacityRoute { route: vec![2,5], capacity: 30, - rate: ConstRate(1), + rate: ConstRate(0), } ], }] @@ -259,7 +259,7 @@ mod tests { CapacityRoute { route: vec![2,5], capacity: 30, - rate: ConstRate(1), + rate: ConstRate(0), } ], }] diff --git a/components/index_server/src/graph/simple_capacity_graph.rs b/components/index_server/src/graph/simple_capacity_graph.rs index 693871106..35b935615 100644 --- a/components/index_server/src/graph/simple_capacity_graph.rs +++ b/components/index_server/src/graph/simple_capacity_graph.rs @@ -140,7 +140,9 @@ where /// Calculate the total rate of sending credits along a given route fn get_route_rate(&self, route: &[N]) -> Option { let mut total_rate = T::zero(); - for i in 0..route.len().checked_sub(1)? { + // If the route is only of length 2, the rate will be 0. + // No fees are paid for the last hop. TODO: Is this the right behaviour? + for i in 0..route.len().checked_sub(2)? { let edge = self.get_edge(&route[i], &route[i + 1])?; total_rate = total_rate.checked_add(&edge.capacity_edge.rate)?; } From d99e20c2122f7ce44a847da415a6f2b86cf1ba7d Mon Sep 17 00:00:00 2001 From: real Date: Wed, 29 May 2019 11:42:42 +0300 Subject: [PATCH 204/248] Temporarily removed serialize tests in offst-proto (To make offst-proto compile). --- components/proto/src/app_server/serialize.rs | 2 ++ components/proto/src/funder/serialize.rs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/components/proto/src/app_server/serialize.rs b/components/proto/src/app_server/serialize.rs index dd0315530..150240a05 100644 --- a/components/proto/src/app_server/serialize.rs +++ b/components/proto/src/app_server/serialize.rs @@ -750,6 +750,7 @@ pub fn deserialize_app_to_app_server(data: &[u8]) -> Result Result Date: Wed, 29 May 2019 11:43:04 +0300 Subject: [PATCH 205/248] Implemented Rate::max_payable(). --- components/proto/src/funder/messages.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index e6be8316d..be8566c6b 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -421,11 +421,36 @@ impl Rate { Rate { mul: 0, add: 0 } } + /// Calculate the amount of additional fee credits we have to pay if + /// we want to pay `dest_payment` credits. pub fn calc_fee(&self, dest_payment: u128) -> Option { let mul_res = (BigUint::from(dest_payment) * BigUint::from(self.mul)) >> 32; let res = mul_res + BigUint::from(self.add); res.to_u128() } + + /// Maximum amount of credits we should be able to pay + /// through a given capacity. + /// + /// Solves the equation: + /// x + (mx + n) <= c + /// As: + /// x <= (c - n) / (m + 1) + /// When m = m0 / 2^32, we get: + /// x <= ((c - n) * 2^32) / (m0 + 2^32) + pub fn max_payable(&self, capacity: u128) -> u128 { + let long_add = u128::from(self.add); + let c_minus_n = if let Some(c_minus_n) = capacity.checked_sub(long_add) { + c_minus_n + } else { + // Right hand side is going to be non-positive, this means maximum payable is 0. + return 0; + }; + + let numerator = BigUint::from(c_minus_n) << 32; + let denominator = BigUint::from(self.mul) + (BigUint::from(1u128) << 32); + (numerator / denominator).to_u128().unwrap() + } } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] From 5d4ef63fa9cacb7235945bc7e202362e1da1bfef Mon Sep 17 00:00:00 2001 From: real Date: Wed, 29 May 2019 11:43:26 +0300 Subject: [PATCH 206/248] Removed parts of offst-stctrl to allow it to compile. --- components/stctrl/Cargo.toml | 4 -- components/stctrl/src/bin/stregister.rs | 26 ----------- components/stctrl/src/file/mod.rs | 2 +- components/stctrl/src/funds.rs | 59 +++++++++++++++---------- components/stctrl/src/lib.rs | 7 ++- 5 files changed, 42 insertions(+), 56 deletions(-) delete mode 100644 components/stctrl/src/bin/stregister.rs diff --git a/components/stctrl/Cargo.toml b/components/stctrl/Cargo.toml index 3a11999e3..549cedb53 100644 --- a/components/stctrl/Cargo.toml +++ b/components/stctrl/Cargo.toml @@ -13,10 +13,6 @@ path = "src/lib.rs" name = "stctrl" path = "src/bin/stctrl.rs" -[[bin]] -name = "stregister" -path = "src/bin/stregister.rs" - [dependencies] app = { path = "../app", version = "0.1.0", package = "offst-app" } diff --git a/components/stctrl/src/bin/stregister.rs b/components/stctrl/src/bin/stregister.rs deleted file mode 100644 index 7b2301cf4..000000000 --- a/components/stctrl/src/bin/stregister.rs +++ /dev/null @@ -1,26 +0,0 @@ -#![feature(async_await, await_macro, arbitrary_self_types)] -#![feature(nll)] -#![feature(generators)] -#![feature(never_type)] -#![deny(trivial_numeric_casts, warnings)] -#![allow(intra_doc_link_resolution_failure)] - -#[macro_use] -extern crate log; - -use std::io; -use structopt::StructOpt; - -use stctrl::stregisterlib::{stregister, StRegisterCmd, StRegisterError}; - -fn run() -> Result<(), StRegisterError> { - env_logger::init(); - let st_register_cmd = StRegisterCmd::from_args(); - stregister(st_register_cmd, &mut io::stdout()) -} - -fn main() { - if let Err(e) = run() { - error!("error: {:?}", e); - } -} diff --git a/components/stctrl/src/file/mod.rs b/components/stctrl/src/file/mod.rs index d4da9c8b8..34e7e7263 100644 --- a/components/stctrl/src/file/mod.rs +++ b/components/stctrl/src/file/mod.rs @@ -1,3 +1,3 @@ pub mod invoice; -pub mod receipt; +// pub mod receipt; pub mod token; diff --git a/components/stctrl/src/funds.rs b/components/stctrl/src/funds.rs index b852f3eb8..c5a2f3bf2 100644 --- a/components/stctrl/src/funds.rs +++ b/components/stctrl/src/funds.rs @@ -11,7 +11,7 @@ use app::invoice::{InvoiceId, INVOICE_ID_LEN}; use app::route::{FriendsRoute, MultiRoute}; use crate::file::invoice::load_invoice_from_file; -use crate::file::receipt::store_receipt_to_file; +// use crate::file::receipt::store_receipt_to_file; /// Send funds to a remote destination #[derive(Clone, Debug, StructOpt)] @@ -41,9 +41,6 @@ pub struct PayInvoiceCmd { /// Funds sending related commands #[derive(Clone, Debug, StructOpt)] pub enum FundsCmd { - /// Send funds to a remote destination (Using destination public key) - #[structopt(name = "send-funds")] - SendFunds(SendFundsCmd), /// Pay an invoice (Using an invoice file) #[structopt(name = "pay-invoice")] PayInvoice(PayInvoiceCmd), @@ -66,26 +63,24 @@ pub enum FundsError { WriteError, } -// TODO: The algorithm here might not be accurate in all cases. -// Fix later. /// Can we push the given amount of credits through this multi route? fn is_good_multi_route( multi_route: &MultiRoute, mut amount: u128) -> bool { let mut credit_count = 0u128; - let mut fees_count = 0u128; - - for route_capacity_rate in multi_route.routes { - let route_fees = route_capacity_rate.rate.calc_fees(route_capacity_rate.capacity)?; - fees_count = fees_count.checked_add(route_fees)?; - // Amount of capacity we are left with for actual payment: - let left_capacity = route_capacity_rate.capacity.checked_sub(route_fees)?; - credit_count = credit_count.checked_add(left_capacity)?; + + for route_capacity_rate in &multi_route.routes { + let max_payable = route_capacity_rate.rate.max_payable(route_capacity_rate.capacity); + credit_count = if let Some(new_credit_count) = credit_count.checked_add(max_payable) { + new_credit_count + } else { + // An overflow happened. This means we can definitely pay `amount`. + return true; + }; } credit_count >= amount - } /// Choose a route for pushing `amount` credits @@ -103,6 +98,16 @@ fn choose_multi_route( Err(FundsError::NoSuitableRoute) } +/// Find a safe choice for how much credits to push through each route in a MultiRoute (Give that we +/// know this is possible) +/// Returns a vector representing how many credits to push through every route. +fn safe_multi_route_amounts(multi_route: &MultiRoute, amount: u128) -> Option> { + // let max_payables: Vec<_> = multi_route.route.iter().map(|route| route.rate.max_payable(route.capacity)).collect(); + unimplemented!(); +} + + +/* /// Send funds to a remote destination without using an invoice. async fn funds_send_funds( send_raw_cmd: SendFundsCmd, @@ -143,7 +148,14 @@ async fn funds_send_funds( .map_err(|_| FundsError::AppRoutesError)?; let multi_route = choose_multi_route(routes_with_capacity, dest_payment)?; - // TODO: Calculate fees? + let amounts = safe_multi_route_amounts(&multi_route).unwrap(); + + // TODO: + // - Create Payment + // - Create a transaction for every route + // - Wait until the first a first failure happens or all transactions succeeded. + // - Close payment + // - Return result (MultiCommit or failure) // A trivial invoice: let request_id = gen_uid(); @@ -166,6 +178,7 @@ async fn funds_send_funds( // We only send the ack if we managed to get the receipt: await!(app_send_funds.receipt_ack(request_id, receipt)).map_err(|_| FundsError::ReceiptAckError) } +*/ /// Pay an invoice async fn funds_pay_invoice( @@ -180,6 +193,9 @@ async fn funds_pay_invoice( receipt_file, } = pay_invoice_cmd; + unimplemented!(); + + /* // Make sure that we will be able to write the receipt // before we do the actual payment: if receipt_file.exists() { @@ -194,7 +210,7 @@ async fn funds_pay_invoice( // we also need to pay nodes on the way. // We might need to solve this issue at the index server side // (Should the Server take into account the extra credits that should be paid along the way?). - let routes_with_capacity = await!(app_routes.request_routes( + let routes_with_capacity = await!(app_routes.request_multi_routes( invoice.dest_payment, local_public_key, // source invoice.dest_public_key, @@ -224,7 +240,9 @@ async fn funds_pay_invoice( // We only send the ack if we managed to get the receipt: await!(app_send_funds.receipt_ack(request_id, receipt)).map_err(|_| FundsError::ReceiptAckError) + */ } + pub async fn funds( funds_cmd: FundsCmd, mut node_connection: NodeConnection, @@ -250,13 +268,6 @@ pub async fn funds( .clone(); match funds_cmd { - FundsCmd::SendFunds(send_raw_cmd) => await!(funds_send_funds( - send_raw_cmd, - local_public_key, - app_routes, - app_send_funds, - writer, - ))?, FundsCmd::PayInvoice(pay_invoice_cmd) => await!(funds_pay_invoice( pay_invoice_cmd, local_public_key, diff --git a/components/stctrl/src/lib.rs b/components/stctrl/src/lib.rs index 970a884fb..db06d3d44 100644 --- a/components/stctrl/src/lib.rs +++ b/components/stctrl/src/lib.rs @@ -13,12 +13,17 @@ #[macro_use] extern crate prettytable; + +#[allow(unused)] #[macro_use] extern crate log; + +#[allow(unused)] #[macro_use] extern crate serde_derive; pub mod config; +#[allow(unused)] pub mod funds; pub mod info; @@ -26,4 +31,4 @@ pub mod file; pub mod utils; pub mod stctrllib; -pub mod stregisterlib; +// pub mod stregisterlib; From 48f1051f6e4b5e66e431f1e17d1816da87952d13 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 29 May 2019 12:08:38 +0300 Subject: [PATCH 207/248] ser_string: Added HashedLock and PlainLock string conversion functions. --- components/proto/src/file/ser_string.rs | 149 +++++++++++------------- 1 file changed, 65 insertions(+), 84 deletions(-) diff --git a/components/proto/src/file/ser_string.rs b/components/proto/src/file/ser_string.rs index 378e1500e..c92cc06d6 100644 --- a/components/proto/src/file/ser_string.rs +++ b/components/proto/src/file/ser_string.rs @@ -1,106 +1,87 @@ use base64::{self, URL_SAFE_NO_PAD}; use crypto::crypto_rand::{RandValue, RAND_VALUE_LEN}; use crypto::hash::{HashResult, HASH_RESULT_LEN}; +use crypto::hash_lock::{HashedLock, PlainLock, HASHED_LOCK_LEN, PLAIN_LOCK_LEN}; use crypto::identity::{PublicKey, Signature, PUBLIC_KEY_LEN, SIGNATURE_LEN}; use crypto::invoice_id::{InvoiceId, INVOICE_ID_LEN}; #[derive(Debug)] pub struct SerStringError; -/// Convert a public key into a string -pub fn public_key_to_string(public_key: &PublicKey) -> String { - base64::encode_config(&public_key, URL_SAFE_NO_PAD) -} +/// Define conversion to/from String: +macro_rules! str_convert_funcs { + ($to_string_func:ident, $from_string_func:ident, $conv_type:ident, $conv_len:ident) => { + /// Convert a our type into a string + pub fn $to_string_func(conv: &$conv_type) -> String { + base64::encode_config(&conv, URL_SAFE_NO_PAD) + } -/// Convert a string into a public key -pub fn string_to_public_key(pk_str: &str) -> Result { - // Decode public key: - let public_key_vec = - base64::decode_config(pk_str, URL_SAFE_NO_PAD).map_err(|_| SerStringError)?; - // TODO: A more idiomatic way to do this? - if public_key_vec.len() != PUBLIC_KEY_LEN { - return Err(SerStringError); - } - let mut public_key_array = [0u8; PUBLIC_KEY_LEN]; - public_key_array.copy_from_slice(&public_key_vec[0..PUBLIC_KEY_LEN]); - Ok(PublicKey::from(&public_key_array)) + /// Convert a string into a our type + pub fn $from_string_func(input_str: &str) -> Result<$conv_type, SerStringError> { + // Decode public key: + let conv_vec = + base64::decode_config(input_str, URL_SAFE_NO_PAD).map_err(|_| SerStringError)?; + // TODO: A more idiomatic way to do this? + if conv_vec.len() != $conv_len { + return Err(SerStringError); + } + let mut conv_array = [0u8; $conv_len]; + conv_array.copy_from_slice(&conv_vec[0..$conv_len]); + Ok($conv_type::from(&conv_array)) + } + }; } -/// Convert a Signature into a string -pub fn signature_to_string(signature: &Signature) -> String { - base64::encode_config(&signature, URL_SAFE_NO_PAD) -} +str_convert_funcs!( + public_key_to_string, + string_to_public_key, + PublicKey, + PUBLIC_KEY_LEN +); -/// Convert a string into a signature -pub fn string_to_signature(signature_str: &str) -> Result { - // Decode public key: - let signature_vec = - base64::decode_config(signature_str, URL_SAFE_NO_PAD).map_err(|_| SerStringError)?; - // TODO: A more idiomatic way to do this? - if signature_vec.len() != SIGNATURE_LEN { - return Err(SerStringError); - } - let mut signature_array = [0u8; SIGNATURE_LEN]; - signature_array.copy_from_slice(&signature_vec[0..SIGNATURE_LEN]); - Ok(Signature::from(&signature_array)) -} +str_convert_funcs!( + signature_to_string, + string_to_signature, + Signature, + SIGNATURE_LEN +); -/// Convert a HashResult into a string -pub fn hash_result_to_string(hash_result: &HashResult) -> String { - base64::encode_config(&hash_result, URL_SAFE_NO_PAD) -} +str_convert_funcs!( + hash_result_to_string, + string_to_hash_result, + HashResult, + HASH_RESULT_LEN +); -/// Convert a string into a HashResult -pub fn string_to_hash_result(hash_result_str: &str) -> Result { - // Decode public key: - let hash_result_vec = - base64::decode_config(hash_result_str, URL_SAFE_NO_PAD).map_err(|_| SerStringError)?; - // TODO: A more idiomatic way to do this? - if hash_result_vec.len() != HASH_RESULT_LEN { - return Err(SerStringError); - } - let mut hash_result_array = [0u8; HASH_RESULT_LEN]; - hash_result_array.copy_from_slice(&hash_result_vec[0..HASH_RESULT_LEN]); - Ok(HashResult::from(&hash_result_array)) -} +str_convert_funcs!( + invoice_id_to_string, + string_to_invoice_id, + InvoiceId, + INVOICE_ID_LEN +); -/// Convert a InvoiceId into a string -pub fn invoice_id_to_string(invoice_id: &InvoiceId) -> String { - base64::encode_config(&invoice_id, URL_SAFE_NO_PAD) -} +str_convert_funcs!( + rand_value_to_string, + string_to_rand_value, + RandValue, + RAND_VALUE_LEN +); -/// Convert a string into a InvoiceId -pub fn string_to_invoice_id(invoice_id_str: &str) -> Result { - // Decode public key: - let invoice_id_vec = - base64::decode_config(invoice_id_str, URL_SAFE_NO_PAD).map_err(|_| SerStringError)?; - // TODO: A more idiomatic way to do this? - if invoice_id_vec.len() != INVOICE_ID_LEN { - return Err(SerStringError); - } - let mut invoice_id_array = [0u8; INVOICE_ID_LEN]; - invoice_id_array.copy_from_slice(&invoice_id_vec[0..INVOICE_ID_LEN]); - Ok(InvoiceId::from(&invoice_id_array)) -} +str_convert_funcs!( + plain_lock_to_string, + string_to_plain_lock, + PlainLock, + PLAIN_LOCK_LEN +); -/// Convert a RandValue into a string -pub fn rand_value_to_string(rand_value: &RandValue) -> String { - base64::encode_config(&rand_value, URL_SAFE_NO_PAD) -} +str_convert_funcs!( + hashed_lock_to_string, + string_to_hashed_lock, + HashedLock, + HASHED_LOCK_LEN +); -/// Convert a string into a RandValue -pub fn string_to_rand_value(rand_value_str: &str) -> Result { - // Decode public key: - let rand_value_vec = - base64::decode_config(rand_value_str, URL_SAFE_NO_PAD).map_err(|_| SerStringError)?; - // TODO: A more idiomatic way to do this? - if rand_value_vec.len() != RAND_VALUE_LEN { - return Err(SerStringError); - } - let mut rand_value_array = [0u8; RAND_VALUE_LEN]; - rand_value_array.copy_from_slice(&rand_value_vec[0..RAND_VALUE_LEN]); - Ok(RandValue::from(&rand_value_array)) -} +// TODO: How to make the macro work nicely with the private key conversion code? // TODO: Find a better way to represent private key. // We currently use [u8; 85] directly because of ring limitations. From 24d1231421297523d6f81295fd0663d927c6ae21 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 29 May 2019 12:50:59 +0300 Subject: [PATCH 208/248] Added TODO comment in ser_string.rs. --- components/proto/src/file/ser_string.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/components/proto/src/file/ser_string.rs b/components/proto/src/file/ser_string.rs index c92cc06d6..673fe86c2 100644 --- a/components/proto/src/file/ser_string.rs +++ b/components/proto/src/file/ser_string.rs @@ -5,6 +5,9 @@ use crypto::hash_lock::{HashedLock, PlainLock, HASHED_LOCK_LEN, PLAIN_LOCK_LEN}; use crypto::identity::{PublicKey, Signature, PUBLIC_KEY_LEN, SIGNATURE_LEN}; use crypto::invoice_id::{InvoiceId, INVOICE_ID_LEN}; +// TODO: Possibly remove this module into offst-crypto +// Will require the extra base64 dependency in offst-crypto. + #[derive(Debug)] pub struct SerStringError; From 387b0d128c5b11024bd38d65b102069e173c9568 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 29 May 2019 12:59:29 +0300 Subject: [PATCH 209/248] Restored offst-stctrl file/receipt.rs. --- components/app/src/lib.rs | 1 + components/stctrl/src/file/mod.rs | 2 +- components/stctrl/src/file/receipt.rs | 47 ++++++++++++++++++++++++--- components/stctrl/src/funds.rs | 4 ++- 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/components/app/src/lib.rs b/components/app/src/lib.rs index b8bb1647b..24c4eea0f 100644 --- a/components/app/src/lib.rs +++ b/components/app/src/lib.rs @@ -64,4 +64,5 @@ pub mod route { pub use crypto::crypto_rand::{RandValue, RAND_VALUE_LEN}; pub use crypto::hash::{HashResult, HASH_RESULT_LEN}; +pub use crypto::hash_lock::{HashedLock, PlainLock, HASHED_LOCK_LEN, PLAIN_LOCK_LEN}; pub use crypto::identity::{PublicKey, Signature, PUBLIC_KEY_LEN, SIGNATURE_LEN}; diff --git a/components/stctrl/src/file/mod.rs b/components/stctrl/src/file/mod.rs index 34e7e7263..d4da9c8b8 100644 --- a/components/stctrl/src/file/mod.rs +++ b/components/stctrl/src/file/mod.rs @@ -1,3 +1,3 @@ pub mod invoice; -// pub mod receipt; +pub mod receipt; pub mod token; diff --git a/components/stctrl/src/file/receipt.rs b/components/stctrl/src/file/receipt.rs index 3e32b1f94..ec85cc039 100644 --- a/components/stctrl/src/file/receipt.rs +++ b/components/stctrl/src/file/receipt.rs @@ -5,11 +5,23 @@ use std::path::Path; use derive_more::*; use app::ser_string::{ - hash_result_to_string, invoice_id_to_string, signature_to_string, string_to_hash_result, - string_to_invoice_id, string_to_signature, SerStringError, + hash_result_to_string, invoice_id_to_string, plain_lock_to_string, signature_to_string, + string_to_hash_result, string_to_invoice_id, string_to_plain_lock, string_to_signature, + SerStringError, }; use app::Receipt; +/* + pub response_hash: HashResult, + // = sha512/256(requestId || sha512/256(route) || randNonce) + pub invoice_id: InvoiceId, + pub src_plain_lock: PlainLock, + pub dest_plain_lock: PlainLock, + pub dest_payment: u128, + pub total_dest_payment: u128, + pub signature: Signature, +*/ + use toml; #[derive(Debug, From)] @@ -19,6 +31,7 @@ pub enum ReceiptFileError { TomlSeError(toml::ser::Error), SerStringError, ParseDestPaymentError, + ParseTotalDestPaymentError, InvalidPublicKey, } @@ -27,7 +40,10 @@ pub enum ReceiptFileError { pub struct ReceiptFile { pub response_hash: String, pub invoice_id: String, + pub src_plain_lock: String, + pub dest_plain_lock: String, pub dest_payment: String, + pub total_dest_payment: String, pub signature: String, } @@ -44,16 +60,27 @@ pub fn load_receipt_from_file(path: &Path) -> Result let response_hash = string_to_hash_result(&receipt_file.response_hash)?; let invoice_id = string_to_invoice_id(&receipt_file.invoice_id)?; + let src_plain_lock = string_to_plain_lock(&receipt_file.src_plain_lock)?; + let dest_plain_lock = string_to_plain_lock(&receipt_file.dest_plain_lock)?; + let dest_payment = receipt_file .dest_payment .parse() .map_err(|_| ReceiptFileError::ParseDestPaymentError)?; + + let total_dest_payment = receipt_file + .total_dest_payment + .parse() + .map_err(|_| ReceiptFileError::ParseTotalDestPaymentError)?; let signature = string_to_signature(&receipt_file.signature)?; Ok(Receipt { response_hash, invoice_id, + src_plain_lock, + dest_plain_lock, dest_payment, + total_dest_payment, signature, }) } @@ -63,14 +90,20 @@ pub fn store_receipt_to_file(receipt: &Receipt, path: &Path) -> Result<(), Recei let Receipt { ref response_hash, ref invoice_id, + ref src_plain_lock, + ref dest_plain_lock, dest_payment, + total_dest_payment, ref signature, } = receipt; let receipt_file = ReceiptFile { response_hash: hash_result_to_string(&response_hash), invoice_id: invoice_id_to_string(&invoice_id), + src_plain_lock: plain_lock_to_string(&src_plain_lock), + dest_plain_lock: plain_lock_to_string(&dest_plain_lock), dest_payment: dest_payment.to_string(), + total_dest_payment: total_dest_payment.to_string(), signature: signature_to_string(&signature), }; @@ -88,7 +121,7 @@ mod tests { use tempfile::tempdir; use app::invoice::{InvoiceId, INVOICE_ID_LEN}; - use app::{HashResult, Signature, HASH_RESULT_LEN, SIGNATURE_LEN}; + use app::{HashResult, PlainLock, Signature, HASH_RESULT_LEN, PLAIN_LOCK_LEN, SIGNATURE_LEN}; #[test] fn test_receipt_file_basic() { @@ -96,7 +129,10 @@ mod tests { r#" response_hash = 'response_hash' invoice_id = 'invoice_id' + src_plain_lock = 'src_plain_lock' + dest_plain_lock = 'dest_plain_lock' dest_payment = '100' + total_dest_payment = '200' signature = 'signature' "#, ) @@ -117,8 +153,11 @@ mod tests { let receipt = Receipt { response_hash: HashResult::from(&[0; HASH_RESULT_LEN]), invoice_id: InvoiceId::from(&[1; INVOICE_ID_LEN]), + src_plain_lock: PlainLock::from(&[2; PLAIN_LOCK_LEN]), + dest_plain_lock: PlainLock::from(&[3; PLAIN_LOCK_LEN]), dest_payment: 100, - signature: Signature::from(&[2; SIGNATURE_LEN]), + total_dest_payment: 200, + signature: Signature::from(&[4; SIGNATURE_LEN]), }; store_receipt_to_file(&receipt, &file_path).unwrap(); diff --git a/components/stctrl/src/funds.rs b/components/stctrl/src/funds.rs index c5a2f3bf2..8c67f7ec2 100644 --- a/components/stctrl/src/funds.rs +++ b/components/stctrl/src/funds.rs @@ -196,12 +196,14 @@ async fn funds_pay_invoice( unimplemented!(); /* - // Make sure that we will be able to write the receipt + + // Make sure that we will be able to write the Commit // before we do the actual payment: if receipt_file.exists() { return Err(FundsError::ReceiptFileAlreadyExists); } + let invoice = load_invoice_from_file(&invoice_file).map_err(|_| FundsError::LoadInvoiceError)?; From 4f994c2746fd9fbb155c826b6d0bccb2ecf17a47 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 29 May 2019 12:59:50 +0300 Subject: [PATCH 210/248] Removed comment. --- components/stctrl/src/file/receipt.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/components/stctrl/src/file/receipt.rs b/components/stctrl/src/file/receipt.rs index ec85cc039..6a0961213 100644 --- a/components/stctrl/src/file/receipt.rs +++ b/components/stctrl/src/file/receipt.rs @@ -11,17 +11,6 @@ use app::ser_string::{ }; use app::Receipt; -/* - pub response_hash: HashResult, - // = sha512/256(requestId || sha512/256(route) || randNonce) - pub invoice_id: InvoiceId, - pub src_plain_lock: PlainLock, - pub dest_plain_lock: PlainLock, - pub dest_payment: u128, - pub total_dest_payment: u128, - pub signature: Signature, -*/ - use toml; #[derive(Debug, From)] From 3796773c53ca63584b9d1ad382a90621e2db500f Mon Sep 17 00:00:00 2001 From: real Date: Wed, 29 May 2019 13:32:38 +0300 Subject: [PATCH 211/248] offst-app is now exporting Commit and MultiCommit. --- components/app/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/app/src/lib.rs b/components/app/src/lib.rs index 24c4eea0f..cd3970ac8 100644 --- a/components/app/src/lib.rs +++ b/components/app/src/lib.rs @@ -26,7 +26,7 @@ pub use proto::file::relay::load_relay_from_file; pub use proto::file::ser_string; pub use proto::app_server::messages::{AppPermissions, NamedRelayAddress, RelayAddress}; -pub use proto::funder::messages::Receipt; +pub use proto::funder::messages::{Commit, MultiCommit, Receipt}; pub use proto::funder::signature_buff::verify_receipt; pub use proto::index_server::messages::NamedIndexServerAddress; pub use proto::report::signature_buff::verify_move_token_hashed_report; From ff6ee739a661f26ee9535b0e0f267c456cd0b93c Mon Sep 17 00:00:00 2001 From: real Date: Wed, 29 May 2019 13:32:57 +0300 Subject: [PATCH 212/248] Added MultiCommit file in offst-stctrl. --- components/stctrl/src/file/mod.rs | 2 + components/stctrl/src/file/multi_commit.rs | 216 +++++++++++++++++++++ 2 files changed, 218 insertions(+) create mode 100644 components/stctrl/src/file/multi_commit.rs diff --git a/components/stctrl/src/file/mod.rs b/components/stctrl/src/file/mod.rs index d4da9c8b8..5b9634d42 100644 --- a/components/stctrl/src/file/mod.rs +++ b/components/stctrl/src/file/mod.rs @@ -1,3 +1,5 @@ pub mod invoice; +#[allow(unused)] +pub mod multi_commit; pub mod receipt; pub mod token; diff --git a/components/stctrl/src/file/multi_commit.rs b/components/stctrl/src/file/multi_commit.rs new file mode 100644 index 000000000..0a17e938f --- /dev/null +++ b/components/stctrl/src/file/multi_commit.rs @@ -0,0 +1,216 @@ +use std::fs::{self, File}; +use std::io::{self, Write}; +use std::path::Path; + +use std::convert::TryFrom; + +use derive_more::*; + +use app::ser_string::{ + hash_result_to_string, hashed_lock_to_string, invoice_id_to_string, plain_lock_to_string, + signature_to_string, string_to_hash_result, string_to_hashed_lock, string_to_invoice_id, + string_to_plain_lock, string_to_signature, SerStringError, +}; +use app::{Commit, MultiCommit}; + +use toml; + +#[derive(Debug)] +pub struct ParseCommitFileError; + +#[derive(Debug, From)] +pub enum MultiCommitFileError { + IoError(io::Error), + TomlDeError(toml::de::Error), + TomlSeError(toml::ser::Error), + SerStringError, + ParseDestPaymentError, + ParseTotalDestPaymentError, + ParseCommitFileError, + InvalidPublicKey, +} + +impl From for MultiCommitFileError { + fn from(_e: SerStringError) -> Self { + MultiCommitFileError::SerStringError + } +} + +impl From for MultiCommitFileError { + fn from(_e: ParseCommitFileError) -> Self { + MultiCommitFileError::ParseCommitFileError + } +} + +/// Representing a Commit in an easy to serialize representation. +#[derive(Serialize, Deserialize)] +pub struct CommitFile { + pub response_hash: String, + pub dest_payment: String, + pub src_plain_lock: String, + pub dest_hashed_lock: String, + pub signature: String, +} + +impl std::convert::From<&Commit> for CommitFile { + fn from(commit: &Commit) -> Self { + CommitFile { + response_hash: hash_result_to_string(&commit.response_hash), + dest_payment: commit.dest_payment.to_string(), + src_plain_lock: plain_lock_to_string(&commit.src_plain_lock), + dest_hashed_lock: hashed_lock_to_string(&commit.dest_hashed_lock), + signature: signature_to_string(&commit.signature), + } + } +} + +impl std::convert::TryFrom<&CommitFile> for Commit { + type Error = ParseCommitFileError; + + fn try_from(commit: &CommitFile) -> Result { + Ok(Commit { + response_hash: string_to_hash_result(&commit.response_hash) + .map_err(|_| ParseCommitFileError)?, + dest_payment: commit + .dest_payment + .parse() + .map_err(|_| ParseCommitFileError)?, + src_plain_lock: string_to_plain_lock(&commit.src_plain_lock) + .map_err(|_| ParseCommitFileError)?, + dest_hashed_lock: string_to_hashed_lock(&commit.dest_hashed_lock) + .map_err(|_| ParseCommitFileError)?, + signature: string_to_signature(&commit.signature).map_err(|_| ParseCommitFileError)?, + }) + } +} + +/// A helper structure for serialize and deserializing MultiCommit. +#[derive(Serialize, Deserialize)] +pub struct MultiCommitFile { + pub invoice_id: String, + pub total_dest_payment: String, + pub commits: Vec, +} + +/// Load MultiCommit from a file +pub fn load_multi_commit_from_file(path: &Path) -> Result { + let data = fs::read_to_string(&path)?; + let multi_commit_file: MultiCommitFile = toml::from_str(&data)?; + + let invoice_id = string_to_invoice_id(&multi_commit_file.invoice_id)?; + let total_dest_payment = multi_commit_file + .total_dest_payment + .parse() + .map_err(|_| MultiCommitFileError::ParseTotalDestPaymentError)?; + + let mut commits: Vec = Vec::new(); + for commit_file in &multi_commit_file.commits { + commits.push(Commit::try_from(commit_file)?); + } + + Ok(MultiCommit { + invoice_id, + total_dest_payment, + commits, + }) +} + +/// Store MultiCommit to file +pub fn store_multi_commit_to_file( + multi_commit: &MultiCommit, + path: &Path, +) -> Result<(), MultiCommitFileError> { + let MultiCommit { + ref invoice_id, + total_dest_payment, + ref commits, + } = multi_commit; + + let multi_commit_file = MultiCommitFile { + invoice_id: invoice_id_to_string(&invoice_id), + total_dest_payment: total_dest_payment.to_string(), + commits: commits.iter().map(|commit| commit.into()).collect(), + }; + + let data = toml::to_string(&multi_commit_file)?; + + let mut file = File::create(path)?; + file.write_all(&data.as_bytes())?; + + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use tempfile::tempdir; + + use app::invoice::{InvoiceId, INVOICE_ID_LEN}; + use app::{ + HashResult, HashedLock, PlainLock, Signature, HASHED_LOCK_LEN, HASH_RESULT_LEN, + PLAIN_LOCK_LEN, SIGNATURE_LEN, + }; + + #[test] + fn test_multi_commit_file_basic() { + let multi_commit_file: MultiCommitFile = toml::from_str( + r#" + invoice_id = 'invoice_id' + total_dest_payment = '200' + + [[commits]] + response_hash = 'response_hash0' + dest_payment = 'dest_payment0' + src_plain_lock = 'src_plain_lock0' + dest_hashed_lock = 'dest_hashed_lock0' + signature = 'signature0' + + [[commits]] + response_hash = 'response_hash1' + dest_payment = 'dest_payment1' + src_plain_lock = 'src_plain_lock1' + dest_hashed_lock = 'dest_hashed_lock1' + signature = 'signature1' + "#, + ) + .unwrap(); + + assert_eq!(multi_commit_file.invoice_id, "invoice_id"); + assert_eq!(multi_commit_file.total_dest_payment, "200"); + assert_eq!(multi_commit_file.commits[0].response_hash, "response_hash0"); + assert_eq!(multi_commit_file.commits[1].signature, "signature1"); + } + + #[test] + fn test_store_load_multi_commit() { + // Create a temporary directory: + let dir = tempdir().unwrap(); + let file_path = dir.path().join("multi_commit_file"); + + let multi_commit = MultiCommit { + invoice_id: InvoiceId::from(&[1; INVOICE_ID_LEN]), + total_dest_payment: 200, + commits: vec![ + Commit { + response_hash: HashResult::from(&[0u8; HASH_RESULT_LEN]), + dest_payment: 150, + src_plain_lock: PlainLock::from(&[1u8; PLAIN_LOCK_LEN]), + dest_hashed_lock: HashedLock::from(&[2u8; HASHED_LOCK_LEN]), + signature: Signature::from(&[3u8; SIGNATURE_LEN]), + }, + Commit { + response_hash: HashResult::from(&[4u8; HASH_RESULT_LEN]), + dest_payment: 50, + src_plain_lock: PlainLock::from(&[5u8; PLAIN_LOCK_LEN]), + dest_hashed_lock: HashedLock::from(&[6u8; HASHED_LOCK_LEN]), + signature: Signature::from(&[7u8; SIGNATURE_LEN]), + }, + ], + }; + + store_multi_commit_to_file(&multi_commit, &file_path).unwrap(); + let multi_commit2 = load_multi_commit_from_file(&file_path).unwrap(); + + assert_eq!(multi_commit, multi_commit2); + } +} From c56f6c99e53c8536e5e5c09b3b3dabbc390a5a1a Mon Sep 17 00:00:00 2001 From: real Date: Wed, 29 May 2019 13:33:57 +0300 Subject: [PATCH 213/248] Updated serialization test string. --- components/stctrl/src/file/multi_commit.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/stctrl/src/file/multi_commit.rs b/components/stctrl/src/file/multi_commit.rs index 0a17e938f..2357a2533 100644 --- a/components/stctrl/src/file/multi_commit.rs +++ b/components/stctrl/src/file/multi_commit.rs @@ -156,7 +156,7 @@ mod tests { let multi_commit_file: MultiCommitFile = toml::from_str( r#" invoice_id = 'invoice_id' - total_dest_payment = '200' + total_dest_payment = 'total_dest_payment' [[commits]] response_hash = 'response_hash0' @@ -176,7 +176,7 @@ mod tests { .unwrap(); assert_eq!(multi_commit_file.invoice_id, "invoice_id"); - assert_eq!(multi_commit_file.total_dest_payment, "200"); + assert_eq!(multi_commit_file.total_dest_payment, "total_dest_payment"); assert_eq!(multi_commit_file.commits[0].response_hash, "response_hash0"); assert_eq!(multi_commit_file.commits[1].signature, "signature1"); } From 5117f85a3afcc4ab78e8cea2bafb9b31c5e861af Mon Sep 17 00:00:00 2001 From: real Date: Wed, 29 May 2019 13:40:58 +0300 Subject: [PATCH 214/248] Some work on stctrl: funds.rs. Not done yet. --- components/stctrl/src/funds.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/components/stctrl/src/funds.rs b/components/stctrl/src/funds.rs index 8c67f7ec2..3839d7f90 100644 --- a/components/stctrl/src/funds.rs +++ b/components/stctrl/src/funds.rs @@ -33,9 +33,9 @@ pub struct PayInvoiceCmd { /// Path to invoice file to pay #[structopt(parse(from_os_str), short = "i", long = "invoice")] pub invoice_file: PathBuf, - /// Output receipt file - #[structopt(parse(from_os_str), short = "r", long = "receipt")] - pub receipt_file: PathBuf, + /// Output commit file + #[structopt(parse(from_os_str), short = "c", long = "commit")] + pub commit_file: PathBuf, } /// Funds sending related commands @@ -56,6 +56,7 @@ pub enum FundsError { AppRoutesError, SendFundsError, NoSuitableRoute, + CommitFileAlreadyExists, ReceiptFileAlreadyExists, StoreReceiptError, ReceiptAckError, @@ -190,20 +191,16 @@ async fn funds_pay_invoice( ) -> Result<(), FundsError> { let PayInvoiceCmd { invoice_file, - receipt_file, + commit_file, } = pay_invoice_cmd; - unimplemented!(); - - /* - // Make sure that we will be able to write the Commit + // Make sure that we will be able to write the MultiCommit // before we do the actual payment: - if receipt_file.exists() { - return Err(FundsError::ReceiptFileAlreadyExists); + if commit_file.exists() { + return Err(FundsError::CommitFileAlreadyExists); } - let invoice = load_invoice_from_file(&invoice_file).map_err(|_| FundsError::LoadInvoiceError)?; @@ -212,7 +209,7 @@ async fn funds_pay_invoice( // we also need to pay nodes on the way. // We might need to solve this issue at the index server side // (Should the Server take into account the extra credits that should be paid along the way?). - let routes_with_capacity = await!(app_routes.request_multi_routes( + let multi_routes = await!(app_routes.request_routes( invoice.dest_payment, local_public_key, // source invoice.dest_public_key, @@ -220,6 +217,9 @@ async fn funds_pay_invoice( )) // No exclusion of edges .map_err(|_| FundsError::AppRoutesError)?; + unimplemented!(); + + /* let route = choose_route(routes_with_capacity, invoice.dest_payment)?; let fees = route.len().checked_sub(2).unwrap(); From 63878a02907b1ad15f10b2384305bf875f447f7c Mon Sep 17 00:00:00 2001 From: real Date: Wed, 29 May 2019 13:44:47 +0300 Subject: [PATCH 215/248] Added TODO comment. --- components/stctrl/src/funds.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/components/stctrl/src/funds.rs b/components/stctrl/src/funds.rs index 3839d7f90..ec8adf9a6 100644 --- a/components/stctrl/src/funds.rs +++ b/components/stctrl/src/funds.rs @@ -217,6 +217,14 @@ async fn funds_pay_invoice( )) // No exclusion of edges .map_err(|_| FundsError::AppRoutesError)?; + + // TODO: + // - Pick suitable routes and decide how much to pay on every route. + // - Create a new payment + // - Create new transactions (One for every route). On the first failure cancel all + // transactions. Succeed only if all transaction succeed. + // - Create a MultiCommit and save it to file. + unimplemented!(); /* @@ -237,11 +245,6 @@ async fn funds_pay_invoice( writeln!(writer, "Payment successful!").map_err(|_| FundsError::WriteError)?; writeln!(writer, "Fees: {}", fees).map_err(|_| FundsError::WriteError)?; - // Store receipt to file: - store_receipt_to_file(&receipt, &receipt_file).map_err(|_| FundsError::StoreReceiptError)?; - - // We only send the ack if we managed to get the receipt: - await!(app_send_funds.receipt_ack(request_id, receipt)).map_err(|_| FundsError::ReceiptAckError) */ } From 646cb303a5c50bca3b525cf1b2a311de494bb1ff Mon Sep 17 00:00:00 2001 From: real Date: Wed, 29 May 2019 17:01:01 +0300 Subject: [PATCH 216/248] Added initial implementation for choosing multi route amounts. --- Cargo.lock | 2 + components/stctrl/Cargo.toml | 3 + components/stctrl/src/funds.rs | 42 -------- components/stctrl/src/lib.rs | 2 + components/stctrl/src/multi_route_util.rs | 125 ++++++++++++++++++++++ 5 files changed, 132 insertions(+), 42 deletions(-) create mode 100644 components/stctrl/src/multi_route_util.rs diff --git a/Cargo.lock b/Cargo.lock index 6d79d4029..d6dba4a17 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -986,6 +986,8 @@ dependencies = [ "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures-preview 0.3.0-alpha.16 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "num-bigint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "offst-app 0.1.0", "prettytable-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/components/stctrl/Cargo.toml b/components/stctrl/Cargo.toml index 549cedb53..54c71d496 100644 --- a/components/stctrl/Cargo.toml +++ b/components/stctrl/Cargo.toml @@ -32,6 +32,9 @@ structopt = "0.2.15" derive_more = "0.14.0" +num-bigint = "0.2.2" +num-traits = "0.2.6" + [dev_dependencies] tempfile = "3.0.5" diff --git a/components/stctrl/src/funds.rs b/components/stctrl/src/funds.rs index ec8adf9a6..9d7767f87 100644 --- a/components/stctrl/src/funds.rs +++ b/components/stctrl/src/funds.rs @@ -64,48 +64,6 @@ pub enum FundsError { WriteError, } -/// Can we push the given amount of credits through this multi route? -fn is_good_multi_route( - multi_route: &MultiRoute, - mut amount: u128) -> bool { - - let mut credit_count = 0u128; - - for route_capacity_rate in &multi_route.routes { - let max_payable = route_capacity_rate.rate.max_payable(route_capacity_rate.capacity); - credit_count = if let Some(new_credit_count) = credit_count.checked_add(max_payable) { - new_credit_count - } else { - // An overflow happened. This means we can definitely pay `amount`. - return true; - }; - } - - credit_count >= amount -} - -/// Choose a route for pushing `amount` credits -fn choose_multi_route( - multi_routes: Vec, - amount: u128, -) -> Result { - // We naively select the first multi-route we find suitable: - // TODO: Possibly improve this later: - for multi_route in multi_routes { - if is_good_multi_route(&multi_route, amount) { - return Ok(multi_route) - } - } - Err(FundsError::NoSuitableRoute) -} - -/// Find a safe choice for how much credits to push through each route in a MultiRoute (Give that we -/// know this is possible) -/// Returns a vector representing how many credits to push through every route. -fn safe_multi_route_amounts(multi_route: &MultiRoute, amount: u128) -> Option> { - // let max_payables: Vec<_> = multi_route.route.iter().map(|route| route.rate.max_payable(route.capacity)).collect(); - unimplemented!(); -} /* diff --git a/components/stctrl/src/lib.rs b/components/stctrl/src/lib.rs index db06d3d44..cc787bc2a 100644 --- a/components/stctrl/src/lib.rs +++ b/components/stctrl/src/lib.rs @@ -22,6 +22,8 @@ extern crate log; #[macro_use] extern crate serde_derive; +mod multi_route_util; + pub mod config; #[allow(unused)] pub mod funds; diff --git a/components/stctrl/src/multi_route_util.rs b/components/stctrl/src/multi_route_util.rs new file mode 100644 index 000000000..49f1b3006 --- /dev/null +++ b/components/stctrl/src/multi_route_util.rs @@ -0,0 +1,125 @@ +use num_bigint::BigUint; +// use num_traits::identities::Zero; +use num_traits::cast::ToPrimitive; +use num_traits::ops::checked::CheckedSub; + +use app::route::MultiRoute; + +/* +/// Can we push the given amount of credits through this multi route? +fn is_good_multi_route(multi_route: &MultiRoute, mut amount: u128) -> bool { + let mut credit_count = 0u128; + + for route_capacity_rate in &multi_route.routes { + let max_payable = route_capacity_rate + .rate + .max_payable(route_capacity_rate.capacity); + credit_count = if let Some(new_credit_count) = credit_count.checked_add(max_payable) { + new_credit_count + } else { + // An overflow happened. This means we can definitely pay `amount`. + return true; + }; + } + + credit_count >= amount +} +*/ + +pub type MultiRouteChoice = Vec<(usize, u128)>; + +/// Attempt to fill the given amount as much as possible, trying not to saturate any route. +/// Returns `Some((i, added_credits(i)))` if in index `i` we can already fill `amount_left`, or +/// `None` if we can never fill the wanted amount. +fn fill_amount(amount: u128, sorted_routes: &[(Option, u128)]) -> Option<(usize, BigUint)> { + let mut amount_left = BigUint::from(amount); + // Chosen route indices (Indices with respect to the original `routes`) + for i in 1..sorted_routes.len() { + let prev_i = i.checked_sub(1).unwrap(); + // sorted_routes is sorted, therefore we can be sure that the next + // checked_sub(...).unwrap() will not panic: + let added_credits: BigUint = BigUint::from( + sorted_routes[i] + .1 + .checked_sub(sorted_routes[prev_i].1) + .unwrap(), + ) * i; + + if let Some(new_amount_left) = amount_left.checked_sub(&added_credits) { + amount_left = new_amount_left; + } else { + return Some((i, amount_left)); + } + } + None +} + +/// Find a safe choice for how much credits to push through each route in a MultiRoute. +/// Returns a vector representing how many credits to push through every chosen route (if successful). +/// For example: (5usize, 100u128) means: push 100 credits through the route that was given in +/// index 5. +fn safe_multi_route_amounts(multi_route: &MultiRoute, amount: u128) -> Option { + let routes = &multi_route.routes; + let sorted_routes = { + let mut sorted_routes: Vec<_> = routes + .iter() + .enumerate() + .map(|(j, route)| (Some(j), route.rate.max_payable(route.capacity))) + .collect(); + + // Reverse sort: (Largest is first) + sorted_routes.sort_by(|(_, a), (_, b)| b.cmp(a)); + // Add a zero entry in the end: + sorted_routes.push((None, 0u128)); + sorted_routes + }; + + let (num_routes, amount_left) = fill_amount(amount, &sorted_routes[..])?; + // Add this amount to the first `num_routes` biggest entries: + let div_extra = (&amount_left / num_routes).to_u128().unwrap(); + // Add `1` to this amount of elements: + let mod_extra = (&amount_left % num_routes).to_u128().unwrap(); + + let mut chosen_routes = Vec::new(); + let mut accum_credits = 0u128; + for i in (0..num_routes).rev() { + let next_i = i.checked_add(1).unwrap(); + let diff = sorted_routes[next_i] + .1 + .checked_sub(sorted_routes[i].1) + .unwrap(); + accum_credits = accum_credits.checked_add(diff).unwrap(); + + let num_credits = accum_credits.checked_add(div_extra).unwrap(); + let num_credits = if (i as u128) < mod_extra { + num_credits.checked_add(1).unwrap() + } else { + num_credits + }; + chosen_routes.push((sorted_routes[i].0.unwrap(), num_credits)); + } + Some(chosen_routes) +} + +#[allow(unused)] +/// Choose a route for pushing `amount` credits +pub fn choose_multi_route( + multi_routes: &[MultiRoute], + amount: u128, +) -> Option<(usize, MultiRouteChoice)> { + // We naively select the first multi-route we find suitable: + // TODO: Possibly improve this later: + for (i, multi_route) in multi_routes.iter().enumerate() { + if let Some(multi_route_choice) = safe_multi_route_amounts(multi_route, amount) { + return Some((i, multi_route_choice)); + } + } + None +} + +/* +#[cfg(test)] +mod tests { + use super::*; +} +*/ From 7c7350a5f00b4a981dc52f54f1b55ef56125de9f Mon Sep 17 00:00:00 2001 From: real Date: Wed, 29 May 2019 18:53:54 +0300 Subject: [PATCH 217/248] Initial implementation of multi_route_util.rs + one basic test. --- components/app/src/lib.rs | 4 +- components/stctrl/src/multi_route_util.rs | 80 +++++++++++++++++++---- 2 files changed, 71 insertions(+), 13 deletions(-) diff --git a/components/app/src/lib.rs b/components/app/src/lib.rs index cd3970ac8..478bdc83a 100644 --- a/components/app/src/lib.rs +++ b/components/app/src/lib.rs @@ -26,7 +26,7 @@ pub use proto::file::relay::load_relay_from_file; pub use proto::file::ser_string; pub use proto::app_server::messages::{AppPermissions, NamedRelayAddress, RelayAddress}; -pub use proto::funder::messages::{Commit, MultiCommit, Receipt}; +pub use proto::funder::messages::{Commit, MultiCommit, Rate, Receipt}; pub use proto::funder::signature_buff::verify_receipt; pub use proto::index_server::messages::NamedIndexServerAddress; pub use proto::report::signature_buff::verify_move_token_hashed_report; @@ -58,7 +58,7 @@ pub mod invoice { pub mod route { pub use proto::funder::messages::FriendsRoute; - pub use proto::index_server::messages::MultiRoute; + pub use proto::index_server::messages::{MultiRoute, RouteCapacityRate}; } diff --git a/components/stctrl/src/multi_route_util.rs b/components/stctrl/src/multi_route_util.rs index 49f1b3006..a34ec8502 100644 --- a/components/stctrl/src/multi_route_util.rs +++ b/components/stctrl/src/multi_route_util.rs @@ -39,9 +39,9 @@ fn fill_amount(amount: u128, sorted_routes: &[(Option, u128)]) -> Option< // sorted_routes is sorted, therefore we can be sure that the next // checked_sub(...).unwrap() will not panic: let added_credits: BigUint = BigUint::from( - sorted_routes[i] + sorted_routes[prev_i] .1 - .checked_sub(sorted_routes[prev_i].1) + .checked_sub(sorted_routes[i].1) .unwrap(), ) * i; @@ -83,13 +83,7 @@ fn safe_multi_route_amounts(multi_route: &MultiRoute, amount: u128) -> Option Option PublicKey { + PublicKey::from(&[i; PUBLIC_KEY_LEN]) + } + + #[test] + fn test_safe_multi_route_amounts() { + let mut multi_route = MultiRoute { routes: vec![] }; + multi_route.routes.push(RouteCapacityRate { + route: FriendsRoute { + public_keys: vec![pk(0), pk(1), pk(2), pk(3), pk(4)], + }, + capacity: 100u128, + rate: Rate { + add: 1, + mul: 0x12345678, + }, + }); + + multi_route.routes.push(RouteCapacityRate { + route: FriendsRoute { + public_keys: vec![pk(0), pk(5), pk(6), pk(4)], + }, + capacity: 200u128, + rate: Rate { + add: 5, + mul: 0x00100000, + }, + }); + + multi_route.routes.push(RouteCapacityRate { + route: FriendsRoute { + public_keys: vec![pk(0), pk(7), pk(8), pk(9), pk(4)], + }, + capacity: 300u128, + rate: Rate { + add: 20, + mul: 0x20000000, + }, + }); + assert!(safe_multi_route_amounts(&multi_route, 601).is_none()); + + let multi_route_choice = safe_multi_route_amounts(&multi_route, 300).unwrap(); + + let mut total_credits = 0u128; + for route_choice in &multi_route_choice { + let route = &multi_route.routes[route_choice.0]; + // Make sure that we don't take too much: + assert!(route_choice.1 < route.rate.max_payable(route.capacity)); + total_credits = total_credits.checked_add(route_choice.1).unwrap(); + } + assert_eq!(total_credits, 300); + } } -*/ From c4283cd254e98a633d03417d4a604baa40cfbd40 Mon Sep 17 00:00:00 2001 From: real Date: Wed, 29 May 2019 19:15:03 +0300 Subject: [PATCH 218/248] Some work on funds_pay_invoice(). Not done yet. --- components/app/src/gen.rs | 9 ++++ components/stctrl/src/funds.rs | 52 +++++++++++++++-------- components/stctrl/src/multi_route_util.rs | 22 ---------- 3 files changed, 43 insertions(+), 40 deletions(-) diff --git a/components/app/src/gen.rs b/components/app/src/gen.rs index e83011f25..0bbc1645a 100644 --- a/components/app/src/gen.rs +++ b/components/app/src/gen.rs @@ -3,6 +3,7 @@ use crypto::uid::Uid; pub use crypto::uid::UID_LEN; use crypto::invoice_id::InvoiceId; +use crypto::payment_id::PaymentId; use node::connect::{node_connect, NodeConnection}; @@ -21,3 +22,11 @@ pub fn gen_invoice_id() -> InvoiceId { InvoiceId::new(&rng) } + +/// Generate a random PaymentId: +pub fn gen_payment_id() -> PaymentId { + // Obtain secure cryptographic random: + let rng = system_random(); + + PaymentId::new(&rng) +} diff --git a/components/stctrl/src/funds.rs b/components/stctrl/src/funds.rs index 9d7767f87..73cb7ebcb 100644 --- a/components/stctrl/src/funds.rs +++ b/components/stctrl/src/funds.rs @@ -6,11 +6,12 @@ use app::{AppRoutes, AppSendFunds, NodeConnection, PublicKey}; use structopt::StructOpt; -use app::gen::gen_uid; +use app::gen::{gen_uid, gen_payment_id}; use app::invoice::{InvoiceId, INVOICE_ID_LEN}; use app::route::{FriendsRoute, MultiRoute}; use crate::file::invoice::load_invoice_from_file; +use crate::multi_route_util::choose_multi_route; // use crate::file::receipt::store_receipt_to_file; /// Send funds to a remote destination @@ -62,6 +63,7 @@ pub enum FundsError { ReceiptAckError, LoadInvoiceError, WriteError, + CreatePaymentFailed, } @@ -170,35 +172,47 @@ async fn funds_pay_invoice( let multi_routes = await!(app_routes.request_routes( invoice.dest_payment, local_public_key, // source - invoice.dest_public_key, + invoice.dest_public_key.clone(), None )) // No exclusion of edges .map_err(|_| FundsError::AppRoutesError)?; + let (route_index, multi_route_choice) = choose_multi_route(&multi_routes, invoice.dest_payment) + .ok_or(FundsError::NoSuitableRoute)?; + let multi_route = &multi_routes[route_index]; + + // Create a new payment + let payment_id = gen_payment_id(); + await!(app_send_funds.create_payment(payment_id, + invoice.invoice_id.clone(), + invoice.dest_payment.clone(), + invoice.dest_public_key.clone())) + .map_err(|_| FundsError::CreatePaymentFailed)?; + + // TODO: Run all of those `create_transaction`-s at the same time: // TODO: - // - Pick suitable routes and decide how much to pay on every route. - // - Create a new payment // - Create new transactions (One for every route). On the first failure cancel all - // transactions. Succeed only if all transaction succeed. + // transactions. Succeed only if all transactions succeed. + for (route_index, dest_payment) in multi_route_choice { + let route = &multi_route.routes[route_index]; + let request_id = gen_uid(); + await!(app_send_funds.create_transaction( + payment_id.clone(), + request_id, + route.route.clone(), + dest_payment, + route.rate.calc_fee(dest_payment).unwrap(), + )); + } + + // TODO: // - Create a MultiCommit and save it to file. + // - Print back PaymentId, to allow the user get the receipt later. unimplemented!(); /* - let route = choose_route(routes_with_capacity, invoice.dest_payment)?; - let fees = route.len().checked_sub(2).unwrap(); - - // Randomly generate a request id: - let request_id = gen_uid(); - - let receipt = await!(app_send_funds.request_send_funds( - request_id, - route, - invoice.invoice_id, - invoice.dest_payment - )) - .map_err(|_| FundsError::SendFundsError)?; writeln!(writer, "Payment successful!").map_err(|_| FundsError::WriteError)?; writeln!(writer, "Fees: {}", fees).map_err(|_| FundsError::WriteError)?; @@ -206,6 +220,8 @@ async fn funds_pay_invoice( */ } +// TODO: Add a command to check information about payment progress. + pub async fn funds( funds_cmd: FundsCmd, mut node_connection: NodeConnection, diff --git a/components/stctrl/src/multi_route_util.rs b/components/stctrl/src/multi_route_util.rs index a34ec8502..2ef30b543 100644 --- a/components/stctrl/src/multi_route_util.rs +++ b/components/stctrl/src/multi_route_util.rs @@ -5,27 +5,6 @@ use num_traits::ops::checked::CheckedSub; use app::route::MultiRoute; -/* -/// Can we push the given amount of credits through this multi route? -fn is_good_multi_route(multi_route: &MultiRoute, mut amount: u128) -> bool { - let mut credit_count = 0u128; - - for route_capacity_rate in &multi_route.routes { - let max_payable = route_capacity_rate - .rate - .max_payable(route_capacity_rate.capacity); - credit_count = if let Some(new_credit_count) = credit_count.checked_add(max_payable) { - new_credit_count - } else { - // An overflow happened. This means we can definitely pay `amount`. - return true; - }; - } - - credit_count >= amount -} -*/ - pub type MultiRouteChoice = Vec<(usize, u128)>; /// Attempt to fill the given amount as much as possible, trying not to saturate any route. @@ -104,7 +83,6 @@ fn safe_multi_route_amounts(multi_route: &MultiRoute, amount: u128) -> Option Date: Wed, 29 May 2019 19:38:10 +0300 Subject: [PATCH 219/248] Some work on funds_pay_invoice(): Added creation of transactions. Not done yet. --- components/stctrl/src/funds.rs | 72 ++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 17 deletions(-) diff --git a/components/stctrl/src/funds.rs b/components/stctrl/src/funds.rs index 73cb7ebcb..c0a8b4671 100644 --- a/components/stctrl/src/funds.rs +++ b/components/stctrl/src/funds.rs @@ -1,8 +1,10 @@ use std::io; use std::path::PathBuf; +use futures::future::select_all; + use app::ser_string::string_to_public_key; -use app::{AppRoutes, AppSendFunds, NodeConnection, PublicKey}; +use app::{AppRoutes, AppSendFunds, NodeConnection, PublicKey, MultiCommit}; use structopt::StructOpt; @@ -12,6 +14,7 @@ use app::route::{FriendsRoute, MultiRoute}; use crate::file::invoice::load_invoice_from_file; use crate::multi_route_util::choose_multi_route; +use crate::file::multi_commit::store_multi_commit_to_file; // use crate::file::receipt::store_receipt_to_file; /// Send funds to a remote destination @@ -64,6 +67,8 @@ pub enum FundsError { LoadInvoiceError, WriteError, CreatePaymentFailed, + CreateTransactionFailed, + StoreCommitError, } @@ -182,6 +187,16 @@ async fn funds_pay_invoice( .ok_or(FundsError::NoSuitableRoute)?; let multi_route = &multi_routes[route_index]; + + // Calculate total fees: + // TODO: Possibly ask the user if he wants to pay this amount of fees at this point. + let mut total_fees = 0u128; + for (route_index, dest_payment) in &multi_route_choice { + let fee = multi_route.routes[*route_index].rate.calc_fee(*dest_payment).unwrap(); + total_fees = total_fees.checked_add(fee).unwrap(); + } + writeln!(writer, "Total fees: {}", total_fees).map_err(|_| FundsError::WriteError)?; + // Create a new payment let payment_id = gen_payment_id(); await!(app_send_funds.create_payment(payment_id, @@ -194,30 +209,53 @@ async fn funds_pay_invoice( // TODO: // - Create new transactions (One for every route). On the first failure cancel all // transactions. Succeed only if all transactions succeed. - for (route_index, dest_payment) in multi_route_choice { - let route = &multi_route.routes[route_index]; + + let mut fut_list = Vec::new(); + for (route_index, dest_payment) in &multi_route_choice { + let route = &multi_route.routes[*route_index]; let request_id = gen_uid(); - await!(app_send_funds.create_transaction( - payment_id.clone(), - request_id, - route.route.clone(), - dest_payment, - route.rate.calc_fee(dest_payment).unwrap(), - )); + let mut c_app_send_funds = app_send_funds.clone(); + fut_list.push( + // TODO: Possibly a more efficient way than Box::pin? + Box::pin(async move { + await!(c_app_send_funds.create_transaction( + payment_id.clone(), + request_id, + route.route.clone(), + *dest_payment, + route.rate.calc_fee(*dest_payment).unwrap(), + )) + }) + ); } + let mut commits = Vec::new(); + for _ in 0 .. multi_route_choice.len() { + let (output, fut_index, new_fut_list) = await!(select_all(fut_list)); + match output { + Ok(commit) => commits.push(commit), + Err(_) => return Err(FundsError::CreateTransactionFailed), + } + fut_list = new_fut_list; + } + + let multi_commit = MultiCommit { + invoice_id: invoice.invoice_id.clone(), + total_dest_payment: invoice.dest_payment, + commits, + }; + + writeln!(writer, "Payment successful!").map_err(|_| FundsError::WriteError)?; + + // Store MultiCommit to file: + store_multi_commit_to_file(&multi_commit, &commit_file) + .map_err(|_| FundsError::StoreCommitError)?; + // TODO: - // - Create a MultiCommit and save it to file. // - Print back PaymentId, to allow the user get the receipt later. unimplemented!(); - /* - - writeln!(writer, "Payment successful!").map_err(|_| FundsError::WriteError)?; - writeln!(writer, "Fees: {}", fees).map_err(|_| FundsError::WriteError)?; - - */ } // TODO: Add a command to check information about payment progress. From f75fa835b370456636e1b5d87915380582c7e4e6 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 14:34:01 +0300 Subject: [PATCH 220/248] Added printing of payment id in case payment is successful. --- components/proto/src/file/ser_string.rs | 8 ++++++++ components/stctrl/src/funds.rs | 8 +++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/components/proto/src/file/ser_string.rs b/components/proto/src/file/ser_string.rs index 673fe86c2..0fb41da33 100644 --- a/components/proto/src/file/ser_string.rs +++ b/components/proto/src/file/ser_string.rs @@ -4,6 +4,7 @@ use crypto::hash::{HashResult, HASH_RESULT_LEN}; use crypto::hash_lock::{HashedLock, PlainLock, HASHED_LOCK_LEN, PLAIN_LOCK_LEN}; use crypto::identity::{PublicKey, Signature, PUBLIC_KEY_LEN, SIGNATURE_LEN}; use crypto::invoice_id::{InvoiceId, INVOICE_ID_LEN}; +use crypto::payment_id::{PaymentId, PAYMENT_ID_LEN}; // TODO: Possibly remove this module into offst-crypto // Will require the extra base64 dependency in offst-crypto. @@ -63,6 +64,13 @@ str_convert_funcs!( INVOICE_ID_LEN ); +str_convert_funcs!( + payment_id_to_string, + string_to_payment_id, + PaymentId, + PAYMENT_ID_LEN +); + str_convert_funcs!( rand_value_to_string, string_to_rand_value, diff --git a/components/stctrl/src/funds.rs b/components/stctrl/src/funds.rs index c0a8b4671..1bc997ff7 100644 --- a/components/stctrl/src/funds.rs +++ b/components/stctrl/src/funds.rs @@ -3,7 +3,7 @@ use std::path::PathBuf; use futures::future::select_all; -use app::ser_string::string_to_public_key; +use app::ser_string::{string_to_public_key, payment_id_to_string}; use app::{AppRoutes, AppSendFunds, NodeConnection, PublicKey, MultiCommit}; use structopt::StructOpt; @@ -205,7 +205,6 @@ async fn funds_pay_invoice( invoice.dest_public_key.clone())) .map_err(|_| FundsError::CreatePaymentFailed)?; - // TODO: Run all of those `create_transaction`-s at the same time: // TODO: // - Create new transactions (One for every route). On the first failure cancel all // transactions. Succeed only if all transactions succeed. @@ -234,7 +233,9 @@ async fn funds_pay_invoice( let (output, fut_index, new_fut_list) = await!(select_all(fut_list)); match output { Ok(commit) => commits.push(commit), - Err(_) => return Err(FundsError::CreateTransactionFailed), + Err(_) => { + return Err(FundsError::CreateTransactionFailed); + }, } fut_list = new_fut_list; } @@ -246,6 +247,7 @@ async fn funds_pay_invoice( }; writeln!(writer, "Payment successful!").map_err(|_| FundsError::WriteError)?; + writeln!(writer, "Payment id: {:?}", payment_id_to_string(&payment_id)).map_err(|_| FundsError::WriteError)?; // Store MultiCommit to file: store_multi_commit_to_file(&multi_commit, &commit_file) From 3456ca4c80f4099b9cac1b50d1008ab4d1a4f687 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 15:01:04 +0300 Subject: [PATCH 221/248] offst-stctrl: Added PaymentStatusCmd. --- components/app/src/lib.rs | 2 +- components/stctrl/src/funds.rs | 111 ++++++++++++++++++++++++++------- 2 files changed, 88 insertions(+), 25 deletions(-) diff --git a/components/app/src/lib.rs b/components/app/src/lib.rs index 478bdc83a..33f6ce56d 100644 --- a/components/app/src/lib.rs +++ b/components/app/src/lib.rs @@ -26,7 +26,7 @@ pub use proto::file::relay::load_relay_from_file; pub use proto::file::ser_string; pub use proto::app_server::messages::{AppPermissions, NamedRelayAddress, RelayAddress}; -pub use proto::funder::messages::{Commit, MultiCommit, Rate, Receipt}; +pub use proto::funder::messages::{Commit, MultiCommit, PaymentStatus, Rate, Receipt}; pub use proto::funder::signature_buff::verify_receipt; pub use proto::index_server::messages::NamedIndexServerAddress; pub use proto::report::signature_buff::verify_move_token_hashed_report; diff --git a/components/stctrl/src/funds.rs b/components/stctrl/src/funds.rs index 1bc997ff7..5d2e42847 100644 --- a/components/stctrl/src/funds.rs +++ b/components/stctrl/src/funds.rs @@ -3,8 +3,8 @@ use std::path::PathBuf; use futures::future::select_all; -use app::ser_string::{string_to_public_key, payment_id_to_string}; -use app::{AppRoutes, AppSendFunds, NodeConnection, PublicKey, MultiCommit}; +use app::ser_string::{string_to_public_key, payment_id_to_string, string_to_payment_id}; +use app::{AppRoutes, AppSendFunds, NodeConnection, PublicKey, MultiCommit, PaymentStatus}; use structopt::StructOpt; @@ -15,21 +15,8 @@ use app::route::{FriendsRoute, MultiRoute}; use crate::file::invoice::load_invoice_from_file; use crate::multi_route_util::choose_multi_route; use crate::file::multi_commit::store_multi_commit_to_file; -// use crate::file::receipt::store_receipt_to_file; +use crate::file::receipt::store_receipt_to_file; -/// Send funds to a remote destination -#[derive(Clone, Debug, StructOpt)] -pub struct SendFundsCmd { - /// recipient's public key - #[structopt(short = "d", long = "dest")] - pub destination_str: String, - /// Amount of credits to send - #[structopt(short = "a", long = "amount")] - pub dest_payment: u128, - /// Output receipt file - #[structopt(parse(from_os_str), short = "r", long = "receipt")] - pub opt_receipt_file: Option, -} /// Pay an invoice #[derive(Clone, Debug, StructOpt)] @@ -42,12 +29,25 @@ pub struct PayInvoiceCmd { pub commit_file: PathBuf, } +/// Check payment status (And obtain receipt if successful) +#[derive(Clone, Debug, StructOpt)] +pub struct PaymentStatusCmd { + /// Payment identifier + #[structopt(short = "p", long = "payment")] + pub payment_id: String, + /// Output receipt file (in case a receipt is ready) + #[structopt(parse(from_os_str), short = "r", long = "receipt")] + pub receipt_file: PathBuf, +} + + /// Funds sending related commands #[derive(Clone, Debug, StructOpt)] pub enum FundsCmd { /// Pay an invoice (Using an invoice file) #[structopt(name = "pay-invoice")] PayInvoice(PayInvoiceCmd), + PaymentStatus(PaymentStatusCmd), } #[derive(Debug)] @@ -69,6 +69,9 @@ pub enum FundsError { CreatePaymentFailed, CreateTransactionFailed, StoreCommitError, + RequestClosePaymentError, + AckClosePaymentError, + ParsePaymentIdError, } @@ -199,15 +202,17 @@ async fn funds_pay_invoice( // Create a new payment let payment_id = gen_payment_id(); + + writeln!(writer, "Payment id: {:?}", payment_id_to_string(&payment_id)).map_err(|_| FundsError::WriteError)?; + await!(app_send_funds.create_payment(payment_id, invoice.invoice_id.clone(), invoice.dest_payment.clone(), invoice.dest_public_key.clone())) .map_err(|_| FundsError::CreatePaymentFailed)?; - // TODO: - // - Create new transactions (One for every route). On the first failure cancel all - // transactions. Succeed only if all transactions succeed. + // Create new transactions (One for every route). On the first failure cancel all + // transactions. Succeed only if all transactions succeed. let mut fut_list = Vec::new(); for (route_index, dest_payment) in &multi_route_choice { @@ -234,12 +239,15 @@ async fn funds_pay_invoice( match output { Ok(commit) => commits.push(commit), Err(_) => { + let _ = await!(app_send_funds.request_close_payment(payment_id.clone())); return Err(FundsError::CreateTransactionFailed); }, } fut_list = new_fut_list; } + let _ = await!(app_send_funds.request_close_payment(payment_id.clone())); + let multi_commit = MultiCommit { invoice_id: invoice.invoice_id.clone(), total_dest_payment: invoice.dest_payment, @@ -247,20 +255,69 @@ async fn funds_pay_invoice( }; writeln!(writer, "Payment successful!").map_err(|_| FundsError::WriteError)?; - writeln!(writer, "Payment id: {:?}", payment_id_to_string(&payment_id)).map_err(|_| FundsError::WriteError)?; // Store MultiCommit to file: store_multi_commit_to_file(&multi_commit, &commit_file) .map_err(|_| FundsError::StoreCommitError)?; - // TODO: - // - Print back PaymentId, to allow the user get the receipt later. + Ok(()) +} - unimplemented!(); +/// Get the current status of a payment +async fn funds_payment_status( + payment_status_cmd: PaymentStatusCmd, + local_public_key: PublicKey, + mut app_send_funds: AppSendFunds, + writer: &mut impl io::Write, +) -> Result<(), FundsError> { + let PaymentStatusCmd { + payment_id, + receipt_file, + } = payment_status_cmd; + + // Make sure that we will be able to write the receipt + // before we do the actual payment: + if receipt_file.exists() { + return Err(FundsError::ReceiptFileAlreadyExists); + } + + let payment_id = string_to_payment_id(&payment_id) + .map_err(|_| FundsError::ParsePaymentIdError)?; + + let payment_status = await!(app_send_funds.request_close_payment(payment_id.clone())) + .map_err(|_| FundsError::RequestClosePaymentError)?; + + let opt_ack_uid = match payment_status { + PaymentStatus::PaymentNotFound => { + writeln!(writer, "Payment could not be found").map_err(|_| FundsError::WriteError)?; + None + }, + PaymentStatus::InProgress => { + writeln!(writer, "Payment is in progress").map_err(|_| FundsError::WriteError)?; + None + }, + PaymentStatus::Success((receipt, ack_uid)) => { + writeln!(writer, "Payment succeeded. Saving receipt to file.").map_err(|_| FundsError::WriteError)?; + // Store receipt to file: + store_receipt_to_file(&receipt, &receipt_file) + .map_err(|_| FundsError::StoreReceiptError)?; + // Note that we must save the receipt to file before we let the node discard it. + Some(ack_uid) + } + PaymentStatus::Canceled(ack_uid) => { + writeln!(writer, "Payment was canceled.").map_err(|_| FundsError::WriteError)?; + Some(ack_uid) + } + }; + + if let Some(ack_uid) = opt_ack_uid { + await!(app_send_funds.ack_close_payment(payment_id.clone(), ack_uid)) + .map_err(|_| FundsError::AckClosePaymentError)?; + } + Ok(()) } -// TODO: Add a command to check information about payment progress. pub async fn funds( funds_cmd: FundsCmd, @@ -294,6 +351,12 @@ pub async fn funds( app_send_funds, writer, ))?, + FundsCmd::PaymentStatus(payment_status_cmd) => await!(funds_payment_status( + payment_status_cmd, + local_public_key, + app_send_funds, + writer, + ))?, } Ok(()) From f1228b1aa5d373e60f34fa2c955fef521d10bcd7 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 15:01:59 +0300 Subject: [PATCH 222/248] stctrl: Added name to PaymentStatusCmd. --- components/stctrl/src/funds.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/components/stctrl/src/funds.rs b/components/stctrl/src/funds.rs index 5d2e42847..664d83b27 100644 --- a/components/stctrl/src/funds.rs +++ b/components/stctrl/src/funds.rs @@ -47,6 +47,7 @@ pub enum FundsCmd { /// Pay an invoice (Using an invoice file) #[structopt(name = "pay-invoice")] PayInvoice(PayInvoiceCmd), + #[structopt(name = "payment-status")] PaymentStatus(PaymentStatusCmd), } From cf682495b28b8c83739599ef79dfea7749b7671f Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 15:10:12 +0300 Subject: [PATCH 223/248] stctrl: Added VerifyTokenCmd to info subcommand. --- components/stctrl/src/info.rs | 82 ++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/components/stctrl/src/info.rs b/components/stctrl/src/info.rs index 857ea561f..934cb379f 100644 --- a/components/stctrl/src/info.rs +++ b/components/stctrl/src/info.rs @@ -8,9 +8,9 @@ use app::report::{ ChannelStatusReport, FriendReport, FriendStatusReport, NodeReport, RequestsStatusReport, }; use app::ser_string::public_key_to_string; -use app::{store_friend_to_file, AppReport, FriendAddress, NodeConnection, RelayAddress}; +use app::{store_friend_to_file, AppReport, FriendAddress, NodeConnection, RelayAddress, verify_move_token_hashed_report}; -use crate::file::token::store_token_to_file; +use crate::file::token::{store_token_to_file, load_token_from_file}; use crate::utils::friend_public_key_by_name; /// Display local public key (Used as address for sending funds) @@ -40,6 +40,15 @@ pub struct FriendLastTokenCmd { pub output_file: PathBuf, } +/// Verify a token received from a friend. +/// A token is some recent commitment of a friend to the mutual credit balance. +#[derive(Clone, Debug, StructOpt)] +pub struct VerifyTokenCmd { + /// Path of token file + #[structopt(parse(from_os_str), short = "t", long = "token")] + pub token: PathBuf, +} + /// Display balance summary #[derive(Clone, Debug, StructOpt)] pub struct BalanceCmd {} @@ -52,6 +61,7 @@ pub struct ExportTicketCmd { pub output_file: PathBuf, } + #[derive(Clone, Debug, StructOpt)] pub enum InfoCmd { /// Show local public key (Used as address for sending funds) @@ -69,6 +79,9 @@ pub enum InfoCmd { /// Export friend's last token #[structopt(name = "friend-last-token")] FriendLastToken(FriendLastTokenCmd), + /// Verify friend's last token + #[structopt(name = "verify-token")] + VerifyToken(VerifyTokenCmd), /// Show current balance #[structopt(name = "balance")] Balance(BalanceCmd), @@ -87,6 +100,8 @@ pub enum InfoError { MissingLastIncomingMoveToken, StoreLastIncomingMoveTokenError, WriteError, + TokenInvalid, + LoadTokenError, } /// Get a most recently known node report: @@ -313,6 +328,66 @@ pub async fn info_friend_last_token( .map_err(|_| InfoError::StoreLastIncomingMoveTokenError) } +/// Verify a given friend token +/// If the given token is valid, output token details +fn info_verify_token( + verify_token_cmd: VerifyTokenCmd, + writer: &mut impl io::Write, +) -> Result<(), InfoError> { + let move_token_hashed_report = load_token_from_file(&verify_token_cmd.token) + .map_err(|_| InfoError::LoadTokenError)?; + + if verify_move_token_hashed_report( + &move_token_hashed_report, + &move_token_hashed_report.local_public_key, + ) { + writeln!(writer, "Token is valid!").map_err(|_| InfoError::WriteError)?; + writeln!(writer).map_err(|_| InfoError::WriteError)?; + writeln!( + writer, + "local_public_key: {}", + public_key_to_string(&move_token_hashed_report.local_public_key) + ) + .map_err(|_| InfoError::WriteError)?; + writeln!( + writer, + "remote_public_key: {}", + public_key_to_string(&move_token_hashed_report.remote_public_key) + ) + .map_err(|_| InfoError::WriteError)?; + writeln!( + writer, + "inconsistency_counter: {}", + move_token_hashed_report.inconsistency_counter + ) + .map_err(|_| InfoError::WriteError)?; + writeln!(writer, "balance: {}", move_token_hashed_report.balance) + .map_err(|_| InfoError::WriteError)?; + writeln!( + writer, + "move_token_counter: {}", + move_token_hashed_report.move_token_counter + ) + .map_err(|_| InfoError::WriteError)?; + writeln!( + writer, + "local_pending_debt: {}", + move_token_hashed_report.local_pending_debt + ) + .map_err(|_| InfoError::WriteError)?; + writeln!( + writer, + "remote_pending_debt: {}", + move_token_hashed_report.remote_pending_debt + ) + .map_err(|_| InfoError::WriteError)?; + + Ok(()) + } else { + Err(InfoError::TokenInvalid) + } +} + /// Get an approximate value for mutual balance with a friend. /// In case of an inconsistency we take the local reset terms to represent the balance. fn friend_balance(friend_report: &FriendReport) -> i128 { @@ -385,6 +460,9 @@ pub async fn info( InfoCmd::FriendLastToken(friend_last_token_cmd) => { await!(info_friend_last_token(friend_last_token_cmd, app_report))? } + InfoCmd::VerifyToken(verify_token_cmd) => { + info_verify_token(verify_token_cmd, writer)? + } InfoCmd::Balance(_balance_cmd) => await!(info_balance(app_report, writer))?, InfoCmd::ExportTicket(export_ticket_cmd) => { await!(info_export_ticket(export_ticket_cmd, app_report))? From 7f472798dd2e9d73b440c7766ff4231bbaba422c Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 15:17:57 +0300 Subject: [PATCH 224/248] stctrl: Added VerifyReceipt info command. --- components/stctrl/src/info.rs | 56 ++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/components/stctrl/src/info.rs b/components/stctrl/src/info.rs index 934cb379f..e0ef5c6b3 100644 --- a/components/stctrl/src/info.rs +++ b/components/stctrl/src/info.rs @@ -8,9 +8,12 @@ use app::report::{ ChannelStatusReport, FriendReport, FriendStatusReport, NodeReport, RequestsStatusReport, }; use app::ser_string::public_key_to_string; -use app::{store_friend_to_file, AppReport, FriendAddress, NodeConnection, RelayAddress, verify_move_token_hashed_report}; +use app::{store_friend_to_file, AppReport, FriendAddress, NodeConnection, + RelayAddress, verify_move_token_hashed_report, verify_receipt}; use crate::file::token::{store_token_to_file, load_token_from_file}; +use crate::file::receipt::load_receipt_from_file; +use crate::file::invoice::load_invoice_from_file; use crate::utils::friend_public_key_by_name; /// Display local public key (Used as address for sending funds) @@ -61,6 +64,17 @@ pub struct ExportTicketCmd { pub output_file: PathBuf, } +/// Verify receipt file +#[derive(Clone, Debug, StructOpt)] +pub struct VerifyReceiptCmd { + /// Path of invoice file (Locally generated) + #[structopt(parse(from_os_str), short = "i", long = "invoice")] + pub invoice: PathBuf, + /// Path of receipt file (Received from buyer) + #[structopt(parse(from_os_str), short = "r", long = "receipt")] + pub receipt: PathBuf, +} + #[derive(Clone, Debug, StructOpt)] pub enum InfoCmd { @@ -88,6 +102,9 @@ pub enum InfoCmd { /// Export ticket for this node #[structopt(name = "export-ticket")] ExportTicket(ExportTicketCmd), + /// Verify a receipt against an invoice + #[structopt(name = "verify-receipt")] + VerifyReceipt(VerifyReceiptCmd), } #[derive(Debug)] @@ -102,6 +119,11 @@ pub enum InfoError { WriteError, TokenInvalid, LoadTokenError, + LoadInvoiceError, + LoadReceiptError, + InvalidReceipt, + DestPaymentMismatch, + InvoiceIdMismatch, } /// Get a most recently known node report: @@ -445,6 +467,35 @@ pub async fn info_export_ticket( Ok(()) } +/// Verify a given receipt +fn info_verify_receipt( + verify_receipt_cmd: VerifyReceiptCmd, + writer: &mut impl io::Write, +) -> Result<(), InfoError> { + let invoice = load_invoice_from_file(&verify_receipt_cmd.invoice) + .map_err(|_| InfoError::LoadInvoiceError)?; + + let receipt = load_receipt_from_file(&verify_receipt_cmd.receipt) + .map_err(|_| InfoError::LoadReceiptError)?; + + // Make sure that the invoice and receipt files match: + // Verify invoice_id match: + if invoice.invoice_id != receipt.invoice_id { + return Err(InfoError::InvoiceIdMismatch); + } + // Verify dest_payment match: + if invoice.dest_payment != receipt.total_dest_payment { + return Err(InfoError::DestPaymentMismatch); + } + + if verify_receipt(&receipt, &invoice.dest_public_key) { + writeln!(writer, "Receipt is valid!").map_err(|_| InfoError::WriteError)?; + Ok(()) + } else { + Err(InfoError::InvalidReceipt) + } +} + pub async fn info( info_cmd: InfoCmd, mut node_connection: NodeConnection, @@ -467,6 +518,9 @@ pub async fn info( InfoCmd::ExportTicket(export_ticket_cmd) => { await!(info_export_ticket(export_ticket_cmd, app_report))? } + InfoCmd::VerifyReceipt(verify_receipt_cmd) => { + info_verify_receipt(verify_receipt_cmd, writer)? + } } Ok(()) } From 83834e9e73eb10b0c512f26d6e4acf18a3dc707d Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 15:25:29 +0300 Subject: [PATCH 225/248] Renamed stctrl funds subcommand -> buyer subcommand. --- components/stctrl/src/{funds.rs => buyer.rs} | 139 +++++-------------- components/stctrl/src/lib.rs | 4 +- components/stctrl/src/stctrllib.rs | 18 +-- 3 files changed, 44 insertions(+), 117 deletions(-) rename components/stctrl/src/{funds.rs => buyer.rs} (65%) diff --git a/components/stctrl/src/funds.rs b/components/stctrl/src/buyer.rs similarity index 65% rename from components/stctrl/src/funds.rs rename to components/stctrl/src/buyer.rs index 664d83b27..939391171 100644 --- a/components/stctrl/src/funds.rs +++ b/components/stctrl/src/buyer.rs @@ -43,7 +43,7 @@ pub struct PaymentStatusCmd { /// Funds sending related commands #[derive(Clone, Debug, StructOpt)] -pub enum FundsCmd { +pub enum BuyerCmd { /// Pay an invoice (Using an invoice file) #[structopt(name = "pay-invoice")] PayInvoice(PayInvoiceCmd), @@ -52,14 +52,14 @@ pub enum FundsCmd { } #[derive(Debug)] -pub enum FundsError { +pub enum BuyerError { GetReportError, NoFundsPermissions, NoRoutesPermissions, InvalidDestination, ParseAmountError, AppRoutesError, - SendFundsError, + SendBuyerError, NoSuitableRoute, CommitFileAlreadyExists, ReceiptFileAlreadyExists, @@ -77,79 +77,6 @@ pub enum FundsError { -/* -/// Send funds to a remote destination without using an invoice. -async fn funds_send_funds( - send_raw_cmd: SendFundsCmd, - local_public_key: PublicKey, - mut app_routes: AppRoutes, - mut app_send_funds: AppSendFunds, - writer: &mut impl io::Write, -) -> Result<(), FundsError> { - let SendFundsCmd { - destination_str, - dest_payment, - opt_receipt_file, - } = send_raw_cmd; - - // In case the user wants a receipt, make sure that we will be able to write the receipt - // before we do the actual payment: - if let Some(receipt_file) = &opt_receipt_file { - if receipt_file.exists() { - return Err(FundsError::ReceiptFileAlreadyExists); - } - } - - // Destination public key: - let destination = - string_to_public_key(&destination_str).map_err(|_| FundsError::InvalidDestination)?; - - // TODO: We might get routes with the exact capacity, - // but this will not be enough for sending our amount because - // we also need to pay nodes on the way. - // We might need to solve this issue at the index server side - // (Should the Server take into account the extra credits that should be paid along the way?). - let routes_with_capacity = await!(app_routes.request_routes( - dest_payment, - local_public_key, // source - destination, - None - )) // No exclusion of edges - .map_err(|_| FundsError::AppRoutesError)?; - - let multi_route = choose_multi_route(routes_with_capacity, dest_payment)?; - let amounts = safe_multi_route_amounts(&multi_route).unwrap(); - - // TODO: - // - Create Payment - // - Create a transaction for every route - // - Wait until the first a first failure happens or all transactions succeeded. - // - Close payment - // - Return result (MultiCommit or failure) - - // A trivial invoice: - let request_id = gen_uid(); - let invoice_id = InvoiceId::from(&[0; INVOICE_ID_LEN]); - - let receipt = - await!(app_send_funds.request_send_funds(request_id, route, invoice_id, dest_payment)) - .map_err(|_| FundsError::SendFundsError)?; - - writeln!(writer, "Payment successful!").map_err(|_| FundsError::WriteError)?; - writeln!(writer, "Fees: {}", fees).map_err(|_| FundsError::WriteError)?; - - // If the user wanted a receipt, we provide one: - if let Some(receipt_file) = opt_receipt_file { - // Store receipt to file: - store_receipt_to_file(&receipt, &receipt_file) - .map_err(|_| FundsError::StoreReceiptError)?; - } - - // We only send the ack if we managed to get the receipt: - await!(app_send_funds.receipt_ack(request_id, receipt)).map_err(|_| FundsError::ReceiptAckError) -} -*/ - /// Pay an invoice async fn funds_pay_invoice( pay_invoice_cmd: PayInvoiceCmd, @@ -157,7 +84,7 @@ async fn funds_pay_invoice( mut app_routes: AppRoutes, mut app_send_funds: AppSendFunds, writer: &mut impl io::Write, -) -> Result<(), FundsError> { +) -> Result<(), BuyerError> { let PayInvoiceCmd { invoice_file, commit_file, @@ -167,11 +94,11 @@ async fn funds_pay_invoice( // Make sure that we will be able to write the MultiCommit // before we do the actual payment: if commit_file.exists() { - return Err(FundsError::CommitFileAlreadyExists); + return Err(BuyerError::CommitFileAlreadyExists); } let invoice = - load_invoice_from_file(&invoice_file).map_err(|_| FundsError::LoadInvoiceError)?; + load_invoice_from_file(&invoice_file).map_err(|_| BuyerError::LoadInvoiceError)?; // TODO: We might get routes with the exact capacity, // but this will not be enough for sending our amount because @@ -184,11 +111,11 @@ async fn funds_pay_invoice( invoice.dest_public_key.clone(), None )) // No exclusion of edges - .map_err(|_| FundsError::AppRoutesError)?; + .map_err(|_| BuyerError::AppRoutesError)?; let (route_index, multi_route_choice) = choose_multi_route(&multi_routes, invoice.dest_payment) - .ok_or(FundsError::NoSuitableRoute)?; + .ok_or(BuyerError::NoSuitableRoute)?; let multi_route = &multi_routes[route_index]; @@ -199,18 +126,18 @@ async fn funds_pay_invoice( let fee = multi_route.routes[*route_index].rate.calc_fee(*dest_payment).unwrap(); total_fees = total_fees.checked_add(fee).unwrap(); } - writeln!(writer, "Total fees: {}", total_fees).map_err(|_| FundsError::WriteError)?; + writeln!(writer, "Total fees: {}", total_fees).map_err(|_| BuyerError::WriteError)?; // Create a new payment let payment_id = gen_payment_id(); - writeln!(writer, "Payment id: {:?}", payment_id_to_string(&payment_id)).map_err(|_| FundsError::WriteError)?; + writeln!(writer, "Payment id: {:?}", payment_id_to_string(&payment_id)).map_err(|_| BuyerError::WriteError)?; await!(app_send_funds.create_payment(payment_id, invoice.invoice_id.clone(), invoice.dest_payment.clone(), invoice.dest_public_key.clone())) - .map_err(|_| FundsError::CreatePaymentFailed)?; + .map_err(|_| BuyerError::CreatePaymentFailed)?; // Create new transactions (One for every route). On the first failure cancel all // transactions. Succeed only if all transactions succeed. @@ -241,7 +168,7 @@ async fn funds_pay_invoice( Ok(commit) => commits.push(commit), Err(_) => { let _ = await!(app_send_funds.request_close_payment(payment_id.clone())); - return Err(FundsError::CreateTransactionFailed); + return Err(BuyerError::CreateTransactionFailed); }, } fut_list = new_fut_list; @@ -255,11 +182,11 @@ async fn funds_pay_invoice( commits, }; - writeln!(writer, "Payment successful!").map_err(|_| FundsError::WriteError)?; + writeln!(writer, "Payment successful!").map_err(|_| BuyerError::WriteError)?; // Store MultiCommit to file: store_multi_commit_to_file(&multi_commit, &commit_file) - .map_err(|_| FundsError::StoreCommitError)?; + .map_err(|_| BuyerError::StoreCommitError)?; Ok(()) } @@ -270,7 +197,7 @@ async fn funds_payment_status( local_public_key: PublicKey, mut app_send_funds: AppSendFunds, writer: &mut impl io::Write, -) -> Result<(), FundsError> { +) -> Result<(), BuyerError> { let PaymentStatusCmd { payment_id, receipt_file, @@ -279,56 +206,56 @@ async fn funds_payment_status( // Make sure that we will be able to write the receipt // before we do the actual payment: if receipt_file.exists() { - return Err(FundsError::ReceiptFileAlreadyExists); + return Err(BuyerError::ReceiptFileAlreadyExists); } let payment_id = string_to_payment_id(&payment_id) - .map_err(|_| FundsError::ParsePaymentIdError)?; + .map_err(|_| BuyerError::ParsePaymentIdError)?; let payment_status = await!(app_send_funds.request_close_payment(payment_id.clone())) - .map_err(|_| FundsError::RequestClosePaymentError)?; + .map_err(|_| BuyerError::RequestClosePaymentError)?; let opt_ack_uid = match payment_status { PaymentStatus::PaymentNotFound => { - writeln!(writer, "Payment could not be found").map_err(|_| FundsError::WriteError)?; + writeln!(writer, "Payment could not be found").map_err(|_| BuyerError::WriteError)?; None }, PaymentStatus::InProgress => { - writeln!(writer, "Payment is in progress").map_err(|_| FundsError::WriteError)?; + writeln!(writer, "Payment is in progress").map_err(|_| BuyerError::WriteError)?; None }, PaymentStatus::Success((receipt, ack_uid)) => { - writeln!(writer, "Payment succeeded. Saving receipt to file.").map_err(|_| FundsError::WriteError)?; + writeln!(writer, "Payment succeeded. Saving receipt to file.").map_err(|_| BuyerError::WriteError)?; // Store receipt to file: store_receipt_to_file(&receipt, &receipt_file) - .map_err(|_| FundsError::StoreReceiptError)?; + .map_err(|_| BuyerError::StoreReceiptError)?; // Note that we must save the receipt to file before we let the node discard it. Some(ack_uid) } PaymentStatus::Canceled(ack_uid) => { - writeln!(writer, "Payment was canceled.").map_err(|_| FundsError::WriteError)?; + writeln!(writer, "Payment was canceled.").map_err(|_| BuyerError::WriteError)?; Some(ack_uid) } }; if let Some(ack_uid) = opt_ack_uid { await!(app_send_funds.ack_close_payment(payment_id.clone(), ack_uid)) - .map_err(|_| FundsError::AckClosePaymentError)?; + .map_err(|_| BuyerError::AckClosePaymentError)?; } Ok(()) } -pub async fn funds( - funds_cmd: FundsCmd, +pub async fn buyer( + buyer_cmd: BuyerCmd, mut node_connection: NodeConnection, writer: &mut impl io::Write, -) -> Result<(), FundsError> { +) -> Result<(), BuyerError> { // Get our local public key: let mut app_report = node_connection.report().clone(); let (node_report, incoming_mutations) = - await!(app_report.incoming_reports()).map_err(|_| FundsError::GetReportError)?; + await!(app_report.incoming_reports()).map_err(|_| BuyerError::GetReportError)?; // We currently don't need live updates about report mutations: drop(incoming_mutations); @@ -336,23 +263,23 @@ pub async fn funds( let app_send_funds = node_connection .send_funds() - .ok_or(FundsError::NoFundsPermissions)? + .ok_or(BuyerError::NoFundsPermissions)? .clone(); let app_routes = node_connection .routes() - .ok_or(FundsError::NoRoutesPermissions)? + .ok_or(BuyerError::NoRoutesPermissions)? .clone(); - match funds_cmd { - FundsCmd::PayInvoice(pay_invoice_cmd) => await!(funds_pay_invoice( + match buyer_cmd { + BuyerCmd::PayInvoice(pay_invoice_cmd) => await!(funds_pay_invoice( pay_invoice_cmd, local_public_key, app_routes, app_send_funds, writer, ))?, - FundsCmd::PaymentStatus(payment_status_cmd) => await!(funds_payment_status( + BuyerCmd::PaymentStatus(payment_status_cmd) => await!(funds_payment_status( payment_status_cmd, local_public_key, app_send_funds, diff --git a/components/stctrl/src/lib.rs b/components/stctrl/src/lib.rs index cc787bc2a..739cdfaa9 100644 --- a/components/stctrl/src/lib.rs +++ b/components/stctrl/src/lib.rs @@ -24,9 +24,9 @@ extern crate serde_derive; mod multi_route_util; -pub mod config; #[allow(unused)] -pub mod funds; +pub mod buyer; +pub mod config; pub mod info; pub mod file; diff --git a/components/stctrl/src/stctrllib.rs b/components/stctrl/src/stctrllib.rs index ee72a7a68..67e56e2d3 100644 --- a/components/stctrl/src/stctrllib.rs +++ b/components/stctrl/src/stctrllib.rs @@ -6,7 +6,7 @@ use futures::executor::ThreadPool; use structopt::StructOpt; use crate::config::{config, ConfigCmd, ConfigError}; -use crate::funds::{funds, FundsCmd, FundsError}; +use crate::buyer::{buyer, BuyerCmd, BuyerError}; use crate::info::{info, InfoCmd, InfoError}; use app::{connect, identity_from_file, load_node_from_file}; @@ -23,7 +23,7 @@ pub enum StCtrlError { ConnectionError, InfoError(InfoError), ConfigError(ConfigError), - FundsError(FundsError), + BuyerError(BuyerError), } impl From for StCtrlError { @@ -38,9 +38,9 @@ impl From for StCtrlError { } } -impl From for StCtrlError { - fn from(e: FundsError) -> Self { - StCtrlError::FundsError(e) +impl From for StCtrlError { + fn from(e: BuyerError) -> Self { + StCtrlError::BuyerError(e) } } @@ -53,8 +53,8 @@ pub enum StCtrlSubcommand { #[structopt(name = "config")] Config(ConfigCmd), /// Payments and funds related commands - #[structopt(name = "funds")] - Funds(FundsCmd), + #[structopt(name = "buyer")] + Buyer(BuyerCmd), } /// stctrl: offST ConTRoL @@ -114,8 +114,8 @@ pub fn stctrl(st_ctrl_cmd: StCtrlCmd, writer: &mut impl io::Write) -> Result<(), match subcommand { StCtrlSubcommand::Info(info_cmd) => await!(info(info_cmd, node_connection, writer))?, StCtrlSubcommand::Config(config_cmd) => await!(config(config_cmd, node_connection))?, - StCtrlSubcommand::Funds(funds_cmd) => { - await!(funds(funds_cmd, node_connection, writer))? + StCtrlSubcommand::Buyer(buyer_cmd) => { + await!(buyer(buyer_cmd, node_connection, writer))? } } Ok(()) From 68fd2472288110559dfdef642cb77154fecfb886 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 15:36:11 +0300 Subject: [PATCH 226/248] stctrl: Initial work on seller subcommand. --- components/stctrl/src/lib.rs | 2 ++ components/stctrl/src/seller.rs | 53 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 components/stctrl/src/seller.rs diff --git a/components/stctrl/src/lib.rs b/components/stctrl/src/lib.rs index 739cdfaa9..83ce68b95 100644 --- a/components/stctrl/src/lib.rs +++ b/components/stctrl/src/lib.rs @@ -28,6 +28,8 @@ mod multi_route_util; pub mod buyer; pub mod config; pub mod info; +#[allow(unused)] +pub mod seller; pub mod file; pub mod utils; diff --git a/components/stctrl/src/seller.rs b/components/stctrl/src/seller.rs new file mode 100644 index 000000000..31198e7a9 --- /dev/null +++ b/components/stctrl/src/seller.rs @@ -0,0 +1,53 @@ +use std::path::PathBuf; + +use structopt::StructOpt; + +/// Create invoice +#[derive(Clone, Debug, StructOpt)] +pub struct CreateInvoiceCmd { + /// Payment recipient's public key (In base 64) + #[structopt(short = "p", long = "pubkey")] + pub public_key: String, + /// Amount of credits to pay (A non negative integer) + #[structopt(short = "a", long = "amount")] + pub amount: u128, + /// Path of output invoice file + #[structopt(parse(from_os_str), short = "o", long = "output")] + pub output: PathBuf, +} + +/// Cancel invoice +#[derive(Clone, Debug, StructOpt)] +pub struct CancelInvoiceCmd { + /// Path to invoice file + #[structopt(parse(from_os_str), short = "i", long = "invoice")] + pub invoice_file: PathBuf, +} + +/// Commit invoice (using a Commit message from buyer) +#[derive(Clone, Debug, StructOpt)] +pub struct CommitInvoiceCmd { + /// Path to invoice file + #[structopt(parse(from_os_str), short = "i", long = "invoice")] + pub invoice_file: PathBuf, + /// Path to commit file + #[structopt(parse(from_os_str), short = "c", long = "commit")] + pub commit_file: PathBuf, +} + +/// Funds sending related commands +#[derive(Clone, Debug, StructOpt)] +pub enum SellerCmd { + /// Create a new invoice (to receive payment) + #[structopt(name = "create-invoice")] + CreateInvoice(CreateInvoiceCmd), + /// Cancel an invoice + #[structopt(name = "cancel-invoice")] + CancelInvoice(CancelInvoiceCmd), + /// Commit an invoice (Using a Commit message from buyer) + #[structopt(name = "commit-invoice")] + CommitInvoice(CommitInvoiceCmd), +} + +#[derive(Debug)] +pub enum SellerError {} From d71bdf4311d30b2b04de69b78c9c8bd34d37c3b0 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 15:42:43 +0300 Subject: [PATCH 227/248] stctrl: Initial skeleton of seller() implementation. --- components/stctrl/src/buyer.rs | 8 +++--- components/stctrl/src/seller.rs | 48 ++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/components/stctrl/src/buyer.rs b/components/stctrl/src/buyer.rs index 939391171..8f5cca096 100644 --- a/components/stctrl/src/buyer.rs +++ b/components/stctrl/src/buyer.rs @@ -78,7 +78,7 @@ pub enum BuyerError { /// Pay an invoice -async fn funds_pay_invoice( +async fn buyer_pay_invoice( pay_invoice_cmd: PayInvoiceCmd, local_public_key: PublicKey, mut app_routes: AppRoutes, @@ -192,7 +192,7 @@ async fn funds_pay_invoice( } /// Get the current status of a payment -async fn funds_payment_status( +async fn buyer_payment_status( payment_status_cmd: PaymentStatusCmd, local_public_key: PublicKey, mut app_send_funds: AppSendFunds, @@ -272,14 +272,14 @@ pub async fn buyer( .clone(); match buyer_cmd { - BuyerCmd::PayInvoice(pay_invoice_cmd) => await!(funds_pay_invoice( + BuyerCmd::PayInvoice(pay_invoice_cmd) => await!(buyer_pay_invoice( pay_invoice_cmd, local_public_key, app_routes, app_send_funds, writer, ))?, - BuyerCmd::PaymentStatus(payment_status_cmd) => await!(funds_payment_status( + BuyerCmd::PaymentStatus(payment_status_cmd) => await!(buyer_payment_status( payment_status_cmd, local_public_key, app_send_funds, diff --git a/components/stctrl/src/seller.rs b/components/stctrl/src/seller.rs index 31198e7a9..e92b237d6 100644 --- a/components/stctrl/src/seller.rs +++ b/components/stctrl/src/seller.rs @@ -1,4 +1,7 @@ use std::path::PathBuf; +use std::io; + +use app::{NodeConnection, PublicKey, AppSendFunds}; use structopt::StructOpt; @@ -50,4 +53,47 @@ pub enum SellerCmd { } #[derive(Debug)] -pub enum SellerError {} +pub enum SellerError { + GetReportError, + NoFundsPermissions, +} + +async fn seller_create_invoice(create_invoice_cmd: CreateInvoiceCmd, + local_public_key: PublicKey, + mut app_send_funds: AppSendFunds, + writer: &mut impl io::Write) -> Result<(), SellerError> { + unimplemented!(); +} + +pub async fn seller( + seller_cmd: SellerCmd, + mut node_connection: NodeConnection, + writer: &mut impl io::Write, +) -> Result<(), SellerError> { + // Get our local public key: + let mut app_report = node_connection.report().clone(); + let (node_report, incoming_mutations) = + await!(app_report.incoming_reports()).map_err(|_| SellerError::GetReportError)?; + // We currently don't need live updates about report mutations: + drop(incoming_mutations); + + let local_public_key = node_report.funder_report.local_public_key.clone(); + + let app_send_funds = node_connection + .send_funds() + .ok_or(SellerError::NoFundsPermissions)? + .clone(); + + match seller_cmd { + SellerCmd::CreateInvoice(create_invoice_cmd) => await!(seller_create_invoice( + create_invoice_cmd, + local_public_key, + app_send_funds, + writer, + ))?, + SellerCmd::CancelInvoice(_create_invoice_cmd) => unimplemented!(), + SellerCmd::CommitInvoice(_commit_invoice_cmd) => unimplemented!(), + } + + Ok(()) +} From d88508b7ada76db59ee14ac04366917bc7676972 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 15:43:09 +0300 Subject: [PATCH 228/248] cargo fmt. --- components/app_server/src/server.rs | 77 ++++++++++++------- .../src/tests/request_send_funds.rs | 12 +-- components/funder/src/funder.rs | 1 - components/funder/src/handler/handler.rs | 5 +- components/funder/src/handler/sender.rs | 50 ++++++------ .../funder/src/handler/tests/pair_basic.rs | 43 ++++------- components/funder/src/tests/tests.rs | 77 +++++++++---------- components/funder/src/tests/utils.rs | 30 +++----- components/funder/src/types.rs | 20 ++--- components/index_client/src/seq_friends.rs | 8 +- components/index_client/src/single_client.rs | 9 ++- components/index_client/src/tests.rs | 10 +-- .../index_server/src/graph/graph_service.rs | 46 +++++------ components/index_server/src/server.rs | 14 ++-- components/node/src/connect/connect.rs | 3 - .../node_connection/node_connection.rs | 31 ++++---- components/stctrl/src/buyer.rs | 59 +++++++------- components/stctrl/src/info.rs | 19 +++-- components/stctrl/src/seller.rs | 14 ++-- components/stctrl/src/stctrllib.rs | 2 +- 20 files changed, 267 insertions(+), 263 deletions(-) diff --git a/components/app_server/src/server.rs b/components/app_server/src/server.rs index cd4c3cfa7..1e3526618 100644 --- a/components/app_server/src/server.rs +++ b/components/app_server/src/server.rs @@ -9,8 +9,8 @@ use futures::{future, stream, Sink, SinkExt, Stream, StreamExt}; use common::conn::ConnPair; use common::select_streams::{select_streams, BoxStream}; // use common::mutable_state::MutableState; -use crypto::uid::Uid; use crypto::payment_id::PaymentId; +use crypto::uid::Uid; use proto::funder::messages::{ FriendStatus, FunderControl, FunderIncomingControl, FunderOutgoingControl, RemoveFriend, @@ -209,32 +209,37 @@ where match funder_message { FunderOutgoingControl::TransactionResult(transaction_result) => { // Find the app that issued the request, and forward the response to this app: - let app_id = if let Some(app_id) = self.transactions.remove(&transaction_result.request_id) { + let app_id = if let Some(app_id) = + self.transactions.remove(&transaction_result.request_id) + { app_id } else { warn!("TransactionResult: Could not find app that initiated CreateTransaction"); return Ok(()); }; if let Some(app) = self.apps.get_mut(&app_id) { - await!( - app.send(AppServerToApp::TransactionResult(transaction_result.clone())) - ); + await!(app.send(AppServerToApp::TransactionResult( + transaction_result.clone() + ))); } - }, + } FunderOutgoingControl::ResponseClosePayment(response_close_payment) => { // Find the app that issued the request, and forward the response to this app: - let app_id = if let Some(app_id) = self.close_payment_requests.remove(&response_close_payment.payment_id) { + let app_id = if let Some(app_id) = self + .close_payment_requests + .remove(&response_close_payment.payment_id) + { app_id } else { warn!("ResponseClosePayment: Could not find app that initiated RequestClosePayment"); return Ok(()); }; if let Some(app) = self.apps.get_mut(&app_id) { - await!( - app.send(AppServerToApp::ResponseClosePayment(response_close_payment.clone())) - ); + await!(app.send(AppServerToApp::ResponseClosePayment( + response_close_payment.clone() + ))); } - }, + } FunderOutgoingControl::ReportMutations(funder_report_mutations) => { let mut index_mutations = Vec::new(); for funder_report_mutation in &funder_report_mutations.mutations { @@ -296,10 +301,15 @@ where } IndexClientToAppServer::ResponseRoutes(client_response_routes) => { // We search for the app that issued the request, and send it the response. - let app_id = if let Some(app_id) = self.route_requests.remove(&client_response_routes.request_id) { + let app_id = if let Some(app_id) = self + .route_requests + .remove(&client_response_routes.request_id) + { app_id } else { - warn!("ResponseRoutes: Could not find the app that issued RequestRoutes request"); + warn!( + "ResponseRoutes: Could not find the app that issued RequestRoutes request" + ); return Ok(()); }; @@ -353,27 +363,37 @@ where FunderIncomingControl::new(app_request_id, FunderControl::RemoveRelay(public_key)) )) .map_err(|_| AppServerError::SendToFunderError), - AppRequest::CreatePayment(create_payment) => await!(self.to_funder.send( - FunderIncomingControl::new(app_request_id, FunderControl::CreatePayment(create_payment)) - )) - .map_err(|_| AppServerError::SendToFunderError), + AppRequest::CreatePayment(create_payment) => { + await!(self.to_funder.send(FunderIncomingControl::new( + app_request_id, + FunderControl::CreatePayment(create_payment) + ))) + .map_err(|_| AppServerError::SendToFunderError) + } AppRequest::CreateTransaction(create_transaction) => { // Keep track of which application issued this request: - self.transactions.insert(create_transaction.request_id.clone(), app_id); + self.transactions + .insert(create_transaction.request_id.clone(), app_id); await!(self.to_funder.send(FunderIncomingControl::new( app_request_id, FunderControl::CreateTransaction(create_transaction) ))) .map_err(|_| AppServerError::SendToFunderError) } - AppRequest::RequestClosePayment(request_close_payment) => await!(self.to_funder.send( - FunderIncomingControl::new(app_request_id, FunderControl::RequestClosePayment(request_close_payment)) - )) - .map_err(|_| AppServerError::SendToFunderError), - AppRequest::AckClosePayment(ack_close_payment) => await!(self.to_funder.send( - FunderIncomingControl::new(app_request_id, FunderControl::AckClosePayment(ack_close_payment)) - )) - .map_err(|_| AppServerError::SendToFunderError), + AppRequest::RequestClosePayment(request_close_payment) => { + await!(self.to_funder.send(FunderIncomingControl::new( + app_request_id, + FunderControl::RequestClosePayment(request_close_payment) + ))) + .map_err(|_| AppServerError::SendToFunderError) + } + AppRequest::AckClosePayment(ack_close_payment) => { + await!(self.to_funder.send(FunderIncomingControl::new( + app_request_id, + FunderControl::AckClosePayment(ack_close_payment) + ))) + .map_err(|_| AppServerError::SendToFunderError) + } AppRequest::AddFriend(add_friend) => await!(self.to_funder.send( FunderIncomingControl::new(app_request_id, FunderControl::AddFriend(add_friend)) )) @@ -467,7 +487,10 @@ where } AppRequest::RequestRoutes(request_routes) => { // Keep track of which application issued this request: - if let Some(_) = self.route_requests.insert(request_routes.request_id.clone(), app_id) { + if let Some(_) = self + .route_requests + .insert(request_routes.request_id.clone(), app_id) + { warn!("RequestRoutes: request_id clash."); } await!(self diff --git a/components/app_server/src/tests/request_send_funds.rs b/components/app_server/src/tests/request_send_funds.rs index a22c793a9..8127e903f 100644 --- a/components/app_server/src/tests/request_send_funds.rs +++ b/components/app_server/src/tests/request_send_funds.rs @@ -5,13 +5,13 @@ use futures::{SinkExt, StreamExt}; use crypto::identity::{PublicKey, PUBLIC_KEY_LEN}; use crypto::invoice_id::{InvoiceId, INVOICE_ID_LEN}; -use crypto::uid::{Uid, UID_LEN}; use crypto::payment_id::{PaymentId, PAYMENT_ID_LEN}; +use crypto::uid::{Uid, UID_LEN}; use proto::app_server::messages::{AppPermissions, AppRequest, AppServerToApp, AppToAppServer}; use proto::funder::messages::{ - FriendsRoute, FunderControl, FunderOutgoingControl, CreatePayment, CreateTransaction, - TransactionResult, RequestResult, + CreatePayment, CreateTransaction, FriendsRoute, FunderControl, FunderOutgoingControl, + RequestResult, TransactionResult, }; use super::utils::spawn_dummy_app_server; @@ -115,7 +115,8 @@ where request_id: Uid::from(&[2; UID_LEN]), result: RequestResult::Failure, }; - await!(funder_sender.send(FunderOutgoingControl::TransactionResult(transaction_result))).unwrap(); + await!(funder_sender.send(FunderOutgoingControl::TransactionResult(transaction_result))) + .unwrap(); // We shouldn't get an message at any of the apps: assert!(app_receiver0.try_next().is_err()); @@ -148,7 +149,8 @@ where request_id: Uid::from(&[3; UID_LEN]), result: RequestResult::Failure, }; - await!(funder_sender.send(FunderOutgoingControl::TransactionResult(transaction_result))).unwrap(); + await!(funder_sender.send(FunderOutgoingControl::TransactionResult(transaction_result))) + .unwrap(); // We shouldn't get an message at any of the apps: assert!(app_receiver0.try_next().is_err()); diff --git a/components/funder/src/funder.rs b/components/funder/src/funder.rs index 864d7c900..863c70f6b 100644 --- a/components/funder/src/funder.rs +++ b/components/funder/src/funder.rs @@ -124,7 +124,6 @@ where ephemeral.mutate(mutation); } - // Send outgoing communication messages: let mut comm_stream = stream::iter::<_>(handler_output.outgoing_comms); await!(comm_sender.send_all(&mut comm_stream)).map_err(|_| FunderError::SendCommError)?; diff --git a/components/funder/src/handler/handler.rs b/components/funder/src/handler/handler.rs index 80b453eb5..6c85715b6 100644 --- a/components/funder/src/handler/handler.rs +++ b/components/funder/src/handler/handler.rs @@ -18,7 +18,7 @@ use crate::handler::handle_friend::{handle_friend_message, HandleFriendError}; use crate::handler::handle_init::handle_init; use crate::handler::handle_liveness::{handle_liveness_message, HandleLivenessError}; use crate::handler::sender::{create_friend_messages, SendCommands}; -use crate::handler::state_wrap::{MutableFunderState, MutableEphemeral}; +use crate::handler::state_wrap::{MutableEphemeral, MutableFunderState}; use crate::ephemeral::{Ephemeral, EphemeralMutation}; use crate::report::{ephemeral_mutation_to_report_mutations, funder_mutation_to_report_mutations}; @@ -41,7 +41,6 @@ where pub outgoing_control: Vec>, } - type FunderHandleIncomingOutput = ( SendCommands, Vec>, @@ -91,7 +90,7 @@ where FunderIncoming::Comm(incoming_comm) => { match incoming_comm { - FunderIncomingComm::Liveness(liveness_message) => handle_liveness_message::( + FunderIncomingComm::Liveness(liveness_message) => handle_liveness_message::( &mut m_state, &mut m_ephemeral, &mut send_commands, diff --git a/components/funder/src/handler/sender.rs b/components/funder/src/handler/sender.rs index 26c5095b1..44688cc70 100644 --- a/components/funder/src/handler/sender.rs +++ b/components/funder/src/handler/sender.rs @@ -9,19 +9,19 @@ use crypto::identity::PublicKey; use proto::app_server::messages::RelayAddress; use proto::funder::messages::{ ChannelerUpdateFriend, FriendMessage, FriendTcOp, FunderOutgoingControl, MoveTokenRequest, - RequestsStatus, TransactionResult, RequestResult + RequestResult, RequestsStatus, TransactionResult, }; use identity::IdentityClient; use crate::mutual_credit::outgoing::{OutgoingMc, QueueOperationError}; use crate::types::{ - create_pending_transaction, - create_unsigned_move_token, sign_move_token, ChannelerConfig, create_cancel_send_funds, + create_cancel_send_funds, create_pending_transaction, create_unsigned_move_token, + sign_move_token, ChannelerConfig, }; use crate::friend::{ - ChannelInconsistent, ChannelStatus, FriendMutation, BackwardsOp, SentLocalRelays, + BackwardsOp, ChannelInconsistent, ChannelStatus, FriendMutation, SentLocalRelays, }; use crate::token_channel::{SetDirection, TcDirection, TcMutation, TokenChannel}; @@ -470,7 +470,9 @@ where // The friend with public key `origin_public_key` is the origin of this request. // We send him back a Cancel message: let pending_transaction = create_pending_transaction(request_send_funds); - let cancel_send_funds = BackwardsOp::Cancel(create_cancel_send_funds(pending_transaction.request_id.clone())); + let cancel_send_funds = BackwardsOp::Cancel(create_cancel_send_funds( + pending_transaction.request_id.clone(), + )); let friend_mutation = FriendMutation::PushBackPendingBackwardsOp(cancel_send_funds); let funder_mutation = FunderMutation::FriendMutation((origin_public_key.clone(), friend_mutation)); @@ -491,13 +493,15 @@ where Ok(()) } -fn backwards_op_to_friend_tc_op( - backwards_op: BackwardsOp, -) -> FriendTcOp { +fn backwards_op_to_friend_tc_op(backwards_op: BackwardsOp) -> FriendTcOp { match backwards_op { - BackwardsOp::Response(response_send_funds) => FriendTcOp::ResponseSendFunds(response_send_funds), + BackwardsOp::Response(response_send_funds) => { + FriendTcOp::ResponseSendFunds(response_send_funds) + } BackwardsOp::Cancel(cancel_send_funds) => FriendTcOp::CancelSendFunds(cancel_send_funds), - BackwardsOp::Collect(collect_send_funds) => FriendTcOp::CollectSendFunds(collect_send_funds), + BackwardsOp::Collect(collect_send_funds) => { + FriendTcOp::CollectSendFunds(collect_send_funds) + } } } @@ -590,7 +594,7 @@ where pending_move_token, cancel_public_keys, outgoing_control, - &operation + &operation, )?; } @@ -618,7 +622,7 @@ where pending_move_token, cancel_public_keys, outgoing_control, - &friend_op + &friend_op, )?; } @@ -627,16 +631,14 @@ where // TODO: Possibly replace this clone with something more efficient later: let mut pending_backwards_ops = friend.pending_backwards_ops.clone(); while let Some(pending_backwards_op) = pending_backwards_ops.pop_front() { - let pending_op = backwards_op_to_friend_tc_op( - pending_backwards_op, - ); + let pending_op = backwards_op_to_friend_tc_op(pending_backwards_op); queue_operation_or_cancel( m_state, pending_move_token, // Not required here, as no requests are being sent. cancel_public_keys, outgoing_control, - &pending_op + &pending_op, )?; let friend_mutation = FriendMutation::PopFrontPendingBackwardsOp; @@ -657,7 +659,7 @@ where pending_move_token, cancel_public_keys, outgoing_control, - &pending_op + &pending_op, )?; let friend_mutation = FriendMutation::PopFrontPendingRequest; let funder_mutation = @@ -676,7 +678,7 @@ where pending_move_token, cancel_public_keys, outgoing_control, - &pending_op + &pending_op, )?; let friend_mutation = FriendMutation::PopFrontPendingUserRequest; let funder_mutation = @@ -700,9 +702,7 @@ where // TODO: Possibly replace this clone with something more efficient later: let mut pending_backwards_ops = friend.pending_backwards_ops.clone(); while let Some(pending_backwards_op) = pending_backwards_ops.pop_front() { - let pending_op = backwards_op_to_friend_tc_op( - pending_backwards_op, - ); + let pending_op = backwards_op_to_friend_tc_op(pending_backwards_op); // TODO: Find a more elegant way to do this: let mut dummy_cancel_public_keys = HashSet::new(); let mut dummy_outgoing_control = Vec::new(); @@ -711,7 +711,7 @@ where pending_move_token, &mut dummy_cancel_public_keys, &mut dummy_outgoing_control, - &pending_op + &pending_op, )?; let friend_mutation = FriendMutation::PopFrontPendingBackwardsOp; @@ -898,11 +898,7 @@ where // Second iteration (Attempt to queue Cancel-s created in the first iteration): for (friend_public_key, pending_move_token) in &mut pending_move_tokens { assert!(ephemeral.liveness.is_online(&friend_public_key)); - let _ = append_cancels_to_move_token( - m_state, - friend_public_key, - pending_move_token, - ); + let _ = append_cancels_to_move_token(m_state, friend_public_key, pending_move_token); } // Send all pending move tokens: diff --git a/components/funder/src/handler/tests/pair_basic.rs b/components/funder/src/handler/tests/pair_basic.rs index d6444892b..c17431fbb 100644 --- a/components/funder/src/handler/tests/pair_basic.rs +++ b/components/funder/src/handler/tests/pair_basic.rs @@ -11,15 +11,15 @@ use identity::{create_identity, IdentityClient}; use crypto::crypto_rand::RngContainer; use crypto::identity::{compare_public_key, generate_pkcs8_key_pair, SoftwareEd25519Identity}; use crypto::invoice_id::{InvoiceId, INVOICE_ID_LEN}; +use crypto::payment_id::{PaymentId, PAYMENT_ID_LEN}; use crypto::test_utils::DummyRandom; use crypto::uid::{Uid, UID_LEN}; -use crypto::payment_id::{PaymentId, PAYMENT_ID_LEN}; use proto::funder::messages::{ - AddFriend, FriendMessage, FriendStatus, FriendsRoute, FunderControl, FunderIncomingControl, - RequestsStatus, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, - CreatePayment, AddInvoice, CreateTransaction, RequestResult, FunderOutgoingControl, - MultiCommit, AckClosePayment, PaymentStatus, + AckClosePayment, AddFriend, AddInvoice, CreatePayment, CreateTransaction, FriendMessage, + FriendStatus, FriendsRoute, FunderControl, FunderIncomingControl, FunderOutgoingControl, + MultiCommit, PaymentStatus, RequestResult, RequestsStatus, SetFriendRemoteMaxDebt, + SetFriendStatus, SetRequestsStatus, }; use crate::ephemeral::Ephemeral; @@ -415,7 +415,6 @@ async fn task_handler_pair_basic<'a>( }; assert_eq!(local_max_debt, 100); - // Node1 opens an invoice (To get payment from Node2): let add_invoice = AddInvoice { invoice_id: InvoiceId::from(&[1u8; INVOICE_ID_LEN]), @@ -440,7 +439,6 @@ async fn task_handler_pair_basic<'a>( // SetNumOpenInvoices(1): assert_eq!(outgoing_control.len(), 1); - // Node2 opens payment (to Node1) according to the invoice from Node1: let create_payment = CreatePayment { payment_id: PaymentId::from(&[3u8; PAYMENT_ID_LEN]), @@ -467,7 +465,6 @@ async fn task_handler_pair_basic<'a>( // SetNumPayments(1): assert_eq!(outgoing_control.len(), 1); - // Node2 creates a transaction to send funds to Node1: let create_transaction = CreateTransaction { payment_id: PaymentId::from(&[3u8; PAYMENT_ID_LEN]), @@ -505,7 +502,7 @@ async fn task_handler_pair_basic<'a>( // We expect failure, because remote side is not ready: assert_eq!(transaction_result.request_id, Uid::from(&[0; UID_LEN])); match transaction_result.result { - RequestResult::Failure => {}, + RequestResult::Failure => {} _ => unreachable!(), }; @@ -524,7 +521,6 @@ async fn task_handler_pair_basic<'a>( RequestsStatus::Closed ); - // Node1 gets a control message to declare his requests are open, // However, Node1 doesn't have the token at this moment. let set_requests_status = SetRequestsStatus { @@ -545,7 +541,6 @@ async fn task_handler_pair_basic<'a>( ))) .unwrap(); - // Node1 will request the token: assert_eq!(outgoing_comms.len(), 1); let friend_message = @@ -555,7 +550,6 @@ async fn task_handler_pair_basic<'a>( unreachable!(); }; - // Node2 receives the request_token message: let funder_incoming = FunderIncoming::Comm(FunderIncomingComm::Friend((pk1.clone(), friend_message))); @@ -568,7 +562,6 @@ async fn task_handler_pair_basic<'a>( ))) .unwrap(); - assert_eq!(outgoing_comms.len(), 1); let friend_message = if let FunderOutgoingComm::FriendMessage((_pk, friend_message)) = &outgoing_comms[0] { @@ -577,7 +570,6 @@ async fn task_handler_pair_basic<'a>( unreachable!(); }; - // Node1 receives the token from Node2: let funder_incoming = FunderIncoming::Comm(FunderIncomingComm::Friend((pk2.clone(), friend_message))); @@ -598,7 +590,6 @@ async fn task_handler_pair_basic<'a>( unreachable!(); }; - // Node2 receives the set requests open message: let funder_incoming = FunderIncoming::Comm(FunderIncomingComm::Friend((pk1.clone(), friend_message))); @@ -611,7 +602,6 @@ async fn task_handler_pair_basic<'a>( ))) .unwrap(); - // Checking the current requests status on the mutual credit for Node1: let friend2 = state1.friends.get(&pk2).unwrap(); let mutual_credit_state = match &friend2.channel_status { @@ -621,7 +611,6 @@ async fn task_handler_pair_basic<'a>( assert!(mutual_credit_state.requests_status.local.is_open()); assert!(!mutual_credit_state.requests_status.remote.is_open()); - // Checking the current requests status on the mutual credit for Node2: let friend1 = state2.friends.get(&pk1).unwrap(); let mutual_credit_state = match &friend1.channel_status { @@ -660,7 +649,6 @@ async fn task_handler_pair_basic<'a>( // Report mutations: assert_eq!(outgoing_control.len(), 1); - // Node2 will send a RequestFunds message to Node1 assert_eq!(outgoing_comms.len(), 1); let friend_message = @@ -682,7 +670,6 @@ async fn task_handler_pair_basic<'a>( ))) .unwrap(); - // Node1 sends a ResponseSendFunds to Node2: assert_eq!(outgoing_comms.len(), 1); let friend_message = match &outgoing_comms[0] { @@ -701,7 +688,6 @@ async fn task_handler_pair_basic<'a>( _ => unreachable!(), }; - // Node2 receives ResponseSendFunds from Node1: let funder_incoming = FunderIncoming::Comm(FunderIncomingComm::Friend((pk1.clone(), friend_message))); @@ -750,7 +736,6 @@ async fn task_handler_pair_basic<'a>( ))) .unwrap(); - assert_eq!(outgoing_comms.len(), 1); assert_eq!(outgoing_control.len(), 1); @@ -884,11 +869,16 @@ async fn task_handler_pair_basic<'a>( assert_eq!(outgoing_control.len(), 2); let response_close_payment = match &outgoing_control[1] { - FunderOutgoingControl::ResponseClosePayment(response_close_payment) => response_close_payment, + FunderOutgoingControl::ResponseClosePayment(response_close_payment) => { + response_close_payment + } _ => unreachable!(), }; - assert_eq!(response_close_payment.payment_id, PaymentId::from(&[3u8; PAYMENT_ID_LEN])); + assert_eq!( + response_close_payment.payment_id, + PaymentId::from(&[3u8; PAYMENT_ID_LEN]) + ); let (receipt, ack_uid) = match &response_close_payment.status { PaymentStatus::Success((receipt, ack_uid)) => (receipt, ack_uid), _ => unreachable!(), @@ -917,7 +907,6 @@ async fn task_handler_pair_basic<'a>( ))) .unwrap(); - // Node2: Request for closing payment again: let incoming_control_message = FunderIncomingControl::new( Uid::from(&[23; UID_LEN]), @@ -937,12 +926,14 @@ async fn task_handler_pair_basic<'a>( assert_eq!(outgoing_control.len(), 2); let response_close_payment = match &outgoing_control[1] { - FunderOutgoingControl::ResponseClosePayment(response_close_payment) => response_close_payment, + FunderOutgoingControl::ResponseClosePayment(response_close_payment) => { + response_close_payment + } _ => unreachable!(), }; match response_close_payment.status { - PaymentStatus::PaymentNotFound => {}, + PaymentStatus::PaymentNotFound => {} _ => unreachable!(), } } diff --git a/components/funder/src/tests/tests.rs b/components/funder/src/tests/tests.rs index 2cc3a7b7e..2604df9e9 100644 --- a/components/funder/src/tests/tests.rs +++ b/components/funder/src/tests/tests.rs @@ -3,19 +3,18 @@ use futures::task::Spawn; use crypto::identity::PublicKey; use crypto::invoice_id::{InvoiceId, INVOICE_ID_LEN}; -use crypto::uid::{Uid, UID_LEN}; use crypto::payment_id::{PaymentId, PAYMENT_ID_LEN}; +use crypto::uid::{Uid, UID_LEN}; use proto::funder::messages::{ - FriendStatus, FriendsRoute, FunderControl, RequestsStatus, - ResetFriendChannel, AddInvoice, CreatePayment, CreateTransaction, RequestResult, MultiCommit, - Rate, AckClosePayment, PaymentStatus, + AckClosePayment, AddInvoice, CreatePayment, CreateTransaction, FriendStatus, FriendsRoute, + FunderControl, MultiCommit, PaymentStatus, Rate, RequestResult, RequestsStatus, + ResetFriendChannel, }; use proto::report::messages::{ChannelStatusReport, FunderReport}; use super::utils::{create_node_controls, dummy_named_relay_address, dummy_relay_address}; - async fn task_funder_basic(spawner: impl Spawn + Clone + Send + 'static) { let num_nodes = 2; let mut node_controls = await!(create_node_controls(num_nodes, spawner)); @@ -47,7 +46,6 @@ async fn task_funder_basic(spawner: impl Spawn + Clone + Send + 'static) { await!(node_controls[0].wait_until_ready(&public_keys[1])); await!(node_controls[1].wait_until_ready(&public_keys[0])); - // Let node 1 open an invoice: let add_invoice = AddInvoice { invoice_id: InvoiceId::from(&[1u8; INVOICE_ID_LEN]), @@ -69,10 +67,7 @@ async fn task_funder_basic(spawner: impl Spawn + Clone + Send + 'static) { payment_id: PaymentId::from(&[2u8; PAYMENT_ID_LEN]), request_id: Uid::from(&[5u8; UID_LEN]), route: FriendsRoute { - public_keys: vec![ - public_keys[0].clone(), - public_keys[1].clone(), - ], + public_keys: vec![public_keys[0].clone(), public_keys[1].clone()], }, dest_payment: 4, fees: 1, @@ -100,12 +95,16 @@ async fn task_funder_basic(spawner: impl Spawn + Clone + Send + 'static) { // 0: Expect a receipt: let (receipt, ack_uid) = loop { - await!(node_controls[0].send( - FunderControl::RequestClosePayment(PaymentId::from(&[2u8; PAYMENT_ID_LEN])))); - let response_close_payment = await!(node_controls[0].recv_until_response_close_payment()).unwrap(); + await!( + node_controls[0].send(FunderControl::RequestClosePayment(PaymentId::from( + &[2u8; PAYMENT_ID_LEN] + ))) + ); + let response_close_payment = + await!(node_controls[0].recv_until_response_close_payment()).unwrap(); match response_close_payment.status { PaymentStatus::Success((receipt, ack_uid)) => break (receipt, ack_uid), - _ => {}, + _ => {} } }; @@ -114,14 +113,12 @@ async fn task_funder_basic(spawner: impl Spawn + Clone + Send + 'static) { payment_id: PaymentId::from(&[2u8; PAYMENT_ID_LEN]), ack_uid, }; - await!(node_controls[0].send( - FunderControl::AckClosePayment(ack_close_payment))); + await!(node_controls[0].send(FunderControl::AckClosePayment(ack_close_payment))); assert_eq!(receipt.invoice_id, InvoiceId::from(&[1u8; INVOICE_ID_LEN])); assert_eq!(receipt.dest_payment, 4); assert_eq!(receipt.total_dest_payment, 4); - // Verify expected balances: let pred = |report: &FunderReport<_>| { let friend = report.friends.get(&public_keys[1]).unwrap(); @@ -150,7 +147,6 @@ fn test_funder_basic() { thread_pool.run(task_funder_basic(thread_pool.clone())); } - async fn task_funder_forward_payment(spawner: impl Spawn + Clone + Send + 'static) { /* * 0 -- 1 -- 2 @@ -182,7 +178,7 @@ async fn task_funder_forward_payment(spawner: impl Spawn + Clone + Send + 'stati // Set rate: // This is the amount of credits node 1 takes from node 0 for forwarding messages. - await!(node_controls[1].set_friend_rate(&public_keys[0], Rate {mul: 0, add: 5})); + await!(node_controls[1].set_friend_rate(&public_keys[0], Rate { mul: 0, add: 5 })); // Set remote max debt: await!(node_controls[0].set_remote_max_debt(&public_keys[1], 200)); @@ -200,7 +196,6 @@ async fn task_funder_forward_payment(spawner: impl Spawn + Clone + Send + 'stati await!(node_controls[0].wait_until_ready(&public_keys[1])); await!(node_controls[1].wait_until_ready(&public_keys[2])); - // Let node 2 open an invoice: let add_invoice = AddInvoice { invoice_id: InvoiceId::from(&[1u8; INVOICE_ID_LEN]), @@ -239,7 +234,6 @@ async fn task_funder_forward_payment(spawner: impl Spawn + Clone + Send + 'stati _ => unreachable!(), }; - // 0: Create multi commit: let multi_commit = MultiCommit { invoice_id: InvoiceId::from(&[1u8; INVOICE_ID_LEN]), @@ -254,12 +248,16 @@ async fn task_funder_forward_payment(spawner: impl Spawn + Clone + Send + 'stati // 0: Expect a receipt: let (receipt, ack_uid) = loop { - await!(node_controls[0].send( - FunderControl::RequestClosePayment(PaymentId::from(&[2u8; PAYMENT_ID_LEN])))); - let response_close_payment = await!(node_controls[0].recv_until_response_close_payment()).unwrap(); + await!( + node_controls[0].send(FunderControl::RequestClosePayment(PaymentId::from( + &[2u8; PAYMENT_ID_LEN] + ))) + ); + let response_close_payment = + await!(node_controls[0].recv_until_response_close_payment()).unwrap(); match response_close_payment.status { PaymentStatus::Success((receipt, ack_uid)) => break (receipt, ack_uid), - _ => {}, + _ => {} } }; @@ -268,14 +266,12 @@ async fn task_funder_forward_payment(spawner: impl Spawn + Clone + Send + 'stati payment_id: PaymentId::from(&[2u8; PAYMENT_ID_LEN]), ack_uid, }; - await!(node_controls[0].send( - FunderControl::AckClosePayment(ack_close_payment))); + await!(node_controls[0].send(FunderControl::AckClosePayment(ack_close_payment))); assert_eq!(receipt.invoice_id, InvoiceId::from(&[1u8; INVOICE_ID_LEN])); assert_eq!(receipt.dest_payment, 15); assert_eq!(receipt.total_dest_payment, 15); - // Make sure that node2 got the credits: let pred = |report: &FunderReport<_>| { let friend = match report.friends.get(&public_keys[1]) { @@ -326,7 +322,6 @@ fn test_funder_forward_payment() { thread_pool.run(task_funder_forward_payment(thread_pool.clone())); } - async fn task_funder_payment_failure(spawner: impl Spawn + Clone + Send + 'static) { /* * 0 -- 1 -- 2 @@ -360,7 +355,7 @@ async fn task_funder_payment_failure(spawner: impl Spawn + Clone + Send + 'stati // Set rate: // This is the amount of credits node 1 takes from node 0 for forwarding messages. - await!(node_controls[1].set_friend_rate(&public_keys[0], Rate {mul: 0, add: 5})); + await!(node_controls[1].set_friend_rate(&public_keys[0], Rate { mul: 0, add: 5 })); // Set remote max debt: await!(node_controls[0].set_remote_max_debt(&public_keys[1], 200)); @@ -378,8 +373,6 @@ async fn task_funder_payment_failure(spawner: impl Spawn + Clone + Send + 'stati await!(node_controls[0].wait_until_ready(&public_keys[1])); await!(node_controls[1].wait_until_ready(&public_keys[2])); - - // Create payment 0 --> 3 (Where 3 does not exist) let create_payment = CreatePayment { payment_id: PaymentId::from(&[2u8; PAYMENT_ID_LEN]), @@ -409,19 +402,22 @@ async fn task_funder_payment_failure(spawner: impl Spawn + Clone + Send + 'stati // We expect failure: match transaction_result.result { - RequestResult::Failure => {}, + RequestResult::Failure => {} _ => unreachable!(), } - // 0: Expect that the payment was canceled: let ack_uid = loop { - await!(node_controls[0].send( - FunderControl::RequestClosePayment(PaymentId::from(&[2u8; PAYMENT_ID_LEN])))); - let response_close_payment = await!(node_controls[0].recv_until_response_close_payment()).unwrap(); + await!( + node_controls[0].send(FunderControl::RequestClosePayment(PaymentId::from( + &[2u8; PAYMENT_ID_LEN] + ))) + ); + let response_close_payment = + await!(node_controls[0].recv_until_response_close_payment()).unwrap(); match response_close_payment.status { PaymentStatus::Canceled(ack_uid) => break ack_uid, - _ => {}, + _ => {} } }; @@ -430,8 +426,7 @@ async fn task_funder_payment_failure(spawner: impl Spawn + Clone + Send + 'stati payment_id: PaymentId::from(&[2u8; PAYMENT_ID_LEN]), ack_uid, }; - await!(node_controls[0].send( - FunderControl::AckClosePayment(ack_close_payment))); + await!(node_controls[0].send(FunderControl::AckClosePayment(ack_close_payment))); // Make sure that node0's balance is left unchanged: let pred = |report: &FunderReport<_>| { @@ -454,7 +449,6 @@ fn test_funder_payment_failure() { thread_pool.run(task_funder_payment_failure(thread_pool.clone())); } - /// Test a basic inconsistency between two adjacent nodes async fn task_funder_inconsistency_basic(spawner: S) where @@ -557,7 +551,6 @@ fn test_funder_inconsistency_basic() { thread_pool.run(task_funder_inconsistency_basic(thread_pool.clone())); } - /// Test setting relay address for local node async fn task_funder_add_relay(spawner: impl Spawn + Clone + Send + 'static) { let num_nodes = 1; diff --git a/components/funder/src/tests/utils.rs b/components/funder/src/tests/utils.rs index e4a0948ca..f1402602e 100644 --- a/components/funder/src/tests/utils.rs +++ b/components/funder/src/tests/utils.rs @@ -14,17 +14,16 @@ use crypto::identity::{ use crypto::test_utils::DummyRandom; use crypto::uid::{Uid, UID_LEN}; - use proto::report::messages::{ - ChannelStatusReport, FriendLivenessReport, FunderReport, - FunderReportMutations, RequestsStatusReport, + ChannelStatusReport, FriendLivenessReport, FunderReport, FunderReportMutations, + RequestsStatusReport, }; use proto::app_server::messages::{NamedRelayAddress, RelayAddress}; use proto::funder::messages::{ - AddFriend, FriendStatus, FunderControl, FunderIncomingControl, FunderOutgoingControl, - RequestsStatus, SetFriendRemoteMaxDebt, SetFriendStatus, SetRequestsStatus, TransactionResult, - ResponseClosePayment, SetFriendRate, Rate, + AddFriend, FriendStatus, FunderControl, FunderIncomingControl, FunderOutgoingControl, Rate, + RequestsStatus, ResponseClosePayment, SetFriendRate, SetFriendRemoteMaxDebt, SetFriendStatus, + SetRequestsStatus, TransactionResult, }; use database::DatabaseClient; @@ -217,11 +216,10 @@ where B: Clone + PartialEq + Eq + CanonicalSerialize, { pub async fn send(&mut self, funder_control: FunderControl) { - // Convert self.next_app_request_id to a Uid: let mut app_request_id_inner = [0u8; UID_LEN]; let mut next_app_request_id = self.next_app_request_id; - for j in 0 .. 8 { + for j in 0..8 { app_request_id_inner[j] = (next_app_request_id & 0xff) as u8; next_app_request_id >>= 8; } @@ -229,7 +227,6 @@ where // Advance self.next_app_request_id: self.next_app_request_id = self.next_app_request_id.checked_add(1).unwrap(); - let app_request_id = Uid::from(&app_request_id_inner); let funder_incoming_control = FunderIncomingControl { app_request_id: app_request_id.clone(), @@ -244,7 +241,7 @@ where break; } } - _ => {}, + _ => {} }; } } @@ -294,8 +291,10 @@ where loop { match await!(self.recv())? { NodeRecv::ReportMutations(_) => {} - NodeRecv::TransactionResult(_) => {}, - NodeRecv::ResponseClosePayment(response_close_payment) => return Some(response_close_payment) + NodeRecv::TransactionResult(_) => {} + NodeRecv::ResponseClosePayment(response_close_payment) => { + return Some(response_close_payment) + } }; } } @@ -360,11 +359,7 @@ where await!(self.send(FunderControl::SetRequestsStatus(set_requests_status))); } - pub async fn set_friend_rate<'a>( - &'a mut self, - friend_public_key: &'a PublicKey, - rate: Rate, - ) { + pub async fn set_friend_rate<'a>(&'a mut self, friend_public_key: &'a PublicKey, rate: Rate) { let set_friend_rate = SetFriendRate { friend_public_key: friend_public_key.clone(), rate, @@ -391,7 +386,6 @@ where } } - // TODO: Add this back later: /// Create a few node_controls, together with a router connecting them all. /// This allows having a conversation between any two nodes. diff --git a/components/funder/src/types.rs b/components/funder/src/types.rs index 05a269617..97ac2eeca 100644 --- a/components/funder/src/types.rs +++ b/components/funder/src/types.rs @@ -8,14 +8,13 @@ use crypto::uid::Uid; use proto::app_server::messages::RelayAddress; use proto::funder::messages::{ - ChannelerUpdateFriend, CancelSendFundsOp, FriendMessage, FriendTcOp, FunderIncomingControl, - FunderOutgoingControl, MoveToken, PendingTransaction, RequestSendFundsOp, ResponseSendFundsOp, - CollectSendFundsOp, TransactionStage, + CancelSendFundsOp, ChannelerUpdateFriend, CollectSendFundsOp, FriendMessage, FriendTcOp, + FunderIncomingControl, FunderOutgoingControl, MoveToken, PendingTransaction, + RequestSendFundsOp, ResponseSendFundsOp, TransactionStage, }; use proto::funder::signature_buff::{ - create_response_signature_buffer, move_token_signature_buff, - prefix_hash, + create_response_signature_buffer, move_token_signature_buff, prefix_hash, }; use identity::IdentityClient; @@ -55,7 +54,6 @@ pub async fn create_response_send_funds<'a>( rand_nonce: RandValue, identity_client: &'a mut IdentityClient, ) -> ResponseSendFundsOp { - let u_response_send_funds = ResponseSendFundsOp { request_id: pending_transaction.request_id, dest_hashed_lock, @@ -63,7 +61,8 @@ pub async fn create_response_send_funds<'a>( signature: (), }; - let signature_buff = create_response_signature_buffer(&u_response_send_funds, pending_transaction); + let signature_buff = + create_response_signature_buffer(&u_response_send_funds, pending_transaction); let signature = await!(identity_client.request_signature(signature_buff)).unwrap(); ResponseSendFundsOp { @@ -74,16 +73,12 @@ pub async fn create_response_send_funds<'a>( } } -pub fn create_cancel_send_funds( - request_id: Uid, -) -> CancelSendFundsOp { - +pub fn create_cancel_send_funds(request_id: Uid) -> CancelSendFundsOp { CancelSendFundsOp { request_id: request_id, } } - pub fn create_pending_transaction(request_send_funds: &RequestSendFundsOp) -> PendingTransaction { PendingTransaction { request_id: request_send_funds.request_id, @@ -109,7 +104,6 @@ pub enum UnsignedFriendTcOp { CollectSendFunds(CollectSendFundsOp), } - #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] pub struct MoveTokenHashed { /// Hash of operations and local_relays diff --git a/components/index_client/src/seq_friends.rs b/components/index_client/src/seq_friends.rs index 56af86e6d..31a5c4360 100644 --- a/components/index_client/src/seq_friends.rs +++ b/components/index_client/src/seq_friends.rs @@ -3,7 +3,7 @@ use futures::task::{Spawn, SpawnError, SpawnExt}; use futures::{SinkExt, StreamExt}; use crypto::identity::PublicKey; -use proto::index_client::messages::{IndexMutation, UpdateFriend, FriendInfo}; +use proto::index_client::messages::{FriendInfo, IndexMutation, UpdateFriend}; use crate::seq_map::SeqMap; @@ -61,7 +61,11 @@ async fn seq_friends_loop( seq_friends .next() .map(|(cycle_countdown, (public_key, friend_info))| { - let FriendInfo {send_capacity, recv_capacity, rate} = friend_info; + let FriendInfo { + send_capacity, + recv_capacity, + rate, + } = friend_info; let update_friend = UpdateFriend { public_key, send_capacity, diff --git a/components/index_client/src/single_client.rs b/components/index_client/src/single_client.rs index 4dce215dc..0f4978a22 100644 --- a/components/index_client/src/single_client.rs +++ b/components/index_client/src/single_client.rs @@ -14,8 +14,8 @@ use crypto::uid::Uid; use identity::IdentityClient; use proto::index_server::messages::{ - IndexClientToServer, IndexMutation, IndexServerToClient, MutationsUpdate, RequestRoutes, - ResponseRoutes, MultiRoute, + IndexClientToServer, IndexMutation, IndexServerToClient, MultiRoute, MutationsUpdate, + RequestRoutes, ResponseRoutes, }; pub type ServerConn = ConnPair; @@ -90,7 +90,10 @@ where match index_server_to_client { IndexServerToClient::TimeHash(time_hash) => self.server_time_hash = time_hash, IndexServerToClient::ResponseRoutes(response_routes) => { - let ResponseRoutes { request_id, multi_routes } = response_routes; + let ResponseRoutes { + request_id, + multi_routes, + } = response_routes; let request_sender = match self.open_requests.remove(&request_id) { Some(request_sender) => request_sender, None => { diff --git a/components/index_client/src/tests.rs b/components/index_client/src/tests.rs index 42ecf754d..748a0a520 100644 --- a/components/index_client/src/tests.rs +++ b/components/index_client/src/tests.rs @@ -7,12 +7,12 @@ use common::dummy_connector::{ConnRequest, DummyConnector}; use crypto::identity::{PublicKey, PUBLIC_KEY_LEN}; use crypto::uid::{Uid, UID_LEN}; +use proto::funder::messages::Rate; use proto::index_client::messages::{ AppServerToIndexClient, IndexClientReportMutation, IndexClientRequest, IndexClientToAppServer, IndexMutation, RequestRoutes, ResponseRoutesResult, UpdateFriend, }; use proto::index_server::messages::{IndexServerAddress, NamedIndexServerAddress}; -use proto::funder::messages::Rate; use database::{DatabaseClient, DatabaseRequest}; @@ -155,7 +155,7 @@ where public_key: PublicKey::from(PublicKey::from(&[0xaa; PUBLIC_KEY_LEN])), send_capacity: 100, recv_capacity: 50, - rate: Rate {mul: 0, add: 1}, + rate: Rate { mul: 0, add: 1 }, }; response_sender.send(Some((0, update_friend))).unwrap(); } @@ -322,7 +322,7 @@ where public_key: PublicKey::from(PublicKey::from(&[0xbb; PUBLIC_KEY_LEN])), send_capacity: 200, recv_capacity: 100, - rate: Rate {mul: 0, add: 1}, + rate: Rate { mul: 0, add: 1 }, }; let index_mutation = IndexMutation::UpdateFriend(update_friend); let mutations = vec![index_mutation.clone()]; @@ -346,7 +346,7 @@ where public_key: PublicKey::from(PublicKey::from(&[0xcc; PUBLIC_KEY_LEN])), send_capacity: 20, recv_capacity: 30, - rate: Rate {mul: 0, add: 1}, + rate: Rate { mul: 0, add: 1 }, }; match await!(icc.seq_friends_receiver.next()).unwrap() { @@ -468,7 +468,7 @@ where public_key: PublicKey::from(PublicKey::from(&[0xbb; PUBLIC_KEY_LEN])), send_capacity: 200, recv_capacity: 100, - rate: Rate {mul: 0, add: 1}, + rate: Rate { mul: 0, add: 1 }, }; let index_mutation = IndexMutation::UpdateFriend(update_friend); let mutations = vec![index_mutation.clone()]; diff --git a/components/index_server/src/graph/graph_service.rs b/components/index_server/src/graph/graph_service.rs index 441e7e5a1..ba9adda96 100644 --- a/components/index_server/src/graph/graph_service.rs +++ b/components/index_server/src/graph/graph_service.rs @@ -9,11 +9,11 @@ pub enum GraphRequest { UpdateEdge( N, N, - CapacityEdge, - oneshot::Sender>>, + CapacityEdge, + oneshot::Sender>>, ), /// Remove a directed edge: - RemoveEdge(N, N, oneshot::Sender>>), + RemoveEdge(N, N, oneshot::Sender>>), /// Remove a node and all edges starting from this node. /// Note: This will not remove edges going to this node. RemoveNode(N, oneshot::Sender), @@ -129,9 +129,9 @@ impl GraphClient { &mut self, a: N, b: N, - capacity_edge: CapacityEdge, - ) -> Result>, GraphClientError> { - let (sender, receiver) = oneshot::channel::>>(); + capacity_edge: CapacityEdge, + ) -> Result>, GraphClientError> { + let (sender, receiver) = oneshot::channel::>>(); await!(self .requests_sender .send(GraphRequest::UpdateEdge(a, b, capacity_edge, sender)))?; @@ -143,7 +143,7 @@ impl GraphClient { &mut self, a: N, b: N, - ) -> Result>, GraphClientError> { + ) -> Result>, GraphClientError> { let (sender, receiver) = oneshot::channel(); await!(self .requests_sender @@ -221,13 +221,12 @@ where #[cfg(test)] mod tests { - use super::*; + use super::super::capacity_graph::CapacityRoute; use super::super::simple_capacity_graph::SimpleCapacityGraph; use super::super::test_utils::ConstRate; - use super::super::capacity_graph::CapacityRoute; + use super::*; use futures::executor::ThreadPool; - async fn task_create_graph_service_basic(spawner: S) where S: Spawn, @@ -237,31 +236,28 @@ mod tests { let mut graph_client = create_graph_service(capacity_graph, graph_service_spawner, spawner).unwrap(); - await!(graph_client.update_edge(2u32, 5u32, CapacityEdge::new((30, 5), ConstRate(1)))).unwrap(); + await!(graph_client.update_edge(2u32, 5u32, CapacityEdge::new((30, 5), ConstRate(1)))) + .unwrap(); await!(graph_client.update_edge(5, 2, CapacityEdge::new((5, 30), ConstRate(1)))).unwrap(); assert_eq!( await!(graph_client.get_multi_routes(2, 5, 29, None)).unwrap(), vec![CapacityMultiRoute { - routes: vec![ - CapacityRoute { - route: vec![2,5], - capacity: 30, - rate: ConstRate(0), - } - ], + routes: vec![CapacityRoute { + route: vec![2, 5], + capacity: 30, + rate: ConstRate(0), + }], }] ); assert_eq!( await!(graph_client.get_multi_routes(2, 5, 30, None)).unwrap(), vec![CapacityMultiRoute { - routes: vec![ - CapacityRoute { - route: vec![2,5], - capacity: 30, - rate: ConstRate(0), - } - ], + routes: vec![CapacityRoute { + route: vec![2, 5], + capacity: 30, + rate: ConstRate(0), + }], }] ); assert_eq!( diff --git a/components/index_server/src/server.rs b/components/index_server/src/server.rs index baee282f0..104108b21 100644 --- a/components/index_server/src/server.rs +++ b/components/index_server/src/server.rs @@ -14,13 +14,14 @@ use crypto::uid::Uid; use proto::index_server::messages::{ ForwardMutationsUpdate, IndexClientToServer, IndexMutation, IndexServerToClient, - IndexServerToServer, MutationsUpdate, ResponseRoutes, MultiRoute, TimeProofLink, RouteCapacityRate, + IndexServerToServer, MultiRoute, MutationsUpdate, ResponseRoutes, RouteCapacityRate, + TimeProofLink, }; use proto::funder::messages::{FriendsRoute, Rate}; +use crate::graph::capacity_graph::{CapacityEdge, LinearRate}; use crate::graph::graph_service::{GraphClient, GraphClientError}; -use crate::graph::capacity_graph::{LinearRate, CapacityEdge}; use crate::verifier::Verifier; @@ -41,7 +42,7 @@ impl LinearRate for Rate { fn checked_add(&self, other: &Self) -> Option { // Entry wise addition: - Some(Rate { + Some(Rate { mul: self.mul.checked_add(other.mul)?, add: self.add.checked_add(other.add)?, }) @@ -421,10 +422,13 @@ async fn client_handler( let multi_routes = graph_multi_routes .into_iter() .map(|graph_multi_route| MultiRoute { - routes: graph_multi_route.routes + routes: graph_multi_route + .routes .into_iter() .map(|graph_route| RouteCapacityRate { - route: FriendsRoute {public_keys: graph_route.route}, + route: FriendsRoute { + public_keys: graph_route.route, + }, capacity: graph_route.capacity, rate: graph_route.rate, }) diff --git a/components/node/src/connect/connect.rs b/components/node/src/connect/connect.rs index fccc653c7..f60cd285d 100644 --- a/components/node/src/connect/connect.rs +++ b/components/node/src/connect/connect.rs @@ -17,16 +17,13 @@ use crypto::crypto_rand::CryptoRandom; use crypto::identity::PublicKey; use identity::IdentityClient; - pub use super::node_connection::NodeConnection; use super::node_connection::NodeConnectionTuple; - use keepalive::KeepAliveChannel; use secure_channel::SecureChannel; use version::VersionPrefix; - #[derive(Debug)] pub enum SetupConnectionError { EncryptSetupError, diff --git a/components/node/src/connect/node_connection/node_connection.rs b/components/node/src/connect/node_connection/node_connection.rs index 1d9ec0a05..3aa7862ab 100644 --- a/components/node/src/connect/node_connection/node_connection.rs +++ b/components/node/src/connect/node_connection/node_connection.rs @@ -6,18 +6,16 @@ use proto::app_server::messages::{AppPermissions, AppServerToApp, AppToAppServer use crypto::crypto_rand::{CryptoRandom, OffstSystemRandom}; +use common::conn::ConnPair; use common::multi_consumer::{multi_consumer_service, MultiConsumerClient}; use common::mutable_state::BatchMutable; use common::state_service::{state_service, StateClient}; -use common::conn::{ConnPair}; - use super::config::AppConfig; use super::report::AppReport; use super::routes::AppRoutes; use super::send_funds::AppSendFunds; - pub type NodeConnectionTuple = ( AppPermissions, NodeReport, @@ -78,22 +76,26 @@ where .spawn(routes_fut) .map_err(|_| NodeConnectionError::SpawnError)?; - let (mut incoming_transaction_results_sender, incoming_transaction_results) = mpsc::channel(0); + let (mut incoming_transaction_results_sender, incoming_transaction_results) = + mpsc::channel(0); let (requests_sender, incoming_requests) = mpsc::channel(0); let transaction_results_mc = MultiConsumerClient::new(requests_sender); - let transaction_results_fut = multi_consumer_service(incoming_transaction_results, incoming_requests) - .map_err(|e| error!("SendFunds multi_consumer_service() error: {:?}", e)) - .map(|_| ()); + let transaction_results_fut = + multi_consumer_service(incoming_transaction_results, incoming_requests) + .map_err(|e| error!("SendFunds multi_consumer_service() error: {:?}", e)) + .map(|_| ()); spawner .spawn(transaction_results_fut) .map_err(|_| NodeConnectionError::SpawnError)?; - let (mut incoming_response_close_payments_sender, incoming_response_close_payments) = mpsc::channel(0); + let (mut incoming_response_close_payments_sender, incoming_response_close_payments) = + mpsc::channel(0); let (requests_sender, incoming_requests) = mpsc::channel(0); let response_close_payments_mc = MultiConsumerClient::new(requests_sender); - let response_close_payments_fut = multi_consumer_service(incoming_response_close_payments, incoming_requests) - .map_err(|e| error!("SendFunds multi_consumer_service() error: {:?}", e)) - .map(|_| ()); + let response_close_payments_fut = + multi_consumer_service(incoming_response_close_payments, incoming_requests) + .map_err(|e| error!("SendFunds multi_consumer_service() error: {:?}", e)) + .map(|_| ()); spawner .spawn(response_close_payments_fut) .map_err(|_| NodeConnectionError::SpawnError)?; @@ -114,10 +116,13 @@ where while let Some(message) = await!(receiver.next()) { match message { AppServerToApp::TransactionResult(transaction_result) => { - let _ = await!(incoming_transaction_results_sender.send(transaction_result)); + let _ = await!( + incoming_transaction_results_sender.send(transaction_result) + ); } AppServerToApp::ResponseClosePayment(response_close_payment) => { - let _ = await!(incoming_response_close_payments_sender.send(response_close_payment)); + let _ = await!(incoming_response_close_payments_sender + .send(response_close_payment)); } AppServerToApp::Report(_node_report) => { // TODO: Maybe somehow redesign the type AppServerToApp diff --git a/components/stctrl/src/buyer.rs b/components/stctrl/src/buyer.rs index 8f5cca096..1e267563d 100644 --- a/components/stctrl/src/buyer.rs +++ b/components/stctrl/src/buyer.rs @@ -3,20 +3,19 @@ use std::path::PathBuf; use futures::future::select_all; -use app::ser_string::{string_to_public_key, payment_id_to_string, string_to_payment_id}; -use app::{AppRoutes, AppSendFunds, NodeConnection, PublicKey, MultiCommit, PaymentStatus}; +use app::ser_string::{payment_id_to_string, string_to_payment_id, string_to_public_key}; +use app::{AppRoutes, AppSendFunds, MultiCommit, NodeConnection, PaymentStatus, PublicKey}; use structopt::StructOpt; -use app::gen::{gen_uid, gen_payment_id}; +use app::gen::{gen_payment_id, gen_uid}; use app::invoice::{InvoiceId, INVOICE_ID_LEN}; use app::route::{FriendsRoute, MultiRoute}; use crate::file::invoice::load_invoice_from_file; -use crate::multi_route_util::choose_multi_route; use crate::file::multi_commit::store_multi_commit_to_file; use crate::file::receipt::store_receipt_to_file; - +use crate::multi_route_util::choose_multi_route; /// Pay an invoice #[derive(Clone, Debug, StructOpt)] @@ -40,7 +39,6 @@ pub struct PaymentStatusCmd { pub receipt_file: PathBuf, } - /// Funds sending related commands #[derive(Clone, Debug, StructOpt)] pub enum BuyerCmd { @@ -75,8 +73,6 @@ pub enum BuyerError { ParsePaymentIdError, } - - /// Pay an invoice async fn buyer_pay_invoice( pay_invoice_cmd: PayInvoiceCmd, @@ -90,7 +86,6 @@ async fn buyer_pay_invoice( commit_file, } = pay_invoice_cmd; - // Make sure that we will be able to write the MultiCommit // before we do the actual payment: if commit_file.exists() { @@ -113,17 +108,18 @@ async fn buyer_pay_invoice( )) // No exclusion of edges .map_err(|_| BuyerError::AppRoutesError)?; - let (route_index, multi_route_choice) = choose_multi_route(&multi_routes, invoice.dest_payment) .ok_or(BuyerError::NoSuitableRoute)?; let multi_route = &multi_routes[route_index]; - // Calculate total fees: // TODO: Possibly ask the user if he wants to pay this amount of fees at this point. let mut total_fees = 0u128; for (route_index, dest_payment) in &multi_route_choice { - let fee = multi_route.routes[*route_index].rate.calc_fee(*dest_payment).unwrap(); + let fee = multi_route.routes[*route_index] + .rate + .calc_fee(*dest_payment) + .unwrap(); total_fees = total_fees.checked_add(fee).unwrap(); } writeln!(writer, "Total fees: {}", total_fees).map_err(|_| BuyerError::WriteError)?; @@ -131,17 +127,24 @@ async fn buyer_pay_invoice( // Create a new payment let payment_id = gen_payment_id(); - writeln!(writer, "Payment id: {:?}", payment_id_to_string(&payment_id)).map_err(|_| BuyerError::WriteError)?; + writeln!( + writer, + "Payment id: {:?}", + payment_id_to_string(&payment_id) + ) + .map_err(|_| BuyerError::WriteError)?; - await!(app_send_funds.create_payment(payment_id, - invoice.invoice_id.clone(), - invoice.dest_payment.clone(), - invoice.dest_public_key.clone())) - .map_err(|_| BuyerError::CreatePaymentFailed)?; + await!(app_send_funds.create_payment( + payment_id, + invoice.invoice_id.clone(), + invoice.dest_payment.clone(), + invoice.dest_public_key.clone() + )) + .map_err(|_| BuyerError::CreatePaymentFailed)?; // Create new transactions (One for every route). On the first failure cancel all // transactions. Succeed only if all transactions succeed. - + let mut fut_list = Vec::new(); for (route_index, dest_payment) in &multi_route_choice { let route = &multi_route.routes[*route_index]; @@ -157,19 +160,19 @@ async fn buyer_pay_invoice( *dest_payment, route.rate.calc_fee(*dest_payment).unwrap(), )) - }) + }), ); } let mut commits = Vec::new(); - for _ in 0 .. multi_route_choice.len() { + for _ in 0..multi_route_choice.len() { let (output, fut_index, new_fut_list) = await!(select_all(fut_list)); match output { Ok(commit) => commits.push(commit), Err(_) => { let _ = await!(app_send_funds.request_close_payment(payment_id.clone())); return Err(BuyerError::CreateTransactionFailed); - }, + } } fut_list = new_fut_list; } @@ -209,8 +212,8 @@ async fn buyer_payment_status( return Err(BuyerError::ReceiptFileAlreadyExists); } - let payment_id = string_to_payment_id(&payment_id) - .map_err(|_| BuyerError::ParsePaymentIdError)?; + let payment_id = + string_to_payment_id(&payment_id).map_err(|_| BuyerError::ParsePaymentIdError)?; let payment_status = await!(app_send_funds.request_close_payment(payment_id.clone())) .map_err(|_| BuyerError::RequestClosePaymentError)?; @@ -219,13 +222,14 @@ async fn buyer_payment_status( PaymentStatus::PaymentNotFound => { writeln!(writer, "Payment could not be found").map_err(|_| BuyerError::WriteError)?; None - }, + } PaymentStatus::InProgress => { writeln!(writer, "Payment is in progress").map_err(|_| BuyerError::WriteError)?; None - }, + } PaymentStatus::Success((receipt, ack_uid)) => { - writeln!(writer, "Payment succeeded. Saving receipt to file.").map_err(|_| BuyerError::WriteError)?; + writeln!(writer, "Payment succeeded. Saving receipt to file.") + .map_err(|_| BuyerError::WriteError)?; // Store receipt to file: store_receipt_to_file(&receipt, &receipt_file) .map_err(|_| BuyerError::StoreReceiptError)?; @@ -246,7 +250,6 @@ async fn buyer_payment_status( Ok(()) } - pub async fn buyer( buyer_cmd: BuyerCmd, mut node_connection: NodeConnection, diff --git a/components/stctrl/src/info.rs b/components/stctrl/src/info.rs index e0ef5c6b3..c1c940907 100644 --- a/components/stctrl/src/info.rs +++ b/components/stctrl/src/info.rs @@ -8,12 +8,14 @@ use app::report::{ ChannelStatusReport, FriendReport, FriendStatusReport, NodeReport, RequestsStatusReport, }; use app::ser_string::public_key_to_string; -use app::{store_friend_to_file, AppReport, FriendAddress, NodeConnection, - RelayAddress, verify_move_token_hashed_report, verify_receipt}; +use app::{ + store_friend_to_file, verify_move_token_hashed_report, verify_receipt, AppReport, + FriendAddress, NodeConnection, RelayAddress, +}; -use crate::file::token::{store_token_to_file, load_token_from_file}; -use crate::file::receipt::load_receipt_from_file; use crate::file::invoice::load_invoice_from_file; +use crate::file::receipt::load_receipt_from_file; +use crate::file::token::{load_token_from_file, store_token_to_file}; use crate::utils::friend_public_key_by_name; /// Display local public key (Used as address for sending funds) @@ -75,7 +77,6 @@ pub struct VerifyReceiptCmd { pub receipt: PathBuf, } - #[derive(Clone, Debug, StructOpt)] pub enum InfoCmd { /// Show local public key (Used as address for sending funds) @@ -356,8 +357,8 @@ fn info_verify_token( verify_token_cmd: VerifyTokenCmd, writer: &mut impl io::Write, ) -> Result<(), InfoError> { - let move_token_hashed_report = load_token_from_file(&verify_token_cmd.token) - .map_err(|_| InfoError::LoadTokenError)?; + let move_token_hashed_report = + load_token_from_file(&verify_token_cmd.token).map_err(|_| InfoError::LoadTokenError)?; if verify_move_token_hashed_report( &move_token_hashed_report, @@ -511,9 +512,7 @@ pub async fn info( InfoCmd::FriendLastToken(friend_last_token_cmd) => { await!(info_friend_last_token(friend_last_token_cmd, app_report))? } - InfoCmd::VerifyToken(verify_token_cmd) => { - info_verify_token(verify_token_cmd, writer)? - } + InfoCmd::VerifyToken(verify_token_cmd) => info_verify_token(verify_token_cmd, writer)?, InfoCmd::Balance(_balance_cmd) => await!(info_balance(app_report, writer))?, InfoCmd::ExportTicket(export_ticket_cmd) => { await!(info_export_ticket(export_ticket_cmd, app_report))? diff --git a/components/stctrl/src/seller.rs b/components/stctrl/src/seller.rs index e92b237d6..0be6a1a58 100644 --- a/components/stctrl/src/seller.rs +++ b/components/stctrl/src/seller.rs @@ -1,7 +1,7 @@ -use std::path::PathBuf; use std::io; +use std::path::PathBuf; -use app::{NodeConnection, PublicKey, AppSendFunds}; +use app::{AppSendFunds, NodeConnection, PublicKey}; use structopt::StructOpt; @@ -58,10 +58,12 @@ pub enum SellerError { NoFundsPermissions, } -async fn seller_create_invoice(create_invoice_cmd: CreateInvoiceCmd, - local_public_key: PublicKey, - mut app_send_funds: AppSendFunds, - writer: &mut impl io::Write) -> Result<(), SellerError> { +async fn seller_create_invoice( + create_invoice_cmd: CreateInvoiceCmd, + local_public_key: PublicKey, + mut app_send_funds: AppSendFunds, + writer: &mut impl io::Write, +) -> Result<(), SellerError> { unimplemented!(); } diff --git a/components/stctrl/src/stctrllib.rs b/components/stctrl/src/stctrllib.rs index 67e56e2d3..523d1d296 100644 --- a/components/stctrl/src/stctrllib.rs +++ b/components/stctrl/src/stctrllib.rs @@ -5,8 +5,8 @@ use futures::executor::ThreadPool; use structopt::StructOpt; -use crate::config::{config, ConfigCmd, ConfigError}; use crate::buyer::{buyer, BuyerCmd, BuyerError}; +use crate::config::{config, ConfigCmd, ConfigError}; use crate::info::{info, InfoCmd, InfoError}; use app::{connect, identity_from_file, load_node_from_file}; From a868d7628e4981dbbe12d0e021159517b887a19f Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 16:06:55 +0300 Subject: [PATCH 229/248] Added missing seller api (offst-app-server). --- components/app/src/lib.rs | 2 +- components/app_server/src/server.rs | 34 +++++++++++++-- components/node/src/connect/mod.rs | 2 +- .../{send_funds.rs => buyer.rs} | 42 +++++++++---------- .../node/src/connect/node_connection/mod.rs | 2 +- .../node_connection/node_connection.rs | 18 ++++---- components/proto/src/app_server/messages.rs | 19 ++++++--- components/proto/src/app_server/serialize.rs | 11 ++++- components/stctrl/src/buyer.rs | 32 +++++++------- components/stctrl/src/lib.rs | 7 ++-- components/stctrl/src/seller.rs | 14 +++---- 11 files changed, 111 insertions(+), 72 deletions(-) rename components/node/src/connect/node_connection/{send_funds.rs => buyer.rs} (82%) diff --git a/components/app/src/lib.rs b/components/app/src/lib.rs index 33f6ce56d..b462106dd 100644 --- a/components/app/src/lib.rs +++ b/components/app/src/lib.rs @@ -31,7 +31,7 @@ pub use proto::funder::signature_buff::verify_receipt; pub use proto::index_server::messages::NamedIndexServerAddress; pub use proto::report::signature_buff::verify_move_token_hashed_report; -pub use node::connect::{AppConfig, AppReport, AppRoutes, AppSendFunds, NodeConnection}; +pub use node::connect::{AppBuyer, AppConfig, AppReport, AppRoutes, NodeConnection}; pub use self::connect::{connect, ConnectError}; pub use self::identity::{identity_from_file, IdentityFromFileError}; diff --git a/components/app_server/src/server.rs b/components/app_server/src/server.rs index 1e3526618..9a419763f 100644 --- a/components/app_server/src/server.rs +++ b/components/app_server/src/server.rs @@ -101,10 +101,15 @@ fn check_permissions(app_permissions: &AppPermissions, app_request: &AppReque match app_request { AppRequest::AddRelay(_) => app_permissions.config, AppRequest::RemoveRelay(_) => app_permissions.config, - AppRequest::CreatePayment(_) => app_permissions.send_funds, - AppRequest::CreateTransaction(_) => app_permissions.send_funds, - AppRequest::RequestClosePayment(_) => app_permissions.send_funds, - AppRequest::AckClosePayment(_) => app_permissions.send_funds, + AppRequest::CreatePayment(_) => app_permissions.buyer, + AppRequest::CreateTransaction(_) => app_permissions.buyer, + AppRequest::RequestClosePayment(_) => app_permissions.buyer, + AppRequest::AckClosePayment(_) => app_permissions.buyer, + + AppRequest::AddInvoice(_) => app_permissions.seller, + AppRequest::CancelInvoice(_) => app_permissions.seller, + AppRequest::CommitInvoice(_) => app_permissions.seller, + AppRequest::AddFriend(_) => app_permissions.config, AppRequest::SetFriendRelays(_) => app_permissions.config, AppRequest::SetFriendName(_) => app_permissions.config, @@ -394,6 +399,27 @@ where ))) .map_err(|_| AppServerError::SendToFunderError) } + AppRequest::AddInvoice(add_invoice) => { + await!(self.to_funder.send(FunderIncomingControl::new( + app_request_id, + FunderControl::AddInvoice(add_invoice) + ))) + .map_err(|_| AppServerError::SendToFunderError) + } + AppRequest::CancelInvoice(invoice_id) => { + await!(self.to_funder.send(FunderIncomingControl::new( + app_request_id, + FunderControl::CancelInvoice(invoice_id) + ))) + .map_err(|_| AppServerError::SendToFunderError) + } + AppRequest::CommitInvoice(multi_commit) => { + await!(self.to_funder.send(FunderIncomingControl::new( + app_request_id, + FunderControl::CommitInvoice(multi_commit) + ))) + .map_err(|_| AppServerError::SendToFunderError) + } AppRequest::AddFriend(add_friend) => await!(self.to_funder.send( FunderIncomingControl::new(app_request_id, FunderControl::AddFriend(add_friend)) )) diff --git a/components/node/src/connect/mod.rs b/components/node/src/connect/mod.rs index 95dccf55e..aa9180537 100644 --- a/components/node/src/connect/mod.rs +++ b/components/node/src/connect/mod.rs @@ -4,5 +4,5 @@ mod node_connection; pub use self::connect::{node_connect, NodeConnection}; pub use self::node_connection::{ - config::AppConfig, report::AppReport, routes::AppRoutes, send_funds::AppSendFunds, + buyer::AppBuyer, config::AppConfig, report::AppReport, routes::AppRoutes, }; diff --git a/components/node/src/connect/node_connection/send_funds.rs b/components/node/src/connect/node_connection/buyer.rs similarity index 82% rename from components/node/src/connect/node_connection/send_funds.rs rename to components/node/src/connect/node_connection/buyer.rs index 47a1b3040..ab7cbc71a 100644 --- a/components/node/src/connect/node_connection/send_funds.rs +++ b/components/node/src/connect/node_connection/buyer.rs @@ -16,7 +16,7 @@ use proto::funder::messages::{ // TODO: Different in naming convention from AppConfigError and AppRoutesError: #[derive(Debug)] -pub enum SendFundsError { +pub enum BuyerError { /// A local error occurred when trying to send funds. /// (Connectivity error) ConnectivityError, @@ -29,7 +29,7 @@ pub enum SendFundsError { } #[derive(Clone)] -pub struct AppSendFunds { +pub struct AppBuyer { sender: mpsc::Sender, transaction_results_mc: MultiConsumerClient, response_close_payments_mc: MultiConsumerClient, @@ -37,7 +37,7 @@ pub struct AppSendFunds { rng: R, } -impl AppSendFunds +impl AppBuyer where R: CryptoRandom, { @@ -48,7 +48,7 @@ where done_app_requests_mc: MultiConsumerClient, rng: R, ) -> Self { - AppSendFunds { + AppBuyer { sender, transaction_results_mc, response_close_payments_mc, @@ -63,7 +63,7 @@ where invoice_id: InvoiceId, total_dest_payment: u128, dest_public_key: PublicKey, - ) -> Result<(), SendFundsError> { + ) -> Result<(), BuyerError> { let create_payment = CreatePayment { payment_id, invoice_id, @@ -77,10 +77,10 @@ where // Start listening to done requests: let mut incoming_done_requests = await!(self.done_app_requests_mc.request_stream()) - .map_err(|_| SendFundsError::ConnectivityError)?; + .map_err(|_| BuyerError::ConnectivityError)?; // Send CreatePayment: - await!(self.sender.send(to_app_server)).map_err(|_| SendFundsError::ConnectivityError)?; + await!(self.sender.send(to_app_server)).map_err(|_| BuyerError::ConnectivityError)?; // Wait for a sign that our request was received: while let Some(done_request_id) = await!(incoming_done_requests.next()) { @@ -89,7 +89,7 @@ where } } // We lost connectivity before we got any response: - Err(SendFundsError::NoResponse) + Err(BuyerError::NoResponse) } pub async fn create_transaction( @@ -99,7 +99,7 @@ where route: FriendsRoute, dest_payment: u128, fees: u128, - ) -> Result { + ) -> Result { let create_transaction = CreateTransaction { payment_id, request_id, @@ -114,9 +114,9 @@ where ); let mut incoming_transaction_results = await!(self.transaction_results_mc.request_stream()) - .map_err(|_| SendFundsError::ConnectivityError)?; + .map_err(|_| BuyerError::ConnectivityError)?; - await!(self.sender.send(to_app_server)).map_err(|_| SendFundsError::ConnectivityError)?; + await!(self.sender.send(to_app_server)).map_err(|_| BuyerError::ConnectivityError)?; while let Some(transaction_result) = await!(incoming_transaction_results.next()) { if transaction_result.request_id != request_id { @@ -125,18 +125,18 @@ where } match transaction_result.result { RequestResult::Success(commit) => return Ok(commit), - RequestResult::Failure => return Err(SendFundsError::NodeError), + RequestResult::Failure => return Err(BuyerError::NodeError), } } // We lost connectivity before we got any response: - Err(SendFundsError::NoResponse) + Err(BuyerError::NoResponse) } pub async fn request_close_payment( &mut self, payment_id: PaymentId, - ) -> Result { + ) -> Result { let app_request_id = Uid::new(&self.rng); let to_app_server = AppToAppServer::new( app_request_id, @@ -145,9 +145,9 @@ where let mut incoming_response_close_payment = await!(self.response_close_payments_mc.request_stream()) - .map_err(|_| SendFundsError::ConnectivityError)?; + .map_err(|_| BuyerError::ConnectivityError)?; - await!(self.sender.send(to_app_server)).map_err(|_| SendFundsError::ConnectivityError)?; + await!(self.sender.send(to_app_server)).map_err(|_| BuyerError::ConnectivityError)?; while let Some(response_close_payment) = await!(incoming_response_close_payment.next()) { if response_close_payment.payment_id != payment_id { @@ -158,14 +158,14 @@ where } // We lost connectivity before we got any response: - Err(SendFundsError::NoResponse) + Err(BuyerError::NoResponse) } pub async fn ack_close_payment( &mut self, payment_id: PaymentId, ack_uid: Uid, - ) -> Result<(), SendFundsError> { + ) -> Result<(), BuyerError> { let app_request_id = Uid::new(&self.rng); let ack_close_payment = AckClosePayment { payment_id: payment_id, @@ -179,10 +179,10 @@ where // Start listening to done requests: let mut incoming_done_requests = await!(self.done_app_requests_mc.request_stream()) - .map_err(|_| SendFundsError::ConnectivityError)?; + .map_err(|_| BuyerError::ConnectivityError)?; // Send ReceiptAck: - await!(self.sender.send(to_app_server)).map_err(|_| SendFundsError::ConnectivityError)?; + await!(self.sender.send(to_app_server)).map_err(|_| BuyerError::ConnectivityError)?; // Wait for a sign that our request was received: while let Some(done_request_id) = await!(incoming_done_requests.next()) { @@ -192,6 +192,6 @@ where } // We lost connectivity before we got any response: - Err(SendFundsError::NoResponse) + Err(BuyerError::NoResponse) } } diff --git a/components/node/src/connect/node_connection/mod.rs b/components/node/src/connect/node_connection/mod.rs index 7bd79ec37..eddb97148 100644 --- a/components/node/src/connect/node_connection/mod.rs +++ b/components/node/src/connect/node_connection/mod.rs @@ -1,7 +1,7 @@ +pub mod buyer; pub mod config; pub mod report; pub mod routes; -pub mod send_funds; mod node_connection; diff --git a/components/node/src/connect/node_connection/node_connection.rs b/components/node/src/connect/node_connection/node_connection.rs index 3aa7862ab..597290397 100644 --- a/components/node/src/connect/node_connection/node_connection.rs +++ b/components/node/src/connect/node_connection/node_connection.rs @@ -14,7 +14,7 @@ use common::state_service::{state_service, StateClient}; use super::config::AppConfig; use super::report::AppReport; use super::routes::AppRoutes; -use super::send_funds::AppSendFunds; +use super::buyer::AppBuyer; pub type NodeConnectionTuple = ( AppPermissions, @@ -34,7 +34,7 @@ pub struct NodeConnection { report: AppReport, opt_config: Option>, opt_routes: Option>, - opt_send_funds: Option>, + opt_buyer: Option>, rng: R, } @@ -82,7 +82,7 @@ where let transaction_results_mc = MultiConsumerClient::new(requests_sender); let transaction_results_fut = multi_consumer_service(incoming_transaction_results, incoming_requests) - .map_err(|e| error!("SendFunds multi_consumer_service() error: {:?}", e)) + .map_err(|e| error!("Buyer multi_consumer_service() error: {:?}", e)) .map(|_| ()); spawner .spawn(transaction_results_fut) @@ -94,7 +94,7 @@ where let response_close_payments_mc = MultiConsumerClient::new(requests_sender); let response_close_payments_fut = multi_consumer_service(incoming_response_close_payments, incoming_requests) - .map_err(|e| error!("SendFunds multi_consumer_service() error: {:?}", e)) + .map_err(|e| error!("Buyer multi_consumer_service() error: {:?}", e)) .map(|_| ()); spawner .spawn(response_close_payments_fut) @@ -167,8 +167,8 @@ where None }; - let opt_send_funds = if app_permissions.send_funds { - Some(AppSendFunds::new( + let opt_buyer = if app_permissions.buyer { + Some(AppBuyer::new( sender.clone(), transaction_results_mc.clone(), response_close_payments_mc.clone(), @@ -183,7 +183,7 @@ where report: AppReport::new(report_client.clone()), opt_config, opt_routes, - opt_send_funds, + opt_buyer, rng, }) } @@ -200,7 +200,7 @@ where self.opt_routes.as_mut() } - pub fn send_funds(&mut self) -> Option<&mut AppSendFunds> { - self.opt_send_funds.as_mut() + pub fn buyer(&mut self) -> Option<&mut AppBuyer> { + self.opt_buyer.as_mut() } } diff --git a/components/proto/src/app_server/messages.rs b/components/proto/src/app_server/messages.rs index 51261d431..b7dc1cd8a 100644 --- a/components/proto/src/app_server/messages.rs +++ b/components/proto/src/app_server/messages.rs @@ -1,13 +1,14 @@ use common::canonical_serialize::CanonicalSerialize; use common::mutable_state::MutableState; use crypto::identity::PublicKey; +use crypto::invoice_id::InvoiceId; use crypto::payment_id::PaymentId; use crypto::uid::Uid; use crate::funder::messages::{ - AckClosePayment, AddFriend, CreatePayment, CreateTransaction, ResetFriendChannel, - ResponseClosePayment, SetFriendName, SetFriendRate, SetFriendRelays, SetFriendRemoteMaxDebt, - TransactionResult, + AckClosePayment, AddFriend, AddInvoice, CreatePayment, CreateTransaction, MultiCommit, + ResetFriendChannel, ResponseClosePayment, SetFriendName, SetFriendRate, SetFriendRelays, + SetFriendRemoteMaxDebt, TransactionResult, }; use crate::index_client::messages::{ ClientResponseRoutes, IndexClientReport, IndexClientReportMutation, @@ -115,11 +116,15 @@ pub enum AppRequest { SetFriendRemoteMaxDebt(SetFriendRemoteMaxDebt), SetFriendRate(SetFriendRate), ResetFriendChannel(ResetFriendChannel), - /// Sending funds: + /// Buyer: CreatePayment(CreatePayment), CreateTransaction(CreateTransaction), RequestClosePayment(PaymentId), AckClosePayment(AckClosePayment), + /// Seller: + AddInvoice(AddInvoice), + CancelInvoice(InvoiceId), + CommitInvoice(MultiCommit), /// Request routes from one node to another: RequestRoutes(RequestRoutes), /// Manage index servers: @@ -179,8 +184,10 @@ where pub struct AppPermissions { /// Can request routes pub routes: bool, - /// Can send credits - pub send_funds: bool, + /// Can send credits as a buyer + pub buyer: bool, + /// Can receive credits as a seller + pub seller: bool, /// Can configure friends pub config: bool, } diff --git a/components/proto/src/app_server/serialize.rs b/components/proto/src/app_server/serialize.rs index 150240a05..6471037b4 100644 --- a/components/proto/src/app_server/serialize.rs +++ b/components/proto/src/app_server/serialize.rs @@ -388,25 +388,32 @@ fn ser_app_permissions( app_permissions: &AppPermissions, app_permissions_builder: &mut app_server_capnp::app_permissions::Builder, ) { + unimplemented!(); + /* app_permissions_builder .reborrow() .set_routes(app_permissions.routes); app_permissions_builder .reborrow() - .set_send_funds(app_permissions.send_funds); + .set_send_funds(app_permissions.buyer); app_permissions_builder .reborrow() .set_config(app_permissions.config); + */ } fn deser_app_permissions( app_permissions_reader: &app_server_capnp::app_permissions::Reader, ) -> Result { + unimplemented!(); + /* Ok(AppPermissions { routes: app_permissions_reader.get_routes(), - send_funds: app_permissions_reader.get_send_funds(), + buyer: app_permissions_reader.get_send_funds(), + seller: app_permissions_reader.get_send_funds(), config: app_permissions_reader.get_config(), }) + */ } fn ser_report_mutations( diff --git a/components/stctrl/src/buyer.rs b/components/stctrl/src/buyer.rs index 1e267563d..7f9f220d0 100644 --- a/components/stctrl/src/buyer.rs +++ b/components/stctrl/src/buyer.rs @@ -4,7 +4,7 @@ use std::path::PathBuf; use futures::future::select_all; use app::ser_string::{payment_id_to_string, string_to_payment_id, string_to_public_key}; -use app::{AppRoutes, AppSendFunds, MultiCommit, NodeConnection, PaymentStatus, PublicKey}; +use app::{AppRoutes, AppBuyer, MultiCommit, NodeConnection, PaymentStatus, PublicKey}; use structopt::StructOpt; @@ -52,7 +52,7 @@ pub enum BuyerCmd { #[derive(Debug)] pub enum BuyerError { GetReportError, - NoFundsPermissions, + NoBuyerPermissions, NoRoutesPermissions, InvalidDestination, ParseAmountError, @@ -78,7 +78,7 @@ async fn buyer_pay_invoice( pay_invoice_cmd: PayInvoiceCmd, local_public_key: PublicKey, mut app_routes: AppRoutes, - mut app_send_funds: AppSendFunds, + mut app_buyer: AppBuyer, writer: &mut impl io::Write, ) -> Result<(), BuyerError> { let PayInvoiceCmd { @@ -134,7 +134,7 @@ async fn buyer_pay_invoice( ) .map_err(|_| BuyerError::WriteError)?; - await!(app_send_funds.create_payment( + await!(app_buyer.create_payment( payment_id, invoice.invoice_id.clone(), invoice.dest_payment.clone(), @@ -149,11 +149,11 @@ async fn buyer_pay_invoice( for (route_index, dest_payment) in &multi_route_choice { let route = &multi_route.routes[*route_index]; let request_id = gen_uid(); - let mut c_app_send_funds = app_send_funds.clone(); + let mut c_app_buyer = app_buyer.clone(); fut_list.push( // TODO: Possibly a more efficient way than Box::pin? Box::pin(async move { - await!(c_app_send_funds.create_transaction( + await!(c_app_buyer.create_transaction( payment_id.clone(), request_id, route.route.clone(), @@ -170,14 +170,14 @@ async fn buyer_pay_invoice( match output { Ok(commit) => commits.push(commit), Err(_) => { - let _ = await!(app_send_funds.request_close_payment(payment_id.clone())); + let _ = await!(app_buyer.request_close_payment(payment_id.clone())); return Err(BuyerError::CreateTransactionFailed); } } fut_list = new_fut_list; } - let _ = await!(app_send_funds.request_close_payment(payment_id.clone())); + let _ = await!(app_buyer.request_close_payment(payment_id.clone())); let multi_commit = MultiCommit { invoice_id: invoice.invoice_id.clone(), @@ -198,7 +198,7 @@ async fn buyer_pay_invoice( async fn buyer_payment_status( payment_status_cmd: PaymentStatusCmd, local_public_key: PublicKey, - mut app_send_funds: AppSendFunds, + mut app_buyer: AppBuyer, writer: &mut impl io::Write, ) -> Result<(), BuyerError> { let PaymentStatusCmd { @@ -215,7 +215,7 @@ async fn buyer_payment_status( let payment_id = string_to_payment_id(&payment_id).map_err(|_| BuyerError::ParsePaymentIdError)?; - let payment_status = await!(app_send_funds.request_close_payment(payment_id.clone())) + let payment_status = await!(app_buyer.request_close_payment(payment_id.clone())) .map_err(|_| BuyerError::RequestClosePaymentError)?; let opt_ack_uid = match payment_status { @@ -243,7 +243,7 @@ async fn buyer_payment_status( }; if let Some(ack_uid) = opt_ack_uid { - await!(app_send_funds.ack_close_payment(payment_id.clone(), ack_uid)) + await!(app_buyer.ack_close_payment(payment_id.clone(), ack_uid)) .map_err(|_| BuyerError::AckClosePaymentError)?; } @@ -264,9 +264,9 @@ pub async fn buyer( let local_public_key = node_report.funder_report.local_public_key.clone(); - let app_send_funds = node_connection - .send_funds() - .ok_or(BuyerError::NoFundsPermissions)? + let app_buyer = node_connection + .buyer() + .ok_or(BuyerError::NoBuyerPermissions)? .clone(); let app_routes = node_connection @@ -279,13 +279,13 @@ pub async fn buyer( pay_invoice_cmd, local_public_key, app_routes, - app_send_funds, + app_buyer, writer, ))?, BuyerCmd::PaymentStatus(payment_status_cmd) => await!(buyer_payment_status( payment_status_cmd, local_public_key, - app_send_funds, + app_buyer, writer, ))?, } diff --git a/components/stctrl/src/lib.rs b/components/stctrl/src/lib.rs index 83ce68b95..e5d1c24f8 100644 --- a/components/stctrl/src/lib.rs +++ b/components/stctrl/src/lib.rs @@ -27,11 +27,10 @@ mod multi_route_util; #[allow(unused)] pub mod buyer; pub mod config; -pub mod info; -#[allow(unused)] -pub mod seller; - +// #[allow(unused)] +// pub mod seller; pub mod file; +pub mod info; pub mod utils; pub mod stctrllib; diff --git a/components/stctrl/src/seller.rs b/components/stctrl/src/seller.rs index 0be6a1a58..435691abd 100644 --- a/components/stctrl/src/seller.rs +++ b/components/stctrl/src/seller.rs @@ -1,7 +1,7 @@ use std::io; use std::path::PathBuf; -use app::{AppSendFunds, NodeConnection, PublicKey}; +use app::{NodeConnection, PublicKey}; use structopt::StructOpt; @@ -55,13 +55,13 @@ pub enum SellerCmd { #[derive(Debug)] pub enum SellerError { GetReportError, - NoFundsPermissions, + NoSellerPermissions, } async fn seller_create_invoice( create_invoice_cmd: CreateInvoiceCmd, local_public_key: PublicKey, - mut app_send_funds: AppSendFunds, + mut app_seller: AppSeller, writer: &mut impl io::Write, ) -> Result<(), SellerError> { unimplemented!(); @@ -81,16 +81,16 @@ pub async fn seller( let local_public_key = node_report.funder_report.local_public_key.clone(); - let app_send_funds = node_connection - .send_funds() - .ok_or(SellerError::NoFundsPermissions)? + let app_seller = node_connection + .seller() + .ok_or(SellerError::NoSellerPermissions)? .clone(); match seller_cmd { SellerCmd::CreateInvoice(create_invoice_cmd) => await!(seller_create_invoice( create_invoice_cmd, local_public_key, - app_send_funds, + app_seller, writer, ))?, SellerCmd::CancelInvoice(_create_invoice_cmd) => unimplemented!(), From c208d92d249e1760a80d3575d499f2d311e4515e Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 16:16:23 +0300 Subject: [PATCH 230/248] Added seller's {add,cancel,commit}-invoice. --- components/node/src/connect/mod.rs | 2 +- .../node/src/connect/node_connection/mod.rs | 1 + .../node_connection/node_connection.rs | 13 ++ .../src/connect/node_connection/seller.rs | 122 ++++++++++++++++++ 4 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 components/node/src/connect/node_connection/seller.rs diff --git a/components/node/src/connect/mod.rs b/components/node/src/connect/mod.rs index aa9180537..428fe03cf 100644 --- a/components/node/src/connect/mod.rs +++ b/components/node/src/connect/mod.rs @@ -4,5 +4,5 @@ mod node_connection; pub use self::connect::{node_connect, NodeConnection}; pub use self::node_connection::{ - buyer::AppBuyer, config::AppConfig, report::AppReport, routes::AppRoutes, + buyer::AppBuyer, config::AppConfig, report::AppReport, routes::AppRoutes, seller::AppSeller, }; diff --git a/components/node/src/connect/node_connection/mod.rs b/components/node/src/connect/node_connection/mod.rs index eddb97148..ced1a09d6 100644 --- a/components/node/src/connect/node_connection/mod.rs +++ b/components/node/src/connect/node_connection/mod.rs @@ -2,6 +2,7 @@ pub mod buyer; pub mod config; pub mod report; pub mod routes; +pub mod seller; mod node_connection; diff --git a/components/node/src/connect/node_connection/node_connection.rs b/components/node/src/connect/node_connection/node_connection.rs index 597290397..af5b4d736 100644 --- a/components/node/src/connect/node_connection/node_connection.rs +++ b/components/node/src/connect/node_connection/node_connection.rs @@ -15,6 +15,7 @@ use super::config::AppConfig; use super::report::AppReport; use super::routes::AppRoutes; use super::buyer::AppBuyer; +use super::seller::AppSeller; pub type NodeConnectionTuple = ( AppPermissions, @@ -35,6 +36,7 @@ pub struct NodeConnection { opt_config: Option>, opt_routes: Option>, opt_buyer: Option>, + opt_seller: Option>, rng: R, } @@ -179,11 +181,22 @@ where None }; + let opt_seller = if app_permissions.seller { + Some(AppSeller::new( + sender.clone(), + done_app_requests_mc.clone(), + rng.clone(), + )) + } else { + None + }; + Ok(NodeConnection { report: AppReport::new(report_client.clone()), opt_config, opt_routes, opt_buyer, + opt_seller, rng, }) } diff --git a/components/node/src/connect/node_connection/seller.rs b/components/node/src/connect/node_connection/seller.rs new file mode 100644 index 000000000..79f2c84df --- /dev/null +++ b/components/node/src/connect/node_connection/seller.rs @@ -0,0 +1,122 @@ +use common::multi_consumer::MultiConsumerClient; +use futures::channel::mpsc; +use futures::{SinkExt, StreamExt}; + +use crypto::crypto_rand::{CryptoRandom, OffstSystemRandom}; +use crypto::invoice_id::InvoiceId; +use crypto::uid::Uid; + +use proto::app_server::messages::{AppRequest, AppToAppServer}; +use proto::funder::messages::{AddInvoice, MultiCommit}; + +// TODO: Different in naming convention from AppConfigError and AppRoutesError: +#[derive(Debug)] +pub enum SellerError { + /// A local error occurred when trying to send funds. + /// (Connectivity error) + ConnectivityError, + /// A remote error occurred when trying to send funds. + /// (Not enough credits, Some node cancelled along the route) + NodeError, + /// The request was issued, but no response was received. + /// The request should be saved (By the caller) and resent at another time. + NoResponse, +} + +#[derive(Clone)] +pub struct AppSeller { + sender: mpsc::Sender, + done_app_requests_mc: MultiConsumerClient, + rng: R, +} + +impl AppSeller +where + R: CryptoRandom, +{ + pub(super) fn new( + sender: mpsc::Sender, + done_app_requests_mc: MultiConsumerClient, + rng: R, + ) -> Self { + AppSeller { + sender, + done_app_requests_mc, + rng, + } + } + + pub async fn add_invoice( + &mut self, + invoice_id: InvoiceId, + total_dest_payment: u128, + ) -> Result<(), SellerError> { + let app_request_id = Uid::new(&self.rng); + let add_invoice = AddInvoice { + invoice_id, + total_dest_payment, + }; + let to_app_server = + AppToAppServer::new(app_request_id, AppRequest::AddInvoice(add_invoice)); + + // Start listening to done requests: + let mut incoming_done_requests = await!(self.done_app_requests_mc.request_stream()) + .map_err(|_| SellerError::ConnectivityError)?; + + // Send AddInvoice: + await!(self.sender.send(to_app_server)).map_err(|_| SellerError::ConnectivityError)?; + + // Wait for a sign that our request was received: + while let Some(done_request_id) = await!(incoming_done_requests.next()) { + if app_request_id == done_request_id { + return Ok(()); + } + } + // We lost connectivity before we got any response: + Err(SellerError::NoResponse) + } + + pub async fn cancel_invoice(&mut self, invoice_id: InvoiceId) -> Result<(), SellerError> { + let app_request_id = Uid::new(&self.rng); + let to_app_server = + AppToAppServer::new(app_request_id, AppRequest::CancelInvoice(invoice_id)); + + // Start listening to done requests: + let mut incoming_done_requests = await!(self.done_app_requests_mc.request_stream()) + .map_err(|_| SellerError::ConnectivityError)?; + + // Send CancelInvoice: + await!(self.sender.send(to_app_server)).map_err(|_| SellerError::ConnectivityError)?; + + // Wait for a sign that our request was received: + while let Some(done_request_id) = await!(incoming_done_requests.next()) { + if app_request_id == done_request_id { + return Ok(()); + } + } + // We lost connectivity before we got any response: + Err(SellerError::NoResponse) + } + + pub async fn commit_invoice(&mut self, multi_commit: MultiCommit) -> Result<(), SellerError> { + let app_request_id = Uid::new(&self.rng); + let to_app_server = + AppToAppServer::new(app_request_id, AppRequest::CommitInvoice(multi_commit)); + + // Start listening to done requests: + let mut incoming_done_requests = await!(self.done_app_requests_mc.request_stream()) + .map_err(|_| SellerError::ConnectivityError)?; + + // Send CommitInvoice: + await!(self.sender.send(to_app_server)).map_err(|_| SellerError::ConnectivityError)?; + + // Wait for a sign that our request was received: + while let Some(done_request_id) = await!(incoming_done_requests.next()) { + if app_request_id == done_request_id { + return Ok(()); + } + } + // We lost connectivity before we got any response: + Err(SellerError::NoResponse) + } +} From 9af71415217d76e515d4d09f3ac273302dd4eebd Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 16:19:54 +0300 Subject: [PATCH 231/248] Updated permissions in stmgr to have: buyer, seller. --- components/bin/src/stmgrlib.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/components/bin/src/stmgrlib.rs b/components/bin/src/stmgrlib.rs index c2ccf7c9f..f45f200c0 100644 --- a/components/bin/src/stmgrlib.rs +++ b/components/bin/src/stmgrlib.rs @@ -55,9 +55,12 @@ pub struct AppTicketCmd { /// Permission to request routes #[structopt(long = "proutes")] pub proutes: bool, - /// Permission to send funds - #[structopt(long = "pfunds")] - pub pfunds: bool, + /// Permission to send funds (buyer) + #[structopt(long = "pbuyer")] + pub pbuyer: bool, + /// Permission to receive funds (seller) + #[structopt(long = "pseller")] + pub pseller: bool, /// Permission to change configuration #[structopt(long = "pconfig")] pub pconfig: bool, @@ -186,7 +189,8 @@ fn app_ticket( idfile, output, proutes, - pfunds, + pbuyer, + pseller, pconfig, }: AppTicketCmd, ) -> Result<(), AppTicketError> { @@ -203,7 +207,8 @@ fn app_ticket( // Get app's permissions: let permissions = AppPermissions { routes: proutes, - send_funds: pfunds, + buyer: pbuyer, + seller: pseller, config: pconfig, }; From b859661d8a9165311d07e62a64d617149b181e68 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 16:24:06 +0300 Subject: [PATCH 232/248] Initial compiling seller.rs in stctrl. --- components/app/src/lib.rs | 2 +- .../node/src/connect/node_connection/node_connection.rs | 4 ++++ components/stctrl/src/lib.rs | 3 ++- components/stctrl/src/seller.rs | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/components/app/src/lib.rs b/components/app/src/lib.rs index b462106dd..9cc6314a1 100644 --- a/components/app/src/lib.rs +++ b/components/app/src/lib.rs @@ -31,7 +31,7 @@ pub use proto::funder::signature_buff::verify_receipt; pub use proto::index_server::messages::NamedIndexServerAddress; pub use proto::report::signature_buff::verify_move_token_hashed_report; -pub use node::connect::{AppBuyer, AppConfig, AppReport, AppRoutes, NodeConnection}; +pub use node::connect::{AppBuyer, AppConfig, AppReport, AppRoutes, AppSeller, NodeConnection}; pub use self::connect::{connect, ConnectError}; pub use self::identity::{identity_from_file, IdentityFromFileError}; diff --git a/components/node/src/connect/node_connection/node_connection.rs b/components/node/src/connect/node_connection/node_connection.rs index af5b4d736..a8019a9f4 100644 --- a/components/node/src/connect/node_connection/node_connection.rs +++ b/components/node/src/connect/node_connection/node_connection.rs @@ -216,4 +216,8 @@ where pub fn buyer(&mut self) -> Option<&mut AppBuyer> { self.opt_buyer.as_mut() } + + pub fn seller(&mut self) -> Option<&mut AppSeller> { + self.opt_seller.as_mut() + } } diff --git a/components/stctrl/src/lib.rs b/components/stctrl/src/lib.rs index e5d1c24f8..ec3525a88 100644 --- a/components/stctrl/src/lib.rs +++ b/components/stctrl/src/lib.rs @@ -28,9 +28,10 @@ mod multi_route_util; pub mod buyer; pub mod config; // #[allow(unused)] -// pub mod seller; pub mod file; pub mod info; +#[allow(unused)] +pub mod seller; pub mod utils; pub mod stctrllib; diff --git a/components/stctrl/src/seller.rs b/components/stctrl/src/seller.rs index 435691abd..41f6e4a00 100644 --- a/components/stctrl/src/seller.rs +++ b/components/stctrl/src/seller.rs @@ -1,7 +1,7 @@ use std::io; use std::path::PathBuf; -use app::{NodeConnection, PublicKey}; +use app::{NodeConnection, PublicKey, AppSeller}; use structopt::StructOpt; From 0b4211731a58dcef5dc851b54d13f281999b4664 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 16:29:15 +0300 Subject: [PATCH 233/248] stctrl: Implemented seller_create_invoice(). --- components/stctrl/src/seller.rs | 34 ++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/components/stctrl/src/seller.rs b/components/stctrl/src/seller.rs index 41f6e4a00..1738509f2 100644 --- a/components/stctrl/src/seller.rs +++ b/components/stctrl/src/seller.rs @@ -2,6 +2,11 @@ use std::io; use std::path::PathBuf; use app::{NodeConnection, PublicKey, AppSeller}; +use app::gen::gen_invoice_id; +use app::ser_string::string_to_public_key; + +use crate::file::invoice::{Invoice, store_invoice_to_file}; + use structopt::StructOpt; @@ -56,6 +61,9 @@ pub enum SellerCmd { pub enum SellerError { GetReportError, NoSellerPermissions, + ParsePublicKeyError, + InvoiceFileAlreadyExists, + StoreInvoiceError, } async fn seller_create_invoice( @@ -64,7 +72,31 @@ async fn seller_create_invoice( mut app_seller: AppSeller, writer: &mut impl io::Write, ) -> Result<(), SellerError> { - unimplemented!(); + let CreateInvoiceCmd { + public_key, + amount, + output, + } = create_invoice_cmd; + + let invoice_id = gen_invoice_id(); + + let dest_public_key = string_to_public_key(&public_key) + .map_err(|_| SellerError::ParsePublicKeyError)?; + + let invoice = Invoice { + invoice_id, + dest_public_key, + dest_payment: amount, + }; + + // Make sure we don't override an existing invoice file: + if output.exists() { + return Err(SellerError::InvoiceFileAlreadyExists); + } + + store_invoice_to_file(&invoice, &output) + .map_err(|_| SellerError::StoreInvoiceError) + } pub async fn seller( From b42226c920840897c790fb8f5ca9e4facb41d512 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 16:37:25 +0300 Subject: [PATCH 234/248] stctrl: Implemented seller_cancel_invoice(). --- components/stctrl/src/seller.rs | 49 +++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/components/stctrl/src/seller.rs b/components/stctrl/src/seller.rs index 1738509f2..b1d9f7536 100644 --- a/components/stctrl/src/seller.rs +++ b/components/stctrl/src/seller.rs @@ -5,7 +5,7 @@ use app::{NodeConnection, PublicKey, AppSeller}; use app::gen::gen_invoice_id; use app::ser_string::string_to_public_key; -use crate::file::invoice::{Invoice, store_invoice_to_file}; +use crate::file::invoice::{Invoice, store_invoice_to_file, load_invoice_from_file}; use structopt::StructOpt; @@ -13,9 +13,6 @@ use structopt::StructOpt; /// Create invoice #[derive(Clone, Debug, StructOpt)] pub struct CreateInvoiceCmd { - /// Payment recipient's public key (In base 64) - #[structopt(short = "p", long = "pubkey")] - pub public_key: String, /// Amount of credits to pay (A non negative integer) #[structopt(short = "a", long = "amount")] pub amount: u128, @@ -64,41 +61,61 @@ pub enum SellerError { ParsePublicKeyError, InvoiceFileAlreadyExists, StoreInvoiceError, + AddInvoiceError, + LoadInvoiceError, + CancelInvoiceError, } async fn seller_create_invoice( create_invoice_cmd: CreateInvoiceCmd, local_public_key: PublicKey, mut app_seller: AppSeller, - writer: &mut impl io::Write, ) -> Result<(), SellerError> { let CreateInvoiceCmd { - public_key, amount, output, } = create_invoice_cmd; + // Make sure we don't override an existing invoice file: + if output.exists() { + return Err(SellerError::InvoiceFileAlreadyExists); + } + let invoice_id = gen_invoice_id(); - let dest_public_key = string_to_public_key(&public_key) - .map_err(|_| SellerError::ParsePublicKeyError)?; + let dest_public_key = local_public_key; let invoice = Invoice { - invoice_id, + invoice_id: invoice_id.clone(), dest_public_key, dest_payment: amount, }; - // Make sure we don't override an existing invoice file: - if output.exists() { - return Err(SellerError::InvoiceFileAlreadyExists); - } + await!(app_seller.add_invoice(invoice_id.clone(), amount)) + .map_err(|_| SellerError::AddInvoiceError)?; + store_invoice_to_file(&invoice, &output) .map_err(|_| SellerError::StoreInvoiceError) } +async fn seller_cancel_invoice( + cancel_invoice_cmd: CancelInvoiceCmd, + mut app_seller: AppSeller, +) -> Result<(), SellerError> { + let CancelInvoiceCmd { + invoice_file + } = cancel_invoice_cmd; + + let invoice = load_invoice_from_file(&invoice_file) + .map_err(|_| SellerError::LoadInvoiceError)?; + + await!(app_seller.cancel_invoice(invoice.invoice_id)) + .map_err(|_| SellerError::CancelInvoiceError) + +} + pub async fn seller( seller_cmd: SellerCmd, mut node_connection: NodeConnection, @@ -123,9 +140,11 @@ pub async fn seller( create_invoice_cmd, local_public_key, app_seller, - writer, ))?, - SellerCmd::CancelInvoice(_create_invoice_cmd) => unimplemented!(), + SellerCmd::CancelInvoice(cancel_invoice_cmd) => await!(seller_cancel_invoice( + cancel_invoice_cmd, + app_seller))?, + SellerCmd::CommitInvoice(_commit_invoice_cmd) => unimplemented!(), } From bddee9be400cf0a8bd1cc4d3dfac3538ab2d5d3c Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 16:46:17 +0300 Subject: [PATCH 235/248] stctrl: Initial seller.rs implementation. --- components/stctrl/src/seller.rs | 36 +++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/components/stctrl/src/seller.rs b/components/stctrl/src/seller.rs index b1d9f7536..176b15ffb 100644 --- a/components/stctrl/src/seller.rs +++ b/components/stctrl/src/seller.rs @@ -6,6 +6,7 @@ use app::gen::gen_invoice_id; use app::ser_string::string_to_public_key; use crate::file::invoice::{Invoice, store_invoice_to_file, load_invoice_from_file}; +use crate::file::multi_commit::load_multi_commit_from_file; use structopt::StructOpt; @@ -64,8 +65,12 @@ pub enum SellerError { AddInvoiceError, LoadInvoiceError, CancelInvoiceError, + LoadCommitError, + CommitInvoiceError, + InvoiceCommitMismatch, } + async fn seller_create_invoice( create_invoice_cmd: CreateInvoiceCmd, local_public_key: PublicKey, @@ -116,10 +121,35 @@ async fn seller_cancel_invoice( } +async fn seller_commit_invoice( + commit_invoice_cmd: CommitInvoiceCmd, + mut app_seller: AppSeller, +) -> Result<(), SellerError> { + let CommitInvoiceCmd { + invoice_file, + commit_file, + } = commit_invoice_cmd; + + // Note: We don't really need the invoice for the internal API. + // We require it here to enforce the user to understand that the commit file corresponds to a + // certain invoice file. + let invoice = load_invoice_from_file(&invoice_file) + .map_err(|_| SellerError::LoadInvoiceError)?; + + let multi_commit = load_multi_commit_from_file(&commit_file) + .map_err(|_| SellerError::LoadCommitError)?; + + if multi_commit.invoice_id != invoice.invoice_id { + return Err(SellerError::InvoiceCommitMismatch); + } + + await!(app_seller.commit_invoice(multi_commit)) + .map_err(|_| SellerError::CommitInvoiceError) +} + pub async fn seller( seller_cmd: SellerCmd, mut node_connection: NodeConnection, - writer: &mut impl io::Write, ) -> Result<(), SellerError> { // Get our local public key: let mut app_report = node_connection.report().clone(); @@ -145,7 +175,9 @@ pub async fn seller( cancel_invoice_cmd, app_seller))?, - SellerCmd::CommitInvoice(_commit_invoice_cmd) => unimplemented!(), + SellerCmd::CommitInvoice(commit_invoice_cmd) => await!(seller_commit_invoice( + commit_invoice_cmd, + app_seller))?, } Ok(()) From 6c01b43c8f6a70470798ce38b68c788f990a2e88 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 16:48:42 +0300 Subject: [PATCH 236/248] Added seller subcommand to stctrl. --- components/stctrl/src/stctrllib.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/components/stctrl/src/stctrllib.rs b/components/stctrl/src/stctrllib.rs index 523d1d296..a4eeae1e3 100644 --- a/components/stctrl/src/stctrllib.rs +++ b/components/stctrl/src/stctrllib.rs @@ -6,6 +6,7 @@ use futures::executor::ThreadPool; use structopt::StructOpt; use crate::buyer::{buyer, BuyerCmd, BuyerError}; +use crate::seller::{seller, SellerCmd, SellerError}; use crate::config::{config, ConfigCmd, ConfigError}; use crate::info::{info, InfoCmd, InfoError}; @@ -24,6 +25,7 @@ pub enum StCtrlError { InfoError(InfoError), ConfigError(ConfigError), BuyerError(BuyerError), + SellerError(SellerError), } impl From for StCtrlError { @@ -44,6 +46,12 @@ impl From for StCtrlError { } } +impl From for StCtrlError { + fn from(e: SellerError) -> Self { + StCtrlError::SellerError(e) + } +} + #[derive(Clone, Debug, StructOpt)] pub enum StCtrlSubcommand { /// Get information about current state of node @@ -52,9 +60,12 @@ pub enum StCtrlSubcommand { /// Configure node's state #[structopt(name = "config")] Config(ConfigCmd), - /// Payments and funds related commands + /// Sending funds (Buyer) #[structopt(name = "buyer")] Buyer(BuyerCmd), + /// Receiving funds (Seller) + #[structopt(name = "seller")] + Seller(SellerCmd), } /// stctrl: offST ConTRoL @@ -117,6 +128,9 @@ pub fn stctrl(st_ctrl_cmd: StCtrlCmd, writer: &mut impl io::Write) -> Result<(), StCtrlSubcommand::Buyer(buyer_cmd) => { await!(buyer(buyer_cmd, node_connection, writer))? } + StCtrlSubcommand::Seller(seller_cmd) => { + await!(seller(seller_cmd, node_connection))? + } } Ok(()) }) From 91f06146def76eb55ad54facb6d53a8338748df7 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 16:49:25 +0300 Subject: [PATCH 237/248] stctrl: seller.rs: removed unused warnings. --- components/stctrl/src/lib.rs | 1 - components/stctrl/src/seller.rs | 2 -- 2 files changed, 3 deletions(-) diff --git a/components/stctrl/src/lib.rs b/components/stctrl/src/lib.rs index ec3525a88..9db9d19fd 100644 --- a/components/stctrl/src/lib.rs +++ b/components/stctrl/src/lib.rs @@ -30,7 +30,6 @@ pub mod config; // #[allow(unused)] pub mod file; pub mod info; -#[allow(unused)] pub mod seller; pub mod utils; diff --git a/components/stctrl/src/seller.rs b/components/stctrl/src/seller.rs index 176b15ffb..e78d390b1 100644 --- a/components/stctrl/src/seller.rs +++ b/components/stctrl/src/seller.rs @@ -1,9 +1,7 @@ -use std::io; use std::path::PathBuf; use app::{NodeConnection, PublicKey, AppSeller}; use app::gen::gen_invoice_id; -use app::ser_string::string_to_public_key; use crate::file::invoice::{Invoice, store_invoice_to_file, load_invoice_from_file}; use crate::file::multi_commit::load_multi_commit_from_file; From 5246ec7ad1d33f187b3dd670a92e132a90d10849 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 16:51:00 +0300 Subject: [PATCH 238/248] stctrl: buyer: removed unused warnings. --- components/stctrl/src/buyer.rs | 8 ++------ components/stctrl/src/lib.rs | 2 -- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/components/stctrl/src/buyer.rs b/components/stctrl/src/buyer.rs index 7f9f220d0..879b8337d 100644 --- a/components/stctrl/src/buyer.rs +++ b/components/stctrl/src/buyer.rs @@ -3,14 +3,12 @@ use std::path::PathBuf; use futures::future::select_all; -use app::ser_string::{payment_id_to_string, string_to_payment_id, string_to_public_key}; +use app::ser_string::{payment_id_to_string, string_to_payment_id}; use app::{AppRoutes, AppBuyer, MultiCommit, NodeConnection, PaymentStatus, PublicKey}; use structopt::StructOpt; use app::gen::{gen_payment_id, gen_uid}; -use app::invoice::{InvoiceId, INVOICE_ID_LEN}; -use app::route::{FriendsRoute, MultiRoute}; use crate::file::invoice::load_invoice_from_file; use crate::file::multi_commit::store_multi_commit_to_file; @@ -166,7 +164,7 @@ async fn buyer_pay_invoice( let mut commits = Vec::new(); for _ in 0..multi_route_choice.len() { - let (output, fut_index, new_fut_list) = await!(select_all(fut_list)); + let (output, _fut_index, new_fut_list) = await!(select_all(fut_list)); match output { Ok(commit) => commits.push(commit), Err(_) => { @@ -197,7 +195,6 @@ async fn buyer_pay_invoice( /// Get the current status of a payment async fn buyer_payment_status( payment_status_cmd: PaymentStatusCmd, - local_public_key: PublicKey, mut app_buyer: AppBuyer, writer: &mut impl io::Write, ) -> Result<(), BuyerError> { @@ -284,7 +281,6 @@ pub async fn buyer( ))?, BuyerCmd::PaymentStatus(payment_status_cmd) => await!(buyer_payment_status( payment_status_cmd, - local_public_key, app_buyer, writer, ))?, diff --git a/components/stctrl/src/lib.rs b/components/stctrl/src/lib.rs index 9db9d19fd..aaf142082 100644 --- a/components/stctrl/src/lib.rs +++ b/components/stctrl/src/lib.rs @@ -24,10 +24,8 @@ extern crate serde_derive; mod multi_route_util; -#[allow(unused)] pub mod buyer; pub mod config; -// #[allow(unused)] pub mod file; pub mod info; pub mod seller; From b009d4dd854250422ca63f2da327cccccbe79552 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 16:52:24 +0300 Subject: [PATCH 239/248] Removed stregisterlib.rs. --- components/stctrl/src/lib.rs | 6 +- components/stctrl/src/stregisterlib.rs | 201 ------------------------- 2 files changed, 2 insertions(+), 205 deletions(-) delete mode 100644 components/stctrl/src/stregisterlib.rs diff --git a/components/stctrl/src/lib.rs b/components/stctrl/src/lib.rs index aaf142082..dbde12678 100644 --- a/components/stctrl/src/lib.rs +++ b/components/stctrl/src/lib.rs @@ -14,11 +14,9 @@ #[macro_use] extern crate prettytable; -#[allow(unused)] -#[macro_use] -extern crate log; +// #[macro_use] +// extern crate log; -#[allow(unused)] #[macro_use] extern crate serde_derive; diff --git a/components/stctrl/src/stregisterlib.rs b/components/stctrl/src/stregisterlib.rs deleted file mode 100644 index edb64d52e..000000000 --- a/components/stctrl/src/stregisterlib.rs +++ /dev/null @@ -1,201 +0,0 @@ -use std::io; -use std::path::PathBuf; -use structopt::StructOpt; - -use app::gen::gen_invoice_id; -use app::ser_string::{public_key_to_string, string_to_public_key}; -use app::{verify_move_token_hashed_report, verify_receipt}; - -use crate::file::invoice::{load_invoice_from_file, store_invoice_to_file, Invoice}; -use crate::file::receipt::load_receipt_from_file; -use crate::file::token::load_token_from_file; - -#[derive(Debug)] -pub enum StRegisterError { - InvoiceFileAlreadyExists, - StoreInvoiceError, - LoadInvoiceError, - LoadReceiptError, - DestPaymentMismatch, - InvoiceIdMismatch, - InvalidReceipt, - ParsePublicKeyError, - LoadTokenError, - TokenInvalid, - WriteError, -} - -/// Generate invoice file -#[derive(Clone, Debug, StructOpt)] -pub struct GenInvoiceCmd { - /// Payment recipient's public key (In base 64) - #[structopt(short = "p", long = "pubkey")] - pub public_key: String, - /// Amount of credits to pay (A non negative integer) - #[structopt(short = "a", long = "amount")] - pub amount: u128, - /// Path of output invoice file - #[structopt(parse(from_os_str), short = "o", long = "output")] - pub output: PathBuf, -} - -/// Verify receipt file -#[derive(Clone, Debug, StructOpt)] -pub struct VerifyReceiptCmd { - /// Path of invoice file (Locally generated) - #[structopt(parse(from_os_str), short = "i", long = "invoice")] - pub invoice: PathBuf, - /// Path of receipt file (Received from buyer) - #[structopt(parse(from_os_str), short = "r", long = "receipt")] - pub receipt: PathBuf, -} - -/// Verify a token received from a friend. -/// A token is some recent commitment of a friend to the mutual credit balance. -#[derive(Clone, Debug, StructOpt)] -pub struct VerifyTokenCmd { - /// Path of token file - #[structopt(parse(from_os_str), short = "t", long = "token")] - pub token: PathBuf, -} - -/// stregister - offST register -/// A minimal credit register util for Offst. Used as a point of sale. -/// Allows to generate invoices and verify receipts. Does not require knowledge of secret -/// identities for its operation. -#[derive(Clone, Debug, StructOpt)] -#[structopt(name = "stregister")] -pub enum StRegisterCmd { - #[structopt(name = "gen-invoice")] - GenInvoice(GenInvoiceCmd), - #[structopt(name = "verify-receipt")] - VerifyReceipt(VerifyReceiptCmd), - #[structopt(name = "verify-token")] - VerifyToken(VerifyTokenCmd), -} - -/// Randomly generate an invoice and store it to an output file -fn subcommand_gen_invoice(gen_invoice_cmd: GenInvoiceCmd) -> Result<(), StRegisterError> { - let invoice_id = gen_invoice_id(); - - let dest_public_key = string_to_public_key(&gen_invoice_cmd.public_key) - .map_err(|_| StRegisterError::ParsePublicKeyError)?; - - let invoice = Invoice { - invoice_id, - dest_public_key, - dest_payment: gen_invoice_cmd.amount, - }; - - // Make sure we don't override an existing invoice file: - if gen_invoice_cmd.output.exists() { - return Err(StRegisterError::InvoiceFileAlreadyExists); - } - - store_invoice_to_file(&invoice, &gen_invoice_cmd.output) - .map_err(|_| StRegisterError::StoreInvoiceError) -} - -/// Verify a given receipt -fn subcommand_verify_receipt( - verify_receipt_cmd: VerifyReceiptCmd, - writer: &mut impl io::Write, -) -> Result<(), StRegisterError> { - let invoice = load_invoice_from_file(&verify_receipt_cmd.invoice) - .map_err(|_| StRegisterError::LoadInvoiceError)?; - - let receipt = load_receipt_from_file(&verify_receipt_cmd.receipt) - .map_err(|_| StRegisterError::LoadReceiptError)?; - - // Make sure that the invoice and receipt files match: - // Verify invoice_id match: - if invoice.invoice_id != receipt.invoice_id { - return Err(StRegisterError::InvoiceIdMismatch); - } - // Verify dest_payment match: - if invoice.dest_payment != receipt.dest_payment { - return Err(StRegisterError::DestPaymentMismatch); - } - - if verify_receipt(&receipt, &invoice.dest_public_key) { - writeln!(writer, "Receipt is valid!").map_err(|_| StRegisterError::WriteError)?; - Ok(()) - } else { - Err(StRegisterError::InvalidReceipt) - } -} - -/// Verify a given friend token -/// If the given token is valid, output token details -fn subcommand_verify_token( - verify_token_cmd: VerifyTokenCmd, - writer: &mut impl io::Write, -) -> Result<(), StRegisterError> { - let move_token_hashed_report = load_token_from_file(&verify_token_cmd.token) - .map_err(|_| StRegisterError::LoadTokenError)?; - - if verify_move_token_hashed_report( - &move_token_hashed_report, - &move_token_hashed_report.local_public_key, - ) { - writeln!(writer, "Token is valid!").map_err(|_| StRegisterError::WriteError)?; - writeln!(writer).map_err(|_| StRegisterError::WriteError)?; - writeln!( - writer, - "local_public_key: {}", - public_key_to_string(&move_token_hashed_report.local_public_key) - ) - .map_err(|_| StRegisterError::WriteError)?; - writeln!( - writer, - "remote_public_key: {}", - public_key_to_string(&move_token_hashed_report.remote_public_key) - ) - .map_err(|_| StRegisterError::WriteError)?; - writeln!( - writer, - "inconsistency_counter: {}", - move_token_hashed_report.inconsistency_counter - ) - .map_err(|_| StRegisterError::WriteError)?; - writeln!(writer, "balance: {}", move_token_hashed_report.balance) - .map_err(|_| StRegisterError::WriteError)?; - writeln!( - writer, - "move_token_counter: {}", - move_token_hashed_report.move_token_counter - ) - .map_err(|_| StRegisterError::WriteError)?; - writeln!( - writer, - "local_pending_debt: {}", - move_token_hashed_report.local_pending_debt - ) - .map_err(|_| StRegisterError::WriteError)?; - writeln!( - writer, - "remote_pending_debt: {}", - move_token_hashed_report.remote_pending_debt - ) - .map_err(|_| StRegisterError::WriteError)?; - - Ok(()) - } else { - Err(StRegisterError::TokenInvalid) - } -} - -pub fn stregister( - st_register_cmd: StRegisterCmd, - writer: &mut impl io::Write, -) -> Result<(), StRegisterError> { - match st_register_cmd { - StRegisterCmd::GenInvoice(gen_invoice_cmd) => subcommand_gen_invoice(gen_invoice_cmd), - StRegisterCmd::VerifyReceipt(verify_receipt_cmd) => { - subcommand_verify_receipt(verify_receipt_cmd, writer) - } - StRegisterCmd::VerifyToken(verify_token_cmd) => { - subcommand_verify_token(verify_token_cmd, writer) - } - } -} From dbd86dfc371c5f86e670050f96c01a025393e87e Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 16:52:52 +0300 Subject: [PATCH 240/248] Removed comment. --- components/stctrl/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/components/stctrl/src/lib.rs b/components/stctrl/src/lib.rs index dbde12678..ce7783b89 100644 --- a/components/stctrl/src/lib.rs +++ b/components/stctrl/src/lib.rs @@ -30,4 +30,3 @@ pub mod seller; pub mod utils; pub mod stctrllib; -// pub mod stregisterlib; From de338108724cd26bf9b4d68508b5566493c47239 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 16:57:48 +0300 Subject: [PATCH 241/248] Updated AppPermissions fields in app_server and proto. --- components/app_server/src/tests/all_apps_closed.rs | 3 ++- components/app_server/src/tests/funder_command.rs | 3 ++- components/app_server/src/tests/index_client_command.rs | 3 ++- components/app_server/src/tests/request_routes.rs | 6 ++++-- components/app_server/src/tests/request_send_funds.rs | 6 ++++-- components/app_server/src/tests/two_apps.rs | 6 ++++-- components/proto/src/file/app.rs | 6 ++++-- 7 files changed, 22 insertions(+), 11 deletions(-) diff --git a/components/app_server/src/tests/all_apps_closed.rs b/components/app_server/src/tests/all_apps_closed.rs index 1feb71c59..fb2d633e2 100644 --- a/components/app_server/src/tests/all_apps_closed.rs +++ b/components/app_server/src/tests/all_apps_closed.rs @@ -32,7 +32,8 @@ where let app_permissions = AppPermissions { routes: true, - send_funds: true, + buyer: true, + seller: true, config: true, }; diff --git a/components/app_server/src/tests/funder_command.rs b/components/app_server/src/tests/funder_command.rs index 70de5fadc..2f907d982 100644 --- a/components/app_server/src/tests/funder_command.rs +++ b/components/app_server/src/tests/funder_command.rs @@ -32,7 +32,8 @@ where let app_permissions = AppPermissions { routes: true, - send_funds: true, + buyer: true, + seller: true, config: true, }; diff --git a/components/app_server/src/tests/index_client_command.rs b/components/app_server/src/tests/index_client_command.rs index 8975146b7..717b5f178 100644 --- a/components/app_server/src/tests/index_client_command.rs +++ b/components/app_server/src/tests/index_client_command.rs @@ -35,7 +35,8 @@ where let app_permissions = AppPermissions { routes: true, - send_funds: true, + buyer: true, + seller: true, config: true, }; diff --git a/components/app_server/src/tests/request_routes.rs b/components/app_server/src/tests/request_routes.rs index b25be905c..d9de48617 100644 --- a/components/app_server/src/tests/request_routes.rs +++ b/components/app_server/src/tests/request_routes.rs @@ -34,7 +34,8 @@ where let app_server_conn_pair = (app_server_sender, app_server_receiver); let app_permissions = AppPermissions { routes: true, - send_funds: true, + buyer: true, + seller: true, config: true, }; await!(connections_sender.send((app_permissions, app_server_conn_pair))).unwrap(); @@ -44,7 +45,8 @@ where let app_server_conn_pair = (app_server_sender, app_server_receiver); let app_permissions = AppPermissions { routes: true, - send_funds: true, + buyer: true, + seller: true, config: true, }; await!(connections_sender.send((app_permissions, app_server_conn_pair))).unwrap(); diff --git a/components/app_server/src/tests/request_send_funds.rs b/components/app_server/src/tests/request_send_funds.rs index 8127e903f..d67e943c6 100644 --- a/components/app_server/src/tests/request_send_funds.rs +++ b/components/app_server/src/tests/request_send_funds.rs @@ -35,7 +35,8 @@ where let app_server_conn_pair = (app_server_sender, app_server_receiver); let app_permissions = AppPermissions { routes: true, - send_funds: true, + buyer: true, + seller: true, config: true, }; await!(connections_sender.send((app_permissions, app_server_conn_pair))).unwrap(); @@ -45,7 +46,8 @@ where let app_server_conn_pair = (app_server_sender, app_server_receiver); let app_permissions = AppPermissions { routes: true, - send_funds: true, + buyer: true, + seller: true, config: true, }; await!(connections_sender.send((app_permissions, app_server_conn_pair))).unwrap(); diff --git a/components/app_server/src/tests/two_apps.rs b/components/app_server/src/tests/two_apps.rs index d44506780..d14a2a127 100644 --- a/components/app_server/src/tests/two_apps.rs +++ b/components/app_server/src/tests/two_apps.rs @@ -31,7 +31,8 @@ where let app_server_conn_pair = (app_server_sender, app_server_receiver); let app_permissions = AppPermissions { routes: true, - send_funds: true, + buyer: true, + seller: true, config: true, }; await!(connections_sender.send((app_permissions, app_server_conn_pair))).unwrap(); @@ -41,7 +42,8 @@ where let app_server_conn_pair = (app_server_sender, app_server_receiver); let app_permissions = AppPermissions { routes: true, - send_funds: true, + buyer: true, + seller: true, config: true, }; await!(connections_sender.send((app_permissions, app_server_conn_pair))).unwrap(); diff --git a/components/proto/src/file/app.rs b/components/proto/src/file/app.rs index 61cdcba1a..78f8b9bed 100644 --- a/components/proto/src/file/app.rs +++ b/components/proto/src/file/app.rs @@ -117,7 +117,8 @@ mod tests { let permissions = AppPermissions { routes: true, - send_funds: false, + buyer: false, + seller: false, config: true, }; let trusted_app = TrustedApp { @@ -139,7 +140,8 @@ mod tests { let file_path = dir.path().join("trusted_app1"); let permissions = AppPermissions { routes: true, - send_funds: false, + buyer: false, + seller: false, config: true, }; let trusted_app1 = TrustedApp { From 47a0fc1a2c11b82902af6593847ddc37bbdfcda3 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 17:02:47 +0300 Subject: [PATCH 242/248] proto/src/file/app.rs: Updated AppPermissions fields. --- components/proto/src/file/app.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/proto/src/file/app.rs b/components/proto/src/file/app.rs index 78f8b9bed..569b2febb 100644 --- a/components/proto/src/file/app.rs +++ b/components/proto/src/file/app.rs @@ -153,7 +153,8 @@ mod tests { let file_path = dir.path().join("trusted_app2"); let permissions = AppPermissions { routes: false, - send_funds: true, + buyer: true, + seller: true, config: false, }; let trusted_app2 = TrustedApp { From 59040addf0b08f10417a4d64bf8d937918172e73 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 17:03:05 +0300 Subject: [PATCH 243/248] Temporarily turn off integration tests. --- components/test/src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/components/test/src/lib.rs b/components/test/src/lib.rs index 9bc0536df..dd0ed4f00 100644 --- a/components/test/src/lib.rs +++ b/components/test/src/lib.rs @@ -21,6 +21,10 @@ extern crate log; #[cfg(test)] mod sim_network; +/* +// TODO: Adjust tests to new atomic payment design +// and restore them. + #[cfg(test)] mod utils; @@ -29,3 +33,4 @@ mod tests; #[cfg(test)] mod cli_tests; +*/ From 3d02fb0e740cf089311c2471ae5d2b5bf4a83ce8 Mon Sep 17 00:00:00 2001 From: real Date: Thu, 30 May 2019 17:18:12 +0300 Subject: [PATCH 244/248] Fixed some clippy warnings. --- components/app_server/src/server.rs | 4 ++-- components/funder/src/handler/handle_control.rs | 2 +- components/funder/src/types.rs | 2 +- components/proto/src/app_server/messages.rs | 1 + components/proto/src/funder/messages.rs | 2 ++ 5 files changed, 7 insertions(+), 4 deletions(-) diff --git a/components/app_server/src/server.rs b/components/app_server/src/server.rs index 9a419763f..040098dfe 100644 --- a/components/app_server/src/server.rs +++ b/components/app_server/src/server.rs @@ -513,9 +513,9 @@ where } AppRequest::RequestRoutes(request_routes) => { // Keep track of which application issued this request: - if let Some(_) = self + if self .route_requests - .insert(request_routes.request_id.clone(), app_id) + .insert(request_routes.request_id, app_id).is_some() { warn!("RequestRoutes: request_id clash."); } diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 05ff3ec08..5369b2289 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -797,7 +797,7 @@ where let funder_mutation = FunderMutation::UpdatePayment((payment_id, new_payment)); m_state.mutate(funder_mutation); - return Ok(()); + Ok(()) } fn control_ack_close_payment( diff --git a/components/funder/src/types.rs b/components/funder/src/types.rs index 97ac2eeca..fe485666d 100644 --- a/components/funder/src/types.rs +++ b/components/funder/src/types.rs @@ -75,7 +75,7 @@ pub async fn create_response_send_funds<'a>( pub fn create_cancel_send_funds(request_id: Uid) -> CancelSendFundsOp { CancelSendFundsOp { - request_id: request_id, + request_id, } } diff --git a/components/proto/src/app_server/messages.rs b/components/proto/src/app_server/messages.rs index b7dc1cd8a..68fc2d811 100644 --- a/components/proto/src/app_server/messages.rs +++ b/components/proto/src/app_server/messages.rs @@ -79,6 +79,7 @@ where pub mutations: Vec>, } +#[allow(clippy::large_enum_variant)] #[derive(Debug, PartialEq, Eq)] pub enum AppServerToApp where diff --git a/components/proto/src/funder/messages.rs b/components/proto/src/funder/messages.rs index be8566c6b..6f4a8a105 100644 --- a/components/proto/src/funder/messages.rs +++ b/components/proto/src/funder/messages.rs @@ -644,6 +644,7 @@ pub struct TransactionResult { pub result: RequestResult, } +#[allow(clippy::large_enum_variant)] #[derive(Debug, Clone, PartialEq, Eq)] pub enum PaymentStatus { PaymentNotFound, @@ -658,6 +659,7 @@ pub struct ResponseClosePayment { pub status: PaymentStatus, } +#[allow(clippy::large_enum_variant)] #[derive(Debug)] pub enum FunderOutgoingControl { TransactionResult(TransactionResult), From d8301899c5773cd0d30b13b896032034084e2fef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Zmarz=C5=82y?= Date: Fri, 31 May 2019 23:07:05 +0200 Subject: [PATCH 245/248] Refactor app_server/server.rs --- components/app_server/src/server.rs | 300 ++++++++++------------------ 1 file changed, 106 insertions(+), 194 deletions(-) diff --git a/components/app_server/src/server.rs b/components/app_server/src/server.rs index 040098dfe..695084815 100644 --- a/components/app_server/src/server.rs +++ b/components/app_server/src/server.rs @@ -13,7 +13,7 @@ use crypto::payment_id::PaymentId; use crypto::uid::Uid; use proto::funder::messages::{ - FriendStatus, FunderControl, FunderIncomingControl, FunderOutgoingControl, RemoveFriend, + FriendStatus, FunderControl, FunderIncomingControl, FunderOutgoingControl, RequestsStatus, SetFriendStatus, SetRequestsStatus, }; use proto::report::convert::funder_report_mutation_to_index_mutation; @@ -96,8 +96,11 @@ pub struct AppServer { spawner: S, } -/// Check if we should process an app_message from an app with certain permissions -fn check_permissions(app_permissions: &AppPermissions, app_request: &AppRequest) -> bool { +/// Check if we should process an app_request from an app with certain permissions +fn check_request_permissions( + app_permissions: &AppPermissions, + app_request: &AppRequest, +) -> bool { match app_request { AppRequest::AddRelay(_) => app_permissions.config, AppRequest::RemoveRelay(_) => app_permissions.config, @@ -328,190 +331,138 @@ where Ok(()) } - async fn handle_app_message( + pub async fn handle_from_app( &mut self, app_id: u128, - app_message: AppToAppServer, + opt_app_message: Option>, ) -> Result<(), AppServerError> { - // Get the relevant application: - - { - let app = match self.apps.get(&app_id) { - Some(app) => app, - None => { - warn!("App {:?} does not exist!", app_id); - return Ok(()); + match opt_app_message { + None => { + // Remove the application. We assert that this application exists + // in our apps map: + self.apps.remove(&app_id).unwrap(); + if self.apps.is_empty() && self.incoming_connections_closed { + return Err(AppServerError::AllAppsClosed); } - }; - - // Make sure this message is allowed for this application: - if !check_permissions(&app.permissions, &app_message.app_request) { - warn!( - "App {:?} does not have permissions for {:?}", - app_id, app_message - ); - return Ok(()); + Ok(()) } + Some(app_message) => await!(self.handle_app_message(app_id, app_message)), } + } - let app_request_id = app_message.app_request_id; - - match app_message.app_request { - AppRequest::AddRelay(named_relay_address) => { - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::AddRelay(named_relay_address) - ))) - .map_err(|_| AppServerError::SendToFunderError) + fn check_app_permissions(&self, app_id: u128, app_message: &AppToAppServer) -> bool { + // Get the relevant application: + let app = match self.apps.get(&app_id) { + Some(app) => app, + None => { + warn!("App {:?} does not exist!", app_id); + return false; } - AppRequest::RemoveRelay(public_key) => await!(self.to_funder.send( - FunderIncomingControl::new(app_request_id, FunderControl::RemoveRelay(public_key)) - )) - .map_err(|_| AppServerError::SendToFunderError), - AppRequest::CreatePayment(create_payment) => { - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::CreatePayment(create_payment) - ))) + }; + + // Make sure this message is allowed for this application: + if !check_request_permissions(&app.permissions, &app_message.app_request) { + warn!( + "App {:?} does not have permissions for {:?}", + app_id, app_message + ); + return false; + } + + true + } + + async fn handle_app_message( + &mut self, + app_id: u128, + app_message: AppToAppServer, + ) -> Result<(), AppServerError> { + if !self.check_app_permissions(app_id, &app_message) { + return Ok(()); + } + + let AppToAppServer { + app_request, + app_request_id, + } = app_message; + + macro_rules! to_funder { + ( $x:expr ) => {{ + use FunderControl::*; + await!(self.to_funder + .send(FunderIncomingControl::new(app_request_id, $x))) .map_err(|_| AppServerError::SendToFunderError) - } - AppRequest::CreateTransaction(create_transaction) => { + }}; + } + + macro_rules! to_index_client { + ( $x:expr ) => {{ + use IndexClientRequest::*; + await!(self.to_index_client.send(AppServerToIndexClient::AppRequest((app_request_id, $x)))) + .map_err(|_| AppServerError::SendToIndexClientError) + }}; + } + + use AppRequest::*; + match app_request { + // Requests that go to funder: + AddRelay(x) => to_funder!(AddRelay(x)), + RemoveRelay(x) => to_funder!(RemoveRelay(x)), + CreatePayment(x) => to_funder!(CreatePayment(x)), + RequestClosePayment(x) => to_funder!(RequestClosePayment(x)), + AckClosePayment(x) => to_funder!(AckClosePayment(x)), + AddInvoice(x) => to_funder!(AddInvoice(x)), + CancelInvoice(x) => to_funder!(CancelInvoice(x)), + CommitInvoice(x) => to_funder!(CommitInvoice(x)), + AddFriend(x) => to_funder!(AddFriend(x)), + SetFriendRelays(x) => to_funder!(SetFriendRelays(x)), + SetFriendName(x) => to_funder!(SetFriendName(x)), + SetFriendRemoteMaxDebt(x) => to_funder!(SetFriendRemoteMaxDebt(x)), + SetFriendRate(x) => to_funder!(SetFriendRate(x)), + ResetFriendChannel(x) => to_funder!(ResetFriendChannel(x)), + CreateTransaction(create_transaction) => { // Keep track of which application issued this request: self.transactions - .insert(create_transaction.request_id.clone(), app_id); - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::CreateTransaction(create_transaction) - ))) - .map_err(|_| AppServerError::SendToFunderError) - } - AppRequest::RequestClosePayment(request_close_payment) => { - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::RequestClosePayment(request_close_payment) - ))) - .map_err(|_| AppServerError::SendToFunderError) + .insert(create_transaction.request_id, app_id); + to_funder!(CreateTransaction(create_transaction)) } - AppRequest::AckClosePayment(ack_close_payment) => { - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::AckClosePayment(ack_close_payment) - ))) - .map_err(|_| AppServerError::SendToFunderError) - } - AppRequest::AddInvoice(add_invoice) => { - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::AddInvoice(add_invoice) - ))) - .map_err(|_| AppServerError::SendToFunderError) - } - AppRequest::CancelInvoice(invoice_id) => { - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::CancelInvoice(invoice_id) - ))) - .map_err(|_| AppServerError::SendToFunderError) - } - AppRequest::CommitInvoice(multi_commit) => { - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::CommitInvoice(multi_commit) - ))) - .map_err(|_| AppServerError::SendToFunderError) - } - AppRequest::AddFriend(add_friend) => await!(self.to_funder.send( - FunderIncomingControl::new(app_request_id, FunderControl::AddFriend(add_friend)) - )) - .map_err(|_| AppServerError::SendToFunderError), - AppRequest::SetFriendRelays(set_friend_address) => { - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::SetFriendRelays(set_friend_address) - ))) - .map_err(|_| AppServerError::SendToFunderError) - } - AppRequest::SetFriendName(set_friend_name) => { - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::SetFriendName(set_friend_name) - ))) - .map_err(|_| AppServerError::SendToFunderError) + RemoveFriend(friend_public_key) => { + let remove_friend = proto::funder::messages::RemoveFriend { friend_public_key }; + to_funder!(RemoveFriend(remove_friend)) } - AppRequest::RemoveFriend(friend_public_key) => { - let remove_friend = RemoveFriend { friend_public_key }; - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::RemoveFriend(remove_friend) - ))) - .map_err(|_| AppServerError::SendToFunderError) - } - AppRequest::EnableFriend(friend_public_key) => { + EnableFriend(friend_public_key) => { let set_friend_status = SetFriendStatus { friend_public_key, status: FriendStatus::Enabled, }; - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::SetFriendStatus(set_friend_status) - ))) - .map_err(|_| AppServerError::SendToFunderError) + to_funder!(SetFriendStatus(set_friend_status)) } - AppRequest::DisableFriend(friend_public_key) => { + DisableFriend(friend_public_key) => { let set_friend_status = SetFriendStatus { friend_public_key, status: FriendStatus::Disabled, }; - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::SetFriendStatus(set_friend_status) - ))) - .map_err(|_| AppServerError::SendToFunderError) + to_funder!(SetFriendStatus(set_friend_status)) } - AppRequest::OpenFriend(friend_public_key) => { + OpenFriend(friend_public_key) => { let set_requests_status = SetRequestsStatus { friend_public_key, status: RequestsStatus::Open, }; - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::SetRequestsStatus(set_requests_status) - ))) - .map_err(|_| AppServerError::SendToFunderError) + to_funder!(SetRequestsStatus(set_requests_status)) } - AppRequest::CloseFriend(friend_public_key) => { + CloseFriend(friend_public_key) => { let set_requests_status = SetRequestsStatus { friend_public_key, status: RequestsStatus::Closed, }; - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::SetRequestsStatus(set_requests_status) - ))) - .map_err(|_| AppServerError::SendToFunderError) + to_funder!(SetRequestsStatus(set_requests_status)) } - AppRequest::SetFriendRemoteMaxDebt(set_friend_remote_max_debt) => { - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::SetFriendRemoteMaxDebt(set_friend_remote_max_debt) - ))) - .map_err(|_| AppServerError::SendToFunderError) - } - AppRequest::SetFriendRate(set_friend_rate) => { - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::SetFriendRate(set_friend_rate) - ))) - .map_err(|_| AppServerError::SendToFunderError) - } - AppRequest::ResetFriendChannel(reset_friend_channel) => { - await!(self.to_funder.send(FunderIncomingControl::new( - app_request_id, - FunderControl::ResetFriendChannel(reset_friend_channel) - ))) - .map_err(|_| AppServerError::SendToFunderError) - } - AppRequest::RequestRoutes(request_routes) => { + + // Requests that go to index client: + AddIndexServer(x) => to_index_client!(AddIndexServer(x)), + RemoveIndexServer(x) => to_index_client!(RemoveIndexServer(x)), + RequestRoutes(request_routes) => { // Keep track of which application issued this request: if self .route_requests @@ -519,47 +470,8 @@ where { warn!("RequestRoutes: request_id clash."); } - await!(self - .to_index_client - .send(AppServerToIndexClient::AppRequest(( - app_request_id, - IndexClientRequest::RequestRoutes(request_routes) - )))) - .map_err(|_| AppServerError::SendToIndexClientError) + to_index_client!(RequestRoutes(request_routes)) } - AppRequest::AddIndexServer(named_index_server_address) => await!(self - .to_index_client - .send(AppServerToIndexClient::AppRequest(( - app_request_id, - IndexClientRequest::AddIndexServer(named_index_server_address) - )))) - .map_err(|_| AppServerError::SendToIndexClientError), - AppRequest::RemoveIndexServer(index_server_address) => await!(self - .to_index_client - .send(AppServerToIndexClient::AppRequest(( - app_request_id, - IndexClientRequest::RemoveIndexServer(index_server_address) - )))) - .map_err(|_| AppServerError::SendToIndexClientError), - } - } - - pub async fn handle_from_app( - &mut self, - app_id: u128, - opt_app_message: Option>, - ) -> Result<(), AppServerError> { - match opt_app_message { - None => { - // Remove the application. We assert that this application exists - // in our apps map: - self.apps.remove(&app_id).unwrap(); - if self.apps.is_empty() && self.incoming_connections_closed { - return Err(AppServerError::AllAppsClosed); - } - Ok(()) - } - Some(app_message) => await!(self.handle_app_message(app_id, app_message)), } } } From 39c47996d981afc3913042278f2200bb25182dbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Zmarz=C5=82y?= Date: Sat, 1 Jun 2019 01:23:41 +0200 Subject: [PATCH 246/248] Fix clippy lints --- components/app_server/src/server.rs | 19 +++++--- components/funder/src/handler/canceler.rs | 4 +- .../funder/src/handler/handle_friend.rs | 22 ++++----- components/funder/src/handler/sender.rs | 5 +- .../funder/src/mutual_credit/incoming.rs | 2 +- .../funder/src/mutual_credit/outgoing.rs | 2 +- components/funder/src/state.rs | 7 +-- components/funder/src/types.rs | 4 +- .../node/src/connect/node_connection/buyer.rs | 10 ++-- .../node_connection/node_connection.rs | 2 +- components/stctrl/src/buyer.rs | 22 ++++----- components/stctrl/src/seller.rs | 48 +++++++------------ components/stctrl/src/stctrllib.rs | 6 +-- 13 files changed, 67 insertions(+), 86 deletions(-) diff --git a/components/app_server/src/server.rs b/components/app_server/src/server.rs index 695084815..ec4a5cf04 100644 --- a/components/app_server/src/server.rs +++ b/components/app_server/src/server.rs @@ -13,8 +13,8 @@ use crypto::payment_id::PaymentId; use crypto::uid::Uid; use proto::funder::messages::{ - FriendStatus, FunderControl, FunderIncomingControl, FunderOutgoingControl, - RequestsStatus, SetFriendStatus, SetRequestsStatus, + FriendStatus, FunderControl, FunderIncomingControl, FunderOutgoingControl, RequestsStatus, + SetFriendStatus, SetRequestsStatus, }; use proto::report::convert::funder_report_mutation_to_index_mutation; @@ -41,6 +41,7 @@ pub enum AppServerError { AllAppsClosed, } +#[allow(clippy::large_enum_variant)] #[derive(Debug)] pub enum AppServerEvent { IncomingConnection(IncomingAppConnection), @@ -372,6 +373,8 @@ where true } + // Clippy doesn't like `match {}` blocks with that many arms + #[allow(clippy::cognitive_complexity)] async fn handle_app_message( &mut self, app_id: u128, @@ -389,7 +392,8 @@ where macro_rules! to_funder { ( $x:expr ) => {{ use FunderControl::*; - await!(self.to_funder + await!(self + .to_funder .send(FunderIncomingControl::new(app_request_id, $x))) .map_err(|_| AppServerError::SendToFunderError) }}; @@ -398,8 +402,10 @@ where macro_rules! to_index_client { ( $x:expr ) => {{ use IndexClientRequest::*; - await!(self.to_index_client.send(AppServerToIndexClient::AppRequest((app_request_id, $x)))) - .map_err(|_| AppServerError::SendToIndexClientError) + await!(self + .to_index_client + .send(AppServerToIndexClient::AppRequest((app_request_id, $x)))) + .map_err(|_| AppServerError::SendToIndexClientError) }}; } @@ -466,7 +472,8 @@ where // Keep track of which application issued this request: if self .route_requests - .insert(request_routes.request_id, app_id).is_some() + .insert(request_routes.request_id, app_id) + .is_some() { warn!("RequestRoutes: request_id clash."); } diff --git a/components/funder/src/handler/canceler.rs b/components/funder/src/handler/canceler.rs index 7656255de..a0772d8b8 100644 --- a/components/funder/src/handler/canceler.rs +++ b/components/funder/src/handler/canceler.rs @@ -25,7 +25,7 @@ pub fn reply_with_cancel( ) where B: Clone + CanonicalSerialize + PartialEq + Eq + Debug, { - let cancel_send_funds = create_cancel_send_funds(request_id.clone()); + let cancel_send_funds = create_cancel_send_funds(*request_id); let friend_mutation = FriendMutation::PushBackPendingBackwardsOp(BackwardsOp::Cancel(cancel_send_funds)); let funder_mutation = @@ -57,7 +57,7 @@ where .clone(); // Remove transaction: - let funder_mutation = FunderMutation::RemoveTransaction(request_id.clone()); + let funder_mutation = FunderMutation::RemoveTransaction(*request_id); m_state.mutate(funder_mutation); // Update payment: diff --git a/components/funder/src/handler/handle_friend.rs b/components/funder/src/handler/handle_friend.rs index 0589077d2..51e8e47a7 100644 --- a/components/funder/src/handler/handle_friend.rs +++ b/components/funder/src/handler/handle_friend.rs @@ -166,13 +166,8 @@ fn handle_request_send_funds( .open_invoices .get(&request_send_funds.invoice_id) { - if (open_invoice.total_dest_payment == request_send_funds.total_dest_payment) - && (request_send_funds.dest_payment <= request_send_funds.total_dest_payment) - { - true - } else { - false - } + open_invoice.total_dest_payment == request_send_funds.total_dest_payment + && request_send_funds.dest_payment <= request_send_funds.total_dest_payment } else { false }; @@ -221,7 +216,7 @@ fn handle_request_send_funds( let rate = &m_state.state().friends.get(remote_public_key).unwrap().rate; let opt_local_fee = rate.calc_fee(request_send_funds.dest_payment); - let request_id = request_send_funds.request_id.clone(); + let request_id = request_send_funds.request_id; // Make sure that calc_fee() worked, and that we can take this amount of credits: let opt_request_send_funds = if let Some(local_fee) = opt_local_fee { @@ -275,7 +270,7 @@ fn handle_response_send_funds( let commit = prepare_commit(&response_send_funds, &pending_transaction, src_plain_lock); let transaction_result = TransactionResult { - request_id: response_send_funds.request_id.clone(), + request_id: response_send_funds.request_id, result: RequestResult::Success(commit), }; outgoing_control.push(FunderOutgoingControl::TransactionResult(transaction_result)); @@ -392,7 +387,7 @@ fn handle_collect_send_funds( Payment::Success((num_transactions, receipt, ack_uid)) => Some(Payment::Success(( num_transactions.checked_sub(1).unwrap(), receipt.clone(), - ack_uid.clone(), + *ack_uid, ))), Payment::Canceled(_) => unreachable!(), Payment::AfterSuccessAck(num_transactions) => { @@ -407,16 +402,15 @@ fn handle_collect_send_funds( let funder_mutation = if let Some(new_payment) = opt_new_payment { // Update payment: - FunderMutation::UpdatePayment((open_transaction.payment_id.clone(), new_payment)) + FunderMutation::UpdatePayment((open_transaction.payment_id, new_payment)) } else { - FunderMutation::RemovePayment(open_transaction.payment_id.clone()) + FunderMutation::RemovePayment(open_transaction.payment_id) // Remove payment: }; m_state.mutate(funder_mutation); // Remove transaction: - let funder_mutation = - FunderMutation::RemoveTransaction(collect_send_funds.request_id.clone()); + let funder_mutation = FunderMutation::RemoveTransaction(collect_send_funds.request_id); m_state.mutate(funder_mutation); } Some(friend_public_key) => { diff --git a/components/funder/src/handler/sender.rs b/components/funder/src/handler/sender.rs index 44688cc70..47c09c085 100644 --- a/components/funder/src/handler/sender.rs +++ b/components/funder/src/handler/sender.rs @@ -470,9 +470,8 @@ where // The friend with public key `origin_public_key` is the origin of this request. // We send him back a Cancel message: let pending_transaction = create_pending_transaction(request_send_funds); - let cancel_send_funds = BackwardsOp::Cancel(create_cancel_send_funds( - pending_transaction.request_id.clone(), - )); + let cancel_send_funds = + BackwardsOp::Cancel(create_cancel_send_funds(pending_transaction.request_id)); let friend_mutation = FriendMutation::PushBackPendingBackwardsOp(cancel_send_funds); let funder_mutation = FunderMutation::FriendMutation((origin_public_key.clone(), friend_mutation)); diff --git a/components/funder/src/mutual_credit/incoming.rs b/components/funder/src/mutual_credit/incoming.rs index 367f01c51..731b089df 100644 --- a/components/funder/src/mutual_credit/incoming.rs +++ b/components/funder/src/mutual_credit/incoming.rs @@ -297,7 +297,7 @@ fn process_response_send_funds( // Set the stage to Response, and remember dest_hashed_lock: let mc_mutation = McMutation::SetLocalPendingTransactionStage(( - response_send_funds.request_id.clone(), + response_send_funds.request_id, TransactionStage::Response(response_send_funds.dest_hashed_lock.clone()), )); mutual_credit.mutate(&mc_mutation); diff --git a/components/funder/src/mutual_credit/outgoing.rs b/components/funder/src/mutual_credit/outgoing.rs index 7d42c1452..8c1c6b0f8 100644 --- a/components/funder/src/mutual_credit/outgoing.rs +++ b/components/funder/src/mutual_credit/outgoing.rs @@ -221,7 +221,7 @@ impl OutgoingMc { // Set the stage to Response, and remember dest_hashed_lock: let mc_mutation = McMutation::SetRemotePendingTransactionStage(( - response_send_funds.request_id.clone(), + response_send_funds.request_id, TransactionStage::Response(response_send_funds.dest_hashed_lock.clone()), )); self.mutual_credit.mutate(&mc_mutation); diff --git a/components/funder/src/state.rs b/components/funder/src/state.rs index 8185a1ad3..a358392bb 100644 --- a/components/funder/src/state.rs +++ b/components/funder/src/state.rs @@ -38,6 +38,7 @@ pub struct NewTransactions { pub dest_public_key: PublicKey, } +#[allow(clippy::large_enum_variant)] #[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] pub enum Payment { /// User can add new transactions @@ -56,7 +57,7 @@ pub enum Payment { #[derive(Clone, Serialize, Deserialize, Debug)] pub struct IncomingTransaction { pub request_id: Uid, - /// The lock we used on our ResponseSendFundsOp message. + /// The lock we used on our ResponseSendFundsOp message. /// We have to keep it, otherwise we will not be able to send a valid CollectSendFundsOp later. pub dest_plain_lock: PlainLock, } @@ -170,7 +171,7 @@ where FunderMutation::AddIncomingTransaction((invoice_id, request_id, dest_plain_lock)) => { let open_invoice = self.open_invoices.get_mut(invoice_id).unwrap(); let incoming_transaction = IncomingTransaction { - request_id: request_id.clone(), + request_id: *request_id, dest_plain_lock: dest_plain_lock.clone(), }; open_invoice @@ -182,7 +183,7 @@ where } FunderMutation::AddTransaction((request_id, payment_id, src_plain_lock)) => { let open_transaction = OpenTransaction { - payment_id: payment_id.clone(), + payment_id: *payment_id, src_plain_lock: src_plain_lock.clone(), opt_response: None, }; diff --git a/components/funder/src/types.rs b/components/funder/src/types.rs index fe485666d..79ef84a08 100644 --- a/components/funder/src/types.rs +++ b/components/funder/src/types.rs @@ -74,9 +74,7 @@ pub async fn create_response_send_funds<'a>( } pub fn create_cancel_send_funds(request_id: Uid) -> CancelSendFundsOp { - CancelSendFundsOp { - request_id, - } + CancelSendFundsOp { request_id } } pub fn create_pending_transaction(request_send_funds: &RequestSendFundsOp) -> PendingTransaction { diff --git a/components/node/src/connect/node_connection/buyer.rs b/components/node/src/connect/node_connection/buyer.rs index ab7cbc71a..1c54584fb 100644 --- a/components/node/src/connect/node_connection/buyer.rs +++ b/components/node/src/connect/node_connection/buyer.rs @@ -138,10 +138,8 @@ where payment_id: PaymentId, ) -> Result { let app_request_id = Uid::new(&self.rng); - let to_app_server = AppToAppServer::new( - app_request_id, - AppRequest::RequestClosePayment(payment_id.clone()), - ); + let to_app_server = + AppToAppServer::new(app_request_id, AppRequest::RequestClosePayment(payment_id)); let mut incoming_response_close_payment = await!(self.response_close_payments_mc.request_stream()) @@ -168,8 +166,8 @@ where ) -> Result<(), BuyerError> { let app_request_id = Uid::new(&self.rng); let ack_close_payment = AckClosePayment { - payment_id: payment_id, - ack_uid: ack_uid, + payment_id, + ack_uid, }; let to_app_server = AppToAppServer::new( diff --git a/components/node/src/connect/node_connection/node_connection.rs b/components/node/src/connect/node_connection/node_connection.rs index a8019a9f4..0cec693b3 100644 --- a/components/node/src/connect/node_connection/node_connection.rs +++ b/components/node/src/connect/node_connection/node_connection.rs @@ -11,10 +11,10 @@ use common::multi_consumer::{multi_consumer_service, MultiConsumerClient}; use common::mutable_state::BatchMutable; use common::state_service::{state_service, StateClient}; +use super::buyer::AppBuyer; use super::config::AppConfig; use super::report::AppReport; use super::routes::AppRoutes; -use super::buyer::AppBuyer; use super::seller::AppSeller; pub type NodeConnectionTuple = ( diff --git a/components/stctrl/src/buyer.rs b/components/stctrl/src/buyer.rs index 879b8337d..5cfdf5091 100644 --- a/components/stctrl/src/buyer.rs +++ b/components/stctrl/src/buyer.rs @@ -4,7 +4,7 @@ use std::path::PathBuf; use futures::future::select_all; use app::ser_string::{payment_id_to_string, string_to_payment_id}; -use app::{AppRoutes, AppBuyer, MultiCommit, NodeConnection, PaymentStatus, PublicKey}; +use app::{AppBuyer, AppRoutes, MultiCommit, NodeConnection, PaymentStatus, PublicKey}; use structopt::StructOpt; @@ -135,7 +135,7 @@ async fn buyer_pay_invoice( await!(app_buyer.create_payment( payment_id, invoice.invoice_id.clone(), - invoice.dest_payment.clone(), + invoice.dest_payment, invoice.dest_public_key.clone() )) .map_err(|_| BuyerError::CreatePaymentFailed)?; @@ -152,7 +152,7 @@ async fn buyer_pay_invoice( // TODO: Possibly a more efficient way than Box::pin? Box::pin(async move { await!(c_app_buyer.create_transaction( - payment_id.clone(), + payment_id, request_id, route.route.clone(), *dest_payment, @@ -168,14 +168,14 @@ async fn buyer_pay_invoice( match output { Ok(commit) => commits.push(commit), Err(_) => { - let _ = await!(app_buyer.request_close_payment(payment_id.clone())); + let _ = await!(app_buyer.request_close_payment(payment_id)); return Err(BuyerError::CreateTransactionFailed); } } fut_list = new_fut_list; } - let _ = await!(app_buyer.request_close_payment(payment_id.clone())); + let _ = await!(app_buyer.request_close_payment(payment_id)); let multi_commit = MultiCommit { invoice_id: invoice.invoice_id.clone(), @@ -212,7 +212,7 @@ async fn buyer_payment_status( let payment_id = string_to_payment_id(&payment_id).map_err(|_| BuyerError::ParsePaymentIdError)?; - let payment_status = await!(app_buyer.request_close_payment(payment_id.clone())) + let payment_status = await!(app_buyer.request_close_payment(payment_id)) .map_err(|_| BuyerError::RequestClosePaymentError)?; let opt_ack_uid = match payment_status { @@ -240,7 +240,7 @@ async fn buyer_payment_status( }; if let Some(ack_uid) = opt_ack_uid { - await!(app_buyer.ack_close_payment(payment_id.clone(), ack_uid)) + await!(app_buyer.ack_close_payment(payment_id, ack_uid)) .map_err(|_| BuyerError::AckClosePaymentError)?; } @@ -279,11 +279,9 @@ pub async fn buyer( app_buyer, writer, ))?, - BuyerCmd::PaymentStatus(payment_status_cmd) => await!(buyer_payment_status( - payment_status_cmd, - app_buyer, - writer, - ))?, + BuyerCmd::PaymentStatus(payment_status_cmd) => { + await!(buyer_payment_status(payment_status_cmd, app_buyer, writer,))? + } } Ok(()) diff --git a/components/stctrl/src/seller.rs b/components/stctrl/src/seller.rs index e78d390b1..e973b280e 100644 --- a/components/stctrl/src/seller.rs +++ b/components/stctrl/src/seller.rs @@ -1,12 +1,11 @@ use std::path::PathBuf; -use app::{NodeConnection, PublicKey, AppSeller}; use app::gen::gen_invoice_id; +use app::{AppSeller, NodeConnection, PublicKey}; -use crate::file::invoice::{Invoice, store_invoice_to_file, load_invoice_from_file}; +use crate::file::invoice::{load_invoice_from_file, store_invoice_to_file, Invoice}; use crate::file::multi_commit::load_multi_commit_from_file; - use structopt::StructOpt; /// Create invoice @@ -68,16 +67,12 @@ pub enum SellerError { InvoiceCommitMismatch, } - async fn seller_create_invoice( create_invoice_cmd: CreateInvoiceCmd, local_public_key: PublicKey, mut app_seller: AppSeller, ) -> Result<(), SellerError> { - let CreateInvoiceCmd { - amount, - output, - } = create_invoice_cmd; + let CreateInvoiceCmd { amount, output } = create_invoice_cmd; // Make sure we don't override an existing invoice file: if output.exists() { @@ -97,26 +92,20 @@ async fn seller_create_invoice( await!(app_seller.add_invoice(invoice_id.clone(), amount)) .map_err(|_| SellerError::AddInvoiceError)?; - - store_invoice_to_file(&invoice, &output) - .map_err(|_| SellerError::StoreInvoiceError) - + store_invoice_to_file(&invoice, &output).map_err(|_| SellerError::StoreInvoiceError) } async fn seller_cancel_invoice( cancel_invoice_cmd: CancelInvoiceCmd, mut app_seller: AppSeller, ) -> Result<(), SellerError> { - let CancelInvoiceCmd { - invoice_file - } = cancel_invoice_cmd; + let CancelInvoiceCmd { invoice_file } = cancel_invoice_cmd; - let invoice = load_invoice_from_file(&invoice_file) - .map_err(|_| SellerError::LoadInvoiceError)?; + let invoice = + load_invoice_from_file(&invoice_file).map_err(|_| SellerError::LoadInvoiceError)?; await!(app_seller.cancel_invoice(invoice.invoice_id)) .map_err(|_| SellerError::CancelInvoiceError) - } async fn seller_commit_invoice( @@ -131,18 +120,17 @@ async fn seller_commit_invoice( // Note: We don't really need the invoice for the internal API. // We require it here to enforce the user to understand that the commit file corresponds to a // certain invoice file. - let invoice = load_invoice_from_file(&invoice_file) - .map_err(|_| SellerError::LoadInvoiceError)?; + let invoice = + load_invoice_from_file(&invoice_file).map_err(|_| SellerError::LoadInvoiceError)?; - let multi_commit = load_multi_commit_from_file(&commit_file) - .map_err(|_| SellerError::LoadCommitError)?; + let multi_commit = + load_multi_commit_from_file(&commit_file).map_err(|_| SellerError::LoadCommitError)?; if multi_commit.invoice_id != invoice.invoice_id { return Err(SellerError::InvoiceCommitMismatch); } - await!(app_seller.commit_invoice(multi_commit)) - .map_err(|_| SellerError::CommitInvoiceError) + await!(app_seller.commit_invoice(multi_commit)).map_err(|_| SellerError::CommitInvoiceError) } pub async fn seller( @@ -169,13 +157,13 @@ pub async fn seller( local_public_key, app_seller, ))?, - SellerCmd::CancelInvoice(cancel_invoice_cmd) => await!(seller_cancel_invoice( - cancel_invoice_cmd, - app_seller))?, + SellerCmd::CancelInvoice(cancel_invoice_cmd) => { + await!(seller_cancel_invoice(cancel_invoice_cmd, app_seller))? + } - SellerCmd::CommitInvoice(commit_invoice_cmd) => await!(seller_commit_invoice( - commit_invoice_cmd, - app_seller))?, + SellerCmd::CommitInvoice(commit_invoice_cmd) => { + await!(seller_commit_invoice(commit_invoice_cmd, app_seller))? + } } Ok(()) diff --git a/components/stctrl/src/stctrllib.rs b/components/stctrl/src/stctrllib.rs index a4eeae1e3..c9a85ea8e 100644 --- a/components/stctrl/src/stctrllib.rs +++ b/components/stctrl/src/stctrllib.rs @@ -6,9 +6,9 @@ use futures::executor::ThreadPool; use structopt::StructOpt; use crate::buyer::{buyer, BuyerCmd, BuyerError}; -use crate::seller::{seller, SellerCmd, SellerError}; use crate::config::{config, ConfigCmd, ConfigError}; use crate::info::{info, InfoCmd, InfoError}; +use crate::seller::{seller, SellerCmd, SellerError}; use app::{connect, identity_from_file, load_node_from_file}; @@ -128,9 +128,7 @@ pub fn stctrl(st_ctrl_cmd: StCtrlCmd, writer: &mut impl io::Write) -> Result<(), StCtrlSubcommand::Buyer(buyer_cmd) => { await!(buyer(buyer_cmd, node_connection, writer))? } - StCtrlSubcommand::Seller(seller_cmd) => { - await!(seller(seller_cmd, node_connection))? - } + StCtrlSubcommand::Seller(seller_cmd) => await!(seller(seller_cmd, node_connection))?, } Ok(()) }) From 4c0ee368028433c625e931e2f34b7c2984a88d33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Zmarz=C5=82y?= Date: Sat, 1 Jun 2019 17:00:37 +0200 Subject: [PATCH 247/248] Fix more clippy lints --- components/funder/src/handler/canceler.rs | 10 ++++---- .../funder/src/handler/handle_control.rs | 23 +++++++++---------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/components/funder/src/handler/canceler.rs b/components/funder/src/handler/canceler.rs index a0772d8b8..e981937b1 100644 --- a/components/funder/src/handler/canceler.rs +++ b/components/funder/src/handler/canceler.rs @@ -84,7 +84,7 @@ where Some(Payment::Success(( new_num_transactions, receipt.clone(), - request_id.clone(), + request_id, ))) } Payment::Canceled(_) => { @@ -101,9 +101,9 @@ where }; let funder_mutation = if let Some(new_payment) = opt_new_payment { - FunderMutation::UpdatePayment((open_transaction.payment_id.clone(), new_payment)) + FunderMutation::UpdatePayment((open_transaction.payment_id, new_payment)) } else { - FunderMutation::RemovePayment(open_transaction.payment_id.clone()) + FunderMutation::RemovePayment(open_transaction.payment_id) }; m_state.mutate(funder_mutation); } @@ -147,7 +147,7 @@ pub fn cancel_local_pending_transactions( // We have found the friend that is the origin of this request. // We send him a cancel message. let cancel_send_funds = - create_cancel_send_funds(pending_local_transaction.request_id.clone()); + create_cancel_send_funds(pending_local_transaction.request_id); let friend_mutation = FriendMutation::PushBackPendingBackwardsOp( BackwardsOp::Cancel(cancel_send_funds), ); @@ -197,7 +197,7 @@ pub fn cancel_pending_requests( Some(origin_public_key) => { let pending_local_transaction = create_pending_transaction(&pending_request); let cancel_send_funds = - create_cancel_send_funds(pending_local_transaction.request_id.clone()); + create_cancel_send_funds(pending_local_transaction.request_id); let friend_mutation = FriendMutation::PushBackPendingBackwardsOp( BackwardsOp::Cancel(cancel_send_funds), ); diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index 5369b2289..fc1cbf436 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -665,11 +665,11 @@ where .open_transactions .get(&create_transaction.request_id) { - match ( + if let (Some(response_send_funds), Some(pending_transaction)) = ( &open_transaction.opt_response, find_local_pending_transaction(m_state.state(), &create_transaction.request_id), ) { - (Some(response_send_funds), Some(pending_transaction)) => { + let commit = prepare_commit( response_send_funds, pending_transaction, @@ -677,12 +677,11 @@ where ); let transaction_result = TransactionResult { - request_id: response_send_funds.request_id.clone(), + request_id: response_send_funds.request_id, result: RequestResult::Success(commit), }; outgoing_control.push(FunderOutgoingControl::TransactionResult(transaction_result)); - } - _ => {} + } return Ok(()); } @@ -726,7 +725,7 @@ where } else { // Payment not found: let response_close_payment = ResponseClosePayment { - payment_id: payment_id.clone(), + payment_id, status: PaymentStatus::PaymentNotFound, }; outgoing_control.push(FunderOutgoingControl::ResponseClosePayment( @@ -744,7 +743,7 @@ where (if *num_transactions == 0 { let ack_uid = Uid::new(rng); ( - Some(Payment::Canceled(ack_uid.clone())), + Some(Payment::Canceled(ack_uid)), PaymentStatus::Canceled(ack_uid), ) } else { @@ -774,7 +773,7 @@ where // Send back a ResponseClosePayment: let response_close_payment = ResponseClosePayment { - payment_id: payment_id.clone(), + payment_id, status: payment_status, }; outgoing_control.push(FunderOutgoingControl::ResponseClosePayment( @@ -828,14 +827,14 @@ where // Update payment to be `AfterSuccessAck`: let new_payment = Payment::AfterSuccessAck(num_transactions); let funder_mutation = FunderMutation::UpdatePayment(( - ack_close_payment.payment_id.clone(), + ack_close_payment.payment_id, new_payment, )); m_state.mutate(funder_mutation); } else { // Remove payment (no pending transactions): let funder_mutation = - FunderMutation::RemovePayment(ack_close_payment.payment_id.clone()); + FunderMutation::RemovePayment(ack_close_payment.payment_id); m_state.mutate(funder_mutation); } } @@ -847,7 +846,7 @@ where // Remove payment: let funder_mutation = - FunderMutation::RemovePayment(ack_close_payment.payment_id.clone()); + FunderMutation::RemovePayment(ack_close_payment.payment_id); m_state.mutate(funder_mutation); } }; @@ -954,7 +953,7 @@ where }; let collect_send_funds = CollectSendFundsOp { - request_id: incoming_transaction.request_id.clone(), + request_id: incoming_transaction.request_id, src_plain_lock: commit.src_plain_lock.clone(), dest_plain_lock: incoming_transaction.dest_plain_lock.clone(), }; From 66e619b491e388c3ac5cb79dc6b43af9eb25e4ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Zmarz=C5=82y?= Date: Sat, 1 Jun 2019 17:56:20 +0200 Subject: [PATCH 248/248] cargo fmt --- .../funder/src/handler/handle_control.rs | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/components/funder/src/handler/handle_control.rs b/components/funder/src/handler/handle_control.rs index fc1cbf436..2d9e0ec03 100644 --- a/components/funder/src/handler/handle_control.rs +++ b/components/funder/src/handler/handle_control.rs @@ -669,19 +669,17 @@ where &open_transaction.opt_response, find_local_pending_transaction(m_state.state(), &create_transaction.request_id), ) { + let commit = prepare_commit( + response_send_funds, + pending_transaction, + open_transaction.src_plain_lock.clone(), + ); - let commit = prepare_commit( - response_send_funds, - pending_transaction, - open_transaction.src_plain_lock.clone(), - ); - - let transaction_result = TransactionResult { - request_id: response_send_funds.request_id, - result: RequestResult::Success(commit), - }; - outgoing_control.push(FunderOutgoingControl::TransactionResult(transaction_result)); - + let transaction_result = TransactionResult { + request_id: response_send_funds.request_id, + result: RequestResult::Success(commit), + }; + outgoing_control.push(FunderOutgoingControl::TransactionResult(transaction_result)); } return Ok(()); } @@ -826,15 +824,12 @@ where if num_transactions > 0 { // Update payment to be `AfterSuccessAck`: let new_payment = Payment::AfterSuccessAck(num_transactions); - let funder_mutation = FunderMutation::UpdatePayment(( - ack_close_payment.payment_id, - new_payment, - )); + let funder_mutation = + FunderMutation::UpdatePayment((ack_close_payment.payment_id, new_payment)); m_state.mutate(funder_mutation); } else { // Remove payment (no pending transactions): - let funder_mutation = - FunderMutation::RemovePayment(ack_close_payment.payment_id); + let funder_mutation = FunderMutation::RemovePayment(ack_close_payment.payment_id); m_state.mutate(funder_mutation); } } @@ -845,8 +840,7 @@ where } // Remove payment: - let funder_mutation = - FunderMutation::RemovePayment(ack_close_payment.payment_id); + let funder_mutation = FunderMutation::RemovePayment(ack_close_payment.payment_id); m_state.mutate(funder_mutation); } };