0

I have a function that creates hash from AccountId's.

#[pallet::call_index(2)]
#[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())]
pub fn connection_request(origin: OriginFor<T>, to: T::AccountId) -> DispatchResult {
    let who = ensure_signed(origin)?;

    let xor = Self::create_xor(&who, &to);
    ...
impl<T: Config> Pallet<T> {
    fn create_xor<Input: Encode>(a: &Input, b: &Input) -> T::Hash {
        let mut bytes_who = a.encode();
        let bytes_to = b.encode();

        bytes_who.iter_mut().zip(bytes_to.iter()).for_each(|(x1, x2)| *x1 ^= *x2);

        T::Hash::decode(&mut bytes_who.as_slice()).expect("all bytes are valid accounts, and the source of these bytes is the xor of two other accounts, thus this must also be a valid account/hash. qed",)
    }
}

And in my tests, create_xor fails with Error { cause: None, desc: "Not enough data to fill buffer" }

#[test]
fn connection_request_should_work() {
    new_test_ext().execute_with(|| {
        System::set_block_number(1);
        assert_ok!(MyPallet::connection_request(RuntimeOrigin::signed(1), 2));
        ...
    });
}

What is the reason I get this error and how do I solve it?

1 Answer 1

1

Then length of an encoded AccountId seems to be shorter than a Hash.
The decoding therefore fails, since "Not enough data to fill buffer" for that hash.

What is the reason I get this error and how do I solve it?

I don't know what you are trying to do. Maybe use a builtin hash function like sp_core::keccak_256?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.