Skip to content

Commit

Permalink
Merge pull request #319 from sora-xor/merge-develop-to-master
Browse files Browse the repository at this point in the history
Merge develop to master
  • Loading branch information
vovac12 authored Mar 6, 2023
2 parents 6fafcbf + 6352213 commit faabc97
Show file tree
Hide file tree
Showing 33 changed files with 1,619 additions and 350 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ cargo build --release --features private-net
./run_script.sh -d -w -r
```
access running network via polkadot.js/apps (select Development -> Local Node, e.g. [link](https://polkadot.js.org/apps/?rpc=ws%3A%2F%2F127.0.0.1%3A9944#/explorer))
#### On macOS
macOS is shipped with the BSD version of `getopt`. If the script behaves incorrectly and does not parse arguments,
make sure you have installed and set as active the GNU versions of `awk` and `getopt`.


### Run benchmarks to generate weights
> For release must be run on hardware matching validator requirements specification.
Expand Down
2 changes: 1 addition & 1 deletion common/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ macro_rules! assert_approx_eq {
let right = $crate::prelude::FixedWrapper::from($right);
assert!(
left.clone() < right.clone() + tolerance.clone() && right < left + tolerance,
"{} != {} with tolerance {}",
"{:?} != {:?} with tolerance {:?}",
$left,
$right,
$tol
Expand Down
7 changes: 7 additions & 0 deletions misc/eth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"networks": {
"0": {
"url": "http://127.0.0.1:8545"
}
}
}
3 changes: 2 additions & 1 deletion node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "framenode"
version = "1.8.0"
version = "1.9.0"
authors = ["Parity Technologies <[email protected]>"]
build = "build.rs"
edition = "2021"
Expand All @@ -13,6 +13,7 @@ path = "src/main.rs"
derive_more = "0.99.13"
exit-future = "0.2.0"
futures = { version = "0.3.1", features = ["compat"] }
futures-timer = "3.0.2"
log = "0.4.8"
parking_lot = "0.12.0"
trie-root = "0.16.0"
Expand Down
2 changes: 1 addition & 1 deletion node/chain_spec/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "framenode-chain-spec"
version = "1.8.0"
version = "1.9.0"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"

Expand Down
174 changes: 174 additions & 0 deletions node/src/eth_bridge_metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
use codec::Decode;
use framenode_runtime::eth_bridge::{
STORAGE_FAILED_PENDING_TRANSACTIONS_KEY, STORAGE_NETWORK_IDS_KEY,
STORAGE_PENDING_TRANSACTIONS_KEY, STORAGE_SUB_TO_HANDLE_FROM_HEIGHT_KEY,
};
use framenode_runtime::{eth_bridge::offchain::SignedTransactionData, opaque::Block, Runtime};
use prometheus_endpoint::{register, Gauge, Opts, PrometheusError, Registry, U64};
use sp_core::H256;
use sp_runtime::offchain::OffchainStorage;
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;

pub struct Metrics<B> {
pub backend: Arc<B>,
pub period: std::time::Duration,
pub pending_transactions: Gauge<U64>,
pub failed_pending_transactions: Gauge<U64>,
pub ethereum_from_height: BTreeMap<framenode_runtime::NetworkId, Gauge<U64>>,
pub ethereum_height: BTreeMap<framenode_runtime::NetworkId, Gauge<U64>>,
pub substrate_from_height: Gauge<U64>,
}

impl<B> Metrics<B>
where
B: sc_client_api::Backend<Block> + Send + Sync + 'static,
B::State: sc_client_api::StateBackend<sp_runtime::traits::HashFor<Block>>,
{
pub fn register(
registry: &Registry,
backend: Arc<B>,
period: std::time::Duration,
) -> Result<Self, PrometheusError> {
let mut ethereum_from_height = BTreeMap::new();
let mut ethereum_height = BTreeMap::new();

if let Some(storage) = backend.offchain_storage() {
Self::get_offchain_value(&storage, STORAGE_NETWORK_IDS_KEY, "network ids").map_or_else(
|| {
log::warn!("No network ids found in offchain storage. If you don't run bridge peer, this is fine");
Ok::<(), PrometheusError>(())
},
|networks: BTreeSet<framenode_runtime::NetworkId>| {
for network in networks {
let opts = Opts::new(
"eth_bridge_ethereum_to_handle_from_height",
"To handle from height for Ethereum network",
)
.const_label("network_id", format!("{}", network));
ethereum_from_height
.insert(network, register(Gauge::with_opts(opts)?, registry)?);
let opts =
Opts::new("eth_bridge_ethereum_height", "Height for Ethereum network")
.const_label("network_id", format!("{}", network));
ethereum_height.insert(network, register(Gauge::with_opts(opts)?, registry)?);
}
Ok(())
},
)?;
}

Ok(Self {
pending_transactions: register(
Gauge::new(
"eth_bridge_pending_transactions",
"Number of pending transactions",
)?,
registry,
)?,
failed_pending_transactions: register(
Gauge::new(
"eth_bridge_failed_pending_transactions",
"Number of failed pending transactions",
)?,
registry,
)?,
substrate_from_height: register(
Gauge::new(
"eth_bridge_substrate_from_height",
"To handle from height for Substrate network",
)?,
registry,
)?,
ethereum_from_height,
ethereum_height,
period,
backend,
})
}

pub fn get_offchain_value<T>(
storage: &<B as sc_client_api::Backend<Block>>::OffchainStorage,
key: &[u8],
description: &str,
) -> Option<T>
where
T: Decode,
{
storage
.get(sp_core::offchain::STORAGE_PREFIX, key)
.and_then(|value| {
T::decode(&mut &value[..])
.map_err(|e| {
log::error!("Failed to decode {} offchain value: {:?}", description, e);
})
.ok()
})
}

pub async fn run(self) {
loop {
if let Some(storage) = self.backend.offchain_storage() {
Self::get_offchain_value(
&storage,
STORAGE_PENDING_TRANSACTIONS_KEY,
"pending transactions",
)
.and_then(
|value: BTreeMap<H256, SignedTransactionData<Runtime>>| {
self.pending_transactions.set(value.len() as u64);
Some(())
},
);

Self::get_offchain_value(
&storage,
STORAGE_FAILED_PENDING_TRANSACTIONS_KEY,
"failed pending transactions",
)
.and_then(
|value: BTreeMap<H256, SignedTransactionData<Runtime>>| {
self.failed_pending_transactions.set(value.len() as u64);
Some(())
},
);

Self::get_offchain_value(
&storage,
STORAGE_SUB_TO_HANDLE_FROM_HEIGHT_KEY,
"handle from height for Substrate network",
)
.and_then(|value: framenode_runtime::BlockNumber| {
self.substrate_from_height.set(value as u64);
Some(())
});

for (network, gauge) in self.ethereum_from_height.iter() {
Self::get_offchain_value(
&storage,
format!("eth-bridge-ocw::eth-to-handle-from-height-{:?}", network)
.as_bytes(),
&format!("handle from height for Ethereum network {:?}", network),
)
.and_then(|value: u64| {
gauge.set(value as u64);
Some(())
});
}

for (network, gauge) in self.ethereum_height.iter() {
Self::get_offchain_value(
&storage,
&format!("eth-bridge-ocw::eth-height-{:?}", network).as_bytes(),
&format!("height for Ethereum network {:?}", network),
)
.and_then(|value: u64| {
gauge.set(value as u64);
Some(())
});
}
}
futures_timer::Delay::new(self.period).await;
}
}
}
1 change: 1 addition & 0 deletions node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
mod service;
mod cli;
mod command;
mod eth_bridge_metrics;
mod rpc;

fn main() -> sc_cli::Result<()> {
Expand Down
35 changes: 35 additions & 0 deletions node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use sp_core::offchain::OffchainStorage;
use sp_core::{ByteArray, Pair};
use sp_keystore::SyncCryptoStore;
use sp_runtime::offchain::STORAGE_PREFIX;
use sp_runtime::traits::IdentifyAccount;
use std::collections::BTreeSet;
use std::fs::File;
use std::sync::Arc;
Expand Down Expand Up @@ -168,6 +169,17 @@ pub fn new_partial(
{
let pk = eth_bridge::offchain::crypto::Public::from_slice(&first_pk_raw[..])
.expect("should have correct size");
let sub_public = sp_core::ecdsa::Public::from(pk.clone());
let public = secp256k1::PublicKey::parse_compressed(&sub_public.0).unwrap();
let address = common::eth::public_key_to_eth_address(&public);
let account = sp_runtime::MultiSigner::Ecdsa(sub_public.clone()).into_account();
log::warn!(
"Peer info: address: {:?}, account: {:?}, {}, public: {:?}",
address,
account,
account,
sub_public
);
if let Some(keystore) = keystore_container.local_keystore() {
if let Ok(Some(kep)) = keystore.key_pair::<eth_bridge::offchain::crypto::Pair>(&pk) {
let seed = kep.to_raw_vec();
Expand Down Expand Up @@ -224,6 +236,29 @@ pub fn new_partial(
STORAGE_SUB_NODE_URL_KEY,
&format!("http://{}", rpc_addr).encode(),
);

config
.prometheus_registry()
.and_then(|registry| {
crate::eth_bridge_metrics::Metrics::register(
registry,
backend.clone(),
std::time::Duration::from_secs(6),
)
.map_err(|e| {
log::error!("Failed to register metrics: {:?}", e);
})
.ok()
})
.and_then(|metrics| {
task_manager.spawn_essential_handle().spawn_blocking(
"eth-bridge-metrics",
Some("eth-bridge-metrics"),
metrics.run(),
);
Some(())
});

log::info!("Ethereum bridge peer initialized");
}

Expand Down
27 changes: 27 additions & 0 deletions pallets/assets/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,32 @@ benchmarks! {
assert_last_event::<T>(Event::Burn(caller, USDT.into(), 100_u32.into()).into())
}

update_balance {
add_assets::<T>(100)?;
let caller = alice::<T>();
frame_system::Pallet::<T>::inc_providers(&caller);
Assets::<T>::register_asset_id(
caller.clone(),
USDT.into(),
AssetSymbol(b"USDT".to_vec()),
AssetName(b"USDT".to_vec()),
DEFAULT_BALANCE_PRECISION,
Balance::zero(),
true,
None,
None,
).unwrap();
}: _(
RawOrigin::Root,
caller.clone(),
USDT.into(),
100_i128.into()
)
verify {
let usdt_issuance = Assets::<T>::total_issuance(&USDT.into())?;
assert_eq!(usdt_issuance, 100_u32.into());
}

set_non_mintable {
add_assets::<T>(100)?;
let caller = alice::<T>();
Expand Down Expand Up @@ -249,6 +275,7 @@ mod tests {
assert_ok!(Pallet::<Runtime>::test_benchmark_mint());
assert_ok!(Pallet::<Runtime>::test_benchmark_force_mint());
assert_ok!(Pallet::<Runtime>::test_benchmark_burn());
assert_ok!(Pallet::<Runtime>::test_benchmark_update_balance());
assert_ok!(Pallet::<Runtime>::test_benchmark_set_non_mintable());
});
}
Expand Down
Loading

0 comments on commit faabc97

Please sign in to comment.