Skip to content

Commit

Permalink
UDT workflow passed and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Jun 6, 2024
1 parent 561aecd commit d7b8c7c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 71 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ tokio = { version = "1", features = [
"signal",
] }
home = "0.5.9"
#ckb-sdk = "3.1.0"
ckb-sdk = { git = "https://github.com/chenyukang/ckb-sdk-rust.git", branch = "v3.1.0-with-pcn-udt" }
ckb-sdk = "3.1.0"
thiserror = "1.0.58"
anyhow = "1.0.81"
env_logger = "0.11.3"
Expand Down
41 changes: 6 additions & 35 deletions src/ckb/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,6 @@ where
.get_commitment_point(commitment_number + 1),
next_local_nonce: state.get_local_musig2_pubnonce(),
};
warn!(
"anan set accept_channel funding script: {:?}",
funding_type_script
);

let command = PCNMessageWithPeerId {
peer_id,
Expand Down Expand Up @@ -1154,11 +1150,6 @@ impl ChannelActorState {
commitment_number, remote_commitment_number
);

warn!(
"anan new_inbound_channel funding_type_script: {:?}",
funding_type_script
);

Self {
state: ChannelState::NegotiatingFunding(NegotiatingFundingFlags::THEIR_INIT_SENT),
peer_id,
Expand Down Expand Up @@ -1377,19 +1368,11 @@ impl ChannelActorState {
}

pub fn get_funding_request(&self, fee_rate: u64) -> FundingRequest {
let udt_info = if let Some(type_script) = &self.funding_type_script {
Some(FundingUdtInfo {
type_script: type_script.clone(),
// FIXME(yukang): this is hardcoded to 61 * 10^8 * 2 shannons
local_ckb_amount: 122000000000,
remote_ckb_amount: 122000000000,
})
} else {
None
};
warn!("anan get_funding_request: {:?}", udt_info);
FundingRequest {
udt_info,
udt_info: self
.funding_type_script
.clone()
.map(FundingUdtInfo::new_with_script),
script: self.get_funding_lock_script(),
local_amount: self.to_local_amount as u64,
local_fee_rate: fee_rate,
Expand Down Expand Up @@ -2001,10 +1984,6 @@ impl ChannelActorState {
self.to_remote_amount = accept_channel.funding_amount;
self.remote_nonce = Some(accept_channel.next_local_nonce.clone());
self.funding_type_script = accept_channel.funding_type_script.clone();
warn!(
"anan handle_accept_channel funding type script: {:?}",
&self.funding_type_script
);

let remote_pubkeys = (&accept_channel).into();
self.remote_channel_parameters = Some(ChannelParametersOneParty {
Expand Down Expand Up @@ -2689,18 +2668,10 @@ impl ChannelActorState {
);

if let Some(type_script) = &self.funding_type_script {
warn!(
"anan UDT local_amount: {}, remote_amount: {}",
debug!(
"UDT local_amount: {}, remote_amount: {}",
self.to_local_amount, self.to_remote_amount
);
// FIXME(yukang): how to handle the CKB amount here,
// after substracting tx fee, we should return the lefted CKB to the holder
// let minimal_capacity = 61_0000_0000 as u64;
// let holder_output = CellOutput::new_builder()
// .capacity(minimal_capacity.pack())
// .type_(Some(type_script.clone()).pack())
// .lock(local_shutdown_script)
// .build();
let holder_output_data = self.to_local_amount.to_le_bytes().pack();
let dummy_output = CellOutput::new_builder()
.lock(local_shutdown_script.clone())
Expand Down
20 changes: 2 additions & 18 deletions src/ckb/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ where
}
}
NetworkActorCommand::AcceptChannel(accept_channel, reply) => {
warn!("anan acceptchannel: {:?}", accept_channel);
let result = state
.create_inbound_channel(accept_channel, self.store.clone())
.await
Expand All @@ -374,7 +373,6 @@ where
"Updating channel funding for channel {:?}, current tx: {:?}",
&channel_id, old_tx
);
warn!("anan fundingrequest: {:?}", request);
let mut tx = FundingTx::new();
tx.update_for_self(old_tx)?;
let tx = match call_t!(
Expand Down Expand Up @@ -608,11 +606,6 @@ impl NetworkActorState {
return Ok((channel.clone(), temp_channel_id, id));
}

warn!(
"anan create_inbound_channel: accept_funding_type_script: {:?}, open_funding_type: {:?}",
funding_type_script, open_channel.funding_type_script
);

match (
open_channel.funding_type_script.as_ref(),
funding_type_script.as_ref(),
Expand Down Expand Up @@ -1046,23 +1039,14 @@ where
// TODO: Here we implies the one who receives AcceptChannel message
// will send TxUpdate message first.
dbg!(&script);
let udt_info = if let Some(funding_script) = funding_script {
Some(FundingUdtInfo {
type_script: funding_script,
// FIXME(yukang): this is hardcoded to 61 * 10^8 * 2 shannons
local_ckb_amount: 122000000000,
remote_ckb_amount: 122000000000,
})
} else {
None
};
myself
.send_message(NetworkActorMessage::new_command(
NetworkActorCommand::UpdateChannelFunding(
new,
Default::default(),
FundingRequest {
udt_info,
udt_info: funding_script
.map(FundingUdtInfo::new_with_script),
script,
local_amount: local as u64,
local_fee_rate: 0,
Expand Down
31 changes: 16 additions & 15 deletions src/ckb_chain/funding/funding_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ pub struct FundingUdtInfo {
pub remote_ckb_amount: u64,
}

impl FundingUdtInfo {
pub fn new_with_script(type_script: packed::Script) -> Self {
// FIXME(yukang): make sure the udt_amount is enough for split into at least 2 cells
let two_cells_amount: u64 = (142 + 10) * (10 as u64).pow(8);
Self {
type_script,
local_ckb_amount: two_cells_amount,
remote_ckb_amount: two_cells_amount,
}
}
}

#[serde_as]
#[derive(Clone, Debug, Default, Deserialize)]
pub struct FundingRequest {
Expand Down Expand Up @@ -126,9 +138,7 @@ impl TxBuilder for FundingTxBuilder {
}
}

warn!("anan build_base now : {:?}", self.request.udt_info);
if let Some(ref udt_info) = self.request.udt_info {
let local_ckb_amount = udt_info.local_ckb_amount;
let udt_amount = self.request.local_amount as u128;

if udt_amount > 0 {
Expand All @@ -146,20 +156,11 @@ impl TxBuilder for FundingTxBuilder {
let (owner_cells, _) = cell_collector.collect_live_cells(&owner_query, true)?;
let mut found_udt_cell = false;
for cell in owner_cells.iter() {
let cell_capacity: u64 = cell.output.capacity().unpack();
let mut amount_bytes = [0u8; 16];
amount_bytes.copy_from_slice(&cell.output_data.as_ref()[0..16]);
let cell_udt_amount = u128::from_le_bytes(amount_bytes);
//FIXME(yukang): we may need to revise the check here
warn!(
"anan compare : {:?} >= {:?} && {:?} >= {:?} result: {:?}",
cell_capacity,
local_ckb_amount,
cell_udt_amount,
udt_amount,
cell_capacity >= local_ckb_amount && cell_udt_amount >= udt_amount
);
if cell_capacity >= local_ckb_amount && cell_udt_amount >= udt_amount {
if cell_udt_amount >= udt_amount {
inputs.push(CellInput::new(cell.out_point.clone(), 0));
if cell_udt_amount > udt_amount {
let change_output_data: Bytes =
Expand All @@ -183,7 +184,7 @@ impl TxBuilder for FundingTxBuilder {
outputs.push(change_output);
outputs_data.push(change_output_data);
}
warn!("anan find proper UDT owner cell: {:?}", cell);
warn!("find proper UDT owner cell: {:?}", cell);
found_udt_cell = true;
break;
}
Expand Down Expand Up @@ -222,7 +223,7 @@ impl TxBuilder for FundingTxBuilder {
.set_outputs(outputs)
.set_outputs_data(outputs_data)
.set_cell_deps(cell_deps.into_iter().collect());
warn!("anan tx_builder: {:?}", tx_builder);
warn!("tx_builder: {:?}", tx_builder);
let tx = tx_builder.build();
Ok(tx)
}
Expand Down Expand Up @@ -337,7 +338,7 @@ impl FundingTxBuilder {

let mut funding_tx = self.funding_tx;
let tx_builder = tx.as_advanced_builder();
warn!("anan final tx_builder: {:?}", tx_builder);
warn!("final tx_builder: {:?}", tx_builder);
funding_tx.update_for_self(tx)?;
Ok(funding_tx)
}
Expand Down

0 comments on commit d7b8c7c

Please sign in to comment.