Skip to content

Commit

Permalink
chore: add tests for Peregrine (#3)
Browse files Browse the repository at this point in the history
* Update zombienet/cli

* Fixes

* Last fixes

* Last fixes

* Lint fixes
  • Loading branch information
ntn-x2 authored Dec 19, 2023
1 parent 089bda9 commit cd6edc0
Show file tree
Hide file tree
Showing 14 changed files with 627 additions and 300 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,3 @@ cjs
# VSCode

.vscode

# Tests
.env.*
2 changes: 2 additions & 0 deletions .yarn/versions/f6e39a6a.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declined:
- "@kiltprotocol/dip-sdk"
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,26 @@ The end-to-end testing use a Zombienet-based setup with a Kubernetes provider.
Hence, a Kubernetes cluster, e.g., [minikube](https://minikube.sigs.k8s.io/docs/start/) must be installed on the machine where the tests are to be executed.
For more information on how to set up the machine to spawn Zombienet-based network, please refer to the [official Zombienet repository](https://github.com/paritytech/zombienet).

### Environment configuration

The Zombienet deployment relies on a number of environment variables, which are:

- `RELAY_IMAGE`: The Docker image for relaychain nodes.
- `RELAY_ALICE_RPC`: The RPC port for the `relay-alice` relaychain node.
- `PROVIDER_IMAGE`: The Docker image for the DIP provider nodes.
- `PROVIDER_ALICE_RPC`: The RPC port for the `provider-alice` provider node.
- `CONSUMER_IMAGE`: The Docker image for the DIP consumer nodes.
- `CONSUMER_ALICE_RPC`: The RPC port for the `consumer-alice` consumer node.

A series of default values is sourced from either the `tests/dip-provider-template-dip-consumer-template/.env.develop.test` or `tests/peregrine-dip-consumer-template/.env.develop.test` file to spin up either a DIP Template Provider <-> DIP Template Consumer network or a Peregrine <-> DIP Template Consumer network.

### Test execution

Test execution requires the following steps:

1. Switch to the expected Node version with `nvm use`
2. Install the repo dependencies with `yarn install`
3. Spin up the Zombienet network with `yarn test:e2e:deploy`.
4. Once the network deployment is complete, create a `.env.test` file with `RELAY_ADDRESS`, `PROVIDER_ADDRESS`, and `CONSUMER_ADDRESS` values pointing to the started nodes for relaychain, provider chain and consumer chain respectively.
5. Run the end-to-end tests with `yarn test:e2e`.
1. Switch to the expected Node version with `nvm use`.
2. Install the repo dependencies with `yarn install`.
3. Set up the environment variables as explained above.
4. Spin up the Zombienet network with `test:e2e:start-network:peregrine-provider:develop` or `test:e2e:start-network:dip-template-provider:develop`.
5. In a new shell session, run the end-to-end tests with `yarn test:e2e:peregrine-provider` or `yarn test:e2e:dip-template-provider`.
6. [OPTIONAL] Tear down the network by killing the process started at step 4.
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
"bugs": "https://github.com/KILTprotocol/dip-sdk/issues",
"description": "An SDK to help integration of the KILT Decentralized Identity Provider (DIP) protocol using KILT as an Identity Provider.",
"dependencies": {
"@kiltprotocol/did": "0.34.0-dip-2",
"@kiltprotocol/types": "0.34.0-dip-2",
"@kiltprotocol/did": "0.34.1-rc.1",
"@kiltprotocol/types": "0.34.1-rc.1",
"@polkadot/api": "^10.10.1",
"@polkadot/util": "^12.5.1"
},
"devDependencies": {
"@kiltprotocol/sdk-js": "0.34.0-dip-2",
"@kiltprotocol/sdk-js": "0.34.1-rc.1",
"@types/node": "^20.9.4",
"@typescript-eslint/eslint-plugin": "^6.2.0",
"@typescript-eslint/parser": "^6.2.0",
"@zombienet/cli": "^1.3.82",
"@zombienet/cli": "^1.3.85",
"bn.js": "^5.2.1",
"dotenv": "^16.3.1",
"eslint": "^8.45.0",
Expand Down Expand Up @@ -52,8 +52,10 @@
"lint:fix": "prettier -w . && eslint --fix --ext .ts .",
"prepublish": "yarn exec cp -f ../../LICENSE .",
"publish": "yarn npm publish --access=public",
"test:e2e": "yarn build && vitest run tests",
"test:e2e:deploy": "yarn zombienet --spawn-concurrency 3 spawn tests/zombienet/network.toml"
"test:e2e:peregrine-provider": "yarn build && vitest run tests/peregrine-dip-consumer-template",
"test:e2e:dip-template-provider": "yarn build && vitest run tests/dip-provider-template-dip-consumer-template",
"test:e2e:start-network:peregrine-provider:develop": "env $(cat tests/peregrine-dip-consumer-template/.env.develop.test | xargs) yarn zombienet spawn tests/peregrine-dip-consumer-template/develop-zombienet.toml",
"test:e2e:start-network:dip-template-provider:develop": "env $(cat tests/dip-provider-template-dip-consumer-template/.env.develop.test | xargs) yarn zombienet spawn tests/dip-provider-template-dip-consumer-template/develop-zombienet.toml"
},
"stableVersion": "0.0.0",
"type": "module",
Expand Down
7 changes: 4 additions & 3 deletions src/sibling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
SubmittableExtrinsic,
DidKey,
SignExtrinsicCallback,
BN,
} from "@kiltprotocol/types"
import type { KeyringPair } from "@polkadot/keyring/types"
import type { Call, Hash } from "@polkadot/types/interfaces"
Expand All @@ -40,9 +41,9 @@ export type DipSiblingProofInput = {
submitterAddress: KeyringPair["address"]
keyRelationship: VerificationKeyRelationship
// Optional, retrieved from chain otherwise
blockHeight?: number
blockHeight?: BN
genesisHash?: Hash
providerBlockHeight?: number
providerBlockHeight?: BN
// With defaults
accountIdRuntimeType?: string
blockNumberRuntimeType?: string
Expand Down Expand Up @@ -109,7 +110,7 @@ export async function generateDipAuthorizedTxForSibling({

// Proof of commitment must be generated with the state root at the block before the last one finalized.
const dipRootProofBlockHash = await providerApi.rpc.chain.getBlockHash(
providerStateRootProofProviderBlockHeight - 1,
providerStateRootProofProviderBlockHeight.subn(1),
)

const {
Expand Down
17 changes: 9 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
DidKey,
VerificationKeyRelationship,
VerificationKeyType,
BN,
} from "@kiltprotocol/types"
import type { ApiPromise } from "@polkadot/api"
import type { KeyringPair } from "@polkadot/keyring/types"
Expand All @@ -33,12 +34,12 @@ type ProviderStateRootProofOpts = {
providerApi: ApiPromise
relayApi: ApiPromise
// Optional
providerBlockHeight?: number
providerBlockHeight?: BN
}
type ProviderStateRootProofRes = {
proof: ReadProof
providerBlockHeight: number
relayBlockHeight: number
providerBlockHeight: BN
relayBlockHeight: BN
}
/**
* Generate a state proof that proofs the head of the specified parachain.
Expand Down Expand Up @@ -66,7 +67,7 @@ export async function generateProviderStateRootProof({
await providerApi.rpc.chain.getFinalizedHead()
const providerLastFinalizedBlockHeight = await providerApi.rpc.chain
.getHeader(providerLastFinalizedBlockHash)
.then((h) => h.number.toNumber())
.then((h) => h.number.toBn())
return [providerLastFinalizedBlockHeight, providerLastFinalizedBlockHash]
})()
const providerApiAtBlock = await providerApi.at(providerBlockHash)
Expand Down Expand Up @@ -199,15 +200,15 @@ type DipDidSignatureConsumerOpts = {
identityDetailsRuntimeType: string
submitterAddress: KeyringPair["address"]
// Optional
blockHeight?: number
blockHeight?: BN
genesisHash?: Hash
}
type DipDidSignatureOpts = {
consumer: DipDidSignatureConsumerOpts
provider: DipDidSignatureProviderOpts
}
type DipDidSignatureRes = {
blockNumber: number
blockNumber: BN
signature: Uint8Array
type: VerificationKeyType
}
Expand Down Expand Up @@ -244,8 +245,8 @@ export async function generateDipDidSignature({
genesisHash,
},
}: DipDidSignatureOpts): Promise<DipDidSignatureRes> {
const blockNumber: number =
blockHeight ?? (await api.query.system.number<any>()).toNumber()
const blockNumber: BN =
blockHeight ?? (await api.query.system.number<any>()).toBn()
const genesis = genesisHash ?? (await api.query.system.blockHash(0))
const identityDetails = (
await api.query.dipConsumer.identityEntries<Option<Codec>>(toChain(didUri))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
RELAY_IMAGE=parity/polkadot:v1.0.0
RELAY_ALICE_RPC=20001
PROVIDER_IMAGE=kiltprotocol/dip-provider-node-template:latest-develop
PROVIDER_ALICE_RPC=20011
CONSUMER_IMAGE=kiltprotocol/dip-consumer-node-template:latest-develop
CONSUMER_ALICE_RPC=20021
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[settings]
enable_tracing = false
provider = "kubernetes"
# 18000 seconds -> 300 minutes -> 5 hours
timeout = 18000

# Env variables:
# * RELAY_IMAGE: Docker image for relaychain nodes
# * RELAY_ALICE_RPC: RPC port for the `relay-alice` relaychain node
# * PROVIDER_IMAGE: Docker image for the DIP provider nodes
# * PROVIDER_ALICE_RPC: RPC port for the `provider-alice` provider node
# * CONSUMER_IMAGE: Docker image for the DIP consumer nodes
# * CONSUMER_ALICE_RPC: RPC port for the `consumer-alice` consumer node

[relaychain]
chain = "rococo-local"
default_image = "{{RELAY_IMAGE}}"
default_substrate_cli_args_version = 1

[[relaychain.nodes]]
name = "relay-alice"
rpc_port = "{{RELAY_ALICE_RPC}}"

[[relaychain.nodes]]
name = "relay-bob"

[[relaychain.nodes]]
name = "relay-charlie"

[[parachains]]
id = 2000

[parachains.collator]
command = "node-executable"
name = "provider-alice"
image = "{{PROVIDER_IMAGE}}"
rpc_port = "{{PROVIDER_ALICE_RPC}}"

[[parachains]]
id = 2001

[parachains.collator]
command = "node-executable"
name = "consumer-alice"
image = "{{CONSUMER_IMAGE}}"
rpc_port = "{{CONSUMER_ALICE_RPC}}"
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@ import { setTimeout } from "timers/promises"

import * as Kilt from "@kiltprotocol/sdk-js"
import { ApiPromise, WsProvider } from "@polkadot/api"
import { BN } from "@polkadot/util"
import { blake2AsHex } from "@polkadot/util-crypto"
import { BN } from "bn.js"
import dotenv from "dotenv"
import { beforeAll, describe, it, expect } from "vitest"

import {
createProviderApi,
signAndSubmitTx,
withCrossModuleSystemImport,
} from "./utils.js"

import type { GetStoreTxSignCallback, Web3Name } from "@kiltprotocol/did"
import type { DipSiblingProofInput } from "@kiltprotocol/dip-sdk"
import type {
Expand All @@ -31,7 +25,15 @@ import type { Option } from "@polkadot/types/codec"
import type { Call } from "@polkadot/types/interfaces"
import type { Codec } from "@polkadot/types/types"

dotenv.config({ path: ".env.test" })
import {
createProviderApi,
signAndSubmitTx,
withCrossModuleSystemImport,
} from "../utils.js"

dotenv.config({
path: "tests/dip-provider-template-dip-consumer-template/.env.develop.test",
})

const baseConfig: Pick<
DipSiblingProofInput,
Expand All @@ -40,7 +42,7 @@ const baseConfig: Pick<
| "identityDetailsRuntimeType"
> = {
accountIdRuntimeType: "AccountId32",
blockNumberRuntimeType: "u32",
blockNumberRuntimeType: "u64",
identityDetailsRuntimeType: "Option<u128>",
}
const web3NameRuntimeType = "Text"
Expand All @@ -52,9 +54,9 @@ const providerAndConsumerSudoKeypair = keyring.addFromUri("//Alice")

Kilt.ConfigService.set({ submitTxResolveOn: Kilt.Blockchain.IS_IN_BLOCK })

const relayAddress = process.env["RELAY_ADDRESS"] as string
const providerAddress = process.env["PROVIDER_ADDRESS"] as string
const consumerAddress = process.env["CONSUMER_ADDRESS"] as string
const relayAddress = `ws://127.0.0.1:${process.env["RELAY_ALICE_RPC"]}`
const providerAddress = `ws://127.0.0.1:${process.env["PROVIDER_ALICE_RPC"]}`
const consumerAddress = `ws://127.0.0.1:${process.env["CONSUMER_ALICE_RPC"]}`

describe("V0", () => {
// beforeAll
Expand Down Expand Up @@ -87,7 +89,7 @@ describe("V0", () => {
let did: DidDocument
let web3Name: Web3Name
let didKeypair: Kilt.KeyringPair
let lastTestSetupProviderBlockNumber: number
let lastTestSetupProviderBlockNumber: BN
let testConfig: typeof v0Config &
Pick<
DipSiblingProofInput,
Expand All @@ -104,13 +106,15 @@ describe("V0", () => {
const newSubmitterKeypair = keyring.addFromMnemonic(
Kilt.Utils.Crypto.mnemonicGenerate(),
)
const providerUnit = "0".repeat(13)
const consumerUnit = "0".repeat(13)
const balanceTransferTxOnProviderChain = providerApi.tx.balances.transfer(
newSubmitterKeypair.address,
10 ** 15,
`1${providerUnit}`,
)
const balanceTransferTxOnConsumerChain = consumerApi.tx.balances.transfer(
newSubmitterKeypair.address,
10 ** 15,
`1${consumerUnit}`,
)
await Promise.all([
Kilt.Blockchain.signAndSubmitTx(
Expand Down Expand Up @@ -168,7 +172,7 @@ describe("V0", () => {
await setTimeout(12_000)
lastTestSetupProviderBlockNumber = (
await providerApi.query.system.number()
).toNumber()
).toBn()
const newFullDid = (await Kilt.Did.resolve(newFullDidUri))
?.document as DidDocument
submitterKeypair = newSubmitterKeypair
Expand Down Expand Up @@ -218,11 +222,12 @@ describe("V0", () => {
// The example PostIt pallet generates the storage key for a post by hashing (block number, submitter's username, content of the post).
const postKey = blake2AsHex(
consumerApi
.createType(`(BlockNumber, ${web3NameRuntimeType}, Bytes)`, [
blockNumber,
web3Name,
postText,
])
.createType(
`(${
config.blockNumberRuntimeType as string
}, ${web3NameRuntimeType}, Bytes)`,
[blockNumber, web3Name, postText],
)
.toHex(),
)
const postEntry =
Expand Down Expand Up @@ -260,11 +265,12 @@ describe("V0", () => {
// The example PostIt pallet generates the storage key for a post by hashing (block number, submitter's username, content of the post).
const postKey = blake2AsHex(
consumerApi
.createType(`(BlockNumber, ${web3NameRuntimeType}, Bytes)`, [
blockNumber,
web3Name,
postText,
])
.createType(
`(${
config.blockNumberRuntimeType as string
}, ${web3NameRuntimeType}, Bytes)`,
[blockNumber, web3Name, postText],
)
.toHex(),
)
const postEntry =
Expand Down
6 changes: 6 additions & 0 deletions tests/peregrine-dip-consumer-template/.env.develop.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
RELAY_IMAGE=parity/polkadot:v1.0.0
RELAY_ALICE_RPC=10001
PROVIDER_IMAGE=kiltprotocol/kilt-node:latest-develop
PROVIDER_ALICE_RPC=10011
CONSUMER_IMAGE=kiltprotocol/dip-consumer-node-template:latest-develop
CONSUMER_ALICE_RPC=10021
Loading

0 comments on commit cd6edc0

Please sign in to comment.