Skip to content

Commit

Permalink
feat: verify withdrawl request that has enough finalized custodian cells
Browse files Browse the repository at this point in the history
  • Loading branch information
magicalne committed Oct 25, 2021
1 parent 1714f6d commit cbc5575
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
20 changes: 20 additions & 0 deletions crates/mem-pool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use crate::{
mem_block::MemBlock,
traits::MemPoolProvider,
types::EntryList,
withdrawal::Generator as WithdrawalGenerator,
};

pub enum MemBlockDBMode {
Expand Down Expand Up @@ -326,6 +327,25 @@ impl MemPool {
self.generator
.check_withdrawal_request_signature(&state, withdrawal_request)?;

// verify finalized custodian
let finalized_custodians = {
// query withdrawals from ckb-indexer
let last_finalized_block_number = self
.generator
.rollup_context()
.last_finalized_block_number(self.current_tip.1);
let task = self.provider.query_available_custodians(
vec![withdrawal_request.clone()],
last_finalized_block_number,
self.generator.rollup_context().to_owned(),
);
smol::block_on(task)?
};
let avaliable_custodians = AvailableCustodians::from(&finalized_custodians);
let withdrawal_generator =
WithdrawalGenerator::new(self.generator.rollup_context(), avaliable_custodians);
withdrawal_generator.verify_remained_amount(withdrawal_request)?;

// withdrawal basic verification
let asset_script =
db.get_asset_script(&withdrawal_request.raw().sudt_script_hash().unpack())?;
Expand Down
10 changes: 8 additions & 2 deletions crates/mem-pool/src/withdrawal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ impl<'a> Generator<'a> {
)
.map_err(|min_capacity| anyhow!("{} minimal capacity for {}", min_capacity, req))?;

self.verify_remained_amount(req).map(|_| output)
}

pub fn verify_remained_amount(&self, req: &WithdrawalRequest) -> Result<()> {
// Verify remaind sudt
let mut ckb_custodian = self.ckb_custodian.clone();
let sudt_type_hash: [u8; 32] = req.raw().sudt_script_hash().unpack();
let req_sudt: u128 = req.raw().amount().unpack();
if 0 != req_sudt {
let sudt_custodian = match self.sudt_custodians.get(&sudt_type_hash) {
Some(custodian) => custodian,
Expand Down Expand Up @@ -139,9 +145,9 @@ impl<'a> Generator<'a> {
// Verify remaind ckb
let req_ckb = req.raw().capacity().unpack() as u128;
match ckb_custodian.balance.checked_sub(req_ckb) {
Some(_) => Ok(output),
Some(_) => Ok(()),
// Consume all remaind ckb
None if req_ckb == ckb_custodian.capacity => Ok(output),
None if req_ckb == ckb_custodian.capacity => Ok(()),
// No able to cover withdrawal cell and ckb custodian change
None => Err(anyhow!(
"no enough finalized custodian capacity, custodian ckb: {}, required ckb: {}",
Expand Down

0 comments on commit cbc5575

Please sign in to comment.