Skip to content

Commit

Permalink
Make Transaction Field in V4 Slate Optional (#356)
Browse files Browse the repository at this point in the history
* make transaction field in V4 Slate optional

* add mutable/non mutable version of tx getter

* removal of mut references where not needed

* remove more muts

* update from master

* update from master

* test fixes
  • Loading branch information
yeastplume authored Mar 10, 2020
1 parent aebc352 commit c42d5dd
Show file tree
Hide file tree
Showing 35 changed files with 312 additions and 205 deletions.
223 changes: 126 additions & 97 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions api/src/foreign_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,14 +562,14 @@ where
message,
)
.map_err(|e| e.kind())?;
Ok(VersionedSlate::into_version(out_slate, version))
Ok(VersionedSlate::into_version(out_slate, version).map_err(|e| e.kind())?)
}

fn finalize_invoice_tx(&self, in_slate: VersionedSlate) -> Result<VersionedSlate, ErrorKind> {
let version = in_slate.version();
let out_slate =
Foreign::finalize_invoice_tx(self, &Slate::from(in_slate)).map_err(|e| e.kind())?;
Ok(VersionedSlate::into_version(out_slate, version))
Ok(VersionedSlate::into_version(out_slate, version).map_err(|e| e.kind())?)
}
}

Expand Down
12 changes: 5 additions & 7 deletions api/src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ where
};

if sa.post_tx {
self.post_tx(keychain_mask, &slate.tx, sa.fluff)?;
self.post_tx(keychain_mask, slate.tx_or_err()?, sa.fluff)?;
}
Ok(slate)
}
Expand Down Expand Up @@ -926,7 +926,7 @@ where
) -> Result<Slate, Error> {
let mut w_lock = self.wallet_inst.lock();
let w = w_lock.lc_provider()?.wallet_inst()?;
owner::finalize_tx(&mut **w, keychain_mask, &slate)
owner::finalize_tx(&mut **w, keychain_mask, slate)
}

/// Posts a completed transaction to the listening node for validation and inclusion in a block
Expand Down Expand Up @@ -976,7 +976,7 @@ where
/// // Retrieve slate back from recipient
/// //
/// let res = api_owner.finalize_tx(None, &slate);
/// let res = api_owner.post_tx(None, &slate.tx, true);
/// let res = api_owner.post_tx(None, slate.tx_or_err().unwrap(), true);
/// }
/// ```
Expand Down Expand Up @@ -1598,10 +1598,8 @@ where
let secp = secp_inst.lock();
return Ok(Some(SecretKey::from_slice(
&secp,
&from_hex(
"d096b3cb75986b3b13f80b8f5243a9edf0af4c74ac37578c5a12cfb5b59b1868".to_owned(),
)
.unwrap(),
&from_hex("d096b3cb75986b3b13f80b8f5243a9edf0af4c74ac37578c5a12cfb5b59b1868")
.unwrap(),
)?));
}
let mut w_lock = self.wallet_inst.lock();
Expand Down
10 changes: 5 additions & 5 deletions api/src/owner_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2059,7 +2059,7 @@ where
let slate = Owner::init_send_tx(self, (&token.keychain_mask).as_ref(), args)
.map_err(|e| e.kind())?;
let version = SlateVersion::V4;
Ok(VersionedSlate::into_version(slate, version))
Ok(VersionedSlate::into_version(slate, version).map_err(|e| e.kind())?)
}

fn issue_invoice_tx(
Expand All @@ -2070,7 +2070,7 @@ where
let slate = Owner::issue_invoice_tx(self, (&token.keychain_mask).as_ref(), args)
.map_err(|e| e.kind())?;
let version = SlateVersion::V4;
Ok(VersionedSlate::into_version(slate, version))
Ok(VersionedSlate::into_version(slate, version).map_err(|e| e.kind())?)
}

fn process_invoice_tx(
Expand All @@ -2087,7 +2087,7 @@ where
)
.map_err(|e| e.kind())?;
let version = SlateVersion::V4;
Ok(VersionedSlate::into_version(out_slate, version))
Ok(VersionedSlate::into_version(out_slate, version).map_err(|e| e.kind())?)
}

fn finalize_tx(
Expand All @@ -2102,7 +2102,7 @@ where
)
.map_err(|e| e.kind())?;
let version = SlateVersion::V4;
Ok(VersionedSlate::into_version(out_slate, version))
Ok(VersionedSlate::into_version(out_slate, version).map_err(|e| e.kind())?)
}

