sui_move_natives_latest/scratch/
runtime.rs1use better_any::{Tid, TidAble};
5use move_core_types::account_address::AccountAddress;
6use move_vm_runtime::execution::{Type, values::Value};
7use move_vm_runtime::natives::extensions::NativeExtensionMarker;
8use std::collections::BTreeMap;
9use sui_protocol_config::ProtocolConfig;
10
11pub struct ScratchEntry {
15 pub ty: Type,
16 pub value: Value,
17}
18
19#[derive(Tid)]
23pub struct ScratchRuntime<'a> {
24 protocol_config: &'a ProtocolConfig,
25 entries: BTreeMap<AccountAddress, ScratchEntry>,
26}
27
28pub enum AddResult {
30 Inserted,
32 Duplicate,
34 LimitExceeded,
36}
37
38impl<'a> NativeExtensionMarker<'a> for ScratchRuntime<'a> {}
39
40impl<'a> ScratchRuntime<'a> {
41 pub fn new(protocol_config: &'a ProtocolConfig) -> Self {
42 Self {
43 protocol_config,
44 entries: BTreeMap::new(),
45 }
46 }
47
48 pub fn clear(&mut self) {
51 self.entries.clear();
52 }
53
54 pub fn add(&mut self, key: AccountAddress, ty: Type, value: Value) -> AddResult {
58 if self.entries.contains_key(&key) {
59 return AddResult::Duplicate;
60 }
61 if let Some(max) = self.protocol_config.max_scratch_pad_size_as_option()
62 && self.entries.len() as u64 >= max
63 {
64 return AddResult::LimitExceeded;
65 }
66 self.entries.insert(key, ScratchEntry { ty, value });
67 AddResult::Inserted
68 }
69
70 pub fn get(&self, key: &AccountAddress) -> Option<&ScratchEntry> {
71 self.entries.get(key)
72 }
73
74 pub fn remove(&mut self, key: &AccountAddress) -> Option<ScratchEntry> {
75 self.entries.remove(key)
76 }
77
78 pub fn contains(&self, key: &AccountAddress) -> bool {
80 self.entries.contains_key(key)
81 }
82}