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

Add benchmark for balances #342

Merged
merged 5 commits into from
Mar 16, 2020
Merged
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
61 changes: 60 additions & 1 deletion Cargo.lock

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

11 changes: 4 additions & 7 deletions bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ node-runtime = { path = "../runtime" }
node-inspect = { optional = true, path = "../inspect" }
node-transaction-factory = { optional = true, path = "../transaction-factory" }

# TODO benchmarking
#frame-benchmarking-cli = { optional = true, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }
# benchmarking
frame-benchmarking-cli = { optional = true, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }

sc-cli = { package = "darwinia-cli", optional = true, path = "../../../client/cli" }

Expand All @@ -125,9 +125,7 @@ structopt = { version = "0.3.8", optional = true }
vergen = { version = "3.0.4", optional =true }

build-script-utils = { package = "substrate-build-script-utils", git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }

# TODO benchmarking
#frame-benchmarking-cli = { optional = true, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }
frame-benchmarking-cli = { optional = true, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }

sc-cli = { package = "darwinia-cli", optional = true, path = "../../../client/cli" }

Expand All @@ -143,8 +141,7 @@ browser = [
"wasm-bindgen-futures",
]
cli = [
# TODO benchmarking
# "frame-benchmarking-cli",
"frame-benchmarking-cli",

"node-executor/wasmi-errno",
"node-inspect",
Expand Down
7 changes: 3 additions & 4 deletions bin/node/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ pub enum Subcommand {
about = "Decode given block or extrinsic using current native runtime."
)]
Inspect(node_inspect::cli::InspectCmd),
// TODO benchmarking
// /// The custom benchmark subcommmand benchmarking runtime pallets.
// #[structopt(name = "benchmark", about = "Benchmark runtime pallets.")]
// Benchmark(frame_benchmarking_cli::BenchmarkCmd),
/// The custom benchmark subcommmand benchmarking runtime pallets.
#[structopt(name = "benchmark", about = "Benchmark runtime pallets.")]
Benchmark(frame_benchmarking_cli::BenchmarkCmd),
}

/// The `factory` command used to generate transactions.
Expand Down
14 changes: 7 additions & 7 deletions bin/node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ where

cmd.run(inspect)
}
// TODO benchmarking
// Some(Subcommand::Benchmark(_cmd)) => {
// cmd.init(&version)?;
// cmd.update_config(&mut config, load_spec, &version)?;
//
// cmd.run::<_, _, node_runtime::Block, node_executor::Executor>(config)
// }
Some(Subcommand::Benchmark(cmd)) => {
cmd.init(&version)?;
cmd.update_config(&mut config, load_spec, &version)?;

cmd.run::<_, _, node_runtime::Block, node_executor::Executor>(config)?;
Ok(())
}
Some(Subcommand::Factory(cli_args)) => {
cli_args.shared_params.init(&version)?;
cli_args.shared_params.update_config(&mut config, load_spec, &version)?;
Expand Down
6 changes: 2 additions & 4 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ sp-version = { default-features = false, git = "https://github.com/darwinia-netw
node-primitives = { default-features = false, path = "../primitives" }

# frame dependencies
# TODO benchmarking
#frame-benchmarking = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }
frame-benchmarking = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }
frame-executive = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }
frame-support = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }
frame-system = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }
Expand Down Expand Up @@ -88,8 +87,7 @@ std = [
"rustc-hex",
"serde",

# TODO benchmarking
# "frame-benchmarking/std",
"frame-benchmarking/std",
"frame-executive/std",
"frame-support/std",
"frame-system-rpc-runtime-api/std",
Expand Down
34 changes: 17 additions & 17 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,23 +837,23 @@ impl_runtime_apis! {
}

// TODO benchmarking
// impl frame_benchmarking::Benchmark<Block> for Runtime {
// fn dispatch_benchmark(
// module: Vec<u8>,
// extrinsic: Vec<u8>,
// steps: Vec<u32>,
// repeat: u32,
// ) -> Option<Vec<frame_benchmarking::BenchmarkResults>> {
// use frame_benchmarking::Benchmarking;
//
// match module.as_slice() {
// b"pallet-balances" | b"balances" => Balances::run_benchmark(extrinsic, steps, repeat).ok(),
// b"pallet-identity" | b"identity" => Identity::run_benchmark(extrinsic, steps, repeat).ok(),
// b"pallet-timestamp" | b"timestamp" => Timestamp::run_benchmark(extrinsic, steps, repeat).ok(),
// _ => None,
// }
// }
// }
impl frame_benchmarking::Benchmark<Block> for Runtime {
fn dispatch_benchmark(
module: Vec<u8>,
extrinsic: Vec<u8>,
steps: Vec<u32>,
repeat: u32,
) -> Option<Vec<frame_benchmarking::BenchmarkResults>> {
use frame_benchmarking::Benchmarking;

match module.as_slice() {
b"darwinia-ring" | b"ring" => Ring::run_benchmark(extrinsic, steps, repeat).ok(),
// b"pallet-identity" | b"identity" => Identity::run_benchmark(extrinsic, steps, repeat).ok(),
// b"pallet-timestamp" | b"timestamp" => Timestamp::run_benchmark(extrinsic, steps, repeat).ok(),
_ => None,
}
}
}
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions client/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ rpassword = "4.0.1"
# github.com
sc-informant = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }
sp-panic-handler = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }
sc-cli = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }
sc-client-api = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }
sp-blockchain = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }
sc-network = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }
Expand Down
17 changes: 17 additions & 0 deletions client/cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

