-
Notifications
You must be signed in to change notification settings - Fork 14
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
Support fiber multiple hops payment in cross chain transaction #511
base: develop
Are you sure you want to change the base?
Support fiber multiple hops payment in cross chain transaction #511
Conversation
21a1d8c
to
ee59e1f
Compare
…er-multiple-hops-payment-in-cross-chain-transaction
1ed77fd
to
969f732
Compare
#[derive(Clone, Debug)] | ||
pub struct Store { | ||
pub struct GenericStore<IH: InvoiceUpdateHook, PH: PaymentUpdateHook> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no need to make a GenericStore
abstraction of the store here, it violates the principle of composition over inheritance, you can just use a store + hook struct wherever you need a store callback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean that the hooks can be passed to every one that needs them (instead of bundling them with the store)? There are many places in current code base which alters the status of an invoice, for example, https://github.com/contrun/fiber/blob/c53f6dfebba73ae7f9b3b75622abd7e8132eb3b6/src/rpc/invoice.rs#L340-L348 https://github.com/contrun/fiber/blob/c53f6dfebba73ae7f9b3b75622abd7e8132eb3b6/src/fiber/channel.rs#L1058-L1062 https://github.com/contrun/fiber/blob/c53f6dfebba73ae7f9b3b75622abd7e8132eb3b6/src/store/store.rs#L444-L447 , all the code listed above must have a reference to the InvoiceUpdateHook
. So it seems to me define a
Struct StoreWithHook<IH: InvoiceUpdateHook> {
store: Store,
hook: IH,
}
and pass this StoreWithHook
to above code would make life easier. That is essentially GenericStore<IH: InvoiceUpdateHook>
.
The thing is that Hook and Store can't really work work independently. Hook (calling it StoreUpdateHook is better) should be part of Store because the hooks run when store is updated. We can't compose these two inter-dependent component.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keep the original store implementation unchanged:
impl InvoiceStore for Store {
fn insert_invoice(...) {
...
}
}
and add a compsited store implementation:
struct InvoiceStoreWithHook {
store: Store,
invoice_hook: ConcreteInvoiceHook,
}
impl InvoiceStore for InvoiceStoreWithHook {
fn insert_invoice(...) {
self.store.insert_invoice(...);
self.invoice_hook.on_invoice_updated(...);
}
}
no need to introduce GenericStore struct and InvoiceUpdateHook trait
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In https://github.com/contrun/fiber/tree/support-fiber-multiple-hops-payment-in-cross-chain-transaction-pass-hooks, I tried to use this approach (as I understood it). Can you confirm this align well with the approach you described? My code in that branch is very complicated, I don't know if that is the simplest way to accomplish what's on your mind. The complexity of my implementation is due to the following points:
- Hooks has to be initialized in the main function (we need to have a singleton to receive all the updates and push them to the subscribers).
- There are many modules which use
InvoiceStore
, so all of they have to callInvoiceStoreWithHooks::new
, which is a function to initialize aInvoiceStore
with a hook. This means we have to pass the invoice hooks created in the main function down to many modules. - Channel actors use
ChannelActorStateStore
along withInvoiceStore
. So we have to implementChannelActorStateStore
for theInvoiceStoreWithHooks
. - The same thing needs to be done for
NetworkGraphStateStore
(yeah, confusingly, this trait updates payment session), because we need to inject hooks for payments.
I didn't have a complete implementation yet. If my current code is what's on your mind. I can finish injecting payment hooks and removing the GenericStore
.
0a2be2d
to
548259b
Compare
// TLCs in the list applied_add_tlcs wouldn't be processed again. | ||
// For the unsettled active hold invoice TLCs, we should process them indefinitely | ||
// until they expire or are settled. | ||
state.tlc_state.applied_add_tlcs.remove(&add_tlc.tlc_id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
applied_add_tlcs
is introduced to resolve some concurrency issue, I'm not sure is it ok to remove it from here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so. Because we are actually adding and removing the tlc from exactly the same function apply_add_tlc_operation_with_peeled_onion_packet
.
src/fiber/channel.rs
Outdated
.insert_payment_preimage(payment_hash, preimage) | ||
.map_err(|_| { | ||
ProcessingChannelError::InternalError("insert preimage failed".to_string()) | ||
})?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems ProcessingChannelError::FinalIncorrectPaymentHash
does not return to caller. we should return the error at line 1019?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The store interface is a little bit confusing to me. Most of the store traits will never return errors. I don't know under what situation insert_payment_preimage
may return an error (the current code does not return any error https://github.com/contrun/fiber/blob/fed2e63c7ec248d7319449d9bd7f388f3a943224/src/store/store.rs#L479-L488). It was my understanding that failure at this stage should be internal errors related to the store saving (not data validation error like the hashing result of this preimage does not match the hash). Can you recall why the InvoiceStore
has Result
as its return value? I thought all the business logic (data validation etc) should be done by the caller of the store trait (instead of in the store trait).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
never mind, 1019 will return error, I got it wrong.
or you can remove the returning result for insert_payment_preimage
by the way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this to FinalIncorrectPaymentHash
anyway.
…er-multiple-hops-payment-in-cross-chain-transaction
cfd0b18
to
6616ffe
Compare
6616ffe
to
412fe2b
Compare
No description provided.