Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the ckb amount check issue for UDT #164

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/fiber/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4471,10 +4471,18 @@ impl ChannelActorState {
))?;

if first_output.lock() != self.get_funding_lock_script() {
error!("Checking if transaction final failed as tx's first output's script is not funding lock: tx: {:?}, first output lock script: {:?}, funding lock script: {:?}",
&tx, first_output.lock(), self.get_funding_lock_script());
// TODO: return an error here. We panic because we want to move fast.
panic!("Invalid funding transation")
return Err(ProcessingChannelError::InvalidState(
"Invalid funding transation lock script".to_string(),
));
}

let current_capacity: u64 = first_output.capacity().unpack();

// make sure both parties have paid the reserved ckb amount
if current_capacity <= self.local_reserved_ckb_amount
|| current_capacity <= self.remote_reserved_ckb_amount
{
return Ok(false);
}

if self.funding_udt_type_script.is_some() {
Expand All @@ -4491,9 +4499,11 @@ impl ChannelActorState {
"udt_amount: {}, to_remote_amount: {}, to_local_amount: {}",
udt_amount, self.to_remote_amount, self.to_local_amount
);
return Ok(udt_amount == self.to_remote_amount + self.to_local_amount);
debug!("current_capacity: {}, remote_reserved_ckb_amount: {}, local_reserved_ckb_amount: {}",
current_capacity, self.remote_reserved_ckb_amount, self.local_reserved_ckb_amount);
let is_udt_amount_ok = udt_amount == self.to_remote_amount + self.to_local_amount;
return Ok(is_udt_amount_ok);
} else {
let current_capacity: u64 = first_output.capacity().unpack();
let is_complete = current_capacity
== (self.to_local_amount
+ self.to_remote_amount
Expand Down
48 changes: 48 additions & 0 deletions tests/bruno/e2e/udt/11-node1-node2-open-channel-no-auto-accept.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
meta {
name: Node1 open a channel to Node2, that should not be auto accepted
type: http
seq: 11
}

post {
url: {{NODE1_RPC_URL}}
body: json
auth: none
}

headers {
Content-Type: application/json
Accept: application/json
}


body:json {
{
"id": "42",
"jsonrpc": "2.0",
"method": "open_channel",
"params": [
{
"peer_id": "{{NODE2_PEERID}}",
"funding_amount": "0x200",
"funding_udt_type_script": {
"code_hash": "{{UDT_CODE_HASH}}",
"hash_type": "data1",
"args": "0x32e555f3ff8e135cece1351a6a2971518392c1e30375c1e006ad0ce8eac07947"
}
}
]
}
}

assert {
res.body.error: isUndefined
res.body.result.temporary_channel_id: isDefined
}

script:post-response {
await new Promise(r => setTimeout(r, 1000));
console.log("N1N2 response: ", res.body);
console.log("N1N2 response: ", res.body.result.temporary_channel_id);
bru.setVar("N1N2_TEMP_CHANNEL_ID", res.body.result.temporary_channel_id);
}
40 changes: 40 additions & 0 deletions tests/bruno/e2e/udt/12-node2-accept-channel.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
meta {
name: node2 accept udt channel
type: http
seq: 12
}

post {
url: {{NODE2_RPC_URL}}
body: json
auth: none
}

headers {
Content-Type: application/json
Accept: application/json
}

body:json {
{
"id": "42",
"jsonrpc": "2.0",
"method": "accept_channel",
"params": [
{
"temporary_channel_id": "{{N1N2_TEMP_CHANNEL_ID}}",
"funding_amount": "0x0"
}
]
}
}

assert {
res.body.error: isUndefined
}

script:post-response {
// Sleep for sometime to make sure current operation finishes before next request starts.
await new Promise(r => setTimeout(r, 1000));
console.log("accept channel result: ", res.body);
}
33 changes: 33 additions & 0 deletions tests/bruno/e2e/udt/13-ckb-generate-blocks.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
meta {
name: generate a few epochs
type: http
seq: 13
}

post {
url: {{CKB_RPC_URL}}
body: json
auth: none
}

headers {
Content-Type: application/json
Accept: application/json
}

body:json {
{
"id": 42,
"jsonrpc": "2.0",
"method": "generate_epochs",
"params": ["0x2"]
}
}

assert {
res.status: eq 200
}

script:post-response {
await new Promise(r => setTimeout(r, 5000));
}
43 changes: 43 additions & 0 deletions tests/bruno/e2e/udt/14-node2-list-channel-expect-two-channel.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
meta {
name: get channel list from node2
type: http
seq: 14
}

post {
url: {{NODE2_RPC_URL}}
body: json
auth: none
}

headers {
Content-Type: application/json
Accept: application/json
}

body:json {
{
"id": "42",
"jsonrpc": "2.0",
"method": "list_channels",
"params": [
{
"peer_id": "{{NODE1_PEERID}}"
}
]
}
}

assert {
res.body.error: isUndefined
res.body.result.channels: isDefined
}

script:post-response {
// Sleep for sometime to make sure current operation finishes before next request starts.
await new Promise(r => setTimeout(r, 2000));
console.log("accept channel result: ", res.body.result.channels);
if (res.body.result.channels.length != 2) {
throw new Error("Assertion failed: expect there are 2 channels");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
meta {
name: Node1 send shutdown channel1
type: http
seq: 11
seq: 15
}

post {
Expand Down Expand Up @@ -41,5 +41,5 @@ assert {

script:post-response {
// Sleep for sometime to make sure current operation finishes before next request starts.
await new Promise(r => setTimeout(r, 100));
await new Promise(r => setTimeout(r, 1000));
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
meta {
name: Node2 send shutdown channel1
type: http
seq: 12
seq: 16
}

post {
Expand Down
7 changes: 2 additions & 5 deletions tests/deploy/udt-init/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ fn init_or_send_udt(
)?;

let json_tx = ckb_jsonrpc_types::TransactionView::from(tx_with_groups.get_tx_view().clone());
println!("tx: {}", serde_json::to_string_pretty(&json_tx).unwrap());

if apply {
let tx_hash = CkbRpcClient::new(network_info.url.as_str())
.send_transaction(json_tx.inner, None)
Expand All @@ -143,11 +141,10 @@ fn init_or_send_udt(
fn generate_blocks(num: u64) -> Result<(), Box<dyn StdErr>> {
let network_info = NetworkInfo::devnet();
let rpc_client = CkbRpcClient::new(network_info.url.as_str());
for i in 0..num {
for _i in 0..num {
rpc_client.generate_block()?;
// sleep 200ms
std::thread::sleep(std::time::Duration::from_millis(200));
eprintln!("block generated: {}", i);
}
Ok(())
}
Expand Down Expand Up @@ -208,7 +205,7 @@ fn genrate_nodes_config() {
for udt in UDT_KINDS {
let udt_info = UdtInfo {
name: udt.to_string(),
auto_accept_amount: Some(0),
auto_accept_amount: Some(1000),
script: UdtScript {
code_hash: get_code_hash(udt),
hash_type: "Data1".to_string(),
Expand Down
Loading