Skip to content

Commit

Permalink
Fix config errors
Browse files Browse the repository at this point in the history
- The key of all TOML maps must be strings.
- fix ValueAfterTable error when using a HashMap
> toml-rs/toml-rs#142 (comment)
  • Loading branch information
floustar committed Nov 24, 2021
1 parent f9648fa commit 4bf277c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
33 changes: 22 additions & 11 deletions crates/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ckb_fixed_hash::{H160, H256};
use gw_common::error::Error;
use gw_jsonrpc_types::{
blockchain::{CellDep, Script},
godwoken::{ChallengeTargetType, L2BlockCommittedInfo, RollupConfig},
godwoken::{ChallengeTargetType, L2BlockCommittedInfo, RollupConfig, Uint32},
};
use serde::{Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -189,32 +189,34 @@ impl Default for OffChainValidatorConfig {
/// Config the base/minimal fee rules
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct FeeConfig {
meta_contract_fee_weight: u8,
sudt_transfer_fee_weight: u8,
withdraw_fee_weight: u8,
/// HashMap<fee_sudt_id, fee_rate>
///
/// fee_rate is known as gasPrice in Ethereum.
///
/// The defaut fee_sudt_id is 1, which is CKB, and the ckb_fee_rate is denoted in shannons,
/// which itself is a fractional denomination of CKBytes.
/// 1 CKByte = 100,000,000 Shannons
fee_rates: HashMap<u32, u64>,
meta_contract_fee_weight: u8,
sudt_transfer_fee_weight: u8,
withdraw_fee_weight: u8,
///
/// Note: The key of all TOML maps must be strings.
fee_rates: HashMap<Uint32, u64>,
}
impl FeeConfig {
pub fn is_supported_sudt(&self, sudt_id: u32) -> bool {
if self.fee_rates.is_empty() {
return true;
}
self.fee_rates.contains_key(&sudt_id)
self.fee_rates.contains_key(&sudt_id.into())
}
pub fn get_fee_rate(&self, sudt_id: u32) -> Result<u128, Error> {
if self.fee_rates.is_empty() {
return Ok(0);
}
let fee_rate = self
.fee_rates
.get(&sudt_id)
.get(&sudt_id.into())
.ok_or(Error::UnsupportedFeeSudt)?;
Ok(fee_rate.to_owned().into())
}
Expand All @@ -239,14 +241,23 @@ impl FeeConfig {
}
}
impl Default for FeeConfig {
/// The suggested configs:
/// ```toml
/// [mem_pool.fee_config]
/// meta_contract_fee_weight = 2
/// sudt_transfer_fee_weight = 2
/// withdraw_fee_weight = 2
///
/// [mem_pool.fee_config.fee_rates]
/// 0x1 = 500
/// ```
fn default() -> Self {
Self {
// suggested config is (CKB_SUDT_ID -> 500 shannons)
// const DEFAULT_CKB_FEE_RATE: u64 = 500;
fee_rates: Default::default(),
meta_contract_fee_weight: 2,
sudt_transfer_fee_weight: 2,
withdraw_fee_weight: 2,
/// The suggested default config is (CKB_SUDT_ID -> 500 shannons)
fee_rates: Default::default(),
}
}
}
Expand All @@ -261,7 +272,7 @@ impl From<FeeConfig> for gw_jsonrpc_types::godwoken::FeeConfig {
} = fee_config;
let fee_rates = fee_rates
.into_iter()
.map(|(id, fee_rate)| (id.into(), fee_rate.into()))
.map(|(id, fee_rate)| (id, fee_rate.into()))
.collect();
gw_jsonrpc_types::godwoken::FeeConfig {
fee_rates,
Expand Down
3 changes: 2 additions & 1 deletion crates/jsonrpc-types/src/godwoken.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::blockchain::Script;
use anyhow::{anyhow, Error as JsonError};
use ckb_fixed_hash::H256;
use ckb_jsonrpc_types::{JsonBytes, Uint128, Uint32, Uint64};
pub use ckb_jsonrpc_types::Uint32;
use ckb_jsonrpc_types::{JsonBytes, Uint128, Uint64};
use gw_types::{bytes::Bytes, offchain, packed, prelude::*};
use serde::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto};
Expand Down

0 comments on commit 4bf277c

Please sign in to comment.