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

accountNextIndex with pending tx #224

Merged
merged 2 commits into from
Mar 7, 2023
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
30 changes: 26 additions & 4 deletions e2e/src/system.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'

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

setupApi(env.mandala)

Expand All @@ -15,8 +15,30 @@ describe('system rpc', () => {
isSyncing: false,
shouldHavePeers: false,
})
expect((await api.rpc.system.accountNextIndex('5EYCAe5fiQJsoMt16QywpBQwyn5cgfkXFFJApwwKPXFoF2h7')).toNumber()).toBe(
0
)
})

it('get correct account next index', async () => {
const { alice } = testingPairs()

const nonce = async (address: string) => (await api.query.system.account(address)).nonce.toNumber()

const accountNextIndex = async (address: string) => (await api.rpc.system.accountNextIndex(address)).toNumber()

// send tx
await api.tx.balances.transfer(alice.address, 0).signAndSend(alice)

expect(await nonce(alice.address)).toBe(0)
expect(await accountNextIndex(alice.address)).toBe(1)

await dev.newBlock()

expect(await nonce(alice.address)).toBe(1)
expect(await accountNextIndex(alice.address)).toBe(1)

// send another tx
await api.tx.balances.transfer(alice.address, 0).signAndSend(alice)

expect(await nonce(alice.address)).toBe(1)
expect(await accountNextIndex(alice.address)).toBe(2)
})
})
2 changes: 1 addition & 1 deletion packages/chopsticks/src/blockchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class Blockchain {
const registry = await this.head.registry
const validity: TransactionValidity = registry.createType('TransactionValidity', res.result)
if (validity.isOk) {
this.#txpool.submitExtrinsic(extrinsic)
await this.#txpool.submitExtrinsic(extrinsic)
return blake2AsHex(extrinsic, 256)
}
throw validity.asErr
Expand Down
25 changes: 19 additions & 6 deletions packages/chopsticks/src/blockchain/txpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import _ from 'lodash'

import { Blockchain } from '.'
import { Deferred, defer } from '../utils'
import { GenericExtrinsic } from '@polkadot/types'
import { InherentProvider } from './inherent'
import { buildBlock } from './block-builder'

Expand Down Expand Up @@ -35,7 +36,7 @@ export interface BuildBlockParams {
export class TxPool {
readonly #chain: Blockchain

readonly #pool: HexString[] = []
readonly #pool: { extrinsic: HexString; signer: string }[] = []
readonly #ump: Record<number, HexString[]> = {}
readonly #dmp: DownwardMessage[] = []
readonly #hrmp: Record<number, HorizontalMessage[]> = {}
Expand All @@ -55,15 +56,25 @@ export class TxPool {
}

get pendingExtrinsics(): HexString[] {
return this.#pool
return this.#pool.map(({ extrinsic }) => extrinsic)
}

submitExtrinsic(extrinsic: HexString) {
this.#pool.push(extrinsic)
pendingExtrinsicsBy(address: string): HexString[] {
return this.#pool.filter(({ signer }) => signer === address).map(({ extrinsic }) => extrinsic)
}

async submitExtrinsic(extrinsic: HexString) {
this.#pool.push({ extrinsic, signer: await this.#getSigner(extrinsic) })

this.#maybeBuildBlock()
}

async #getSigner(extrinsic: HexString) {
const registry = await this.#chain.head.registry
const tx = registry.createType<GenericExtrinsic>('GenericExtrinsic', extrinsic)
return tx.signer.toString()
}

submitUpwardMessages(id: number, ump: HexString[]) {
if (!this.#ump[id]) {
this.#ump[id] = []
Expand Down Expand Up @@ -114,7 +125,7 @@ export class TxPool {
}

async buildBlock(params?: Partial<BuildBlockParams>) {
const transactions = params?.transactions || this.#pool.splice(0)
const transactions = params?.transactions || this.#pool.splice(0).map(({ extrinsic }) => extrinsic)
const upwardMessages = params?.upwardMessages || { ...this.#ump }
const downwardMessages = params?.downwardMessages || this.#dmp.splice(0)
const horizontalMessages = params?.horizontalMessages || { ...this.#hrmp }
Expand Down Expand Up @@ -177,7 +188,9 @@ export class TxPool {
this.event.emit(APPLY_EXTRINSIC_ERROR, [extrinsic, error])
}
)
this.#pool.push(...pendingExtrinsics)
for (const extrinsic of pendingExtrinsics) {
this.#pool.push({ extrinsic, signer: await this.#getSigner(extrinsic) })
}
await this.#chain.setHead(newBlock)

this.#pendingBlocks.shift()
Expand Down
3 changes: 2 additions & 1 deletion packages/chopsticks/src/rpc/substrate/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ const handlers: Handlers = {
const registry = await head.registry
const account = registry.createType('AccountId', address)
const result = await head.call('AccountNonceApi_account_nonce', [account.toHex()])
return registry.createType<Index>('Index', hexToU8a(result.result)).toNumber()
const nonce = registry.createType<Index>('Index', hexToU8a(result.result)).toNumber()
return nonce + context.chain.txPool.pendingExtrinsicsBy(address).length
},
}

Expand Down