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

Review all weights #354

Merged
merged 9 commits into from
Mar 18, 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
27 changes: 8 additions & 19 deletions frame/balances/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,11 @@ 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 |
## Weights

| Call | Origin | Darwinia | Substrate |
|-------------------------------|--------|-----------|-----------|
| fn force\_transfer(...) | S | 1,000,000 | 1,000,000 |
| fn transfer(...) | S | 1,000,000 | 1,000,000 |
| fn transfer\_keep\_alive(...) | S | 1,000,000 | 1,000,000 |
| fn set\_balance(...) | R | 50,000 | 50,000 |
7 changes: 7 additions & 0 deletions frame/chainrelay/eth/backing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# darwinia-eth-backing

## Weights

| Call | Origin | darwinia |
|----------------|--------|----------|
| fn redeem(...) | S | 10,000 |
7 changes: 7 additions & 0 deletions frame/chainrelay/eth/backing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use ethabi::{Event as EthEvent, EventParam as EthEventParam, ParamType, RawLog};
use frame_support::{
decl_error, decl_event, decl_module, decl_storage, ensure,
traits::{Currency, OnUnbalanced, Time},
weights::SimpleDispatchInfo,
};
use frame_system::{self as system, ensure_signed};
use sp_runtime::{
Expand Down Expand Up @@ -152,6 +153,12 @@ decl_module! {

fn deposit_event() = default;

/// Redeem balances
///
/// # <weight>
/// - `O(1)`
/// # </weight>
#[weight = SimpleDispatchInfo::FixedNormal(10_000)]
pub fn redeem(origin, r#for: RedeemFor) {
let _relayer = ensure_signed(origin)?;

Expand Down
12 changes: 12 additions & 0 deletions frame/chainrelay/eth/relay/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# darwinia-eth-relay

## Weights

| Call | Origin | Darwinia |
|-------------------------------------------|--------|----------|
| fn relay\_header(...) | S | 100,000 |
| fn check\_receipt(...) | S | 100,000 |
| fn add\_authority(...) | R | 50,000 |
| fn remove\_authority(...) | R | 50,000 |
| fn toggle\_check\_authorities(...) | R | 10,000 |
| fn set\_number\_of\_blocks\_finality(...) | R | 10,000 |
59 changes: 57 additions & 2 deletions frame/chainrelay/eth/relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ decl_module! {
<Module<T>>::deposit_event(RawEvent::SetGenesisHeader(relayer, header, genesis_difficulty));
}

/// Relay header of eth block, store the passing header
/// to darwinia Storage if it is verified.
///
/// # <weight>
/// - `O(1)`.
/// - Limited Storage reads
/// - One storage read
/// - One storage write
/// - Up to one event
/// # </weight>
#[weight = SimpleDispatchInfo::FixedNormal(100_000)]
pub fn relay_header(origin, header: EthHeader) {
let relayer = ensure_signed(origin)?;
if Self::check_authorities() {
Expand All @@ -192,6 +203,14 @@ decl_module! {
<Module<T>>::deposit_event(RawEvent::RelayHeader(relayer, header));
}

/// Check receipt
///
/// # <weight>
/// - `O(1)`.
/// - Limited Storage reads
/// - Up to one event
/// # </weight>
#[weight = SimpleDispatchInfo::FixedNormal(100_000)]
pub fn check_receipt(origin, proof_record: EthReceiptProof) {
let relayer = ensure_signed(origin)?;
if Self::check_authorities() {
Expand All @@ -205,6 +224,14 @@ decl_module! {

// --- root call ---

/// Add authority
///
/// # <weight>
/// - `O(A)` where `A` length of `authorities`
/// - One storage mutation (codec `O(A)`).
/// - Up to one event
/// # </weight>
#[weight = SimpleDispatchInfo::FixedNormal(50_000)]
pub fn add_authority(origin, who: T::AccountId) {
ensure_root(origin)?;

Expand All @@ -215,6 +242,14 @@ decl_module! {
}
}

/// Remove authority
///
/// # <weight>
/// - `O(A)` where `A` length of `authorities`
/// - One storage mutation (codec `O(A)`).
/// - Up to one event
/// # </weight>
#[weight = SimpleDispatchInfo::FixedNormal(50_000)]
pub fn remove_authority(origin, who: T::AccountId) {
ensure_root(origin)?;

Expand All @@ -227,6 +262,14 @@ decl_module! {
}
}

/// Check authorities
///
/// # <weight>
/// - `O(1)`.
/// - One storage write
/// - Up to one event
/// # </weight>
#[weight = SimpleDispatchInfo::FixedNormal(10_000)]
pub fn toggle_check_authorities(origin) {
ensure_root(origin)?;

Expand All @@ -235,13 +278,25 @@ decl_module! {
<Module<T>>::deposit_event(RawEvent::ToggleCheckAuthorities(Self::check_authorities()));
}

#[weight = SimpleDispatchInfo::FixedNormal(5_000)]
/// Set number of blocks finality
///
/// # <weight>
/// - `O(1)`.
/// - One storage write
/// # </weight>
#[weight = SimpleDispatchInfo::FixedNormal(10_000)]
pub fn set_number_of_blocks_finality(origin, #[compact] new: u64) {
ensure_root(origin)?;
NumberOfBlocksFinality::put(new);
}

#[weight = SimpleDispatchInfo::FixedNormal(5_000)]
/// Set number of blocks finality
///
/// # <weight>
/// - `O(1)`.
/// - One storage write
/// # </weight>
#[weight = SimpleDispatchInfo::FixedNormal(10_000)]
pub fn set_number_of_blocks_safe(origin, #[compact] new: u64) {
ensure_root(origin)?;
NumberOfBlocksSafe::put(new);
Expand Down
12 changes: 12 additions & 0 deletions frame/elections-phragmen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# elections-phragmen

## Weights

| Call | Origin | darwinia | substrate |
|--------------------------------|--------|-----------|-----------|
| fn remove\_member(...) | S | 2,000,000 | 2,000,000 |
| fn renounce\_candidacy(...) | S | 2,000,000 | 2,000,000 |
| fn report\_defunct\_voter(...) | S | 1,000,000 | 1,000,000 |
| fn submit\_candidacy(...) | S | 500,000 | 500,000 |
| fn vote(...) | S | 100,000 | 100,000 |
| fn remove\_voter(...) | S | 10,000 | 10,000 |
37 changes: 29 additions & 8 deletions frame/staking/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,45 @@ Some concepts should have some explaination for the changing from substrate

power is a mixture of ring and kton.

+ For *RING*: `power = ring_ratio * POWER_COUNT / 2`
+ For *KTON*: `power = kton_ratio * POWER_COUNT / 2`
+ *RING*: `power = ring_ratio * POWER_COUNT / 2`
+ *KTON*: `power = kton_ratio * POWER_COUNT / 2`

### rebond
We use `currency_to_power` and `power_of` to calculcate `power`.

The darwinia style `rebond` implementation.
### rebond

We doesn't support `rebond` currently now.

### withdraw

What should happen after all balances being unbonded?(the locked balance)


## Moudle
+ delete `withdraw_unbond`
+ delete `slashable_balance_of`
+ use `power_of`
+ use `stake_of`
### delete `withdraw_unbond`

+ **withdraw_unbond**: Remove all associated data of a stash account from the staking system.

Darwinia has `active_balance` and `active_deposit_balance`, we calculate `normal_balance` by `active_balance - active_deposit_balance`, the `normal_balance` is **free to transfer**, so we don't need the `withdraw_unbond` function actually.

### delete `slashable_balance_of`

+ **slashable_balance_of**: The total balance that can be slashed from a stash account as of right now.

We use `power_of` and `stake_of` instead of `slashable_balance_of`:

+ **power_of**: The total power that can be slashed from a stash account as of right now.
+ **stake_of**: The `active_ring` and `active_kton` from a stash account.

**For if an account is slashale:**

Just use `power_of`, if the return `power` is zero, the target account is not slashable.

**For the amount of slashable balances:**

The slashable balances actually mean `active-ring` and `active-kton` in darwinia's staking
process, we can use `Staking::ledger(controller)` to get a `StakingLedger` which contains
the `active-ring` and `active-kton` the `controller` have.

## Structs

Expand Down
104 changes: 104 additions & 0 deletions frame/staking/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Staking

The Staking module is the means by which a set of network maintainers (known as _authorities_
in some contexts and _validators_ in others) are chosen based upon those who voluntarily place
funds under deposit. Under deposit, those funds are rewarded under normal operation but are
held at pain of _slash_ (expropriation) should the staked maintainer be found not to be
discharging its duties properly.

## Terminology

- **Staking**: The process of locking up funds for some time, placing them at risk of slashing
(loss) in order to become a rewarded maintainer of the network.
- **Validating**: The process of running a node to actively maintain the network, either by
producing blocks or guaranteeing finality of the chain.
- **Nominating**: The process of placing staked funds behind one or more validators in order to
share in any reward, and punishment, they take.
- **Stash account**: The account holding an owner's funds used for staking.
- **Controller account**: The account that controls an owner's funds for staking.
- **Era**: A (whole) number of sessions, which is the period that the validator set (and each
validator's active nominator set) is recalculated and where rewards are paid out.
- **Slash**: The punishment of a staker by reducing its funds.

## Weights

| Call | Origin | darwinia | substrate |
|---------------------------------------|--------|-----------|-----------|
| fn cancel\_deferred\_slash | R | 1,000,000 | 1,000,000 |
| fn validate(...) | S | 750,000 | 750,000 |
| fn nominate(...) | S | 750,000 | 750,000 |
| fn set\_controller(...) | S | 750,000 | 750,000 |
| fn try\_claim\_deposits\_with\_punish | S | 750,000 | - |
| fn deposit\_extra(...) | S | 500,000 | - |
| fn bond(...) | S | 500,000 | 500,000 |
| fn bond\_extra(...) | S | 500,000 | 500,000 |
| fn chill(...) | S | 500,000 | 500,000 |
| fn set\_payee(...) | S | 500,000 | 500,000 |
| fn payout\_nominator | R | 500,000 | 500,000 |
| fn set\_history\_depth | R | 500,000 | 500,000 |
| fn unbond(...) | S | 500,000 | 400,000 |
| fn clain\_mature\_deposits | S | 100,000 | 10,000 |
| fn set\_invulnerables | R | 10,000 | 10,000 |
| fn force\_unstake | R | 10,000 | 10,000 |
| fn force\_new\_era_\always | R | 10,000 | 10,000 |
| fn set\_validator\_count(...) | R | 10,000 | 5,000 |
| fn force\_no\_eras | R | 10,000 | 5,000 |
| fn reap\_stash | R | 10,000 | - |

## FAQ

### Q1: What is the relationship between stash and controller?

Stash account holding an owner's funds used for staking, controller account controls an owner's funds for staking.

### Q2: What does staker mean?

Almost any interaction with the Staking module requires a process of **bonding** (also known
as being a *staker*). To become *bonded*, a fund-holding account known as the *stash account*,
which holds some or all of the funds that become frozen in place as part of the staking process,
is paired with an active **controller** account, which issues instructions on how they shall be
used.

### Q3: What are the differents from BlockNumber, Era, Session and TimeStamp?

We config the relationships manually, for example:

```rust
pub fn start_session(session_index: SessionIndex) {
for i in Session::current_index()..session_index {
Staking::on_finalize(System::block_number());
System::set_block_number((i + 1).into());
Timestamp::set_timestamp(System::block_number() * 1000);
Session::on_initialize(System::block_number());
}
assert_eq!(Session::current_index(), session_index);
}
```

| Unit | Value |
|-------------|----------|
| BlockNumber | 4 |
| Session | 3 |
| Timestamp | 3 * 1000 |
| Era | 1 |

### Q4: What is the process of rewrad?

```rust
// 1. Insert stash account into Payment map.
Payee::<Test>::insert(11, RewardDestination::Controller);
// 2. Add reward points to validators using their stash account ID.
Staking::reward_by_ids(vec![(11, 50)]);
// 3. Make all validator and nominator request their payment
make_all_reward_payment(0); // 0 means 0 era.
```

**What happend exactly?**

`make_all_reward_payment` triggers reward process:

+ `make_all_reward_payment`
+ reward nominators
+ payout from nominators to controller
+ reward validators
+ payout from validators to controller
Loading