Skip to content

Commit cd542de

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 65a0072 commit cd542de

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

core/src/client/test_client.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ impl AccountData for TestBlockChainClient {
392392
fn seq(&self, address: &Address, id: BlockId) -> Option<u64> {
393393
match id {
394394
BlockId::Latest => Some(self.seqs.read().get(address).cloned().unwrap_or(0)),
395+
BlockId::Hash(hash) if hash == *self.last_hash.read() => {
396+
Some(self.seqs.read().get(address).cloned().unwrap_or(0))
397+
}
395398
_ => None,
396399
}
397400
}
@@ -401,6 +404,9 @@ impl AccountData for TestBlockChainClient {
401404
StateOrBlock::Block(BlockId::Latest) | StateOrBlock::State(_) => {
402405
Some(self.balances.read().get(address).cloned().unwrap_or(0))
403406
}
407+
StateOrBlock::Block(BlockId::Hash(hash)) if hash == *self.last_hash.read() => {
408+
Some(self.balances.read().get(address).cloned().unwrap_or(0))
409+
}
404410
_ => None,
405411
}
406412
}

core/src/miner/mem_pool.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use super::mem_pool_types::{
3333
use super::TransactionImportResult;
3434
use crate::client::{AccountData, BlockChainTrait};
3535
use crate::transaction::{PendingSignedTransactions, SignedTransaction};
36-
use crate::Error as CoreError;
36+
use crate::{BlockId, Error as CoreError};
3737

3838
const DEFAULT_POOLING_PERIOD: BlockNumber = 128;
3939

@@ -429,12 +429,14 @@ impl MemPool {
429429

430430
// Recover MemPool state from db stored data
431431
pub fn recover_from_db<C: AccountData + BlockChainTrait>(&mut self, client: &C) {
432+
let recover_block_hash = client.chain_info().best_block_hash;
433+
let recover_block_id = BlockId::Hash(recover_block_hash);
432434
let fetch_account = |p: &Public| -> AccountDetails {
433435
let address = public_to_address(p);
434-
let a = client.latest_regular_key_owner(&address).unwrap_or(address);
436+
let a = client.regular_key_owner(&address, recover_block_id.into()).unwrap_or(address);
435437
AccountDetails {
436-
seq: client.latest_seq(&a),
437-
balance: client.latest_balance(&a),
438+
seq: client.seq(&a, recover_block_id).expect("Read from best block"),
439+
balance: client.balance(&a, recover_block_id.into()).expect("Read from best block"),
438440
}
439441
};
440442
let by_hash = backup::recover_to_data(self.db.as_ref());

core/src/miner/miner.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,10 @@ impl Miner {
460460

461461
let fetch_account = |p: &Public| -> AccountDetails {
462462
let address = public_to_address(p);
463-
let a = client.latest_regular_key_owner(&address).unwrap_or(address);
463+
let a = client.regular_key_owner(&address, BlockId::Hash(best_header.hash()).into()).unwrap_or(address);
464464
AccountDetails {
465-
seq: client.latest_seq(&a),
466-
balance: client.latest_balance(&a),
465+
seq: client.seq(&a, BlockId::Hash(best_header.hash())).expect("Read from best block"),
466+
balance: client.balance(&a, BlockId::Hash(best_header.hash()).into()).expect("Read from best block"),
467467
}
468468
};
469469

@@ -718,10 +718,12 @@ impl Miner {
718718
};
719719
let block = open_block.close(&parent_header, &parent_common_params, term_common_params.as_ref())?;
720720

721+
let best_block_hash = chain.chain_info().best_block_hash;
722+
let block_id = BlockId::Hash(best_block_hash);
721723
let fetch_seq = |p: &Public| {
722724
let address = public_to_address(p);
723-
let a = chain.latest_regular_key_owner(&address).unwrap_or(address);
724-
chain.latest_seq(&a)
725+
let a = chain.regular_key_owner(&address, block_id.into()).unwrap_or(address);
726+
chain.seq(&a, block_id).expect("Read from best block")
725727
};
726728

727729
{
@@ -903,13 +905,15 @@ impl MinerService for Miner {
903905

904906
// ...and at the end remove the old ones
905907
{
908+
let current_block_hash = chain.chain_info().best_block_hash;
909+
let block_id = BlockId::Hash(current_block_hash);
906910
let fetch_account = |p: &Public| {
907911
let address = public_to_address(p);
908-
let a = chain.latest_regular_key_owner(&address).unwrap_or(address);
912+
let a = chain.regular_key_owner(&address, block_id.into()).unwrap_or(address);
909913

910914
AccountDetails {
911-
seq: chain.latest_seq(&a),
912-
balance: chain.latest_balance(&a),
915+
seq: chain.seq(&a, block_id).expect("Read from best block"),
916+
balance: chain.balance(&a, block_id.into()).expect("Read from best block"),
913917
}
914918
};
915919
let current_block_number = chain.chain_info().best_block_number;

0 commit comments

Comments
 (0)