Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

feat: add custom chain spec #725

Merged
merged 21 commits into from
Mar 13, 2023
Merged

feat: add custom chain spec #725

merged 21 commits into from
Mar 13, 2023

Conversation

kratico
Copy link
Contributor

@kratico kratico commented Mar 8, 2023

Resolves #515

Update PolkadotDevProvider/ContractsDevProvider providers with a custom chain spec and add the /user_i endpoints

The custom chain spec add test accounts with an initial balance.

How to test?

Run the following example to validate that a test account has an initial balance.

import { System, users } from "polkadot_dev/mod.ts"

const [user0, _user1, _user2] = await users(3)

const result = await System.Account.entry([user0.publicKey]).run()

console.log(result)

To test the API

curl -X post http://localhost:4646/frame/dev/polkadot/@latest/user_i -d '{"count":2}'     
{"index":4}

curl -X post http://localhost:4646/frame/contracts_dev/@latest/user_i -d '{"count":2}'
{"index":2}

@kratico
Copy link
Contributor Author

kratico commented Mar 8, 2023

@harrysolovay @tjjfvi

I explored how to get unique tests users doing something like

import users from http://localhost:4646/frame/dev/polkadot/@v0.9.37/test_users.ts

But, this is a bit challenging given that

  • deno caches the imports
  • in the CAPI server, the polkadot instance is shared by all imports to the same provider

There are a few ways to bypass the deno cache, for example we can tweak the CAPI server to respond differently based on

  • authorization header, DENO_AUTH_TOKENS=a1b2c3d4e5f6@localhost:4646 deno run script.ts, but it will need a different token on every run
  • import {} from "http://localhost:4646/frame/dev/polkadot/@v0.9.37/test_users.ts?cacheId=1234, but it will need a different query string on every import

@kratico kratico marked this pull request as ready for review March 8, 2023 18:48
@kratico kratico force-pushed the feat/custom-chain-spec branch from 5e23a1a to 96c726c Compare March 8, 2023 18:49
@tjjfvi
Copy link
Contributor

tjjfvi commented Mar 8, 2023

Yeah, imports probably won't work well.

You'll want to add logic to the provider itself (override handle) to handle a request to a filePath of e.g. /testUsers?count=5 and return the relevant user ids.

Then, you'll want to add a custom function to the codegen to call this route. The easiest way will be to override chainFile to add a function like

type ArrayOfLength<
  T,
  L extends number,
  A extends T[] = [],
> = number extends L ? T[]
  : L extends A["length"] ? A
  : ArrayOfLength<T, L, [...A, T]>

export async function getTestUsers<N extends number>(count: N): Promise<ArrayOfLength<C.Sr25519, N>>
export async function getTestUsers(count: number): Promise<C.Sr25519[]> {
  const userIds: number[] = await fetch(`.../testUsers?count=${count}`).then(r => r.json())
  return userIds.map(C.testUser)
}

Then, it can be used like

import { getTestUsers } from "polkadot_dev"

const [alex, bill, carol] = await getTestUsers(3)

@kratico
Copy link
Contributor Author

kratico commented Mar 8, 2023

@tjjfvi I've just spoke with @harrysolovay and he suggested a similar approach.

Thank you both

@kratico kratico marked this pull request as draft March 8, 2023 20:35
@harrysolovay
Copy link
Contributor

harrysolovay commented Mar 8, 2023

Should we model this as a Rune factory instead?

import { System, user } from "polkadot_dev/mod.ts"

const alice = user()

const result = await System.Account.value(alice.publicKey).run()

console.log(result)

And let's also make sure to include an ExceededMaximumTestUsersError... although I doubt anyone will exceed 1000. Is there much initialization overhead? Could we do 10000? 100000? ...

@tjjfvi
Copy link
Contributor

tjjfvi commented Mar 8, 2023

We need it to persist state between runs, so it wouldn't work well as a rune at the moment.

@harrysolovay
Copy link
Contributor

harrysolovay commented Mar 8, 2023

Ah, I see. Good point.

