From c6a9d644d1a1feb08924c1a0c6556c3fa564635c Mon Sep 17 00:00:00 2001 From: parazyd Date: Fri, 22 Oct 2021 16:45:13 +0200 Subject: [PATCH] util/parse: Use BigUint for encode_base10(). --- Cargo.lock | 2 ++ Cargo.toml | 1 + Makefile | 3 ++- src/bin/darkfid.rs | 3 ++- src/error.rs | 12 +++++++++--- src/util/parse.rs | 3 ++- 6 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 722e1232e..f8c7adff9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1264,6 +1264,7 @@ dependencies = [ "lazy_static", "log", "native-tls", + "num-bigint", "num_cpus", "prettytable-rs", "rand 0.7.3", @@ -2554,6 +2555,7 @@ dependencies = [ "autocfg 1.0.1", "num-integer", "num-traits", + "rand 0.7.3", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index ebcb2eef1..90d98affd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ hex = "0.4.2" bs58 = "0.4.0" prettytable-rs = "0.8" num_cpus = "1.13.0" +num-bigint = {version = "0.3.2", features = ["rand"]} smol = "1.2.5" futures = "0.3.17" diff --git a/Makefile b/Makefile index d87ea655a..bef2b3341 100644 --- a/Makefile +++ b/Makefile @@ -8,10 +8,11 @@ DLTOOL = wget -nv --show-progress -O- #DLTOOL = curl # Here it's possible to append "cashierd" and "gatewayd". -BINS = drk darkfid +BINS = eth drk darkfid cashierd gatewayd # Dependencies which should force the binaries to be rebuilt BINDEPS = \ + Cargo.toml \ $(shell find src -type f) \ $(shell find token -type f) \ $(shell find sql -type f) diff --git a/src/bin/darkfid.rs b/src/bin/darkfid.rs index 7f1483b41..7a2fd54f7 100644 --- a/src/bin/darkfid.rs +++ b/src/bin/darkfid.rs @@ -8,6 +8,7 @@ use async_trait::async_trait; use clap::clap_app; use easy_parallel::Parallel; use log::debug; +use num_bigint::BigUint; use serde_json::{json, Value}; use url::Url; @@ -151,7 +152,7 @@ impl Darkfid { } if let Some(symbol) = self.drk_tokenlist.symbol_from_id(balance.token_id)? { - let amount = encode_base10(balance.value, 8); + let amount = encode_base10(BigUint::from(balance.value), 8); symbols.insert(symbol, (amount, network.to_string())); } } diff --git a/src/error.rs b/src/error.rs index b883dacc4..3b44d89aa 100644 --- a/src/error.rs +++ b/src/error.rs @@ -18,6 +18,7 @@ pub enum Error { ParseFailed(&'static str), ParseIntError, ParseFloatError, + FromHexError, UrlParseError, MalformedPacket, AddrParseError, @@ -97,6 +98,7 @@ impl fmt::Display for Error { Error::ParseIntError => f.write_str("Parse 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"), Error::AsyncChannelSenderError => f.write_str("Async_channel sender error"), Error::AsyncChannelReceiverError => f.write_str("Async_channel receiver error"), Error::AsyncNativeTlsError => f.write_str("Async_Native_TLS error"), @@ -142,9 +144,7 @@ impl fmt::Display for Error { Error::TomlSerializeError(ref err) => write!(f, "Toml parsing error: {}", err), Error::Base58EncodeError(ref err) => write!(f, "bs58 encode error: {}", err), Error::Base58DecodeError(ref err) => write!(f, "bs58 decode error: {}", err), - Error::ConfigNotFound => { - f.write_str("No config file detected. Please create a config file") - } + Error::ConfigNotFound => f.write_str("No config file detected. Please create one."), Error::KeypairPathNotFound => f.write_str("No keypair file detected."), Error::CashierKeysNotFound => f.write_str("No cashier public keys detected."), Error::SetLoggerError => f.write_str("SetLoggerError"), @@ -227,6 +227,12 @@ impl From for Error { } } +impl From for Error { + fn from(_err: hex::FromHexError) -> Error { + Error::FromHexError + } +} + impl From for Error { fn from(_err: std::num::ParseIntError) -> Error { Error::ParseIntError diff --git a/src/util/parse.rs b/src/util/parse.rs index 002f99dfc..da1562908 100644 --- a/src/util/parse.rs +++ b/src/util/parse.rs @@ -1,6 +1,7 @@ use std::iter::FromIterator; use std::str::FromStr; +use num_bigint::BigUint; use sha2::{Digest, Sha256}; use crate::{ @@ -145,7 +146,7 @@ pub fn decode_base10(amount: &str, decimal_places: usize, strict: bool) -> Resul Ok(number + round as u64) } -pub fn encode_base10(amount: u64, decimal_places: usize) -> String { +pub fn encode_base10(amount: BigUint, decimal_places: usize) -> String { let mut s: Vec = format!("{:0width$}", amount, width = 1 + decimal_places) .chars() .collect();