diff --git a/e2e/__snapshots__/dev.test.ts.snap b/e2e/__snapshots__/dev.test.ts.snap new file mode 100644 index 00000000..dc977874 --- /dev/null +++ b/e2e/__snapshots__/dev.test.ts.snap @@ -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, +} +`; diff --git a/e2e/author.test.ts b/e2e/author.test.ts index 5bd0cf9f..0f882fc3 100644 --- a/e2e/author.test.ts +++ b/e2e/author.test.ts @@ -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) diff --git a/e2e/dev.test.ts b/e2e/dev.test.ts new file mode 100644 index 00000000..920621ac --- /dev/null +++ b/e2e/dev.test.ts @@ -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() + }) +}) diff --git a/e2e/helper.ts b/e2e/helper.ts index 51571388..cf2fa7b0 100644 --- a/e2e/helper.ts +++ b/e2e/helper.ts @@ -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' @@ -92,9 +93,12 @@ export const expectHex = (codec: CodecOrArray | Promise) => { } export const dev = { - newBlock: () => { + newBlock: (): Promise => { return ws.send('dev_newBlock', []) }, + setStorages: (values: Record, blockHash?: string) => { + return ws.send('dev_setStorages', [values, blockHash]) + }, } function defer() { @@ -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, + } +} diff --git a/src/blockchain/index.ts b/src/blockchain/index.ts index bb641185..b5b53e39 100644 --- a/src/blockchain/index.ts +++ b/src/blockchain/index.ts @@ -66,7 +66,10 @@ export class Blockchain { return this.#blocksByNumber[number] } - async getBlock(hash: string = this.head.hash): Promise { + async getBlock(hash?: string): Promise { + if (hash == null) { + hash = this.head.hash + } if (!this.#blocksByHash[hash]) { try { const header = await this.#api.rpc.chain.getHeader(hash) diff --git a/src/rpc/dev.ts b/src/rpc/dev.ts index a053274d..df5fc19d 100644 --- a/src/rpc/dev.ts +++ b/src/rpc/dev.ts @@ -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 },