-
Notifications
You must be signed in to change notification settings - Fork 80
/
acknowledgement.rs
102 lines (86 loc) · 3.36 KB
/
acknowledgement.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use core::fmt::{Display, Error as FmtError, Formatter};
use super::error::TokenTransferError;
use crate::core::ics04_channel::msgs::acknowledgement::Acknowledgement as GenericAcknowledgement;
use crate::prelude::*;
/// A string constant included in error acknowledgements.
/// NOTE: Changing this const is state machine breaking as acknowledgements are written into state
pub const ACK_ERR_STR: &str = "error handling packet on destination chain: see events for details";
/// A successful acknowledgement, equivalent to `base64::encode(0x01)`.
pub const ACK_SUCCESS_B64: &str = "AQ==";
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ConstAckSuccess {
#[cfg_attr(feature = "serde", serde(rename = "AQ=="))]
Success,
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Acknowledgement {
/// Successful Acknowledgement
/// e.g. `{"result":"AQ=="}`
#[cfg_attr(feature = "serde", serde(rename = "result"))]
Success(ConstAckSuccess),
/// Error Acknowledgement
/// e.g. `{"error":"cannot unmarshal ICS-20 transfer packet data"}`
#[cfg_attr(feature = "serde", serde(rename = "error"))]
Error(String),
}
impl Acknowledgement {
pub fn success() -> Self {
Self::Success(ConstAckSuccess::Success)
}
pub fn from_error(err: TokenTransferError) -> Self {
Self::Error(format!("{ACK_ERR_STR}: {err}"))
}
}
impl AsRef<[u8]> for Acknowledgement {
fn as_ref(&self) -> &[u8] {
match self {
Acknowledgement::Success(_) => ACK_SUCCESS_B64.as_bytes(),
Acknowledgement::Error(s) => s.as_bytes(),
}
}
}
impl Display for Acknowledgement {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
match self {
Acknowledgement::Success(_) => write!(f, "{ACK_SUCCESS_B64}"),
Acknowledgement::Error(err_str) => write!(f, "{err_str}"),
}
}
}
impl From<Acknowledgement> for GenericAcknowledgement {
fn from(ack: Acknowledgement) -> Self {
ack.as_ref().into()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_ack_ser() {
fn ser_json_assert_eq(ack: Acknowledgement, json_str: &str) {
let ser = serde_json::to_string(&ack).unwrap();
assert_eq!(ser, json_str)
}
ser_json_assert_eq(Acknowledgement::success(), r#"{"result":"AQ=="}"#);
ser_json_assert_eq(
Acknowledgement::Error("cannot unmarshal ICS-20 transfer packet data".to_owned()),
r#"{"error":"cannot unmarshal ICS-20 transfer packet data"}"#,
);
}
#[test]
fn test_ack_de() {
fn de_json_assert_eq(json_str: &str, ack: Acknowledgement) {
let de = serde_json::from_str::<Acknowledgement>(json_str).unwrap();
assert_eq!(de, ack)
}
de_json_assert_eq(r#"{"result":"AQ=="}"#, Acknowledgement::success());
de_json_assert_eq(
r#"{"error":"cannot unmarshal ICS-20 transfer packet data"}"#,
Acknowledgement::Error("cannot unmarshal ICS-20 transfer packet data".to_owned()),
);
assert!(serde_json::from_str::<Acknowledgement>(r#"{"result":"AQ="}"#).is_err());
assert!(serde_json::from_str::<Acknowledgement>(r#"{"success":"AQ=="}"#).is_err());
}
}