@tjjfvi tjjfvi mentioned this pull request Mar 9, 2023
@harrysolovay harrysolovay mentioned this pull request Mar 9, 2023
@kratico kratico force-pushed the feat/custom-chain-spec branch from 6022a28 to 4d177de Compare March 9, 2023 18:39
@kratico kratico marked this pull request as ready for review March 9, 2023 18:47
@kratico
Copy link
Contributor Author

kratico commented Mar 9, 2023

@tjjfvi @harrysolovay Would you give it a quick pass to validate the provider override handle and override chainFile implementations?

I'd like to get a quick feedback before refactoring them for the ContractsDevProvider

providers/frame/polkadot_dev.ts Outdated Show resolved Hide resolved
providers/frame/polkadot_dev.ts Outdated Show resolved Hide resolved
providers/frame/polkadot_dev.ts Outdated Show resolved Hide resolved
providers/frame/polkadot_dev.ts Outdated Show resolved Hide resolved
providers/frame/polkadot_dev.ts Outdated Show resolved Hide resolved
providers/frame/utils/createCustomChainSpec.ts Outdated Show resolved Hide resolved
providers/frame/utils/createCustomChainSpec.ts Outdated Show resolved Hide resolved
providers/frame/utils/createCustomChainSpec.ts Outdated Show resolved Hide resolved
providers/frame/utils/mod.ts Outdated Show resolved Hide resolved
providers/frame/polkadot_dev.ts Outdated Show resolved Hide resolved
providers/frame/polkadot_dev.ts Outdated Show resolved Hide resolved
providers/frame/polkadot_dev.ts Outdated Show resolved Hide resolved
providers/frame/polkadot_dev.ts Outdated Show resolved Hide resolved
providers/frame/polkadot_dev.ts Outdated Show resolved Hide resolved
providers/frame/polkadot_dev.ts Outdated Show resolved Hide resolved
providers/frame/polkadot_dev.ts Outdated Show resolved Hide resolved
providers/frame/polkadot_dev.ts Outdated Show resolved Hide resolved
@kratico kratico requested review from harrysolovay and tjjfvi March 10, 2023 15:35
@kratico
Copy link
Contributor Author

kratico commented Mar 10, 2023

@tjjfvi @harrysolovay Shall I move these .handle and .chainFile overrides to the FrameBinProvider?

That way the ContractsDevProvider will have the test user API too

Thoughts?

@kratico
Copy link
Contributor Author

kratico commented Mar 10, 2023

@harrysolovay @tjjfvi I just realized that the ProjectProvider and the ZombienetProvider also extends from FrameBinProvider

So adding the overrides to .chainFile and .handle will work only for substrate based nodes because the assumption is that using the binary the chain spec could be generated with binary build-spec ....

Maybe I need to change the design by extracting the user testing stuff in a new class and then use composition instead of inheritance.

Thoughts?

@tjjfvi
Copy link
Contributor

tjjfvi commented Mar 10, 2023

Perhaps we should just merge the dev and contracts providers

@kratico
Copy link
Contributor Author

kratico commented Mar 10, 2023

Perhaps we should just merge the dev and contracts providers

I've just meet with Harry and decided to add a common.ts module and extract createCustomChainSpec and fns related to handling test users.

@kratico kratico force-pushed the feat/custom-chain-spec branch from f4e7b6d to c140f6b Compare March 10, 2023 18:24
crypto/test_pairs.ts Outdated Show resolved Hide resolved
crypto/test_pairs.ts Show resolved Hide resolved
tjjfvi
tjjfvi previously approved these changes Mar 13, 2023
let index = cache.count
const newCount = index + count
if (newCount < DEFAULT_TEST_USER_COUNT) cache.count = newCount
else index = -1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last thing: per @tjjfvi's recommendation, let's actually return an error response here. Then we can delete the if (index === -1) check in the client users fn.

@harrysolovay harrysolovay merged commit d39d76a into main Mar 13, 2023
@harrysolovay harrysolovay deleted the feat/custom-chain-spec branch March 13, 2023 20:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add server api to get unique test users
3 participants