Skip to content

Commit 1b8b574

Browse files
Seonpyo Kimmergify[bot]
authored andcommitted
Extract sending failing transaction precedure to a function.
Previously when sending certainly failing transaction to the node, the code inserted a successful dummy transaction together to make sure the mempool creates the block. Now this procedure extracted to a function. And did some refactoring.
1 parent 90b7e09 commit 1b8b574

File tree

2 files changed

+72
-219
lines changed

2 files changed

+72
-219
lines changed

test/src/e2e/multisig.test.ts

Lines changed: 36 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import {
2020
AssetAddress,
2121
H160,
2222
Script,
23-
TransferAsset
23+
TransferAsset,
24+
Transaction
2425
} from "codechain-sdk/lib/core/classes";
2526
import {
2627
blake160,
@@ -29,7 +30,6 @@ import {
2930
} from "codechain-sdk/lib/utils";
3031
import "mocha";
3132
import {
32-
aliceAddress,
3333
alicePublic,
3434
aliceSecret,
3535
bobPublic,
@@ -39,12 +39,38 @@ import {
3939
daveSecret,
4040
faucetAddress
4141
} from "../helper/constants";
42+
import { AssetTransaction } from "codechain-sdk/lib/core/Transaction";
4243
import CodeChain from "../helper/spawn";
4344

4445
const { PUSH, PUSHB, CHKMULTISIG } = Script.Opcode;
4546

47+
// If one only sends certainly failing trasactions, the miner would not generate any block.
48+
// So to clearly check the result failed, insert the failing transactions inbetween succeessful ones.
49+
async function expectTransactionFail(
50+
node: CodeChain,
51+
targetTx: Transaction & AssetTransaction
52+
) {
53+
await node.sdk.rpc.devel.stopSealing();
54+
55+
const blockNumber = await node.getBestBlockNumber();
56+
const seq = await node.sdk.rpc.chain.getSeq(faucetAddress);
57+
const signedDummyTx = await node.sendPayTx({ seq, quantity: 1 });
58+
const targetTxHash = await node.sendAssetTransaction(targetTx, {
59+
seq: seq + 1
60+
});
61+
62+
await node.sdk.rpc.devel.startSealing();
63+
await node.waitBlockNumber(blockNumber + 1);
64+
65+
expect(await node.sdk.rpc.chain.containsTransaction(signedDummyTx.hash()))
66+
.be.true;
67+
expect(await node.sdk.rpc.chain.containsTransaction(targetTxHash)).be.false;
68+
expect(await node.sdk.rpc.chain.getErrorHint(targetTxHash)).not.null;
69+
}
70+
4671
describe("Multisig", function() {
4772
let node: CodeChain;
73+
4874
beforeEach(async function() {
4975
node = new CodeChain();
5076
await node.start();
@@ -168,27 +194,7 @@ describe("Multisig", function() {
168194
])
169195
);
170196

171-
await node.sdk.rpc.devel.stopSealing();
172-
const seq = await node.sdk.rpc.chain.getSeq(faucetAddress);
173-
const blockNumber = await node.getBestBlockNumber();
174-
const pay = await node.sendPayTx({
175-
seq,
176-
quantity: 1,
177-
recipient: aliceAddress
178-
});
179-
const hash = await node.sendAssetTransaction(transfer, {
180-
seq: seq + 1
181-
});
182-
await node.sdk.rpc.devel.startSealing();
183-
await node.waitBlockNumber(blockNumber + 1);
184-
185-
expect(await node.sdk.rpc.chain.containsTransaction(pay.hash())).be
186-
.true;
187-
expect(await node.sdk.rpc.chain.getTransaction(pay.hash())).not
188-
.null;
189-
expect(await node.sdk.rpc.chain.containsTransaction(hash)).be.false;
190-
expect(await node.sdk.rpc.chain.getTransaction(hash)).null;
191-
expect(await node.sdk.rpc.chain.getErrorHint(hash)).not.null;
197+
await expectTransactionFail(node, transfer);
192198
});
193199
});
194200

@@ -341,27 +347,7 @@ describe("Multisig", function() {
341347
])
342348
);
343349

