DoubleMap extended tests (#1179)
* DoubleMap extended tests * TwoX64Concat test (aligned with Rust) * Remove .only * Update Type.ts * And working * Check the full JSON output for the keys (future-proof) * Renames
This commit is contained in:
@@ -58,6 +58,7 @@ export default function createFunction ({ meta, method, prefix, section }: Creat
|
||||
|
||||
if (meta.type.isDoubleMap) {
|
||||
assert(!isUndefined(arg) && !isNull(arg) && !isUndefined(arg[0]) && !isNull(arg[0]) && !isUndefined(arg[1]) && !isNull(arg[1]), `${meta.name} expects two arguments`);
|
||||
|
||||
const type1 = meta.type.asDoubleMap.key1.toString();
|
||||
const type2 = meta.type.asDoubleMap.key2.toString();
|
||||
const param1Encoded = u8aConcat(key, createType(type1, arg[0]).toU8a(true));
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright 2017-2019 @polkadot/api-metadata authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { StorageHasher } from '@polkadot/types';
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
import { xxhashAsU8a } from '@polkadot/util-crypto';
|
||||
|
||||
import getHasher from './getHasher';
|
||||
|
||||
describe('getHasher', (): void => {
|
||||
describe('Twox64Concat', (): void => {
|
||||
it('matches the foo test from Rust', (): void => {
|
||||
const hasher = getHasher(new StorageHasher('Twox64Concat'));
|
||||
const hash = hasher('foo');
|
||||
const xxhash = xxhashAsU8a('foo', 128);
|
||||
|
||||
expect([
|
||||
hash.subarray(0, 8),
|
||||
hash.subarray(8)
|
||||
]).toEqual([
|
||||
xxhash.subarray(0, 8),
|
||||
stringToU8a('foo')
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -2,8 +2,10 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import testingPairs from '@polkadot/keyring/testingPairs';
|
||||
import WsProvider from '@polkadot/rpc-provider/ws';
|
||||
import { Header, Option } from '@polkadot/types';
|
||||
import { encodeAddress, randomAsU8a } from '@polkadot/util-crypto';
|
||||
|
||||
import ApiPromise from '../../../src/promise';
|
||||
import describeE2E from '../../util/describeE2E';
|
||||
@@ -18,12 +20,14 @@ describeE2E({
|
||||
]
|
||||
})('Promise e2e doubleMap queries (since Metadata v5)', (wsUrl): void => {
|
||||
let api: ApiPromise;
|
||||
const keyring = testingPairs({ type: 'ed25519' });
|
||||
|
||||
beforeEach(async (done): Promise<void> => {
|
||||
api = await ApiPromise.create(new WsProvider(wsUrl));
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
// TODO Update ['any', '0x1234'] to the key of a known event topic and update EXPECTED_VALUE to the expected value
|
||||
describe('with double map type', (): void => {
|
||||
const KEY1 = 'any';
|
||||
@@ -90,4 +94,51 @@ describeE2E({
|
||||
expect(size.toNumber()).toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('session Keys', (): void => {
|
||||
it('sets a session key, retrieves the value', async (done): Promise<void> => {
|
||||
const ed25519 = encodeAddress(randomAsU8a());
|
||||
|
||||
async function queryKeys (): Promise<void> {
|
||||
console.error('*** query.session.nextKeys');
|
||||
|
||||
const result = JSON.stringify(
|
||||
await api.query.session.nextKeys(
|
||||
api.consts.session.dedupKeyPrefix, keyring.bob.address
|
||||
)
|
||||
);
|
||||
|
||||
console.error(result, ed25519);
|
||||
|
||||
expect(result).toEqual(JSON.stringify({ ed25519 }));
|
||||
done();
|
||||
}
|
||||
|
||||
async function setKeys (): Promise<void> {
|
||||
console.error('*** tx.session.setKeys');
|
||||
|
||||
await api.tx.session
|
||||
.setKeys({ ed25519 }, new Uint8Array([]))
|
||||
.signAndSend(keyring.eve, (result): void => {
|
||||
if (result.isCompleted) {
|
||||
queryKeys();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function setBond (): Promise<void> {
|
||||
console.error('*** tx.staking.bond');
|
||||
|
||||
await api.tx.staking
|
||||
.bond(keyring.eve.address, 123456789012345, 'Stash')
|
||||
.signAndSend(keyring.bob, (result): void => {
|
||||
if (result.isCompleted) {
|
||||
setKeys();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
setBond();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1483,7 +1483,7 @@
|
||||
"constants": [
|
||||
{
|
||||
"name": "DEDUP_KEY_PREFIX",
|
||||
"type": "Bytes",
|
||||
"type": "Data",
|
||||
"value": "0x343a73657373696f6e3a6b657973",
|
||||
"documentation": [
|
||||
" Used as first key for `NextKeys` and `KeyOwner` to put all the data into the same branch",
|
||||
|
||||
@@ -92,7 +92,7 @@ export default class U8a extends Uint8Array implements Codec {
|
||||
* @description Returns the base runtime type name for this instance
|
||||
*/
|
||||
public toRawType (): string {
|
||||
return `Vec<u8>`;
|
||||
return `&[u8]`;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,10 +10,4 @@ import U8a from '../codec/U8a';
|
||||
* A raw data structure. It is just an encoding of a U8a, without any length encoding
|
||||
*/
|
||||
export default class Data extends U8a {
|
||||
/**
|
||||
* @description Returns the base runtime type name for this instance
|
||||
*/
|
||||
public toRawType (): string {
|
||||
return 'Data';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,8 +57,8 @@ export default class Type extends Text {
|
||||
Type._alias('Compact<Index>', 'IndexCompact'),
|
||||
// alias Vec<u8> -> Bytes
|
||||
Type._alias('Vec<u8>', 'Bytes'),
|
||||
// alias &[u8] -> Bytes
|
||||
Type._alias('&\\[u8\\]', 'Bytes'),
|
||||
// alias &[u8] -> Data (this has no length-prefix)
|
||||
Type._alias('&\\[u8\\]', 'Data'),
|
||||
// alias RawAddress -> Address
|
||||
Type._alias('RawAddress', 'Address'),
|
||||
// alias Lookup::Source to Address (_could_ be AccountId on certain chains)
|
||||
|
||||
Reference in New Issue
Block a user