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

Egoistic funding #397

Merged
merged 3 commits into from
Mar 12, 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
18 changes: 9 additions & 9 deletions MAINTAINERS.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Maintainers

## Active Maintainers
| Name | Github | [Discord][_chat_url] |
|-------------------|-----------|----------------|
| Hendrik Amler | [@tinnendo](https://github.com/tinnendo) | hendrik#5345 |
| Jan Bormet | [@janbormet](https://github.com/janbormet) | _.pants |
| Philipp-Florens Lehwalder | [@cryptphil](https://github.com/cryptphil) | cryptphil |
| Steffen Rattay | [@rmbrt](https://github.com/rmbrt) | rmbrt |
| Ilja von Hoessle | [@iljabvh](https://github.com/iljabvh) | iljabvh |
| Name | Github | [Discord][_chat_url] |
|-------------------|----------------------------------------------------|----------------|
sophia1ch marked this conversation as resolved.
Show resolved Hide resolved
| Hendrik Amler | [@tinnendo](https://github.com/tinnendo) | hendrik#5345 |
| Jan Bormet | [@janbormet](https://github.com/janbormet) | _.pants |
| Ilja von Hoessle | [@iljabvh](https://github.com/iljabvh) | iljabvh |
| Sophia Koehler | [@sophia1ch](https://github.com/sophia1ch) | sophia#3072 |
| Philipp-Florens Lehwalder | [@cryptphil](https://github.com/cryptphil) | cryptphil |
| Steffen Rattay | [@rmbrt](https://github.com/rmbrt) | rmbrt |
| Minh Huy Tran | [@NhoxxKienn](https://github.com/NhoxxKienn) | NhoxxKienn |
| Jens Winkle | [@DragonDev1906](https://github.com/DragonDev1906) | jens#4601 |
| Minh Huy Tran | [@NhoxxKienn](https://github.com/NhoxxKienn) | NhoxxKienn |
| Sophia Koehler | [@sophia1ch](https://githun.com/sophia1ch) | sophia#3072 |

## Emeritus Maintainers

Expand Down
52 changes: 44 additions & 8 deletions channel/multi/funder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,30 @@ import (
)

// Funder is a multi-ledger funder.
cryptphil marked this conversation as resolved.
Show resolved Hide resolved
// funders is a map of LedgerIDs corresponding to a funder on some chain.
// egoisticChains is a map of LedgerIDs corresponding to a boolean indicating whether the chain should be funded last.
type Funder struct {
funders map[LedgerIDMapKey]channel.Funder
funders map[LedgerIDMapKey]channel.Funder
egoisticChains map[LedgerIDMapKey]bool
}

// NewFunder creates a new funder.
func NewFunder() *Funder {
return &Funder{
funders: make(map[LedgerIDMapKey]channel.Funder),
funders: make(map[LedgerIDMapKey]channel.Funder),
egoisticChains: make(map[LedgerIDMapKey]bool),
}
}

// RegisterFunder registers a funder for a given ledger.
func (f *Funder) RegisterFunder(l LedgerID, lf channel.Funder) {
f.funders[l.MapKey()] = lf
f.egoisticChains[l.MapKey()] = false
}

// SetEgoisticChain sets the egoistic chain flag for a given ledger.
func (f *Funder) SetEgoisticChain(l LedgerID, egoistic bool) {
f.egoisticChains[l.MapKey()] = egoistic
}

// Fund funds a multi-ledger channel. It dispatches funding calls to all
Expand All @@ -53,21 +63,48 @@ func (f *Funder) Fund(ctx context.Context, request channel.FundingReq) error {
return err
}

var egoisticLedgers []LedgerID
var nonEgoisticLedgers []LedgerID

for _, l := range ledgers {
if f.egoisticChains[l.MapKey()] {
egoisticLedgers = append(egoisticLedgers, l)
} else {
nonEgoisticLedgers = append(nonEgoisticLedgers, l)
}
}

// First fund with Funders that are not egoistic.
cryptphil marked this conversation as resolved.
Show resolved Hide resolved
err = fundLedgers(ctx, request, nonEgoisticLedgers, f.funders)
if err != nil {
return err
}

// Then fund with egoistic Funders.
err = fundLedgers(ctx, request, egoisticLedgers, f.funders)
if err != nil {
return err
}

return nil
}

func fundLedgers(ctx context.Context, request channel.FundingReq, ledgers []LedgerID, funders map[LedgerIDMapKey]channel.Funder) error {
n := len(ledgers)
errs := make(chan error, n)
for _, l := range ledgers {
go func(l LedgerID) {
for _, le := range ledgers {
go func(le LedgerID) {
errs <- func() error {
id := l.MapKey()
lf, ok := f.funders[id]
id := le.MapKey()
lf, ok := funders[id]
if !ok {
return fmt.Errorf("Funder not found for ledger %v", id)
}

err := lf.Fund(ctx, request)
return err
}()
}(l)
}(le)
}

for i := 0; i < n; i++ {
Expand All @@ -76,6 +113,5 @@ func (f *Funder) Fund(ctx context.Context, request channel.FundingReq) error {
return err
}
}

return nil
}
18 changes: 18 additions & 0 deletions client/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ func (r *ProposalResponder) Accept(ctx context.Context, acc ChannelProposalAccep
return r.client.handleChannelProposalAcc(ctx, r.peer, r.req, acc)
}

// SetEgoisticChain sets the egoistic chain flag for a given ledger.
func (r *ProposalResponder) SetEgoisticChain(egoistic multi.LedgerID) {
cryptphil marked this conversation as resolved.
Show resolved Hide resolved
mf, ok := r.client.funder.(*multi.Funder)
if !ok {
log.Panic("unexpected type for funder")
}
mf.SetEgoisticChain(egoistic, true)
}

// RemoveEgoisticChain removes the egoistic chain flag for a given ledger.
func (r *ProposalResponder) RemoveEgoisticChain(egoistic multi.LedgerID) {
mf, ok := r.client.funder.(*multi.Funder)
if !ok {
log.Panic("unexpected type for funder")
}
mf.SetEgoisticChain(egoistic, false)
}

// Reject lets the user signal that they reject the channel proposal.
// Returns whether the rejection message was successfully sent. Panics if the
// proposal was already accepted or rejected.
Expand Down
Loading