casper_types/
deploy_info.rs1use alloc::vec::Vec;
2
3#[cfg(feature = "datasize")]
4use datasize::DataSize;
5#[cfg(feature = "json-schema")]
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8
9use crate::{
10 account::AccountHash,
11 bytesrepr::{self, FromBytes, ToBytes},
12 serde_helpers, DeployHash, TransferAddr, URef, U512,
13};
14
15#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Serialize, Deserialize)]
17#[cfg_attr(feature = "datasize", derive(DataSize))]
18#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
19#[serde(deny_unknown_fields)]
20pub struct DeployInfo {
21 #[serde(with = "serde_helpers::deploy_hash_as_array")]
23 #[cfg_attr(
24 feature = "json-schema",
25 schemars(with = "DeployHash", description = "Hex-encoded Deploy hash.")
26 )]
27 pub deploy_hash: DeployHash,
28 pub transfers: Vec<TransferAddr>,
30 pub from: AccountHash,
32 pub source: URef,
34 pub gas: U512,
36}
37
38impl DeployInfo {
39 pub fn new(
41 deploy_hash: DeployHash,
42 transfers: &[TransferAddr],
43 from: AccountHash,
44 source: URef,
45 gas: U512,
46 ) -> Self {
47 let transfers = transfers.to_vec();
48 DeployInfo {
49 deploy_hash,
50 transfers,
51 from,
52 source,
53 gas,
54 }
55 }
56}
57
58impl FromBytes for DeployInfo {
59 fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
60 let (deploy_hash, rem) = DeployHash::from_bytes(bytes)?;
61 let (transfers, rem) = Vec::<TransferAddr>::from_bytes(rem)?;
62 let (from, rem) = AccountHash::from_bytes(rem)?;
63 let (source, rem) = URef::from_bytes(rem)?;
64 let (gas, rem) = U512::from_bytes(rem)?;
65 Ok((
66 DeployInfo {
67 deploy_hash,
68 transfers,
69 from,
70 source,
71 gas,
72 },
73 rem,
74 ))
75 }
76}
77
78impl ToBytes for DeployInfo {
79 fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
80 let mut result = bytesrepr::allocate_buffer(self)?;
81 self.deploy_hash.write_bytes(&mut result)?;
82 self.transfers.write_bytes(&mut result)?;
83 self.from.write_bytes(&mut result)?;
84 self.source.write_bytes(&mut result)?;
85 self.gas.write_bytes(&mut result)?;
86 Ok(result)
87 }
88
89 fn serialized_length(&self) -> usize {
90 self.deploy_hash.serialized_length()
91 + self.transfers.serialized_length()
92 + self.from.serialized_length()
93 + self.source.serialized_length()
94 + self.gas.serialized_length()
95 }
96
97 fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
98 self.deploy_hash.write_bytes(writer)?;
99 self.transfers.write_bytes(writer)?;
100 self.from.write_bytes(writer)?;
101 self.source.write_bytes(writer)?;
102 self.gas.write_bytes(writer)?;
103 Ok(())
104 }
105}
106
107#[cfg(any(feature = "testing", feature = "gens", test))]
109pub(crate) mod gens {
110 use crate::{
111 gens::{account_hash_arb, u512_arb, uref_arb},
112 transaction::gens::deploy_hash_arb,
113 transfer::gens::transfer_v1_addr_arb,
114 DeployInfo,
115 };
116 use proptest::{collection, prelude::Strategy};
117
118 pub fn deploy_info_arb() -> impl Strategy<Value = DeployInfo> {
119 let transfers_length_range = 0..5;
120 (
121 deploy_hash_arb(),
122 collection::vec(transfer_v1_addr_arb(), transfers_length_range),
123 account_hash_arb(),
124 uref_arb(),
125 u512_arb(),
126 )
127 .prop_map(|(deploy_hash, transfers, from, source, gas)| DeployInfo {
128 deploy_hash,
129 transfers,
130 from,
131 source,
132 gas,
133 })
134 }
135}
136
137#[cfg(test)]
138mod tests {
139 use proptest::prelude::*;
140
141 use crate::bytesrepr;
142
143 use super::gens;
144
145 proptest! {
146 #[test]
147 fn test_serialization_roundtrip(deploy_info in gens::deploy_info_arb()) {
148 bytesrepr::test_serialization_roundtrip(&deploy_info)
149 }
150 }
151}