Skip to content

Commit

Permalink
Merge pull request #826 from jjyr/merge-1.6-rc
Browse files Browse the repository at this point in the history
Merge 1.6-rc
  • Loading branch information
jjyr authored Oct 25, 2022
2 parents 28fa963 + 06fcaab commit 3010f64
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 29 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com).

- Decouple block producing, submission and confirming [#776](https://github.com/nervosnetwork/godwoken/pull/776)

## [v1.6.2] - 2022-10-25

- Increase max return data [#822](https://github.com/godwokenrises/godwoken/pull/822)

## [v1.6.1] - 2022-10-18

- Add `fee_rate` option to block_producer config [#815](https://github.com/godwokenrises/godwoken/pull/815)

## [v1.6.0] - 2022-09-13


Expand Down
1 change: 1 addition & 0 deletions crates/block-producer/src/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ impl BlockProducer {
&self.rpc_client.indexer,
self.wallet.lock_script().to_owned(),
local_cells_manager,
self.config.fee_rate,
)
.await?;
debug_assert_eq!(
Expand Down
32 changes: 28 additions & 4 deletions crates/block-producer/src/challenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,13 @@ impl Challenger {
let challenger_lock_dep = self.ckb_genesis_info.sighash_dep();
let challenger_lock = self.wallet.lock_script().to_owned();
tx_skeleton.cell_deps_mut().push(challenger_lock_dep);
fill_tx_fee(&mut tx_skeleton, &self.rpc_client.indexer, challenger_lock).await?;
fill_tx_fee(
&mut tx_skeleton,
&self.rpc_client.indexer,
challenger_lock,
self.config.fee_rate,
)
.await?;

let tx = self.wallet.sign_tx_skeleton(tx_skeleton)?;

Expand Down Expand Up @@ -498,7 +504,13 @@ impl Challenger {
let challenger_lock_dep = self.ckb_genesis_info.sighash_dep();
let challenger_lock = self.wallet.lock_script().to_owned();
tx_skeleton.cell_deps_mut().push(challenger_lock_dep);
fill_tx_fee(&mut tx_skeleton, &self.rpc_client.indexer, challenger_lock).await?;
fill_tx_fee(
&mut tx_skeleton,
&self.rpc_client.indexer,
challenger_lock,
self.config.fee_rate,
)
.await?;

let tx = self.wallet.sign_tx_skeleton(tx_skeleton)?;

Expand Down Expand Up @@ -535,7 +547,13 @@ impl Challenger {
let challenger_lock_dep = self.ckb_genesis_info.sighash_dep();
let challenger_lock = self.wallet.lock_script().to_owned();
tx_skeleton.cell_deps_mut().push(challenger_lock_dep);
fill_tx_fee(&mut tx_skeleton, &self.rpc_client.indexer, challenger_lock).await?;
fill_tx_fee(
&mut tx_skeleton,
&self.rpc_client.indexer,
challenger_lock,
self.config.fee_rate,
)
.await?;

self.wallet.sign_tx_skeleton(tx_skeleton)
}
Expand Down Expand Up @@ -638,7 +656,13 @@ impl Challenger {
}

let owner_lock = self.wallet.lock_script().to_owned();
fill_tx_fee(&mut tx_skeleton, &self.rpc_client.indexer, owner_lock).await?;
fill_tx_fee(
&mut tx_skeleton,
&self.rpc_client.indexer,
owner_lock,
self.config.fee_rate,
)
.await?;
self.wallet.sign_tx_skeleton(tx_skeleton)
}

Expand Down
17 changes: 15 additions & 2 deletions crates/block-producer/src/cleaner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,22 @@ pub struct Cleaner {
ckb_genesis_info: CKBGenesisInfo,
wallet: Wallet,
consumed_verifiers: ConsumedVerifiers,
fee_rate: u64,
}

impl Cleaner {
pub fn new(rpc_client: RPCClient, ckb_genesis_info: CKBGenesisInfo, wallet: Wallet) -> Self {
pub fn new(
rpc_client: RPCClient,
ckb_genesis_info: CKBGenesisInfo,
wallet: Wallet,
fee_rate: u64,
) -> Self {
Cleaner {
rpc_client,
ckb_genesis_info,
wallet,
consumed_verifiers: Arc::new(Mutex::new(Vec::new())),
fee_rate,
}
}

Expand Down Expand Up @@ -229,7 +236,13 @@ impl Cleaner {
.push(to_input_cell_info(owner_input));

let owner_lock = self.wallet.lock_script().to_owned();
fill_tx_fee(&mut tx_skeleton, &self.rpc_client.indexer, owner_lock).await?;
fill_tx_fee(
&mut tx_skeleton,
&self.rpc_client.indexer,
owner_lock,
self.fee_rate,
)
.await?;
self.wallet.sign_tx_skeleton(tx_skeleton)
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/block-producer/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,14 @@ pub async fn run(config: Config, skip_config_check: bool) -> Result<()> {
contracts_dep_manager.clone(),
unlocker_wallet,
config.debug.clone(),
block_producer_config.fee_rate,
);

let cleaner = Arc::new(Cleaner::new(
rpc_client.clone(),
ckb_genesis_info.clone(),
wallet,
block_producer_config.fee_rate,
));

let challenger_wallet = match block_producer_config.wallet_config {
Expand Down
21 changes: 18 additions & 3 deletions crates/block-producer/src/withdrawal_unlocker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ impl FinalizedWithdrawalUnlocker {
contracts_dep_manager: ContractsCellDepManager,
wallet: Wallet,
debug_config: DebugConfig,
fee_rate: u64,
) -> Self {
let unlocker =
DefaultUnlocker::new(rpc_client, ckb_genesis_info, contracts_dep_manager, wallet);
let unlocker = DefaultUnlocker::new(
rpc_client,
ckb_genesis_info,
contracts_dep_manager,
wallet,
fee_rate,
);

FinalizedWithdrawalUnlocker {
unlocker,
Expand Down Expand Up @@ -211,6 +217,7 @@ struct DefaultUnlocker {
ckb_genesis_info: CKBGenesisInfo,
contracts_dep_manager: ContractsCellDepManager,
wallet: Wallet,
fee_rate: u64,
}

impl DefaultUnlocker {
Expand All @@ -221,12 +228,14 @@ impl DefaultUnlocker {
ckb_genesis_info: CKBGenesisInfo,
contracts_dep_manager: ContractsCellDepManager,
wallet: Wallet,
fee_rate: u64,
) -> Self {
DefaultUnlocker {
rpc_client,
ckb_genesis_info,
contracts_dep_manager,
wallet,
fee_rate,
}
}
}
Expand Down Expand Up @@ -264,7 +273,13 @@ impl BuildUnlockWithdrawalToOwner for DefaultUnlocker {
tx_skeleton.cell_deps_mut().push(owner_lock_dep);

let owner_lock = self.wallet.lock_script().to_owned();
fill_tx_fee(&mut tx_skeleton, &self.rpc_client.indexer, owner_lock).await?;
fill_tx_fee(
&mut tx_skeleton,
&self.rpc_client.indexer,
owner_lock,
self.fee_rate,
)
.await?;
self.wallet.sign_tx_skeleton(tx_skeleton)
}
}
6 changes: 6 additions & 0 deletions crates/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ pub struct RegistryAddressConfig {
pub struct BlockProducerConfig {
#[serde(default = "default_check_mem_block_before_submit")]
pub check_mem_block_before_submit: bool,
#[serde(default = "default_fee_rate")]
pub fee_rate: u64,
#[serde(flatten)]
pub psc_config: PscConfig,
pub block_producer: RegistryAddressConfig,
Expand Down Expand Up @@ -215,6 +217,10 @@ fn test_psc_config_optional() {
);
}

const fn default_fee_rate() -> u64 {
1000
}

const fn default_check_mem_block_before_submit() -> bool {
false
}
Expand Down
7 changes: 5 additions & 2 deletions crates/tools/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ async fn main() -> Result<()> {
.arg(Arg::with_name("type-id").long("type-id").takes_value(true).required(true).help("The type-id of the exist cell"))
.arg(Arg::with_name("cell-data-path").long("cell-data-path").takes_value(true).required(true).help("The path of new data"))
.arg(arg_privkey_path.clone())
.arg(Arg::with_name("fee-rate").long("fee-rate").takes_value(true).required(false).default_value("1000").help("tx fee rate"))
)
.subcommand(
SubCommand::with_name("deposit-ckb")
Expand Down Expand Up @@ -1038,20 +1039,22 @@ async fn main() -> Result<()> {
let type_id = cli_args::to_h256(m.value_of("type-id").unwrap())?;
let cell_data_path = Path::new(m.value_of("cell-data-path").unwrap());
let privkey_path = Path::new(m.value_of("privkey-path").unwrap());
let fee_rate: u64 = m.value_of("fee-rate").unwrap().parse()?;
let pk_path = {
let mut buf = PathBuf::new();
buf.push(privkey_path);
buf
};
update_cell::update_cell(
update_cell::update_cell(update_cell::UpdateCellArgs {
ckb_rpc_url,
indexer_rpc_url,
tx_hash,
index,
type_id,
cell_data_path,
pk_path,
)
fee_rate,
})
.await?;
}
Some(("deposit-ckb", m)) => {
Expand Down
40 changes: 30 additions & 10 deletions crates/tools/src/update_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,29 @@ use gw_utils::{
};
use std::path::{Path, PathBuf};

pub async fn update_cell<P: AsRef<Path>>(
ckb_rpc_url: &str,
indexer_rpc_url: &str,
tx_hash: [u8; 32],
index: u32,
type_id: [u8; 32],
cell_data_path: P,
pk_path: PathBuf,
) -> Result<()> {
pub struct UpdateCellArgs<'a, P> {
pub ckb_rpc_url: &'a str,
pub indexer_rpc_url: &'a str,
pub tx_hash: [u8; 32],
pub index: u32,
pub type_id: [u8; 32],
pub cell_data_path: P,
pub pk_path: PathBuf,
pub fee_rate: u64,
}

pub async fn update_cell<P: AsRef<Path>>(args: UpdateCellArgs<'_, P>) -> Result<()> {
let UpdateCellArgs {
ckb_rpc_url,
indexer_rpc_url,
tx_hash,
index,
type_id,
cell_data_path,
pk_path,
fee_rate,
} = args;

let mut rpc_client = CkbRpcClient::new(ckb_rpc_url);
let indexer_client = CKBIndexerClient::with_url(indexer_rpc_url)?;
// check existed_cell
Expand Down Expand Up @@ -105,7 +119,13 @@ pub async fn update_cell<P: AsRef<Path>>(
// use same lock of existed cell to pay fee
let payment_lock = existed_cell.lock();
// tx fee cell
fill_tx_fee(&mut tx_skeleton, &indexer_client, payment_lock.clone()).await?;
fill_tx_fee(
&mut tx_skeleton,
&indexer_client,
payment_lock.clone(),
fee_rate,
)
.await?;
// sign
let wallet = Wallet::from_config(&WalletConfig {
privkey_path: pk_path,
Expand Down
2 changes: 1 addition & 1 deletion crates/traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ edition = "2018"
[dependencies]
gw-types = { path = "../types" }
gw-common = { path = "../common" }
gw-db = { path = "../db" }
gw-db = { path = "../db" }
2 changes: 1 addition & 1 deletion crates/tx-filter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ gw-traits = { path = "../traits" }
gw-types = { path = "../types" }
thiserror = "1.0"
log = "0.4"
hex = "0.4"
hex = "0.4"
20 changes: 14 additions & 6 deletions crates/utils/src/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ use gw_types::{
};

/// Calculate tx fee
/// TODO accept fee rate args
fn calculate_required_tx_fee(tx_size: usize) -> u64 {
fn calculate_required_tx_fee(tx_size: usize, fee_rate: u64) -> u64 {
// tx_size * KB / MIN_FEE_RATE
tx_size as u64
(tx_size as u64) * fee_rate / 1000
}

/// Add fee cell to tx skeleton
Expand All @@ -32,6 +31,7 @@ pub async fn fill_tx_fee_with_local(
client: &CKBIndexerClient,
lock_script: Script,
local_cells_manager: &LocalCellsManager,
fee_rate: u64,
) -> Result<()> {
const CHANGE_CELL_CAPACITY: u64 = 61_00000000;

Expand All @@ -54,7 +54,7 @@ pub async fn fill_tx_fee_with_local(
// calculate required fee
// Try to generate a change output cell. If input cannot cover fee, query an owner cell.
let tx_size = estimate_tx_size_with_change(tx_skeleton)?;
let tx_fee = calculate_required_tx_fee(tx_size);
let tx_fee = calculate_required_tx_fee(tx_size, fee_rate);
let max_paid_fee = tx_skeleton
.calculate_fee()?
.saturating_sub(CHANGE_CELL_CAPACITY);
Expand Down Expand Up @@ -102,7 +102,7 @@ pub async fn fill_tx_fee_with_local(
}));

let tx_size = estimate_tx_size_with_change(tx_skeleton)?;
let tx_fee = calculate_required_tx_fee(tx_size);
let tx_fee = calculate_required_tx_fee(tx_size, fee_rate);
let max_paid_fee = tx_skeleton
.calculate_fee()?
.saturating_sub(CHANGE_CELL_CAPACITY);
Expand All @@ -128,8 +128,16 @@ pub async fn fill_tx_fee(
tx_skeleton: &mut TransactionSkeleton,
client: &CKBIndexerClient,
lock_script: Script,
fee_rate: u64,
) -> Result<()> {
fill_tx_fee_with_local(tx_skeleton, client, lock_script, &Default::default()).await
fill_tx_fee_with_local(
tx_skeleton,
client,
lock_script,
&Default::default(),
fee_rate,
)
.await
}

/// query payment cells, the returned cells should provide at least required_capacity fee,
Expand Down

0 comments on commit 3010f64

Please sign in to comment.