-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for filters and JSON-RPC batched requests (#75)
* initial filter support * better error handling * support batched JSON RPC requests * support uninstalling filters * add chain -> filters tests * rpc -> filters test * tweak filters behaviour to match the spec * lint fix
- Loading branch information
Showing
10 changed files
with
295 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Internal stored representation of blockchain filter | ||
*/ | ||
export interface ChainFilter { | ||
type: 'block', | ||
lastSeenBlock: number, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Chain, numberToQuantity } from '../../src' | ||
import { expect } from 'chai' | ||
|
||
async function createChain (): Promise<Chain> { | ||
const chain = new Chain() | ||
await chain.init() | ||
|
||
return chain | ||
} | ||
|
||
describe('Chain -> filters', () => { | ||
it('creates filters', async () => { | ||
const chain = await createChain() | ||
|
||
const id = await chain.createNewBlockFilter() | ||
|
||
expect(id).to.be.eq(numberToQuantity(0)) | ||
}) | ||
|
||
it('gets changes right after creating a filter', async () => { | ||
const chain = await createChain() | ||
|
||
const id = await chain.createNewBlockFilter() | ||
|
||
const changes = await chain.getFilterChanges(id) | ||
|
||
expect(changes).to.have.length(1) | ||
expect(changes[0]).to.be.string | ||
}) | ||
|
||
it('gets changes from existing filters', async () => { | ||
const chain = await createChain() | ||
|
||
// this block shouldn't matter | ||
await chain.mineBlock() | ||
|
||
const id = await chain.createNewBlockFilter() | ||
|
||
await chain.mineBlock() | ||
|
||
const changes = await chain.getFilterChanges(id) | ||
|
||
expect(changes).to.have.length(2) | ||
}) | ||
|
||
it('gets changes from existing filters from the last pool', async () => { | ||
const chain = await createChain() | ||
|
||
const id = await chain.createNewBlockFilter() | ||
await chain.getFilterChanges(id) | ||
const changes = await chain.getFilterChanges(id) | ||
|
||
expect(changes).to.have.length(0) | ||
}) | ||
|
||
it('throws on not-existing filters', async () => { | ||
const chain = await createChain() | ||
|
||
const changesPromise = chain.getFilterChanges(numberToQuantity(1)) | ||
|
||
expect(changesPromise).to.be.rejected | ||
}) | ||
|
||
it('uninstalls existing filters', async () => { | ||
const chain = await createChain() | ||
|
||
const id = await chain.createNewBlockFilter() | ||
|
||
expect(await chain.uninstallFilter(id)).to.be.true | ||
// it should reject since this filter doesnt exist anymore | ||
expect(chain.getFilterChanges(id)).to.be.rejected | ||
}) | ||
|
||
it('uninstalls not-existing filters', async () => { | ||
const chain = await createChain() | ||
|
||
expect(await chain.uninstallFilter(numberToQuantity(1))).to.be.false | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.