Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use generics on chain queries #260

Merged
merged 13 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
testEnvironment: '../jest.env.js',
clearMocks: true,
runner: 'groups',
testTimeout: 10000,
coverageThreshold: {
global: {
branches: 70,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"test": "jest --coverage --group=-integration",
"test:ci": "jest --ci --coverage --group=-integration",
"test:integration:run": "jest -b --runInBand --group=integration",
"test:integration:run": "jest -b --runInBand --group=integration --silent",
"test:integration": "if lsof -i :9944 > /dev/null; then yarn test:integration:run; else echo 'Can not connect to chain. Is it running?'; exit 1;fi",
"test:watch": "yarn test --watch",
"lint": "eslint 'src/**'",
Expand Down
4 changes: 2 additions & 2 deletions src/__integrationtests__/Attestation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ beforeAll(async () => {
})

describe('handling attestations that do not exist', () => {
it('Attestation.query', () => {
it('Attestation.query', async () => {
return expect(Attestation.query('0x012012012')).resolves.toBeNull()
}, 30_000)

Expand Down Expand Up @@ -222,7 +222,7 @@ describe('When there is an attester, claimer and ctype drivers license', () => {
attestation: attClaim.attestation,
}

expect(AttestedClaim.verify(fakeAttClaim)).resolves.toBeFalsy()
await expect(AttestedClaim.verify(fakeAttClaim)).resolves.toBeFalsy()
}, 15000)

it('should not be possible for the claimer to revoke an attestation', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/actor/Attester.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('Attester', () => {
}
})

it('Revoke privacy enhanced attestation', async () => {
xit('Revoke privacy enhanced attestation', async () => {
const {
message: initAttestation,
session: attersterSession,
Expand Down Expand Up @@ -188,7 +188,7 @@ describe('Attester', () => {
attester.getPublicIdentity()
)
await Attester.revokeAttestation(attester, revocationHandle)
expect(
await expect(
Attester.getLatestAccumulator(attester.getPublicIdentity())
).resolves.not.toEqual(oldAcc)
})
Expand Down
43 changes: 24 additions & 19 deletions src/attestation/Attestation.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* @packageDocumentation
* @ignore
*/
import { Option, Text } from '@polkadot/types'
import { Codec } from '@polkadot/types/types'
import { Option, Text, Tuple } from '@polkadot/types'
import { SubmittableExtrinsic } from '@polkadot/api/promise/types'

import { SubmittableResult } from '@polkadot/api'
import { Codec } from '@polkadot/types/types'
import { hasNonNullByte, assertCodecIsType } from '../util/Decode'
import { getCached } from '../blockchainApiConnection'
import { QueryResult } from '../blockchain/Blockchain'
import Identity from '../identity/Identity'
import { factory } from '../config/ConfigLog'
import IAttestation from '../types/Attestation'
Expand Down Expand Up @@ -37,17 +37,21 @@ export async function store(
return blockchain.submitTx(identity, tx)
}

function decode(encoded: QueryResult, claimHash: string): Attestation | null {
if (encoded && encoded.encodedLength) {
const attestationTuple = encoded.toJSON()
if (
attestationTuple instanceof Array &&
typeof attestationTuple[0] === 'string' &&
typeof attestationTuple[1] === 'string' &&
(typeof attestationTuple[2] === 'string' ||
attestationTuple[2] === null) &&
typeof attestationTuple[3] === 'boolean'
) {
interface IChainAttestation extends Codec {
toJSON: () => [string, string, string | null, boolean] | null
}

function decode(
encoded: Option<Tuple> | Tuple,
claimHash: string // all the other decoders do not use extra data; they just return partial types
): Attestation | null {
assertCodecIsType(encoded, [
'Option<(H256,AccountId,Option<H256>,bool)>',
'(H256,AccountId,Option<H256>,bool)',
])
if (encoded instanceof Option || hasNonNullByte(encoded)) {
const attestationTuple = (encoded as IChainAttestation).toJSON()
if (attestationTuple instanceof Array) {
const attestation: IAttestation = {
claimHash,
cTypeHash: attestationTuple[0],
Expand All @@ -62,17 +66,18 @@ function decode(encoded: QueryResult, claimHash: string): Attestation | null {
return null
}

async function queryRaw(claimHash: string): Promise<Codec | null> {
// return types reflect backwards compatibility with mashnet-node v 0.22
async function queryRaw(claimHash: string): Promise<Option<Tuple> | Tuple> {
log.debug(() => `Query chain for attestations with claim hash ${claimHash}`)
const blockchain = await getCached()
const result: QueryResult = await blockchain.api.query.attestation.attestations(
claimHash
)
const result = await blockchain.api.query.attestation.attestations<
Option<Tuple> | Tuple
>(claimHash)
return result
}

export async function query(claimHash: string): Promise<Attestation | null> {
const encoded: QueryResult = await queryRaw(claimHash)
const encoded = await queryRaw(claimHash)
return decode(encoded, claimHash)
}

Expand Down
31 changes: 12 additions & 19 deletions src/attestation/Attestation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Text, Data } from '@polkadot/types'
import { H256 } from '@polkadot/types'
import Bool from '@polkadot/types/primitive/Bool'
import AccountId from '@polkadot/types/primitive/Generic/AccountId'
import { Tuple, Option } from '@polkadot/types/codec'
Expand Down Expand Up @@ -53,13 +53,12 @@ describe('Attestation', () => {

it('stores attestation', async () => {
blockchainApi.query.attestation.attestations.mockReturnValue(
new Option(
Tuple,
new Tuple(
[Data, AccountId, Text, Bool],
[testCType.hash, identityAlice.getAddress(), undefined, false]
)
)
new Option(Tuple.with([H256, AccountId, Option.with(H256), Bool]), [
testCType.hash,
identityAlice.getAddress(),
null,
false,
])
)

const attestation: Attestation = Attestation.fromRequestAndPublicIdentity(
Expand All @@ -71,7 +70,7 @@ describe('Attestation', () => {

it('verify attestations not on chain', async () => {
blockchainApi.query.attestation.attestations.mockReturnValue(
new Option(Tuple)
new Option(Tuple.with([H256, AccountId, Option.with(H256), Bool]))
)

const attestation: Attestation = Attestation.fromAttestation({
Expand All @@ -87,17 +86,11 @@ describe('Attestation', () => {
it('verify attestation revoked', async () => {
blockchainApi.query.attestation.attestations.mockReturnValue(
new Option(
Tuple,
new Tuple(
Tuple.with(
// Attestations: claim-hash -> (ctype-hash, account, delegation-id?, revoked)
[Data, AccountId, Text, Bool],
[
testCType.hash,
identityAlice.signKeyringPair.address,
undefined,
true,
]
)
[H256, AccountId, 'Option<H256>', Bool]
),
[testCType.hash, identityAlice.getAddress(), null, true]
)
)

Expand Down
22 changes: 6 additions & 16 deletions src/blockchain/Blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ import { ApiPromise, SubmittableResult } from '@polkadot/api'
import { SubmittableExtrinsic } from '@polkadot/api/promise/types'
import { Header } from '@polkadot/types/interfaces/types'
import { Codec, AnyJson } from '@polkadot/types/types'
import { Text, u64 } from '@polkadot/types'
import * as gabi from '@kiltprotocol/portablegabi'
import { Text } from '@polkadot/types'
import { ErrorHandler } from '../errorhandling/ErrorHandler'
import { factory as LoggerFactory } from '../config/ConfigLog'
import { ERROR_UNKNOWN, ExtrinsicError } from '../errorhandling/ExtrinsicError'
import Identity from '../identity/Identity'

const log = LoggerFactory.getLogger('Blockchain')

export type QueryResult = Codec | undefined | null

export type Stats = {
chain: string
nodeName: string
Expand All @@ -46,12 +44,9 @@ export interface IBlockchainApi {
// https://polkadot.js.org/api/api/classes/_promise_index_.apipromise.html

export default class Blockchain implements IBlockchainApi {
public static asArray(queryResult: QueryResult): AnyJson[] {
const json =
queryResult && queryResult.encodedLength ? queryResult.toJSON() : null
if (json instanceof Array) {
return json
}
public static asArray(queryResult: Codec): AnyJson[] {
const json = queryResult.toJSON()
if (json instanceof Array) return json
return []
}

Expand Down Expand Up @@ -128,12 +123,7 @@ export default class Blockchain implements IBlockchainApi {
})
}

public async getNonce(accountAddress: string): Promise<Codec> {
const nonce = await this.api.query.system.accountNonce(accountAddress)
if (!nonce) {
throw Error(`Nonce not found for account ${accountAddress}`)
}

return nonce
public async getNonce(accountAddress: string): Promise<u64> {
return this.api.query.system.accountNonce<u64>(accountAddress)
}
}
48 changes: 31 additions & 17 deletions src/blockchainApiConnection/__mocks__/BlockchainApiConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import { Option, Tuple, Vec, H256, u64, u128 } from '@polkadot/types'
import { SubmittableExtrinsic } from '@polkadot/api/promise/types'
import { ExtrinsicStatus } from '@polkadot/types/interfaces'
import AccountId from '@polkadot/types/primitive/Generic/AccountId'
import Bool from '@polkadot/types/primitive/Bool'
import U32 from '@polkadot/types/primitive/U32'

const BlockchainApiConnection = jest.requireActual('../BlockchainApiConnection')

Expand Down Expand Up @@ -207,12 +209,15 @@ const __mocked_api: any = {
*/

// default return value decodes to null, represents attestation not found
attestations: jest.fn(async (claim_hash: string) => new Option(Tuple)),
attestations: jest.fn(
async (claim_hash: string) =>
new Option(Tuple.with([H256, AccountId, 'Option<H256>', Bool]))
),
/* example return value:
new Option(
Tuple,
new Tuple(
[H256, AccountId, 'Option<H256>', Bool],
Tuple.with(
[H256, AccountId, 'Option<H256>', Bool]
),
[
'0x1234', // ctype hash
'5FA9nQDVg267DEd8m1ZypXLBnvN7SFxYwV7ndqSYGiN9TTpu', // Account
Expand All @@ -232,12 +237,15 @@ const __mocked_api: any = {
},
delegation: {
// default return value decodes to null, represents delegation not found
root: jest.fn(async (rootId: string) => new Option(Tuple)),
root: jest.fn(
async (rootId: string) =>
new Option(Tuple.with([H256, AccountId, Bool]))
),
/* example return value:
new Option(
Tuple,
new Tuple(
[H256, AccountId, Bool],
Tuple.with(
[H256, AccountId, Bool]
),
[
'0x1234', // ctype hash
'5FA9nQDVg267DEd8m1ZypXLBnvN7SFxYwV7ndqSYGiN9TTpu', // Account
Expand All @@ -247,12 +255,15 @@ const __mocked_api: any = {
) */

// default return value decodes to null, represents delegation not found
delegations: jest.fn(async (delegationId: string) => new Option(Tuple)),
delegations: jest.fn(
async (delegationId: string) =>
new Option(Tuple.with([H256, 'Option<H256>', AccountId, U32, Bool]))
),
/* example return value:
new Option(
Tuple,
new Tuple(
[H256,'Option<H256>',AccountId,U32,Bool],
Tuple.with(
[H256,'Option<H256>',AccountId,U32,Bool]
),
[
'0x1234', // root-id
null, // parent-id?
Expand All @@ -264,7 +275,7 @@ const __mocked_api: any = {
) */

// default return value decodes to [], represents: no children found
children: jest.fn(async (id: string) => new Vec(H256, [])),
children: jest.fn(async (id: string) => new Vec(H256)),
/* example return value:
new Vec(
H256,
Expand All @@ -274,12 +285,15 @@ const __mocked_api: any = {
},
did: {
// default return value decodes to null, represents dID not found
dIDs: jest.fn(async (address: string) => new Option(Tuple)),
dIDs: jest.fn(
async (address: string) =>
new Option(Tuple.with([H256, H256, 'Option<Bytes>']))
),
/* example return value:
new Option(
Tuple,
new Tuple(
[H256,H256,'Option<Bytes>'],
Tuple.with(
[H256,H256,'Option<Bytes>']
),
[
'publicSigningKey', // publicSigningKey
'publicBoxKey', // publicBoxKey
Expand Down
20 changes: 12 additions & 8 deletions src/ctype/CType.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

import { SubmittableExtrinsic } from '@polkadot/api/promise/types'
import { SubmittableResult } from '@polkadot/api'
import { QueryResult } from '../blockchain/Blockchain'
import { AccountId } from '@polkadot/types/interfaces'
import { Option } from '@polkadot/types'
import { getCached } from '../blockchainApiConnection'
import Identity from '../identity/Identity'
import IPublicIdentity from '../types/PublicIdentity'
import { factory } from '../config/ConfigLog'
import ICType from '../types/CType'
import { assertCodecIsType } from '../util/Decode'

const log = factory.getLogger('CType')

Expand All @@ -24,19 +26,21 @@ export async function store(
return blockchain.submitTx(identity, tx)
}

function decode(encoded: QueryResult): IPublicIdentity['address'] | null {
return encoded && encoded.encodedLength && !encoded.isEmpty
? encoded.toString()
: null
// decoding is backwards compatible with mashnet-node 0.22
export function decode(
encoded: Option<AccountId> | AccountId
): IPublicIdentity['address'] | null {
assertCodecIsType(encoded, ['AccountId', 'Option<AccountId>'])
return !encoded.isEmpty ? encoded.toString() : null
}

export async function getOwner(
ctypeHash: ICType['hash']
): Promise<IPublicIdentity['address'] | null> {
const blockchain = await getCached()
const encoded: QueryResult = await blockchain.api.query.ctype.cTYPEs(
ctypeHash
)
const encoded = await blockchain.api.query.ctype.cTYPEs<
Option<AccountId> | AccountId
>(ctypeHash)
const queriedCTypeAccount = decode(encoded)
return queriedCTypeAccount
}
Loading