mirror of
https://github.com/alexgo-io/stacks-puppet-node.git
synced 2026-01-12 16:53:21 +08:00
feat: optimize c32 address
This commit is contained in:
79
Cargo.lock
generated
79
Cargo.lock
generated
@@ -939,6 +939,12 @@ dependencies = [
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fuchsia-cprng"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
|
||||
|
||||
[[package]]
|
||||
name = "fuchsia-zircon"
|
||||
version = "0.3.3"
|
||||
@@ -1020,6 +1026,12 @@ dependencies = [
|
||||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gcc"
|
||||
version = "0.3.55"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2"
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.12.4"
|
||||
@@ -1938,6 +1950,29 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.3.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rand 0.4.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
|
||||
dependencies = [
|
||||
"fuchsia-cprng",
|
||||
"libc",
|
||||
"rand_core 0.3.1",
|
||||
"rdrand",
|
||||
"winapi 0.3.9",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.7.3"
|
||||
@@ -1983,6 +2018,21 @@ dependencies = [
|
||||
"rand_core 0.6.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
|
||||
dependencies = [
|
||||
"rand_core 0.4.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.5.1"
|
||||
@@ -2044,6 +2094,15 @@ dependencies = [
|
||||
"num_cpus",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rdrand"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
|
||||
dependencies = [
|
||||
"rand_core 0.3.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.2.10"
|
||||
@@ -2203,12 +2262,31 @@ dependencies = [
|
||||
"smallvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust-crypto"
|
||||
version = "0.2.36"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a"
|
||||
dependencies = [
|
||||
"gcc",
|
||||
"libc",
|
||||
"rand 0.3.23",
|
||||
"rustc-serialize",
|
||||
"time 0.1.44",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-serialize"
|
||||
version = "0.3.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
|
||||
|
||||
[[package]]
|
||||
name = "rustc_version"
|
||||
version = "0.2.3"
|
||||
@@ -2649,6 +2727,7 @@ dependencies = [
|
||||
"rstest",
|
||||
"rstest_reuse",
|
||||
"rusqlite",
|
||||
"rust-crypto",
|
||||
"secp256k1",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
|
||||
@@ -14,7 +14,7 @@ resolver = "2"
|
||||
edition = "2021"
|
||||
|
||||
[profile.release]
|
||||
debug = true
|
||||
debug = false
|
||||
|
||||
[lib]
|
||||
name = "blockstack_lib"
|
||||
|
||||
@@ -1134,6 +1134,8 @@ impl StandardPrincipalData {
|
||||
impl fmt::Display for StandardPrincipalData {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let c32_str = self.to_address();
|
||||
// f.write_str(c32_str.as_str()).unwrap();
|
||||
// Ok(())
|
||||
write!(f, "{}", c32_str)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ slog-term = "2.6.0"
|
||||
slog-json = { version = "2.3.0", optional = true }
|
||||
chrono = "0.4.19"
|
||||
libc = "0.2.82"
|
||||
rust-crypto = "0.2.36"
|
||||
|
||||
[dependencies.serde_json]
|
||||
version = "1.0"
|
||||
@@ -68,7 +69,7 @@ slog_json = ["slog-json"]
|
||||
testing = []
|
||||
|
||||
[target.'cfg(all(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64"), not(target_env = "msvc")))'.dependencies]
|
||||
sha2 = { version = "0.10", features = ["asm"] }
|
||||
sha2 = { version = "0.10.2", features = ["asm"] }
|
||||
|
||||
[target.'cfg(any(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64")), target_env = "msvc"))'.dependencies]
|
||||
sha2 = { version = "0.10" }
|
||||
sha2 = { version = "0.10.2" }
|
||||
@@ -14,11 +14,16 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::borrow::{Borrow, BorrowMut};
|
||||
use std::collections::HashMap;
|
||||
use super::Error;
|
||||
|
||||
use sha2::Digest;
|
||||
use sha2::Sha256;
|
||||
use std::convert::TryFrom;
|
||||
use std::ops::Add;
|
||||
use std::sync::{Arc, Mutex, MutexGuard, Once};
|
||||
use libc::wchar_t;
|
||||
|
||||
const C32_CHARACTERS: &[u8; 32] = b"0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
||||
|
||||
@@ -289,7 +294,19 @@ fn c32_decode_ascii(input_str: &str) -> Result<Vec<u8>, Error> {
|
||||
}
|
||||
|
||||
fn double_sha256_checksum(data: &[u8]) -> Vec<u8> {
|
||||
let tmp = Sha256::digest(Sha256::digest(data));
|
||||
use crypto::digest::Digest;
|
||||
use crypto::sha2::Sha256;
|
||||
let mut tmp = vec![0u8; 32];
|
||||
{
|
||||
let mut d1 = Sha256::new();
|
||||
d1.input(data);
|
||||
d1.result(&mut tmp);
|
||||
}
|
||||
{
|
||||
let mut d2 = Sha256::new();
|
||||
d2.input(&tmp);
|
||||
d2.result(&mut tmp);
|
||||
}
|
||||
tmp[0..4].to_vec()
|
||||
}
|
||||
|
||||
@@ -363,13 +380,40 @@ pub fn c32_address_decode(c32_address_str: &str) -> Result<(u8, Vec<u8>), Error>
|
||||
}
|
||||
}
|
||||
|
||||
static mut c32_address_cache: Option<Mutex<HashMap<String, String>>> = None;
|
||||
static INIT: Once = Once::new();
|
||||
|
||||
pub fn c32_address(version: u8, data: &[u8]) -> Result<String, Error> {
|
||||
let c32_string = c32_check_encode(version, data)?;
|
||||
Ok(format!("S{}", c32_string))
|
||||
INIT.call_once(|| {
|
||||
unsafe {
|
||||
*c32_address_cache.borrow_mut() = Some(Mutex::new(HashMap::new()))
|
||||
}
|
||||
});
|
||||
let mut key = String::new();
|
||||
key.push(version as char);
|
||||
for &u in data {
|
||||
key.push(u as char)
|
||||
}
|
||||
let mut cache = unsafe {
|
||||
c32_address_cache.as_ref().unwrap().lock().expect("should be able to lock global cache")
|
||||
};
|
||||
let r = cache.get(&key[..]);
|
||||
match r {
|
||||
Some(s) => {
|
||||
Ok(s.clone())
|
||||
}
|
||||
None => {
|
||||
let c32_string = "S".to_string().add(c32_check_encode(version, data)?.as_str());
|
||||
cache.insert(key, c32_string.clone());
|
||||
Ok(c32_string)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::time::SystemTime;
|
||||
use libc::printf;
|
||||
use super::super::c32_old::{
|
||||
c32_address as c32_address_old, c32_address_decode as c32_address_decode_old,
|
||||
};
|
||||
@@ -478,6 +522,21 @@ mod test {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encode() {
|
||||
let start = SystemTime::now();
|
||||
for r in 0..10000 {
|
||||
// c32_check_encode(1, &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]).unwrap();
|
||||
Sha256::digest(&[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
|
||||
// use crypto::digest::Digest;
|
||||
// let mut digest = crypto::sha2::Sha256::new();
|
||||
// let mut out: Vec<u8> = vec![0; 32];
|
||||
// digest.input(&[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
|
||||
// digest.result(&mut out);
|
||||
}
|
||||
println!("Costs: {}", start.elapsed().unwrap().as_millis())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple() {
|
||||
let hex_strings = &[
|
||||
|
||||
Reference in New Issue
Block a user