//! Initialization errors.

use sc_cli::Error as ScError;

/// Result type alias for the CLI.
pub type Result<T> = std::result::Result<T, Error>;

Expand Down Expand Up @@ -68,3 +70,18 @@ impl std::error::Error for Error {
}
}
}

// Convert the Error of sc-cli
impl std::convert::From<ScError> for Error {
fn from(e: ScError) -> Error {
match e {
ScError::Io(e) => Error::Io(e),
ScError::Cli(e) => Error::Cli(e),
ScError::Service(e) => Error::Service(e),
ScError::Client(e) => Error::Client(e),
ScError::Input(e) => Error::Input(e),
ScError::InvalidListenMultiaddress => Error::InvalidListenMultiaddress,
ScError::Other(e) => Error::Other(e),
}
}
}
84 changes: 84 additions & 0 deletions frame/balances/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,87 @@ Balances currently contains **RING** and **KTON**.
**KTON** is the staking and governance credential of Darwinia Network, KTON can **only obtained by locking RING**, the initial supply is 0.

At present, some **RING** and **KTON** exist in the Ethereum network and the Tron network in the form of **ERC-20** and **TRC-20**. These TOKENs will be transferred to the Darwinia main network by 1:1 cross-chain conversion after the Darwinia main online.

**The Balances module provides functions for**

- Getting and setting free balances.
- Retrieving total, reserved and unreserved balances.
- Repatriating a reserved balance to a beneficiary account that exists.
- Transferring a balance between accounts (when not reserved).
- Slashing an account balance.
- Account creation and removal.
- Managing total issuance.
- Setting and managing locks.

## Terminology

- **Existential Deposit:** The minimum balance required to create or keep an account open. This prevents
"dust accounts" from filling storage. When the free plus the reserved balance (i.e. the total balance)
fall below this, then the account is said to be dead; and it loses its functionality as well as any
prior history and all information on it is removed from the chain's state.
No account should ever have a total balance that is strictly between 0 and the existential
deposit (exclusive). If this ever happens, it indicates either a bug in this module or an
erroneous raw mutation of storage.

- **Total Issuance:** The total number of units in existence in a system.

- **Reaping an account:** The act of removing an account by resetting its nonce. Happens after its
total balance has become zero (or, strictly speaking, less than the Existential Deposit).

- **Free Balance:** The portion of a balance that is not reserved. The free balance is the only
balance that matters for most operations.

- **Reserved Balance:** Reserved balance still belongs to the account holder, but is suspended.
Reserved balance can still be slashed, but only after all the free balance has been slashed.

- **Imbalance:** A condition when some funds were credited or debited without equal and opposite accounting
(i.e. a difference between total issuance and account balances). Functions that result in an imbalance will
return an object of the `Imbalance` trait that can be managed within your runtime logic. (If an imbalance is
simply dropped, it should automatically maintain any book-keeping such as total issuance.)

- **Lock:** A freeze on a specified amount of an account's free balance until a specified block number. Multiple
locks always operate over the same funds, so they "overlay" rather than "stack".


## Usage

### transfer balances

Transfer some liquid free balance to another account.

`transfer` will set the `FreeBalance` of the sender and receiver.
It will decrease the total issuance of the system by the `TransferFee`.
If the sender's account is below the existential deposit as a result
of the transfer, the account will be reaped.

### set balances

Set the balances of a given account.

This will alter `FreeBalance` and `ReservedBalance` in storage. it will
also decrease the total issuance of the system (`TotalIssuance`).
If the new free or reserved balance is below the existential deposit,
it will reset the account nonce (`frame_system::AccountNonce`).

The dispatch origin for this call is `root`.


## Weight

### Weight in Balances

| Call | Origin | weight |
|-------------------------------|--------|-----------|
| fn force\_transfer(...) | S | 1,000,000 |
| fn transfer(...) | S | 1,000,000 |
| fn transfer\_keep\_alive(...) | S | 1,000,000 |
| fn set\_balance(...) | R | 50,000 |

### Weight in Substrate's Balances

| Call | Origin | weight |
|-------------------------------|--------|-----------|
| fn force\_transfer(...) | S | 1,000,000 |
| fn transfer(...) | S | 1,000,000 |
| fn transfer\_keep\_alive(...) | S | 1,000,000 |
| fn set\_balance(...) | R | 50,000 |
4 changes: 2 additions & 2 deletions frame/balances/ring/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ codec = { package = "parity-scale-codec", version = "1.0.0", default-features =
serde = { version = "1.0.101", optional = true }

# github.com
# TODO benchmarking
#frame-benchmarking = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }
frame-benchmarking = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }
frame-support = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }
frame-system = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" }

Expand All @@ -36,6 +35,7 @@ std = [
"codec/std",
"serde",

"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",

Expand Down
Loading