Skip to content

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ntn-x2 committed Jan 8, 2025
1 parent 22c3af8 commit 4962634
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
28 changes: 18 additions & 10 deletions packages/chain-helpers/src/blockchain/Blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { SignerOptions } from '@polkadot/api-base/types'
import type { Vec } from '@polkadot/types'
import type { Call, Extrinsic } from '@polkadot/types/interfaces'
import type { AnyNumber, IMethod } from '@polkadot/types/types'
import { type BN } from '@polkadot/util'
import { u8aToHex, type BN } from '@polkadot/util'
import {
type ExtraInfo,
merkleizeMetadata,
Expand All @@ -20,6 +20,7 @@ import {
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- doing this instead of import '@kiltprotocol/augment-api' to avoid creating an import at runtime
import type * as _ from '@kiltprotocol/augment-api'
import type {
HexString,
ISubmittableResult,
KeyringPair,
SubmittableExtrinsic,
Expand Down Expand Up @@ -172,11 +173,13 @@ export async function submitSignedTx(

export const dispatchTx = submitSignedTx

const metadataHashes = new Map<string, Uint8Array>()
const metadataHashes = new Map<string, HexString>()

// Returns the Merkle root of the metadata as stored in the `ConfigService` cache. If not present, it computes it, stores it in the cache for future retrievals, and returns it.
async function getMetadataHash(api: ApiPromise): Promise<Uint8Array> {
const metadata = api.runtimeMetadata.asV15
async function getMetadataHash(api: ApiPromise): Promise<HexString> {
const metadata = await api.call.metadata.metadataAtVersion(15)
// TODO: Find out why using this metadata here fails to decode when calculating the Merkle root.
// const metadata = api.runtimeMetadata.asV15
const { specName, specVersion } = api.runtimeVersion
const genesisHash = await api.genesisHash
const cacheKey = blake2AsHex(
Expand All @@ -187,7 +190,7 @@ async function getMetadataHash(api: ApiPromise): Promise<Uint8Array> {
])
)
if (metadataHashes.has(cacheKey)) {
return metadataHashes.get(cacheKey) as Uint8Array
return metadataHashes.get(cacheKey) as HexString
}
const merkleInfo: ExtraInfo = {
base58Prefix: api.consts.system.ss58Prefix.toNumber(),
Expand All @@ -197,7 +200,7 @@ async function getMetadataHash(api: ApiPromise): Promise<Uint8Array> {
tokenSymbol: api.registry.chainTokens[0],
}
const merkleizedMetadata = merkleizeMetadata(metadata.toHex(), merkleInfo)
const metadataHash = merkleizedMetadata.digest()
const metadataHash = u8aToHex(merkleizedMetadata.digest())
metadataHashes.set(cacheKey, metadataHash)
return metadataHash
}
Expand All @@ -218,7 +221,15 @@ export async function signTx(
{ tip, checkMetadata }: { tip?: AnyNumber; checkMetadata?: boolean } = {}
): Promise<SubmittableExtrinsic> {
const signOptions: Partial<SignerOptions> = checkMetadata
? { tip, metadataHash: await getMetadataHash(ConfigService.get('api')) }
? {
tip,
// Required as described in https://github.com/polkadot-js/api/blob/109d3b2201ea51f27180e34dfd883ec71d402f6b/packages/api-base/src/types/submittable.ts#L79.
metadataHash: await getMetadataHash(ConfigService.get('api')),
// Used by external signers to to know there's additional data to be included in the payload (see link above).
withSignedTransaction: true,
// Forces the tx to fail of the metadata does not match (added for backward compatibility). See https://paritytech.github.io/polkadot-sdk/master/frame_metadata_hash_extension/struct.CheckMetadataHash.html.
mode: 1,
}
: { tip }

if ('address' in signer) {
Expand All @@ -227,9 +238,6 @@ export async function signTx(

return tx.signAsync(signer.id, {
...signOptions,
// Required as described in https://github.com/polkadot-js/api/blob/109d3b2201ea51f27180e34dfd883ec71d402f6b/packages/api-base/src/types/submittable.ts#L79.
withSignedTransaction: true,
mode: 1,
signer: Signers.getPolkadotSigner([signer]),
})
}
Expand Down
26 changes: 25 additions & 1 deletion tests/integration/Blockchain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ import {
import type { KeyringPair } from '@kiltprotocol/types'

import { makeSigningKeyTool } from '../testUtils/index.js'
import { devCharlie, devFaucet, initializeApi, submitTx } from './utils.js'
import {
devAlice,
devCharlie,
devFaucet,
initializeApi,
submitTx,
} from './utils.js'

let api: ApiPromise
beforeAll(async () => {
Expand Down Expand Up @@ -153,6 +159,24 @@ describe('Chain returns specific errors, that we check for', () => {
}, 40000)
})

describe('The added `SignedExtension`s are valid', () => {
it(`'CheckMetadataHash' works`, async () => {
const systemRemarkTx = api.tx.system.remark('Test remark')
const submitPromise = Blockchain.signAndSubmitTx(systemRemarkTx, devAlice, {
checkMetadata: true,
})
await expect(submitPromise).resolves.not.toThrow()
})

it(`No 'CheckMetadataHash' works`, async () => {
const systemRemarkTx = api.tx.system.remark('Test remark')
const submitPromise = Blockchain.signAndSubmitTx(systemRemarkTx, devAlice, {
checkMetadata: false,
})
await expect(submitPromise).resolves.not.toThrow()
})
})

afterAll(async () => {
if (typeof api !== 'undefined') await disconnect()
})

0 comments on commit 4962634

Please sign in to comment.