Skip to content

Commit 24e0a8a

Browse files
bertllllBert
andauthored
GH-598-json-hotfix (#699)
* GH-598-json-hotfix: corrected * GH-598-json-hotfix: little fix * GH-598-json-hotfix: rearrangement * GH-598-json-hotfix: addressing the review * GH-598-json-hotfix: finished addressing review --------- Co-authored-by: Bert <Bert@Bert.com>
1 parent 7a664d6 commit 24e0a8a

File tree

7 files changed

+613
-72
lines changed

7 files changed

+613
-72
lines changed

node/src/accountant/db_access_objects/failed_payable_dao.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ mod tests {
382382
make_read_only_db_connection, FailedTxBuilder,
383383
};
384384
use crate::accountant::db_access_objects::utils::current_unix_timestamp;
385-
use crate::blockchain::errors::rpc_errors::AppRpcErrorKind;
385+
use crate::blockchain::errors::rpc_errors::{AppRpcErrorKind, LocalErrorKind, RemoteErrorKind};
386386
use crate::blockchain::errors::validation_status::{
387387
PreviousAttempts, ValidationFailureClockReal,
388388
};
@@ -591,8 +591,8 @@ mod tests {
591591
fn failure_reason_from_str_works() {
592592
// Submission error
593593
assert_eq!(
594-
FailureReason::from_str(r#"{"Submission":{"Local":{"Decoder"}}}"#).unwrap(),
595-
FailureReason::Submission(AppRpcErrorKind::Decoder)
594+
FailureReason::from_str(r#"{"Submission":{"Local":"Decoder"}}"#).unwrap(),
595+
FailureReason::Submission(AppRpcErrorKind::Local(LocalErrorKind::Decoder))
596596
);
597597

598598
// Reverted
@@ -640,8 +640,8 @@ mod tests {
640640
);
641641

642642
assert_eq!(
643-
FailureStatus::from_str(r#"{"RecheckRequired":{"Reattempting":{"ServerUnreachable":{"firstSeen":{"secs_since_epoch":1755080031,"nanos_since_epoch":612180914},"attempts":1}}}}"#).unwrap(),
644-
FailureStatus::RecheckRequired(ValidationStatus::Reattempting( PreviousAttempts::new(BlockchainErrorKind::AppRpc(AppRpcErrorKind::ServerUnreachable), &validation_failure_clock)))
643+
FailureStatus::from_str(r#"{"RecheckRequired":{"Reattempting":[{"error":{"AppRpc":{"Remote":"Unreachable"}},"firstSeen":{"secs_since_epoch":1755080031,"nanos_since_epoch":612180914},"attempts":1}]}}"#).unwrap(),
644+
FailureStatus::RecheckRequired(ValidationStatus::Reattempting( PreviousAttempts::new(BlockchainErrorKind::AppRpc(AppRpcErrorKind::Remote(RemoteErrorKind::Unreachable)), &validation_failure_clock)))
645645
);
646646

647647
assert_eq!(
@@ -652,9 +652,8 @@ mod tests {
652652
// Invalid Variant
653653
assert_eq!(
654654
FailureStatus::from_str("\"UnknownStatus\"").unwrap_err(),
655-
"unknown variant `UnknownStatus`, \
656-
expected one of `RetryRequired`, `RecheckRequired`, `Concluded` \
657-
at line 1 column 15 in '\"UnknownStatus\"'"
655+
"unknown variant `UnknownStatus`, expected one of `RetryRequired`, `RecheckRequired`, \
656+
`Concluded` at line 1 column 15 in '\"UnknownStatus\"'"
658657
);
659658

660659
// Invalid Input
@@ -724,7 +723,9 @@ mod tests {
724723
.reason(PendingTooLong)
725724
.status(RecheckRequired(ValidationStatus::Reattempting(
726725
PreviousAttempts::new(
727-
BlockchainErrorKind::AppRpc(AppRpcErrorKind::ServerUnreachable),
726+
BlockchainErrorKind::AppRpc(AppRpcErrorKind::Remote(
727+
RemoteErrorKind::Unreachable,
728+
)),
728729
&ValidationFailureClockReal::default(),
729730
),
730731
)))
@@ -775,13 +776,19 @@ mod tests {
775776
subject
776777
.insert_new_records(&vec![tx1.clone(), tx2.clone(), tx3.clone(), tx4.clone()])
777778
.unwrap();
779+
let timestamp = SystemTime::now();
780+
let clock = ValidationFailureClockMock::default()
781+
.now_result(timestamp)
782+
.now_result(timestamp);
778783
let hashmap = HashMap::from([
779784
(tx1.hash, Concluded),
780785
(
781786
tx2.hash,
782787
RecheckRequired(ValidationStatus::Reattempting(PreviousAttempts::new(
783-
BlockchainErrorKind::AppRpc(AppRpcErrorKind::ServerUnreachable),
784-
&ValidationFailureClockReal::default(),
788+
BlockchainErrorKind::AppRpc(AppRpcErrorKind::Remote(
789+
RemoteErrorKind::Unreachable,
790+
)),
791+
&clock,
785792
))),
786793
),
787794
(tx3.hash, Concluded),
@@ -797,8 +804,8 @@ mod tests {
797804
assert_eq!(
798805
updated_txs[1].status,
799806
RecheckRequired(ValidationStatus::Reattempting(PreviousAttempts::new(
800-
BlockchainErrorKind::AppRpc(AppRpcErrorKind::ServerUnreachable),
801-
&ValidationFailureClockReal::default()
807+
BlockchainErrorKind::AppRpc(AppRpcErrorKind::Remote(RemoteErrorKind::Unreachable)),
808+
&clock
802809
)))
803810
);
804811
assert_eq!(tx3.status, RetryRequired);

node/src/accountant/db_access_objects/sent_payable_dao.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ mod tests {
442442
use crate::accountant::db_access_objects::test_utils::{make_read_only_db_connection, TxBuilder};
443443
use crate::blockchain::blockchain_interface::blockchain_interface_web3::lower_level_interface_web3::{TransactionBlock};
444444
use crate::blockchain::errors::BlockchainErrorKind;
445-
use crate::blockchain::errors::rpc_errors::AppRpcErrorKind;
445+
use crate::blockchain::errors::rpc_errors::{AppRpcErrorKind, RemoteErrorKind};
446446
use crate::blockchain::errors::validation_status::{PreviousAttempts, ValidationFailureClockReal};
447447
use crate::blockchain::test_utils::{make_block_hash, make_tx_hash, ValidationFailureClockMock};
448448

@@ -458,11 +458,15 @@ mod tests {
458458
.hash(make_tx_hash(2))
459459
.status(TxStatus::Pending(ValidationStatus::Reattempting(
460460
PreviousAttempts::new(
461-
BlockchainErrorKind::AppRpc(AppRpcErrorKind::ServerUnreachable),
461+
BlockchainErrorKind::AppRpc(AppRpcErrorKind::Remote(
462+
RemoteErrorKind::Unreachable,
463+
)),
462464
&ValidationFailureClockReal::default(),
463465
)
464466
.add_attempt(
465-
BlockchainErrorKind::AppRpc(AppRpcErrorKind::ServerUnreachable),
467+
BlockchainErrorKind::AppRpc(AppRpcErrorKind::Remote(
468+
RemoteErrorKind::Unreachable,
469+
)),
466470
&ValidationFailureClockReal::default(),
467471
),
468472
)))
@@ -694,7 +698,9 @@ mod tests {
694698
.hash(make_tx_hash(2))
695699
.status(TxStatus::Pending(ValidationStatus::Reattempting(
696700
PreviousAttempts::new(
697-
BlockchainErrorKind::AppRpc(AppRpcErrorKind::ServerUnreachable),
701+
BlockchainErrorKind::AppRpc(AppRpcErrorKind::Remote(
702+
RemoteErrorKind::Unreachable,
703+
)),
698704
&ValidationFailureClockReal::default(),
699705
),
700706
)))
@@ -1189,8 +1195,8 @@ mod tests {
11891195
);
11901196

11911197
assert_eq!(
1192-
TxStatus::from_str(r#"{"Pending":{"Reattempting":{"InvalidResponse":{"firstSeen":{"secs_since_epoch":12456,"nanos_since_epoch":0},"attempts":1}}}}"#).unwrap(),
1193-
TxStatus::Pending(ValidationStatus::Reattempting(PreviousAttempts::new(BlockchainErrorKind::AppRpc(AppRpcErrorKind::InvalidResponse), &validation_failure_clock)))
1198+
TxStatus::from_str(r#"{"Pending":{"Reattempting":[{"error":{"AppRpc":{"Remote":"InvalidResponse"}},"firstSeen":{"secs_since_epoch":12456,"nanos_since_epoch":0},"attempts":1}]}}"#).unwrap(),
1199+
TxStatus::Pending(ValidationStatus::Reattempting(PreviousAttempts::new(BlockchainErrorKind::AppRpc(AppRpcErrorKind::Remote(RemoteErrorKind::InvalidResponse)), &validation_failure_clock)))
11941200
);
11951201

11961202
assert_eq!(

node/src/blockchain/errors/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ pub enum BlockchainErrorKind {
2323
#[cfg(test)]
2424
mod tests {
2525
use crate::blockchain::errors::internal_errors::InternalErrorKind;
26-
use crate::blockchain::errors::rpc_errors::AppRpcErrorKind;
26+
use crate::blockchain::errors::rpc_errors::{AppRpcErrorKind, LocalErrorKind};
2727
use crate::blockchain::errors::BlockchainErrorKind;
2828

2929
#[test]
3030
fn blockchain_error_serialization_deserialization() {
3131
vec![
3232
(
33-
BlockchainErrorKind::AppRpc(AppRpcErrorKind::Decoder),
34-
"{\"AppRpc\":\"Decoder\"}",
33+
BlockchainErrorKind::AppRpc(AppRpcErrorKind::Local(LocalErrorKind::Decoder)),
34+
r#"{"AppRpc":{"Local":"Decoder"}}"#,
3535
),
3636
(
3737
BlockchainErrorKind::Internal(InternalErrorKind::PendingTooLongNotReplaced),
38-
"{\"Internal\":\"PendingTooLongNotReplaced\"}",
38+
r#"{"Internal":"PendingTooLongNotReplaced"}"#,
3939
),
4040
]
4141
.into_iter()

node/src/blockchain/errors/rpc_errors.rs

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub enum AppRpcError {
1414
pub enum LocalError {
1515
Decoder(String),
1616
Internal,
17-
Io(String),
17+
IO(String),
1818
Signing(String),
1919
Transport(String),
2020
}
@@ -33,7 +33,7 @@ impl From<Web3Error> for AppRpcError {
3333
// Local Errors
3434
Web3Error::Decoder(error) => AppRpcError::Local(LocalError::Decoder(error)),
3535
Web3Error::Internal => AppRpcError::Local(LocalError::Internal),
36-
Web3Error::Io(error) => AppRpcError::Local(LocalError::Io(error.to_string())),
36+
Web3Error::Io(error) => AppRpcError::Local(LocalError::IO(error.to_string())),
3737
Web3Error::Signing(error) => {
3838
// This variant cannot be tested due to import limitations.
3939
AppRpcError::Local(LocalError::Signing(error.to_string()))
@@ -53,35 +53,44 @@ impl From<Web3Error> for AppRpcError {
5353
}
5454
}
5555

56-
#[derive(Debug, Hash, Clone, PartialEq, Eq, Serialize, Deserialize)]
56+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
5757
pub enum AppRpcErrorKind {
58-
// Local
58+
Local(LocalErrorKind),
59+
Remote(RemoteErrorKind),
60+
}
61+
62+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
63+
pub enum LocalErrorKind {
5964
Decoder,
6065
Internal,
6166
IO,
6267
Signing,
6368
Transport,
69+
}
6470

65-
// Remote
71+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
72+
pub enum RemoteErrorKind {
6673
InvalidResponse,
67-
ServerUnreachable,
74+
Unreachable,
6875
Web3RpcError(i64), // Keep only the stable error code
6976
}
7077

7178
impl From<&AppRpcError> for AppRpcErrorKind {
7279
fn from(err: &AppRpcError) -> Self {
7380
match err {
7481
AppRpcError::Local(local) => match local {
75-
LocalError::Decoder(_) => Self::Decoder,
76-
LocalError::Internal => Self::Internal,
77-
LocalError::Io(_) => Self::IO,
78-
LocalError::Signing(_) => Self::Signing,
79-
LocalError::Transport(_) => Self::Transport,
82+
LocalError::Decoder(_) => Self::Local(LocalErrorKind::Decoder),
83+
LocalError::Internal => Self::Local(LocalErrorKind::Internal),
84+
LocalError::IO(_) => Self::Local(LocalErrorKind::IO),
85+
LocalError::Signing(_) => Self::Local(LocalErrorKind::Signing),
86+
LocalError::Transport(_) => Self::Local(LocalErrorKind::Transport),
8087
},
8188
AppRpcError::Remote(remote) => match remote {
82-
RemoteError::InvalidResponse(_) => Self::InvalidResponse,
83-
RemoteError::Unreachable => Self::ServerUnreachable,
84-
RemoteError::Web3RpcError { code, .. } => Self::Web3RpcError(*code),
89+
RemoteError::InvalidResponse(_) => Self::Remote(RemoteErrorKind::InvalidResponse),
90+
RemoteError::Unreachable => Self::Remote(RemoteErrorKind::Unreachable),
91+
RemoteError::Web3RpcError { code, .. } => {
92+
Self::Remote(RemoteErrorKind::Web3RpcError(*code))
93+
}
8594
},
8695
}
8796
}
@@ -90,7 +99,7 @@ impl From<&AppRpcError> for AppRpcErrorKind {
9099
#[cfg(test)]
91100
mod tests {
92101
use crate::blockchain::errors::rpc_errors::{
93-
AppRpcError, AppRpcErrorKind, LocalError, RemoteError,
102+
AppRpcError, AppRpcErrorKind, LocalError, LocalErrorKind, RemoteError, RemoteErrorKind,
94103
};
95104
use web3::error::Error as Web3Error;
96105

@@ -110,7 +119,7 @@ mod tests {
110119
std::io::ErrorKind::Other,
111120
"IO error"
112121
))),
113-
AppRpcError::Local(LocalError::Io("IO error".to_string()))
122+
AppRpcError::Local(LocalError::IO("IO error".to_string()))
114123
);
115124
assert_eq!(
116125
AppRpcError::from(Web3Error::Transport("Transport error".to_string())),
@@ -145,69 +154,66 @@ mod tests {
145154
AppRpcErrorKind::from(&AppRpcError::Local(LocalError::Decoder(
146155
"Decoder error".to_string()
147156
))),
148-
AppRpcErrorKind::Decoder
157+
AppRpcErrorKind::Local(LocalErrorKind::Decoder)
149158
);
150159
assert_eq!(
151160
AppRpcErrorKind::from(&AppRpcError::Local(LocalError::Internal)),
152-
AppRpcErrorKind::Internal
161+
AppRpcErrorKind::Local(LocalErrorKind::Internal)
153162
);
154163
assert_eq!(
155-
AppRpcErrorKind::from(&AppRpcError::Local(LocalError::Io("IO error".to_string()))),
156-
AppRpcErrorKind::IO
164+
AppRpcErrorKind::from(&AppRpcError::Local(LocalError::IO("IO error".to_string()))),
165+
AppRpcErrorKind::Local(LocalErrorKind::IO)
157166
);
158167
assert_eq!(
159168
AppRpcErrorKind::from(&AppRpcError::Local(LocalError::Signing(
160169
"Signing error".to_string()
161170
))),
162-
AppRpcErrorKind::Signing
171+
AppRpcErrorKind::Local(LocalErrorKind::Signing)
163172
);
164173
assert_eq!(
165174
AppRpcErrorKind::from(&AppRpcError::Local(LocalError::Transport(
166175
"Transport error".to_string()
167176
))),
168-
AppRpcErrorKind::Transport
177+
AppRpcErrorKind::Local(LocalErrorKind::Transport)
169178
);
170179
assert_eq!(
171180
AppRpcErrorKind::from(&AppRpcError::Remote(RemoteError::InvalidResponse(
172181
"Invalid response".to_string()
173182
))),
174-
AppRpcErrorKind::InvalidResponse
183+
AppRpcErrorKind::Remote(RemoteErrorKind::InvalidResponse)
175184
);
176185
assert_eq!(
177186
AppRpcErrorKind::from(&AppRpcError::Remote(RemoteError::Unreachable)),
178-
AppRpcErrorKind::ServerUnreachable
187+
AppRpcErrorKind::Remote(RemoteErrorKind::Unreachable)
179188
);
180189
assert_eq!(
181190
AppRpcErrorKind::from(&AppRpcError::Remote(RemoteError::Web3RpcError {
182191
code: 55,
183192
message: "Booga".to_string()
184193
})),
185-
AppRpcErrorKind::Web3RpcError(55)
194+
AppRpcErrorKind::Remote(RemoteErrorKind::Web3RpcError(55))
186195
);
187196
}
188197

189198
#[test]
190199
fn app_rpc_error_kind_serialization_deserialization() {
191200
let errors = vec![
192-
// Local Errors
193-
AppRpcErrorKind::Decoder,
194-
AppRpcErrorKind::Internal,
195-
AppRpcErrorKind::IO,
196-
AppRpcErrorKind::Signing,
197-
AppRpcErrorKind::Transport,
198-
// Remote Errors
199-
AppRpcErrorKind::InvalidResponse,
200-
AppRpcErrorKind::ServerUnreachable,
201-
AppRpcErrorKind::Web3RpcError(42),
201+
AppRpcErrorKind::Local(LocalErrorKind::Decoder),
202+
AppRpcErrorKind::Local(LocalErrorKind::Internal),
203+
AppRpcErrorKind::Local(LocalErrorKind::IO),
204+
AppRpcErrorKind::Local(LocalErrorKind::Signing),
205+
AppRpcErrorKind::Local(LocalErrorKind::Transport),
206+
AppRpcErrorKind::Remote(RemoteErrorKind::InvalidResponse),
207+
AppRpcErrorKind::Remote(RemoteErrorKind::Unreachable),
208+
AppRpcErrorKind::Remote(RemoteErrorKind::Web3RpcError(42)),
202209
];
203210

204211
errors.into_iter().for_each(|error| {
205212
let serialized = serde_json::to_string(&error).unwrap();
206213
let deserialized: AppRpcErrorKind = serde_json::from_str(&serialized).unwrap();
207214
assert_eq!(
208215
error, deserialized,
209-
"Failed serde attempt for {:?} that should look \
210-
like {:?}",
216+
"Failed serde attempt for {:?} that should look like {:?}",
211217
deserialized, error
212218
);
213219
});

0 commit comments

Comments
 (0)