near_primitives_core/
code.rs

1use std::fmt::{Debug, Formatter};
2
3use crate::hash::{hash as sha256, CryptoHash};
4
5pub struct ContractCode {
6    code: Vec<u8>,
7    hash: CryptoHash,
8}
9
10impl ContractCode {
11    pub fn new(code: Vec<u8>, hash: Option<CryptoHash>) -> ContractCode {
12        let hash = hash.unwrap_or_else(|| sha256(&code));
13        debug_assert_eq!(hash, sha256(&code));
14
15        ContractCode { code, hash }
16    }
17
18    pub fn code(&self) -> &[u8] {
19        self.code.as_slice()
20    }
21
22    pub fn into_code(self) -> Vec<u8> {
23        self.code
24    }
25
26    pub fn hash(&self) -> &CryptoHash {
27        &self.hash
28    }
29
30    pub fn clone_for_tests(&self) -> Self {
31        Self { code: self.code.clone(), hash: self.hash }
32    }
33
34    /// Destructs this instance and returns the code.
35    pub fn take_code(self) -> Vec<u8> {
36        self.code
37    }
38}
39
40impl Debug for ContractCode {
41    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
42        f.debug_struct("ContractCode")
43            .field("hash", &self.hash)
44            .field("code_size", &self.code.len())
45            .finish()
46    }
47}