Swap Keys to "Opaque" tuple definitions (#1281)

This commit is contained in:
Jaco Greeff
2019-08-09 09:19:47 +02:00
committed by GitHub
parent 9c301551e0
commit 2e109ba2a5
4 changed files with 13 additions and 52 deletions
+4 -22
View File
@@ -22,10 +22,6 @@ import { eraLength } from '../session/eraLength';
import { recentlyOffline } from './recentlyOffline';
import { drr } from '../util/drr';
const EMPTY_KEYS = {
toU8a: (): Uint8Array => new Uint8Array()
};
type MultiResult = [Option<Keys>, [
Option<AccountId> | Vec<[AccountId, Keys] & Codec>, Option<StakingLedger>, [AccountId[]], RewardDestination, Exposure, [ValidatorPrefs]
]];
@@ -88,27 +84,13 @@ function redeemableSum (stakingLedger: StakingLedger | undefined, eraLength: BN,
);
}
function keysToAccountIds (keys: Uint8Array): AccountId[] {
const result: AccountId[] = [];
let offset = 0;
while (offset < keys.length) {
result.push(createType('AccountId', keys.subarray(offset, offset + 32)));
offset += 32;
}
return result;
}
function unwrapSessionIds (stashId: AccountId, queuedKeys: Option<AccountId> | Vec<[AccountId, Keys] & Codec>, nextKeys: Option<Keys>): { nextSessionIds: AccountId[]; nextSessionId?: AccountId; sessionIds: AccountId[]; sessionId?: AccountId } {
// for 2.x we have a Vec<(ValidatorId,Keys)> of the keys
if (Array.isArray(queuedKeys)) {
const sessionIds = keysToAccountIds(
(queuedKeys.find(([currentId]): boolean =>
currentId.eq(stashId)
) || [undefined, EMPTY_KEYS])[1].toU8a(true)
);
const nextSessionIds = keysToAccountIds(nextKeys.unwrapOr(EMPTY_KEYS).toU8a(true));
const sessionIds = (queuedKeys.find(([currentId]): boolean =>
currentId.eq(stashId)
) || [undefined, [] as AccountId[]])[1];
const nextSessionIds = nextKeys.unwrapOr([] as AccountId[]);
return {
nextSessionId: nextSessionIds[0],
@@ -110,7 +110,7 @@ describeE2E({
(await api.query.session.nextKeys<Keys>(
api.consts.session.dedupKeyPrefix,
keyring.bob.address
)).toArray()
))
);
console.error(result, [grandpa, babe, imOnline]);
@@ -10,16 +10,9 @@ export default {
// Defined in `node/runtime/src/lib.rs` as follow
// impl_opaque_keys! {
// pub struct SessionKeys {
SessionKeysSubstrate: {
grandpa: 'AccountId', // aka GrandpaId
babe: 'AccountId', // aka BabeId
imOnline: 'AccountId' // aka ImOnlineId
},
SessionKeysPolkadot: {
grandpa: 'AccountId', // aka GrandpaId
babe: 'AccountId', // aka BabeId
imOnline: 'AccountId' // aka ImOnlineId
},
// Here we revert to tuples to keep the interfaces "opaque", as per the use
SessionKeysSubstrate: '(AccountId, AccountId, AccountId)', // Grandpa, Babe, ImOnline
SessionKeysPolkadot: '(AccountId, AccountId, AccountId)',
Keys: 'SessionKeysSubstrate'
}
};
+5 -19
View File
@@ -1,6 +1,6 @@
// Auto-generated via `yarn build:interfaces`, do not edit
import { Struct } from '../../codec';
import { Codec } from '../../types';
import { u32 } from '../../primitive';
import { AccountId } from '../runtime';
@@ -10,22 +10,8 @@ export type Keys = SessionKeysSubstrate;
/** u32 */
export type SessionIndex = u32;
/** Struct */
export interface SessionKeysPolkadot extends Struct {
/** AccountId */
readonly grandpa: AccountId;
/** AccountId */
readonly babe: AccountId;
/** AccountId */
readonly imOnline: AccountId;
}
/** [AccountId, AccountId, AccountId] & Codec */
export type SessionKeysPolkadot = [AccountId, AccountId, AccountId] & Codec;
/** Struct */
export interface SessionKeysSubstrate extends Struct {
/** AccountId */
readonly grandpa: AccountId;
/** AccountId */
readonly babe: AccountId;
/** AccountId */
readonly imOnline: AccountId;
}
/** [AccountId, AccountId, AccountId] & Codec */
export type SessionKeysSubstrate = [AccountId, AccountId, AccountId] & Codec;