mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 15:41:02 +00:00
Use array-bytes for All Array/Bytes/Hex Operations (#12190)
* Use `array-bytes` for All Array/Bytes/Hex Operations Signed-off-by: Xavier Lau <xavier@inv.cafe> * Reorder * Self Review * Format * Fix Tests * Bump `array-bytes` * Optimize large test res Signed-off-by: Xavier Lau <xavier@inv.cafe> Co-authored-by: parity-processbot <>
This commit is contained in:
@@ -9,12 +9,12 @@ description = "BEEFY Client gadget for substrate"
|
||||
homepage = "https://substrate.io"
|
||||
|
||||
[dependencies]
|
||||
array-bytes = "4.1"
|
||||
async-trait = "0.1.57"
|
||||
codec = { package = "parity-scale-codec", version = "3.0.0", features = ["derive"] }
|
||||
fnv = "1.0.6"
|
||||
futures = "0.3"
|
||||
futures-timer = "3.0.1"
|
||||
hex = "0.4.2"
|
||||
log = "0.4"
|
||||
parking_lot = "0.12.1"
|
||||
thiserror = "1.0"
|
||||
|
||||
@@ -69,9 +69,10 @@ pub(crate) mod beefy_protocol_name {
|
||||
genesis_hash: &Hash,
|
||||
chain_spec: &Box<dyn ChainSpec>,
|
||||
) -> ProtocolName {
|
||||
let genesis_hash = genesis_hash.as_ref();
|
||||
let chain_prefix = match chain_spec.fork_id() {
|
||||
Some(fork_id) => format!("/{}/{}", hex::encode(genesis_hash), fork_id),
|
||||
None => format!("/{}", hex::encode(genesis_hash)),
|
||||
Some(fork_id) => format!("/{}/{}", array_bytes::bytes2hex("", genesis_hash), fork_id),
|
||||
None => format!("/{}", array_bytes::bytes2hex("", genesis_hash)),
|
||||
};
|
||||
format!("{}{}", chain_prefix, NAME).into()
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ fn beefy_protocol_name() {
|
||||
|
||||
// Create protocol name using random genesis hash.
|
||||
let genesis_hash = H256::random();
|
||||
let expected = format!("/{}/beefy/1", hex::encode(genesis_hash));
|
||||
let expected = format!("/{}/beefy/1", array_bytes::bytes2hex("", genesis_hash.as_ref()));
|
||||
let proto_name = beefy_protocol_name::standard_name(&genesis_hash, &chain_spec);
|
||||
assert_eq!(proto_name.to_string(), expected);
|
||||
|
||||
|
||||
@@ -13,11 +13,11 @@ readme = "README.md"
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
array-bytes = "4.1"
|
||||
chrono = "0.4.10"
|
||||
clap = { version = "3.1.18", features = ["derive"] }
|
||||
fdlimit = "0.2.1"
|
||||
futures = "0.3.21"
|
||||
hex = "0.4.2"
|
||||
libp2p = "0.46.1"
|
||||
log = "0.4.17"
|
||||
names = { version = "0.13.0", default-features = false }
|
||||
|
||||
@@ -57,7 +57,7 @@ impl GenerateNodeKeyCmd {
|
||||
let file_data = if self.bin {
|
||||
secret.as_ref().to_owned()
|
||||
} else {
|
||||
hex::encode(secret.as_ref()).into_bytes()
|
||||
array_bytes::bytes2hex("", secret.as_ref()).into_bytes()
|
||||
};
|
||||
|
||||
match &self.file {
|
||||
@@ -85,6 +85,6 @@ mod tests {
|
||||
assert!(generate.run().is_ok());
|
||||
let mut buf = String::new();
|
||||
assert!(file.read_to_string(&mut buf).is_ok());
|
||||
assert!(hex::decode(buf).is_ok());
|
||||
assert!(array_bytes::hex2bytes(&buf).is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ fn expect_public_from_phrase<Pair: sp_core::Pair>(
|
||||
) -> Result<(), Error> {
|
||||
let secret_uri = SecretUri::from_str(suri).map_err(|e| format!("{:?}", e))?;
|
||||
let expected_public = if let Some(public) = expect_public.strip_prefix("0x") {
|
||||
let hex_public = hex::decode(&public)
|
||||
let hex_public = array_bytes::hex2bytes(public)
|
||||
.map_err(|_| format!("Invalid expected public key hex: `{}`", expect_public))?;
|
||||
Pair::Public::try_from(&hex_public)
|
||||
.map_err(|_| format!("Invalid expected public key: `{}`", expect_public))?
|
||||
@@ -208,7 +208,7 @@ mod tests {
|
||||
.expect("Valid")
|
||||
.0
|
||||
.public();
|
||||
let valid_public_hex = format!("0x{}", hex::encode(valid_public.as_slice()));
|
||||
let valid_public_hex = array_bytes::bytes2hex("0x", valid_public.as_slice());
|
||||
let valid_accountid = format!("{}", valid_public.into_account());
|
||||
|
||||
// It should fail with the invalid public key
|
||||
@@ -226,7 +226,7 @@ mod tests {
|
||||
.0
|
||||
.public();
|
||||
let valid_public_hex_with_password =
|
||||
format!("0x{}", hex::encode(&valid_public_with_password.as_slice()));
|
||||
array_bytes::bytes2hex("0x", valid_public_with_password.as_slice());
|
||||
let valid_accountid_with_password =
|
||||
format!("{}", &valid_public_with_password.into_account());
|
||||
|
||||
@@ -248,7 +248,7 @@ mod tests {
|
||||
.0
|
||||
.public();
|
||||
let valid_public_hex_with_password_and_derivation =
|
||||
format!("0x{}", hex::encode(&valid_public_with_password_and_derivation.as_slice()));
|
||||
array_bytes::bytes2hex("0x", valid_public_with_password_and_derivation.as_slice());
|
||||
|
||||
// They should still be valid, because we check the base secret key.
|
||||
check_cmd(&seed_with_password_and_derivation, &valid_public_hex_with_password, true);
|
||||
|
||||
@@ -66,7 +66,8 @@ impl InspectNodeKeyCmd {
|
||||
if !self.bin {
|
||||
// With hex input, give to the user a bit of tolerance about whitespaces
|
||||
let keyhex = String::from_utf8_lossy(&file_data);
|
||||
file_data = hex::decode(keyhex.trim()).map_err(|_| "failed to decode secret as hex")?;
|
||||
file_data = array_bytes::hex2bytes(keyhex.trim())
|
||||
.map_err(|_| "failed to decode secret as hex")?;
|
||||
}
|
||||
|
||||
let secret =
|
||||
|
||||
@@ -70,7 +70,7 @@ fn sign<P: sp_core::Pair>(
|
||||
message: Vec<u8>,
|
||||
) -> error::Result<String> {
|
||||
let pair = utils::pair_from_suri::<P>(suri, password)?;
|
||||
Ok(hex::encode(pair.sign(&message)))
|
||||
Ok(array_bytes::bytes2hex("", pair.sign(&message).as_ref()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -203,7 +203,7 @@ where
|
||||
Pair: sp_core::Pair,
|
||||
Pair::Public: Into<MultiSigner>,
|
||||
{
|
||||
let public = decode_hex(public_str)?;
|
||||
let public = array_bytes::hex2bytes(public_str)?;
|
||||
|
||||
let public_key = Pair::Public::try_from(&public)
|
||||
.map_err(|_| "Failed to construct public key from given hex")?;
|
||||
@@ -273,26 +273,17 @@ where
|
||||
format!("0x{}", HexDisplay::from(&public_key.into().into_account().as_ref()))
|
||||
}
|
||||
|
||||
/// helper method for decoding hex
|
||||
pub fn decode_hex<T: AsRef<[u8]>>(message: T) -> Result<Vec<u8>, Error> {
|
||||
let mut message = message.as_ref();
|
||||
if message[..2] == [b'0', b'x'] {
|
||||
message = &message[2..]
|
||||
}
|
||||
Ok(hex::decode(message)?)
|
||||
}
|
||||
|
||||
/// checks if message is Some, otherwise reads message from stdin and optionally decodes hex
|
||||
pub fn read_message(msg: Option<&String>, should_decode: bool) -> Result<Vec<u8>, Error> {
|
||||
let mut message = vec![];
|
||||
match msg {
|
||||
Some(m) => {
|
||||
message = decode_hex(m)?;
|
||||
message = array_bytes::hex2bytes(m.as_str())?;
|
||||
},
|
||||
None => {
|
||||
std::io::stdin().lock().read_to_end(&mut message)?;
|
||||
if should_decode {
|
||||
message = decode_hex(&message)?;
|
||||
message = array_bytes::hex2bytes(array_bytes::hex_bytes2hex_str(&message)?)?;
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
@@ -178,7 +178,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_generation_with_single_char() {
|
||||
let seed = generate_key::<sr25519::Pair>("ab", default_ss58_version()).unwrap();
|
||||
assert!(sr25519::Pair::from_seed_slice(&hex::decode(&seed[2..]).unwrap())
|
||||
assert!(sr25519::Pair::from_seed_slice(&array_bytes::hex2bytes_unchecked(&seed))
|
||||
.unwrap()
|
||||
.public()
|
||||
.to_ss58check()
|
||||
@@ -190,7 +190,7 @@ mod tests {
|
||||
let seed =
|
||||
generate_key::<sr25519::Pair>("ab", Ss58AddressFormatRegistry::PolkadotAccount.into())
|
||||
.unwrap();
|
||||
assert!(sr25519::Pair::from_seed_slice(&hex::decode(&seed[2..]).unwrap())
|
||||
assert!(sr25519::Pair::from_seed_slice(&array_bytes::hex2bytes_unchecked(&seed))
|
||||
.unwrap()
|
||||
.public()
|
||||
.to_ss58check_with_version(Ss58AddressFormatRegistry::PolkadotAccount.into())
|
||||
|
||||
@@ -55,7 +55,7 @@ impl VerifyCmd {
|
||||
/// Run the command
|
||||
pub fn run(&self) -> error::Result<()> {
|
||||
let message = utils::read_message(self.message.as_ref(), self.hex)?;
|
||||
let sig_data = utils::decode_hex(&self.sig)?;
|
||||
let sig_data = array_bytes::hex2bytes(&self.sig)?;
|
||||
let uri = utils::read_uri(self.uri.as_ref())?;
|
||||
let uri = if let Some(uri) = uri.strip_prefix("0x") { uri } else { &uri };
|
||||
|
||||
@@ -71,7 +71,7 @@ where
|
||||
let signature =
|
||||
Pair::Signature::try_from(&sig_data).map_err(|_| error::Error::SignatureFormatInvalid)?;
|
||||
|
||||
let pubkey = if let Ok(pubkey_vec) = hex::decode(uri) {
|
||||
let pubkey = if let Ok(pubkey_vec) = array_bytes::hex2bytes(uri) {
|
||||
Pair::Public::from_slice(pubkey_vec.as_slice())
|
||||
.map_err(|_| error::Error::KeyFormatInvalid)?
|
||||
} else {
|
||||
|
||||
@@ -69,8 +69,8 @@ pub enum Error {
|
||||
#[error("Key storage issue encountered")]
|
||||
KeyStorage(#[from] sc_keystore::Error),
|
||||
|
||||
#[error("Invalid hexadecimal string data")]
|
||||
HexDataConversion(#[from] hex::FromHexError),
|
||||
#[error("Invalid hexadecimal string data, {0:?}")]
|
||||
HexDataConversion(array_bytes::Error),
|
||||
|
||||
/// Application specific error chain sequence forwarder.
|
||||
#[error(transparent)]
|
||||
@@ -97,3 +97,9 @@ impl From<crypto::PublicError> for Error {
|
||||
Error::InvalidUri(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<array_bytes::Error> for Error {
|
||||
fn from(e: array_bytes::Error) -> Error {
|
||||
Error::HexDataConversion(e)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,7 +176,7 @@ mod tests {
|
||||
let file = tmp.path().join("mysecret").to_path_buf();
|
||||
let key = ed25519::SecretKey::generate();
|
||||
|
||||
fs::write(&file, hex::encode(key.as_ref())).expect("Writes secret key");
|
||||
fs::write(&file, array_bytes::bytes2hex("", key.as_ref())).expect("Writes secret key");
|
||||
check_key(file.clone(), &key);
|
||||
|
||||
fs::write(&file, &key).expect("Writes secret key");
|
||||
|
||||
@@ -37,8 +37,8 @@ sp-version = { version = "5.0.0", path = "../../primitives/version" }
|
||||
sp-wasm-interface = { version = "6.0.0", path = "../../primitives/wasm-interface" }
|
||||
|
||||
[dev-dependencies]
|
||||
array-bytes = "4.1"
|
||||
wat = "1.0"
|
||||
hex-literal = "0.3.4"
|
||||
sc-runtime-test = { version = "2.0.0", path = "runtime-test" }
|
||||
substrate-test-runtime = { version = "2.0.0", path = "../../test-utils/runtime" }
|
||||
sp-state-machine = { version = "0.12.0", path = "../../primitives/state-machine" }
|
||||
|
||||
@@ -21,7 +21,6 @@ mod linux;
|
||||
mod sandbox;
|
||||
|
||||
use codec::{Decode, Encode};
|
||||
use hex_literal::hex;
|
||||
use sc_executor_common::{error::Error, runtime_blob::RuntimeBlob, wasm_runtime::WasmModule};
|
||||
use sc_runtime_test::wasm_binary_unwrap;
|
||||
use sp_core::{
|
||||
@@ -391,16 +390,18 @@ fn sha2_256_should_work(wasm_method: WasmExecutionMethod) {
|
||||
let mut ext = ext.ext();
|
||||
assert_eq!(
|
||||
call_in_wasm("test_sha2_256", &[0], wasm_method, &mut ext,).unwrap(),
|
||||
hex!("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
|
||||
.to_vec()
|
||||
.encode(),
|
||||
array_bytes::hex2bytes_unchecked(
|
||||
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
||||
)
|
||||
.encode(),
|
||||
);
|
||||
assert_eq!(
|
||||
call_in_wasm("test_sha2_256", &b"Hello world!".to_vec().encode(), wasm_method, &mut ext,)
|
||||
.unwrap(),
|
||||
hex!("c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a")
|
||||
.to_vec()
|
||||
.encode(),
|
||||
array_bytes::hex2bytes_unchecked(
|
||||
"c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a"
|
||||
)
|
||||
.encode(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -410,16 +411,18 @@ fn twox_256_should_work(wasm_method: WasmExecutionMethod) {
|
||||
let mut ext = ext.ext();
|
||||
assert_eq!(
|
||||
call_in_wasm("test_twox_256", &[0], wasm_method, &mut ext,).unwrap(),
|
||||
hex!("99e9d85137db46ef4bbea33613baafd56f963c64b1f3685a4eb4abd67ff6203a")
|
||||
.to_vec()
|
||||
.encode(),
|
||||
array_bytes::hex2bytes_unchecked(
|
||||
"99e9d85137db46ef4bbea33613baafd56f963c64b1f3685a4eb4abd67ff6203a"
|
||||
)
|
||||
.encode(),
|
||||
);
|
||||
assert_eq!(
|
||||
call_in_wasm("test_twox_256", &b"Hello world!".to_vec().encode(), wasm_method, &mut ext,)
|
||||
.unwrap(),
|
||||
hex!("b27dfd7f223f177f2a13647b533599af0c07f68bda23d96d059da2b451a35a74")
|
||||
.to_vec()
|
||||
.encode(),
|
||||
array_bytes::hex2bytes_unchecked(
|
||||
"b27dfd7f223f177f2a13647b533599af0c07f68bda23d96d059da2b451a35a74"
|
||||
)
|
||||
.encode(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -429,12 +432,12 @@ fn twox_128_should_work(wasm_method: WasmExecutionMethod) {
|
||||
let mut ext = ext.ext();
|
||||
assert_eq!(
|
||||
call_in_wasm("test_twox_128", &[0], wasm_method, &mut ext,).unwrap(),
|
||||
hex!("99e9d85137db46ef4bbea33613baafd5").to_vec().encode(),
|
||||
array_bytes::hex2bytes_unchecked("99e9d85137db46ef4bbea33613baafd5").encode(),
|
||||
);
|
||||
assert_eq!(
|
||||
call_in_wasm("test_twox_128", &b"Hello world!".to_vec().encode(), wasm_method, &mut ext,)
|
||||
.unwrap(),
|
||||
hex!("b27dfd7f223f177f2a13647b533599af").to_vec().encode(),
|
||||
array_bytes::hex2bytes_unchecked("b27dfd7f223f177f2a13647b533599af").encode(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -704,7 +707,7 @@ fn parallel_execution(wasm_method: WasmExecutionMethod) {
|
||||
&[0],
|
||||
)
|
||||
.unwrap(),
|
||||
hex!("99e9d85137db46ef4bbea33613baafd5").to_vec().encode(),
|
||||
array_bytes::hex2bytes_unchecked("99e9d85137db46ef4bbea33613baafd5").encode()
|
||||
);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -15,12 +15,12 @@ targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
ahash = "0.7.6"
|
||||
array-bytes = "4.1"
|
||||
async-trait = "0.1.57"
|
||||
dyn-clone = "1.0"
|
||||
finality-grandpa = { version = "0.16.0", features = ["derive-codec"] }
|
||||
futures = "0.3.21"
|
||||
futures-timer = "3.0.1"
|
||||
hex = "0.4.2"
|
||||
log = "0.4.17"
|
||||
parity-scale-codec = { version = "3.0.0", features = ["derive"] }
|
||||
parking_lot = "0.12.1"
|
||||
|
||||
@@ -83,9 +83,10 @@ pub mod grandpa_protocol_name {
|
||||
genesis_hash: &Hash,
|
||||
chain_spec: &Box<dyn ChainSpec>,
|
||||
) -> ProtocolName {
|
||||
let genesis_hash = genesis_hash.as_ref();
|
||||
let chain_prefix = match chain_spec.fork_id() {
|
||||
Some(fork_id) => format!("/{}/{}", hex::encode(genesis_hash), fork_id),
|
||||
None => format!("/{}", hex::encode(genesis_hash)),
|
||||
Some(fork_id) => format!("/{}/{}", array_bytes::bytes2hex("", genesis_hash), fork_id),
|
||||
None => format!("/{}", array_bytes::bytes2hex("", genesis_hash)),
|
||||
};
|
||||
format!("{}{}", chain_prefix, NAME).into()
|
||||
}
|
||||
|
||||
@@ -646,7 +646,7 @@ fn grandpa_protocol_name() {
|
||||
|
||||
// Create protocol name using random genesis hash.
|
||||
let genesis_hash = sp_core::H256::random();
|
||||
let expected = format!("/{}/grandpa/1", hex::encode(genesis_hash));
|
||||
let expected = format!("/{}/grandpa/1", array_bytes::bytes2hex("", genesis_hash.as_ref()));
|
||||
let proto_name = grandpa_protocol_name::standard_name(&genesis_hash, &chain_spec);
|
||||
assert_eq!(proto_name.to_string(), expected);
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ readme = "README.md"
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
array-bytes = "4.1"
|
||||
async-trait = "0.1.57"
|
||||
hex = "0.4.0"
|
||||
parking_lot = "0.12.1"
|
||||
serde_json = "1.0.85"
|
||||
thiserror = "1.0"
|
||||
|
||||
@@ -512,8 +512,8 @@ impl KeystoreInner {
|
||||
/// Returns `None` if the keystore only exists in-memory and there isn't any path to provide.
|
||||
fn key_file_path(&self, public: &[u8], key_type: KeyTypeId) -> Option<PathBuf> {
|
||||
let mut buf = self.path.as_ref()?.clone();
|
||||
let key_type = hex::encode(key_type.0);
|
||||
let key = hex::encode(public);
|
||||
let key_type = array_bytes::bytes2hex("", &key_type.0);
|
||||
let key = array_bytes::bytes2hex("", public);
|
||||
buf.push(key_type + key.as_str());
|
||||
Some(buf)
|
||||
}
|
||||
@@ -534,7 +534,7 @@ impl KeystoreInner {
|
||||
|
||||
// skip directories and non-unicode file names (hex is unicode)
|
||||
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
|
||||
match hex::decode(name) {
|
||||
match array_bytes::hex2bytes(name) {
|
||||
Ok(ref hex) if hex.len() > 4 => {
|
||||
if hex[0..4] != id.0 {
|
||||
continue
|
||||
@@ -739,7 +739,7 @@ mod tests {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let store = LocalKeystore::open(temp_dir.path(), None).unwrap();
|
||||
|
||||
let file_name = temp_dir.path().join(hex::encode(&SR25519.0[..2]));
|
||||
let file_name = temp_dir.path().join(array_bytes::bytes2hex("", &SR25519.0[..2]));
|
||||
fs::write(file_name, "test").expect("Invalid file is written");
|
||||
|
||||
assert!(SyncCryptoStore::sr25519_public_keys(&store, SR25519).is_empty());
|
||||
|
||||
@@ -14,6 +14,7 @@ readme = "README.md"
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
array-bytes = "4.1"
|
||||
async-trait = "0.1"
|
||||
asynchronous-codec = "0.6"
|
||||
bitflags = "1.3.2"
|
||||
@@ -24,7 +25,6 @@ either = "1.5.3"
|
||||
fnv = "1.0.6"
|
||||
futures = "0.3.21"
|
||||
futures-timer = "3.0.2"
|
||||
hex = "0.4.0"
|
||||
ip_network = "0.4.1"
|
||||
libp2p = "0.46.1"
|
||||
linked_hash_set = "0.1.3"
|
||||
|
||||
@@ -17,11 +17,11 @@ targets = ["x86_64-unknown-linux-gnu"]
|
||||
prost-build = "0.10"
|
||||
|
||||
[dependencies]
|
||||
array-bytes = "4.1"
|
||||
codec = { package = "parity-scale-codec", version = "3.0.0", features = [
|
||||
"derive",
|
||||
] }
|
||||
futures = "0.3.21"
|
||||
hex = "0.4.0"
|
||||
libp2p = "0.46.1"
|
||||
log = "0.4.16"
|
||||
prost = "0.10"
|
||||
|
||||
@@ -27,10 +27,11 @@ use std::time::Duration;
|
||||
|
||||
/// Generate the light client protocol name from the genesis hash and fork id.
|
||||
fn generate_protocol_name<Hash: AsRef<[u8]>>(genesis_hash: Hash, fork_id: Option<&str>) -> String {
|
||||
let genesis_hash = genesis_hash.as_ref();
|
||||
if let Some(fork_id) = fork_id {
|
||||
format!("/{}/{}/light/2", hex::encode(genesis_hash), fork_id)
|
||||
format!("/{}/{}/light/2", array_bytes::bytes2hex("", genesis_hash), fork_id)
|
||||
} else {
|
||||
format!("/{}/light/2", hex::encode(genesis_hash))
|
||||
format!("/{}/light/2", array_bytes::bytes2hex("", genesis_hash))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -585,7 +585,7 @@ impl NodeKeyConfig {
|
||||
f,
|
||||
|mut b| match String::from_utf8(b.to_vec()).ok().and_then(|s| {
|
||||
if s.len() == 64 {
|
||||
hex::decode(&s).ok()
|
||||
array_bytes::hex2bytes(&s).ok()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
||||
@@ -369,10 +369,15 @@ where
|
||||
let block_announces_protocol = {
|
||||
let genesis_hash =
|
||||
chain.hash(0u32.into()).ok().flatten().expect("Genesis block exists; qed");
|
||||
let genesis_hash = genesis_hash.as_ref();
|
||||
if let Some(fork_id) = fork_id {
|
||||
format!("/{}/{}/block-announces/1", hex::encode(genesis_hash), fork_id)
|
||||
format!(
|
||||
"/{}/{}/block-announces/1",
|
||||
array_bytes::bytes2hex("", genesis_hash),
|
||||
fork_id
|
||||
)
|
||||
} else {
|
||||
format!("/{}/block-announces/1", hex::encode(genesis_hash))
|
||||
format!("/{}/block-announces/1", array_bytes::bytes2hex("", genesis_hash))
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -143,10 +143,11 @@ impl TransactionsHandlerPrototype {
|
||||
genesis_hash: Hash,
|
||||
fork_id: Option<String>,
|
||||
) -> Self {
|
||||
let genesis_hash = genesis_hash.as_ref();
|
||||
let protocol_name = if let Some(fork_id) = fork_id {
|
||||
format!("/{}/{}/transactions/1", hex::encode(genesis_hash), fork_id)
|
||||
format!("/{}/{}/transactions/1", array_bytes::bytes2hex("", genesis_hash), fork_id)
|
||||
} else {
|
||||
format!("/{}/transactions/1", hex::encode(genesis_hash))
|
||||
format!("/{}/transactions/1", array_bytes::bytes2hex("", genesis_hash))
|
||||
};
|
||||
let legacy_protocol_name = format!("/{}/transactions/1", protocol_id.as_ref());
|
||||
|
||||
|
||||
@@ -17,11 +17,11 @@ targets = ["x86_64-unknown-linux-gnu"]
|
||||
prost-build = "0.10"
|
||||
|
||||
[dependencies]
|
||||
array-bytes = "4.1"
|
||||
codec = { package = "parity-scale-codec", version = "3.0.0", features = [
|
||||
"derive",
|
||||
] }
|
||||
futures = "0.3.21"
|
||||
hex = "0.4.0"
|
||||
libp2p = "0.46.1"
|
||||
log = "0.4.17"
|
||||
lru = "0.7.5"
|
||||
|
||||
@@ -80,10 +80,11 @@ pub fn generate_protocol_config<Hash: AsRef<[u8]>>(
|
||||
|
||||
/// Generate the block protocol name from the genesis hash and fork id.
|
||||
fn generate_protocol_name<Hash: AsRef<[u8]>>(genesis_hash: Hash, fork_id: Option<&str>) -> String {
|
||||
let genesis_hash = genesis_hash.as_ref();
|
||||
if let Some(fork_id) = fork_id {
|
||||
format!("/{}/{}/sync/2", hex::encode(genesis_hash), fork_id)
|
||||
format!("/{}/{}/sync/2", array_bytes::bytes2hex("", genesis_hash), fork_id)
|
||||
} else {
|
||||
format!("/{}/sync/2", hex::encode(genesis_hash))
|
||||
format!("/{}/sync/2", array_bytes::bytes2hex("", genesis_hash))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -69,10 +69,11 @@ pub fn generate_protocol_config<Hash: AsRef<[u8]>>(
|
||||
|
||||
/// Generate the state protocol name from the genesis hash and fork id.
|
||||
fn generate_protocol_name<Hash: AsRef<[u8]>>(genesis_hash: Hash, fork_id: Option<&str>) -> String {
|
||||
let genesis_hash = genesis_hash.as_ref();
|
||||
if let Some(fork_id) = fork_id {
|
||||
format!("/{}/{}/state/2", hex::encode(genesis_hash), fork_id)
|
||||
format!("/{}/{}/state/2", array_bytes::bytes2hex("", genesis_hash), fork_id)
|
||||
} else {
|
||||
format!("/{}/state/2", hex::encode(genesis_hash))
|
||||
format!("/{}/state/2", array_bytes::bytes2hex("", genesis_hash))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -54,10 +54,11 @@ pub fn generate_request_response_config<Hash: AsRef<[u8]>>(
|
||||
|
||||
/// Generate the grandpa warp sync protocol name from the genesi hash and fork id.
|
||||
fn generate_protocol_name<Hash: AsRef<[u8]>>(genesis_hash: Hash, fork_id: Option<&str>) -> String {
|
||||
let genesis_hash = genesis_hash.as_ref();
|
||||
if let Some(fork_id) = fork_id {
|
||||
format!("/{}/{}/sync/warp", hex::encode(genesis_hash), fork_id)
|
||||
format!("/{}/{}/sync/warp", array_bytes::bytes2hex("", genesis_hash), fork_id)
|
||||
} else {
|
||||
format!("/{}/sync/warp", hex::encode(genesis_hash))
|
||||
format!("/{}/sync/warp", array_bytes::bytes2hex("", genesis_hash))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,12 +13,12 @@ readme = "README.md"
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
array-bytes = "4.1"
|
||||
bytes = "1.1"
|
||||
codec = { package = "parity-scale-codec", version = "3.0.0", features = ["derive"] }
|
||||
fnv = "1.0.6"
|
||||
futures = "0.3.21"
|
||||
futures-timer = "3.0.2"
|
||||
hex = "0.4"
|
||||
hyper = { version = "0.14.16", features = ["stream", "http2"] }
|
||||
hyper-rustls = { version = "0.23.0", features = ["http2"] }
|
||||
libp2p = { version = "0.46.1", default-features = false }
|
||||
|
||||
@@ -79,8 +79,8 @@ impl<Storage: OffchainStorage> offchain::DbExternalities for Db<Storage> {
|
||||
tracing::debug!(
|
||||
target: "offchain-worker::storage",
|
||||
?kind,
|
||||
key = ?hex::encode(key),
|
||||
value = ?hex::encode(value),
|
||||
key = ?array_bytes::bytes2hex("", key),
|
||||
value = ?array_bytes::bytes2hex("", value),
|
||||
"Write",
|
||||
);
|
||||
match kind {
|
||||
@@ -93,7 +93,7 @@ impl<Storage: OffchainStorage> offchain::DbExternalities for Db<Storage> {
|
||||
tracing::debug!(
|
||||
target: "offchain-worker::storage",
|
||||
?kind,
|
||||
key = ?hex::encode(key),
|
||||
key = ?array_bytes::bytes2hex("", key),
|
||||
"Clear",
|
||||
);
|
||||
match kind {
|
||||
@@ -112,9 +112,9 @@ impl<Storage: OffchainStorage> offchain::DbExternalities for Db<Storage> {
|
||||
tracing::debug!(
|
||||
target: "offchain-worker::storage",
|
||||
?kind,
|
||||
key = ?hex::encode(key),
|
||||
new_value = ?hex::encode(new_value),
|
||||
old_value = ?old_value.as_ref().map(hex::encode),
|
||||
key = ?array_bytes::bytes2hex("", key),
|
||||
new_value = ?array_bytes::bytes2hex("", new_value),
|
||||
old_value = ?old_value.as_ref().map(|s| array_bytes::bytes2hex("", s)),
|
||||
"CAS",
|
||||
);
|
||||
match kind {
|
||||
@@ -132,8 +132,8 @@ impl<Storage: OffchainStorage> offchain::DbExternalities for Db<Storage> {
|
||||
tracing::debug!(
|
||||
target: "offchain-worker::storage",
|
||||
?kind,
|
||||
key = ?hex::encode(key),
|
||||
result = ?result.as_ref().map(hex::encode),
|
||||
key = ?array_bytes::bytes2hex("", key),
|
||||
result = ?result.as_ref().map(|s| array_bytes::bytes2hex("", s)),
|
||||
"Read",
|
||||
);
|
||||
result
|
||||
|
||||
@@ -12,10 +12,9 @@ repository = "https://github.com/paritytech/substrate/"
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
array-bytes = "4.1"
|
||||
fdlimit = "0.2.1"
|
||||
futures = "0.3.21"
|
||||
hex = "0.4"
|
||||
hex-literal = "0.3.4"
|
||||
log = "0.4.17"
|
||||
parity-scale-codec = "3.0.0"
|
||||
parking_lot = "0.12.1"
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use futures::executor::block_on;
|
||||
use hex_literal::hex;
|
||||
use parity_scale_codec::{Decode, Encode, Joiner};
|
||||
use sc_block_builder::BlockBuilderProvider;
|
||||
use sc_client_api::{
|
||||
@@ -155,7 +154,9 @@ fn block1(genesis_hash: Hash, backend: &InMemoryBackend<BlakeTwo256>) -> (Vec<u8
|
||||
backend,
|
||||
1,
|
||||
genesis_hash,
|
||||
hex!("25e5b37074063ab75c889326246640729b40d0c86932edc527bc80db0e04fe5c").into(),
|
||||
array_bytes::hex_n_into_unchecked(
|
||||
"25e5b37074063ab75c889326246640729b40d0c86932edc527bc80db0e04fe5c",
|
||||
),
|
||||
vec![Transfer {
|
||||
from: AccountKeyring::One.into(),
|
||||
to: AccountKeyring::Two.into(),
|
||||
@@ -1594,7 +1595,7 @@ fn storage_keys_iter_prefix_and_start_key_works() {
|
||||
.build();
|
||||
|
||||
let child_root = b":child_storage:default:child".to_vec();
|
||||
let prefix = StorageKey(hex!("3a").to_vec());
|
||||
let prefix = StorageKey(array_bytes::hex2bytes_unchecked("3a"));
|
||||
let child_prefix = StorageKey(b"sec".to_vec());
|
||||
|
||||
let res: Vec<_> = client
|
||||
@@ -1604,25 +1605,29 @@ fn storage_keys_iter_prefix_and_start_key_works() {
|
||||
.collect();
|
||||
assert_eq!(
|
||||
res,
|
||||
[child_root.clone(), hex!("3a636f6465").to_vec(), hex!("3a686561707061676573").to_vec(),]
|
||||
[
|
||||
child_root.clone(),
|
||||
array_bytes::hex2bytes_unchecked("3a636f6465"),
|
||||
array_bytes::hex2bytes_unchecked("3a686561707061676573"),
|
||||
]
|
||||
);
|
||||
|
||||
let res: Vec<_> = client
|
||||
.storage_keys_iter(
|
||||
&BlockId::Number(0),
|
||||
Some(&prefix),
|
||||
Some(&StorageKey(hex!("3a636f6465").to_vec())),
|
||||
Some(&StorageKey(array_bytes::hex2bytes_unchecked("3a636f6465"))),
|
||||
)
|
||||
.unwrap()
|
||||
.map(|x| x.0)
|
||||
.collect();
|
||||
assert_eq!(res, [hex!("3a686561707061676573").to_vec()]);
|
||||
assert_eq!(res, [array_bytes::hex2bytes_unchecked("3a686561707061676573")]);
|
||||
|
||||
let res: Vec<_> = client
|
||||
.storage_keys_iter(
|
||||
&BlockId::Number(0),
|
||||
Some(&prefix),
|
||||
Some(&StorageKey(hex!("3a686561707061676573").to_vec())),
|
||||
Some(&StorageKey(array_bytes::hex2bytes_unchecked("3a686561707061676573"))),
|
||||
)
|
||||
.unwrap()
|
||||
.map(|x| x.0)
|
||||
@@ -1653,13 +1658,13 @@ fn storage_keys_iter_prefix_and_start_key_works() {
|
||||
fn storage_keys_iter_works() {
|
||||
let client = substrate_test_runtime_client::new();
|
||||
|
||||
let prefix = StorageKey(hex!("").to_vec());
|
||||
let prefix = StorageKey(array_bytes::hex2bytes_unchecked(""));
|
||||
|
||||
let res: Vec<_> = client
|
||||
.storage_keys_iter(&BlockId::Number(0), Some(&prefix), None)
|
||||
.unwrap()
|
||||
.take(8)
|
||||
.map(|x| hex::encode(&x.0))
|
||||
.map(|x| array_bytes::bytes2hex("", &x.0))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
res,
|
||||
@@ -1679,11 +1684,11 @@ fn storage_keys_iter_works() {
|
||||
.storage_keys_iter(
|
||||
&BlockId::Number(0),
|
||||
Some(&prefix),
|
||||
Some(&StorageKey(hex!("3a636f6465").to_vec())),
|
||||
Some(&StorageKey(array_bytes::hex2bytes_unchecked("3a636f6465"))),
|
||||
)
|
||||
.unwrap()
|
||||
.take(7)
|
||||
.map(|x| hex::encode(&x.0))
|
||||
.map(|x| array_bytes::bytes2hex("", &x.0))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
res,
|
||||
@@ -1702,13 +1707,13 @@ fn storage_keys_iter_works() {
|
||||
.storage_keys_iter(
|
||||
&BlockId::Number(0),
|
||||
Some(&prefix),
|
||||
Some(&StorageKey(
|
||||
hex!("79c07e2b1d2e2abfd4855b936617eeff5e0621c4869aa60c02be9adcc98a0d1d").to_vec(),
|
||||
)),
|
||||
Some(&StorageKey(array_bytes::hex2bytes_unchecked(
|
||||
"79c07e2b1d2e2abfd4855b936617eeff5e0621c4869aa60c02be9adcc98a0d1d",
|
||||
))),
|
||||
)
|
||||
.unwrap()
|
||||
.take(5)
|
||||
.map(|x| hex::encode(x.0))
|
||||
.map(|x| array_bytes::bytes2hex("", &x.0))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
res,
|
||||
|
||||
@@ -34,9 +34,9 @@ sp-tracing = { version = "5.0.0", path = "../../primitives/tracing" }
|
||||
sp-transaction-pool = { version = "4.0.0-dev", path = "../../primitives/transaction-pool" }
|
||||
|
||||
[dev-dependencies]
|
||||
array-bytes = "4.1"
|
||||
assert_matches = "1.3.0"
|
||||
criterion = "0.3"
|
||||
hex = "0.4"
|
||||
sc-block-builder = { version = "0.10.0-dev", path = "../block-builder" }
|
||||
sp-consensus = { version = "0.10.0-dev", path = "../../primitives/consensus/common" }
|
||||
substrate-test-runtime = { version = "2.0.0", path = "../../test-utils/runtime" }
|
||||
|
||||
@@ -880,7 +880,7 @@ fn should_not_accept_old_signatures() {
|
||||
|
||||
// generated with schnorrkel 0.1.1 from `_bytes`
|
||||
let old_singature = sp_core::sr25519::Signature::try_from(
|
||||
&hex::decode(
|
||||
&array_bytes::hex2bytes(
|
||||
"c427eb672e8c441c86d31f1a81b22b43102058e9ce237cabe9897ea5099ffd426\
|
||||
cd1c6a1f4f2869c3df57901d36bedcb295657adb3a4355add86ed234eb83108",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user