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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// SPDX-License-Identifier: Apache-2.0
use hiero_sdk_proto::services;
use crate::protobuf::{
FromProtobuf,
ToProtobuf,
};
use crate::ContractId;
/// Info about a contract account's nonce value.
/// The nonce for a contract is only incremented when that contract creates another contract.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ContractNonceInfo {
/// The contract's ID.
pub contract_id: ContractId,
/// The contract's nonce.
pub nonce: u64,
}
impl ContractNonceInfo {
/// Create a new `ContractNonceInfo` from protobuf-encoded `bytes`.
///
/// # Errors
/// - [`Error::FromProtobuf`](crate::Error::FromProtobuf) if decoding the bytes fails to produce a valid protobuf.
/// - [`Error::FromProtobuf`](crate::Error::FromProtobuf) if decoding the protobuf fails.
pub fn from_bytes(bytes: &[u8]) -> crate::Result<Self> {
FromProtobuf::from_bytes(bytes)
}
/// Convert `self` to a protobuf-encoded [`Vec<u8>`].
#[must_use]
pub fn to_bytes(&self) -> Vec<u8> {
ToProtobuf::to_bytes(self)
}
}
impl FromProtobuf<services::ContractNonceInfo> for ContractNonceInfo {
fn from_protobuf(pb: services::ContractNonceInfo) -> crate::Result<Self>
where
Self: Sized,
{
Ok(Self {
contract_id: ContractId::from_protobuf(pb_getf!(pb, contract_id)?)?,
nonce: pb.nonce as u64,
})
}
}
impl ToProtobuf for ContractNonceInfo {
type Protobuf = services::ContractNonceInfo;
fn to_protobuf(&self) -> Self::Protobuf {
Self::Protobuf {
contract_id: Some(self.contract_id.to_protobuf()),
nonce: self.nonce as i64,
}
}
}
#[cfg(test)]
mod tests {
use expect_test::expect;
use hiero_sdk_proto::services;
use crate::protobuf::{
FromProtobuf,
ToProtobuf,
};
use crate::ContractNonceInfo;
const INFO: services::ContractNonceInfo = services::ContractNonceInfo {
contract_id: Some(services::ContractId {
shard_num: 0,
realm_num: 0,
contract: Some(services::contract_id::Contract::ContractNum(2)),
}),
nonce: 2,
};
#[test]
fn from_protobuf() {
expect![[r#"
ContractNonceInfo {
contract_id: "0.0.2",
nonce: 2,
}
"#]]
.assert_debug_eq(&ContractNonceInfo::from_protobuf(INFO).unwrap());
}
#[test]
fn to_protobuf() {
expect![[r#"
ContractNonceInfo {
contract_id: Some(
ContractId {
shard_num: 0,
realm_num: 0,
contract: Some(
ContractNum(
2,
),
),
},
),
nonce: 2,
}
"#]]
.assert_debug_eq(&ContractNonceInfo::from_protobuf(INFO).unwrap().to_protobuf());
}
#[test]
fn from_bytes() {
expect![[r#"
ContractNonceInfo {
contract_id: Some(
ContractId {
shard_num: 0,
realm_num: 0,
contract: Some(
ContractNum(
2,
),
),
},
),
nonce: 2,
}
"#]]
.assert_debug_eq(
&ContractNonceInfo::from_bytes(&prost::Message::encode_to_vec(&INFO))
.unwrap()
.to_protobuf(),
);
}
}