Skip to content

Commit

Permalink
Merge pull request #242 from freedomlayer/real/feat/reduced-commit
Browse files Browse the repository at this point in the history
Reduce MultiCommit into Commit
  • Loading branch information
realcr committed Oct 26, 2019
2 parents efcdbd1 + 78feb02 commit b607abf
Show file tree
Hide file tree
Showing 34 changed files with 631 additions and 449 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Expand Up @@ -49,8 +49,9 @@ matrix:
os: osx
rust: none
osx_image: xcode9.4.1
# Solution due to https://travis-ci.community/t/homebrew-syntax-error/5623 :
before_script:
- brew install capnp
- HOMEBREW_NO_AUTO_UPDATE=1 brew install capnp
script:
- cargo test

Expand Down
6 changes: 4 additions & 2 deletions components/app/src/app_conn/buyer.rs
Expand Up @@ -107,7 +107,8 @@ where
route: FriendsRoute,
dest_payment: u128,
fees: u128,
) -> Result<Commit, BuyerError> {
// TODO: Possibly use a new type instead of `Option<Commit>` ?
) -> Result<Option<Commit>, BuyerError> {
let create_transaction = CreateTransaction {
payment_id,
request_id: request_id.clone(),
Expand Down Expand Up @@ -138,7 +139,8 @@ where
continue;
}
match transaction_result.result {
RequestResult::Success(commit) => return Ok(commit),
RequestResult::Complete(commit) => return Ok(Some(commit)),
RequestResult::Success => return Ok(None),
RequestResult::Failure => return Err(BuyerError::NodeError),
}
}
Expand Down
10 changes: 4 additions & 6 deletions components/app/src/app_conn/seller.rs
Expand Up @@ -7,7 +7,7 @@ use proto::crypto::{InvoiceId, Uid};
use crypto::rand::{CryptoRandom, OffstSystemRandom, RandGen};

use proto::app_server::messages::{AppRequest, AppToAppServer};
use proto::funder::messages::{AddInvoice, Currency, MultiCommit};
use proto::funder::messages::{AddInvoice, Commit, Currency};

// TODO: Different in naming convention from AppConfigError and AppRoutesError:
#[derive(Debug)]
Expand Down Expand Up @@ -114,12 +114,10 @@ where
Err(SellerError::NoResponse)
}

pub async fn commit_invoice(&mut self, multi_commit: MultiCommit) -> Result<(), SellerError> {
pub async fn commit_invoice(&mut self, commit: Commit) -> Result<(), SellerError> {
let app_request_id = Uid::rand_gen(&self.rng);
let to_app_server = AppToAppServer::new(
app_request_id.clone(),
AppRequest::CommitInvoice(multi_commit),
);
let to_app_server =
AppToAppServer::new(app_request_id.clone(), AppRequest::CommitInvoice(commit));

// Start listening to done requests:
let mut incoming_done_requests = self
Expand Down
4 changes: 2 additions & 2 deletions components/app/src/lib.rs
Expand Up @@ -24,8 +24,8 @@ pub use proto::ser_string;

pub use proto::app_server::messages::{AppPermissions, NamedRelayAddress, RelayAddress};
pub use proto::funder::messages::{
BalanceInfo, Commit, CountersInfo, Currency, CurrencyBalanceInfo, McInfo, MultiCommit,
PaymentStatus, PaymentStatusSuccess, Rate, Receipt, TokenInfo,
BalanceInfo, Commit, CountersInfo, Currency, CurrencyBalanceInfo, McInfo, PaymentStatus,
PaymentStatusSuccess, Rate, Receipt, TokenInfo,
};
pub use proto::index_server::messages::NamedIndexServerAddress;

Expand Down
35 changes: 22 additions & 13 deletions components/funder/src/handler/canceler.rs
Expand Up @@ -14,7 +14,7 @@ use crate::handler::types::SendCommands;
use crate::handler::utils::find_request_origin;

use crate::friend::{BackwardsOp, ChannelStatus, FriendMutation};
use crate::state::{FunderMutation, Payment};
use crate::state::{FunderMutation, Payment, PaymentStage};
use crate::types::{create_cancel_send_funds, create_pending_transaction};

#[derive(Debug)]
Expand Down Expand Up @@ -72,47 +72,56 @@ where
let funder_mutation = FunderMutation::RemoveTransaction(request_id.clone());
m_state.mutate(funder_mutation);

let Payment {
src_plain_lock,
stage,
} = payment;

// Update payment:
// - Decrease num_transactions
// - Possibly remove payment
let opt_new_payment = match payment {
Payment::NewTransactions(new_transactions) => {
let opt_new_stage = match stage {
PaymentStage::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))
Some(PaymentStage::NewTransactions(new_new_transactions))
}
Payment::InProgress(num_transactions) => {
PaymentStage::InProgress(num_transactions) => {
let new_num_transactions = num_transactions.checked_sub(1).unwrap();
if new_num_transactions > 0 {
Some(Payment::InProgress(new_num_transactions))
Some(PaymentStage::InProgress(new_num_transactions))
} else {
let ack_uid = Uid::rand_gen(rng);
Some(Payment::Canceled(ack_uid))
Some(PaymentStage::Canceled(ack_uid))
}
}
Payment::Success((num_transactions, receipt, request_id)) => {
PaymentStage::Success((num_transactions, receipt, request_id)) => {
let new_num_transactions = num_transactions.checked_sub(1).unwrap();
Some(Payment::Success((
Some(PaymentStage::Success((
new_num_transactions,
receipt.clone(),
request_id,
)))
}
Payment::Canceled(_) => {
PaymentStage::Canceled(_) => {
unreachable!();
}
Payment::AfterSuccessAck(num_transactions) => {
PaymentStage::AfterSuccessAck(num_transactions) => {
let new_num_transactions = num_transactions.checked_sub(1).unwrap();
if new_num_transactions > 0 {
Some(Payment::AfterSuccessAck(new_num_transactions))
Some(PaymentStage::AfterSuccessAck(new_num_transactions))
} else {
None
}
}
};

let funder_mutation = if let Some(new_payment) = opt_new_payment {
let funder_mutation = if let Some(new_stage) = opt_new_stage {
let new_payment = Payment {
src_plain_lock,
stage: new_stage,
};
FunderMutation::UpdatePayment((open_transaction.payment_id, new_payment))
} else {
FunderMutation::RemovePayment(open_transaction.payment_id)
Expand Down

0 comments on commit b607abf

Please sign in to comment.