neo3/neo_builder/script/
interop_service.rs

1use std::{
2	collections::HashMap,
3	hash::Hash,
4	sync::{Arc, Mutex},
5};
6
7use lazy_static::lazy_static;
8use strum::IntoEnumIterator;
9use strum_macros::{Display, EnumIter, EnumString};
10
11use crate::neo_crypto::HashableForVec;
12
13lazy_static! {
14	static ref INTEROP_SERVICE_HASHES: Arc<Mutex<HashMap<String, String>>> =
15		Arc::new(Mutex::new(HashMap::new()));
16}
17
18#[derive(EnumString, EnumIter, Display, Copy, Clone, PartialEq, Eq)]
19pub enum InteropService {
20	#[strum(serialize = "System.Crypto.CheckSig")]
21	SystemCryptoCheckSig,
22	#[strum(serialize = "System.Crypto.CheckMultisig")]
23	SystemCryptoCheckMultiSig,
24	#[strum(serialize = "System.Contract.Call")]
25	SystemContractCall,
26	#[strum(serialize = "System.Contract.CallNative")]
27	SystemContractCallNative,
28	#[strum(serialize = "System.Contract.GetCallFlags")]
29	SystemContractGetCallFlags,
30	#[strum(serialize = "System.Contract.CreateStandardAccount")]
31	SystemContractCreateStandardAccount,
32	#[strum(serialize = "System.Contract.CreateMultiSigAccount")]
33	SystemContractCreateMultiSigAccount,
34	#[strum(serialize = "System.Contract.NativeOnPersist")]
35	SystemContractNativeOnPersist,
36	#[strum(serialize = "System.Contract.NativePostPersist")]
37	SystemContractNativePostPersist,
38	#[strum(serialize = "System.Iterator.Next")]
39	SystemIteratorNext,
40	#[strum(serialize = "System.Iterator.Value")]
41	SystemIteratorValue,
42	#[strum(serialize = "System.Runtime.Platform")]
43	SystemRuntimePlatform,
44	#[strum(serialize = "System.Runtime.GetTrigger")]
45	SystemRuntimeGetTrigger,
46	#[strum(serialize = "System.Runtime.GetTime")]
47	SystemRuntimeGetTime,
48	#[strum(serialize = "System.Runtime.GetScriptContainer")]
49	SystemRuntimeGetScriptContainer,
50	#[strum(serialize = "System.Runtime.GetExecutingScriptHash")]
51	SystemRuntimeGetExecutingScriptHash,
52	#[strum(serialize = "System.Runtime.GetCallingScriptHash")]
53	SystemRuntimeGetCallingScriptHash,
54	#[strum(serialize = "System.Runtime.GetEntryScriptHash")]
55	SystemRuntimeGetEntryScriptHash,
56	#[strum(serialize = "System.Runtime.CheckWitness")]
57	SystemRuntimeCheckWitness,
58	#[strum(serialize = "System.Runtime.GetInvocationCounter")]
59	SystemRuntimeGetInvocationCounter,
60	#[strum(serialize = "System.Runtime.Log")]
61	SystemRuntimeLog,
62	#[strum(serialize = "System.Runtime.Notify")]
63	SystemRuntimeNotify,
64	#[strum(serialize = "System.Runtime.GetNotifications")]
65	SystemRuntimeGetNotifications,
66	#[strum(serialize = "System.Runtime.GasLeft")]
67	SystemRuntimeGasLeft,
68	#[strum(serialize = "System.Runtime.BurnGas")]
69	SystemRuntimeBurnGas,
70	#[strum(serialize = "System.Runtime.GetNetwork")]
71	SystemRuntimeGetNetwork,
72	#[strum(serialize = "System.Runtime.GetRandom")]
73	SystemRuntimeGetRandom,
74	#[strum(serialize = "System.Storage.GetContext")]
75	SystemStorageGetContext,
76	#[strum(serialize = "System.Storage.GetReadOnlyContext")]
77	SystemStorageGetReadOnlyContext,
78	#[strum(serialize = "System.Storage.AsReadOnly")]
79	SystemStorageAsReadOnly,
80	#[strum(serialize = "System.Storage.Get")]
81	SystemStorageGet,
82	#[strum(serialize = "System.Storage.Find")]
83	SystemStorageFind,
84	#[strum(serialize = "System.Storage.Put")]
85	SystemStoragePut,
86	#[strum(serialize = "System.Storage.Delete")]
87	SystemStorageDelete,
88}
89
90impl InteropService {
91	pub fn hash(&self) -> String {
92		// May introduce problem, temporily remove
93		// let mut hashes = INTEROP_SERVICE_HASHES.lock().unwrap();
94		// return if let Some(hash) = hashes.get(self.to_string().as_str()) {
95		// 	hash.clone()
96		// } else {
97		let self_string = self.to_string();
98		let sha = self_string.as_bytes().hash256();
99		let hash = hex::encode(sha[..4].to_vec());
100		// hashes.insert(self.to_string(), hash.clone());
101		hash
102	}
103
104	pub fn from_hash(hash: String) -> Option<InteropService> {
105		InteropService::iter().find(|service| service.hash() == hash)
106	}
107	pub fn price(&self) -> u64 {
108		match self {
109			InteropService::SystemRuntimePlatform
110			| InteropService::SystemRuntimeGetTrigger
111			| InteropService::SystemRuntimeGetTime
112			| InteropService::SystemRuntimeGetScriptContainer
113			| InteropService::SystemRuntimeGetNetwork => 1 << 3,
114
115			InteropService::SystemIteratorValue
116			| InteropService::SystemRuntimeGetExecutingScriptHash
117			| InteropService::SystemRuntimeGetCallingScriptHash
118			| InteropService::SystemRuntimeGetEntryScriptHash
119			| InteropService::SystemRuntimeGetInvocationCounter
120			| InteropService::SystemRuntimeGasLeft
121			| InteropService::SystemRuntimeBurnGas
122			| InteropService::SystemRuntimeGetRandom
123			| InteropService::SystemStorageGetContext
124			| InteropService::SystemStorageGetReadOnlyContext
125			| InteropService::SystemStorageAsReadOnly => 1 << 4,
126
127			InteropService::SystemContractGetCallFlags
128			| InteropService::SystemRuntimeCheckWitness => 1 << 10,
129
130			InteropService::SystemRuntimeGetNotifications => 1 << 12,
131
132			InteropService::SystemCryptoCheckSig
133			| InteropService::SystemContractCall
134			| InteropService::SystemContractCreateStandardAccount
135			| InteropService::SystemIteratorNext
136			| InteropService::SystemRuntimeLog
137			| InteropService::SystemRuntimeNotify
138			| InteropService::SystemStorageGet
139			| InteropService::SystemStorageFind
140			| InteropService::SystemStoragePut
141			| InteropService::SystemStorageDelete => 1 << 15,
142			_ => 0,
143		}
144	}
145}