fn tx_lock_outputs(
Expand Down Expand Up @@ -2527,7 +2527,7 @@ pub fn run_doctest_owner(
}

if payment_proof {
api_impl::owner::post_tx(&client1, &slate_outer.tx, true).unwrap();
api_impl::owner::post_tx(&client1, slate_outer.tx_or_err().unwrap(), true).unwrap();
}

if perform_tx && lock_tx && finalize_tx {
Expand Down
8 changes: 4 additions & 4 deletions api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ impl EncryptedBody {
let mut to_decrypt = base64::decode(&self.body_enc).context(ErrorKind::APIEncryption(
"EncryptedBody Dec: Encrypted request contains invalid Base64".to_string(),
))?;
let nonce = from_hex(self.nonce.clone()).context(ErrorKind::APIEncryption(
"EncryptedBody Dec: Invalid Nonce".to_string(),
))?;
let nonce = from_hex(&self.nonce).map_err(|_| {
ErrorKind::APIEncryption("EncryptedBody Dec: Invalid Nonce".to_string())
})?;
if nonce.len() < 12 {
return Err(ErrorKind::APIEncryption(
"EncryptedBody Dec: Invalid Nonce length".to_string(),
Expand Down Expand Up @@ -321,7 +321,7 @@ fn encrypted_request() -> Result<(), Error> {
let secp_inst = static_secp_instance();
let secp = secp_inst.lock();

let sec_key_bytes = from_hex(sec_key_str.to_owned()).unwrap();
let sec_key_bytes = from_hex(sec_key_str).unwrap();
SecretKey::from_slice(&secp, &sec_key_bytes)?
};
let req = serde_json::json!({
Expand Down
10 changes: 5 additions & 5 deletions controller/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ where
e
})?;
slate = api.finalize_tx(m, &slate)?;
let result = api.post_tx(m, &slate.tx, args.fluff);
let result = api.post_tx(m, slate.tx_or_err()?, args.fluff);
match result {
Ok(_) => {
info!("Tx sent ok",);
Expand Down Expand Up @@ -477,7 +477,7 @@ where
error!("Error validating participant messages: {}", e);
return Err(e);
}
slate = api.finalize_invoice_tx(&mut slate)?;
slate = api.finalize_invoice_tx(&slate)?;
Ok(())
})?;
} else {
Expand All @@ -486,14 +486,14 @@ where
error!("Error validating participant messages: {}", e);
return Err(e);
}
slate = api.finalize_tx(m, &mut slate)?;
slate = api.finalize_tx(m, &slate)?;
Ok(())
})?;
}

if !args.nopost {
controller::owner_single_use(None, keychain_mask, Some(owner_api), |api, m| {
let result = api.post_tx(m, &slate.tx, args.fluff);
let result = api.post_tx(m, slate.tx_or_err()?, args.fluff);
match result {
Ok(_) => {
info!(
Expand Down Expand Up @@ -798,7 +798,7 @@ where
let slate = PathToSlate((&args.input).into()).get_tx()?;

controller::owner_single_use(None, keychain_mask, Some(owner_api), |api, m| {
api.post_tx(m, &slate.tx, args.fluff)?;
api.post_tx(m, slate.tx_or_err()?, args.fluff)?;
info!("Posted transaction");
return Ok(());
})?;
Expand Down
2 changes: 1 addition & 1 deletion controller/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ impl OwnerV3Helpers {
/// Update the shared mask, in case of foreign API being run
pub fn update_mask(mask: Arc<Mutex<Option<SecretKey>>>, val: &serde_json::Value) {
if let Some(key) = val["result"]["Ok"].as_str() {
let key_bytes = match from_hex(key.to_owned()) {
let key_bytes = match from_hex(key) {
Ok(k) => k,
Err(_) => return,
};
Expand Down
2 changes: 1 addition & 1 deletion controller/tests/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ fn accounts_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
slate = client1.send_tx_slate_direct("wallet2", &slate)?;
api.tx_lock_outputs(m, &slate, 0)?;
slate = api.finalize_tx(m, &slate)?;
api.post_tx(m, &slate.tx, false)?;
api.post_tx(m, slate.tx_or_err()?, false)?;
Ok(())
})?;

Expand Down
6 changes: 3 additions & 3 deletions controller/tests/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ fn file_exchange_test_impl(test_dir: &'static str) -> Result<(), libwallet::Erro
message: Some(message.to_owned()),
..Default::default()
};
let mut slate = api.init_send_tx(m, args)?;
let slate = api.init_send_tx(m, args)?;
// output tx file
PathToSlate((&send_file).into()).put_tx(&mut slate)?;
PathToSlate((&send_file).into()).put_tx(&slate)?;
api.tx_lock_outputs(m, &slate, 0)?;
Ok(())
})?;
Expand Down Expand Up @@ -160,7 +160,7 @@ fn file_exchange_test_impl(test_dir: &'static str) -> Result<(), libwallet::Erro
let mut slate = PathToSlate(receive_file.into()).get_tx()?;
api.verify_slate_messages(m, &slate)?;
slate = api.finalize_tx(m, &slate)?;
api.post_tx(m, &slate.tx, false)?;
api.post_tx(m, slate.tx_or_err()?, false)?;
bh += 1;
Ok(())
})?;
Expand Down
2 changes: 1 addition & 1 deletion controller/tests/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn invoice_tx_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {

// wallet 1 posts so wallet 2 doesn't get the mined amount
wallet::controller::owner_single_use(Some(wallet1.clone()), mask1, None, |api, m| {
api.post_tx(m, &slate.tx, false)?;
api.post_tx(m, slate.tx_or_err()?, false)?;
Ok(())
})?;
bh += 1;
Expand Down
4 changes: 2 additions & 2 deletions controller/tests/no_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn no_change_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
slate = client1.send_tx_slate_direct("wallet2", &slate)?;
api.tx_lock_outputs(m, &slate, 0)?;
slate = api.finalize_tx(m, &slate)?;
api.post_tx(m, &slate.tx, false)?;
api.post_tx(m, slate.tx_or_err()?, false)?;
Ok(())
})?;

Expand Down Expand Up @@ -139,7 +139,7 @@ fn no_change_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
Ok(())
})?;
wallet::controller::owner_single_use(Some(wallet2.clone()), mask1, None, |api, m| {
api.post_tx(m, &slate.tx, false)?;
api.post_tx(m, slate.tx_or_err()?, false)?;
Ok(())
})?;

Expand Down
2 changes: 1 addition & 1 deletion controller/tests/payment_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn payment_proofs_test_impl(test_dir: &'static str) -> Result<(), libwallet::Err
assert!(pp.is_err());

slate = sender_api.finalize_tx(m, &slate)?;
sender_api.post_tx(m, &slate.tx, true)?;
sender_api.post_tx(m, slate.tx_or_err()?, true)?;
Ok(())
})?;

Expand Down
2 changes: 1 addition & 1 deletion controller/tests/repost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ fn file_repost_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error>
let slate_i = sender_api.init_send_tx(m, args)?;
slate = client1.send_tx_slate_direct("wallet2", &slate_i)?;
sender_api.tx_lock_outputs(m, &slate, 0)?;
slate = sender_api.finalize_tx(m, &mut slate)?;
slate = sender_api.finalize_tx(m, &slate)?;
Ok(())
})?;