344-
await node.sdk.rpc.devel.stopSealing();
345-
const seq = await node.sdk.rpc.chain.getSeq(faucetAddress);
346-
const blockNumber = await node.getBestBlockNumber();
347-
const pay = await node.sendPayTx({
348-
seq,
349-
quantity: 1,
350-
recipient: aliceAddress
351-
});
352-
const hash = await node.sendAssetTransaction(transfer, {
353-
seq: seq + 1
354-
});
355-
await node.sdk.rpc.devel.startSealing();
356-
await node.waitBlockNumber(blockNumber + 1);
357-
358-
expect(await node.sdk.rpc.chain.containsTransaction(pay.hash())).be
359-
.true;
360-
expect(await node.sdk.rpc.chain.getTransaction(pay.hash())).not
361-
.null;
362-
expect(await node.sdk.rpc.chain.containsTransaction(hash)).be.false;
363-
expect(await node.sdk.rpc.chain.getTransaction(hash)).null;
364-
expect(await node.sdk.rpc.chain.getErrorHint(hash)).not.null;
350+
await expectTransactionFail(node, transfer);
365351
});
366352
});
367353

@@ -530,27 +516,7 @@ describe("Multisig", function() {
530516
])
531517
);
532518

533-
await node.sdk.rpc.devel.stopSealing();
534-
const seq = await node.sdk.rpc.chain.getSeq(faucetAddress);
535-
const blockNumber = await node.getBestBlockNumber();
536-
const pay = await node.sendPayTx({
537-
seq,
538-
quantity: 1,
539-
recipient: aliceAddress
540-
});
541-
const hash = await node.sendAssetTransaction(transfer, {
542-
seq: seq + 1
543-
});
544-
await node.sdk.rpc.devel.startSealing();
545-
await node.waitBlockNumber(blockNumber + 1);
546-
547-
expect(await node.sdk.rpc.chain.containsTransaction(pay.hash())).be
548-
.true;
549-
expect(await node.sdk.rpc.chain.getTransaction(pay.hash())).not
550-
.null;
551-
expect(await node.sdk.rpc.chain.containsTransaction(hash)).be.false;
552-
expect(await node.sdk.rpc.chain.getTransaction(hash)).null;
553-
expect(await node.sdk.rpc.chain.getErrorHint(hash)).not.null;
519+
await expectTransactionFail(node, transfer);
554520
});
555521

556522
it("fail to unlock with the third and the first key - signature unordered", async function() {
@@ -580,27 +546,7 @@ describe("Multisig", function() {
580546
])
581547
);
582548

583-
await node.sdk.rpc.devel.stopSealing();
584-
const seq = await node.sdk.rpc.chain.getSeq(faucetAddress);
585-
const blockNumber = await node.getBestBlockNumber();
586-
const pay = await node.sendPayTx({
587-
seq,
588-
quantity: 1,
589-
recipient: aliceAddress
590-
});
591-
const hash = await node.sendAssetTransaction(transfer, {
592-
seq: seq + 1
593-
});
594-
await node.sdk.rpc.devel.startSealing();
595-
await node.waitBlockNumber(blockNumber + 1);
596-
597-
expect(await node.sdk.rpc.chain.containsTransaction(pay.hash())).be
598-
.true;
599-
expect(await node.sdk.rpc.chain.getTransaction(pay.hash())).not
600-
.null;
601-
expect(await node.sdk.rpc.chain.containsTransaction(hash)).be.false;
602-
expect(await node.sdk.rpc.chain.getTransaction(hash)).null;
603-
expect(await node.sdk.rpc.chain.getErrorHint(hash)).not.null;
549+
await expectTransactionFail(node, transfer);
604550
});
605551

606552
it("fail to unlock with the third and the second key - signature unordered", async function() {
@@ -630,27 +576,7 @@ describe("Multisig", function() {
630576
])
631577
);
632578

633-
await node.sdk.rpc.devel.stopSealing();
634-
const seq = await node.sdk.rpc.chain.getSeq(faucetAddress);
635-
const blockNumber = await node.getBestBlockNumber();
636-
const pay = await node.sendPayTx({
637-
seq,
638-
quantity: 1,
639-
recipient: aliceAddress
640-
});
641-
const hash = await node.sendAssetTransaction(transfer, {
642-
seq: seq + 1
643-
});
644-
await node.sdk.rpc.devel.startSealing();
645-
await node.waitBlockNumber(blockNumber + 1);
646-
647-
expect(await node.sdk.rpc.chain.containsTransaction(pay.hash())).be
648-
.true;
649-
expect(await node.sdk.rpc.chain.getTransaction(pay.hash())).not
650-
.null;
651-
expect(await node.sdk.rpc.chain.containsTransaction(hash)).be.false;
652-
expect(await node.sdk.rpc.chain.getTransaction(hash)).null;
653-
expect(await node.sdk.rpc.chain.getErrorHint(hash)).not.null;
579+
await expectTransactionFail(node, transfer);
654580
});
655581

