zebra/zebra-consensus/src/error.rs

212 lines
7.4 KiB
Rust
Raw Normal View History

//! Errors that can occur when checking consensus rules.
//!
//! Each error variant corresponds to a consensus rule, so enumerating
//! all possible verification failures enumerates the consensus rules we
//! implement, and ensures that we don't reject blocks or transactions
//! for a non-enumerated reason.
use thiserror::Error;
Reject a mempool transaction if it has internal spend conflicts (#2843) * Reorder imports to follow convention Place the imports from `std` at the top. * Add transaction errors for double spends Add a variant for each pool. They represent a double spend inside a transaction. * Add `check::spend_conflicts` implementation Checks if a transaction has spend conflicts, i.e., if a transaction spends a UTXO more than once or if it reveals a nullifier more than once. * Reject transactions with internal spend conflicts The transaction verifier should reject transactions that spend the same transparent UTXO or that reveal the same nullifier. * Add transparent spend consensus rule Add it to the documentation to help with understanding and auditing it. Co-authored-by: teor <teor@riseup.net> * Use different nullifiers by default Don't use the same nullifier twice when mocking a `sprout::JoinSplitData` because it will lead to an invalid transaction. * Test transactions with repeated spend outpoints Since that represents a spend conflict, they should be rejected. * Test duplicate nullifiers in joinsplit Check if a mock transaction with a joinsplit that reveals the same nullifier twice is rejected. * Test duplicate nullifiers across joinsplits Check if a duplicate nullifier in two different joinsplits in the same transaction is rejected. * Test V4 transaction with duplicate Sapling spend Check if a V4 transaction that has a duplicate Sapling spend is rejected. * Test V5 transaction with duplicate Sapling spend Check if a V5 transaction that has a duplicate Sapling spend is rejected. * Test V5 transaction with duplicate Orchard actions Check if a V5 transaction that has duplicate Orchard actions is rejected by the transaction verifier. Co-authored-by: teor <teor@riseup.net>
2021-10-27 19:49:28 -07:00
use zebra_chain::{orchard, sapling, sprout, transparent};
use crate::BoxError;
Send crawled transaction IDs to downloader (#2801) * Rename type parameter to be more explicit Replace the single letter with a proper name. * Remove imports for `Request` and `Response` The type names will conflict with the ones for the mempool service. * Attach `Mempool` service to the `Crawler` Add a field to the `Crawler` type to store a way to access the `Mempool` service. * Forward crawled transactions to downloader The crawled transactions are now sent to the transaction downloader and verifier, to be included in the mempool. * Derive `Eq` and `PartialEq` for `mempool::Request` Make it simpler to use the `MockService::expect_request` method. * Test if crawled transactions are downloaded Create some dummy crawled transactions, and let the crawler discover them. Then check if they are forwarded to the mempool to be downloaded and verified. * Don't send empty transaction ID list to downloader Ignore response from peers that don't provide any crawled transactions. * Log errors when forwarding crawled transaction IDs Calling the Mempool service should not fail, so if an error happens it should be visible. However, errors when downloading individual transactions can happen from time to time, so there's no need for them to be very visible. * Document existing `mempool::Crawler` test Provide some depth as to what the test expect from the crawler's behavior. * Refactor to create `setup_crawler` helper function Make it easier to reuse the common test setup code. * Simplify code to expect requests Now that `zebra_network::Request` implement `Eq`, the call can be simplified into `expect_request`. * Refactor to create `respond_with_transaction_ids` A helper function that checks for a network crawl request and responds with the given list of crawled transaction IDs. * Refactor to create `crawler_iterator` helper A function to intercept and respond to the fanned-out requests sent during a single crawl iteration. * Refactor to create `respond_to_queue_request` Reduce the repeated code necessary to intercept and reply to a request for queuing transactions to be downloaded. * Add `respond_to_queue_request_with_error` helper Intercepts a mempool request to queue transactions to be downloaded, and responds with an error, simulating an internal problem in the mempool service implementation. * Derive `Arbitrary` for `NetworkUpgrade` This is required for deriving `Arbitrary` for some error types. * Derive `Arbitrary` for `TransactionError` Allow random transaction errors to be generated for property tests. * Derive `Arbitrary` for `MempoolError` Allow random Mempool errors to be generated for property tests. * Test if errors don't stop the mempool crawler The crawler should be robust enough to continue operating even if the mempool service fails to download transactions or even fails to handle requests to enqueue transactions. * Reduce the log level for download errors They should happen regularly, so there's no need to have them with a high visibility level. Co-authored-by: teor <teor@riseup.net> * Stop crawler if service stops If `Mempool::poll_ready` returns an error, it's because the mempool service has stopped and can't handle any requests, so the crawler should stop as well. Co-authored-by: teor <teor@riseup.net> Co-authored-by: Conrado Gouvea <conrado@zfnd.org>
2021-10-04 17:55:42 -07:00
#[cfg(any(test, feature = "proptest-impl"))]
use proptest_derive::Arbitrary;
#[derive(Error, Copy, Clone, Debug, PartialEq, Eq)]
pub enum SubsidyError {
#[error("no coinbase transaction in block")]
NoCoinbase,
#[error("founders reward output not found")]
FoundersRewardNotFound,
#[error("funding stream output not found")]
FundingStreamNotFound,
}
#[derive(Error, Clone, Debug, PartialEq, Eq)]
Send crawled transaction IDs to downloader (#2801) * Rename type parameter to be more explicit Replace the single letter with a proper name. * Remove imports for `Request` and `Response` The type names will conflict with the ones for the mempool service. * Attach `Mempool` service to the `Crawler` Add a field to the `Crawler` type to store a way to access the `Mempool` service. * Forward crawled transactions to downloader The crawled transactions are now sent to the transaction downloader and verifier, to be included in the mempool. * Derive `Eq` and `PartialEq` for `mempool::Request` Make it simpler to use the `MockService::expect_request` method. * Test if crawled transactions are downloaded Create some dummy crawled transactions, and let the crawler discover them. Then check if they are forwarded to the mempool to be downloaded and verified. * Don't send empty transaction ID list to downloader Ignore response from peers that don't provide any crawled transactions. * Log errors when forwarding crawled transaction IDs Calling the Mempool service should not fail, so if an error happens it should be visible. However, errors when downloading individual transactions can happen from time to time, so there's no need for them to be very visible. * Document existing `mempool::Crawler` test Provide some depth as to what the test expect from the crawler's behavior. * Refactor to create `setup_crawler` helper function Make it easier to reuse the common test setup code. * Simplify code to expect requests Now that `zebra_network::Request` implement `Eq`, the call can be simplified into `expect_request`. * Refactor to create `respond_with_transaction_ids` A helper function that checks for a network crawl request and responds with the given list of crawled transaction IDs. * Refactor to create `crawler_iterator` helper A function to intercept and respond to the fanned-out requests sent during a single crawl iteration. * Refactor to create `respond_to_queue_request` Reduce the repeated code necessary to intercept and reply to a request for queuing transactions to be downloaded. * Add `respond_to_queue_request_with_error` helper Intercepts a mempool request to queue transactions to be downloaded, and responds with an error, simulating an internal problem in the mempool service implementation. * Derive `Arbitrary` for `NetworkUpgrade` This is required for deriving `Arbitrary` for some error types. * Derive `Arbitrary` for `TransactionError` Allow random transaction errors to be generated for property tests. * Derive `Arbitrary` for `MempoolError` Allow random Mempool errors to be generated for property tests. * Test if errors don't stop the mempool crawler The crawler should be robust enough to continue operating even if the mempool service fails to download transactions or even fails to handle requests to enqueue transactions. * Reduce the log level for download errors They should happen regularly, so there's no need to have them with a high visibility level. Co-authored-by: teor <teor@riseup.net> * Stop crawler if service stops If `Mempool::poll_ready` returns an error, it's because the mempool service has stopped and can't handle any requests, so the crawler should stop as well. Co-authored-by: teor <teor@riseup.net> Co-authored-by: Conrado Gouvea <conrado@zfnd.org>
2021-10-04 17:55:42 -07:00
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
pub enum TransactionError {
#[error("first transaction must be coinbase")]
CoinbasePosition,
#[error("coinbase input found in non-coinbase transaction")]
CoinbaseAfterFirst,
#[error("coinbase transaction MUST NOT have any transparent (PrevOut) inputs")]
CoinbaseHasPrevOutInput,
#[error("coinbase transaction MUST NOT have any JoinSplit descriptions")]
CoinbaseHasJoinSplit,
#[error("coinbase transaction MUST NOT have any Spend descriptions")]
CoinbaseHasSpend,
#[error("coinbase transaction MUST NOT have any Output descriptions pre-Heartwood")]
CoinbaseHasOutputPreHeartwood,
#[error("coinbase transaction MUST NOT have the EnableSpendsOrchard flag set")]
CoinbaseHasEnableSpendsOrchard,
#[error("coinbase transaction Sapling or Orchard outputs MUST be decryptable with an all-zero outgoing viewing key")]
CoinbaseOutputsNotDecryptable,
#[error("coinbase inputs MUST NOT exist in mempool")]
CoinbaseInMempool,
#[error("coinbase transaction failed subsidy validation")]
Send crawled transaction IDs to downloader (#2801) * Rename type parameter to be more explicit Replace the single letter with a proper name. * Remove imports for `Request` and `Response` The type names will conflict with the ones for the mempool service. * Attach `Mempool` service to the `Crawler` Add a field to the `Crawler` type to store a way to access the `Mempool` service. * Forward crawled transactions to downloader The crawled transactions are now sent to the transaction downloader and verifier, to be included in the mempool. * Derive `Eq` and `PartialEq` for `mempool::Request` Make it simpler to use the `MockService::expect_request` method. * Test if crawled transactions are downloaded Create some dummy crawled transactions, and let the crawler discover them. Then check if they are forwarded to the mempool to be downloaded and verified. * Don't send empty transaction ID list to downloader Ignore response from peers that don't provide any crawled transactions. * Log errors when forwarding crawled transaction IDs Calling the Mempool service should not fail, so if an error happens it should be visible. However, errors when downloading individual transactions can happen from time to time, so there's no need for them to be very visible. * Document existing `mempool::Crawler` test Provide some depth as to what the test expect from the crawler's behavior. * Refactor to create `setup_crawler` helper function Make it easier to reuse the common test setup code. * Simplify code to expect requests Now that `zebra_network::Request` implement `Eq`, the call can be simplified into `expect_request`. * Refactor to create `respond_with_transaction_ids` A helper function that checks for a network crawl request and responds with the given list of crawled transaction IDs. * Refactor to create `crawler_iterator` helper A function to intercept and respond to the fanned-out requests sent during a single crawl iteration. * Refactor to create `respond_to_queue_request` Reduce the repeated code necessary to intercept and reply to a request for queuing transactions to be downloaded. * Add `respond_to_queue_request_with_error` helper Intercepts a mempool request to queue transactions to be downloaded, and responds with an error, simulating an internal problem in the mempool service implementation. * Derive `Arbitrary` for `NetworkUpgrade` This is required for deriving `Arbitrary` for some error types. * Derive `Arbitrary` for `TransactionError` Allow random transaction errors to be generated for property tests. * Derive `Arbitrary` for `MempoolError` Allow random Mempool errors to be generated for property tests. * Test if errors don't stop the mempool crawler The crawler should be robust enough to continue operating even if the mempool service fails to download transactions or even fails to handle requests to enqueue transactions. * Reduce the log level for download errors They should happen regularly, so there's no need to have them with a high visibility level. Co-authored-by: teor <teor@riseup.net> * Stop crawler if service stops If `Mempool::poll_ready` returns an error, it's because the mempool service has stopped and can't handle any requests, so the crawler should stop as well. Co-authored-by: teor <teor@riseup.net> Co-authored-by: Conrado Gouvea <conrado@zfnd.org>
2021-10-04 17:55:42 -07:00
#[cfg_attr(any(test, feature = "proptest-impl"), proptest(skip))]
Subsidy(#[from] SubsidyError),
#[error("transaction version number MUST be >= 4")]
WrongVersion,
Reject V5 transactions before NU5 activation (#2285) * Add a `Transaction::version` getter Returns the version of the transaction as a `u32`. * Add `Transaction::is_overwintered` helper method Returns if the `fOverwintered` flag should be set for the transaction's version. * Use new helpers to serialize transaction version Reduce the repeated code and make it less error-prone with future changes. * Add getter methods to `transaction::Request` type Refactor to move the type deconstruction code into the `Request` type. The main objective is to make it easier to split the call handler into methods that receive the request directly. * Refactor to create `verify_v4_transaction` helper Split the code specific to V4 transactions into a separate helper method. * Create `verify_v5_transaction` helper method Prepare a separate method to have the validation code. * Add `UnsupportedByNetworkUpgrade` error variant An error for when a transaction's version isn't supported by the network upgrade of the block it's included or for the current network upgrade if the transaction is for the mempool. * Verify a V5 transaction's network upgrade For now, only NU5 supports V5 transactions. * Test that V5 transaction is rejected on Canopy Create a fake V5 transaction and try to verify it using a block height from Canopy's activation. The verifier should reject the transaction with an error saying that the network upgrade does not support that transaction version. * Test if V5 tx. is accepted after NU5 activation Create a fake V5 transaction and pretend it is placed in a block that has a height after the NU5 activation. The test should succeed, but since the NU5 activation height has not been specified yet (neither for the testnet nor the mainnet), for now this test is marked as `should_panic`. * Add `TODO` comment to the code Add more detail to what's left to do, and link to the appropriate PRs. * Use `u32` to store transaction version Use a type consistent with how the version is specified. Co-authored-by: teor <teor@riseup.net> Co-authored-by: teor <teor@riseup.net>
2021-06-14 17:15:59 -07:00
#[error("transaction version {0} not supported by the network upgrade {1:?}")]
UnsupportedByNetworkUpgrade(u32, zebra_chain::parameters::NetworkUpgrade),
#[error("must have at least one input: transparent, shielded spend, or joinsplit")]
NoInputs,
#[error("must have at least one output: transparent, shielded output, or joinsplit")]
NoOutputs,
#[error("if there are no Spends or Outputs, the value balance MUST be 0.")]
BadBalance,
2020-10-28 17:22:25 -07:00
#[error("could not verify a transparent script")]
Send crawled transaction IDs to downloader (#2801) * Rename type parameter to be more explicit Replace the single letter with a proper name. * Remove imports for `Request` and `Response` The type names will conflict with the ones for the mempool service. * Attach `Mempool` service to the `Crawler` Add a field to the `Crawler` type to store a way to access the `Mempool` service. * Forward crawled transactions to downloader The crawled transactions are now sent to the transaction downloader and verifier, to be included in the mempool. * Derive `Eq` and `PartialEq` for `mempool::Request` Make it simpler to use the `MockService::expect_request` method. * Test if crawled transactions are downloaded Create some dummy crawled transactions, and let the crawler discover them. Then check if they are forwarded to the mempool to be downloaded and verified. * Don't send empty transaction ID list to downloader Ignore response from peers that don't provide any crawled transactions. * Log errors when forwarding crawled transaction IDs Calling the Mempool service should not fail, so if an error happens it should be visible. However, errors when downloading individual transactions can happen from time to time, so there's no need for them to be very visible. * Document existing `mempool::Crawler` test Provide some depth as to what the test expect from the crawler's behavior. * Refactor to create `setup_crawler` helper function Make it easier to reuse the common test setup code. * Simplify code to expect requests Now that `zebra_network::Request` implement `Eq`, the call can be simplified into `expect_request`. * Refactor to create `respond_with_transaction_ids` A helper function that checks for a network crawl request and responds with the given list of crawled transaction IDs. * Refactor to create `crawler_iterator` helper A function to intercept and respond to the fanned-out requests sent during a single crawl iteration. * Refactor to create `respond_to_queue_request` Reduce the repeated code necessary to intercept and reply to a request for queuing transactions to be downloaded. * Add `respond_to_queue_request_with_error` helper Intercepts a mempool request to queue transactions to be downloaded, and responds with an error, simulating an internal problem in the mempool service implementation. * Derive `Arbitrary` for `NetworkUpgrade` This is required for deriving `Arbitrary` for some error types. * Derive `Arbitrary` for `TransactionError` Allow random transaction errors to be generated for property tests. * Derive `Arbitrary` for `MempoolError` Allow random Mempool errors to be generated for property tests. * Test if errors don't stop the mempool crawler The crawler should be robust enough to continue operating even if the mempool service fails to download transactions or even fails to handle requests to enqueue transactions. * Reduce the log level for download errors They should happen regularly, so there's no need to have them with a high visibility level. Co-authored-by: teor <teor@riseup.net> * Stop crawler if service stops If `Mempool::poll_ready` returns an error, it's because the mempool service has stopped and can't handle any requests, so the crawler should stop as well. Co-authored-by: teor <teor@riseup.net> Co-authored-by: Conrado Gouvea <conrado@zfnd.org>
2021-10-04 17:55:42 -07:00
#[cfg_attr(any(test, feature = "proptest-impl"), proptest(skip))]
Script(#[from] zebra_script::Error),
#[error("spend description cv and rk MUST NOT be of small order")]
SmallOrder,
// XXX change this when we align groth16 verifier errors with bellman
// and add a from annotation when the error type is more precise
#[error("spend proof MUST be valid given a primary input formed from the other fields except spendAuthSig")]
2020-10-28 17:22:25 -07:00
Groth16,
#[error(
"Sprout joinSplitSig MUST represent a valid signature under joinSplitPubKey of dataToBeSigned"
)]
Send crawled transaction IDs to downloader (#2801) * Rename type parameter to be more explicit Replace the single letter with a proper name. * Remove imports for `Request` and `Response` The type names will conflict with the ones for the mempool service. * Attach `Mempool` service to the `Crawler` Add a field to the `Crawler` type to store a way to access the `Mempool` service. * Forward crawled transactions to downloader The crawled transactions are now sent to the transaction downloader and verifier, to be included in the mempool. * Derive `Eq` and `PartialEq` for `mempool::Request` Make it simpler to use the `MockService::expect_request` method. * Test if crawled transactions are downloaded Create some dummy crawled transactions, and let the crawler discover them. Then check if they are forwarded to the mempool to be downloaded and verified. * Don't send empty transaction ID list to downloader Ignore response from peers that don't provide any crawled transactions. * Log errors when forwarding crawled transaction IDs Calling the Mempool service should not fail, so if an error happens it should be visible. However, errors when downloading individual transactions can happen from time to time, so there's no need for them to be very visible. * Document existing `mempool::Crawler` test Provide some depth as to what the test expect from the crawler's behavior. * Refactor to create `setup_crawler` helper function Make it easier to reuse the common test setup code. * Simplify code to expect requests Now that `zebra_network::Request` implement `Eq`, the call can be simplified into `expect_request`. * Refactor to create `respond_with_transaction_ids` A helper function that checks for a network crawl request and responds with the given list of crawled transaction IDs. * Refactor to create `crawler_iterator` helper A function to intercept and respond to the fanned-out requests sent during a single crawl iteration. * Refactor to create `respond_to_queue_request` Reduce the repeated code necessary to intercept and reply to a request for queuing transactions to be downloaded. * Add `respond_to_queue_request_with_error` helper Intercepts a mempool request to queue transactions to be downloaded, and responds with an error, simulating an internal problem in the mempool service implementation. * Derive `Arbitrary` for `NetworkUpgrade` This is required for deriving `Arbitrary` for some error types. * Derive `Arbitrary` for `TransactionError` Allow random transaction errors to be generated for property tests. * Derive `Arbitrary` for `MempoolError` Allow random Mempool errors to be generated for property tests. * Test if errors don't stop the mempool crawler The crawler should be robust enough to continue operating even if the mempool service fails to download transactions or even fails to handle requests to enqueue transactions. * Reduce the log level for download errors They should happen regularly, so there's no need to have them with a high visibility level. Co-authored-by: teor <teor@riseup.net> * Stop crawler if service stops If `Mempool::poll_ready` returns an error, it's because the mempool service has stopped and can't handle any requests, so the crawler should stop as well. Co-authored-by: teor <teor@riseup.net> Co-authored-by: Conrado Gouvea <conrado@zfnd.org>
2021-10-04 17:55:42 -07:00
#[cfg_attr(any(test, feature = "proptest-impl"), proptest(skip))]
Ed25519(#[from] zebra_chain::primitives::ed25519::Error),
#[error("Sapling bindingSig MUST represent a valid signature under the transaction binding validating key bvk of SigHash")]
Send crawled transaction IDs to downloader (#2801) * Rename type parameter to be more explicit Replace the single letter with a proper name. * Remove imports for `Request` and `Response` The type names will conflict with the ones for the mempool service. * Attach `Mempool` service to the `Crawler` Add a field to the `Crawler` type to store a way to access the `Mempool` service. * Forward crawled transactions to downloader The crawled transactions are now sent to the transaction downloader and verifier, to be included in the mempool. * Derive `Eq` and `PartialEq` for `mempool::Request` Make it simpler to use the `MockService::expect_request` method. * Test if crawled transactions are downloaded Create some dummy crawled transactions, and let the crawler discover them. Then check if they are forwarded to the mempool to be downloaded and verified. * Don't send empty transaction ID list to downloader Ignore response from peers that don't provide any crawled transactions. * Log errors when forwarding crawled transaction IDs Calling the Mempool service should not fail, so if an error happens it should be visible. However, errors when downloading individual transactions can happen from time to time, so there's no need for them to be very visible. * Document existing `mempool::Crawler` test Provide some depth as to what the test expect from the crawler's behavior. * Refactor to create `setup_crawler` helper function Make it easier to reuse the common test setup code. * Simplify code to expect requests Now that `zebra_network::Request` implement `Eq`, the call can be simplified into `expect_request`. * Refactor to create `respond_with_transaction_ids` A helper function that checks for a network crawl request and responds with the given list of crawled transaction IDs. * Refactor to create `crawler_iterator` helper A function to intercept and respond to the fanned-out requests sent during a single crawl iteration. * Refactor to create `respond_to_queue_request` Reduce the repeated code necessary to intercept and reply to a request for queuing transactions to be downloaded. * Add `respond_to_queue_request_with_error` helper Intercepts a mempool request to queue transactions to be downloaded, and responds with an error, simulating an internal problem in the mempool service implementation. * Derive `Arbitrary` for `NetworkUpgrade` This is required for deriving `Arbitrary` for some error types. * Derive `Arbitrary` for `TransactionError` Allow random transaction errors to be generated for property tests. * Derive `Arbitrary` for `MempoolError` Allow random Mempool errors to be generated for property tests. * Test if errors don't stop the mempool crawler The crawler should be robust enough to continue operating even if the mempool service fails to download transactions or even fails to handle requests to enqueue transactions. * Reduce the log level for download errors They should happen regularly, so there's no need to have them with a high visibility level. Co-authored-by: teor <teor@riseup.net> * Stop crawler if service stops If `Mempool::poll_ready` returns an error, it's because the mempool service has stopped and can't handle any requests, so the crawler should stop as well. Co-authored-by: teor <teor@riseup.net> Co-authored-by: Conrado Gouvea <conrado@zfnd.org>
2021-10-04 17:55:42 -07:00
#[cfg_attr(any(test, feature = "proptest-impl"), proptest(skip))]
RedJubjub(zebra_chain::primitives::redjubjub::Error),
#[error("Orchard bindingSig MUST represent a valid signature under the transaction binding validating key bvk of SigHash")]
Send crawled transaction IDs to downloader (#2801) * Rename type parameter to be more explicit Replace the single letter with a proper name. * Remove imports for `Request` and `Response` The type names will conflict with the ones for the mempool service. * Attach `Mempool` service to the `Crawler` Add a field to the `Crawler` type to store a way to access the `Mempool` service. * Forward crawled transactions to downloader The crawled transactions are now sent to the transaction downloader and verifier, to be included in the mempool. * Derive `Eq` and `PartialEq` for `mempool::Request` Make it simpler to use the `MockService::expect_request` method. * Test if crawled transactions are downloaded Create some dummy crawled transactions, and let the crawler discover them. Then check if they are forwarded to the mempool to be downloaded and verified. * Don't send empty transaction ID list to downloader Ignore response from peers that don't provide any crawled transactions. * Log errors when forwarding crawled transaction IDs Calling the Mempool service should not fail, so if an error happens it should be visible. However, errors when downloading individual transactions can happen from time to time, so there's no need for them to be very visible. * Document existing `mempool::Crawler` test Provide some depth as to what the test expect from the crawler's behavior. * Refactor to create `setup_crawler` helper function Make it easier to reuse the common test setup code. * Simplify code to expect requests Now that `zebra_network::Request` implement `Eq`, the call can be simplified into `expect_request`. * Refactor to create `respond_with_transaction_ids` A helper function that checks for a network crawl request and responds with the given list of crawled transaction IDs. * Refactor to create `crawler_iterator` helper A function to intercept and respond to the fanned-out requests sent during a single crawl iteration. * Refactor to create `respond_to_queue_request` Reduce the repeated code necessary to intercept and reply to a request for queuing transactions to be downloaded. * Add `respond_to_queue_request_with_error` helper Intercepts a mempool request to queue transactions to be downloaded, and responds with an error, simulating an internal problem in the mempool service implementation. * Derive `Arbitrary` for `NetworkUpgrade` This is required for deriving `Arbitrary` for some error types. * Derive `Arbitrary` for `TransactionError` Allow random transaction errors to be generated for property tests. * Derive `Arbitrary` for `MempoolError` Allow random Mempool errors to be generated for property tests. * Test if errors don't stop the mempool crawler The crawler should be robust enough to continue operating even if the mempool service fails to download transactions or even fails to handle requests to enqueue transactions. * Reduce the log level for download errors They should happen regularly, so there's no need to have them with a high visibility level. Co-authored-by: teor <teor@riseup.net> * Stop crawler if service stops If `Mempool::poll_ready` returns an error, it's because the mempool service has stopped and can't handle any requests, so the crawler should stop as well. Co-authored-by: teor <teor@riseup.net> Co-authored-by: Conrado Gouvea <conrado@zfnd.org>
2021-10-04 17:55:42 -07:00
#[cfg_attr(any(test, feature = "proptest-impl"), proptest(skip))]
RedPallas(zebra_chain::primitives::redpallas::Error),
// temporary error type until #1186 is fixed
#[error("Downcast from BoxError to redjubjub::Error failed")]
InternalDowncastError(String),
#[error("adding to the sprout pool is disabled after Canopy")]
DisabledAddToSproutPool,
#[error("could not calculate the transaction fee")]
IncorrectFee,
Reject a mempool transaction if it has internal spend conflicts (#2843) * Reorder imports to follow convention Place the imports from `std` at the top. * Add transaction errors for double spends Add a variant for each pool. They represent a double spend inside a transaction. * Add `check::spend_conflicts` implementation Checks if a transaction has spend conflicts, i.e., if a transaction spends a UTXO more than once or if it reveals a nullifier more than once. * Reject transactions with internal spend conflicts The transaction verifier should reject transactions that spend the same transparent UTXO or that reveal the same nullifier. * Add transparent spend consensus rule Add it to the documentation to help with understanding and auditing it. Co-authored-by: teor <teor@riseup.net> * Use different nullifiers by default Don't use the same nullifier twice when mocking a `sprout::JoinSplitData` because it will lead to an invalid transaction. * Test transactions with repeated spend outpoints Since that represents a spend conflict, they should be rejected. * Test duplicate nullifiers in joinsplit Check if a mock transaction with a joinsplit that reveals the same nullifier twice is rejected. * Test duplicate nullifiers across joinsplits Check if a duplicate nullifier in two different joinsplits in the same transaction is rejected. * Test V4 transaction with duplicate Sapling spend Check if a V4 transaction that has a duplicate Sapling spend is rejected. * Test V5 transaction with duplicate Sapling spend Check if a V5 transaction that has a duplicate Sapling spend is rejected. * Test V5 transaction with duplicate Orchard actions Check if a V5 transaction that has duplicate Orchard actions is rejected by the transaction verifier. Co-authored-by: teor <teor@riseup.net>
2021-10-27 19:49:28 -07:00
#[error("transparent double-spend: {_0:?} is spent twice")]
DuplicateTransparentSpend(transparent::OutPoint),
#[error("sprout double-spend: duplicate nullifier: {_0:?}")]
DuplicateSproutNullifier(sprout::Nullifier),
#[error("sapling double-spend: duplicate nullifier: {_0:?}")]
DuplicateSaplingNullifier(sapling::Nullifier),
#[error("orchard double-spend: duplicate nullifier: {_0:?}")]
DuplicateOrchardNullifier(orchard::Nullifier),
2021-11-08 13:45:54 -08:00
#[error("must have at least one active orchard flag")]
NotEnoughFlags,
}
impl From<BoxError> for TransactionError {
fn from(mut err: BoxError) -> Self {
// TODO: handle redpallas::Error, ScriptInvalid, InvalidSignature
match err.downcast::<zebra_chain::primitives::redjubjub::Error>() {
Ok(e) => return TransactionError::RedJubjub(*e),
Err(e) => err = e,
}
// buffered transaction verifier service error
match err.downcast::<TransactionError>() {
Ok(e) => return *e,
Err(e) => err = e,
}
TransactionError::InternalDowncastError(format!(
"downcast to known transaction error type failed, original error: {:?}",
err,
))
}
}
impl From<SubsidyError> for BlockError {
fn from(err: SubsidyError) -> BlockError {
BlockError::Transaction(TransactionError::Subsidy(err))
}
}
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum BlockError {
#[error("block contains invalid transactions")]
Transaction(#[from] TransactionError),
#[error("block has no transactions")]
NoTransactions,
#[error("block has mismatched merkle root")]
BadMerkleRoot {
actual: zebra_chain::block::merkle::Root,
expected: zebra_chain::block::merkle::Root,
},
#[error("block contains duplicate transactions")]
DuplicateTransaction,
#[error("block {0:?} is already in the chain at depth {1:?}")]
AlreadyInChain(zebra_chain::block::Hash, u32),
#[error("invalid block {0:?}: missing block height")]
MissingHeight(zebra_chain::block::Hash),
#[error("invalid block height {0:?} in {1:?}: greater than the maximum height {2:?}")]
MaxHeight(
zebra_chain::block::Height,
zebra_chain::block::Hash,
zebra_chain::block::Height,
),
#[error("invalid difficulty threshold in block header {0:?} {1:?}")]
InvalidDifficulty(zebra_chain::block::Height, zebra_chain::block::Hash),
2020-10-13 18:35:45 -07:00
#[error("block {0:?} has a difficulty threshold {2:?} that is easier than the {3:?} difficulty limit {4:?}, hash: {1:?}")]
TargetDifficultyLimit(
zebra_chain::block::Height,
zebra_chain::block::Hash,
zebra_chain::work::difficulty::ExpandedDifficulty,
zebra_chain::parameters::Network,
zebra_chain::work::difficulty::ExpandedDifficulty,
),
#[error(
"block {0:?} on {3:?} has a hash {1:?} that is easier than its difficulty threshold {2:?}"
)]
DifficultyFilter(
zebra_chain::block::Height,
zebra_chain::block::Hash,
zebra_chain::work::difficulty::ExpandedDifficulty,
zebra_chain::parameters::Network,
),
#[error("transaction has wrong consensus branch id for block network upgrade")]
WrongTransactionConsensusBranchId,
}