|
| 1 | +// Copyright 2019-2022 Parity Technologies (UK) Ltd. |
| 2 | +// This file is part of subxt. |
| 3 | +// |
| 4 | +// subxt is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// subxt is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with subxt. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +use crate::{ |
| 18 | + node_runtime::{ |
| 19 | + self, |
| 20 | + DispatchError, |
| 21 | + }, |
| 22 | + pair_signer, |
| 23 | + test_context, |
| 24 | +}; |
| 25 | +use sp_keyring::AccountKeyring; |
| 26 | + |
| 27 | +#[async_std::test] |
| 28 | +async fn storage_plain_lookup() -> Result<(), subxt::Error<DispatchError>> { |
| 29 | + let ctx = test_context().await; |
| 30 | + |
| 31 | + // Look up a plain value |
| 32 | + let entry = ctx.api.storage().timestamp().now(None).await?; |
| 33 | + assert!(entry > 0); |
| 34 | + |
| 35 | + Ok(()) |
| 36 | +} |
| 37 | + |
| 38 | +#[async_std::test] |
| 39 | +async fn storage_map_lookup() -> Result<(), subxt::Error<DispatchError>> { |
| 40 | + let ctx = test_context().await; |
| 41 | + |
| 42 | + let signer = pair_signer(AccountKeyring::Alice.pair()); |
| 43 | + let alice = AccountKeyring::Alice.to_account_id(); |
| 44 | + |
| 45 | + // Do some transaction to bump the Alice nonce to 1: |
| 46 | + ctx.api |
| 47 | + .tx() |
| 48 | + .system() |
| 49 | + .remark(vec![1, 2, 3, 4, 5]) |
| 50 | + .sign_and_submit_then_watch(&signer) |
| 51 | + .await? |
| 52 | + .wait_for_finalized_success() |
| 53 | + .await?; |
| 54 | + |
| 55 | + // Look up the nonce for the user (we expect it to be 1). |
| 56 | + let entry = ctx.api.storage().system().account(alice, None).await?; |
| 57 | + assert_eq!(entry.nonce, 1); |
| 58 | + |
| 59 | + Ok(()) |
| 60 | +} |
| 61 | + |
| 62 | +// This fails until the fix in https://github.com/paritytech/subxt/pull/458 is introduced. |
| 63 | +// Here we create a key that looks a bit like a StorageNMap key, but should in fact be |
| 64 | +// treated as a StorageKey (ie we should hash both values together with one hasher, rather |
| 65 | +// than hash both values separately, or ignore the second value). |
| 66 | +#[async_std::test] |
| 67 | +async fn storage_n_mapish_key_is_properly_created( |
| 68 | +) -> Result<(), subxt::Error<DispatchError>> { |
| 69 | + use codec::Encode; |
| 70 | + use node_runtime::{ |
| 71 | + runtime_types::sp_core::crypto::KeyTypeId, |
| 72 | + session::storage::KeyOwner, |
| 73 | + }; |
| 74 | + use subxt::{ |
| 75 | + storage::StorageKeyPrefix, |
| 76 | + StorageEntry, |
| 77 | + }; |
| 78 | + |
| 79 | + // This is what the generated code hashes a `session().key_owner(..)` key into: |
| 80 | + let actual_key_bytes = KeyOwner(KeyTypeId([1, 2, 3, 4]), vec![5u8, 6, 7, 8]) |
| 81 | + .key() |
| 82 | + .final_key(StorageKeyPrefix::new::<KeyOwner>()) |
| 83 | + .0; |
| 84 | + |
| 85 | + // Let's manually hash to what we assume it should be and compare: |
| 86 | + let expected_key_bytes = { |
| 87 | + // Hash the prefix to the storage entry: |
| 88 | + let mut bytes = sp_core::twox_128("Session".as_bytes()).to_vec(); |
| 89 | + bytes.extend(&sp_core::twox_128("KeyOwner".as_bytes())[..]); |
| 90 | + // twox64_concat a *tuple* of the args expected: |
| 91 | + let suffix = (KeyTypeId([1, 2, 3, 4]), vec![5u8, 6, 7, 8]).encode(); |
| 92 | + bytes.extend(&sp_core::twox_64(&suffix)); |
| 93 | + bytes.extend(&suffix); |
| 94 | + bytes |
| 95 | + }; |
| 96 | + |
| 97 | + assert_eq!(actual_key_bytes, expected_key_bytes); |
| 98 | + Ok(()) |
| 99 | +} |
| 100 | + |
| 101 | +#[async_std::test] |
| 102 | +async fn storage_n_map_storage_lookup() -> Result<(), subxt::Error<DispatchError>> { |
| 103 | + let ctx = test_context().await; |
| 104 | + |
| 105 | + // Boilerplate; we create a new asset class with ID 99, and then |
| 106 | + // we "approveTransfer" of some of this asset class. This gives us an |
| 107 | + // entry in the `Approvals` StorageNMap that we can try to look up. |
| 108 | + let signer = pair_signer(AccountKeyring::Alice.pair()); |
| 109 | + let alice = AccountKeyring::Alice.to_account_id(); |
| 110 | + let bob = AccountKeyring::Bob.to_account_id(); |
| 111 | + ctx.api |
| 112 | + .tx() |
| 113 | + .assets() |
| 114 | + .create(99, alice.clone().into(), 1) |
| 115 | + .sign_and_submit_then_watch(&signer) |
| 116 | + .await? |
| 117 | + .wait_for_finalized_success() |
| 118 | + .await?; |
| 119 | + ctx.api |
| 120 | + .tx() |
| 121 | + .assets() |
| 122 | + .approve_transfer(99, bob.clone().into(), 123) |
| 123 | + .sign_and_submit_then_watch(&signer) |
| 124 | + .await? |
| 125 | + .wait_for_finalized_success() |
| 126 | + .await?; |
| 127 | + |
| 128 | + // The actual test; look up this approval in storage: |
| 129 | + let entry = ctx |
| 130 | + .api |
| 131 | + .storage() |
| 132 | + .assets() |
| 133 | + .approvals(99, alice, bob, None) |
| 134 | + .await?; |
| 135 | + assert_eq!(entry.map(|a| a.amount), Some(123)); |
| 136 | + Ok(()) |
| 137 | +} |
0 commit comments