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

Patch feat/expose expt rpc apis #301

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
de203d5
feat: expose experimantal apis
Aug 3, 2023
9cc0622
feat: impl apis on sandbox
Aug 3, 2023
040e5f0
fix: impl for client
Aug 4, 2023
eb6ab8f
fix: revert unrelated changes
Aug 4, 2023
3908730
feat: impl api with trial test
Aug 4, 2023
689ddd1
upd: adding expt api examples
Aug 4, 2023
567edee
upd: include left expt examples
Aug 8, 2023
356d035
upd: using Option for args
Aug 10, 2023
4173862
upd: add examples & change tx_status
Aug 22, 2023
ff88645
chore: clippy fixes and fmt
Aug 29, 2023
62f9000
Merge branch 'main' into feat/expose-expt-rpc-apis
Sep 1, 2023
7898757
upd: address review
Sep 11, 2023
c0893d4
Merge branch 'main' into feat/expose-expt-rpc-apis
Sep 11, 2023
f180cd7
Merge branch 'main' of github.com:shariffdev/workspaces-rs into feat/…
Sep 13, 2023
6550f45
upd: addressing reviews
Sep 13, 2023
90676de
Merge branch 'main' into patch-feat/expose-expt-rpc-apis
Sep 25, 2023
eb08e19
Merge branch 'main' into patch-feat/expose-expt-rpc-apis
Oct 6, 2023
1212259
Merge branch 'patch-feat/expose-expt-rpc-apis' of github.com:shariffd…
Oct 6, 2023
35547ca
fix: merge misses
Oct 6, 2023
d99ec8c
Merge branch 'main' into patch-feat/expose-expt-rpc-apis
frol Oct 31, 2023
ddb50b4
addr reviews, abstract away low-level pkgs
Nov 1, 2023
aada532
Merge branch 'patch-feat/expose-expt-rpc-apis' of github.com:shariffd…
Nov 1, 2023
2a6ed2d
Merge branch 'main' into patch-feat/expose-expt-rpc-apis
Nov 28, 2023
01955b2
addr review
Nov 29, 2023
aa9d52b
Merge branch 'main' into patch-feat/expose-expt-rpc-apis
Nov 29, 2023
1430dd8
fix clippy
Nov 29, 2023
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
2 changes: 0 additions & 2 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ borsh = "1.2.0"
maplit = "1.0"
near-units = "0.2.0"
near-gas = { version = "0.2.3", features = ["serde", "borsh", "schemars"] }
near-jsonrpc-primitives = "0.17"
near-primitives = "0.17"
serde = "1.0"
serde_with = "3.4"
serde_json = { version = "1.0" }
Expand Down
19 changes: 4 additions & 15 deletions examples/src/changes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use near_primitives::{types::BlockReference, views::StateChangesRequestView};
use serde_json::json;

