From 4a4b03dee43f18be95d7c30657fd06ac4392406d Mon Sep 17 00:00:00 2001 From: Juhyung Park Date: Thu, 3 Jan 2019 20:53:08 +0900 Subject: [PATCH 1/3] Add PromiseExpect helper class The purpose of PromiseExpect is logging the async function which causes timeout. --- test/src/helper/promise.ts | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/test/src/helper/promise.ts b/test/src/helper/promise.ts index aa0cf6f6f5..16b280bc63 100644 --- a/test/src/helper/promise.ts +++ b/test/src/helper/promise.ts @@ -17,3 +17,45 @@ export async function wait(duration: number) { await new Promise(resolve => setTimeout(() => resolve(), duration)); } + +export class PromiseExpect { + public waiting: { [index: string]: number }; + + constructor() { + this.waiting = {}; + } + + public async shouldFulfill(key: string, p: Promise): Promise { + if (!this.waiting[key]) { + this.waiting[key] = 1; + } else { + this.waiting[key] += 1; + } + + const result = await p; + + this.waiting[key] -= 1; + if (this.waiting[key] === 0) { + delete this.waiting[key]; + } + return result; + } + + public checkFulfilled() { + const timeoutJobs = []; + for (const key in this.waiting) { + if (this.waiting.hasOwnProperty(key)) { + timeoutJobs.push(key); + } + } + this.waiting = {}; + + if (timeoutJobs.length > 0) { + throw new Error( + `Timeout period is expired while waiting ${timeoutJobs.join( + ", " + )}` + ); + } + } +} From c017a6da84846871bbbbfb79aa9d7a5c1178e259 Mon Sep 17 00:00:00 2001 From: Juhyung Park Date: Thu, 3 Jan 2019 20:54:12 +0900 Subject: [PATCH 2/3] Add an integration test that checks whether future parcels is mined or not This test fails now. --- .../futureTransaction.test.ts | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 test/src/integration.long/futureTransaction.test.ts diff --git a/test/src/integration.long/futureTransaction.test.ts b/test/src/integration.long/futureTransaction.test.ts new file mode 100644 index 0000000000..6ba2451875 --- /dev/null +++ b/test/src/integration.long/futureTransaction.test.ts @@ -0,0 +1,84 @@ +// Copyright 2018 Kodebox, Inc. +// This file is part of CodeChain. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +import { PromiseExpect } from "../helper/promise"; +import CodeChain from "../helper/spawn"; + +import "mocha"; + +const BASE = 1100; + +describe("Handle future transactions", function() { + let nodeA: CodeChain; + const promiseExpect = new PromiseExpect(); + + beforeEach(async function() { + nodeA = new CodeChain({ + base: BASE + }); + + await nodeA.start(["--no-tx-relay"]); + }); + + it("Alone", async function() { + const sending = []; + for (let i = 0; i < 10; i++) { + sending.push(nodeA.sendPayTx({ seq: 9 - i })); + } + await Promise.all(sending); + }).timeout(10_000); + + describe("Two nodes", function() { + let nodeB: CodeChain; + + beforeEach(async function() { + nodeB = new CodeChain({ + base: BASE + }); + await nodeB.start(["--no-tx-relay"]); + await promiseExpect.shouldFulfill("connect", nodeB.connect(nodeA)); + }); + + it("ping pong", async function() { + const sending = []; + for (let i = 0; i < 10; i++) { + if (i % 2 == 0) { + sending.push(nodeA.sendPayTx({ seq: 9 - i })); + } else { + sending.push(nodeB.sendPayTx({ seq: 9 - i })); + } + } + await promiseExpect.shouldFulfill("Payment", Promise.all(sending)); + }).timeout(20_000); + + afterEach(async function() { + const bestBlockNumber = await nodeB.getBestBlockNumber(); + if (this.currentTest!.state === "failed") { + nodeB.testFailed(this.currentTest!.fullTitle()); + } + promiseExpect.checkFulfilled(); + await nodeB.clean(); + }); + }); + + afterEach(async function() { + const bestBlockNumber = await nodeA.getBestBlockNumber(); + if (this.currentTest!.state === "failed") { + nodeA.testFailed(this.currentTest!.fullTitle()); + } + await nodeA.clean(); + }); +}); From 3e0f8a84848e2f78fb7b00dc95b1d619c7079970 Mon Sep 17 00:00:00 2001 From: Juhyung Park Date: Thu, 3 Jan 2019 21:04:18 +0900 Subject: [PATCH 3/3] Trigger RESEAL_MIN_TIMER when block is imported This fixes src/integration.long/futureTransaction.ts test --- core/src/miner/miner.rs | 6 +++++- core/src/miner/mod.rs | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/core/src/miner/miner.rs b/core/src/miner/miner.rs index 1dc8c2685f..89ba1134e9 100644 --- a/core/src/miner/miner.rs +++ b/core/src/miner/miner.rs @@ -619,7 +619,7 @@ impl MinerService for Miner { _enacted: &[H256], retracted: &[H256], ) where - C: AccountData + BlockChain + BlockProducer + ImportSealedBlock + RegularKeyOwner, { + C: AccountData + BlockChain + BlockProducer + ImportSealedBlock + RegularKeyOwner + ResealTimer, { ctrace!(MINER, "chain_new_blocks"); // Then import all transactions... @@ -650,6 +650,10 @@ impl MinerService for Miner { let mut mem_pool = self.mem_pool.write(); mem_pool.remove_old(&fetch_account, time, timestamp); } + + if !self.options.no_reseal_timer { + chain.set_min_timer(); + } } fn can_produce_work_package(&self) -> bool { diff --git a/core/src/miner/mod.rs b/core/src/miner/mod.rs index 2b89d458a8..f14c295c52 100644 --- a/core/src/miner/mod.rs +++ b/core/src/miner/mod.rs @@ -72,7 +72,7 @@ pub trait MinerService: Send + Sync { /// Called when blocks are imported to chain, updates transactions queue. fn chain_new_blocks(&self, chain: &C, imported: &[H256], invalid: &[H256], enacted: &[H256], retracted: &[H256]) where - C: AccountData + BlockChain + BlockProducer + ImportSealedBlock + RegularKeyOwner; + C: AccountData + BlockChain + BlockProducer + ImportSealedBlock + RegularKeyOwner + ResealTimer; /// PoW chain - can produce work package fn can_produce_work_package(&self) -> bool;