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

Fix upcoming #168

Merged
merged 2 commits into from
Jan 26, 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
37 changes: 37 additions & 0 deletions e2e/block.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, it } from 'vitest'

import { chain, delay, dev, setupApi } from './helper'

setupApi({ endpoint: 'wss://rpc.polkadot.io' })

describe('block', () => {
it('upcoming block works', async () => {
const blockNumber = chain.head.number

setTimeout(() => {
dev.newBlock()
}, 1000)
{
const next = await chain.upcomingBlock()
expect(next.number).toEqual(blockNumber + 1)
}

setTimeout(() => {
dev.newBlock()
}, 1000)
{
const next = await chain.upcomingBlock()
expect(next.number).toEqual(blockNumber + 2)
}

setTimeout(() => {
dev.newBlock({ count: 3 })
}, 1000)
{
const next = await chain.upcomingBlock(2)
expect(next.number).toEqual(blockNumber + 5)
}

await delay(1000)
})
})
3 changes: 2 additions & 1 deletion e2e/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ export const setupApi = (option: SetupOption) => {
beforeAll(async () => {
const res = await setupAll(option)
setup = res.setup
return () => res.teardownAll()

return res.teardownAll
})

beforeEach(async () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@acala-network/chopsticks",
"version": "0.3.8",
"version": "0.3.9",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"author": "Bryan Chen <[email protected]>",
Expand Down
4 changes: 2 additions & 2 deletions src/blockchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ export class Blockchain {
return this.#head
}

async upcomingBlock(count = 1) {
return this.#txpool.upcomingBlock(count)
async upcomingBlock(skipCount = 0) {
return this.#txpool.upcomingBlock(skipCount)
}

async dryRunExtrinsic(
Expand Down
15 changes: 7 additions & 8 deletions src/blockchain/txpool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BehaviorSubject, firstValueFrom } from 'rxjs'
import { HexString } from '@polkadot/util/types'
import { ReplaySubject, firstValueFrom, share } from 'rxjs'
import { first, skip } from 'rxjs/operators'
import { skip, take } from 'rxjs/operators'
import _ from 'lodash'

import { Block } from './block'
Expand Down Expand Up @@ -37,13 +37,12 @@ export class TxPool {
readonly #mode: BuildBlockMode
readonly #inherentProvider: InherentProvider

#last: BehaviorSubject<Block>
#lastBuildBlockPromise: Promise<void> = Promise.resolve()

#last = new ReplaySubject<Block>(1)
#upcoming = this.#last.pipe(share())

constructor(chain: Blockchain, inherentProvider: InherentProvider, mode: BuildBlockMode = BuildBlockMode.Batch) {
this.#chain = chain
this.#last = new BehaviorSubject<Block>(chain.head)
this.#mode = mode
this.#inherentProvider = inherentProvider
}
Expand Down Expand Up @@ -77,9 +76,9 @@ export class TxPool {
this.#last.next(this.#chain.head)
}

async upcomingBlock(count = 1) {
if (count < 1) throw new Error('count needs to be greater than 0')
return firstValueFrom(this.#upcoming.pipe(skip(count - 1), first()))
async upcomingBlock(skipCount = 0) {
if (skipCount < 0) throw new Error('skipCount needs to be greater or equal to 0')
return firstValueFrom(this.#last.pipe(skip(1 + skipCount), take(1)))
}

async #buildBlock(wait: Promise<void>, params?: BuildBlockParams) {
Expand Down