Embed the mempool into the Blockchain

This commit is contained in:
Luke Parker
2023-04-13 09:47:14 -04:00
parent 03a6470a5b
commit a509dbfad6
3 changed files with 73 additions and 23 deletions

View File

@@ -24,13 +24,21 @@ impl<T: Transaction> Mempool<T> {
) -> bool {
match tx.kind() {
TransactionKind::Signed(Signed { signer, nonce, .. }) => {
// If the mempool doesn't have a nonce tracked, grab it from the blockchain
if !self.next_nonces.contains_key(signer) {
let Some(blockchain_next_nonces) = blockchain_next_nonces.get(signer).cloned() else {
// Not a participant
return false;
};
self.next_nonces.insert(*signer, blockchain_next_nonces);
// Get the nonce from the blockchain
let Some(blockchain_next_nonce) = blockchain_next_nonces.get(signer).cloned() else {
// Not a participant
return false;
};
// If the blockchain's nonce is greater than the mempool's, use it
// Default to true so if the mempool hasn't tracked this nonce yet, it'll be inserted
let mut blockchain_is_greater = true;
if let Some(mempool_next_nonce) = self.next_nonces.get(signer) {
blockchain_is_greater = blockchain_next_nonce > *mempool_next_nonce;
}
if blockchain_is_greater {
self.next_nonces.insert(*signer, blockchain_next_nonce);
}
if verify_transaction(&tx, self.genesis, &mut HashSet::new(), &mut self.next_nonces)