mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
util/parse: Use BigUint for decode_base10.
This could use some further testing.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use async_std::sync::{Arc, Mutex};
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryInto;
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
|
||||
@@ -17,8 +18,7 @@ use drk::{
|
||||
cli::{Config, DarkfidConfig},
|
||||
client::{Client, State},
|
||||
crypto::{
|
||||
load_params, merkle::CommitmentTree, save_params, setup_mint_prover,
|
||||
setup_spend_prover,
|
||||
load_params, merkle::CommitmentTree, save_params, setup_mint_prover, setup_spend_prover,
|
||||
},
|
||||
rpc::{
|
||||
jsonrpc::{error as jsonerr, request as jsonreq, response as jsonresp, send_raw_request},
|
||||
@@ -108,7 +108,6 @@ impl Darkfid {
|
||||
let own_coins = self.client.lock().await.get_own_coins()?;
|
||||
|
||||
for own_coin in own_coins.iter() {
|
||||
|
||||
let nullifier_exists = self
|
||||
.state
|
||||
.lock()
|
||||
@@ -121,7 +120,6 @@ impl Darkfid {
|
||||
.await
|
||||
.confirm_spend_coin(&own_coin.coin)?;
|
||||
}
|
||||
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -430,7 +428,12 @@ impl Darkfid {
|
||||
self.client
|
||||
.lock()
|
||||
.await
|
||||
.transfer(*token_id, cashier_public, amount_in_apo, self.state.clone())
|
||||
.transfer(
|
||||
token_id.clone(),
|
||||
cashier_public,
|
||||
amount_in_apo.try_into()?,
|
||||
self.state.clone(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
@@ -514,7 +517,12 @@ impl Darkfid {
|
||||
self.client
|
||||
.lock()
|
||||
.await
|
||||
.transfer(*token_id, drk_address, amount, self.state.clone())
|
||||
.transfer(
|
||||
token_id.clone(),
|
||||
drk_address,
|
||||
amount.try_into()?,
|
||||
self.state.clone(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
|
||||
16
src/error.rs
16
src/error.rs
@@ -17,6 +17,7 @@ pub enum Error {
|
||||
/// Parsing And Encode/Decode errors
|
||||
ParseFailed(&'static str),
|
||||
ParseIntError,
|
||||
ParseBigIntError,
|
||||
ParseFloatError,
|
||||
FromHexError,
|
||||
UrlParseError,
|
||||
@@ -28,6 +29,7 @@ pub enum Error {
|
||||
StrUtf8Error(String),
|
||||
TryIntoError,
|
||||
TryFromError,
|
||||
TryFromBigIntError,
|
||||
SerdeJsonError(String),
|
||||
TomlDeserializeError(String),
|
||||
TomlSerializeError(String),
|
||||
@@ -96,6 +98,7 @@ impl fmt::Display for Error {
|
||||
Error::NonMinimalVarInt => f.write_str("non-minimal varint"),
|
||||
Error::ParseFailed(ref err) => write!(f, "parse failed: {}", err),
|
||||
Error::ParseIntError => f.write_str("Parse int error"),
|
||||
Error::ParseBigIntError => f.write_str("Parse big int error"),
|
||||
Error::ParseFloatError => f.write_str("Parse float error"),
|
||||
Error::UrlParseError => f.write_str("Failed to parse URL"),
|
||||
Error::FromHexError => f.write_str("Failed to convert from hex"),
|
||||
@@ -132,6 +135,7 @@ impl fmt::Display for Error {
|
||||
Error::SolFailed(ref err) => write!(f, "Sol client failed: {}", err),
|
||||
Error::TryIntoError => f.write_str("TryInto error"),
|
||||
Error::TryFromError => f.write_str("TryFrom error"),
|
||||
Error::TryFromBigIntError => f.write_str("TryFromBigInt error"),
|
||||
Error::RocksdbError(ref err) => write!(f, "Rocksdb Error: {}", err),
|
||||
Error::SlabsStore(ref err) => write!(f, "SlabsStore Error: {}", err),
|
||||
Error::JsonRpcError(ref err) => write!(f, "JsonRpc Error: {}", err),
|
||||
@@ -239,6 +243,18 @@ impl From<std::num::ParseIntError> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<num_bigint::ParseBigIntError> for Error {
|
||||
fn from(_err: num_bigint::ParseBigIntError) -> Error {
|
||||
Error::ParseBigIntError
|
||||
}
|
||||
}
|
||||
|
||||
impl From<num_bigint::TryFromBigIntError<num_bigint::BigUint>> for Error {
|
||||
fn from(_err: num_bigint::TryFromBigIntError<num_bigint::BigUint>) -> Error {
|
||||
Error::TryFromBigIntError
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::num::ParseFloatError> for Error {
|
||||
fn from(_err: std::num::ParseFloatError) -> Error {
|
||||
Error::ParseFloatError
|
||||
|
||||
@@ -93,7 +93,7 @@ fn char_eq(a: char, b: char) -> bool {
|
||||
a == b
|
||||
}
|
||||
|
||||
pub fn decode_base10(amount: &str, decimal_places: usize, strict: bool) -> Result<u64> {
|
||||
pub fn decode_base10(amount: &str, decimal_places: usize, strict: bool) -> Result<BigUint> {
|
||||
let mut s: Vec<char> = amount.to_string().chars().collect();
|
||||
|
||||
// Get rid of the decimal point:
|
||||
@@ -136,12 +136,14 @@ pub fn decode_base10(amount: &str, decimal_places: usize, strict: bool) -> Resul
|
||||
}
|
||||
|
||||
// Convert to an integer
|
||||
let number = u64::from_str(&String::from_iter(&s))?;
|
||||
let number = BigUint::from_str(&String::from_iter(&s))?;
|
||||
|
||||
// Round and return
|
||||
/*
|
||||
if round && number == u64::MAX {
|
||||
return Err(Error::ParseFailed("u64 overflow"));
|
||||
}
|
||||
*/
|
||||
|
||||
Ok(number + round as u64)
|
||||
}
|
||||
@@ -184,10 +186,22 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_decode_base10() {
|
||||
assert_eq!(124, decode_base10("12.33", 1, false).unwrap());
|
||||
assert_eq!(1233000, decode_base10("12.33", 5, false).unwrap());
|
||||
assert_eq!(1200000, decode_base10("12.", 5, false).unwrap());
|
||||
assert_eq!(1200000, decode_base10("12", 5, false).unwrap());
|
||||
assert_eq!(
|
||||
124.to_biguint().unwrap(),
|
||||
decode_base10("12.33", 1, false).unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
1233000.to_biguint().unwrap(),
|
||||
decode_base10("12.33", 5, false).unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
1200000.to_biguint().unwrap(),
|
||||
decode_base10("12.", 5, false).unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
1200000.to_biguint().unwrap(),
|
||||
decode_base10("12", 5, false).unwrap()
|
||||
);
|
||||
assert!(decode_base10("12.33", 1, true).is_err());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user