Skip to content

Commit a9aada0

Browse files
committed
Merge branch 'feature/td-era-snapshot' into tmp/tendermint-e2e
2 parents 526a280 + c4500dd commit a9aada0

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed

core/src/consensus/tendermint/worker.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,8 @@ impl Worker {
16451645
}
16461646
};
16471647

1648+
self.send_snapshot_notify(c.as_ref(), enacted.as_slice());
1649+
16481650
if self.step.is_commit() && (imported.len() + enacted.len() == 1) {
16491651
let (_, committed_block_hash) = self.step.committed().expect("Commit state always has block_hash");
16501652
if imported.first() == Some(&committed_block_hash) {
@@ -1662,28 +1664,6 @@ impl Worker {
16621664
}
16631665
}
16641666

1665-
let mut last_term_end = None;
1666-
for block_hash in &enacted {
1667-
let header = c.block_header(&BlockId::Hash(*block_hash)).expect("Block is enacted").decode();
1668-
let parent_header = match c.block_header(&BlockId::Hash(*header.parent_hash())) {
1669-
Some(h) => h.decode(),
1670-
// NOTE: Only the genesis block and the snapshot target don't have the parent in the blockchain
1671-
None => continue,
1672-
};
1673-
let term_seconds = if let Some(p) = c.term_common_params(parent_header.hash().into()) {
1674-
p.term_seconds()
1675-
} else {
1676-
continue
1677-
};
1678-
if super::engine::is_term_changed(&header, &parent_header, term_seconds) {
1679-
last_term_end = Some(*block_hash);
1680-
}
1681-
}
1682-
if let Some(last_term_end) = last_term_end {
1683-
// TODO: Reduce the snapshot frequency.
1684-
self.snapshot_notify_sender.notify(last_term_end);
1685-
}
1686-
16871667
if let Some((last, rest)) = imported.split_last() {
16881668
let (imported, last_proposal_header) = {
16891669
let header =
@@ -1718,6 +1698,26 @@ impl Worker {
17181698
}
17191699
}
17201700

1701+
// Notify once for the latest block even if multiple blocks have been enacted.
1702+
fn send_snapshot_notify(&mut self, c: &dyn ConsensusClient, enacted: &[BlockHash]) {
1703+
let mut last_snapshot_point = None;
1704+
for block_hash in enacted.iter().rev() {
1705+
let block_id = BlockId::Hash(*block_hash);
1706+
let last_term_finished_block_num = c.last_term_finished_block_num(block_id).expect("Block is enacted");
1707+
let block_number = c.block_number(&block_id).expect("Block number should exist for enacted block");
1708+
1709+
if let Some(params) = c.term_common_params(block_id) {
1710+
if params.era() == 1 && (last_term_finished_block_num + 1 == block_number) {
1711+
last_snapshot_point = Some(block_hash);
1712+
}
1713+
}
1714+
}
1715+
if let Some(last_snapshot_point) = last_snapshot_point {
1716+
// TODO: Reduce the snapshot frequency.
1717+
self.snapshot_notify_sender.notify(*last_snapshot_point);
1718+
}
1719+
}
1720+
17211721
fn send_proposal_block(
17221722
&self,
17231723
signature: SchnorrSignature,

test/src/e2e.dynval/2/snapshot.test.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
import * as chai from "chai";
1818
import { expect } from "chai";
1919
import * as chaiAsPromised from "chai-as-promised";
20+
import * as stake from "codechain-stakeholder-sdk";
2021
import * as fs from "fs";
2122
import "mocha";
2223
import * as path from "path";
2324

2425
import mkdirp = require("mkdirp");
2526
import { validators } from "../../../tendermint.dynval/constants";
2627
import { PromiseExpect } from "../../helper/promise";
28+
import CodeChain from "../../helper/spawn";
2729
import { setTermTestTimeout, withNodes } from "../setup";
2830

2931
chai.use(chaiAsPromised);
@@ -37,7 +39,8 @@ describe("Snapshot for Tendermint with Dynamic Validator", function() {
3739
const { nodes } = withNodes(this, {
3840
promiseExpect,
3941
overrideParams: {
40-
maxNumOfValidators: 3
42+
maxNumOfValidators: 3,
43+
era: 1
4144
},
4245
validators: snapshotValidators.map((signer, index) => ({
4346
signer,
@@ -63,30 +66,32 @@ describe("Snapshot for Tendermint with Dynamic Validator", function() {
6366

6467
it("should be exist after some time", async function() {
6568
const termWaiter = setTermTestTimeout(this, {
66-
terms: 1
69+
terms: 2
6770
});
6871
const termMetadata = await termWaiter.waitNodeUntilTerm(nodes[0], {
6972
target: 2,
7073
termPeriods: 1
7174
});
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;
75+
const snapshotBlock = await getSnapshotBlock(nodes[0], termMetadata);
7876
expect(
79-
fs.existsSync(
80-
path.join(
81-
nodes[0].snapshotPath,
82-
blockHash.toString(),
83-
stateRoot.toString()
84-
)
77+
path.join(
78+
nodes[0].snapshotPath,
79+
snapshotBlock.hash.toString(),
80+
snapshotBlock.stateRoot.toString()
8581
)
86-
).to.be.true;
82+
).to.satisfy(fs.existsSync);
8783
});
8884

8985
afterEach(async function() {
9086
promiseExpect.checkFulfilled();
9187
});
9288
});
89+
90+
async function getSnapshotBlock(
91+
node: CodeChain,
92+
termMetadata: stake.TermMetadata
93+
) {
94+
const blockNumber = termMetadata.lastTermFinishedBlockNumber + 1;
95+
await node.waitBlockNumber(blockNumber);
96+
return (await node.sdk.rpc.chain.getBlock(blockNumber))!;
97+
}

0 commit comments

Comments
 (0)