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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
//! External dependencies of the near-vm-logic. use crate::types::{AccountId, Balance, Gas, PublicKey, ReceiptIndex}; use near_vm_errors::VMLogicError; /// An abstraction over the memory of the smart contract. pub trait MemoryLike { /// Returns whether the memory interval is completely inside the smart contract memory. fn fits_memory(&self, offset: u64, len: u64) -> bool; /// Reads the content of the given memory interval. /// /// # Panics /// /// If memory interval is outside the smart contract memory. fn read_memory(&self, offset: u64, buffer: &mut [u8]); /// Reads a single byte from the memory. /// /// # Panics /// /// If pointer is outside the smart contract memory. fn read_memory_u8(&self, offset: u64) -> u8; /// Writes the buffer into the smart contract memory. /// /// # Panics /// /// If `offset + buffer.len()` is outside the smart contract memory. fn write_memory(&mut self, offset: u64, buffer: &[u8]); } pub type Result<T> = ::std::result::Result<T, VMLogicError>; /// Logical pointer to a value in storage. /// Allows getting value length before getting the value itself. This is needed so that runtime /// can charge gas before accessing a potentially large value. pub trait ValuePtr { /// Returns the length of the value fn len(&self) -> u32; /// Dereferences the pointer. /// Takes a box because currently runtime code uses dynamic dispatch. /// # Errors /// StorageError if reading from storage fails fn deref(&self) -> Result<Vec<u8>>; } /// An external blockchain interface for the Runtime logic pub trait External { /// Write to the storage trie of the current account /// /// # Arguments /// /// * `key` - a key for a new value /// * `value` - a new value to be set /// /// # Errors /// /// This function could return HostErrorOrStorageError::StorageError on underlying DB failure /// /// # Example /// ``` /// # use near_vm_logic::mocks::mock_external::MockedExternal; /// # use near_vm_logic::External; /// /// # let mut external = MockedExternal::new(); /// assert_eq!(external.storage_set(b"key42", b"value1337"), Ok(())); /// // Should return an old value if the key exists /// assert_eq!(external.storage_set(b"key42", b"new_value"), Ok(())); /// ``` fn storage_set(&mut self, key: &[u8], value: &[u8]) -> Result<()>; /// Reads from the storage trie of the current account /// /// # Arguments /// /// * `key` - a key to read /// /// # Errors /// /// This function could return HostErrorOrStorageError::StorageError on underlying DB failure /// /// # Example /// ``` /// # use near_vm_logic::mocks::mock_external::{MockedExternal}; /// # use near_vm_logic::{External, ValuePtr}; /// /// # let mut external = MockedExternal::new(); /// external.storage_set(b"key42", b"value1337").unwrap(); /// assert_eq!(external.storage_get(b"key42").unwrap().map(|ptr| ptr.deref().unwrap()), Some(b"value1337".to_vec())); /// // Returns Ok(None) if there is no value for a key /// assert_eq!(external.storage_get(b"no_key").unwrap().map(|ptr| ptr.deref().unwrap()), None); /// ``` fn storage_get<'a>(&'a self, key: &[u8]) -> Result<Option<Box<dyn ValuePtr + 'a>>>; /// Removes the key from the storage /// /// # Arguments /// /// * `key` - a key to remove /// /// # Errors /// /// This function could return HostErrorOrStorageError::StorageError on underlying DB failure /// /// # Example /// ``` /// # use near_vm_logic::mocks::mock_external::MockedExternal; /// # use near_vm_logic::External; /// /// # let mut external = MockedExternal::new(); /// external.storage_set(b"key42", b"value1337").unwrap(); /// // Returns value if exists /// assert_eq!(external.storage_remove(b"key42"), Ok(())); /// // Returns None if there was no value /// assert_eq!(external.storage_remove(b"no_value_key"), Ok(())); /// ``` fn storage_remove(&mut self, key: &[u8]) -> Result<()>; /// Check whether key exists. Returns Ok(true) if key exists or Ok(false) otherwise /// /// # Arguments /// /// * `key` - a key to check /// /// # Errors /// /// This function could return HostErrorOrStorageError::StorageError on underlying DB failure /// /// # Example /// ``` /// # use near_vm_logic::mocks::mock_external::MockedExternal; /// # use near_vm_logic::External; /// /// # let mut external = MockedExternal::new(); /// external.storage_set(b"key42", b"value1337").unwrap(); /// // Returns value if exists /// assert_eq!(external.storage_has_key(b"key42"), Ok(true)); /// // Returns None if there was no value /// assert_eq!(external.storage_has_key(b"no_value_key"), Ok(false)); /// ``` fn storage_has_key(&mut self, key: &[u8]) -> Result<bool>; /// Creates a receipt which will be executed after `receipt_indices` /// /// # Arguments /// /// * `receipt_indices` - a list of receipt indices the new receipt is depend on /// /// # Example /// ``` /// # use near_vm_logic::mocks::mock_external::MockedExternal; /// # use near_vm_logic::External; /// /// # let mut external = MockedExternal::new(); /// let receipt_index_one = external.create_receipt(vec![], "charli.near".to_owned()).unwrap(); /// let receipt_index_two = external.create_receipt(vec![receipt_index_one], "bob.near".to_owned()); /// /// ``` /// /// # Panics /// Panics if one of `receipt_indices` is missing fn create_receipt( &mut self, receipt_indices: Vec<ReceiptIndex>, receiver_id: AccountId, ) -> Result<ReceiptIndex>; /// Attaches an `Action::CreateAccount` action to an existing receipt /// /// # Arguments /// /// * `receipt_index` - an index of Receipt to append an action /// /// # Example /// ``` /// # use near_vm_logic::mocks::mock_external::MockedExternal; /// # use near_vm_logic::External; /// /// # let mut external = MockedExternal::new(); /// let receipt_index = external.create_receipt(vec![], "charli.near".to_owned()).unwrap(); /// external.append_action_create_account(receipt_index).unwrap(); /// /// ``` /// /// # Panics /// Panics if `receipt_index` is missing fn append_action_create_account(&mut self, receipt_index: ReceiptIndex) -> Result<()>; /// Attaches an `Action::DeployContract` action to an existing receipt /// /// # Arguments /// /// * `receipt_index` - an index of Receipt to append an action /// * `code` - a Wasm code to attach /// /// # Example /// ``` /// # use near_vm_logic::mocks::mock_external::MockedExternal; /// # use near_vm_logic::External; /// /// # let mut external = MockedExternal::new(); /// let receipt_index = external.create_receipt(vec![], "charli.near".to_owned()).unwrap(); /// external.append_action_deploy_contract(receipt_index, b"some valid Wasm code".to_vec()).unwrap(); /// /// ``` /// /// # Panics /// Panics if `receipt_index` is missing fn append_action_deploy_contract( &mut self, receipt_index: ReceiptIndex, code: Vec<u8>, ) -> Result<()>; /// Attaches an `Action::FunctionCall` action to an existing receipt /// /// # Arguments /// /// * `receipt_index` - an index of Receipt to append an action /// * `method_name` - a name of the contract method to call /// * `arguments` - a Wasm code to attach /// * `attached_deposit` - amount of tokens to transfer with the call /// * `prepaid_gas` - amount of prepaid gas to attach to the call /// /// # Example /// ``` /// # use near_vm_logic::mocks::mock_external::MockedExternal; /// # use near_vm_logic::External; /// /// # let mut external = MockedExternal::new(); /// let receipt_index = external.create_receipt(vec![], "charli.near".to_owned()).unwrap(); /// external.append_action_function_call( /// receipt_index, /// b"method_name".to_vec(), /// b"{serialised: arguments}".to_vec(), /// 100000u128, /// 100u64 /// ).unwrap(); /// /// ``` /// /// # Panics /// Panics if `receipt_index` is missing fn append_action_function_call( &mut self, receipt_index: ReceiptIndex, method_name: Vec<u8>, arguments: Vec<u8>, attached_deposit: Balance, prepaid_gas: Gas, ) -> Result<()>; /// Attaches an `TransferAction` action to an existing receipt /// /// # Arguments /// /// * `receipt_index` - an index of Receipt to append an action /// * `amount` - amount of tokens to transfer /// /// # Example /// ``` /// # use near_vm_logic::mocks::mock_external::MockedExternal; /// # use near_vm_logic::External; /// /// # let mut external = MockedExternal::new(); /// let receipt_index = external.create_receipt(vec![], "charli.near".to_owned()).unwrap(); /// external.append_action_transfer( /// receipt_index, /// 100000u128, /// ).unwrap(); /// /// ``` /// /// # Panics /// Panics if `receipt_index` is missing fn append_action_transfer( &mut self, receipt_index: ReceiptIndex, amount: Balance, ) -> Result<()>; /// Attaches an `StakeAction` action to an existing receipt /// /// # Arguments /// /// * `receipt_index` - an index of Receipt to append an action /// * `stake` - amount of tokens to stake /// * `public_key` - a validator public key /// /// # Example /// ``` /// # use near_vm_logic::mocks::mock_external::MockedExternal; /// # use near_vm_logic::External; /// /// # let mut external = MockedExternal::new(); /// let receipt_index = external.create_receipt(vec![], "charli.near".to_owned()).unwrap(); /// external.append_action_stake( /// receipt_index, /// 100000u128, /// b"some public key".to_vec() /// ).unwrap(); /// /// ``` /// /// # Panics /// Panics if `receipt_index` is missing fn append_action_stake( &mut self, receipt_index: ReceiptIndex, stake: Balance, public_key: PublicKey, ) -> Result<()>; /// Attaches an `AddKeyAction` action to an existing receipt /// /// # Arguments /// /// * `receipt_index` - an index of Receipt to append an action /// * `public_key` - a public key for an access key /// * `nonce` - a nonce /// /// # Example /// ``` /// # use near_vm_logic::mocks::mock_external::MockedExternal; /// # use near_vm_logic::External; /// /// # let mut external = MockedExternal::new(); /// let receipt_index = external.create_receipt(vec![], "charli.near".to_owned()).unwrap(); /// external.append_action_add_key_with_full_access( /// receipt_index, /// b"some public key".to_vec(), /// 0u64 /// ).unwrap(); /// /// ``` /// /// # Panics /// Panics if `receipt_index` is missing fn append_action_add_key_with_full_access( &mut self, receipt_index: ReceiptIndex, public_key: PublicKey, nonce: u64, ) -> Result<()>; /// Attaches an `AddKeyAction` action to an existing receipt with `AccessKeyPermission::FunctionCall` /// /// # Arguments /// /// * `receipt_index` - an index of Receipt to append an action /// * `public_key` - a public key for an access key /// * `nonce` - a nonce /// * `allowance` - amount of tokens allowed to spend by this access key /// * `receiver_id` - a contract witch will be allowed to call with this access key /// * `method_names` - a list of method names is allowed to call with this access key (empty = any method) /// /// # Example /// ``` /// # use near_vm_logic::mocks::mock_external::MockedExternal; /// # use near_vm_logic::External; /// /// # let mut external = MockedExternal::new(); /// let receipt_index = external.create_receipt(vec![], "charli.near".to_owned()).unwrap(); /// external.append_action_add_key_with_function_call( /// receipt_index, /// b"some public key".to_vec(), /// 0u64, /// None, /// "bob.near".to_owned(), /// vec![b"foo".to_vec(), b"bar".to_vec()] /// ).unwrap(); /// /// ``` /// /// # Panics /// Panics if `receipt_index` is missing fn append_action_add_key_with_function_call( &mut self, receipt_index: ReceiptIndex, public_key: PublicKey, nonce: u64, allowance: Option<Balance>, receiver_id: AccountId, method_names: Vec<Vec<u8>>, ) -> Result<()>; /// Attaches an `DeleteKeyAction` action to an existing receipt /// /// # Arguments /// /// * `receipt_index` - an index of Receipt to append an action /// * `public_key` - a public key for an access key to delete /// /// # Example /// ``` /// # use near_vm_logic::mocks::mock_external::MockedExternal; /// # use near_vm_logic::External; /// /// # let mut external = MockedExternal::new(); /// let receipt_index = external.create_receipt(vec![], "charli.near".to_owned()).unwrap(); /// external.append_action_delete_key( /// receipt_index, /// b"some public key".to_vec() /// ).unwrap(); /// /// ``` /// /// # Panics /// Panics if `receipt_index` is missing fn append_action_delete_key( &mut self, receipt_index: ReceiptIndex, public_key: PublicKey, ) -> Result<()>; /// Attaches an `DeleteAccountAction` action to an existing receipt /// /// # Arguments /// /// * `receipt_index` - an index of Receipt to append an action /// * `beneficiary_id` - an account id to which the rest of the funds of the removed account will be transferred /// /// # Example /// ``` /// # use near_vm_logic::mocks::mock_external::MockedExternal; /// # use near_vm_logic::External; /// /// # let mut external = MockedExternal::new(); /// let receipt_index = external.create_receipt(vec![], "charli.near".to_owned()).unwrap(); /// external.append_action_delete_account( /// receipt_index, /// "sam".to_owned() /// ).unwrap(); /// /// ``` /// /// # Panics /// Panics if `receipt_index` is missing fn append_action_delete_account( &mut self, receipt_index: ReceiptIndex, beneficiary_id: AccountId, ) -> Result<()>; /// Returns amount of touched trie nodes by storage operations fn get_touched_nodes_count(&self) -> u64; /// Resets amount of touched trie nodes by storage operations fn reset_touched_nodes_counter(&mut self); /// Returns the validator stake for given account in the current epoch. /// If the account is not a validator, returns `None`. fn validator_stake(&self, account_id: &AccountId) -> Result<Option<Balance>>; /// Returns total stake of validators in the current epoch. fn validator_total_stake(&self) -> Result<Balance>; }