Skip to content

Commit

Permalink
Merge pull request #505 from quake/quake/fix-force-close-amount-bug
Browse files Browse the repository at this point in the history
fix: resolve force close amount bug
  • Loading branch information
quake authored Feb 5, 2025
2 parents f624685 + ea4609a commit 412a7e8
Show file tree
Hide file tree
Showing 18 changed files with 342 additions and 142 deletions.
22 changes: 14 additions & 8 deletions src/fiber/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6284,7 +6284,7 @@ impl ChannelActorState {
}

fn build_init_commitment_tx_signature(&self) -> Result<PartialSignature, SigningError> {
let sign_ctx = self.get_sign_context(true);
let sign_ctx = self.get_sign_context(false);
let x_only_aggregated_pubkey = sign_ctx.common_ctx.x_only_aggregated_pubkey();
let ([to_local_output, to_remote_output], [to_local_output_data, to_remote_output_data]) =
self.build_settlement_transaction_outputs(false);
Expand Down Expand Up @@ -6314,7 +6314,7 @@ impl ChannelActorState {
&self,
signature: PartialSignature,
) -> Result<SettlementData, ProcessingChannelError> {
let sign_ctx = self.get_sign_context(false);
let sign_ctx = self.get_sign_context(true);
let x_only_aggregated_pubkey = sign_ctx.common_ctx.x_only_aggregated_pubkey();

let ([to_local_output, to_remote_output], [to_local_output_data, to_remote_output_data]) =
Expand Down Expand Up @@ -6803,33 +6803,39 @@ impl ChannelActorState {
let mut received_fullfilled = 0;
for info in pending_tlcs {
if info.is_offered() {
offered_pending += info.amount;
if (info.outbound_status() == OutboundTlcStatus::RemoveWaitAck
|| info.outbound_status() == OutboundTlcStatus::RemoveAckConfirmed)
|| info.outbound_status() == OutboundTlcStatus::RemoveAckConfirmed
|| (info.outbound_status() == OutboundTlcStatus::RemoteRemoved && !for_remote))
&& info
.removed_reason
.as_ref()
.map(|r| matches!(r, RemoveTlcReason::RemoveTlcFulfill(_)))
.unwrap_or_default()
{
offered_fullfilled += info.amount;
} else {
offered_pending += info.amount;
}
} else {
received_pending += info.amount;
if info.inbound_status() == InboundTlcStatus::RemoveAckConfirmed
if (info.inbound_status() == InboundTlcStatus::RemoveAckConfirmed
|| (info.inbound_status() == InboundTlcStatus::LocalRemoved && for_remote))
&& info
.removed_reason
.as_ref()
.map(|r| matches!(r, RemoveTlcReason::RemoveTlcFulfill(_)))
.unwrap_or_default()
{
received_fullfilled += info.amount;
} else {
received_pending += info.amount;
}
}
}

let to_local_value = self.to_local_amount + received_fullfilled - offered_pending;
let to_remote_value = self.to_remote_amount + offered_fullfilled - received_pending;
let to_local_value =
self.to_local_amount + received_fullfilled - offered_pending - offered_fullfilled;
let to_remote_value =
self.to_remote_amount + offered_fullfilled - received_pending - received_fullfilled;

let commitment_tx_fee =
calculate_commitment_tx_fee(self.commitment_fee_rate, &self.funding_udt_type_script);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
meta {
name: Node1 open a channel to Node2
name: Node1 open a channel to Node2 with amount 99 ckb
type: http
seq: 2
}
Expand All @@ -23,9 +23,9 @@ body:json {
"params": [
{
"peer_id": "{{NODE2_PEERID}}",
"funding_amount": "0xba43b7400",
"funding_amount": "0x24e160300",
"shutdown_script": {
"code_hash": "0x2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b",
"code_hash": "0x2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c",
"hash_type": "data",
"args": "0x42"
}
Expand All @@ -41,4 +41,5 @@ assert {

script:post-response {
await new Promise(r => setTimeout(r, 2000));
bru.setVar("N1N2_TEMP_CHANNEL_ID", res.body.result.temporary_channel_id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
meta {
name: node2 accept channel
type: http
seq: 3
}

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": "0x1004ccb00",
"shutdown_script": {
"code_hash": "0x2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d",
"hash_type": "data",
"args": "0x42"
}
}
]
}
}

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);
bru.setVar("CHANNEL_ID", res.body.result.channel_id);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ body:json {
"method": "new_invoice",
"params": [
{
"amount": "0x5d21dba00",
"amount": "0x2dc6c0",
"currency": "Fibd",
"description": "test invoice generated by node2",
"expiry": "0xe10",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ body:json {
"method": "new_invoice",
"params": [
{
"amount": "0x2e90edd00",
"amount": "0x16e360",
"currency": "Fibd",
"description": "test invoice generated by node1",
"expiry": "0xe10",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
meta {
name: check balance, 0x24e160300 - 0x2dc6c0 + 0x16e360 - 0x1c7 (fee) == 0x24dff1dd9
type: http
seq: 15
}

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": "get_cells_capacity",
"params": [
{
"script": {
"code_hash": "0x2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c",
"hash_type": "data",
"args": "0x42"
},
"script_type": "lock"
}
]
}
}

assert {
res.body.result.capacity: eq "0x24dff1dd9"
}

script:post-response {
console.log("result: ", res.body);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
meta {
name: check balance, 0x1004ccb00 + 0x2dc6c0 - 0x16e360 - 0x1c7 (fee) == 0x10063ac99
type: http
seq: 16
}

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": "get_cells_capacity",
"params": [
{
"script": {
"code_hash": "0x2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d",
"hash_type": "data",
"args": "0x42"
},
"script_type": "lock"
}
]
}
}

assert {
res.body.result.capacity: eq "0x10063ac99"
}

script:post-response {
console.log("result: ", res.body);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
meta {
name: Node1 open a channel to Node2
name: Node1 open a channel to Node2 with amount 99 ckb
type: http
seq: 2
}
Expand All @@ -23,7 +23,12 @@ body:json {
"params": [
{
"peer_id": "{{NODE2_PEERID}}",
"funding_amount": "0xba43b7400"
"funding_amount": "0x24e160300",
"shutdown_script": {
"code_hash": "0x2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e",
"hash_type": "data",
"args": "0x42"
}
}
]
}
Expand All @@ -36,4 +41,5 @@ assert {

script:post-response {
await new Promise(r => setTimeout(r, 2000));
bru.setVar("N1N2_TEMP_CHANNEL_ID", res.body.result.temporary_channel_id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
meta {
name: node2 accept channel
type: http
seq: 3
}

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": "0x1004ccb00",
"shutdown_script": {
"code_hash": "0x2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f",
"hash_type": "data",
"args": "0x42"
}
}
]
}
}

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);
bru.setVar("CHANNEL_ID", res.body.result.channel_id);
}

This file was deleted.

Loading

0 comments on commit 412a7e8

Please sign in to comment.