Catch promise errors in subscription (#1138)

* Testcase for invalid tx (not caught)

* Re-enable commented tests

* Make the test pass

* Cleanup tests, check for promise to exist
This commit is contained in:
Jaco Greeff
2019-07-16 20:01:31 +02:00
committed by GitHub
parent 970f689309
commit eaec0da2f5
3 changed files with 68 additions and 11 deletions
+1 -1
View File
@@ -52,7 +52,7 @@ export default async function test (): Promise<void> {
// constants has actual value & metadata
console.log(
api.consts.balances.creationFee.toHex(),
api.consts.balances.creationFee.meta.documentation.map((s) => s.toString()).join('')
api.consts.balances.creationFee.meta.documentation.map((s): string => s.toString()).join('')
);
const transfer = api.tx.balances.transfer(keyring.bob.address, 12345);
@@ -5,6 +5,7 @@
import Keyring from '@polkadot/keyring';
import testingPairs from '@polkadot/keyring/testingPairs';
import WsProvider from '@polkadot/rpc-provider/ws';
import { u8aToHex } from '@polkadot/util';
import { randomAsHex } from '@polkadot/util-crypto';
import { EventRecord, ExtrinsicEra, Hash, Header, Index, SignedBlock } from '@polkadot/types';
@@ -55,6 +56,28 @@ describeE2E({
return api.tx(hex).send(logEvents(done));
});
it('invalid hex does throw a catchable exception', async (done): Promise<void> => {
const nonce = await api.query.system.accountNonce(keyring.dave.address) as Index;
const hex = api.tx.balances
.transfer(keyring.eve.address, 12345)
.sign(keyring.dave, { nonce })
.toHex();
// change to an invalid signature, 32 * 2 for hex
const sigIdx = hex.indexOf(u8aToHex(keyring.dave.publicKey).substr(2)) + 64;
const mangled = hex.replace(
hex.substr(sigIdx, 32), // first 16 bytes of sig
hex.substr(2, 32) // replaced by first 16 bytes of tx
);
try {
await api.tx(mangled).send(logEvents(done));
} catch (error) {
console.error(error);
done();
}
});
it('makes a transfer (sign, then send)', async (done): Promise<() => void> => {
const nonce = await api.query.system.accountNonce(keyring.dave.address) as Index;
@@ -166,6 +189,31 @@ describeE2E({
});
});
it('fails on a transfer with invalid time (via Signer)', async (done): Promise<void> => {
const signer: Signer = new SingleAccountSigner(keyring.alice);
api.setSigner(signer);
const nonce = await api.query.system.accountNonce(keyring.alice.address) as Index;
const signedBlock = await api.rpc.chain.getBlock() as SignedBlock;
const currentHeight = signedBlock.block.header.number;
const exERA = new ExtrinsicEra({ current: currentHeight, period: 4 });
const eraDeath = exERA.asMortalEra.death(currentHeight.toNumber());
const blockHash = signedBlock.block.header.hash;
const ex = api.tx.balances.transfer(keyring.eve.address, 12345);
await api.rpc.chain.subscribeNewHead(async (header: Header): Promise<void> => {
if (header.blockNumber.toNumber() === eraDeath - 1) {
try {
await ex.signAndSend(keyring.alice.address, { blockHash, era: exERA, nonce } as any);
} catch (error) {
expect(error.message).toMatch(/1010: Invalid Transaction \(0\)/);
done();
}
}
});
});
it('makes a transfer with custom numeric era', async (done): Promise<void> => {
await api.tx.balances
.transfer(keyring.eve.address, 12345)