casper_types/
deploy_info.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use alloc::vec::Vec;

#[cfg(feature = "datasize")]
use datasize::DataSize;
#[cfg(feature = "json-schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{
    account::AccountHash,
    bytesrepr::{self, FromBytes, ToBytes},
    serde_helpers, DeployHash, TransferAddr, URef, U512,
};

/// Information relating to the given Deploy.
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "datasize", derive(DataSize))]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct DeployInfo {
    /// The relevant Deploy.
    #[serde(with = "serde_helpers::deploy_hash_as_array")]
    #[cfg_attr(
        feature = "json-schema",
        schemars(with = "DeployHash", description = "Hex-encoded Deploy hash.")
    )]
    pub deploy_hash: DeployHash,
    /// Version 1 transfers performed by the Deploy.
    pub transfers: Vec<TransferAddr>,
    /// Account identifier of the creator of the Deploy.
    pub from: AccountHash,
    /// Source purse used for payment of the Deploy.
    pub source: URef,
    /// Gas cost of executing the Deploy.
    pub gas: U512,
}

impl DeployInfo {
    /// Creates a [`DeployInfo`].
    pub fn new(
        deploy_hash: DeployHash,
        transfers: &[TransferAddr],
        from: AccountHash,
        source: URef,
        gas: U512,
    ) -> Self {
        let transfers = transfers.to_vec();
        DeployInfo {
            deploy_hash,
            transfers,
            from,
            source,
            gas,
        }
    }
}

impl FromBytes for DeployInfo {
    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
        let (deploy_hash, rem) = DeployHash::from_bytes(bytes)?;
        let (transfers, rem) = Vec::<TransferAddr>::from_bytes(rem)?;
        let (from, rem) = AccountHash::from_bytes(rem)?;
        let (source, rem) = URef::from_bytes(rem)?;
        let (gas, rem) = U512::from_bytes(rem)?;
        Ok((
            DeployInfo {
                deploy_hash,
                transfers,
                from,
                source,
                gas,
            },
            rem,
        ))
    }
}

impl ToBytes for DeployInfo {
    fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
        let mut result = bytesrepr::allocate_buffer(self)?;
        self.deploy_hash.write_bytes(&mut result)?;
        self.transfers.write_bytes(&mut result)?;
        self.from.write_bytes(&mut result)?;
        self.source.write_bytes(&mut result)?;
        self.gas.write_bytes(&mut result)?;
        Ok(result)
    }

    fn serialized_length(&self) -> usize {
        self.deploy_hash.serialized_length()
            + self.transfers.serialized_length()
            + self.from.serialized_length()
            + self.source.serialized_length()
            + self.gas.serialized_length()
    }

    fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
        self.deploy_hash.write_bytes(writer)?;
        self.transfers.write_bytes(writer)?;
        self.from.write_bytes(writer)?;
        self.source.write_bytes(writer)?;
        self.gas.write_bytes(writer)?;
        Ok(())
    }
}

/// Generators for a `DeployInfo`
#[cfg(any(feature = "testing", feature = "gens", test))]
pub(crate) mod gens {
    use crate::{
        gens::{account_hash_arb, u512_arb, uref_arb},
        transaction::gens::deploy_hash_arb,
        transfer::gens::transfer_v1_addr_arb,
        DeployInfo,
    };
    use proptest::{collection, prelude::Strategy};

    pub fn deploy_info_arb() -> impl Strategy<Value = DeployInfo> {
        let transfers_length_range = 0..5;
        (
            deploy_hash_arb(),
            collection::vec(transfer_v1_addr_arb(), transfers_length_range),
            account_hash_arb(),
            uref_arb(),
            u512_arb(),
        )
            .prop_map(|(deploy_hash, transfers, from, source, gas)| DeployInfo {
                deploy_hash,
                transfers,
                from,
                source,
                gas,
            })
    }
}

#[cfg(test)]
mod tests {
    use proptest::prelude::*;

    use crate::bytesrepr;

    use super::gens;

    proptest! {
        #[test]
        fn test_serialization_roundtrip(deploy_info in gens::deploy_info_arb()) {
            bytesrepr::test_serialization_roundtrip(&deploy_info)
        }
    }
}