mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-09 20:59:23 +00:00
Have Tributary's add_transaction return a proper error
Modifies main.rs to properly handle the returned error.
This commit is contained in:
@@ -105,11 +105,9 @@ fn invalid_block() {
|
||||
{
|
||||
// Add a valid transaction
|
||||
let (_, mut blockchain) = new_blockchain(genesis, &[tx.1.signer]);
|
||||
assert!(blockchain.add_transaction::<N>(
|
||||
true,
|
||||
Transaction::Application(tx.clone()),
|
||||
validators.clone()
|
||||
));
|
||||
blockchain
|
||||
.add_transaction::<N>(true, Transaction::Application(tx.clone()), validators.clone())
|
||||
.unwrap();
|
||||
let mut block = blockchain.build_block::<N>(validators.clone());
|
||||
assert_eq!(block.header.transactions, merkle(&[tx.hash()]));
|
||||
blockchain.verify_block::<N>(&block, validators.clone(), false).unwrap();
|
||||
@@ -130,11 +128,9 @@ fn invalid_block() {
|
||||
{
|
||||
// Invalid signature
|
||||
let (_, mut blockchain) = new_blockchain(genesis, &[tx.1.signer]);
|
||||
assert!(blockchain.add_transaction::<N>(
|
||||
true,
|
||||
Transaction::Application(tx),
|
||||
validators.clone()
|
||||
));
|
||||
blockchain
|
||||
.add_transaction::<N>(true, Transaction::Application(tx), validators.clone())
|
||||
.unwrap();
|
||||
let mut block = blockchain.build_block::<N>(validators.clone());
|
||||
blockchain.verify_block::<N>(&block, validators.clone(), false).unwrap();
|
||||
match &mut block.transactions[0] {
|
||||
@@ -170,11 +166,9 @@ fn signed_transaction() {
|
||||
panic!("tendermint tx found");
|
||||
};
|
||||
let next_nonce = blockchain.next_nonce(signer).unwrap();
|
||||
assert!(blockchain.add_transaction::<N>(
|
||||
true,
|
||||
Transaction::Application(tx),
|
||||
validators.clone()
|
||||
));
|
||||
blockchain
|
||||
.add_transaction::<N>(true, Transaction::Application(tx), validators.clone())
|
||||
.unwrap();
|
||||
assert_eq!(next_nonce + 1, blockchain.next_nonce(signer).unwrap());
|
||||
}
|
||||
let block = blockchain.build_block::<N>(validators.clone());
|
||||
@@ -363,11 +357,9 @@ async fn tendermint_evidence_tx() {
|
||||
let Transaction::Tendermint(tx) = tx else {
|
||||
panic!("non-tendermint tx found");
|
||||
};
|
||||
assert!(blockchain.add_transaction::<N>(
|
||||
true,
|
||||
Transaction::Tendermint(tx),
|
||||
validators.clone()
|
||||
));
|
||||
blockchain
|
||||
.add_transaction::<N>(true, Transaction::Tendermint(tx), validators.clone())
|
||||
.unwrap();
|
||||
}
|
||||
let block = blockchain.build_block::<N>(validators.clone());
|
||||
assert_eq!(blockchain.tip(), tip);
|
||||
@@ -475,7 +467,7 @@ async fn block_tx_ordering() {
|
||||
let signed_tx = Transaction::Application(SignedTx::Signed(Box::new(
|
||||
crate::tests::signed_transaction(&mut OsRng, genesis, &key, i),
|
||||
)));
|
||||
assert!(blockchain.add_transaction::<N>(true, signed_tx.clone(), validators.clone()));
|
||||
blockchain.add_transaction::<N>(true, signed_tx.clone(), validators.clone()).unwrap();
|
||||
mempool.push(signed_tx);
|
||||
|
||||
let unsigned_tx = Transaction::Tendermint(
|
||||
@@ -485,7 +477,7 @@ async fn block_tx_ordering() {
|
||||
)
|
||||
.await,
|
||||
);
|
||||
assert!(blockchain.add_transaction::<N>(true, unsigned_tx.clone(), validators.clone()));
|
||||
blockchain.add_transaction::<N>(true, unsigned_tx.clone(), validators.clone()).unwrap();
|
||||
mempool.push(unsigned_tx);
|
||||
|
||||
let provided_tx =
|
||||
|
||||
@@ -10,7 +10,7 @@ use tendermint::ext::Commit;
|
||||
use serai_db::MemDb;
|
||||
|
||||
use crate::{
|
||||
transaction::Transaction as TransactionTrait,
|
||||
transaction::{TransactionError, Transaction as TransactionTrait},
|
||||
tendermint::{TendermintBlock, Validators, Signer, TendermintNetwork},
|
||||
ACCOUNT_MEMPOOL_LIMIT, Transaction, Mempool,
|
||||
tests::{SignedTransaction, signed_transaction, p2p::DummyP2p, random_evidence_tx},
|
||||
@@ -43,69 +43,85 @@ async fn mempool_addition() {
|
||||
|
||||
// Add TX 0
|
||||
let mut blockchain_next_nonces = HashMap::from([(signer, 0)]);
|
||||
assert!(mempool.add::<N>(
|
||||
&blockchain_next_nonces,
|
||||
true,
|
||||
Transaction::Application(first_tx.clone()),
|
||||
validators.clone(),
|
||||
unsigned_in_chain,
|
||||
commit,
|
||||
));
|
||||
assert!(mempool
|
||||
.add::<N>(
|
||||
&blockchain_next_nonces,
|
||||
true,
|
||||
Transaction::Application(first_tx.clone()),
|
||||
validators.clone(),
|
||||
unsigned_in_chain,
|
||||
commit,
|
||||
)
|
||||
.unwrap());
|
||||
assert_eq!(mempool.next_nonce(&signer), Some(1));
|
||||
|
||||
// add a tendermint evidence tx
|
||||
let evidence_tx =
|
||||
random_evidence_tx::<N>(Signer::new(genesis, key.clone()).into(), TendermintBlock(vec![]))
|
||||
.await;
|
||||
assert!(mempool.add::<N>(
|
||||
&blockchain_next_nonces,
|
||||
true,
|
||||
Transaction::Tendermint(evidence_tx.clone()),
|
||||
validators.clone(),
|
||||
unsigned_in_chain,
|
||||
commit,
|
||||
));
|
||||
assert!(mempool
|
||||
.add::<N>(
|
||||
&blockchain_next_nonces,
|
||||
true,
|
||||
Transaction::Tendermint(evidence_tx.clone()),
|
||||
validators.clone(),
|
||||
unsigned_in_chain,
|
||||
commit,
|
||||
)
|
||||
.unwrap());
|
||||
|
||||
// Test reloading works
|
||||
assert_eq!(mempool, Mempool::new(db, genesis));
|
||||
|
||||
// Adding it again should fail
|
||||
assert!(!mempool.add::<N>(
|
||||
&blockchain_next_nonces,
|
||||
true,
|
||||
Transaction::Application(first_tx.clone()),
|
||||
validators.clone(),
|
||||
unsigned_in_chain,
|
||||
commit,
|
||||
));
|
||||
assert!(!mempool.add::<N>(
|
||||
&blockchain_next_nonces,
|
||||
true,
|
||||
Transaction::Tendermint(evidence_tx.clone()),
|
||||
validators.clone(),
|
||||
unsigned_in_chain,
|
||||
commit,
|
||||
));
|
||||
// Adding them again should fail
|
||||
assert_eq!(
|
||||
mempool.add::<N>(
|
||||
&blockchain_next_nonces,
|
||||
true,
|
||||
Transaction::Application(first_tx.clone()),
|
||||
validators.clone(),
|
||||
unsigned_in_chain,
|
||||
commit,
|
||||
),
|
||||
Err(TransactionError::InvalidNonce)
|
||||
);
|
||||
assert_eq!(
|
||||
mempool.add::<N>(
|
||||
&blockchain_next_nonces,
|
||||
true,
|
||||
Transaction::Tendermint(evidence_tx.clone()),
|
||||
validators.clone(),
|
||||
unsigned_in_chain,
|
||||
commit,
|
||||
),
|
||||
Ok(false)
|
||||
);
|
||||
|
||||
// Do the same with the next nonce
|
||||
let second_tx = signed_transaction(&mut OsRng, genesis, &key, 1);
|
||||
assert!(mempool.add::<N>(
|
||||
&blockchain_next_nonces,
|
||||
true,
|
||||
Transaction::Application(second_tx.clone()),
|
||||
validators.clone(),
|
||||
unsigned_in_chain,
|
||||
commit,
|
||||
));
|
||||
assert_eq!(
|
||||
mempool.add::<N>(
|
||||
&blockchain_next_nonces,
|
||||
true,
|
||||
Transaction::Application(second_tx.clone()),
|
||||
validators.clone(),
|
||||
unsigned_in_chain,
|
||||
commit,
|
||||
),
|
||||
Ok(true)
|
||||
);
|
||||
assert_eq!(mempool.next_nonce(&signer), Some(2));
|
||||
assert!(!mempool.add::<N>(
|
||||
&blockchain_next_nonces,
|
||||
true,
|
||||
Transaction::Application(second_tx.clone()),
|
||||
validators.clone(),
|
||||
unsigned_in_chain,
|
||||
commit,
|
||||
));
|
||||
assert_eq!(
|
||||
mempool.add::<N>(
|
||||
&blockchain_next_nonces,
|
||||
true,
|
||||
Transaction::Application(second_tx.clone()),
|
||||
validators.clone(),
|
||||
unsigned_in_chain,
|
||||
commit,
|
||||
),
|
||||
Err(TransactionError::InvalidNonce)
|
||||
);
|
||||
|
||||
// If the mempool doesn't have a nonce for an account, it should successfully use the
|
||||
// blockchain's
|
||||
@@ -114,14 +130,16 @@ async fn mempool_addition() {
|
||||
let second_signer = tx.1.signer;
|
||||
assert_eq!(mempool.next_nonce(&second_signer), None);
|
||||
blockchain_next_nonces.insert(second_signer, 2);
|
||||
assert!(mempool.add::<N>(
|
||||
&blockchain_next_nonces,
|
||||
true,
|
||||
Transaction::Application(tx.clone()),
|
||||
validators.clone(),
|
||||
unsigned_in_chain,
|
||||
commit
|
||||
));
|
||||
assert!(mempool
|
||||
.add::<N>(
|
||||
&blockchain_next_nonces,
|
||||
true,
|
||||
Transaction::Application(tx.clone()),
|
||||
validators.clone(),
|
||||
unsigned_in_chain,
|
||||
commit
|
||||
)
|
||||
.unwrap());
|
||||
assert_eq!(mempool.next_nonce(&second_signer), Some(3));
|
||||
|
||||
// Getting a block should work
|
||||
@@ -159,22 +177,32 @@ fn too_many_mempool() {
|
||||
|
||||
// We should be able to add transactions up to the limit
|
||||
for i in 0 .. ACCOUNT_MEMPOOL_LIMIT {
|
||||
assert!(mempool.add::<N>(
|
||||
assert!(mempool
|
||||
.add::<N>(
|
||||
&HashMap::from([(signer, 0)]),
|
||||
false,
|
||||
Transaction::Application(signed_transaction(&mut OsRng, genesis, &key, i)),
|
||||
validators.clone(),
|
||||
unsigned_in_chain,
|
||||
commit,
|
||||
)
|
||||
.unwrap());
|
||||
}
|
||||
// Yet adding more should fail
|
||||
assert_eq!(
|
||||
mempool.add::<N>(
|
||||
&HashMap::from([(signer, 0)]),
|
||||
false,
|
||||
Transaction::Application(signed_transaction(&mut OsRng, genesis, &key, i)),
|
||||
Transaction::Application(signed_transaction(
|
||||
&mut OsRng,
|
||||
genesis,
|
||||
&key,
|
||||
ACCOUNT_MEMPOOL_LIMIT
|
||||
)),
|
||||
validators.clone(),
|
||||
unsigned_in_chain,
|
||||
commit,
|
||||
));
|
||||
}
|
||||
// Yet adding more should fail
|
||||
assert!(!mempool.add::<N>(
|
||||
&HashMap::from([(signer, 0)]),
|
||||
false,
|
||||
Transaction::Application(signed_transaction(&mut OsRng, genesis, &key, ACCOUNT_MEMPOOL_LIMIT)),
|
||||
validators.clone(),
|
||||
unsigned_in_chain,
|
||||
commit,
|
||||
));
|
||||
),
|
||||
Err(TransactionError::TooManyInMempool)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user