Skip to content

Commit 7238e64

Browse files
authored
Merge pull request #3807 from dusk-network/repeat-nonce-error
2 parents 7309fe5 + 9df1c7c commit 7238e64

File tree

5 files changed

+33
-102
lines changed

5 files changed

+33
-102
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node/src/mempool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ impl MempoolSrv {
324324
// VM Preverify call
325325
let preverification_data =
326326
vm.read().await.preverify(tx).map_err(|e| {
327-
TxAcceptanceError::VerificationFailed(format!("{e:?}"))
327+
TxAcceptanceError::VerificationFailed(format!("{e}"))
328328
})?;
329329

330330
if let PreverificationResult::FutureNonce {

rusk/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ tracing-subscriber = { workspace = true, features = [
3131
clap = { workspace = true, features = ["env", "string", "derive"] }
3232
semver = { workspace = true }
3333
anyhow = { workspace = true }
34+
thiserror = { workspace = true }
3435
rustc_tools_util = { workspace = true }
3536
rand = { workspace = true }
3637
toml = { workspace = true }

rusk/src/lib/error.rs

Lines changed: 27 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,66 @@
44
//
55
// Copyright (c) DUSK NETWORK. All rights reserved.
66

7-
use std::{fmt, io};
7+
use std::io;
88

99
use dusk_bytes::Serializable;
10-
use dusk_core::{
11-
signatures::bls::PublicKey as BlsPublicKey, transfer::phoenix::CoreError,
12-
BlsScalar, Error as ExecErr,
13-
};
10+
use dusk_core::signatures::bls::PublicKey as BlsPublicKey;
11+
use dusk_core::transfer::phoenix::CoreError as PhoenixError;
12+
use dusk_core::{BlsScalar, Error as ExecErr};
1413
use dusk_vm::Error as VMError;
1514

16-
#[derive(Debug)]
15+
#[derive(Debug, thiserror::Error)]
1716
pub enum Error {
18-
/// Failed to register a backend for persistence
19-
BackendRegistrationFailed,
20-
/// Failed to restore a network state from disk
21-
RestoreFailed,
22-
/// Proof verification failure
23-
ProofVerification,
2417
/// Out of gas in block execution
18+
#[error("Out of gas")]
2519
OutOfGas,
2620
/// Repeated nullifier in transaction verification
21+
#[error("Nullifiers already spent: {0:?}")]
2722
RepeatingNullifiers(Vec<BlsScalar>),
2823
/// Repeated nullifier in the same transaction
24+
#[error("Double nullifiers")]
2925
DoubleNullifiers,
3026
/// Repeating a nonce that has already been used
27+
#[error("Nonce repeat: {} {1}", bs58::encode(.0.to_bytes()).into_string())]
3128
RepeatingNonce(Box<BlsPublicKey>, u64),
3229
/// Wrong inputs and/or outputs in the transaction verification
30+
#[error("Expected: 0 < (inputs: {0}) < 5, 0 ≤ (outputs: {1}) < 3")]
3331
InvalidCircuitArguments(usize, usize),
34-
/// Failed to build a Rusk instance
35-
BuilderInvalidState,
3632
/// Failed to fetch opening
33+
#[error("Failed to fetch opening of position {0}")]
3734
OpeningPositionNotFound(u64),
38-
/// Failed to fetch opening due to undefined Note
39-
OpeningNoteUndefined(u64),
4035
/// Bytes Serialization Errors
36+
#[error("Serialization Error: {0:?}")]
4137
Serialization(dusk_bytes::Error),
4238
/// Originating from transaction-creation
39+
#[error("Transaction Error: {0}")]
4340
Transaction(ExecErr),
4441
/// Originating from Phoenix.
45-
Phoenix(CoreError),
42+
#[error("Phoenix Error: {0}")]
43+
Phoenix(PhoenixError),
4644
/// Piecrust VM internal Errors
47-
Vm(VMError),
45+
#[error("VM Error: {0}")]
46+
Vm(#[from] VMError),
4847
/// IO Errors
49-
Io(io::Error),
48+
#[error("IO Error: {0}")]
49+
Io(#[from] io::Error),
5050
/// Other
51-
Other(Box<dyn std::error::Error>),
51+
#[error("Other Error: {0}")]
52+
Other(#[from] Box<dyn std::error::Error>),
5253
/// Commit not found amongst existing commits
54+
#[error("Commit not found, id = {}", hex::encode(.0))]
5355
CommitNotFound([u8; 32]),
5456
/// Invalid credits count
57+
#[error("Invalid credits: H= {0}, credits= {1}")]
5558
InvalidCreditsCount(u64, usize),
5659
/// Memo too large
60+
#[error("The memo size {0} is too large")]
5761
MemoTooLarge(usize),
5862
/// Blob related errors
63+
#[error("Blob error: {0}")]
5964
Blob(String),
6065
}
6166

62-
impl std::error::Error for Error {}
63-
64-
impl From<Box<dyn std::error::Error>> for Error {
65-
fn from(err: Box<dyn std::error::Error>) -> Self {
66-
Error::Other(err)
67-
}
68-
}
69-
70-
impl From<VMError> for Error {
71-
fn from(err: VMError) -> Self {
72-
Error::Vm(err)
73-
}
74-
}
75-
7667
impl From<dusk_core::Error> for Error {
7768
fn from(err: ExecErr) -> Self {
7869
match err {
@@ -117,70 +108,8 @@ impl From<dusk_bytes::Error> for Error {
117108
}
118109
}
119110

120-
impl From<CoreError> for Error {
121-
fn from(pe: CoreError) -> Self {
111+
impl From<PhoenixError> for Error {
112+
fn from(pe: PhoenixError) -> Self {
122113
Self::Phoenix(pe)
123114
}
124115
}
125-
126-
impl From<io::Error> for Error {
127-
fn from(err: io::Error) -> Self {
128-
Error::Io(err)
129-
}
130-
}
131-
132-
impl fmt::Display for Error {
133-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
134-
match self {
135-
Error::BackendRegistrationFailed => {
136-
write!(f, "Failed to register a backend for persistence")
137-
}
138-
Error::RestoreFailed => {
139-
write!(f, "Failed to restore a network state")
140-
}
141-
Error::BuilderInvalidState => {
142-
write!(f, "Failed to build a Rusk instance")
143-
}
144-
Error::OpeningPositionNotFound(pos) => {
145-
write!(f, "Failed to fetch opening of position {pos}")
146-
}
147-
Error::OpeningNoteUndefined(pos) => {
148-
write!(f, "Note {pos} not found, opening of position")
149-
}
150-
Error::Serialization(err) => {
151-
write!(f, "Serialization Error: {err:?}")
152-
}
153-
Error::Vm(err) => write!(f, "VM Error: {err}"),
154-
Error::Io(err) => write!(f, "IO Error: {err}"),
155-
Error::Transaction(err) => write!(f, "Transaction Error: {err}"),
156-
Error::Phoenix(err) => write!(f, "Phoenix error: {err}"),
157-
Error::Other(err) => write!(f, "Other error: {err}"),
158-
Error::ProofVerification => write!(f, "Proof verification failure"),
159-
Error::OutOfGas => write!(f, "Out of gas"),
160-
Error::RepeatingNullifiers(n) => {
161-
write!(f, "Nullifiers already spent: {n:?}")
162-
}
163-
Error::DoubleNullifiers => write!(f, "Double nullifiers"),
164-
Error::RepeatingNonce(account, nonce) => {
165-
let encoded_account =
166-
bs58::encode(&account.to_bytes()).into_string();
167-
write!(f, "Nonce repeat: {encoded_account} {nonce}")
168-
}
169-
Error::InvalidCircuitArguments(inputs_len, outputs_len) => {
170-
write!(f,"Expected: 0 < (inputs: {inputs_len}) < 5, 0 ≤ (outputs: {outputs_len}) < 3")
171-
}
172-
Error::CommitNotFound(commit_id) => {
173-
write!(f, "Commit not found, id = {}", hex::encode(commit_id),)
174-
}
175-
Error::InvalidCreditsCount(height, credits) => {
176-
write!(f, "Invalid credits: H= {height}, credits= {credits}",)
177-
}
178-
Error::MemoTooLarge(size) => {
179-
write!(f, "The memo size {size} is too large")
180-
}
181-
Error::Blob(e) => {
182-
write!(f, "Blob error: {e}")
183-
}
184-
}
185-
}
186-
}

rusk/src/lib/node/vm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ impl VMExecution for Rusk {
154154
if !existing_nullifiers.is_empty() {
155155
let err =
156156
RuskError::RepeatingNullifiers(existing_nullifiers);
157-
return Err(anyhow::anyhow!("Invalid tx: {err}"));
157+
return Err(anyhow::anyhow!("{err}"));
158158
}
159159

160160
if !has_unique_elements(tx_nullifiers) {
161161
let err = RuskError::DoubleNullifiers;
162-
return Err(anyhow::anyhow!("Invalid tx: {err}"));
162+
return Err(anyhow::anyhow!("{err}"));
163163
}
164164

165165
match crate::verifier::verify_proof(tx) {
@@ -193,7 +193,7 @@ impl VMExecution for Rusk {
193193
(*tx.sender()).into(),
194194
tx.nonce(),
195195
);
196-
return Err(anyhow::anyhow!("Invalid tx: {err}"));
196+
return Err(anyhow::anyhow!("{err}"));
197197
}
198198

199199
let result = if tx.nonce() > account_data.nonce + 1 {

0 commit comments

Comments
 (0)