diff --git a/packages/rpc-core/test/e2e/state.test.ts b/packages/rpc-core/test/e2e/state.test.ts index 4ec7ade4fe..cc3fa15611 100644 --- a/packages/rpc-core/test/e2e/state.test.ts +++ b/packages/rpc-core/test/e2e/state.test.ts @@ -2,13 +2,24 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { Balance, Bytes, Metadata, Moment } from '@polkadot/types'; -import storage from '@polkadot/storage/static'; +import fs from 'fs'; +import path from 'path'; + +import { SubmittableResult } from '@polkadot/api'; +import ApiPromise from '@polkadot/api/promise/Api'; +import { Abi } from '@polkadot/api-contract'; +import testingPairs from '@polkadot/keyring/testingPairs'; +import { KeyringPair } from '@polkadot/keyring/types'; import WsProvider from '@polkadot/rpc-provider/ws'; +import storage from '@polkadot/storage/static'; +import { Balance, Bytes, Hash, Metadata, Moment, StorageData, StorageKey } from '@polkadot/types'; import Rpc from '../../src'; +import flipperAbi from '../../../api-contract/test/contracts/flipper.json'; const ALICE = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'; +const CODE = '0x3a636f6465'; // :code +const CHILD_STORAGE = '0x3a6368696c645f73746f726167653a'; // :child_storage: describe.skip('e2e state', () => { let api: Rpc; @@ -18,22 +29,7 @@ describe.skip('e2e state', () => { api = new Rpc(new WsProvider('ws://127.0.0.1:9944')); }); - it('retrieves code', () => { - return api.state - .getStorage([ - storage.substrate.code - ]) - .then((code: Bytes) => { - console.error(code.toHex().substr(0, 256), '...'); - }) - .catch((error) => { - console.error(error); - - throw error; - }); - }); - - it('retrieves the wasm metadata', () => { + it('getMetadata(): retrieves the wasm metadata', () => { return api.state .getMetadata() .then((meta: Metadata) => { @@ -46,37 +42,139 @@ describe.skip('e2e state', () => { }); }); - it('retrieves balances', () => { + it('getKeys(): retrieves storage keys for ":code"', () => { return api.state - .getStorage([ - storage.balances.freeBalance, ALICE - ]) - .then((balance: Balance) => { - console.error(balance); - - expect(balance.isZero()).not.toEqual(true); - }) - .catch((error) => { - console.error(error); - - throw error; + .getKeys(CODE) + .then((keys: Array) => { + expect(keys.length).toEqual(1); }); }); - it('retrieves timestamp', () => { - return api.state - .getStorage([ - storage.timestamp.now - ]) - .then((moment: Moment) => { - console.error(moment); + describe('test-suite getStorage()', () => { + it('retrieves code', () => { + return api.state + .getStorage([ + storage.substrate.code + ]) + .then((code: Bytes) => { + console.error(code.toHex().substr(0, 256), '...'); + }) + .catch((error) => { + console.error(error); - expect(moment.toNumber()).not.toEqual(0); - }) - .catch((error) => { - console.error(error); + throw error; + }); + }); - throw error; + it('retrieves balances', () => { + return api.state + .getStorage([ + storage.balances.freeBalance, ALICE + ]) + .then((balance: Balance) => { + console.error(balance); + + expect(balance.isZero()).not.toEqual(true); + }) + .catch((error) => { + console.error(error); + + throw error; + }); + }); + + it('retrieves timestamp', () => { + return api.state + .getStorage([ + storage.timestamp.now + ]) + .then((moment: Moment) => { + console.error(moment); + + expect(moment.toNumber()).not.toEqual(0); + }) + .catch((error) => { + console.error(error); + + throw error; + }); + }); + }); + + describe('e2e state child methods', () => { + // `child_storage` is currently not used anywhere in substrate, that's why we need to + // add a Smart Contract that is using `child_storage` before being able to test it. + let codeHash: Hash; + + beforeAll(async (done) => { + const code: string = fs.readFileSync(path.join(__dirname, '../../../api-contract/test/contracts/flipper-pruned.wasm')).toString('hex'); + const abi = new Abi(flipperAbi); + const apiPromise: ApiPromise = await ApiPromise.create(new WsProvider('ws://127.0.0.1:9944')); + const keyring: { + [index: string]: KeyringPair + } = testingPairs({ type: 'sr25519' }); + + const putCode = apiPromise.tx.contract + .putCode(50000, `0x${code}`) + .signAndSend(keyring.eve, (result: SubmittableResult) => { + if (result.status.isFinalized) { + const record = result.findRecord('contract', 'CodeStored'); + + if (record) { + codeHash = record.event.data[0] as Hash; + } + } + done(); + }); + + return putCode.then(() => { + return apiPromise.tx.contract + .create(12345, 50000, codeHash, abi.deploy()) + .signAndSend(keyring.bob); }); + }); + + it('getChildKeys(): retrieves :child_storage: keys for one deployed flipper contract', async () => { + const storageKeys = await api.state.getKeys(CHILD_STORAGE); + + return api.state + .getChildKeys(storageKeys[0], '0x') + .then((keys: Array) => { + expect(keys.length).toBeGreaterThanOrEqual(1); + }); + }); + + it('getChildStorage(): retrieves the default value of the flipper smart contract', async () => { + const storageKeys = await api.state.getKeys(CHILD_STORAGE); + const childStorageKeys = await api.state.getChildKeys(storageKeys[0], '0x'); + + return api.state + .getChildStorage(storageKeys[0], childStorageKeys[0]) + .then((storage: StorageData) => { + expect(storage.toString()).toBe('0x00'); + }); + }); + + it('getChildStorageHash(): retrieves the Hash of the flipper smart contract', async () => { + const storageKeys = await api.state.getKeys(CHILD_STORAGE); + const childStorageKeys = await api.state.getChildKeys(storageKeys[0], '0x'); + + return api.state + .getChildStorageHash(storageKeys[0], childStorageKeys[0]) + .then((storage: StorageData) => { + expect(storage.toString()).toBe('0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314'); + }); + }); + + it('getChildStorageSize(): retrieves the size of the flipper smart contract', async () => { + const storageKeys = await api.state.getKeys(CHILD_STORAGE); + const childStorageKeys = await api.state.getChildKeys(storageKeys[0], '0x'); + + return api.state + .getChildStorageSize(storageKeys[0], childStorageKeys[0]) + .then((storage: StorageData) => { + expect(storage.toString()).toBe('1'); + }); + }); }); }); diff --git a/packages/type-jsonrpc/src/state.ts b/packages/type-jsonrpc/src/state.ts index 166a8cc77b..ed15e544be 100644 --- a/packages/type-jsonrpc/src/state.ts +++ b/packages/type-jsonrpc/src/state.ts @@ -19,6 +19,16 @@ const call: RpcMethodOpt = { type: 'Bytes' }; +const getKeys: RpcMethodOpt = { + description: 'Retrieves the keys with a certain prefix', + params: [ + createParam('prefix', 'StorageKey'), + createParam('block', 'Hash', { isOptional: true }) + ], + // @ts-ignore The Vec<> wrap is fine + type: 'Vec' +}; + const getStorage: RpcMethodOpt = { description: 'Retrieves the storage for a key', params: [ @@ -46,6 +56,47 @@ const getStorageSize: RpcMethodOpt = { type: 'u64' }; +const getChildKeys: RpcMethodOpt = { + description: 'Retrieves the keys with prefix of a specific child storage', + params: [ + createParam('childStorageKey', 'StorageKey'), + createParam('prefix', 'StorageKey'), + createParam('block', 'Hash', { isOptional: true }) + ], + // @ts-ignore The Vec<> wrap is fine + type: 'Vec' +}; + +const getChildStorage: RpcMethodOpt = { + description: 'Retrieves the child storage for a key', + params: [ + createParam('childStorageKey', 'StorageKey'), + createParam('key', 'StorageKey'), + createParam('block', 'Hash', { isOptional: true }) + ], + type: 'StorageData' +}; + +const getChildStorageHash: RpcMethodOpt = { + description: 'Retrieves the child storage hash', + params: [ + createParam('childStorageKey', 'StorageKey'), + createParam('key', 'StorageKey'), + createParam('block', 'Hash', { isOptional: true }) + ], + type: 'Hash' +}; + +const getChildStorageSize: RpcMethodOpt = { + description: 'Retrieves the child storage size', + params: [ + createParam('childStorageKey', 'StorageKey'), + createParam('key', 'StorageKey'), + createParam('block', 'Hash', { isOptional: true }) + ], + type: 'u64' +}; + const getMetadata: RpcMethodOpt = { description: 'Returns the runtime metadata', params: [ @@ -100,6 +151,11 @@ export default { section, methods: { call: createMethod(section, 'call', call), + getChildKeys: createMethod(section, 'getChildKeys', getChildKeys), + getChildStorage: createMethod(section, 'getChildStorage', getChildStorage), + getChildStorageHash: createMethod(section, 'getChildStorageHash', getChildStorageHash), + getChildStorageSize: createMethod(section, 'getChildStorageSize', getChildStorageSize), + getKeys: createMethod(section, 'getKeys', getKeys), getMetadata: createMethod(section, 'getMetadata', getMetadata), getRuntimeVersion: createMethod(section, 'getRuntimeVersion', getRuntimeVersion), getStorage: createMethod(section, 'getStorage', getStorage), diff --git a/packages/type-storage/src/fromMetadata/storage.spec.ts b/packages/type-storage/src/fromMetadata/storage.spec.ts index f55bba2c5b..c44b218e23 100644 --- a/packages/type-storage/src/fromMetadata/storage.spec.ts +++ b/packages/type-storage/src/fromMetadata/storage.spec.ts @@ -13,6 +13,7 @@ describe('storage', () => { expect(storage.substrate.authorityCount).toBeTruthy(); expect(storage.substrate.authorityPrefix).toBeTruthy(); expect(storage.substrate.changesTrieConfig).toBeTruthy(); + expect(storage.substrate.childStorageKeyPrefix).toBeTruthy(); expect(storage.substrate.code).toBeTruthy(); expect(storage.substrate.extrinsicIndex).toBeTruthy(); expect(storage.substrate.heapPages).toBeTruthy(); diff --git a/packages/type-storage/src/fromMetadata/substrate.spec.ts b/packages/type-storage/src/fromMetadata/substrate.spec.ts index 865d9edf90..fe9576891a 100644 --- a/packages/type-storage/src/fromMetadata/substrate.spec.ts +++ b/packages/type-storage/src/fromMetadata/substrate.spec.ts @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { authorityCount, authorityPrefix, changesTrieConfig, code, extrinsicIndex, heapPages } from './substrate'; +import { authorityCount, authorityPrefix, changesTrieConfig, childStorageKeyPrefix, code, extrinsicIndex, heapPages } from './substrate'; describe('substrate', () => { @@ -20,6 +20,10 @@ describe('substrate', () => { expect(changesTrieConfig()).toEqual(Uint8Array.from([52, 58, 99, 104, 97, 110, 103, 101, 115, 95, 116, 114, 105, 101])); // Length-prefixed }); + it('childStorageKeyPrefix should return the correct storage key', () => { + expect(childStorageKeyPrefix()).toEqual(Uint8Array.from([ 60, 58, 99, 104, 105, 108, 100, 95, 115, 116, 111, 114, 97, 103, 101, 58])); // Length-prefixed + }); + it('code should return the correct storage key', () => { expect(code()).toEqual(Uint8Array.from([20, 58, 99, 111, 100, 101])); // Length-prefixed }); diff --git a/packages/type-storage/src/fromMetadata/substrate.ts b/packages/type-storage/src/fromMetadata/substrate.ts index 2933201ea7..a5512b8f1b 100644 --- a/packages/type-storage/src/fromMetadata/substrate.ts +++ b/packages/type-storage/src/fromMetadata/substrate.ts @@ -61,3 +61,8 @@ export const changesTrieConfig = createRuntimeFunction('changesTrieConfig', ':ch documentation: 'Changes trie configuration is stored under this key.', type: 'u32' }); + +export const childStorageKeyPrefix = createRuntimeFunction('childStorageKeyPrefix', ':child_storage:', { + documentation: 'Prefix of child storage keys.', + type: 'u32' +}); diff --git a/packages/types/src/scripts/METHODS_STORAGE_SUBSTRATE.md b/packages/types/src/scripts/METHODS_STORAGE_SUBSTRATE.md index d38af4b52a..d3580d3005 100644 --- a/packages/types/src/scripts/METHODS_STORAGE_SUBSTRATE.md +++ b/packages/types/src/scripts/METHODS_STORAGE_SUBSTRATE.md @@ -8,6 +8,9 @@ _These are keys that are always available to the runtime implementation_ ▸ **changesTrieConfig**(): `u32` - **summary**: Changes trie configuration is stored under this key. +▸ **childStorageKeyPrefix**(): `u32` +- **summary**: Prefix of child storage keys. + ▸ **code**(): `Bytes` - **summary**: Wasm code of the runtime.