Skip to content

Commit 1017936

Browse files
committed
Fetch consistent state in mempool functions
We found that mempool mis-calculates sequence in the TPS test. After fixing the mempool to read consistent state, mempool calculates sequence well.
1 parent 791e639 commit 1017936

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

core/src/client/test_client.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ impl AccountData for TestBlockChainClient {
366366
fn seq(&self, pubkey: &Public, id: BlockId) -> Option<u64> {
367367
match id {
368368
BlockId::Latest => Some(self.seqs.read().get(pubkey).cloned().unwrap_or(0)),
369+
BlockId::Hash(hash) if hash == *self.last_hash.read() => {
370+
Some(self.seqs.read().get(pubkey).cloned().unwrap_or(0))
371+
}
369372
_ => None,
370373
}
371374
}
@@ -375,6 +378,9 @@ impl AccountData for TestBlockChainClient {
375378
StateOrBlock::Block(BlockId::Latest) | StateOrBlock::State(_) => {
376379
Some(self.balances.read().get(pubkey).cloned().unwrap_or(0))
377380
}
381+
StateOrBlock::Block(BlockId::Hash(hash)) if hash == *self.last_hash.read() => {
382+
Some(self.balances.read().get(pubkey).cloned().unwrap_or(0))
383+
}
378384
_ => None,
379385
}
380386
}

core/src/miner/mem_pool.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::transaction::{PendingVerifiedTransactions, VerifiedTransaction};
2626
use crate::Error as CoreError;
2727
use ckey::Ed25519Public as Public;
2828
use ctypes::errors::{HistoryError, RuntimeError, SyntaxError};
29-
use ctypes::{BlockNumber, TxHash};
29+
use ctypes::{BlockId, BlockNumber, TxHash};
3030
use kvdb::{DBTransaction, KeyValueDB};
3131
use std::cmp::max;
3232
use std::collections::{BTreeSet, HashMap, HashSet};
@@ -404,7 +404,12 @@ impl MemPool {
404404

405405
// Recover MemPool state from db stored data
406406
pub fn recover_from_db<C: AccountData + BlockChainTrait>(&mut self, client: &C) {
407-
let fetch_account = fetch_account_creator(client);
407+
let recover_block_id = {
408+
let recover_block_hash = client.chain_info().best_block_hash;
409+
BlockId::Hash(recover_block_hash)
410+
};
411+
let fetch_account = fetch_account_creator(client, recover_block_id);
412+
408413
let by_hash = backup::recover_to_data(self.db.as_ref());
409414

410415
let recover_block_number = client.chain_info().best_block_number;
@@ -1007,8 +1012,7 @@ pub mod test {
10071012
let db = Arc::new(kvdb_memorydb::create(crate::db::NUM_COLUMNS.unwrap_or(0)));
10081013
let mut mem_pool = MemPool::with_limits(8192, usize::max_value(), 3, db.clone());
10091014

1010-
let fetch_account = fetch_account_creator(&test_client);
1011-
1015+
let fetch_account = fetch_account_creator(&test_client, BlockId::Latest);
10121016
let inserted_block_number = 1;
10131017
let inserted_timestamp = 100;
10141018
let mut inputs: Vec<MemPoolInput> = Vec::new();
@@ -1081,12 +1085,11 @@ pub mod test {
10811085
let db = Arc::new(kvdb_memorydb::create(crate::db::NUM_COLUMNS.unwrap_or(0)));
10821086
let mut mem_pool = MemPool::with_limits(8192, usize::max_value(), 3, db);
10831087

1084-
let fetch_account = fetch_account_creator(&test_client);
1088+
let fetch_account = fetch_account_creator(&test_client, BlockId::Latest);
10851089
let keypair: KeyPair = Random.generate().unwrap();
10861090
let pubkey = keypair.public();
10871091
test_client.set_balance(*pubkey, 1_000_000_000_000);
10881092
assert_eq!(1_000_000_000_000, test_client.latest_balance(&pubkey));
1089-
10901093
let inserted_block_number = 1;
10911094
let inserted_timestamp = 100;
10921095
let inputs = vec![

core/src/miner/miner.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ impl Miner {
292292
})
293293
.collect();
294294

295-
let fetch_account = fetch_account_creator(client);
295+
let block_id = BlockId::Hash(best_header.hash());
296+
let fetch_account = fetch_account_creator(client, block_id);
296297

297298
let insertion_results = mem_pool.add(to_insert, current_block_number, current_timestamp, &fetch_account);
298299

@@ -469,8 +470,11 @@ impl Miner {
469470
}
470471
let block = open_block.close()?;
471472

472-
let fetch_seq = |p: &Public| chain.latest_seq(p);
473-
473+
let block_id = {
474+
let best_block_hash = chain.chain_info().best_block_hash;
475+
BlockId::Hash(best_block_hash)
476+
};
477+
let fetch_seq = |p: &Public| chain.seq(p, block_id).expect("Read from best block");
474478
{
475479
let mut mem_pool = self.mem_pool.write();
476480
mem_pool.remove(
@@ -551,7 +555,11 @@ impl MinerService for Miner {
551555
ctrace!(MINER, "chain_new_blocks");
552556

553557
{
554-
let fetch_account = fetch_account_creator(chain);
558+
let block_id = {
559+
let current_block_hash = chain.chain_info().best_block_hash;
560+
BlockId::Hash(current_block_hash)
561+
};
562+
let fetch_account = fetch_account_creator(chain, block_id);
555563
let current_block_number = chain.chain_info().best_block_number;
556564
let current_timestamp = chain.chain_info().best_block_timestamp;
557565
let mut mem_pool = self.mem_pool.write();

core/src/miner/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,12 @@ pub enum TransactionImportResult {
156156
Future,
157157
}
158158

159-
fn fetch_account_creator<'c>(client: &'c dyn AccountData) -> impl Fn(&Public) -> AccountDetails + 'c {
159+
fn fetch_account_creator<'c>(
160+
client: &'c dyn AccountData,
161+
block_id: BlockId,
162+
) -> impl Fn(&Public) -> AccountDetails + 'c {
160163
move |pubkey: &Public| AccountDetails {
161-
seq: client.latest_seq(&pubkey),
162-
balance: client.latest_balance(&pubkey),
164+
seq: client.seq(&pubkey, block_id).expect("We are querying sequence using trusted block id"),
165+
balance: client.balance(&pubkey, block_id.into()).expect("We are querying balance using trusted block id"),
163166
}
164167
}

0 commit comments

Comments
 (0)