diff --git a/packages/client-db-chain/src/state/consensys/types.js b/packages/client-db-chain/src/state/consensys/types.js index b72fe5e9..6269fff9 100644 --- a/packages/client-db-chain/src/state/consensys/types.js +++ b/packages/client-db-chain/src/state/consensys/types.js @@ -3,17 +3,11 @@ // of the ISC license. See the LICENSE file for details. // @flow -import type BN from 'bn.js'; +import type { IdWithU8a, NoneWithBn } from '../typesShared'; -export type ChainDb$State$Consensys$Authority = { - get: (id: BN | number) => Uint8Array, - set: (id: BN | number, publicKey: Uint8Array) => void -}; +export type ChainDb$State$Consensys$Authority = IdWithU8a; -export type ChainDb$State$Consensys$AuthorityCount = { - get: () => BN, - set: (count: BN | number) => void -}; +export type ChainDb$State$Consensys$AuthorityCount = NoneWithBn; export type ChainDb$State$Consensys = { authority: ChainDb$State$Consensys$Authority, diff --git a/packages/client-db-chain/src/state/governance/types.js b/packages/client-db-chain/src/state/governance/types.js index 1c1a290e..95bfe372 100644 --- a/packages/client-db-chain/src/state/governance/types.js +++ b/packages/client-db-chain/src/state/governance/types.js @@ -3,12 +3,9 @@ // of the ISC license. See the LICENSE file for details. // @flow -import type BN from 'bn.js'; +import type { NoneWithBn } from '../typesShared'; -export type ChainDb$State$Governance$ApprovalsRatio = { - get: () => BN, - set: (ratio: BN | number) => void -}; +export type ChainDb$State$Governance$ApprovalsRatio = NoneWithBn; export type ChainDb$State$Governance = { approvalsRatio: ChainDb$State$Governance$ApprovalsRatio diff --git a/packages/client-db-chain/src/state/session/types.js b/packages/client-db-chain/src/state/session/types.js index 0f7cc24a..bce2b83c 100644 --- a/packages/client-db-chain/src/state/session/types.js +++ b/packages/client-db-chain/src/state/session/types.js @@ -3,22 +3,13 @@ // of the ISC license. See the LICENSE file for details. // @flow -import type BN from 'bn.js'; +import type { IdWithU8a, NoneWithBn } from '../typesShared'; -export type ChainDb$State$Session$Length = { - get: () => BN, - set: (length: BN | number) => void -}; +export type ChainDb$State$Session$Length = NoneWithBn; -export type ChainDb$State$Session$Value = { - get: (id: BN | number) => Uint8Array, - set: (id: BN | number, publicKey: Uint8Array) => void -}; +export type ChainDb$State$Session$Value = IdWithU8a; -export type ChainDb$State$Session$ValueCount = { - get: () => BN, - set: (count: BN | number) => void -} +export type ChainDb$State$Session$ValueCount = NoneWithBn; export type ChainDb$State$Session = { length: ChainDb$State$Session$Length, diff --git a/packages/client-db-chain/src/state/staking/types.js b/packages/client-db-chain/src/state/staking/types.js index 8d53e045..0f10820e 100644 --- a/packages/client-db-chain/src/state/staking/types.js +++ b/packages/client-db-chain/src/state/staking/types.js @@ -4,36 +4,22 @@ // @flow import type BN from 'bn.js'; +import type { IdWithU8a, NoneWithBn } from '../typesShared'; export type ChainDb$State$Staking$Balance = { get (publicKey: Uint8Array): BN, set (publicKey: Uint8Array, value: BN | number): void } -export type ChainDb$State$Staking$CurrentEra = { - get: () => BN, - set: (era: BN | number) => void -} +export type ChainDb$State$Staking$CurrentEra = NoneWithBn; -export type ChainDb$State$Staking$Intent = { - get: (id: BN | number) => Uint8Array, - set: (id: BN | number, publicKey: Uint8Array) => void, -} +export type ChainDb$State$Staking$Intent = IdWithU8a; -export type ChainDb$State$Staking$IntentLength = { - get: () => BN, - set: (length: BN | number) => void -} +export type ChainDb$State$Staking$IntentLength = NoneWithBn; -export type ChainDb$State$Staking$SessionsPerEra = { - get: () => BN, - set: (count: BN | number) => void -} +export type ChainDb$State$Staking$SessionsPerEra = NoneWithBn; -export type ChainDb$State$Staking$ValidatorCount = { - get: () => BN, - set: (count: BN | number) => void -} +export type ChainDb$State$Staking$ValidatorCount = NoneWithBn; export type ChainDb$State$Staking = { balance: ChainDb$State$Staking$Balance, diff --git a/packages/client-db-chain/src/state/system/nonce.js b/packages/client-db-chain/src/state/system/nonce.js index 93892175..2d341349 100644 --- a/packages/client-db-chain/src/state/system/nonce.js +++ b/packages/client-db-chain/src/state/system/nonce.js @@ -13,7 +13,7 @@ module.exports = function nonce (db: WrapDbInterface): ChainDb$State$System$Nonc return { get: (publicKey: Uint8Array): BN => db.getBn64(NONCE_OF(publicKey)), - set: (value: number | BN, publicKey: Uint8Array): void => - db.detBn64(NONCE_OF(publicKey), value) + set: (publicKey: Uint8Array, value: number | BN): void => + db.setBn64(NONCE_OF(publicKey), value) }; }; diff --git a/packages/client-db-chain/src/state/system/nonce.spec.js b/packages/client-db-chain/src/state/system/nonce.spec.js index 7e480089..65dadf0e 100644 --- a/packages/client-db-chain/src/state/system/nonce.spec.js +++ b/packages/client-db-chain/src/state/system/nonce.spec.js @@ -12,28 +12,39 @@ describe('nonce', () => { let system; beforeEach(() => { + const store = { + '0xc7f790aa4fc95a8813b0d5734a8c195b': hexToU8a('0x0100000000000000') + }; + system = index({ - get: (key) => { - switch (u8aToHex(key)) { - case '0xc7f790aa4fc95a8813b0d5734a8c195b': - return hexToU8a('0x0100000000000000'); - - default: - return new Uint8Array([]); - } + get: (key) => store[u8aToHex(key)] || new Uint8Array([]), + set: (key, value) => { + store[u8aToHex(key)] = value; } }).system; }); - it('returns nonce', () => { - expect( - system.nonce.get(keyring.one.publicKey()).toNumber() - ).toEqual(1); + describe('get', () => { + it('returns nonce', () => { + expect( + system.nonce.get(keyring.one.publicKey()).toNumber() + ).toEqual(1); + }); + + it('returns zero nonces', () => { + expect( + system.nonce.get(keyring.alice.publicKey()).toNumber() + ).toEqual(0); + }); }); - it('returns zero nonces', () => { - expect( - system.nonce.get(keyring.alice.publicKey()).toNumber() - ).toEqual(0); + describe('set', () => { + it('sets a nonce', () => { + system.nonce.set(keyring.alice.publicKey(), 666); + + expect( + system.nonce.get(keyring.alice.publicKey()).toNumber() + ).toEqual(666); + }); }); }); diff --git a/packages/client-db-chain/src/state/system/types.js b/packages/client-db-chain/src/state/system/types.js index 5c413dc9..b8355ea3 100644 --- a/packages/client-db-chain/src/state/system/types.js +++ b/packages/client-db-chain/src/state/system/types.js @@ -4,11 +4,9 @@ // @flow import type BN from 'bn.js'; +import type { IdWithU8a } from '../typesShared'; -export type ChainDb$State$System$BlockHash = { - get: (block: BN | number) => Uint8Array, - set: (block: BN | number, hash: Uint8Array) => void -}; +export type ChainDb$State$System$BlockHash = IdWithU8a; export type ChainDb$State$System$Code = { get: () => Uint8Array, @@ -17,7 +15,7 @@ export type ChainDb$State$System$Code = { export type ChainDb$State$System$Nonce = { get: (publicKey: Uint8Array) => BN, - set: (nonce: number | BN, publicKey: Uint8Array) => void + set: (publicKey: Uint8Array, nonce: number | BN) => void }; export type ChainDb$State$System = { diff --git a/packages/client-db-chain/src/state/typesShared.js b/packages/client-db-chain/src/state/typesShared.js new file mode 100644 index 00000000..d41b6722 --- /dev/null +++ b/packages/client-db-chain/src/state/typesShared.js @@ -0,0 +1,16 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. +// @flow + +import type BN from 'bn.js'; + +export type IdWithU8a = { + get: (id: BN | number) => Uint8Array, + set: (id: BN | number, u8a: Uint8Array) => void +}; + +export type NoneWithBn = { + get: () => BN, + set: (length: BN | number) => void +}