Skip to content

Commit

Permalink
fix!: Key type of state map and remove exposed primitives type (#109)
Browse files Browse the repository at this point in the history
* fix: Key type of state map, remove exposed primitives type, add helper to contract function

* remove duplicate view_state function
  • Loading branch information
austinabell committed Apr 5, 2022
1 parent 8f12f3d commit b45f4b5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion examples/src/spooning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async fn main() -> anyhow::Result<()> {

let mut state_items = worker.view_state(&contract_id, None).await?;

let state = state_items.remove("STATE").unwrap();
let state = state_items.remove(b"STATE".as_slice()).unwrap();
let status_msg = StatusMessage::try_from_slice(&state)?;

(contract_id, status_msg)
Expand Down
16 changes: 2 additions & 14 deletions workspaces/src/rpc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,7 @@ impl Client {
pub(crate) async fn view_state(
&self,
contract_id: AccountId,
prefix: Option<StoreKey>,
) -> anyhow::Result<HashMap<String, Vec<u8>>> {
self.view_state_raw(contract_id, prefix, None)
.await?
.into_iter()
.map(|(k, v)| Ok((String::from_utf8(k)?, v.to_vec())))
.collect()
}

pub(crate) async fn view_state_raw(
&self,
contract_id: AccountId,
prefix: Option<StoreKey>,
prefix: Option<&[u8]>,
block_id: Option<BlockId>,
) -> anyhow::Result<HashMap<Vec<u8>, Vec<u8>>> {
let block_reference = block_id
Expand All @@ -194,7 +182,7 @@ impl Client {
block_reference,
request: QueryRequest::ViewState {
account_id: contract_id,
prefix: prefix.clone().unwrap_or_else(|| vec![].into()),
prefix: StoreKey::from(prefix.map(Vec::from).unwrap_or_default()),
},
})
.await?;
Expand Down
2 changes: 1 addition & 1 deletion workspaces/src/rpc/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl<'a, 'b> ImportContractTransaction<'a, 'b> {
if self.import_data {
records.extend(
self.from_network
.view_state_raw(account_id.clone(), None, self.block_id)
.view_state(account_id.clone(), None, self.block_id)
.await?
.into_iter()
.map(|(key, value)| StateRecord::Data {
Expand Down
10 changes: 10 additions & 0 deletions workspaces/src/types/account.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::path::Path;

use near_primitives::views::AccountView;
Expand Down Expand Up @@ -209,6 +210,15 @@ impl Contract {
worker.view_code(self.id()).await
}

/// View a contract's state map of key value pairs.
pub async fn view_state<T: Network>(
&self,
worker: &Worker<T>,
prefix: Option<&[u8]>,
) -> anyhow::Result<HashMap<Vec<u8>, Vec<u8>>> {
worker.view_state(self.id(), prefix).await
}

/// Views the current contract's details such as balance and storage usage.
pub async fn view_account<T: Network>(
&self,
Expand Down
10 changes: 6 additions & 4 deletions workspaces/src/worker/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::worker::Worker;
use crate::{Account, Block, Contract};
use crate::{AccountDetails, Network};
use async_trait::async_trait;
use near_primitives::types::{Balance, StoreKey};
use near_primitives::types::Balance;
use std::collections::HashMap;

impl<T> Clone for Worker<T> {
Expand Down Expand Up @@ -107,9 +107,11 @@ where
pub async fn view_state(
&self,
contract_id: &AccountId,
prefix: Option<StoreKey>,
) -> anyhow::Result<HashMap<String, Vec<u8>>> {
self.client().view_state(contract_id.clone(), prefix).await
prefix: Option<&[u8]>,
) -> anyhow::Result<HashMap<Vec<u8>, Vec<u8>>> {
self.client()
.view_state(contract_id.clone(), prefix, None)
.await
}

/// View the latest block from the network
Expand Down
4 changes: 2 additions & 2 deletions workspaces/tests/patch_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ async fn view_status_state(
.transact()
.await?;

let mut state_items = worker.view_state(contract.id(), None).await?;
let mut state_items = contract.view_state(&worker, None).await?;
let state = state_items
.remove("STATE")
.remove(b"STATE".as_slice())
.ok_or_else(|| anyhow::anyhow!("Could not retrieve STATE"))?;
let status_msg: StatusMessage = StatusMessage::try_from_slice(&state)?;

Expand Down

0 comments on commit b45f4b5

Please sign in to comment.