Skip to content

Commit

Permalink
Merge pull request #397 from perun-network/feat-egoistic-funding
Browse files Browse the repository at this point in the history
Egoistic funding
  • Loading branch information
NhoxxKienn authored Mar 12, 2024
2 parents 61ed2dc + 53e92e9 commit 3811fdb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 17 deletions.
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] |
|-------------------|----------------------------------------------------|----------------|
| 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.
// 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.
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) {
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

0 comments on commit 3811fdb

Please sign in to comment.