Skip to content

Commit

Permalink
Expose information about the size of recorded trie storage to the run…
Browse files Browse the repository at this point in the history
…time

To enforce the hard per-receipt limit we need to monitor how much storage
proof has been recorded during execution of the receipt and halt the
execution when the size of generated storage proof goes over the limit.

To achieve this the runtime needs to be able to see how much proof was recorded,
so let's expose this information so that it's available from the runtime.

`recorded_storage_size()` doesn't provide the exact size of storage
proof, as it doesn't cover some corner cases (see near#10890),
so we use the `upper_bound` version to estimate how much storage proof
could've been generated by the receipt. As long as upper bound is
under the limit we can be sure that the actual value is also
under the limit.
  • Loading branch information
jancionear committed Apr 16, 2024
1 parent bd80b1d commit 60e80f0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions runtime/near-vm-runner/src/logic/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ pub trait External {
/// Returns amount of touched trie nodes by storage operations
fn get_trie_nodes_count(&self) -> TrieNodesCount;

/// Size of the recorded trie storage proof.
fn get_recorded_storage_size(&self) -> usize;

/// 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>>;
Expand Down
4 changes: 4 additions & 0 deletions runtime/near-vm-runner/src/logic/mocks/mock_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ impl External for MockedExternal {
TrieNodesCount { db_reads: 0, mem_reads: 0 }
}

fn get_recorded_storage_size(&self) -> usize {
0
}

fn validator_stake(&self, account_id: &AccountId) -> Result<Option<Balance>> {
Ok(self.validators.get(account_id).cloned())
}
Expand Down
9 changes: 9 additions & 0 deletions runtime/runtime/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ impl<'a> External for RuntimeExt<'a> {
Convert::convert(self.trie_update.trie().get_trie_nodes_count())
}

fn get_recorded_storage_size(&self) -> usize {
// `recorded_storage_size()` doesn't provide the exact size of storage proof
// as it doesn't cover some corner cases (see https://github.com/near/nearcore/issues/10890),
// so we use the `upper_bound` version to estimate how much storage proof
// could've been generated by the receipt. As long as upper bound is
// under the limit we can be sure that the actual value is also under the limit.
self.trie_update.trie().recorded_storage_size_upper_bound()
}

fn validator_stake(&self, account_id: &AccountId) -> ExtResult<Option<Balance>> {
self.epoch_info_provider
.validator_stake(self.epoch_id, self.prev_block_hash, account_id)
Expand Down

0 comments on commit 60e80f0

Please sign in to comment.