Skip to content

Commit

Permalink
refactor and improve gw-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
blckngm committed Feb 13, 2023
1 parent a4d54ca commit 494b31e
Show file tree
Hide file tree
Showing 64 changed files with 1,019 additions and 4,757 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/godwoken-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
POLYJUICE_GIT_URL=https://github.com/${{ github.repository }}
POLYJUICE_GIT_CHECKOUT=${{ github.ref }}
GODWOKEN_KICKER_REPO=godwokenrises/godwoken-kicker
GODWOKEN_KICKER_REF=refs/pull/338/merge
GODWOKEN_KICKER_REF=refs/pull/345/merge
27 changes: 20 additions & 7 deletions Cargo.lock

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

21 changes: 6 additions & 15 deletions crates/block-producer/src/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use gw_types::{
h256::*,
offchain::{global_state_from_slice, CompatibleFinalizedTimepoint, DepositInfo, InputCellInfo},
packed::{
CellDep, CellInput, CellOutput, GlobalState, L2Block, RollupAction, RollupActionUnion,
CellDep, CellOutput, GlobalState, L2Block, RollupAction, RollupActionUnion,
RollupSubmitBlock, Script, Transaction, WithdrawalRequestExtra, WitnessArgs,
},
prelude::*,
Expand Down Expand Up @@ -225,13 +225,10 @@ impl BlockProducer {
.map(|d| d.clone().into());

// rollup cell
tx_skeleton.inputs_mut().push(InputCellInfo {
input: CellInput::new_builder()
.previous_output(rollup_cell.out_point.clone())
.since(since.as_u64().pack())
.build(),
cell: rollup_cell.clone(),
});
tx_skeleton.inputs_mut().push(InputCellInfo::with_since(
rollup_cell.clone(),
since.as_u64(),
));
// rollup deps
tx_skeleton.cell_deps_mut().extend(rollup_deps);
// deposit lock dep
Expand Down Expand Up @@ -353,13 +350,7 @@ impl BlockProducer {
// deposit cells
for deposit in &deposit_cells {
log::info!("using deposit cell {:?}", deposit.cell.out_point);
let input = CellInput::new_builder()
.previous_output(deposit.cell.out_point.clone())
.build();
tx_skeleton.inputs_mut().push(InputCellInfo {
input,
cell: deposit.cell.clone(),
});
tx_skeleton.inputs_mut().push(deposit.cell.clone().into());
}

// custodian cells
Expand Down
35 changes: 8 additions & 27 deletions crates/block-producer/src/challenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use gw_types::{
h256::*,
offchain::{global_state_from_slice, CellInfo, InputCellInfo},
packed::{
CellDep, CellInput, CellOutput, ChallengeLockArgs, ChallengeLockArgsReader,
ChallengeTarget, GlobalState, OutPoint, Script, Transaction, WitnessArgs,
CellDep, CellOutput, ChallengeLockArgs, ChallengeLockArgsReader, ChallengeTarget,
GlobalState, OutPoint, Script, Transaction, WitnessArgs,
},
prelude::*,
};
Expand Down Expand Up @@ -350,7 +350,7 @@ impl Challenger {
.with_context(|| format!("waiting for tx proposed 0x{}", to_hex(&verifier_tx_hash)))??;

// Build cancellation transaction
let challenge_input = to_input_cell_info(challenge_cell);
let challenge_input = challenge_cell.into();
let verifier_context = {
let contracts_dep = self.contracts_dep_manager.load();
let cell_dep = cancel_output.verifier_dep(&contracts_dep)?;
Expand Down Expand Up @@ -523,13 +523,13 @@ impl Challenger {
tx_skeleton.witnesses_mut().push(rollup_witness);

// Challenge
let challenge_input = to_input_cell_info_with_since(challenge_cell, since);
let challenge_input = InputCellInfo::with_since(challenge_cell, since);
let challenge_dep = contracts_dep.challenge_cell_lock.clone().into();
tx_skeleton.cell_deps_mut().push(challenge_dep);
tx_skeleton.inputs_mut().push(challenge_input);

// Stake
let stake_inputs = stake_cells.into_iter().map(to_input_cell_info);
let stake_inputs = stake_cells.into_iter().map(Into::into);
let stake_dep = contracts_dep.stake_cell_lock.clone().into();
tx_skeleton.cell_deps_mut().push(stake_dep);
tx_skeleton.inputs_mut().extend(stake_inputs);
Expand Down Expand Up @@ -722,7 +722,7 @@ impl Challenger {
let owner_lock = self.wallet.lock_script().to_owned();

if let Ok(Some(cell)) = rpc_client.query_owner_cell(owner_lock, spent_inputs).await {
return Ok(to_input_cell_info(cell));
return Ok(cell.into());
}

log::debug!("can't find a owner cell for verifier, try wait verifier tx committed");
Expand All @@ -739,7 +739,7 @@ impl Challenger {
query.ok_or_else(|| anyhow!("can't find an owner cell for verifier"))?
};

Ok(to_input_cell_info(cell))
Ok(cell.into())
}

async fn dry_run_transaction(&self, tx: &Transaction, action: &str) -> Result<()> {
Expand Down Expand Up @@ -784,7 +784,7 @@ impl RollupState {
}

fn rollup_input(&self) -> InputCellInfo {
to_input_cell_info(self.rollup_cell.clone())
self.rollup_cell.clone().into()
}

fn rollup_output(&self) -> CellOutput {
Expand Down Expand Up @@ -852,25 +852,6 @@ fn to_tip_number(event: &ChainEvent) -> u64 {
tip_block.header().raw().number().unpack()
}

fn to_input_cell_info(cell_info: CellInfo) -> InputCellInfo {
InputCellInfo {
input: CellInput::new_builder()
.previous_output(cell_info.out_point.clone())
.build(),
cell: cell_info,
}
}

fn to_input_cell_info_with_since(cell_info: CellInfo, since: u64) -> InputCellInfo {
InputCellInfo {
input: CellInput::new_builder()
.previous_output(cell_info.out_point.clone())
.since(since.pack())
.build(),
cell: cell_info,
}
}

fn to_hex(hash: &H256) -> String {
hex::encode(hash.as_slice())
}
Expand Down
18 changes: 3 additions & 15 deletions crates/block-producer/src/cleaner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ use gw_utils::transaction_skeleton::TransactionSkeleton;
use gw_utils::{fee::fill_tx_fee, wallet::Wallet};

use anyhow::{anyhow, Result};
use ckb_types::prelude::{Builder, Entity};
use gw_challenge::cancel_challenge::RecoverAccountsContext;
use gw_rpc_client::rpc_client::RPCClient;
use gw_types::core::Status;
use gw_types::h256::*;
use gw_types::offchain::{global_state_from_slice, CellInfo, InputCellInfo};
use gw_types::packed::{CellDep, CellInput, Transaction, WitnessArgs};
use gw_types::offchain::{global_state_from_slice, InputCellInfo};
use gw_types::packed::{CellDep, Transaction, WitnessArgs};
use gw_types::prelude::Unpack;
use tracing::instrument;

Expand Down Expand Up @@ -237,9 +236,7 @@ impl Cleaner {

let owner_lock_dep = self.ckb_genesis_info.sighash_dep();
tx_skeleton.cell_deps_mut().push(owner_lock_dep);
tx_skeleton
.inputs_mut()
.push(to_input_cell_info(owner_input));
tx_skeleton.inputs_mut().push(owner_input.into());

let owner_lock = self.wallet.lock_script().to_owned();
fill_tx_fee(
Expand All @@ -252,12 +249,3 @@ impl Cleaner {
self.wallet.sign_tx_skeleton(tx_skeleton)
}
}

fn to_input_cell_info(cell_info: CellInfo) -> InputCellInfo {
InputCellInfo {
input: CellInput::new_builder()
.previous_output(cell_info.out_point.clone())
.build(),
cell: cell_info,
}
}
14 changes: 2 additions & 12 deletions crates/block-producer/src/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use gw_types::bytes::Bytes;
use gw_types::core::ScriptHashType;
use gw_types::offchain::{CellInfo, InputCellInfo};
use gw_types::packed::{
CellDep, CellInput, CellOutput, CustodianLockArgs, Script, UnlockCustodianViaRevertWitness,
WitnessArgs,
CellDep, CellOutput, CustodianLockArgs, Script, UnlockCustodianViaRevertWitness, WitnessArgs,
};
use gw_types::prelude::*;
use gw_utils::RollupContext;
Expand Down Expand Up @@ -56,16 +55,7 @@ pub fn revert(
output_builder.lock(deposit_lock.clone()).build()
};

let custodian_input = {
let input = CellInput::new_builder()
.previous_output(revert_custodian.out_point.clone())
.build();

InputCellInfo {
input,
cell: revert_custodian.clone(),
}
};
let custodian_input = InputCellInfo::from(revert_custodian.clone());

let unlock_custodian_witness = UnlockCustodianViaRevertWitness::new_builder()
.deposit_lock_hash(deposit_lock.hash().pack())
Expand Down
15 changes: 0 additions & 15 deletions crates/block-producer/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,21 +1067,6 @@ fn check_locks(
));
}

// check wallet lock
let opt_wallet_config = block_producer_config.wallet_config.as_ref();
if let Some(block_producer_wallet_lock) = opt_wallet_config.map(|c| &c.lock) {
if zeros == block_producer_wallet_lock.code_hash {
bail!("[block_producer.wallet.lock.code_hash] shouldn't be zero");
}

let challenger_rewards_receiver_lock = &block_producer_config
.challenger_config
.rewards_receiver_lock;
if block_producer_wallet_lock == challenger_rewards_receiver_lock {
bail!("[block_producer.challenger.rewards_receiver_lock] and [block_producer.wallet.lock] have the same address, which is not recommended");
}
}

Ok(())
}

Expand Down
11 changes: 2 additions & 9 deletions crates/block-producer/src/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use gw_types::offchain::CompatibleFinalizedTimepoint;
use gw_types::{
core::ScriptHashType,
offchain::{CellInfo, InputCellInfo},
packed::{CellDep, CellInput, CellOutput, L2Block, Script, StakeLockArgs, StakeLockArgsReader},
packed::{CellDep, CellOutput, L2Block, Script, StakeLockArgs, StakeLockArgsReader},
prelude::*,
};
use gw_utils::local_cells::{
Expand Down Expand Up @@ -81,16 +81,9 @@ pub async fn generate(
.lock(lock)
.build();

let input_unlocked_stake = InputCellInfo {
input: CellInput::new_builder()
.previous_output(unlocked_stake.out_point.clone())
.build(),
cell: unlocked_stake,
};

let generated_stake = GeneratedStake {
deps: vec![stake_lock_dep.into()],
inputs: vec![input_unlocked_stake],
inputs: vec![unlocked_stake.into()],
output: stake_cell,
output_data: Bytes::new(),
};
Expand Down
Loading

0 comments on commit 494b31e

Please sign in to comment.