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