656582
it("fail to unlock if the first key is unknown", async function() {
@@ -680,27 +606,7 @@ describe("Multisig", function() {
680606
])
681607
);
682608

683-
await node.sdk.rpc.devel.stopSealing();
684-
const seq = await node.sdk.rpc.chain.getSeq(faucetAddress);
685-
const blockNumber = await node.getBestBlockNumber();
686-
const pay = await node.sendPayTx({
687-
seq,
688-
quantity: 1,
689-
recipient: aliceAddress
690-
});
691-
const hash = await node.sendAssetTransaction(transfer, {
692-
seq: seq + 1
693-
});
694-
await node.sdk.rpc.devel.startSealing();
695-
await node.waitBlockNumber(blockNumber + 1);
696-
697-
expect(await node.sdk.rpc.chain.containsTransaction(pay.hash())).be
698-
.true;
699-
expect(await node.sdk.rpc.chain.getTransaction(pay.hash())).not
700-
.null;
701-
expect(await node.sdk.rpc.chain.containsTransaction(hash)).be.false;
702-
expect(await node.sdk.rpc.chain.getTransaction(hash)).null;
703-
expect(await node.sdk.rpc.chain.getErrorHint(hash)).not.null;
609+
await expectTransactionFail(node, transfer);
704610
});
705611

706612
it("fail to unlock if the second key is unknown", async function() {
@@ -730,27 +636,7 @@ describe("Multisig", function() {
730636
])
731637
);
732638

733-
await node.sdk.rpc.devel.stopSealing();
734-
const seq = await node.sdk.rpc.chain.getSeq(faucetAddress);
735-
const blockNumber = await node.getBestBlockNumber();
736-
const pay = await node.sendPayTx({
737-
seq,
738-
quantity: 1,
739-
recipient: aliceAddress
740-
});
741-
const hash = await node.sendAssetTransaction(transfer, {
742-
seq: seq + 1
743-
});
744-
await node.sdk.rpc.devel.startSealing();
745-
await node.waitBlockNumber(blockNumber + 1);
746-
747-
expect(await node.sdk.rpc.chain.containsTransaction(pay.hash())).be
748-
.true;
749-
expect(await node.sdk.rpc.chain.getTransaction(pay.hash())).not
750-
.null;
751-
expect(await node.sdk.rpc.chain.containsTransaction(hash)).be.false;
752-
expect(await node.sdk.rpc.chain.getTransaction(hash)).null;
753-
expect(await node.sdk.rpc.chain.getErrorHint(hash)).not.null;
639+
await expectTransactionFail(node, transfer);
754640
});
755641

756642
it("fail to unlock if the same key signs twice", async function() {
@@ -780,27 +666,7 @@ describe("Multisig", function() {
780666
])
781667
);
782668

783-
await node.sdk.rpc.devel.stopSealing();
784-
const seq = await node.sdk.rpc.chain.getSeq(faucetAddress);
785-
const blockNumber = await node.getBestBlockNumber();
786-
const pay = await node.sendPayTx({
787-
seq,
788-
quantity: 1,
789-
recipient: aliceAddress
790-
});
791-
const hash = await node.sendAssetTransaction(transfer, {
792-
seq: seq + 1
793-
});
794-
await node.sdk.rpc.devel.startSealing();
795-
await node.waitBlockNumber(blockNumber + 1);
796-
797-
expect(await node.sdk.rpc.chain.containsTransaction(pay.hash())).be
798-
.true;
799-
expect(await node.sdk.rpc.chain.getTransaction(pay.hash())).not
800-
.null;
801-
expect(await node.sdk.rpc.chain.containsTransaction(hash)).be.false;
802-
expect(await node.sdk.rpc.chain.getTransaction(hash)).null;
803-
expect(await node.sdk.rpc.chain.getErrorHint(hash)).not.null;
669+
await expectTransactionFail(node, transfer);
804670
});
805671
});
806672

0 commit comments

Comments
 (0)