Skip to content

Commit

Permalink
Add additional mechanisms to introspect state witnesses
Browse files Browse the repository at this point in the history
In particular this code is what I had used to inspect the contents of
the state witnesses and the contribution of trie's contents to the state
witness size.

This may or may not be useful, so am submitting this as a PR.
  • Loading branch information
nagisa committed Oct 1, 2024
1 parent 6b76ee6 commit cedb679
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
3 changes: 2 additions & 1 deletion core/primitives/src/challenge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ impl Debug for PartialState {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
PartialState::TrieValues(values) => {
f.write_str(&format!("{} trie values", values.len()))
let size = bytesize::ByteSize(values.iter().map(|v| v.len() as u64).sum());
f.write_str(&format!("{} trie values sized {}", values.len(), size))
}
}
}
Expand Down
58 changes: 53 additions & 5 deletions core/primitives/src/state_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ use crate::account::{AccessKey, Account};
use crate::hash::{hash, CryptoHash};
use crate::receipt::{Receipt, ReceivedData};
use crate::trie_key::trie_key_parsers::{
parse_account_id_from_access_key_key, parse_account_id_from_account_key,
parse_account_id_from_contract_code_key, parse_account_id_from_contract_data_key,
parse_account_id_from_received_data_key, parse_data_id_from_received_data_key,
parse_data_key_from_contract_data_key, parse_public_key_from_access_key_key,
parse_account_id_from_access_key_key, parse_account_id_from_account_key, parse_account_id_from_contract_code_key, parse_account_id_from_contract_data_key, parse_account_id_from_raw_key, parse_account_id_from_received_data_key, parse_data_id_from_received_data_key, parse_data_key_from_contract_data_key, parse_public_key_from_access_key_key
};
use crate::trie_key::{col, TrieKey};
use crate::trie_key::{col, TrieKey, ACCESS_KEY_SEPARATOR, ACCOUNT_DATA_SEPARATOR};
use crate::types::{AccountId, StoreKey, StoreValue};
use borsh::BorshDeserialize;
use near_crypto::PublicKey;
Expand Down Expand Up @@ -103,6 +100,57 @@ impl StateRecord {
let receipt = Receipt::try_from_slice(&value)?;
Some(StateRecord::DelayedReceipt(Box::new(receipt)))
}
0xff => {
if let Some(index) = key.iter().position(|v| *v == ACCOUNT_DATA_SEPARATOR || *v == ACCESS_KEY_SEPARATOR) {
let column = key[index + 1];
println!("col {}", column);
match column {
col::CONTRACT_DATA => {
let account_id = parse_account_id_from_contract_data_key(&key)?;
let data_key =
parse_data_key_from_contract_data_key(&key, &account_id)?;
Some(StateRecord::Data {
account_id,
data_key: data_key.to_vec().into(),
value: value.into(),
})
}
col::ACCESS_KEY => {
let access_key = AccessKey::try_from_slice(&value)?;
let account_id = parse_account_id_from_access_key_key(&key)?;
let public_key =
parse_public_key_from_access_key_key(&key, &account_id)?;
Some(StateRecord::AccessKey { account_id, public_key, access_key })
}
col::RECEIVED_DATA => {
let data = ReceivedData::try_from_slice(&value)?.data;
let account_id = parse_account_id_from_received_data_key(&key)?;
let data_id = parse_data_id_from_received_data_key(&key, &account_id)?;
Some(StateRecord::ReceivedData { account_id, data_id, data })
}
_ => {
println!("key {:?} is unreachable", key);
None
},
}
} else {
println!("col {:?}", key.last());
match key.last() {
Some(&col::ACCOUNT) => Some(StateRecord::Account {
account_id: parse_account_id_from_account_key(&key)?,
account: Account::try_from_slice(&value)?,
}),
Some(&col::CONTRACT_CODE) => Some(StateRecord::Contract {
account_id: parse_account_id_from_contract_code_key(&key)?,
code: value,
}),
_ => {
println!("key {:?} is unreachable", key);
None
}
}
}
}
_ => {
println!("key[0]: {} is unreachable", key[0]);
None
Expand Down

0 comments on commit cedb679

Please sign in to comment.