mirror of
https://github.com/Qortal/piratewallet-light-cli.git
synced 2025-07-31 20:31:24 +00:00
Add bad spend tests
This commit is contained in:
@@ -713,11 +713,11 @@ impl LightClient {
|
|||||||
);
|
);
|
||||||
|
|
||||||
match rawtx {
|
match rawtx {
|
||||||
Some(txbytes) => match broadcast_raw_tx(&self.get_server_uri(), txbytes) {
|
Ok(txbytes) => match broadcast_raw_tx(&self.get_server_uri(), txbytes) {
|
||||||
Ok(k) => k,
|
Ok(k) => k,
|
||||||
Err(e) => e,
|
Err(e) => e,
|
||||||
},
|
},
|
||||||
None => format!("No Tx to broadcast")
|
Err(e) => format!("No Tx to broadcast. Error was: {}", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1055,7 +1055,7 @@ impl LightWallet {
|
|||||||
spend_params: &[u8],
|
spend_params: &[u8],
|
||||||
output_params: &[u8],
|
output_params: &[u8],
|
||||||
tos: Vec<(&str, u64, Option<String>)>
|
tos: Vec<(&str, u64, Option<String>)>
|
||||||
) -> Option<Box<[u8]>> {
|
) -> Result<Box<[u8]>, String> {
|
||||||
let start_time = now();
|
let start_time = now();
|
||||||
|
|
||||||
let total_value = tos.iter().map(|to| to.1).sum::<u64>();
|
let total_value = tos.iter().map(|to| to.1).sum::<u64>();
|
||||||
@@ -1066,7 +1066,7 @@ impl LightWallet {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Convert address (str) to RecepientAddress and value to Amount
|
// Convert address (str) to RecepientAddress and value to Amount
|
||||||
let maybe_tos: Result<Vec<(address::RecipientAddress, Amount, Option<String>)>, _> = tos.iter().map(|to| {
|
let tos = tos.iter().map(|to| {
|
||||||
let ra = match address::RecipientAddress::from_str(to.0,
|
let ra = match address::RecipientAddress::from_str(to.0,
|
||||||
self.config.hrp_sapling_address(),
|
self.config.hrp_sapling_address(),
|
||||||
self.config.base58_pubkey_address(),
|
self.config.base58_pubkey_address(),
|
||||||
@@ -1082,22 +1082,15 @@ impl LightWallet {
|
|||||||
let value = Amount::from_u64(to.1).unwrap();
|
let value = Amount::from_u64(to.1).unwrap();
|
||||||
|
|
||||||
Ok((ra, value, to.2.clone()))
|
Ok((ra, value, to.2.clone()))
|
||||||
}).collect();
|
}).collect::<Result<Vec<(address::RecipientAddress, Amount, Option<String>)>, String>>()?;
|
||||||
|
|
||||||
let tos = match maybe_tos {
|
|
||||||
Ok(t) => t,
|
|
||||||
Err(e) => {
|
|
||||||
error!("{}", e);
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Target the next block, assuming we are up-to-date.
|
// Target the next block, assuming we are up-to-date.
|
||||||
let (height, anchor_offset) = match self.get_target_height_and_anchor_offset() {
|
let (height, anchor_offset) = match self.get_target_height_and_anchor_offset() {
|
||||||
Some(res) => res,
|
Some(res) => res,
|
||||||
None => {
|
None => {
|
||||||
eprintln!("Cannot send funds before scanning any blocks");
|
let e = format!("Cannot send funds before scanning any blocks");
|
||||||
return None;
|
error!("{}", e);
|
||||||
|
return Err(e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1142,7 +1135,7 @@ impl LightWallet {
|
|||||||
).collect();
|
).collect();
|
||||||
|
|
||||||
// Add all tinputs
|
// Add all tinputs
|
||||||
let r = tinputs.iter()
|
tinputs.iter()
|
||||||
.map(|utxo| {
|
.map(|utxo| {
|
||||||
let outpoint: OutPoint = utxo.to_outpoint();
|
let outpoint: OutPoint = utxo.to_outpoint();
|
||||||
|
|
||||||
@@ -1163,15 +1156,8 @@ impl LightWallet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<_>, _>>();
|
.collect::<Result<Vec<_>, _>>()
|
||||||
|
.map_err(|e| format!("{}", e))?;
|
||||||
match r {
|
|
||||||
Err(e) => {
|
|
||||||
error!("Error adding transparent inputs: {:?}", e);
|
|
||||||
return None;
|
|
||||||
},
|
|
||||||
Ok(_) => {}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// Confirm we were able to select sufficient value
|
// Confirm we were able to select sufficient value
|
||||||
@@ -1179,11 +1165,12 @@ impl LightWallet {
|
|||||||
+ tinputs.iter().map::<u64, _>(|utxo| utxo.value.into()).sum::<u64>();
|
+ tinputs.iter().map::<u64, _>(|utxo| utxo.value.into()).sum::<u64>();
|
||||||
|
|
||||||
if selected_value < u64::from(target_value) {
|
if selected_value < u64::from(target_value) {
|
||||||
eprintln!(
|
let e = format!(
|
||||||
"Insufficient verified funds (have {}, need {:?}).\n Note, funds need {} confirmations before they can be spent",
|
"Insufficient verified funds (have {}, need {:?}).\n Note, funds need {} confirmations before they can be spent",
|
||||||
selected_value, target_value, self.config.anchor_offset
|
selected_value, target_value, self.config.anchor_offset
|
||||||
);
|
);
|
||||||
return None;
|
error!("{}", e);
|
||||||
|
return Err(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the transaction
|
// Create the transaction
|
||||||
@@ -1196,8 +1183,9 @@ impl LightWallet {
|
|||||||
selected.note.clone(),
|
selected.note.clone(),
|
||||||
selected.witness.clone(),
|
selected.witness.clone(),
|
||||||
) {
|
) {
|
||||||
eprintln!("Error adding note: {:?}", e);
|
let e = format!("Error adding note: {:?}", e);
|
||||||
return None;
|
error!("{}", e);
|
||||||
|
return Err(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1227,8 +1215,9 @@ impl LightWallet {
|
|||||||
builder.add_transparent_output(&to, value)
|
builder.add_transparent_output(&to, value)
|
||||||
}
|
}
|
||||||
} {
|
} {
|
||||||
eprintln!("Error adding output: {:?}", e);
|
let e = format!("Error adding output: {:?}", e);
|
||||||
return None;
|
error!("{}", e);
|
||||||
|
return Err(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1240,8 +1229,9 @@ impl LightWallet {
|
|||||||
) {
|
) {
|
||||||
Ok(res) => res,
|
Ok(res) => res,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("Error creating transaction: {:?}", e);
|
let e = format!("Error creating transaction: {:?}", e);
|
||||||
return None;
|
error!("{}", e);
|
||||||
|
return Err(e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
println!("{}: Transaction created", now() - start_time);
|
println!("{}: Transaction created", now() - start_time);
|
||||||
@@ -1271,7 +1261,7 @@ impl LightWallet {
|
|||||||
// Return the encoded transaction, so the caller can send it.
|
// Return the encoded transaction, so the caller can send it.
|
||||||
let mut raw_tx = vec![];
|
let mut raw_tx = vec![];
|
||||||
tx.write(&mut raw_tx).unwrap();
|
tx.write(&mut raw_tx).unwrap();
|
||||||
Some(raw_tx.into_boxed_slice())
|
Ok(raw_tx.into_boxed_slice())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2143,7 +2133,7 @@ pub mod tests {
|
|||||||
let (wallet, txid1, block_hash) = get_test_wallet(AMOUNT1);
|
let (wallet, txid1, block_hash) = get_test_wallet(AMOUNT1);
|
||||||
|
|
||||||
let branch_id = u32::from_str_radix("2bb40e60", 16).unwrap();
|
let branch_id = u32::from_str_radix("2bb40e60", 16).unwrap();
|
||||||
let (ss, so) =get_sapling_params().unwrap();
|
let (ss, so) = get_sapling_params().unwrap();
|
||||||
|
|
||||||
let taddr = wallet.address_from_sk(&SecretKey::from_slice(&[1u8; 32]).unwrap());
|
let taddr = wallet.address_from_sk(&SecretKey::from_slice(&[1u8; 32]).unwrap());
|
||||||
const AMOUNT_SENT: u64 = 30;
|
const AMOUNT_SENT: u64 = 30;
|
||||||
@@ -2312,7 +2302,7 @@ pub mod tests {
|
|||||||
let fee: u64 = DEFAULT_FEE.try_into().unwrap();
|
let fee: u64 = DEFAULT_FEE.try_into().unwrap();
|
||||||
|
|
||||||
let branch_id = u32::from_str_radix("2bb40e60", 16).unwrap();
|
let branch_id = u32::from_str_radix("2bb40e60", 16).unwrap();
|
||||||
let (ss, so) =get_sapling_params().unwrap();
|
let (ss, so) = get_sapling_params().unwrap();
|
||||||
|
|
||||||
// Create a tx and send to address
|
// Create a tx and send to address
|
||||||
let raw_tx = wallet.send_to_address(branch_id, &ss, &so,
|
let raw_tx = wallet.send_to_address(branch_id, &ss, &so,
|
||||||
@@ -2664,6 +2654,41 @@ pub mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_bad_send() {
|
||||||
|
// Test all the ways in which a send should fail
|
||||||
|
const AMOUNT1: u64 = 50000;
|
||||||
|
let _fee: u64 = DEFAULT_FEE.try_into().unwrap();
|
||||||
|
|
||||||
|
let (wallet, _txid1, _block_hash) = get_test_wallet(AMOUNT1);
|
||||||
|
|
||||||
|
let branch_id = u32::from_str_radix("2bb40e60", 16).unwrap();
|
||||||
|
let (ss, so) = get_sapling_params().unwrap();
|
||||||
|
let ext_taddr = wallet.address_from_sk(&SecretKey::from_slice(&[1u8; 32]).unwrap());
|
||||||
|
|
||||||
|
// Bad address
|
||||||
|
let raw_tx = wallet.send_to_address(branch_id, &ss, &so,
|
||||||
|
vec![(&"badaddress", 10, None)]);
|
||||||
|
assert!(raw_tx.err().unwrap().contains("Invalid recipient address"));
|
||||||
|
|
||||||
|
// Insufficient funds
|
||||||
|
let raw_tx = wallet.send_to_address(branch_id, &ss, &so,
|
||||||
|
vec![(&ext_taddr, AMOUNT1 + 10, None)]);
|
||||||
|
assert!(raw_tx.err().unwrap().contains("Insufficient verified funds"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn test_bad_params() {
|
||||||
|
let (wallet, _, _) = get_test_wallet(100000);
|
||||||
|
let ext_taddr = wallet.address_from_sk(&SecretKey::from_slice(&[1u8; 32]).unwrap());
|
||||||
|
|
||||||
|
let branch_id = u32::from_str_radix("2bb40e60", 16).unwrap();
|
||||||
|
// Bad params
|
||||||
|
let _ = wallet.send_to_address(branch_id, &[], &[],
|
||||||
|
vec![(&ext_taddr, 10, None)]);
|
||||||
|
}
|
||||||
|
|
||||||
/// Test helper to add blocks
|
/// Test helper to add blocks
|
||||||
fn add_blocks(wallet: &LightWallet, start: i32, num: i32, mut prev_hash: BlockHash) -> Result<BlockHash, i32>{
|
fn add_blocks(wallet: &LightWallet, start: i32, num: i32, mut prev_hash: BlockHash) -> Result<BlockHash, i32>{
|
||||||
// Add it to a block
|
// Add it to a block
|
||||||
@@ -2677,7 +2702,6 @@ pub mod tests {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Ok(new_blk.hash())
|
Ok(new_blk.hash())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user