Expand Down
19 changes: 16 additions & 3 deletions controller/tests/revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,13 @@ fn revert(

// Build 2 blocks at same height: 1 with the tx, 1 without
let head = chain.head_header().unwrap();
let block_with =
create_block_for_wallet(&chain, head.clone(), vec![&tx], wallet1.clone(), mask1)?;
let block_with = create_block_for_wallet(
&chain,
head.clone(),
vec![&tx.as_ref().unwrap()],
wallet1.clone(),
mask1,
)?;
let block_without = create_block_for_wallet(&chain, head, vec![], wallet1.clone(), mask1)?;

// Add block with tx to the chain
Expand Down Expand Up @@ -277,7 +282,15 @@ fn revert(

stopper2.store(false, Ordering::Relaxed);
Ok((
chain, stopper, sent, bh, tx, wallet1, mask1_i, wallet2, mask2_i,
chain,
stopper,
sent,
bh,
tx.unwrap(),
wallet1,
mask1_i,
wallet2,
mask2_i,
))
}

Expand Down
2 changes: 1 addition & 1 deletion controller/tests/self_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn self_send_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
Ok(())
})?;
slate = api.finalize_tx(m, &slate)?;
api.post_tx(m, &slate.tx, false)?; // mines a block
api.post_tx(m, slate.tx_or_err()?, false)?; // mines a block
bh += 1;
Ok(())
})?;
Expand Down
11 changes: 8 additions & 3 deletions controller/tests/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,14 @@ fn basic_transaction_api(test_dir: &'static str) -> Result<(), libwallet::Error>
slate = sender_api.finalize_tx(m, &slate)?;

// Check we have a single kernel and that it is a Plain kernel (no lock_height).
assert_eq!(slate.tx.kernels().len(), 1);
assert_eq!(slate.tx_or_err()?.kernels().len(), 1);
assert_eq!(
slate.tx.kernels().first().map(|k| k.features).unwrap(),
slate
.tx_or_err()?
.kernels()
.first()
.map(|k| k.features)
.unwrap(),
transaction::KernelFeatures::Plain { fee: 2000000 }
);

Expand Down Expand Up @@ -170,7 +175,7 @@ fn basic_transaction_api(test_dir: &'static str) -> Result<(), libwallet::Error>

// post transaction
wallet::controller::owner_single_use(Some(wallet1.clone()), mask1, None, |api, m| {
api.post_tx(m, &slate.tx, false)?;
api.post_tx(m, slate.tx_or_err()?, false)?;
Ok(())
})?;

Expand Down
4 changes: 2 additions & 2 deletions impls/src/adapters/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ impl SlatePutter for PathToSlate {
if false {
warn!("Transaction contains features that require grin-wallet 4.0.0 or later");
warn!("Please ensure the other party is running grin-wallet v4.0.0 or later before sending");
VersionedSlate::into_version(slate.clone(), SlateVersion::V4)
VersionedSlate::into_version(slate.clone(), SlateVersion::V4)?
} else {
let mut s = slate.clone();
s.version_info.version = 3;
s.version_info.orig_version = 3;
VersionedSlate::into_version(s, SlateVersion::V3)
VersionedSlate::into_version(s, SlateVersion::V3)?
}
};
pub_tx.write_all(
Expand Down
4 changes: 2 additions & 2 deletions impls/src/adapters/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl SlateSender for HttpSlateSender {
}

let slate_send = match self.check_other_version(&url_str)? {
SlateVersion::V4 => VersionedSlate::into_version(slate.clone(), SlateVersion::V4),
SlateVersion::V4 => VersionedSlate::into_version(slate.clone(), SlateVersion::V4)?,
SlateVersion::V3 => {
let mut slate = slate.clone();
let _r: crate::adapters::Reminder;
Expand All @@ -191,7 +191,7 @@ impl SlateSender for HttpSlateSender {
}
slate.version_info.version = 3;
slate.version_info.orig_version = 3;
VersionedSlate::into_version(slate, SlateVersion::V3)
VersionedSlate::into_version(slate, SlateVersion::V3)?
}
};
// Note: not using easy-jsonrpc as don't want the dependencies in this crate
Expand Down
2 changes: 1 addition & 1 deletion impls/src/backends/lmdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ where
let mut tx_f = File::open(tx_file)?;
let mut content = String::new();
tx_f.read_to_string(&mut content)?;
let tx_bin = util::from_hex(content).unwrap();
let tx_bin = util::from_hex(&content).unwrap();
Ok(Some(
ser::deserialize::<Transaction>(&mut &tx_bin[..], ser::ProtocolVersion(1)).unwrap(),
))
Expand Down
10 changes: 5 additions & 5 deletions impls/src/lifecycle/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ impl WalletSeed {
}

pub fn _from_hex(hex: &str) -> Result<WalletSeed, Error> {
let bytes = util::from_hex(hex.to_string())
.context(ErrorKind::GenericError("Invalid hex".to_owned()))?;
let bytes = util::from_hex(&hex.to_string())
.map_err(|_| ErrorKind::GenericError("Invalid hex".to_owned()))?;
Ok(WalletSeed::from_bytes(&bytes))
}

Expand Down Expand Up @@ -274,15 +274,15 @@ impl EncryptedWalletSeed {

/// Decrypt seed
pub fn decrypt(&self, password: &str) -> Result<WalletSeed, Error> {
let mut encrypted_seed = match util::from_hex(self.encrypted_seed.clone()) {
let mut encrypted_seed = match util::from_hex(&self.encrypted_seed.clone()) {
Ok(s) => s,
Err(_) => return Err(ErrorKind::Encryption.into()),
};
let salt = match util::from_hex(self.salt.clone()) {
let salt = match util::from_hex(&self.salt.clone()) {
Ok(s) => s,
Err(_) => return Err(ErrorKind::Encryption.into()),
};
let nonce = match util::from_hex(self.nonce.clone()) {
let nonce = match util::from_hex(&self.nonce.clone()) {
Ok(s) => s,
Err(_) => return Err(ErrorKind::Encryption.into()),
};
Expand Down
Loading

0 comments on commit c42d5dd

Please sign in to comment.