Skip to content

Commit 1a5f0cf

Browse files
majectymergify[bot]
authored andcommitted
Use the parent block's information in the timelock OPcode
1 parent f9f3c46 commit 1a5f0cf

File tree

13 files changed

+475
-283
lines changed

13 files changed

+475
-283
lines changed

core/src/block.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use cstate::{FindActionHandler, StateDB, StateError, StateWithCache, TopLevelSta
2323
use ctypes::errors::HistoryError;
2424
use ctypes::machine::{LiveBlock, Transactions};
2525
use ctypes::util::unexpected::Mismatch;
26+
use ctypes::BlockNumber;
2627
use cvm::ChainTimeInfo;
2728
use primitives::{Bytes, H256};
2829
use rlp::{Decodable, DecoderError, Encodable, RlpStream, UntrustedRlp};
@@ -169,14 +170,23 @@ impl<'x> OpenBlock<'x> {
169170
tx: SignedTransaction,
170171
h: Option<H256>,
171172
client: &C,
173+
parent_block_number: BlockNumber,
174+
parent_block_timestamp: u64,
172175
) -> Result<(), Error> {
173176
if self.block.transactions_set.contains(&tx.hash()) {
174177
return Err(HistoryError::TransactionAlreadyImported.into())
175178
}
176179

177180
let hash = tx.hash();
178181
let tracker = tx.tracker();
179-
let error = match self.block.state.apply(&tx, &hash, &tx.signer_public(), client) {
182+
let error = match self.block.state.apply(
183+
&tx,
184+
&hash,
185+
&tx.signer_public(),
186+
client,
187+
parent_block_number,
188+
parent_block_timestamp,
189+
) {
180190
Ok(()) => {
181191
self.block.transactions_set.insert(h.unwrap_or(hash));
182192
self.block.transactions.push(tx);
@@ -201,9 +211,11 @@ impl<'x> OpenBlock<'x> {
201211
&mut self,
202212
transactions: &[SignedTransaction],
203213
client: &C,
214+
parent_block_number: BlockNumber,
215+
parent_block_timestamp: u64,
204216
) -> Result<(), Error> {
205217
for tx in transactions {
206-
self.push_transaction(tx.clone(), None, client)?;
218+
self.push_transaction(tx.clone(), None, client, parent_block_number, parent_block_timestamp)?;
207219
}
208220
Ok(())
209221
}
@@ -443,7 +455,7 @@ pub fn enact<C: ChainTimeInfo + FindActionHandler>(
443455
let mut b = OpenBlock::try_new(engine, db, parent, Address::default(), vec![], is_epoch_begin)?;
444456

445457
b.populate_from(header);
446-
b.push_transactions(transactions, client)?;
458+
b.push_transactions(transactions, client, parent.number(), parent.timestamp())?;
447459

448460
b.close_and_lock(*parent.transactions_root())
449461
}

core/src/client/client.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,14 @@ impl TextClient for Client {
405405
impl ExecuteClient for Client {
406406
fn execute_transaction(&self, transaction: &ShardTransaction, sender: &Address) -> StateResult<()> {
407407
let mut state = Client::state_at(&self, BlockId::Latest).expect("Latest state MUST exist");
408-
state.apply_shard_transaction(transaction, sender, &[], self)
408+
state.apply_shard_transaction(
409+
transaction,
410+
sender,
411+
&[],
412+
self,
413+
self.best_block_header().number(),
414+
self.best_block_header().timestamp(),
415+
)
409416
}
410417

411418
fn execute_vm(
@@ -434,6 +441,8 @@ impl ExecuteClient for Client {
434441
&input,
435442
false,
436443
self,
444+
self.best_block_header().number(),
445+
self.best_block_header().timestamp(),
437446
) {
438447
Ok(ScriptResult::Burnt) => "burnt".to_string(),
439448
Ok(ScriptResult::Unlocked) => "unlocked".to_string(),
@@ -803,21 +812,12 @@ impl ImportSealedBlock for Client {
803812
impl MiningBlockChainClient for Client {}
804813

805814
impl ChainTimeInfo for Client {
806-
fn best_block_number(&self) -> BlockNumber {
807-
self.chain_info().best_block_number
815+
fn transaction_block_age(&self, tracker: &H256, parent_block_number: BlockNumber) -> Option<u64> {
816+
self.transaction_block_number(tracker).map(|block_number| parent_block_number - block_number)
808817
}
809818

810-
fn best_block_timestamp(&self) -> u64 {
811-
self.chain_info().best_block_timestamp
812-
}
813-
814-
fn transaction_block_age(&self, tracker: &H256) -> Option<u64> {
815-
self.transaction_block_number(tracker).map(|block_number| self.chain_info().best_block_number - block_number)
816-
}
817-
818-
fn transaction_time_age(&self, tracker: &H256) -> Option<u64> {
819-
self.transaction_block_timestamp(tracker)
820-
.map(|block_timestamp| self.chain_info().best_block_timestamp - block_timestamp)
819+
fn transaction_time_age(&self, tracker: &H256, parent_timestamp: u64) -> Option<u64> {
820+
self.transaction_block_timestamp(tracker).map(|block_timestamp| parent_timestamp - block_timestamp)
821821
}
822822
}
823823

core/src/client/test_client.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -562,19 +562,11 @@ impl ResealTimer for TestBlockChainClient {
562562
}
563563

564564
impl ChainTimeInfo for TestBlockChainClient {
565-
fn best_block_number(&self) -> u64 {
566-
0
567-
}
568-
569-
fn best_block_timestamp(&self) -> u64 {
570-
0
571-
}
572-
573-
fn transaction_block_age(&self, _: &H256) -> Option<u64> {
565+
fn transaction_block_age(&self, _: &H256, _parent_block_number: BlockNumber) -> Option<u64> {
574566
Some(0)
575567
}
576568

577-
fn transaction_time_age(&self, _: &H256) -> Option<u64> {
569+
fn transaction_time_age(&self, _: &H256, _parent_timestamp: u64) -> Option<u64> {
578570
Some(0)
579571
}
580572
}

core/src/miner/miner.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,10 @@ impl Miner {
467467
let mut invalid_transactions = Vec::new();
468468
let block_number = open_block.block().header().number();
469469

470+
let parent_header = {
471+
let parent_hash = open_block.header().parent_hash();
472+
chain.block_header(&BlockId::Hash(*parent_hash)).expect("Parent header MUST exist")
473+
};
470474
let mut tx_count: usize = 0;
471475
let tx_total = transactions.len();
472476
let mut invald_tx_users = HashSet::new();
@@ -485,11 +489,10 @@ impl Miner {
485489
let hash = tx.hash();
486490
let start = Instant::now();
487491
// Check whether transaction type is allowed for sender
488-
let result = self
489-
.engine
490-
.machine()
491-
.verify_transaction(&tx, open_block.header(), chain, true)
492-
.and_then(|_| open_block.push_transaction(tx, None, chain));
492+
let result =
493+
self.engine.machine().verify_transaction(&tx, open_block.header(), chain, true).and_then(|_| {
494+
open_block.push_transaction(tx, None, chain, parent_header.number(), parent_header.timestamp())
495+
});
493496

494497
match result {
495498
// already have transaction - ignore

0 commit comments

Comments
 (0)