# Fiber Auto-Cleaning Preimage Issues
## Metadata
**Status**:: #x
**Zettel**:: #zettel/fleeting
**Created**:: [[2025-06-16]]
## Synopsis
目前 Fiber 在 Channel Actor 中会在 RemoveTlcFulfill 后自动删除 preimage。这个自动删除对支付者、收款者和中间节点都会发生,并且时机非常的早。在支付者节点中,它甚至比标记 Payment Session Status 为 Success 还要早。
CCH 跨链在收 BTC 的时候,是需要 Hub 节点在支付 Fiber 收款者时跟踪 Payment 状态。当 Payment 变为 Success 时取出 preimage 去清算 BTC 侧的待收款。而这时 preimage 已经被删除了。
现在用了很 Tricky 的[临时方案](https://github.com/nervosnetwork/fiber/blob/42c2bda3573ad382cd6353a738506b3722441962/crates/fiber-lib/src/store/store_impl/mod.rs#L630-L635),就是在 `remove_preimage` 中加上判断,如果有对应的 PaymentSession 并且状态是 Inflight 或者 Success 的时候,就发一个 `PaymentUpdatedPayload::Success { preiamge }` 的通知。
而如果付款者不自动清理 preiamge,会导致有两个依赖此行为的测试失败:
- `fiber::tests::channel::test_network_send_payment_normal_keysend_workflow`
- `fiber::tests::channel::test_send_payment_with_3_nodes`
这两个测试在最后都会断言付款者节点中 preimage 已经被自动删除了。
由于不清楚自动删除 preimage 这个功能的上下文,不太好判断要如何改。以下提供一些备选方案供讨论。
1. 保留 <https://github.com/nervosnetwork/fiber/pull/615#discussion_r2146379461> 中的修改,也就是付款者不自动删除 preimage。如果只是那两个测试依赖自动删除这个行为的话,去掉测试中的断言,或者改成断言收款者的 preimage 已经自动删除。
2. 延后 preimage 的删除。对于付款者至少要在 PaymentSession 更新为 Success 之后才删除。但是其实对于 CCH 来说,必须要等到 BTC 端待收款清算成功之后才能够删除,这个已经提了 [issue](https://github.com/nervosnetwork/fiber/issues/741)。具体实现的话需要对 PaymentSession 进行标记,能够判断是 CCH 的支付。在 CCH 整个完成之后才可以选择自动删除 preimage。
> [!bot] Translate to English
> Currently, Fiber automatically removes the preimage after processing `RemoveTlcFulfill` in the Channel Actor. This automatic deletion occurs for the payer, payee, and intermediate nodes, and it happens very early. In the payer node, it even occurs before marking the Payment Session Status as Success.
>
> For CCH cross-chain transactions when receiving BTC, the Hub node needs to track the Payment status while paying the Fiber payee. When the Payment changes to Success, it retrieves the preimage to settle the pending receipt on the BTC side. However, by this time, the preimage has already been deleted.
>
> A [temporary workaround](https://github.com/nervosnetwork/fiber/blob/42c2bda3573ad382cd6353a738506b3722441962/crates/fiber-lib/src/store/store_impl/mod.rs#L630-L635) is currently in place, which involves adding a check in `remove_preimage`. If there is a corresponding PaymentSession and its status is Inflight or Success, a `PaymentUpdatedPayload::Success { preimage }` notification is sent.
>
> If the payer does not automatically clean up the preimage, it causes two tests that rely on this behavior to fail:
>
> - `fiber::tests::channel::test_network_send_payment_normal_keysend_workflow`
> - `fiber::tests::channel::test_send_payment_with_3_nodes`
>
> Both tests assert at the end that the preimage has been automatically deleted in the payer node.
>
> Since the context of the automatic preimage deletion feature is unclear, it’s difficult to determine the best way to modify it. Below are some alternative solutions for discussion:
>
> 1. Retain the changes in <https://github.com/nervosnetwork/fiber/pull/615#discussion_r2146379461>, meaning the payer does not automatically delete the preimage. If only those two tests rely on this behavior, remove the assertions in the tests or modify them to assert that the payee’s preimage has been automatically deleted.
> 2. Delay the deletion of the preimage. For the payer, deletion should occur only after the PaymentSession is updated to Success. However, for CCH, the preimage must not be deleted until the pending receipt on the BTC side is successfully settled, as noted in this [issue](https://github.com/nervosnetwork/fiber/issues/741). Implementation-wise, this would require marking the PaymentSession to identify CCH payments. The preimage would only be automatically deleted after the entire CCH process is completed.