Skip to content

Commit

Permalink
feat(gsdk): introduce backtrace system (#3170)
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop authored Sep 21, 2023
1 parent 586a852 commit 808149f
Show file tree
Hide file tree
Showing 13 changed files with 584 additions and 337 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions gclient/src/api/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl GearApi {

let amount = calls.len();

let tx = self.0.force_batch(calls).await?;
let tx = self.0.calls.force_batch(calls).await?;
let mut res = Vec::with_capacity(amount);

for event in tx.wait_for_success().await?.iter() {
Expand Down Expand Up @@ -677,7 +677,7 @@ impl GearApi {

let amount = calls.len();

let tx = self.0.force_batch(calls).await?;
let tx = self.0.calls.force_batch(calls).await?;
let mut res = Vec::with_capacity(amount);

for event in tx.wait_for_success().await?.iter() {
Expand Down Expand Up @@ -773,7 +773,7 @@ impl GearApi {

let amount = calls.len();

let tx = self.0.force_batch(calls).await?;
let tx = self.0.calls.force_batch(calls).await?;
let mut res = Vec::with_capacity(amount);

for event in tx.wait_for_success().await?.iter() {
Expand Down Expand Up @@ -913,7 +913,7 @@ impl GearApi {

let amount = calls.len();

let tx = self.0.force_batch(calls).await?;
let tx = self.0.calls.force_batch(calls).await?;
let mut res = Vec::with_capacity(amount);

for event in tx.wait_for_success().await?.iter() {
Expand Down Expand Up @@ -1012,7 +1012,7 @@ impl GearApi {

let amount = calls.len();

let tx = self.0.force_batch(calls).await?;
let tx = self.0.calls.force_batch(calls).await?;
let mut res = Vec::with_capacity(amount);

for event in tx.wait_for_success().await?.iter() {
Expand Down Expand Up @@ -1143,7 +1143,7 @@ impl GearApi {

let amount = calls.len();

let tx = self.0.force_batch(calls).await?;
let tx = self.0.calls.force_batch(calls).await?;
let mut res = Vec::with_capacity(amount);

for event in tx.wait_for_success().await?.iter() {
Expand Down Expand Up @@ -1251,6 +1251,7 @@ impl GearApi {
pub async fn set_code(&self, code: impl AsRef<[u8]>) -> Result<H256> {
let events = self
.0
.calls
.sudo_unchecked_weight(
RuntimeCall::System(SystemCall::set_code {
code: code.as_ref().to_vec(),
Expand Down Expand Up @@ -1283,6 +1284,7 @@ impl GearApi {
pub async fn set_code_without_checks(&self, code: impl AsRef<[u8]>) -> Result<H256> {
let events = self
.0
.calls
.sudo_unchecked_weight(
RuntimeCall::System(SystemCall::set_code_without_checks {
code: code.as_ref().to_vec(),
Expand Down Expand Up @@ -1318,6 +1320,7 @@ impl GearApi {
) -> Result<H256> {
let events = self
.0
.calls
.sudo_unchecked_weight(
RuntimeCall::Balances(BalancesCall::set_balance {
who: to.into().convert(),
Expand Down
2 changes: 2 additions & 0 deletions gsdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ futures.workspace = true
gear-core = { workspace = true, features = [ "std" ] }
gear-core-errors.workspace = true
hex.workspace = true
indexmap.workspace = true
jsonrpsee = { workspace = true, features = [ "http-client", "ws-client" ] }
log.workspace = true
scale-value.workspace = true
Expand All @@ -28,6 +29,7 @@ thiserror.workspace = true
sp-runtime = { workspace = true, features = [ "std" ] }
sp-core.workspace = true
gsdk-codegen = { path = "codegen" }
parking_lot.workspace = true

# Importing these two libraries for trimming
# the the size of the generated file.
Expand Down
101 changes: 101 additions & 0 deletions gsdk/src/backtrace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// This file is part of Gear.
//
// Copyright (C) 2021-2023 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
//! Backtrace support for `gsdk`
use crate::TxStatus;
use indexmap::IndexMap;
use parking_lot::Mutex;
use sp_core::H256;
use std::{collections::BTreeMap, sync::Arc, time::SystemTime};

/// Transaction Status for Backtrace
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BacktraceStatus {
Future,
Ready,
Broadcast(Vec<String>),
InBlock {
block_hash: H256,
extrinsic_hash: H256,
},
Retracted {
block_hash: H256,
},
FinalityTimeout {
block_hash: H256,
},
Finalized {
block_hash: H256,
extrinsic_hash: H256,
},
Usurped {
extrinsic_hash: H256,
},
Dropped,
Invalid,
}

impl<'s> From<&'s TxStatus> for BacktraceStatus {
fn from(status: &'s TxStatus) -> BacktraceStatus {
match status {
TxStatus::Future => BacktraceStatus::Future,
TxStatus::Ready => BacktraceStatus::Ready,
TxStatus::Broadcast(v) => BacktraceStatus::Broadcast(v.clone()),
TxStatus::InBlock(b) => BacktraceStatus::InBlock {
block_hash: b.block_hash(),
extrinsic_hash: b.extrinsic_hash(),
},
TxStatus::Retracted(h) => BacktraceStatus::Retracted { block_hash: *h },
TxStatus::FinalityTimeout(h) => BacktraceStatus::FinalityTimeout { block_hash: *h },
TxStatus::Finalized(b) => BacktraceStatus::Finalized {
block_hash: b.block_hash(),
extrinsic_hash: b.extrinsic_hash(),
},
TxStatus::Usurped(h) => BacktraceStatus::Usurped { extrinsic_hash: *h },
TxStatus::Dropped => BacktraceStatus::Dropped,
TxStatus::Invalid => BacktraceStatus::Invalid,
}
}
}

/// Backtrace support for transactions
#[derive(Clone, Debug, Default)]
pub struct Backtrace {
inner: Arc<Mutex<IndexMap<H256, BTreeMap<SystemTime, BacktraceStatus>>>>,
}

impl Backtrace {
/// Append status to transaction
pub fn append(&self, tx: H256, status: impl Into<BacktraceStatus>) {
let mut inner = self.inner.lock();

if let Some(map) = inner.get_mut(&tx) {
map.insert(SystemTime::now(), status.into());
} else {
let mut map: BTreeMap<SystemTime, BacktraceStatus> = Default::default();
map.insert(SystemTime::now(), status.into());
inner.insert(tx, map);
};
}

/// Get backtrace of transaction
pub fn get(&self, tx: H256) -> Option<BTreeMap<SystemTime, BacktraceStatus>> {
self.inner.lock().get(&tx).cloned()
}
}
1 change: 1 addition & 0 deletions gsdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use subxt::{
};

mod api;
pub mod backtrace;
mod client;
pub mod config;
mod constants;
Expand Down
Loading

0 comments on commit 808149f

Please sign in to comment.