Skip to content

Commit 7490d25

Browse files
committed
Add snapshot sync test with Tendermint dynamic validator
1 parent 4e5ba6b commit 7490d25

File tree

3 files changed

+138
-10
lines changed

3 files changed

+138
-10
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2019 Kodebox, Inc.
2+
// This file is part of CodeChain.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as
6+
// published by the Free Software Foundation, either version 3 of the
7+
// License, or (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
import * as chai from "chai";
18+
import { expect } from "chai";
19+
import * as chaiAsPromised from "chai-as-promised";
20+
import * as fs from "fs";
21+
import "mocha";
22+
import * as path from "path";
23+
24+
import mkdirp = require("mkdirp");
25+
import { validators } from "../../../tendermint.dynval/constants";
26+
import { PromiseExpect } from "../../helper/promise";
27+
import { setTermTestTimeout, withNodes } from "../setup";
28+
29+
chai.use(chaiAsPromised);
30+
31+
const SNAPSHOT_CONFIG = `${__dirname}/../../../tendermint.dynval/snapshot-config.yml`;
32+
const SNAPSHOT_PATH = `${__dirname}/../../../../snapshot/`;
33+
34+
describe("Snapshot for Tendermint with Dynamic Validator", function() {
35+
const promiseExpect = new PromiseExpect();
36+
const snapshotValidators = validators.slice(0, 3);
37+
const { nodes } = withNodes(this, {
38+
promiseExpect,
39+
overrideParams: {
40+
maxNumOfValidators: 3
41+
},
42+
validators: snapshotValidators.map((signer, index) => ({
43+
signer,
44+
delegation: 5000,
45+
deposit: 10_000_000 - index // tie-breaker
46+
})),
47+
modify: () => {
48+
mkdirp.sync(SNAPSHOT_PATH);
49+
const snapshotPath = fs.mkdtempSync(SNAPSHOT_PATH);
50+
return {
51+
additionalArgv: [
52+
"--snapshot-path",
53+
snapshotPath,
54+
"--config",
55+
SNAPSHOT_CONFIG
56+
],
57+
nodeAdditionalProperties: {
58+
snapshotPath
59+
}
60+
};
61+
}
62+
});
63+
64+
it("should be exist after some time", async function() {
65+
const termWaiter = setTermTestTimeout(this, {
66+
terms: 1
67+
});
68+
const termMetadata = await termWaiter.waitNodeUntilTerm(nodes[0], {
69+
target: 2,
70+
termPeriods: 1
71+
});
72+
73+
const blockHash = (await nodes[0].sdk.rpc.chain.getBlockHash(
74+
termMetadata.lastTermFinishedBlockNumber
75+
))!;
76+
const stateRoot = (await nodes[0].sdk.rpc.chain.getBlock(blockHash))!
77+
.stateRoot;
78+
expect(
79+
fs.existsSync(
80+
path.join(
81+
nodes[0].snapshotPath,
82+
blockHash.toString(),
83+
stateRoot.toString()
84+
)
85+
)
86+
).to.be.true;
87+
});
88+
89+
afterEach(async function() {
90+
promiseExpect.checkFulfilled();
91+
});
92+
});

test/src/e2e.dynval/setup.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,29 @@ interface ValidatorConfig {
3939
delegation?: U64Value;
4040
}
4141

42-
export function withNodes(
42+
interface NodePropertyModifier<T> {
43+
additionalArgv: string[];
44+
nodeAdditionalProperties: T;
45+
}
46+
47+
export function withNodes<T>(
4348
suite: Suite,
4449
options: {
4550
promiseExpect: PromiseExpect;
4651
validators: ValidatorConfig[];
4752
overrideParams?: Partial<CommonParams>;
4853
onBeforeEnable?: (nodes: CodeChain[]) => Promise<void>;
54+
modify?: (signer: Signer, index: number) => NodePropertyModifier<T>;
4955
}
5056
) {
51-
const nodes: CodeChain[] = [];
52-
const { overrideParams = {} } = options;
57+
const nodes: (CodeChain & T)[] = [];
58+
const {
59+
overrideParams = {},
60+
modify = () => ({
61+
additionalArgv: [],
62+
nodeAdditionalProperties: {} as T
63+
})
64+
} = options;
5365
const initialParams = {
5466
...defaultParams,
5567
...overrideParams
@@ -62,7 +74,8 @@ export function withNodes(
6274
nodes.length = 0;
6375
const newNodes = await createNodes({
6476
...options,
65-
initialParams
77+
initialParams,
78+
modify
6679
});
6780
nodes.push(...newNodes);
6881
});
@@ -95,14 +108,15 @@ export function findNode(nodes: CodeChain[], signer: Signer) {
95108
);
96109
}
97110

98-
async function createNodes(options: {
111+
async function createNodes<T>(options: {
99112
promiseExpect: PromiseExpect;
100113
validators: ValidatorConfig[];
101114
initialParams: CommonParams;
102115
onBeforeEnable?: (nodes: CodeChain[]) => Promise<void>;
103-
}): Promise<CodeChain[]> {
116+
modify: (signer: Signer, index: number) => NodePropertyModifier<T>;
117+
}): Promise<(CodeChain & T)[]> {
104118
const chain = `${__dirname}/../scheme/tendermint-dynval.json`;
105-
const { promiseExpect, validators, initialParams } = options;
119+
const { promiseExpect, validators, initialParams, modify } = options;
106120

107121
const initialNodes: CodeChain[] = [];
108122
const initialValidators = [
@@ -124,20 +138,23 @@ async function createNodes(options: {
124138
});
125139
}
126140

127-
const nodes: CodeChain[] = [];
141+
const nodes: (CodeChain & T)[] = [];
128142
for (let i = 0; i < validators.length; i++) {
129143
const { signer: validator } = validators[i];
130-
nodes[i] = new CodeChain({
144+
const modifier = modify(validator, i);
145+
const node = new CodeChain({
131146
chain,
132147
argv: [
133148
"--engine-signer",
134149
validator.platformAddress.value,
135150
"--password-path",
136151
`test/tendermint.dynval/${validator.platformAddress.value}/password.json`,
137-
"--force-sealing"
152+
"--force-sealing",
153+
...modifier.additionalArgv
138154
],
139155
additionalKeysPath: `tendermint.dynval/${validator.platformAddress.value}/keys`
140156
});
157+
nodes[i] = Object.assign(node, modifier.nodeAdditionalProperties);
141158
nodes[i].signer = validator;
142159
}
143160
let bootstrapFailed = false;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[codechain]
2+
3+
[mining]
4+
5+
[network]
6+
7+
[rpc]
8+
9+
[ipc]
10+
11+
[ws]
12+
13+
[snapshot]
14+
disable = false
15+
16+
[stratum]
17+
18+
[email_alarm]
19+

0 commit comments

Comments
 (0)