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

set storages #31

Merged
merged 1 commit into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
35 changes: 35 additions & 0 deletions e2e/__snapshots__/dev.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Vitest Snapshot v1

exports[`dev rpc > setStroages 1`] = `"5F98oWfz2r5rcRVnP9VCndg33DAAsky3iuoBSpaPUbgN9AJn"`;

exports[`dev rpc > setStroages 2`] = `"5FA9nQDVg267DEd8m1ZypXLBnvN7SFxYwV7ndqSYGiN9TTpu"`;

exports[`dev rpc > setStroages 3`] = `
{
"consumers": 0,
"data": {
"feeFrozen": 0,
"free": 1000000000000,
"miscFrozen": 0,
"reserved": 0,
},
"nonce": 0,
"providers": 1,
"sufficients": 0,
}
`;

exports[`dev rpc > setStroages 4`] = `
{
"consumers": 0,
"data": {
"feeFrozen": 0,
"free": 0,
"miscFrozen": 0,
"reserved": 0,
},
"nonce": 0,
"providers": 0,
"sufficients": 0,
}
`;
8 changes: 2 additions & 6 deletions e2e/author.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { Keyring } from '@polkadot/keyring'
import { describe, expect, it } from 'vitest'

import { api, dev, expectJson, mockCallback } from './helper'
import { api, dev, expectJson, mockCallback, testingPairs } from './helper'

describe('author rpc', () => {
it('works', async () => {
const keyring = new Keyring({ type: 'ed25519' }) // cannot use sr25519 as it is non determinstic
const alice = keyring.addFromUri('//Alice')
const bob = keyring.addFromUri('//Bob')
const { alice, bob } = testingPairs()

console.log(alice.address, bob.address)
{
const { callback, next } = mockCallback()
await api.tx.balances.transfer(bob.address, 100).signAndSend(alice, callback)
Expand Down
32 changes: 32 additions & 0 deletions e2e/dev.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, it } from 'vitest'
import { u8aToHex } from '@polkadot/util'

import { api, dev, expectJson, testingPairs } from './helper'

describe('dev rpc', () => {
it('setStroages', async () => {
const { alice, test1 } = testingPairs()

await expectJson(api.query.sudo.key()).toMatchSnapshot()

await dev.setStorages({
[api.query.sudo.key.key()]: u8aToHex(alice.addressRaw),
})

await expectJson(api.query.sudo.key()).toMatchSnapshot()

await api.tx.sudo.sudo(api.tx.balances.setBalance(test1.address, 1000000000000, 0)).signAndSend(alice)
const hash = await dev.newBlock()

await expectJson(api.query.system.account(test1.address)).toMatchSnapshot()

await dev.setStorages(
{
[api.query.system.account.key(test1.address)]: null,
},
hash
)

await expectJson(api.query.system.account(test1.address)).toMatchSnapshot()
})
})
27 changes: 26 additions & 1 deletion e2e/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiPromise, WsProvider } from '@polkadot/api'
import { Codec } from '@polkadot/types/types'
import { Keyring } from '@polkadot/keyring'
import { beforeAll, beforeEach, expect, vi } from 'vitest'

import { Blockchain } from '../src/blockchain'
Expand Down Expand Up @@ -92,9 +93,12 @@ export const expectHex = (codec: CodecOrArray | Promise<CodecOrArray>) => {
}

export const dev = {
newBlock: () => {
newBlock: (): Promise<string> => {
return ws.send('dev_newBlock', [])
},
setStorages: (values: Record<string, string | null>, blockHash?: string) => {
return ws.send('dev_setStorages', [values, blockHash])
},
}

function defer<T>() {
Expand Down Expand Up @@ -122,3 +126,24 @@ export const mockCallback = () => {
}

export const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

export const testingPairs = () => {
const keyring = new Keyring({ type: 'ed25519' }) // cannot use sr25519 as it is non determinstic
const alice = keyring.addFromUri('//Alice')
const bob = keyring.addFromUri('//Bob')
const charlie = keyring.addFromUri('//Charlie')
const dave = keyring.addFromUri('//Dave')
const eve = keyring.addFromUri('//Eve')
const test1 = keyring.addFromUri('//test1')
const test2 = keyring.addFromUri('//test2')
return {
alice,
bob,
charlie,
dave,
eve,
test1,
test2,
keyring,
}
}
5 changes: 4 additions & 1 deletion src/blockchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ export class Blockchain {
return this.#blocksByNumber[number]
}

async getBlock(hash: string = this.head.hash): Promise<Block | undefined> {
async getBlock(hash?: string): Promise<Block | undefined> {
if (hash == null) {
hash = this.head.hash
}
if (!this.#blocksByHash[hash]) {
try {
const header = await this.#api.rpc.chain.getHeader(hash)
Expand Down
9 changes: 8 additions & 1 deletion src/rpc/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ const handlers: Handlers = {
logger.debug({ hash: block.hash }, 'dev_newBlock')
return block.hash
},
dev_setStroages: async (context, [values, blockHash]) => {
dev_setStorages: async (context, [values, blockHash]) => {
const block = await context.chain.getBlock(blockHash)
if (!block) {
throw new ResponseError(1, `Block ${blockHash} not found`)
}
logger.debug(
{
hash: block.hash,
values,
},
'dev_setStorages'
)
block.pushStorageLayer().setAll(values)
return block.hash
},
Expand Down