JoshOrndorff / recipes

A Hands-On Cookbook for Aspiring Blockchain Chefs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What is the default value of `StorageValue`

ashWhiteHat opened this issue · comments

What I did

I updated the sum-storage pallet to FRAME v2.
https://github.com/NoCtrlZ/recipes/blob/master/pallets/sum-storage/src/lib.rs

Error

The error occurred when I tried to run the test.
https://github.com/NoCtrlZ/recipes/blob/master/pallets/sum-storage/src/tests.rs

(base) ➜  sum-storage git:(master) ✗ cargo test
   Compiling sum-storage v3.0.0 (/Users/user/recipes/pallets/sum-storage)
    Finished test [unoptimized + debuginfo] target(s) in 4.51s
     Running unittests (/Users/user/recipes/target/debug/deps/sum_storage-5e8566f4a4aa95ba)

running 5 tests
test tests::__construct_runtime_integrity_test::runtime_integrity_tests ... ok
test tests::sums_thing_two ... FAILED
test tests::default_sum_zero ... FAILED
test tests::sums_both_values ... ok
test tests::sums_thing_one ... FAILED

failures:

---- tests::sums_thing_two stdout ----
thread 'tests::sums_thing_two' panicked at 'called `Option::unwrap()` on a `None` value', pallets/sum-storage/src/lib.rs:75:28

---- tests::default_sum_zero stdout ----
thread 'tests::default_sum_zero' panicked at 'called `Option::unwrap()` on a `None` value', pallets/sum-storage/src/lib.rs:75:28
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- tests::sums_thing_one stdout ----
thread 'tests::sums_thing_one' panicked at 'called `Option::unwrap()` on a `None` value', pallets/sum-storage/src/lib.rs:75:58


failures:
    tests::default_sum_zero
    tests::sums_thing_one
    tests::sums_thing_two

test result: FAILED. 2 passed; 3 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass '--lib'

Question

I have a question about StorageValue.
Does StorageValue getter method return the None?
It seems the function returns QueryKind::Query here.

Thank you.

Looks like the old code used 0 as a default and your new code makes it an option. I believe this is related to whether you use OptionQuery or ValueQuery but I don't know much more about it.

Indeed, you can read more about it at https://crates.parity.io/frame_support/storage/types/trait.QueryKindTrait.html

I think you need to change the definition of the storage item.

	#[pallet::storage]
	#[pallet::getter(fn thing1)]
-	pub type Thing1<T: Config> = StorageValue<_, u32>;
+	pub type Thing1<T: Config> = StorageValue<_, u32, ValueQuery>;

Something like that.

Thank you for the answer!
It worked well.
I created PR for this updating.
#450
Thank you!