Skip to content

Commit 221cd23

Browse files
Seulgi Kimmergify[bot]
authored andcommitted
Use the child of the best header to verify transactions
In the previous implementation, the transaction expired after the parent block is added in the pool and rejected during execution. This patch makes mempool reject the transaction. So the expriation e2e tests are adjusted.
1 parent 3bc234e commit 221cd23

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

core/src/block.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ pub struct ExecutedBlock {
9393
}
9494

9595
impl ExecutedBlock {
96-
fn new(state: TopLevelState) -> ExecutedBlock {
96+
fn new(state: TopLevelState, parent: &Header) -> ExecutedBlock {
9797
ExecutedBlock {
98-
header: Default::default(),
98+
header: parent.generate_child(),
9999
state,
100100
transactions: Default::default(),
101101
invoices: Default::default(),
@@ -132,17 +132,13 @@ impl<'x> OpenBlock<'x> {
132132
author: Address,
133133
extra_data: Bytes,
134134
) -> Result<Self, Error> {
135-
let number = parent.number() + 1;
136135
let state = TopLevelState::from_existing(db, *parent.state_root()).map_err(StateError::from)?;
137136
let mut r = OpenBlock {
138-
block: ExecutedBlock::new(state),
137+
block: ExecutedBlock::new(state, parent),
139138
engine,
140139
};
141140

142-
r.block.header.set_parent_hash(parent.hash());
143-
r.block.header.set_number(number);
144141
r.block.header.set_author(author);
145-
r.block.header.set_timestamp_now(parent.timestamp());
146142
r.block.header.set_extra_data(extra_data);
147143
r.block.header.note_dirty();
148144

core/src/header.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,17 @@ impl Header {
261261
pub fn rlp_blake(&self, with_seal: &Seal) -> H256 {
262262
blake256(&self.rlp(with_seal))
263263
}
264+
265+
pub fn generate_child(&self) -> Self {
266+
let mut header = Header::default();
267+
268+
header.set_parent_hash(self.hash());
269+
header.set_number(self.number() + 1);
270+
header.set_timestamp_now(self.timestamp());
271+
header.note_dirty();
272+
273+
header
274+
}
264275
}
265276

266277
impl Decodable for Header {

core/src/miner/miner.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl Miner {
254254
default_origin: TxOrigin,
255255
mem_pool: &mut MemPool,
256256
) -> Vec<Result<TransactionImportResult, Error>> {
257-
let best_block_header = client.best_block_header().decode();
257+
let fake_header = client.best_block_header().decode().generate_child();
258258
let current_block_number = client.chain_info().best_block_number;
259259
let current_timestamp = client.chain_info().best_block_timestamp;
260260
let mut inserted = Vec::with_capacity(transactions.len());
@@ -274,16 +274,16 @@ impl Miner {
274274
}
275275
match self
276276
.engine
277-
.verify_transaction_basic(&tx, &best_block_header)
278-
.and_then(|_| self.engine.verify_transaction_unordered(tx, &best_block_header))
277+
.verify_transaction_basic(&tx, &fake_header)
278+
.and_then(|_| self.engine.verify_transaction_unordered(tx, &fake_header))
279279
{
280280
Err(e) => {
281281
cdebug!(MINER, "Rejected transaction {:?} with invalid signature: {:?}", hash, e);
282282
Err(e)
283283
}
284284
Ok(tx) => {
285285
// This check goes here because verify_transaction takes SignedTransaction parameter
286-
self.engine.machine().verify_transaction(&tx, &best_block_header, client, false)?;
286+
self.engine.machine().verify_transaction(&tx, &fake_header, client, false)?;
287287

288288
let origin = self
289289
.accounts

test/src/e2e.long/expiration.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe("TransferAsset expiration test", function() {
5151

5252
// 3. Send TransferAsset transactions (which should not processed)
5353
const seq = await node.sdk.rpc.chain.getSeq(faucetAddress);
54-
startTime = Math.round(new Date().getTime() / 1000);
54+
startTime = Math.round(new Date().getTime() / 1000) + 5;
5555
for (let i = 0; i < numTx; i++) {
5656
const recipient = await node.createP2PKHAddress();
5757
const tx = node.sdk.core.createTransferAssetTransaction({
@@ -95,7 +95,7 @@ describe("TransferAsset expiration test", function() {
9595
const isExpired = bestBlockTimestamp > startTime + numTx - i;
9696
expect(isResultsEmpty).to.be.equal(isExpired);
9797
}
98-
}).timeout(10_000);
98+
}).timeout(15_000);
9999
});
100100

101101
afterEach(async function() {

0 commit comments

Comments
 (0)