-
Notifications
You must be signed in to change notification settings - Fork 52
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
refactor!: Reorganize exports and modules exposed #102
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d3665d3
refactor!: Reorganize exports and modules exposed
austinabell 045762a
refactor!: move operations into own module
austinabell 5ad3e49
document modules and collapse one file modules
austinabell 83e503c
fmt
austinabell 96a0520
Merge branch 'main' of github.com:near/runner-rs into austin/refactor…
austinabell a4ada04
fmt
austinabell 3912cc4
fix test import
austinabell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,154 +1,20 @@ | ||
mod account; | ||
mod block; | ||
//! All builtin network types and traits. | ||
//! | ||
//! Currently the builtin network types are [`Mainnet`], [`Testnet`], and [`Sandbox`]. | ||
|
||
mod info; | ||
mod mainnet; | ||
pub mod result; | ||
mod sandbox; | ||
mod server; | ||
mod testnet; | ||
pub(crate) mod variants; | ||
|
||
pub mod transaction; | ||
|
||
use async_trait::async_trait; | ||
|
||
use near_jsonrpc_client::methods::sandbox_patch_state::RpcSandboxPatchStateRequest; | ||
use near_primitives::state_record::StateRecord; | ||
|
||
pub(crate) use crate::network::info::Info; | ||
use crate::rpc::client::Client; | ||
use crate::rpc::patch::ImportContractTransaction; | ||
use crate::types::{AccountId, KeyType, SecretKey}; | ||
use crate::Worker; | ||
|
||
pub use crate::network::account::{Account, AccountDetails, Contract}; | ||
pub use crate::network::block::Block; | ||
pub use crate::network::mainnet::Mainnet; | ||
pub use crate::network::result::{CallExecution, CallExecutionDetails, ViewResultDetails}; | ||
pub use crate::network::sandbox::Sandbox; | ||
pub use crate::network::testnet::Testnet; | ||
|
||
pub(crate) const DEV_ACCOUNT_SEED: &str = "testificate"; | ||
|
||
pub trait NetworkClient { | ||
fn client(&self) -> &Client; | ||
} | ||
|
||
pub trait NetworkInfo { | ||
fn info(&self) -> &Info; | ||
} | ||
|
||
#[async_trait] | ||
pub trait TopLevelAccountCreator { | ||
async fn create_tla( | ||
&self, | ||
id: AccountId, | ||
sk: SecretKey, | ||
) -> anyhow::Result<CallExecution<Account>>; | ||
|
||
async fn create_tla_and_deploy( | ||
&self, | ||
id: AccountId, | ||
sk: SecretKey, | ||
wasm: &[u8], | ||
) -> anyhow::Result<CallExecution<Contract>>; | ||
} | ||
|
||
// NOTE: Not all networks/runtimes will have the ability to be able to do dev_deploy. | ||
// This trait acts as segmented boundary for only specific networks such as sandbox and testnet. | ||
pub trait AllowDevAccountCreation {} | ||
|
||
#[async_trait] | ||
pub trait DevAccountDeployer { | ||
async fn dev_generate(&self) -> (AccountId, SecretKey); | ||
async fn dev_create_account(&self) -> anyhow::Result<Account>; | ||
async fn dev_deploy(&self, wasm: &[u8]) -> anyhow::Result<Contract>; | ||
} | ||
|
||
#[async_trait] | ||
impl<T> DevAccountDeployer for T | ||
where | ||
T: TopLevelAccountCreator + NetworkInfo + AllowDevAccountCreation + Send + Sync, | ||
{ | ||
async fn dev_generate(&self) -> (AccountId, SecretKey) { | ||
let id = crate::rpc::tool::random_account_id(); | ||
let sk = SecretKey::from_seed(KeyType::ED25519, DEV_ACCOUNT_SEED); | ||
(id, sk) | ||
} | ||
|
||
async fn dev_create_account(&self) -> anyhow::Result<Account> { | ||
let (id, sk) = self.dev_generate().await; | ||
let account = self.create_tla(id.clone(), sk).await?; | ||
account.into() | ||
} | ||
|
||
async fn dev_deploy(&self, wasm: &[u8]) -> anyhow::Result<Contract> { | ||
let (id, sk) = self.dev_generate().await; | ||
let contract = self.create_tla_and_deploy(id.clone(), sk, wasm).await?; | ||
contract.into() | ||
} | ||
} | ||
|
||
pub trait AllowStatePatching {} | ||
|
||
#[async_trait] | ||
pub trait StatePatcher { | ||
async fn patch_state( | ||
&self, | ||
contract_id: &AccountId, | ||
key: &[u8], | ||
value: &[u8], | ||
) -> anyhow::Result<()>; | ||
|
||
fn import_contract<'a, 'b>( | ||
&'b self, | ||
id: &AccountId, | ||
worker: &'a Worker<impl Network>, | ||
) -> ImportContractTransaction<'a, 'b>; | ||
} | ||
|
||
#[async_trait] | ||
impl<T> StatePatcher for T | ||
where | ||
T: AllowStatePatching + NetworkClient + Send + Sync, | ||
{ | ||
async fn patch_state( | ||
&self, | ||
contract_id: &AccountId, | ||
key: &[u8], | ||
value: &[u8], | ||
) -> anyhow::Result<()> { | ||
let state = StateRecord::Data { | ||
account_id: contract_id.to_owned(), | ||
data_key: key.to_vec(), | ||
value: value.to_vec(), | ||
}; | ||
let records = vec![state]; | ||
|
||
// NOTE: RpcSandboxPatchStateResponse is an empty struct with no fields, so don't do anything with it: | ||
let _patch_resp = self | ||
.client() | ||
.query(&RpcSandboxPatchStateRequest { records }) | ||
.await | ||
.map_err(|err| anyhow::anyhow!("Failed to patch state: {:?}", err))?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn import_contract<'a, 'b>( | ||
&'b self, | ||
id: &AccountId, | ||
worker: &'a Worker<impl Network>, | ||
) -> ImportContractTransaction<'a, 'b> { | ||
ImportContractTransaction::new(id.to_owned(), worker.client(), self.client()) | ||
} | ||
} | ||
|
||
pub trait Network: TopLevelAccountCreator + NetworkInfo + NetworkClient + Send + Sync {} | ||
|
||
impl<T> Network for T where T: TopLevelAccountCreator + NetworkInfo + NetworkClient + Send + Sync {} | ||
|
||
/// DevNetwork is a Network that can call into `dev_create` and `dev_deploy` to create developer accounts. | ||
pub trait DevNetwork: AllowDevAccountCreation + Network {} | ||
pub(crate) use variants::DEV_ACCOUNT_SEED; | ||
|
||
// Implemented by default if we have `AllowDevAccountCreation` | ||
impl<T> DevNetwork for T where T: AllowDevAccountCreation + Network {} | ||
pub use self::info::Info; | ||
pub use self::mainnet::Mainnet; | ||
pub use self::sandbox::Sandbox; | ||
pub use self::testnet::Testnet; | ||
pub use self::variants::{ | ||
AllowDevAccountCreation, DevAccountDeployer, NetworkClient, NetworkInfo, TopLevelAccountCreator, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use crate::network::Info; | ||
use crate::result::CallExecution; | ||
use crate::rpc::client::Client; | ||
use crate::types::{AccountId, KeyType, SecretKey}; | ||
use crate::{Account, Contract}; | ||
use async_trait::async_trait; | ||
|
||
pub(crate) const DEV_ACCOUNT_SEED: &str = "testificate"; | ||
|
||
pub trait NetworkClient { | ||
fn client(&self) -> &Client; | ||
} | ||
|
||
pub trait NetworkInfo { | ||
fn info(&self) -> &Info; | ||
} | ||
|
||
#[async_trait] | ||
pub trait TopLevelAccountCreator { | ||
async fn create_tla( | ||
&self, | ||
id: AccountId, | ||
sk: SecretKey, | ||
) -> anyhow::Result<CallExecution<Account>>; | ||
|
||
async fn create_tla_and_deploy( | ||
&self, | ||
id: AccountId, | ||
sk: SecretKey, | ||
wasm: &[u8], | ||
) -> anyhow::Result<CallExecution<Contract>>; | ||
} | ||
|
||
// NOTE: Not all networks/runtimes will have the ability to be able to do dev_deploy. | ||
// This trait acts as segmented boundary for only specific networks such as sandbox and testnet. | ||
pub trait AllowDevAccountCreation {} | ||
|
||
#[async_trait] | ||
pub trait DevAccountDeployer { | ||
async fn dev_generate(&self) -> (AccountId, SecretKey); | ||
async fn dev_create_account(&self) -> anyhow::Result<Account>; | ||
async fn dev_deploy(&self, wasm: &[u8]) -> anyhow::Result<Contract>; | ||
} | ||
|
||
#[async_trait] | ||
impl<T> DevAccountDeployer for T | ||
where | ||
T: TopLevelAccountCreator + NetworkInfo + AllowDevAccountCreation + Send + Sync, | ||
{ | ||
async fn dev_generate(&self) -> (AccountId, SecretKey) { | ||
let id = crate::rpc::tool::random_account_id(); | ||
let sk = SecretKey::from_seed(KeyType::ED25519, DEV_ACCOUNT_SEED); | ||
(id, sk) | ||
} | ||
|
||
async fn dev_create_account(&self) -> anyhow::Result<Account> { | ||
let (id, sk) = self.dev_generate().await; | ||
let account = self.create_tla(id.clone(), sk).await?; | ||
account.into() | ||
} | ||
|
||
async fn dev_deploy(&self, wasm: &[u8]) -> anyhow::Result<Contract> { | ||
let (id, sk) = self.dev_generate().await; | ||
let contract = self.create_tla_and_deploy(id.clone(), sk, wasm).await?; | ||
contract.into() | ||
} | ||
} | ||
|
||
pub trait Network: TopLevelAccountCreator + NetworkInfo + NetworkClient + Send + Sync {} | ||
|
||
impl<T> Network for T where T: TopLevelAccountCreator + NetworkInfo + NetworkClient + Send + Sync {} | ||
|
||
/// DevNetwork is a Network that can call into `dev_create` and `dev_deploy` to create developer accounts. | ||
pub trait DevNetwork: AllowDevAccountCreation + Network {} | ||
|
||
// Implemented by default if we have `AllowDevAccountCreation` | ||
impl<T> DevNetwork for T where T: AllowDevAccountCreation + Network {} |
19 changes: 10 additions & 9 deletions
19
workspaces/src/network/transaction.rs → workspaces/src/operations.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
pub use crate::network::{DevAccountDeployer, StatePatcher, TopLevelAccountCreator}; | ||
pub use crate::network::{DevAccountDeployer, TopLevelAccountCreator}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this type not exposed? It is returned by exposed functions. So if a user wants to make a function that returns the result of one of the exposed function they reference the type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are you referring to? This is just a type that wraps the type returned that includes metadata.
Edit: Also confused on what you're asking. It is exposed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I meant from the crate: https://github.com/near/workspaces-rs/pull/102/files#diff-f2509a15c16bf20cbd905fc2d19c6b77363687408bf1020327da68936ec19ef9
Account exposes
CallExecutionDetails
andCallExecution
, but they aren't exported.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
those are exposed under the
result
module which we have exported viapub mod result;
inlib.rs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see thanks!