From 59616684698cd6d763a6bfba42cc6a07b1d981b0 Mon Sep 17 00:00:00 2001 From: open-junius Date: Fri, 10 May 2024 19:15:19 +0800 Subject: [PATCH 1/3] fix nonce to bytes --- pallets/subtensor/src/registration.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/subtensor/src/registration.rs b/pallets/subtensor/src/registration.rs index 90ce2d961..9ba79ccfe 100644 --- a/pallets/subtensor/src/registration.rs +++ b/pallets/subtensor/src/registration.rs @@ -546,7 +546,7 @@ impl Pallet { } pub fn create_seal_hash(block_number_u64: u64, nonce_u64: u64, hotkey: &T::AccountId) -> H256 { - let nonce = nonce_u64.to_be_bytes(); + let nonce = U256::from(nonce_u64); let block_hash_at_number: H256 = Self::get_block_hash_from_u64(block_number_u64); let block_hash_bytes: &[u8; 32] = block_hash_at_number.as_fixed_bytes(); let binding = Self::hash_block_and_hotkey(block_hash_bytes, hotkey); From 5f93914a6aacc459d9b885439fda2ab450a4c793 Mon Sep 17 00:00:00 2001 From: open-junius Date: Fri, 10 May 2024 19:39:59 +0800 Subject: [PATCH 2/3] use le types --- pallets/subtensor/src/registration.rs | 62 ++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/pallets/subtensor/src/registration.rs b/pallets/subtensor/src/registration.rs index 9ba79ccfe..f02026f35 100644 --- a/pallets/subtensor/src/registration.rs +++ b/pallets/subtensor/src/registration.rs @@ -546,7 +546,7 @@ impl Pallet { } pub fn create_seal_hash(block_number_u64: u64, nonce_u64: u64, hotkey: &T::AccountId) -> H256 { - let nonce = U256::from(nonce_u64); + let nonce = nonce_u64.to_le_bytes(); let block_hash_at_number: H256 = Self::get_block_hash_from_u64(block_number_u64); let block_hash_bytes: &[u8; 32] = block_hash_at_number.as_fixed_bytes(); let binding = Self::hash_block_and_hotkey(block_hash_bytes, hotkey); @@ -728,3 +728,63 @@ impl Pallet { Ok(Some(weight).into()) } } + +#[test] +fn test_seal_hash() { + use sp_core::{Get, H256, U256}; + let nonce_u64 = 123456_u64; + let nonce = nonce_u64.to_le_bytes(); + let block_and_hotkey_hash_bytes: &[u8; 32] = &[1u8; 32]; + + let mut full_bytes = [0u8; 40]; + let (first_chunk, second_chunk) = full_bytes.split_at_mut(8); + first_chunk.copy_from_slice(&nonce); + second_chunk.copy_from_slice(block_and_hotkey_hash_bytes); + + let nonce = U256::from(nonce_u64); + + let second_full_bytes: &[u8; 40] = &[ + nonce.byte(0), + nonce.byte(1), + nonce.byte(2), + nonce.byte(3), + nonce.byte(4), + nonce.byte(5), + nonce.byte(6), + nonce.byte(7), + block_and_hotkey_hash_bytes[0], + block_and_hotkey_hash_bytes[1], + block_and_hotkey_hash_bytes[2], + block_and_hotkey_hash_bytes[3], + block_and_hotkey_hash_bytes[4], + block_and_hotkey_hash_bytes[5], + block_and_hotkey_hash_bytes[6], + block_and_hotkey_hash_bytes[7], + block_and_hotkey_hash_bytes[8], + block_and_hotkey_hash_bytes[9], + block_and_hotkey_hash_bytes[10], + block_and_hotkey_hash_bytes[11], + block_and_hotkey_hash_bytes[12], + block_and_hotkey_hash_bytes[13], + block_and_hotkey_hash_bytes[14], + block_and_hotkey_hash_bytes[15], + block_and_hotkey_hash_bytes[16], + block_and_hotkey_hash_bytes[17], + block_and_hotkey_hash_bytes[18], + block_and_hotkey_hash_bytes[19], + block_and_hotkey_hash_bytes[20], + block_and_hotkey_hash_bytes[21], + block_and_hotkey_hash_bytes[22], + block_and_hotkey_hash_bytes[23], + block_and_hotkey_hash_bytes[24], + block_and_hotkey_hash_bytes[25], + block_and_hotkey_hash_bytes[26], + block_and_hotkey_hash_bytes[27], + block_and_hotkey_hash_bytes[28], + block_and_hotkey_hash_bytes[29], + block_and_hotkey_hash_bytes[30], + block_and_hotkey_hash_bytes[31], + ]; + + assert_eq!(full_bytes, *second_full_bytes); +} From 1a538c50866bc47f55fca8c755ed55cd33466c0e Mon Sep 17 00:00:00 2001 From: open-junius Date: Fri, 10 May 2024 19:41:46 +0800 Subject: [PATCH 3/3] remove unneed test --- pallets/subtensor/src/registration.rs | 60 --------------------------- 1 file changed, 60 deletions(-) diff --git a/pallets/subtensor/src/registration.rs b/pallets/subtensor/src/registration.rs index f02026f35..7edf3e358 100644 --- a/pallets/subtensor/src/registration.rs +++ b/pallets/subtensor/src/registration.rs @@ -728,63 +728,3 @@ impl Pallet { Ok(Some(weight).into()) } } - -#[test] -fn test_seal_hash() { - use sp_core::{Get, H256, U256}; - let nonce_u64 = 123456_u64; - let nonce = nonce_u64.to_le_bytes(); - let block_and_hotkey_hash_bytes: &[u8; 32] = &[1u8; 32]; - - let mut full_bytes = [0u8; 40]; - let (first_chunk, second_chunk) = full_bytes.split_at_mut(8); - first_chunk.copy_from_slice(&nonce); - second_chunk.copy_from_slice(block_and_hotkey_hash_bytes); - - let nonce = U256::from(nonce_u64); - - let second_full_bytes: &[u8; 40] = &[ - nonce.byte(0), - nonce.byte(1), - nonce.byte(2), - nonce.byte(3), - nonce.byte(4), - nonce.byte(5), - nonce.byte(6), - nonce.byte(7), - block_and_hotkey_hash_bytes[0], - block_and_hotkey_hash_bytes[1], - block_and_hotkey_hash_bytes[2], - block_and_hotkey_hash_bytes[3], - block_and_hotkey_hash_bytes[4], - block_and_hotkey_hash_bytes[5], - block_and_hotkey_hash_bytes[6], - block_and_hotkey_hash_bytes[7], - block_and_hotkey_hash_bytes[8], - block_and_hotkey_hash_bytes[9], - block_and_hotkey_hash_bytes[10], - block_and_hotkey_hash_bytes[11], - block_and_hotkey_hash_bytes[12], - block_and_hotkey_hash_bytes[13], - block_and_hotkey_hash_bytes[14], - block_and_hotkey_hash_bytes[15], - block_and_hotkey_hash_bytes[16], - block_and_hotkey_hash_bytes[17], - block_and_hotkey_hash_bytes[18], - block_and_hotkey_hash_bytes[19], - block_and_hotkey_hash_bytes[20], - block_and_hotkey_hash_bytes[21], - block_and_hotkey_hash_bytes[22], - block_and_hotkey_hash_bytes[23], - block_and_hotkey_hash_bytes[24], - block_and_hotkey_hash_bytes[25], - block_and_hotkey_hash_bytes[26], - block_and_hotkey_hash_bytes[27], - block_and_hotkey_hash_bytes[28], - block_and_hotkey_hash_bytes[29], - block_and_hotkey_hash_bytes[30], - block_and_hotkey_hash_bytes[31], - ]; - - assert_eq!(full_bytes, *second_full_bytes); -}