const STATUS_MSG_WASM_FILEPATH: &str = "./examples/res/status_message.wasm";
Expand All @@ -9,27 +8,17 @@ async fn main() -> anyhow::Result<()> {
let wasm = std::fs::read(STATUS_MSG_WASM_FILEPATH)?;
let contract = worker.dev_deploy(&wasm).await?;

let outcome = contract
_ = contract
.call("set_status")
.args_json(json!({
"message": "hello_world",
}))
.transact()
.await?;

let block_ref = {
let hash = near_primitives::hash::CryptoHash(outcome.outcome().block_hash.0);
BlockReference::BlockId(near_primitives::types::BlockId::Hash(hash))
};

let state_changes = {
StateChangesRequestView::ContractCodeChanges {
account_ids: vec![contract.id().clone()],
}
};
.await?
.into_result()?;

// NOTE: this API is under the "experimental" flag and no guarantees are given.
let res = worker.changes(block_ref, state_changes).await?;
let res = worker.changes(&[contract.id().clone()]).await?;

// Example output:
//
Expand Down
27 changes: 6 additions & 21 deletions examples/src/changes_in_block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use near_primitives::types::BlockReference;
use serde_json::json;

const STATUS_MSG_WASM_FILEPATH: &str = "./examples/res/status_message.wasm";
Expand All @@ -9,40 +8,26 @@ async fn main() -> anyhow::Result<()> {
let wasm = std::fs::read(STATUS_MSG_WASM_FILEPATH)?;
let contract = worker.dev_deploy(&wasm).await?;

let outcome = contract
_ = contract
.call("set_status")
.args_json(json!({
"message": "hello_world",
}))
.transact()
.await?;

let block_ref = {
let hash = near_primitives::hash::CryptoHash(outcome.outcome().block_hash.0);
BlockReference::BlockId(near_primitives::types::BlockId::Hash(hash))
};
.await?
.into_result()?;

// NOTE: this API is under the "experimental" flag and no guarantees are given.
let res = worker.changes_in_block(block_ref).await?;
let res = worker.changes_in_block().await?;

// Example output:
//
// StateChangesInBlockByType RpcStateChangesInBlockByTypeResponse {
// block_hash: 7ifRdyBsJMXVyp8zw8uGdBMaRShiXuD6yghrp66jqrst,
// block_hash: CixdibXkD1ifLmmVXNhEiRGRH6eB9171Q2UhCP2NazJz,
// changes: [
// AccountTouched {
// account_id: AccountId(
// "dev-20230822100117-44171728969098",
// ),
// },
// AccessKeyTouched {
// account_id: AccountId(
// "dev-20230822100117-44171728969098",
// ),
// },
// DataTouched {
// account_id: AccountId(
// "dev-20230822100117-44171728969098",
// "dev-20230913102437-62490697138398",
// ),
// },
// ],
Expand Down
21 changes: 1 addition & 20 deletions examples/src/protocol_config.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
const STATUS_MSG_WASM_FILEPATH: &str = "./examples/res/status_message.wasm";

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let wasm = std::fs::read(STATUS_MSG_WASM_FILEPATH)?;
let contract = worker.dev_deploy(&wasm).await?;

let outcome = contract
.call("set_status")
.args_json(serde_json::json!({
"message": "hello_world",
}))
.transact()
.await?;

let block_reference = {
let hash = outcome.outcome().block_hash;
near_primitives::types::BlockReference::BlockId(near_primitives::types::BlockId::Hash(
near_primitives::hash::CryptoHash(hash.0),
))
};

// NOTE: this API is under the "experimental" flag and no guarantees are given.
let protocol_config = worker.protocol_config(block_reference).await?;
let protocol_config = worker.protocol_config().await?;

// Example output:
//
Expand Down
24 changes: 6 additions & 18 deletions examples/src/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use near_jsonrpc_primitives::types::receipts::ReceiptReference;

const STATUS_MSG_WASM_FILEPATH: &str = "./examples/res/status_message.wasm";

#[tokio::main]
Expand All @@ -16,24 +14,14 @@ async fn main() -> anyhow::Result<()> {
.transact()
.await?;

let receipt_ref = {
let mut ids = outcome.outcome().receipt_ids.clone();
if ids.is_empty() {
println!("no receipt ids present");
return Ok(());
}

println!("receipts found: {ids:?}");

ReceiptReference {
receipt_id: near_primitives::hash::CryptoHash(
ids.pop().expect("expected at least one receipt id").0,
),
}
};
// let receipt_ref = receipt(&outcome.outcome().receipt_ids)?;
let ids = &outcome.outcome().receipt_ids;
println!("receipts found: {ids:?}");

// NOTE: this API is under the "experimental" flag and no guarantees are given.
let resp = worker.receipt(receipt_ref).await?;
let resp = worker
.receipt(ids.last().expect("a receipt id is expected"))
.await?;

println!("ReceiptView: {resp:?}");
Ok(())
Expand Down
14 changes: 4 additions & 10 deletions examples/src/tx_status.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use near_jsonrpc_primitives::types::transactions::TransactionInfo;
use near_primitives::hash::CryptoHash;
use serde_json::json;

const STATUS_MSG_WASM_FILEPATH: &str = "./examples/res/status_message.wasm";
Expand All @@ -18,16 +16,12 @@ async fn main() -> anyhow::Result<()> {
.transact()
.await?;

let tx_info = {
let outcome = outcome.outcome();
TransactionInfo::TransactionId {
hash: CryptoHash(outcome.transaction_hash.0),
account_id: outcome.executor_id.clone(),
}
};
let outcome = outcome.outcome();

// NOTE: this API is under the "experimental" flag and no guarantees are given.
let resp = worker.tx_status(tx_info).await?;
let resp = worker
.tx_status(outcome.transaction_hash, outcome.executor_id.clone())
.await?;

// Example outcome:
//
Expand Down
58 changes: 3 additions & 55 deletions workspaces/src/rpc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,13 @@ use near_primitives::views::{

#[cfg(feature = "experimental")]
use {
near_chain_configs::{GenesisConfig, ProtocolConfigView},
near_jsonrpc_primitives::types::{
changes::RpcStateChangesInBlockByTypeResponse, changes::RpcStateChangesInBlockResponse,
receipts::ReceiptReference, transactions::TransactionInfo,
},
near_chain_configs::GenesisConfig,
near_jsonrpc_primitives::types::{receipts::ReceiptReference, transactions::TransactionInfo},
near_primitives::{
types::MaybeBlockId,
views::{
validator_stake_view::ValidatorStakeView, FinalExecutionOutcomeWithReceiptView,
ReceiptView, StateChangesRequestView,
ReceiptView,
},
},
};
Expand Down Expand Up @@ -356,41 +353,6 @@ impl Client {

#[cfg(feature = "experimental")]
impl Client {
pub(crate) async fn changes_in_block(
&self,
block_reference: BlockReference,
) -> Result<RpcStateChangesInBlockByTypeResponse> {
let resp = self
.rpc_client
.call(
methods::EXPERIMENTAL_changes_in_block::RpcStateChangesInBlockRequest {
block_reference,
},
)
.await
.map_err(|e| RpcErrorCode::QueryFailure.custom(e))?;

Ok(resp)
}

pub(crate) async fn changes(
&self,
block_reference: BlockReference,
state_changes_request: StateChangesRequestView,
) -> Result<RpcStateChangesInBlockResponse> {
let resp = self
.rpc_client
.call(
methods::EXPERIMENTAL_changes::RpcStateChangesInBlockByTypeRequest {
block_reference,
state_changes_request,
},
)
.await
.map_err(|e| RpcErrorCode::QueryFailure.custom(e))?;
Ok(resp)
}

pub(crate) async fn genesis_config(&self) -> Result<GenesisConfig> {
let resp = self
.rpc_client
Expand All @@ -400,20 +362,6 @@ impl Client {
Ok(resp)
}

pub(crate) async fn protocol_config(
&self,
block_reference: BlockReference,
) -> Result<ProtocolConfigView> {
let resp = self
.rpc_client
.call(
methods::EXPERIMENTAL_protocol_config::RpcProtocolConfigRequest { block_reference },
)
.await
.map_err(|e| RpcErrorCode::QueryFailure.custom(e))?;
Ok(resp)
}

pub(crate) async fn receipt(&self, receipt_reference: ReceiptReference) -> Result<ReceiptView> {
let resp = self
.rpc_client
Expand Down
63 changes: 63 additions & 0 deletions workspaces/src/rpc/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ use near_primitives::types::{BlockId, BlockReference, StoreKey};
use near_primitives::views::{BlockView, QueryRequest};
use near_token::NearToken;

#[cfg(feature = "experimental")]
use near_primitives::views::StateChangesRequestView;

use crate::error::RpcErrorCode;
use crate::operations::Function;
use crate::result::ViewResultDetails;
Expand Down Expand Up @@ -185,6 +188,17 @@ pub struct ViewAccessKeyList {

pub struct GasPrice;

#[cfg(feature = "experimental")]
pub struct StateChanges {
pub(crate) state_changes: StateChangesRequestView,
}

#[cfg(feature = "experimental")]
pub struct StateChangesInBlock;

#[cfg(feature = "experimental")]
pub struct ProtocolConfig;

impl ProcessQuery for ViewFunction {
type Method = methods::query::RpcQueryRequest;
type Output = ViewResultDetails;
Expand Down Expand Up @@ -401,6 +415,55 @@ impl ProcessQuery for GasPrice {
}
}

#[cfg(feature = "experimental")]
impl ProcessQuery for StateChanges {
type Method = methods::EXPERIMENTAL_changes::RpcStateChangesInBlockByTypeRequest;
type Output = methods::EXPERIMENTAL_changes::RpcStateChangesInBlockResponse;

fn into_request(self, block_ref: BlockReference) -> Result<Self::Method> {
Ok(Self::Method {
block_reference: block_ref,
state_changes_request: self.state_changes,
})
}

fn from_response(resp: <Self::Method as RpcMethod>::Response) -> Result<Self::Output> {
Ok(resp)
}
}

#[cfg(feature = "experimental")]
impl ProcessQuery for StateChangesInBlock {
type Method = methods::EXPERIMENTAL_changes_in_block::RpcStateChangesInBlockRequest;
type Output = methods::EXPERIMENTAL_changes_in_block::RpcStateChangesInBlockByTypeResponse;

fn into_request(self, block_ref: BlockReference) -> Result<Self::Method> {
Ok(Self::Method {
block_reference: block_ref,
})
}

fn from_response(resp: <Self::Method as RpcMethod>::Response) -> Result<Self::Output> {
Ok(resp)
}
}

#[cfg(feature = "experimental")]
impl ProcessQuery for ProtocolConfig {
type Method = methods::EXPERIMENTAL_protocol_config::RpcProtocolConfigRequest;
type Output = methods::EXPERIMENTAL_protocol_config::RpcProtocolConfigResponse;

fn into_request(self, block_ref: BlockReference) -> Result<Self::Method> {
Ok(Self::Method {
block_reference: block_ref,
})
}

fn from_response(resp: <Self::Method as RpcMethod>::Response) -> Result<Self::Output> {
Ok(resp)
}
}

/// Query object used to query for chunk related details at a specific `ChunkReference` which
/// consists of either a chunk [`CryptoHash`], or a `BlockShardId` (which consists of [`ShardId`]
/// and either block [`CryptoHash`] or [`BlockHeight`]).
Expand Down
Loading
Loading