Skip to content

Commit

Permalink
Cleanup duplicate types
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Mar 23, 2018
1 parent 6aed7d0 commit 83ad342
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 70 deletions.
12 changes: 3 additions & 9 deletions packages/client-db-chain/src/state/consensys/types.js
Expand Up @@ -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,
Expand Down
7 changes: 2 additions & 5 deletions packages/client-db-chain/src/state/governance/types.js
Expand Up @@ -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
Expand Down
17 changes: 4 additions & 13 deletions packages/client-db-chain/src/state/session/types.js
Expand Up @@ -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,
Expand Down
26 changes: 6 additions & 20 deletions packages/client-db-chain/src/state/staking/types.js
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/client-db-chain/src/state/system/nonce.js
Expand Up @@ -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)
};
};
43 changes: 27 additions & 16 deletions packages/client-db-chain/src/state/system/nonce.spec.js
Expand Up @@ -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);
});
});
});
8 changes: 3 additions & 5 deletions packages/client-db-chain/src/state/system/types.js
Expand Up @@ -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,
Expand All @@ -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 = {
Expand Down
16 changes: 16 additions & 0 deletions 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
}

0 comments on commit 83ad342

Please sign in to comment.