Просмотр исходного кода

util/parse: Use BigUint for encode_base10().

parazyd 4 лет назад
Родитель
Сommit
c6a9d644d1
6 измененных файлов с 18 добавлено и 6 удалено
  1. 2 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 2 1
      Makefile
  4. 2 1
      src/bin/darkfid.rs
  5. 9 3
      src/error.rs
  6. 2 1
      src/util/parse.rs

+ 2 - 0
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]]

+ 1 - 0
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"

+ 2 - 1
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)

+ 2 - 1
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()));
                 }
             }

+ 9 - 3
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<url::ParseError> for Error {
     }
 }
 
+impl From<hex::FromHexError> for Error {
+    fn from(_err: hex::FromHexError) -> Error {
+        Error::FromHexError
+    }
+}
+
 impl From<std::num::ParseIntError> for Error {
     fn from(_err: std::num::ParseIntError) -> Error {
         Error::ParseIntError

+ 2 - 1
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<char> = format!("{:0width$}", amount, width = 1 + decimal_places)
         .chars()
         .collect();