Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ref #2067: querying contract state #2075

Merged
merged 5 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions core/primitives/src/views.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
use std::fmt;

Expand Down Expand Up @@ -124,7 +123,7 @@ impl From<AccessKeyView> for AccessKey {

#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct ViewStateResult {
pub values: HashMap<Vec<u8>, Vec<u8>>,
pub values: Vec<(String, String)>,
}

#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
Expand Down
18 changes: 6 additions & 12 deletions runtime/runtime/src/state_viewer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::str;
use std::time::Instant;

Expand All @@ -16,6 +15,7 @@ use near_vm_logic::{ReturnData, VMConfig, VMContext};

use crate::actions::get_code_with_cache;
use crate::ext::RuntimeExt;
use near_primitives::serialize::to_base64;

pub struct TrieViewer {}

Expand Down Expand Up @@ -60,14 +60,14 @@ impl TrieViewer {
if !is_valid_account_id(account_id) {
return Err(format!("Account ID '{}' is not valid", account_id).into());
}
let mut values = HashMap::default();
let mut values: Vec<(String, String)> = Default::default();
let mut query = prefix_for_data(account_id);
let acc_sep_len = query.len();
query.extend_from_slice(prefix);
state_update.for_keys_with_prefix(&query, |key| {
// TODO error
if let Ok(Some(value)) = state_update.get(key) {
values.insert(key[acc_sep_len..].to_vec(), value.to_vec());
values.push((to_base64(&key[acc_sep_len..]), to_base64(&value)));
}
})?;
Ok(ViewStateResult { values })
Expand Down Expand Up @@ -238,16 +238,10 @@ mod tests {
let state_update = TrieUpdate::new(trie, new_root);
let trie_viewer = TrieViewer::new();
let result = trie_viewer.view_state(&state_update, &alice_account(), b"").unwrap();
assert_eq!(
result.values,
[(b"test123".to_vec(), b"123".to_vec())].iter().cloned().collect()
);
assert_eq!(result.values, [("dGVzdDEyMw==".to_string(), "MTIz".to_string())]);
let result = trie_viewer.view_state(&state_update, &alice_account(), b"test321").unwrap();
assert_eq!(result.values, [].iter().cloned().collect());
assert_eq!(result.values, []);
let result = trie_viewer.view_state(&state_update, &alice_account(), b"test123").unwrap();
assert_eq!(
result.values,
[(b"test123".to_vec(), b"123".to_vec())].iter().cloned().collect()
)
assert_eq!(result.values, [("dGVzdDEyMw==".to_string(), "MTIz".to_string())])
}
}