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

feat: expose experimental apis #285

Merged
merged 13 commits into from Sep 11, 2023
2 changes: 2 additions & 0 deletions workspaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ near-primitives = "0.17"
near-jsonrpc-primitives = "0.17"
near-jsonrpc-client = { version = "0.6", features = ["sandbox"] }
near-sandbox-utils = "0.6.2"
near-chain-configs = { version = "0.17.0", optional = true }

[build-dependencies]
near-sandbox-utils = "0.6.2"
Expand All @@ -60,6 +61,7 @@ default = ["install", "interop_sdk"]
install = [] # Install the sandbox binary during compile time
interop_sdk = ["near-sdk"]
unstable = ["cargo_metadata"]
experimental = ["near-chain-configs"]

[package.metadata.docs.rs]
features = ["unstable"]
24 changes: 24 additions & 0 deletions workspaces/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,30 @@ pub enum SandboxErrorCode {
PatchStateFailure,
#[error("Sandbox failed to fast forward")]
FastForwardFailure,
#[cfg(feature = "experimental")]
#[error("Sandbox failed to check transaction")]
CheckTxFailure,
#[cfg(feature = "experimental")]
#[error("Sandbox failed to make changes in block")]
ChangesInBlockFailure,
#[cfg(feature = "experimental")]
#[error("Sandbox failed to make changes in block by type")]
ChangesFailure,
#[cfg(feature = "experimental")]
#[error("Sandbox failed to fetch the genesis config")]
GenesisConfigFailure,
#[cfg(feature = "experimental")]
#[error("Sandboc failed to fetch the protocl config")]
ProtocolConfigFailure,
#[cfg(feature = "experimental")]
#[error("Sandbox failed to fetch the receipt")]
ReceiptFailure,
#[cfg(feature = "experimental")]
#[error("Sandbox failed to fetch tx status")]
TXStatusFailure,
#[cfg(feature = "experimental")]
#[error("Sandbox failed to fetch validator info")]
ValidatorsOrderdFailure,
}

#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
Expand Down
131 changes: 131 additions & 0 deletions workspaces/src/network/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@ use crate::rpc::client::Client;
use crate::types::{AccountId, Balance, InMemorySigner, SecretKey};
use crate::{Account, Contract, Network, Worker};

#[cfg(feature = "experimental")]
use near_chain_configs::ProtocolConfigView;
#[cfg(feature = "experimental")]
use near_jsonrpc_client::methods::{
EXPERIMENTAL_changes::RpcStateChangesInBlockByTypeRequest,
EXPERIMENTAL_changes_in_block::RpcStateChangesInBlockRequest,
EXPERIMENTAL_check_tx::RpcCheckTxRequest, EXPERIMENTAL_genesis_config::RpcGenesisConfigRequest,
EXPERIMENTAL_tx_status::RpcTransactionStatusRequest,
};
#[cfg(feature = "experimental")]
use near_jsonrpc_primitives::types::{
changes::RpcStateChangesInBlockResponse,
config::RpcProtocolConfigRequest,
receipts::{ReceiptReference, RpcReceiptRequest},
transactions::RpcBroadcastTxSyncResponse,
transactions::TransactionInfo,
validator::RpcValidatorsOrderedRequest,
};
#[cfg(feature = "experimental")]
use near_primitives::{
transaction::SignedTransaction,
types::{BlockReference, MaybeBlockId},
views::{
validator_stake_view::ValidatorStakeView, FinalExecutionOutcomeWithReceiptView,
ReceiptView, StateChangesRequestView,
},
};

// Constant taken from nearcore crate to avoid dependency
pub(crate) const NEAR_BASE: Balance = 1_000_000_000_000_000_000_000_000;

Expand Down Expand Up @@ -206,3 +234,106 @@ impl Sandbox {
Ok(())
}
}

