Skip to content
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
6 changes: 5 additions & 1 deletion core/src/miner/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion core/src/miner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub trait MinerService: Send + Sync {
/// Called when blocks are imported to chain, updates transactions queue.
fn chain_new_blocks<C>(&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;
Expand Down
42 changes: 42 additions & 0 deletions test/src/helper/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(key: string, p: Promise<T>): Promise<T> {
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(
", "
)}`
);
}
}
}
84 changes: 84 additions & 0 deletions test/src/integration.long/futureTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.

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();
});
});