Skip to content

Commit 3811fdb

Browse files
authored
Merge pull request #397 from perun-network/feat-egoistic-funding
Egoistic funding
2 parents 61ed2dc + 53e92e9 commit 3811fdb

File tree

3 files changed

+71
-17
lines changed

3 files changed

+71
-17
lines changed

MAINTAINERS.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# Maintainers
22

33
## Active Maintainers
4-
| Name | Github | [Discord][_chat_url] |
5-
|-------------------|-----------|----------------|
6-
| Hendrik Amler | [@tinnendo](https://github.com/tinnendo) | hendrik#5345 |
7-
| Jan Bormet | [@janbormet](https://github.com/janbormet) | _.pants |
8-
| Philipp-Florens Lehwalder | [@cryptphil](https://github.com/cryptphil) | cryptphil |
9-
| Steffen Rattay | [@rmbrt](https://github.com/rmbrt) | rmbrt |
10-
| Ilja von Hoessle | [@iljabvh](https://github.com/iljabvh) | iljabvh |
4+
| Name | Github | [Discord][_chat_url] |
5+
|-------------------|----------------------------------------------------|----------------|
6+
| Hendrik Amler | [@tinnendo](https://github.com/tinnendo) | hendrik#5345 |
7+
| Jan Bormet | [@janbormet](https://github.com/janbormet) | _.pants |
8+
| Ilja von Hoessle | [@iljabvh](https://github.com/iljabvh) | iljabvh |
9+
| Sophia Koehler | [@sophia1ch](https://github.com/sophia1ch) | sophia#3072 |
10+
| Philipp-Florens Lehwalder | [@cryptphil](https://github.com/cryptphil) | cryptphil |
11+
| Steffen Rattay | [@rmbrt](https://github.com/rmbrt) | rmbrt |
12+
| Minh Huy Tran | [@NhoxxKienn](https://github.com/NhoxxKienn) | NhoxxKienn |
1113
| Jens Winkle | [@DragonDev1906](https://github.com/DragonDev1906) | jens#4601 |
12-
| Minh Huy Tran | [@NhoxxKienn](https://github.com/NhoxxKienn) | NhoxxKienn |
13-
| Sophia Koehler | [@sophia1ch](https://githun.com/sophia1ch) | sophia#3072 |
1414

1515
## Emeritus Maintainers
1616

channel/multi/funder.go

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,30 @@ import (
2323
)
2424

2525
// Funder is a multi-ledger funder.
26+
// funders is a map of LedgerIDs corresponding to a funder on some chain.
27+
// egoisticChains is a map of LedgerIDs corresponding to a boolean indicating whether the chain should be funded last.
2628
type Funder struct {
27-
funders map[LedgerIDMapKey]channel.Funder
29+
funders map[LedgerIDMapKey]channel.Funder
30+
egoisticChains map[LedgerIDMapKey]bool
2831
}
2932

3033
// NewFunder creates a new funder.
3134
func NewFunder() *Funder {
3235
return &Funder{
33-
funders: make(map[LedgerIDMapKey]channel.Funder),
36+
funders: make(map[LedgerIDMapKey]channel.Funder),
37+
egoisticChains: make(map[LedgerIDMapKey]bool),
3438
}
3539
}
3640

3741
// RegisterFunder registers a funder for a given ledger.
3842
func (f *Funder) RegisterFunder(l LedgerID, lf channel.Funder) {
3943
f.funders[l.MapKey()] = lf
44+
f.egoisticChains[l.MapKey()] = false
45+
}
46+
47+
// SetEgoisticChain sets the egoistic chain flag for a given ledger.
48+
func (f *Funder) SetEgoisticChain(l LedgerID, egoistic bool) {
49+
f.egoisticChains[l.MapKey()] = egoistic
4050
}
4151

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

66+
var egoisticLedgers []LedgerID
67+
var nonEgoisticLedgers []LedgerID
68+
69+
for _, l := range ledgers {
70+
if f.egoisticChains[l.MapKey()] {
71+
egoisticLedgers = append(egoisticLedgers, l)
72+
} else {
73+
nonEgoisticLedgers = append(nonEgoisticLedgers, l)
74+
}
75+
}
76+
77+
// First fund with Funders that are not egoistic.
78+
err = fundLedgers(ctx, request, nonEgoisticLedgers, f.funders)
79+
if err != nil {
80+
return err
81+
}
82+
83+
// Then fund with egoistic Funders.
84+
err = fundLedgers(ctx, request, egoisticLedgers, f.funders)
85+
if err != nil {
86+
return err
87+
}
88+
89+
return nil
90+
}
91+
92+
func fundLedgers(ctx context.Context, request channel.FundingReq, ledgers []LedgerID, funders map[LedgerIDMapKey]channel.Funder) error {
5693
n := len(ledgers)
5794
errs := make(chan error, n)
58-
for _, l := range ledgers {
59-
go func(l LedgerID) {
95+
for _, le := range ledgers {
96+
go func(le LedgerID) {
6097
errs <- func() error {
61-
id := l.MapKey()
62-
lf, ok := f.funders[id]
98+
id := le.MapKey()
99+
lf, ok := funders[id]
63100
if !ok {
64101
return fmt.Errorf("Funder not found for ledger %v", id)
65102
}
66103

67104
err := lf.Fund(ctx, request)
68105
return err
69106
}()
70-
}(l)
107+
}(le)
71108
}
72109

73110
for i := 0; i < n; i++ {
@@ -76,6 +113,5 @@ func (f *Funder) Fund(ctx context.Context, request channel.FundingReq) error {
76113
return err
77114
}
78115
}
79-
80116
return nil
81117
}

client/proposal.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,24 @@ func (r *ProposalResponder) Accept(ctx context.Context, acc ChannelProposalAccep
127127
return r.client.handleChannelProposalAcc(ctx, r.peer, r.req, acc)
128128
}
129129

130+
// SetEgoisticChain sets the egoistic chain flag for a given ledger.
131+
func (r *ProposalResponder) SetEgoisticChain(egoistic multi.LedgerID) {
132+
mf, ok := r.client.funder.(*multi.Funder)
133+
if !ok {
134+
log.Panic("unexpected type for funder")
135+
}
136+
mf.SetEgoisticChain(egoistic, true)
137+
}
138+
139+
// RemoveEgoisticChain removes the egoistic chain flag for a given ledger.
140+
func (r *ProposalResponder) RemoveEgoisticChain(egoistic multi.LedgerID) {
141+
mf, ok := r.client.funder.(*multi.Funder)
142+
if !ok {
143+
log.Panic("unexpected type for funder")
144+
}
145+
mf.SetEgoisticChain(egoistic, false)
146+
}
147+
130148
// Reject lets the user signal that they reject the channel proposal.
131149
// Returns whether the rejection message was successfully sent. Panics if the
132150
// proposal was already accepted or rejected.

0 commit comments

Comments
 (0)