darwinia-network / darwinia-common

Darwinia Runtime Pallet Library and Pangolin/Pangoro Testnet
https://rust-docs.darwinia.network/darwinia-common
GNU General Public License v3.0
30 stars 9 forks source link

The ratio of the gas to weight #705

Closed xiaoch05 closed 3 years ago

xiaoch05 commented 3 years ago

current we use 1:1 from gas to weight. Suggest: 1:1000

hackfisher commented 3 years ago

@xiaoch05 Any specific reason, could you give more details and description.

boundless-forest commented 3 years ago

We use DarwiniaGasWeightMapping in master now. https://github.com/darwinia-network/darwinia-common/blob/master/bin/node/runtime/pangolin/src/pallets/evm_.rs#L75-L83

boundless-forest commented 3 years ago

https://github.com/darwinia-network/darwinia-common/blob/master/frame/dvm/src/lib.rs#L147

#[pallet::weight(<T as darwinia_evm::Config>::GasWeightMapping::gas_to_weight(transaction.gas_limit.unique_saturated_into()))]`
pub fn transact(...) { ... }

The transaction call weight is calculated based on the gas_to_weight mapping from tx gas_limit to weight. Once the tx gas limit is larger enough, gas_limit * 10000 is larger than the block weight limit, then no other txs can be packaged.

xiaoch05 commented 3 years ago

@xiaoch05 Any specific reason, could you give more details and description.

I remember that the restricted weight for precompile contract requires the gas for a large number, about 3000_000_000, this is not a suitable ratio for gas and weight. https://github.com/darwinia-network/darwinia-common/blob/master/frame/evm/precompile/contracts/dispatch/src/lib.rs#L55

boundless-forest commented 3 years ago

https://github.com/paritytech/frontier/pull/413 This pull request does some benchmark about gas and weight ratio.

boundless-forest commented 3 years ago

Analysis

// Max Block weight setting 
pub const WEIGHT_PER_SECOND: Weight = 1_000_000_000_000;
const MAXIMUM_BLOCK_WEIGHT: Weight = 2 * WEIGHT_PER_SECOND; // 2_000_000_000_000

// Weight for Normal dispatch class
const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
.for_class(DispatchClass::Normal, |weights| {
        //  2_000_000_000_000 * 75% = 1500000000000
    weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT);
})

// Reserved for block init( 25% is such a large number here, in production env, the number will be smaller than this)
const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(25);

The block consumed weight in the current empty block(from Apps):

// Apps -> Storage -> System -> BlockWeight
system.blockWeight: ConsumedWeight
{
  normal: 225,000,000,
  operational: 0,
  mandatory: 6,417,263,000
}

Then lets us calculate the max weight of a single extrinsic supported:

max weight = 2_000_000_000_000 * 0.5 - 225000000 = 999775000000.0

Since the gas: weight = 1: 1000, the max gas of a single dvm transaction is:

max gas = 999775000000 / 1000 = 999775000.0

Test

Deploy and call contract in remix together MetaMask:

  1. Send transaction with gas_limit = 999775000000.0, works well.
  2. Send transaction with gas_limit = 999775000000.0 * 1.05, report ExhaustResources

Finally

999775000.0 can cover almost 200 DVM transactions whose gas limit is 5_000_000. So I think 1:1000 works fine in our chain now.

boundless-forest commented 3 years ago

@HackFisher @xiaoch05 I would like to see your ideas about this. I think we can keep the ratio 1:1000.

xiaoch05 commented 3 years ago

@HackFisher @xiaoch05 I would like to see your ideas about this. I think we can keep the ratio 1:1000.

I think we can estimate a ratio from their FLOPs,the ethereum's gas cost is https://github.com/ethereum/go-ethereum/blob/master/params/protocol_params.go

boundless-forest commented 3 years ago

FLOPs? My understanding is that we need an estimated ratio of 1 gas =? weight. If so, https://github.com/paritytech/frontier/pull/413 can be helpful.

xiaoch05 commented 3 years ago

FLOPs? My understanding is that we need an estimated ratio of 1 gas =? weight. If so, paritytech/frontier#413 can be helpful.

gas/weight means calculation and storage. So we should compare them for 1 gas and 1 weight. Right?

boundless-forest commented 3 years ago

Let me write a simple benchmark about this, then choose a more suitable value.