mirror of
https://github.com/mimblewimble/grin.git
synced 2025-02-01 17:01:09 +03:00
Some simple Option / Result / iterator pattern simplifications (#3205)
This commit is contained in:
parent
616dad43fd
commit
dcdbdd4bcc
10 changed files with 39 additions and 57 deletions
|
@ -463,10 +463,7 @@ impl KernelHandler {
|
||||||
height,
|
height,
|
||||||
mmr_index,
|
mmr_index,
|
||||||
});
|
});
|
||||||
match kernel {
|
kernel.ok_or_else(|| ErrorKind::NotFound.into())
|
||||||
Some(kernel) => Ok(kernel),
|
|
||||||
None => Err(ErrorKind::NotFound.into()),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -334,10 +334,10 @@ impl OutputPrintable {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn range_proof(&self) -> Result<pedersen::RangeProof, ser::Error> {
|
pub fn range_proof(&self) -> Result<pedersen::RangeProof, ser::Error> {
|
||||||
let proof_str = match self.proof.clone() {
|
let proof_str = self
|
||||||
Some(p) => p,
|
.proof
|
||||||
None => return Err(ser::Error::HexError(format!("output range_proof missing"))),
|
.clone()
|
||||||
};
|
.ok_or_else(|| ser::Error::HexError(format!("output range_proof missing")))?;
|
||||||
|
|
||||||
let p_vec = util::from_hex(proof_str)
|
let p_vec = util::from_hex(proof_str)
|
||||||
.map_err(|_| ser::Error::HexError(format!("invalid output range_proof")))?;
|
.map_err(|_| ser::Error::HexError(format!("invalid output range_proof")))?;
|
||||||
|
|
|
@ -105,10 +105,7 @@ impl QueryParams {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, name: &str) -> Option<&String> {
|
pub fn get(&self, name: &str) -> Option<&String> {
|
||||||
match self.params.get(name) {
|
self.params.get(name).and_then(|v| v.first())
|
||||||
None => None,
|
|
||||||
Some(v) => v.first(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -65,10 +65,7 @@ pub fn verify(
|
||||||
extra_data: Option<Vec<u8>>,
|
extra_data: Option<Vec<u8>>,
|
||||||
) -> Result<(), secp::Error> {
|
) -> Result<(), secp::Error> {
|
||||||
let result = secp.verify_bullet_proof(commit, proof, extra_data);
|
let result = secp.verify_bullet_proof(commit, proof, extra_data);
|
||||||
match result {
|
result.map(|_| ())
|
||||||
Ok(_) => Ok(()),
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Rewind a rangeproof to retrieve the amount, derivation path and switch commitment type
|
/// Rewind a rangeproof to retrieve the amount, derivation path and switch commitment type
|
||||||
|
@ -228,9 +225,10 @@ where
|
||||||
let id = Identifier::from_serialized_path(depth, &msg[4..]);
|
let id = Identifier::from_serialized_path(depth, &msg[4..]);
|
||||||
|
|
||||||
let commit_exp = self.keychain.commit(amount, &id, &switch)?;
|
let commit_exp = self.keychain.commit(amount, &id, &switch)?;
|
||||||
match commit == &commit_exp {
|
if commit == &commit_exp {
|
||||||
true => Ok(Some((id, switch))),
|
Ok(Some((id, switch)))
|
||||||
false => Ok(None),
|
} else {
|
||||||
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -338,9 +336,10 @@ where
|
||||||
let commit_exp = self
|
let commit_exp = self
|
||||||
.keychain
|
.keychain
|
||||||
.commit(amount, &id, &SwitchCommitmentType::Regular)?;
|
.commit(amount, &id, &SwitchCommitmentType::Regular)?;
|
||||||
match commit == &commit_exp {
|
if commit == &commit_exp {
|
||||||
true => Ok(Some((id, SwitchCommitmentType::Regular))),
|
Ok(Some((id, SwitchCommitmentType::Regular)))
|
||||||
false => Ok(None),
|
} else {
|
||||||
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,15 +107,17 @@ impl BIP32GrinHasher {
|
||||||
|
|
||||||
impl BIP32Hasher for BIP32GrinHasher {
|
impl BIP32Hasher for BIP32GrinHasher {
|
||||||
fn network_priv(&self) -> [u8; 4] {
|
fn network_priv(&self) -> [u8; 4] {
|
||||||
match self.is_floo {
|
if self.is_floo {
|
||||||
true => [0x03, 0x27, 0x3A, 0x10], // fprv
|
[0x03, 0x27, 0x3A, 0x10]
|
||||||
false => [0x03, 0x3C, 0x04, 0xA4], // gprv
|
} else {
|
||||||
|
[0x03, 0x3C, 0x04, 0xA4]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn network_pub(&self) -> [u8; 4] {
|
fn network_pub(&self) -> [u8; 4] {
|
||||||
match self.is_floo {
|
if self.is_floo {
|
||||||
true => [0x03, 0x27, 0x3E, 0x4B], // fpub
|
[0x03, 0x27, 0x3E, 0x4B]
|
||||||
false => [0x03, 0x3C, 0x08, 0xDF], // gpub
|
} else {
|
||||||
|
[0x03, 0x3C, 0x08, 0xDF]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn master_seed() -> [u8; 12] {
|
fn master_seed() -> [u8; 12] {
|
||||||
|
@ -380,10 +382,7 @@ impl ExtendedPrivKey {
|
||||||
passphrase: &str,
|
passphrase: &str,
|
||||||
is_floo: bool,
|
is_floo: bool,
|
||||||
) -> Result<ExtendedPrivKey, Error> {
|
) -> Result<ExtendedPrivKey, Error> {
|
||||||
let seed = match mnemonic::to_seed(mnemonic, passphrase) {
|
let seed = mnemonic::to_seed(mnemonic, passphrase).map_err(|e| Error::MnemonicError(e))?;
|
||||||
Ok(s) => s,
|
|
||||||
Err(e) => return Err(Error::MnemonicError(e)),
|
|
||||||
};
|
|
||||||
let mut hasher = BIP32GrinHasher::new(is_floo);
|
let mut hasher = BIP32GrinHasher::new(is_floo);
|
||||||
let key = ExtendedPrivKey::new_master(secp, &mut hasher, &seed)?;
|
let key = ExtendedPrivKey::new_master(secp, &mut hasher, &seed)?;
|
||||||
Ok(key)
|
Ok(key)
|
||||||
|
|
|
@ -403,11 +403,7 @@ impl Pool {
|
||||||
// Oldest (based on pool insertion time) will then be prioritized.
|
// Oldest (based on pool insertion time) will then be prioritized.
|
||||||
tx_buckets.sort_unstable_by_key(|x| (Reverse(x.fee_to_weight), x.age_idx));
|
tx_buckets.sort_unstable_by_key(|x| (Reverse(x.fee_to_weight), x.age_idx));
|
||||||
|
|
||||||
tx_buckets
|
tx_buckets.into_iter().flat_map(|x| x.raw_txs).collect()
|
||||||
.into_iter()
|
|
||||||
.map(|x| x.raw_txs)
|
|
||||||
.flatten()
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_matching_transactions(&self, kernels: &[TxKernel]) -> Vec<Transaction> {
|
pub fn find_matching_transactions(&self, kernels: &[TxKernel]) -> Vec<Transaction> {
|
||||||
|
|
|
@ -130,9 +130,10 @@ impl TableViewItem<DiffColumn> for DiffBlock {
|
||||||
fn to_column(&self, column: DiffColumn) -> String {
|
fn to_column(&self, column: DiffColumn) -> String {
|
||||||
let naive_datetime = NaiveDateTime::from_timestamp(self.time as i64, 0);
|
let naive_datetime = NaiveDateTime::from_timestamp(self.time as i64, 0);
|
||||||
let datetime: DateTime<Utc> = DateTime::from_utc(naive_datetime, Utc);
|
let datetime: DateTime<Utc> = DateTime::from_utc(naive_datetime, Utc);
|
||||||
let pow_type = match self.is_secondary {
|
let pow_type = if self.is_secondary {
|
||||||
true => String::from("Secondary"),
|
String::from("Secondary")
|
||||||
false => String::from("Primary"),
|
} else {
|
||||||
|
String::from("Primary")
|
||||||
};
|
};
|
||||||
|
|
||||||
match column {
|
match column {
|
||||||
|
|
|
@ -96,8 +96,7 @@ impl LeafSet {
|
||||||
/// Only applicable for the output MMR.
|
/// Only applicable for the output MMR.
|
||||||
fn unpruned_pre_cutoff(&self, cutoff_pos: u64, prune_list: &PruneList) -> Bitmap {
|
fn unpruned_pre_cutoff(&self, cutoff_pos: u64, prune_list: &PruneList) -> Bitmap {
|
||||||
(1..=cutoff_pos)
|
(1..=cutoff_pos)
|
||||||
.filter(|&x| pmmr::is_leaf(x))
|
.filter(|&x| pmmr::is_leaf(x) && !prune_list.is_pruned(x))
|
||||||
.filter(|&x| !prune_list.is_pruned(x))
|
|
||||||
.map(|x| x as u32)
|
.map(|x| x as u32)
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,10 +107,7 @@ where
|
||||||
/// Elements can be of variable size (handled internally in the append-only file impl).
|
/// Elements can be of variable size (handled internally in the append-only file impl).
|
||||||
///
|
///
|
||||||
pub fn read(&self, position: u64) -> Option<T> {
|
pub fn read(&self, position: u64) -> Option<T> {
|
||||||
match self.file.read_as_elmt(position - 1) {
|
self.file.read_as_elmt(position - 1).ok()
|
||||||
Ok(x) => Some(x),
|
|
||||||
Err(_) => None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Rewind the backend file to the specified position.
|
/// Rewind the backend file to the specified position.
|
||||||
|
|
|
@ -70,15 +70,12 @@ fn copy_to(src: &Path, src_type: &fs::FileType, dst: &Path) -> io::Result<u64> {
|
||||||
|
|
||||||
/// Retrieve first line from file
|
/// Retrieve first line from file
|
||||||
pub fn get_first_line(file_path: Option<String>) -> Option<String> {
|
pub fn get_first_line(file_path: Option<String>) -> Option<String> {
|
||||||
match file_path {
|
file_path.and_then(|path| match fs::File::open(path) {
|
||||||
Some(path) => match fs::File::open(path) {
|
Ok(file) => {
|
||||||
Ok(file) => {
|
let buf_reader = io::BufReader::new(file);
|
||||||
let buf_reader = io::BufReader::new(file);
|
let mut lines_iter = buf_reader.lines().map(|l| l.unwrap());
|
||||||
let mut lines_iter = buf_reader.lines().map(|l| l.unwrap());
|
lines_iter.next()
|
||||||
lines_iter.next()
|
}
|
||||||
}
|
Err(_) => None,
|
||||||
Err(_) => None,
|
})
|
||||||
},
|
|
||||||
None => None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue