Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
1,254 changes: 633 additions & 621 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,16 @@ region = "3.0.2"
reqwest = { version = "0.12.8", default-features = false }
roast-secp256k1-evm = { version = "0.1.0", git = "https://github.com/gear-tech/roast", branch = "gear-v0.1.0" }
scale-info = { version = "2.11", default-features = false }
scale-decode = "0.16.0"
scale-encode = "0.10.0"
serde = { version = "^1", default-features = false }
serde_json = { version = "1.0.135", default-features = false, features = [
"alloc",
] }
serde-json-wasm = { version = "1.0.1", default-features = false }
sha-1 = "0.10.1"
static_init = "1.0.3"
subxt = "0.37.0"
subxt = "0.44.0"
subxt-codegen = "0.37.0"
syn = "2.0.71"
thiserror = { version = "2.0.12", default-features = false }
Expand Down Expand Up @@ -530,11 +532,9 @@ tracing-appender = "0.2" # utils/node-loader
trybuild = "1" # gstd/codegen
wasmprinter = "0.230" # utils/wasm-gen
fail = "0.5" # gear-common
scale-value = "^0.16" # gsdk
heck = "0.5.0" # gsdk-api-gen
etc = "0.1.19" # gcli
toml_edit = "0.22.12" # crates-io
scale-decode = "0.13.0" # gsdk
directories = "5.0.1" # utils/key-finder
num-traits = { version = "0.2", default-features = false } # gear-core
glob = "0.3.1" # cargo-gbuild
Expand Down
2 changes: 2 additions & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ gear-wasm-instrument = { workspace = true, optional = true }
gsys.workspace = true

# Substrate deps
scale-decode.workspace = true
scale-encode.workspace = true
sp-core.workspace = true
sp-io.workspace = true
sp-std.workspace = true
Expand Down
18 changes: 17 additions & 1 deletion common/src/gas_provider/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,30 @@ use super::*;
use core::ops::{Add, Index, IndexMut};
use enum_iterator::cardinality;
use gear_core::ids::ReservationId;
use scale_decode::DecodeAsType;
use scale_encode::EncodeAsType;
use sp_runtime::{
codec::{self, MaxEncodedLen},
scale_info,
traits::Zero,
};

/// ID of the [`GasNode`].
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, TypeInfo)]
#[derive(
Debug,
Copy,
Clone,
Hash,
Eq,
PartialEq,
Ord,
PartialOrd,
Encode,
EncodeAsType,
Decode,
DecodeAsType,
TypeInfo,
)]
#[codec(crate = codec)]
#[scale_info(crate = scale_info)]
pub enum GasNodeId<T, U> {
Expand Down
4 changes: 3 additions & 1 deletion gprimitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ alloy-sol-types = { workspace = true, optional = true }
derive_more.workspace = true
primitive-types = { workspace = true, features = ["scale-info", "rustc-hex"] }
scale-info = { workspace = true, features = ["derive"], optional = true }
scale-decode = { workspace = true, optional = true }
scale-encode = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"], optional = true }
hex.workspace = true
gear-ss58.workspace = true
Expand All @@ -24,6 +26,6 @@ thiserror.workspace = true
[features]
default = ["std"]
std = []
codec = ["dep:scale-info"]
codec = ["dep:scale-info", "dep:scale-encode", "dep:scale-decode"]
serde = ["dep:serde", "primitive-types/serde", "std"]
ethexe = ["dep:alloy-primitives", "dep:alloy-sol-types"]
14 changes: 9 additions & 5 deletions gprimitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ use core::{
use derive_more::{AsMut, AsRef, From, Into};
use gear_ss58::RawSs58Address;
#[cfg(feature = "codec")]
use scale_decode::DecodeAsType;
#[cfg(feature = "codec")]
use scale_encode::EncodeAsType;
#[cfg(feature = "codec")]
use scale_info::{
TypeInfo,
scale::{self, Decode, Encode, MaxEncodedLen},
Expand Down Expand Up @@ -76,7 +80,7 @@ pub enum ConversionError {
/// message sending.
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, From, Into)]
#[cfg_attr(feature = "codec", derive(TypeInfo, Encode, Decode, MaxEncodedLen), codec(crate = scale))]
#[cfg_attr(feature = "codec", derive(TypeInfo, Encode, EncodeAsType, Decode, DecodeAsType, MaxEncodedLen), codec(crate = scale))]
pub struct MessageHandle(u32);

/// Program (actor) identifier.
Expand All @@ -91,7 +95,7 @@ pub struct MessageHandle(u32);
#[derive(Clone, Copy, Default, Hash, Ord, PartialEq, PartialOrd, Eq, From, Into, AsRef, AsMut)]
#[as_ref(forward)]
#[as_mut(forward)]
#[cfg_attr(feature = "codec", derive(TypeInfo, Encode, Decode, MaxEncodedLen), codec(crate = scale))]
#[cfg_attr(feature = "codec", derive(TypeInfo, Encode, EncodeAsType, Decode, DecodeAsType, MaxEncodedLen), codec(crate = scale))]
pub struct ActorId([u8; 32]);

macros::impl_primitive!(new zero into_bytes from_h256 into_h256 try_from_slice debug, ActorId);
Expand Down Expand Up @@ -296,7 +300,7 @@ impl<'de> Deserialize<'de> for ActorId {
#[derive(Clone, Copy, Default, Hash, Ord, PartialEq, PartialOrd, Eq, From, Into, AsRef, AsMut)]
#[as_ref(forward)]
#[as_mut(forward)]
#[cfg_attr(feature = "codec", derive(TypeInfo, Encode, Decode, MaxEncodedLen), codec(crate = scale))]
#[cfg_attr(feature = "codec", derive(TypeInfo, Encode, EncodeAsType, Decode, DecodeAsType, MaxEncodedLen), codec(crate = scale))]
pub struct MessageId([u8; 32]);

macros::impl_primitive!(new zero into_bytes from_u64 from_h256 into_h256 from_str display debug serde, MessageId);
Expand All @@ -312,7 +316,7 @@ macros::impl_primitive!(new zero into_bytes from_u64 from_h256 into_h256 from_st
#[derive(Clone, Copy, Default, Hash, Ord, PartialEq, PartialOrd, Eq, From, Into, AsRef, AsMut)]
#[as_ref(forward)]
#[as_mut(forward)]
#[cfg_attr(feature = "codec", derive(TypeInfo, Encode, Decode, MaxEncodedLen), codec(crate = scale))]
#[cfg_attr(feature = "codec", derive(TypeInfo, Encode, EncodeAsType, Decode, DecodeAsType, MaxEncodedLen), codec(crate = scale))]
pub struct CodeId([u8; 32]);

macros::impl_primitive!(new zero into_bytes from_u64 from_h256 into_h256 from_str try_from_slice display debug serde, CodeId);
Expand All @@ -324,7 +328,7 @@ macros::impl_primitive!(new zero into_bytes from_u64 from_h256 into_h256 from_st
#[derive(Clone, Copy, Default, Hash, Ord, PartialEq, PartialOrd, Eq, From, Into, AsRef, AsMut)]
#[as_ref(forward)]
#[as_mut(forward)]
#[cfg_attr(feature = "codec", derive(TypeInfo, Encode, Decode, MaxEncodedLen), codec(crate = scale))]
#[cfg_attr(feature = "codec", derive(TypeInfo, Encode, EncodeAsType, Decode, DecodeAsType, MaxEncodedLen), codec(crate = scale))]
pub struct ReservationId([u8; 32]);

macros::impl_primitive!(new zero into_bytes from_u64 from_h256 into_h256 from_str display debug serde, ReservationId);
Expand Down
17 changes: 6 additions & 11 deletions gsdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,25 @@ repository.workspace = true
rust-version.workspace = true

[dependencies]
anyhow.workspace = true
base64.workspace = true
colored.workspace = true
derive_more.workspace = true
futures-util.workspace = true
futures.workspace = true
gear-core = { workspace = true, features = ["std"] }
gear-core-errors.workspace = true
gsdk-codegen.workspace = true
hex.workspace = true
indexmap = { workspace = true, features = ["std"] }
jsonrpsee = { workspace = true, features = [ "http-client", "ws-client" ] }
log.workspace = true
scale-value.workspace = true
parking_lot.workspace = true
serde_json = { workspace = true, features = [ "std" ] }
sp-core = { workspace = true, features = [ "std" , "full_crypto"] }
sp-runtime = { workspace = true, features = [ "std" ] }
subxt.workspace = true
thiserror.workspace = true
sp-runtime = { workspace = true, features = [ "std" ] }
sp-core = { workspace = true, features = [ "std" , "full_crypto"] }
gsdk-codegen.workspace = true
parking_lot.workspace = true
derive_more.workspace = true

# Importing these two libraries for trimming
# the the size of the generated file.
scale-decode.workspace = true
url.workspace = true
parity-scale-codec.workspace = true

[dev-dependencies]
Expand Down
6 changes: 6 additions & 0 deletions gsdk/api-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,14 @@ fn generate_runtime_types(metadata: Metadata) -> TokenStream {
parse_quote!(Debug),
parse_quote!(crate::gp::Encode),
parse_quote!(crate::gp::Decode),
parse_quote!(crate::gp::EncodeAsType),
parse_quote!(crate::gp::DecodeAsType),
]);
builder.set_additional_global_attributes(vec![
parse_quote!(#[encode_as_type(crate_path = "::subxt::ext::scale_encode")]),
parse_quote!(#[decode_as_type(crate_path = "::subxt::ext::scale_decode")]),
parse_quote!(#[codec(crate = ::subxt::ext::codec)]),
]);

for ty in [
parse_quote!(gprimitives::CodeId),
Expand Down
119 changes: 54 additions & 65 deletions gsdk/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,66 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{
Blocks, Events, TxInBlock, client::Rpc, config::GearConfig, metadata::Event, signer::Signer,
Blocks, Events, Result, TxInBlock, config::GearConfig, metadata::Event, signer::Signer,
};
use anyhow::Result;
use core::ops::{Deref, DerefMut};
use subxt::OnlineClient;
use jsonrpsee::{
client_transport::ws::{Url, WsTransportClientBuilder},
core::client::Client,
};
use std::time::Duration;
use subxt::{OnlineClient, backend::rpc::RpcClient, ext::subxt_rpcs::LegacyRpcMethods};

const DEFAULT_GEAR_ENDPOINT: &str = "wss://rpc.vara.network:443";
const DEFAULT_TIMEOUT_MILLISECS: u64 = 60_000;
const DEFAULT_RETRIES: u8 = 0;
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(60);

const ONE_HUNDRED_MEGABYTES: u32 = 100 * 1024 * 1024;

/// Gear api wrapper.
#[derive(Clone)]
pub struct Api {
/// Substrate client
rpc: RpcClient,
legacy_methods: LegacyRpcMethods<GearConfig>,
client: OnlineClient<GearConfig>,

/// Gear RPC client
rpc: Rpc,
}

impl Api {
/// Create new API client.
pub async fn new(uri: impl Into<Option<&str>>) -> Result<Self> {
Self::builder().build(uri).await
Self::with_timeout(uri, DEFAULT_TIMEOUT).await
}

pub async fn with_timeout(uri: impl Into<Option<&str>>, timeout: Duration) -> Result<Self> {
let uri: Option<&str> = uri.into();
let rpc_client = Self::rpc_client(uri.unwrap_or(DEFAULT_GEAR_ENDPOINT), timeout).await?;

Self::from_rpc_client(rpc_client).await
}

/// Resolve api builder
pub fn builder() -> ApiBuilder {
ApiBuilder::default()
async fn rpc_client(uri: &str, timeout: Duration) -> Result<RpcClient> {
let url = Url::parse(uri)?;
let (sender, receiver) = WsTransportClientBuilder::default()
.max_request_size(ONE_HUNDRED_MEGABYTES)
.connection_timeout(timeout)
.build(url)
.await?;

let client = Client::builder()
.request_timeout(timeout)
.build_with_tokio(sender, receiver);

Ok(RpcClient::new(client))
}

/// Gear RPC Client
pub fn rpc(&self) -> Rpc {
self.rpc.clone()
pub async fn from_rpc_client(rpc: RpcClient) -> Result<Self> {
let legacy_methods = LegacyRpcMethods::new(rpc.clone());
let client = OnlineClient::from_rpc_client(rpc.clone()).await?;

Ok(Self {
rpc,
client,
legacy_methods,
})
}

/// Subscribe all blocks
Expand All @@ -65,7 +91,7 @@ impl Api {
/// }
/// ```
pub async fn subscribe_blocks(&self) -> Result<Blocks> {
Ok(self.client.blocks().subscribe_all().await?.into())
Ok(self.blocks().subscribe_all().await?.into())
}

/// Subscribe finalized blocks
Expand Down Expand Up @@ -107,7 +133,17 @@ impl Api {

/// New signer from api
pub fn signer(self, suri: &str, passwd: Option<&str>) -> Result<Signer> {
Signer::new(self, suri, passwd).map_err(Into::into)
Signer::new(self, suri, passwd)
}

/// Get the underlying [`RpcClient`] instance.
pub fn rpc(&self) -> &RpcClient {
&self.rpc
}

/// Access legacy RPC methods.
pub fn legacy(&self) -> &LegacyRpcMethods<GearConfig> {
&self.legacy_methods
}
}

Expand All @@ -124,50 +160,3 @@ impl DerefMut for Api {
&mut self.client
}
}

/// gsdk api builder
pub struct ApiBuilder {
/// RPC retries
retries: u8,
/// RPC timeout
timeout: u64,
}

impl ApiBuilder {
/// Build api from the provided config
pub async fn build(self, uri: impl Into<Option<&str>>) -> Result<Api> {
let uri: Option<&str> = uri.into();
let rpc = Rpc::new(
uri.unwrap_or(DEFAULT_GEAR_ENDPOINT),
self.timeout,
self.retries,
)
.await?;

Ok(Api {
client: OnlineClient::from_rpc_client(rpc.client()).await?,
rpc,
})
}

/// Set rpc retries
pub fn retries(mut self, retries: u8) -> Self {
self.retries = retries;
self
}

/// Set rpc timeout in milliseconds
pub fn timeout(mut self, timeout: u64) -> Self {
self.timeout = timeout;
self
}
}

impl Default for ApiBuilder {
fn default() -> Self {
Self {
retries: DEFAULT_RETRIES,
timeout: DEFAULT_TIMEOUT_MILLISECS,
}
}
}
10 changes: 3 additions & 7 deletions gsdk/src/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@
use crate::TxStatus;
use indexmap::IndexMap;
use parking_lot::Mutex;
use sp_core::H256;
use std::{collections::BTreeMap, sync::Arc, time::SystemTime};
use subxt::utils::H256;

/// Transaction Status for Backtrace
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BacktraceStatus {
Validated,
NoLongerInBestBlock,
Broadcasted {
num_peers: u32,
},
Broadcasted,
InBestBlock {
block_hash: H256,
extrinsic_hash: H256,
Expand All @@ -56,9 +54,7 @@ impl<'s> From<&'s TxStatus> for BacktraceStatus {
match status {
TxStatus::Validated => BacktraceStatus::Validated,
TxStatus::NoLongerInBestBlock => BacktraceStatus::NoLongerInBestBlock,
TxStatus::Broadcasted { num_peers } => BacktraceStatus::Broadcasted {
num_peers: *num_peers,
},
TxStatus::Broadcasted => BacktraceStatus::Broadcasted,
TxStatus::InBestBlock(b) => BacktraceStatus::InBestBlock {
block_hash: b.block_hash(),
extrinsic_hash: b.extrinsic_hash(),
Expand Down
Loading