Skip to content

Commit ee8b6b5

Browse files
committed
feat(funder):
Add egoisticChains to funder to prioritize funding of assets. feat(proposal): Add SetEgoisticChain and RemoveEgoisticChain to change egoistic funder map, when it is known which assets need to be funded first. Signed-off-by: sophia1ch <sophia.koehler@outlook.com>
1 parent da863c8 commit ee8b6b5

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

channel/multi/funder.go

+52-8
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,26 @@ import (
2525
// Funder is a multi-ledger funder.
2626
type Funder struct {
2727
funders map[LedgerIDMapKey]channel.Funder
28+
egoisticChains map[LedgerIDMapKey]bool
2829
}
2930

3031
// NewFunder creates a new funder.
3132
func NewFunder() *Funder {
3233
return &Funder{
3334
funders: make(map[LedgerIDMapKey]channel.Funder),
35+
egoisticChains: make(map[LedgerIDMapKey]bool),
3436
}
3537
}
3638

3739
// RegisterFunder registers a funder for a given ledger.
3840
func (f *Funder) RegisterFunder(l LedgerID, lf channel.Funder) {
3941
f.funders[l.MapKey()] = lf
42+
f.egoisticChains[l.MapKey()] = false
43+
}
44+
45+
// SetEgoisticChain sets the egoistic chain flag for a given ledger.
46+
func (f *Funder) SetEgoisticChain(l LedgerID, egoistic bool) {
47+
f.egoisticChains[l.MapKey()] = egoistic
4048
}
4149

4250
// Fund funds a multi-ledger channel. It dispatches funding calls to all
@@ -53,12 +61,25 @@ func (f *Funder) Fund(ctx context.Context, request channel.FundingReq) error {
5361
return err
5462
}
5563

56-
n := len(ledgers)
57-
errs := make(chan error, n)
64+
var egoisticLedgers []LedgerID
65+
var nonEgoisticLedgers []LedgerID
66+
5867
for _, l := range ledgers {
59-
go func(l LedgerID) {
60-
errs <- func() error {
61-
id := l.MapKey()
68+
if f.egoisticChains[l.MapKey()] {
69+
egoisticLedgers = append(egoisticLedgers, l)
70+
} else {
71+
nonEgoisticLedgers = append(nonEgoisticLedgers, l)
72+
}
73+
}
74+
75+
76+
// First fund with Funders that are not egoistic.
77+
nn := len(nonEgoisticLedgers)
78+
errsN := make(chan error, nn)
79+
for _, ln := range nonEgoisticLedgers {
80+
go func(ln LedgerID) {
81+
errsN <- func() error {
82+
id := ln.MapKey()
6283
lf, ok := f.funders[id]
6384
if !ok {
6485
return fmt.Errorf("Funder not found for ledger %v", id)
@@ -67,11 +88,34 @@ func (f *Funder) Fund(ctx context.Context, request channel.FundingReq) error {
6788
err := lf.Fund(ctx, request)
6889
return err
6990
}()
70-
}(l)
91+
}(ln)
92+
}
93+
for i := 0; i < nn; i++ {
94+
err := <-errsN
95+
if err != nil {
96+
return err
97+
}
98+
}
99+
// Then fund with egoistic Funders.
100+
ne := len(egoisticLedgers)
101+
errsE := make(chan error, ne)
102+
for _, le := range egoisticLedgers {
103+
go func(le LedgerID) {
104+
errsE <- func() error {
105+
id := le.MapKey()
106+
lf, ok := f.funders[id]
107+
if !ok {
108+
return fmt.Errorf("Funder not found for ledger %v", id)
109+
}
110+
111+
err := lf.Fund(ctx, request)
112+
return err
113+
}()
114+
}(le)
71115
}
72116

73-
for i := 0; i < n; i++ {
74-
err := <-errs
117+
for i := 0; i < ne; i++ {
118+
err := <-errsE
75119
if err != nil {
76120
return err
77121
}

client/proposal.go

+12
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@ 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 := r.client.funder.(*multi.Funder)
133+
mf.SetEgoisticChain(egoistic, true)
134+
}
135+
136+
// RemoveEgoisticChain removes the egoistic chain flag for a given ledger.
137+
func (r *ProposalResponder) RemoveEgoisticChain(egoistic multi.LedgerID) {
138+
mf := r.client.funder.(*multi.Funder)
139+
mf.SetEgoisticChain(egoistic, false)
140+
}
141+
130142
// Reject lets the user signal that they reject the channel proposal.
131143
// Returns whether the rejection message was successfully sent. Panics if the
132144
// proposal was already accepted or rejected.

0 commit comments

Comments
 (0)