#[cfg(feature = "experimental")]
impl Sandbox {
pub(crate) async fn changes_in_block(&self, block_reference: BlockReference) -> Result<()> {
frol marked this conversation as resolved.
Show resolved Hide resolved
// NOTE: RpcStateChangesInBlockByTypeResponse is an empty struct with no fields, so don't do anything with it.
frol marked this conversation as resolved.
Show resolved Hide resolved
self.client()
.query_nolog(&RpcStateChangesInBlockRequest { block_reference })
.await
.map_err(|e| SandboxErrorCode::ChangesInBlockFailure.custom(e))?;

Ok(())
}

pub(crate) async fn changes(
&self,
block_reference: BlockReference,
state_changes_request: StateChangesRequestView,
) -> Result<RpcStateChangesInBlockResponse> {
let resp = self
.client()
.query_nolog(&RpcStateChangesInBlockByTypeRequest {
block_reference,
state_changes_request,
})
.await
.map_err(|e| SandboxErrorCode::ChangesFailure.custom(e))?;

Ok(resp)
}

pub(crate) async fn check_tx(
&self,
signed_transaction: SignedTransaction,
) -> Result<RpcBroadcastTxSyncResponse> {
let resp = self
.client()
.query_nolog(&RpcCheckTxRequest { signed_transaction })
.await
.map_err(|e| SandboxErrorCode::CheckTxFailure.custom(e))?;

Ok(resp)
}

// This method is a stub and does nothing.
pub(crate) async fn genesis_config(&self) -> Result<()> {
// NOTE: RpcGenesisConfigResponse is an empty struct with no fields, so don't do anything with it.
frol marked this conversation as resolved.
Show resolved Hide resolved
self.client()
.query_nolog(&RpcGenesisConfigRequest)
.await
.map_err(|e| SandboxErrorCode::GenesisConfigFailure.custom(e))?;

Ok(())
}

pub(crate) async fn protocol_config(
&self,
block_reference: BlockReference,
) -> Result<ProtocolConfigView> {
let resp = self
.client()
.query_nolog(&RpcProtocolConfigRequest { block_reference })
.await
.map_err(|e| SandboxErrorCode::ProtocolConfigFailure.custom(e))?;

Ok(resp)
}

pub(crate) async fn receipt(&self, receipt_reference: ReceiptReference) -> Result<ReceiptView> {
let resp = self
.client()
.query_nolog(&RpcReceiptRequest { receipt_reference })
.await
.map_err(|e| SandboxErrorCode::ReceiptFailure.custom(e))?;

Ok(resp)
}

pub(crate) async fn tx_status(
&self,
transaction_info: TransactionInfo,
) -> Result<FinalExecutionOutcomeWithReceiptView> {
let resp = self
.client()
.query_nolog(&RpcTransactionStatusRequest { transaction_info })
.await
.map_err(|e| SandboxErrorCode::TXStatusFailure.custom(e))?;

Ok(resp)
}

pub(crate) async fn validators_ordered(
&self,
block_id: MaybeBlockId,
) -> Result<Vec<ValidatorStakeView>> {
let resp = self
.client()
.query_nolog(&RpcValidatorsOrderedRequest { block_id })
.await
.map_err(|e| SandboxErrorCode::ValidatorsOrderdFailure.custom(e))?;

Ok(resp)
}
}
73 changes: 71 additions & 2 deletions workspaces/src/worker/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,27 @@ use crate::rpc::query::{
GasPrice, Query, QueryChunk, ViewAccessKey, ViewAccessKeyList, ViewAccount, ViewBlock,
ViewCode, ViewFunction, ViewState,
};
use crate::types::{AccountId, InMemorySigner, PublicKey};
use crate::types::{AccountId, Balance, InMemorySigner, PublicKey};
use crate::worker::Worker;
use crate::{Account, Network};

use near_primitives::types::Balance;
#[cfg(feature = "experimental")]
use near_chain_configs::ProtocolConfigView;
#[cfg(feature = "experimental")]
use near_jsonrpc_primitives::types::{
changes::RpcStateChangesInBlockResponse,
receipts::ReceiptReference,
transactions::{RpcBroadcastTxSyncResponse, TransactionInfo},
};
#[cfg(feature = "experimental")]
use near_primitives::{
transaction::SignedTransaction,
types::{BlockReference, MaybeBlockId},
views::{
validator_stake_view::ValidatorStakeView, FinalExecutionOutcomeWithReceiptView,
ReceiptView, StateChangesRequestView,
},
};

impl<T: ?Sized> Clone for Worker<T> {
fn clone(&self) -> Self {
Expand Down Expand Up @@ -246,3 +262,56 @@ impl Worker<Sandbox> {
self.workspace.server.rpc_addr()
}
}

#[cfg(feature = "experimental")]
impl Worker<Sandbox> {
pub async fn changes_in_block(&self, block_reference: BlockReference) -> Result<()> {
self.workspace.changes_in_block(block_reference).await
}

pub async fn changes(
&self,
block_reference: BlockReference,
state_changes_request: StateChangesRequestView,
) -> Result<RpcStateChangesInBlockResponse> {
self.workspace
.changes(block_reference, state_changes_request)
.await
}

pub async fn check_tx(
&self,
signed_tx: SignedTransaction,
) -> Result<RpcBroadcastTxSyncResponse> {
self.workspace.check_tx(signed_tx).await
}

pub async fn genesis_config(&self) -> Result<()> {
self.workspace.genesis_config().await
}

pub async fn protocol_config(
&self,
block_reference: BlockReference,
) -> Result<ProtocolConfigView> {
self.workspace.protocol_config(block_reference).await
}

pub async fn receipt(&self, receipt_reference: ReceiptReference) -> Result<ReceiptView> {
self.workspace.receipt(receipt_reference).await
}

pub async fn tx_status(
&self,
transaction_info: TransactionInfo,
) -> Result<FinalExecutionOutcomeWithReceiptView> {
self.workspace.tx_status(transaction_info).await
}

pub async fn validators_ordered(
&self,
block_id: MaybeBlockId,
) -> Result<Vec<ValidatorStakeView>> {
self.workspace.validators_ordered(block_id).await
}
}
Loading