Have Tributary's add_transaction return a proper error

Modifies main.rs to properly handle the returned error.
This commit is contained in:
Luke Parker
2023-10-14 21:50:11 -04:00
parent 584943d1e9
commit 19e90b28b0
11 changed files with 163 additions and 133 deletions

View File

@@ -10,7 +10,7 @@ use tendermint::ext::{Network, Commit};
use crate::{
ReadWrite, ProvidedError, ProvidedTransactions, BlockError, Block, Mempool, Transaction,
transaction::{Signed, TransactionKind, Transaction as TransactionTrait},
transaction::{Signed, TransactionKind, TransactionError, Transaction as TransactionTrait},
};
#[derive(Debug)]
@@ -165,7 +165,7 @@ impl<D: Db, T: TransactionTrait> Blockchain<D, T> {
internal: bool,
tx: Transaction<T>,
schema: N::SignatureScheme,
) -> bool {
) -> Result<bool, TransactionError> {
let db = self.db.as_ref().unwrap();
let genesis = self.genesis;

View File

@@ -256,10 +256,10 @@ impl<D: Db, T: TransactionTrait, P: P2p> Tributary<D, T, P> {
self.network.blockchain.read().await.next_nonce(signer)
}
// Returns if the transaction was new and valid.
// Returns Ok(true) if new, Ok(false) if an already present unsigned, or the error.
// Safe to be &self since the only meaningful usage of self is self.network.blockchain which
// successfully acquires its own write lock
pub async fn add_transaction(&self, tx: T) -> bool {
pub async fn add_transaction(&self, tx: T) -> Result<bool, TransactionError> {
let tx = Transaction::Application(tx);
let mut to_broadcast = vec![TRANSACTION_MESSAGE];
tx.write(&mut to_broadcast).unwrap();
@@ -268,7 +268,7 @@ impl<D: Db, T: TransactionTrait, P: P2p> Tributary<D, T, P> {
tx,
self.network.signature_scheme(),
);
if res {
if res == Ok(true) {
self.network.p2p.broadcast(self.genesis, to_broadcast).await;
}
res
@@ -339,8 +339,8 @@ impl<D: Db, T: TransactionTrait, P: P2p> Tributary<D, T, P> {
tx,
self.network.signature_scheme(),
);
log::debug!("received transaction message. valid new transaction: {res}");
res
log::debug!("received transaction message. valid new transaction: {res:?}");
res == Ok(true)
}
Some(&TENDERMINT_MESSAGE) => {

View File

@@ -8,7 +8,9 @@ use tendermint::ext::{Network, Commit};
use crate::{
ACCOUNT_MEMPOOL_LIMIT, ReadWrite,
transaction::{Signed, TransactionKind, Transaction as TransactionTrait, verify_transaction},
transaction::{
Signed, TransactionKind, TransactionError, Transaction as TransactionTrait, verify_transaction,
},
tendermint::tx::verify_tendermint_tx,
Transaction,
};
@@ -92,7 +94,7 @@ impl<D: Db, T: TransactionTrait> Mempool<D, T> {
res
}
/// Returns true if this is a valid, new transaction.
// Returns Ok(true) if new, Ok(false) if an already present unsigned, or the error.
pub(crate) fn add<N: Network>(
&mut self,
blockchain_next_nonces: &HashMap<<Ristretto as Ciphersuite>::G, u32>,
@@ -101,7 +103,7 @@ impl<D: Db, T: TransactionTrait> Mempool<D, T> {
schema: N::SignatureScheme,
unsigned_in_chain: impl Fn([u8; 32]) -> bool,
commit: impl Fn(u32) -> Option<Commit<N::SignatureScheme>>,
) -> bool {
) -> Result<bool, TransactionError> {
match &tx {
Transaction::Tendermint(tendermint_tx) => {
// All Tendermint transactions should be unsigned
@@ -109,13 +111,11 @@ impl<D: Db, T: TransactionTrait> Mempool<D, T> {
// check we have the tx in the pool/chain
if self.unsigned_already_exist(tx.hash(), unsigned_in_chain) {
return false;
return Ok(false);
}
// verify the tx
if verify_tendermint_tx::<N>(tendermint_tx, schema, commit).is_err() {
return false;
}
verify_tendermint_tx::<N>(tendermint_tx, schema, commit)?;
}
Transaction::Application(app_tx) => {
match app_tx.kind() {
@@ -123,7 +123,7 @@ impl<D: Db, T: TransactionTrait> Mempool<D, T> {
// Get the nonce from the blockchain
let Some(blockchain_next_nonce) = blockchain_next_nonces.get(signer).cloned() else {
// Not a participant
return false;
Err(TransactionError::InvalidSigner)?
};
// If the blockchain's nonce is greater than the mempool's, use it
@@ -140,32 +140,28 @@ impl<D: Db, T: TransactionTrait> Mempool<D, T> {
// If we have too many transactions from this sender, don't add this yet UNLESS we are
// this sender
if !internal && (nonce >= &(blockchain_next_nonce + ACCOUNT_MEMPOOL_LIMIT)) {
return false;
Err(TransactionError::TooManyInMempool)?;
}
if verify_transaction(app_tx, self.genesis, &mut self.next_nonces).is_err() {
return false;
}
verify_transaction(app_tx, self.genesis, &mut self.next_nonces)?;
debug_assert_eq!(self.next_nonces[signer], nonce + 1);
}
TransactionKind::Unsigned => {
// check we have the tx in the pool/chain
if self.unsigned_already_exist(tx.hash(), unsigned_in_chain) {
return false;
return Ok(false);
}
if app_tx.verify().is_err() {
return false;
}
app_tx.verify()?;
}
TransactionKind::Provided(_) => return false,
TransactionKind::Provided(_) => Err(TransactionError::ProvidedAddedToMempool)?,
}
}
}
// Save the TX to the pool
self.save_tx(tx);
true
Ok(true)
}
// Returns None if the mempool doesn't have a nonce tracked.

View File

@@ -349,7 +349,8 @@ impl<D: Db, T: TransactionTrait, P: P2p> Network for TendermintNetwork<D, T, P>
true,
Transaction::Tendermint(tx),
self.signature_scheme(),
) {
) == Ok(true)
{
self.p2p.broadcast(signer.genesis, to_broadcast).await;
}
}

View File

@@ -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 =

View File

@@ -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)
);
}

View File

@@ -31,6 +31,12 @@ pub enum TransactionError {
/// Transaction's content is invalid.
#[error("transaction content is invalid")]
InvalidContent,
/// Transaction's signer has too many transactions in the mempool.
#[error("signer has too many transactions in the mempool")]
TooManyInMempool,
/// Provided Transaction added to mempool.
#[error("provided transaction added to mempool")]
ProvidedAddedToMempool,
}
/// Data for a signed transaction.