Skip to content

Commit

Permalink
V4 Slate field tweaks (#386)
Browse files Browse the repository at this point in the history
* various tweaks to V4 slate

* field renaming
  • Loading branch information
yeastplume authored Apr 21, 2020
1 parent 0d0d947 commit f726d35
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 89 deletions.
2 changes: 1 addition & 1 deletion controller/tests/payment_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn payment_proofs_test_impl(test_dir: &'static str) -> Result<(), libwallet::Err

// Check we are creating a tx with the expected lock_height of 0.
// We will check this produces a Plain kernel later.
assert_eq!(0, slate.lock_height());
assert_eq!(0, slate.lock_height);

slate = client1.send_tx_slate_direct("wallet2", &slate_i)?;
sender_api.tx_lock_outputs(m, &slate)?;
Expand Down
2 changes: 1 addition & 1 deletion controller/tests/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn basic_transaction_api(test_dir: &'static str) -> Result<(), libwallet::Error>

// Check we are creating a tx with the expected lock_height of 0.
// We will check this produces a Plain kernel later.
assert_eq!(0, slate.lock_height());
assert_eq!(0, slate.lock_height);

slate = client1.send_tx_slate_direct("wallet2", &slate_i)?;
assert_eq!(slate.state, SlateState::Standard2);
Expand Down
6 changes: 3 additions & 3 deletions libwallet/src/api_impl/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ where

// update ttl if desired
if let Some(b) = args.ttl_blocks {
ret_slate.ttl_cutoff_height = Some(height + b);
ret_slate.ttl_cutoff_height = height + b;
}

// if this is compact mode, we need to create the transaction now
Expand Down Expand Up @@ -896,8 +896,8 @@ where
{
// Refuse if TTL is expired
let last_confirmed_height = w.last_confirmed_height()?;
if let Some(e) = slate.ttl_cutoff_height {
if last_confirmed_height >= e {
if slate.ttl_cutoff_height != 0 {
if last_confirmed_height >= slate.ttl_cutoff_height {
return Err(ErrorKind::TransactionExpired.into());
}
}
Expand Down
10 changes: 8 additions & 2 deletions libwallet/src/internal/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ where
let filename = format!("{}.grintx", slate_id);
t.stored_tx = Some(filename);
t.fee = Some(slate.fee);
t.ttl_cutoff_height = slate.ttl_cutoff_height;
t.ttl_cutoff_height = match slate.ttl_cutoff_height {
0 => None,
n => Some(n),
};

if let Ok(e) = slate.calc_excess(keychain.secp()) {
t.kernel_excess = Some(e)
Expand Down Expand Up @@ -271,7 +274,10 @@ where
t.tx_slate_id = Some(slate_id);
t.amount_credited = amount;
t.num_outputs = 1;
t.ttl_cutoff_height = slate.ttl_cutoff_height;
t.ttl_cutoff_height = match slate.ttl_cutoff_height {
0 => None,
n => Some(n),
};
// when invoicing, this will be invalid
if let Ok(e) = slate.calc_excess(keychain.secp()) {
t.kernel_excess = Some(e)
Expand Down
6 changes: 3 additions & 3 deletions libwallet/src/internal/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn new_tx_slate<'a, T: ?Sized, C, K>(
wallet: &mut T,
amount: u64,
is_invoice: bool,
num_participants: usize,
num_participants: u8,
use_test_rng: bool,
ttl_blocks: Option<u64>,
) -> Result<Slate, Error>
Expand All @@ -57,7 +57,7 @@ where
let current_height = wallet.w2n_client().get_chain_tip()?.0;
let mut slate = Slate::blank(num_participants, is_invoice);
if let Some(b) = ttl_blocks {
slate.ttl_cutoff_height = Some(current_height + b);
slate.ttl_cutoff_height = current_height + b;
}
if use_test_rng {
{
Expand All @@ -83,7 +83,7 @@ where

// Set the lock_height explicitly to 0 here.
// This will generate a Plain kernel (rather than a HeightLocked kernel).
slate.lock_height = None;
slate.lock_height = 0;

Ok(slate)
}
Expand Down
74 changes: 33 additions & 41 deletions libwallet/src/slate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub struct Slate {
/// Versioning info
pub version_info: VersionCompatInfo,
/// The number of participants intended to take part in this transaction
pub num_participants: Option<usize>,
pub num_participants: u8,
/// Unique transaction ID, selected by sender
pub id: Uuid,
/// Slate state
Expand All @@ -109,11 +109,11 @@ pub struct Slate {
/// fee amount
pub fee: u64,
/// Lock height
pub lock_height: Option<u64>,
pub lock_height: u64,
/// TTL, the block height at which wallets
/// should refuse to process the transaction and unlock all
/// associated outputs
pub ttl_cutoff_height: Option<u64>,
pub ttl_cutoff_height: u64,
/// Participant data, each participant in the transaction will
/// insert their public data here. For now, 0 is sender and 1
/// is receiver, though this will change for multi-party
Expand Down Expand Up @@ -180,18 +180,10 @@ impl Slate {
}

/// number of participants
pub fn num_participants(&self) -> usize {
pub fn num_participants(&self) -> u8 {
match self.num_participants {
Some(n) => n,
None => 2,
}
}

/// Lock Height
pub fn lock_height(&self) -> u64 {
match self.lock_height {
Some(n) => n,
None => 0,
0 => 2,
n => n,
}
}

Expand All @@ -216,10 +208,10 @@ impl Slate {
}

/// Create a new slate
pub fn blank(num_participants: usize, is_invoice: bool) -> Slate {
pub fn blank(num_participants: u8, is_invoice: bool) -> Slate {
let np = match num_participants {
2 => None,
n => Some(n),
0 => 2,
n => n,
};
let state = match is_invoice {
true => SlateState::Invoice1,
Expand All @@ -232,8 +224,8 @@ impl Slate {
tx: Some(Transaction::empty()),
amount: 0,
fee: 0,
lock_height: None,
ttl_cutoff_height: None,
lock_height: 0,
ttl_cutoff_height: 0,
participant_data: vec![],
version_info: VersionCompatInfo {
version: CURRENT_SLATE_VERSION,
Expand All @@ -252,7 +244,7 @@ impl Slate {
K: Keychain,
{
let pub_nonce = PublicKey::from_secret_key(keychain.secp(), &sec_nonce)?;
for i in 0..self.num_participants() {
for i in 0..self.num_participants() as usize {
// find my entry
if self.participant_data[i].public_nonce == pub_nonce {
return Ok(Some(self.participant_data[i].clone()));
Expand Down Expand Up @@ -315,11 +307,11 @@ impl Slate {
// Construct the appropriate kernel features based on our fee and lock_height.
// If lock_height is 0 then its a plain kernel, otherwise its a height locked kernel.
fn kernel_features(&self) -> KernelFeatures {
match self.lock_height() {
match self.lock_height {
0 => KernelFeatures::Plain { fee: self.fee },
_ => KernelFeatures::HeightLocked {
fee: self.fee,
lock_height: self.lock_height(),
lock_height: self.lock_height,
},
}
}
Expand Down Expand Up @@ -356,7 +348,7 @@ impl Slate {
&self.msg_to_sign()?,
)?;
let pub_excess = PublicKey::from_secret_key(keychain.secp(), &sec_key)?;
for i in 0..self.num_participants() {
for i in 0..self.num_participants() as usize {
// find my entry
if self.participant_data[i].public_blind_excess == pub_excess {
self.participant_data[i].part_sig = Some(sig_part);
Expand Down Expand Up @@ -638,14 +630,14 @@ impl From<CbData> for CoinbaseV4 {
impl From<Slate> for SlateV4 {
fn from(slate: Slate) -> SlateV4 {
let Slate {
num_participants,
num_participants: num_parts,
id,
state,
tx: _,
amount,
fee,
lock_height,
ttl_cutoff_height,
lock_height: lock_hgt,
ttl_cutoff_height: ttl,
participant_data,
version_info,
payment_proof,
Expand All @@ -658,14 +650,14 @@ impl From<Slate> for SlateV4 {
};
let sta = SlateStateV4::from(&state);
SlateV4 {
num_participants,
num_parts,
id,
sta,
coms: (&slate).into(),
amt: amount,
fee,
lock_height,
ttl_cutoff_height,
lock_hgt,
ttl,
sigs: participant_data,
ver,
payment_proof,
Expand All @@ -688,12 +680,12 @@ impl From<&Slate> for SlateV4 {
version_info,
payment_proof,
} = slate;
let num_participants = *num_participants;
let num_parts = *num_participants;
let id = *id;
let amount = *amount;
let fee = *fee;
let lock_height = *lock_height;
let ttl_cutoff_height = *ttl_cutoff_height;
let lock_hgt = *lock_height;
let ttl = *ttl_cutoff_height;
let participant_data = map_vec!(participant_data, |data| ParticipantDataV4::from(data));
let ver = VersionCompatInfoV4::from(version_info);
let payment_proof = match payment_proof {
Expand All @@ -703,14 +695,14 @@ impl From<&Slate> for SlateV4 {
let sta = SlateStateV4::from(state);

SlateV4 {
num_participants,
num_parts,
id,
sta,
coms: slate.into(),
amt: amount,
fee,
lock_height,
ttl_cutoff_height,
lock_hgt,
ttl,
sigs: participant_data,
ver,
payment_proof,
Expand Down Expand Up @@ -890,14 +882,14 @@ impl From<&TxKernel> for TxKernelV4 {
impl From<SlateV4> for Slate {
fn from(slate: SlateV4) -> Slate {
let SlateV4 {
num_participants,
num_parts: num_participants,
id,
sta,
coms: _,
amt: amount,
fee,
lock_height,
ttl_cutoff_height,
lock_hgt: lock_height,
ttl: ttl_cutoff_height,
sigs: participant_data,
ver,
payment_proof,
Expand Down Expand Up @@ -949,9 +941,9 @@ pub fn tx_from_slate_v4(slate: &SlateV4) -> Option<Transaction> {
Err(_) => Signature::from_raw_data(&[0; 64]).unwrap(),
};
let kernel = TxKernel {
features: match slate.lock_height {
Some(0) | None => KernelFeatures::Plain { fee: slate.fee },
Some(n) => KernelFeatures::HeightLocked {
features: match slate.lock_hgt {
0 => KernelFeatures::Plain { fee: slate.fee },
n => KernelFeatures::HeightLocked {
fee: slate.fee,
lock_height: n,
},
Expand Down
Loading

0 comments on commit f726d35

Please sign in to comment.