Skip to content

Commit

Permalink
feat(funder):
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sophia1ch committed Feb 13, 2024
1 parent da863c8 commit eece54c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
60 changes: 52 additions & 8 deletions channel/multi/funder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,26 @@ import (
// Funder is a multi-ledger funder.
type Funder struct {
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),
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,12 +61,25 @@ func (f *Funder) Fund(ctx context.Context, request channel.FundingReq) error {
return err
}

n := len(ledgers)
errs := make(chan error, n)
var egoisticLedgers []LedgerID
var nonEgoisticLedgers []LedgerID

for _, l := range ledgers {
go func(l LedgerID) {
errs <- func() error {
id := l.MapKey()
if f.egoisticChains[l.MapKey()] {
egoisticLedgers = append(egoisticLedgers, l)
} else {
nonEgoisticLedgers = append(nonEgoisticLedgers, l)
}
}


// First fund with Funders that are not egoistic.
nn := len(nonEgoisticLedgers)
errsN := make(chan error, nn)
for _, ln := range nonEgoisticLedgers {
go func(ln LedgerID) {
errsN <- func() error {
id := ln.MapKey()
lf, ok := f.funders[id]
if !ok {
return fmt.Errorf("Funder not found for ledger %v", id)
Expand All @@ -67,11 +88,34 @@ func (f *Funder) Fund(ctx context.Context, request channel.FundingReq) error {
err := lf.Fund(ctx, request)
return err
}()
}(l)
}(ln)
}
for i := 0; i < nn; i++ {
err := <-errsN
if err != nil {
return err
}
}
// Then fund with egoistic Funders.
ne := len(egoisticLedgers)
errsE := make(chan error, ne)
for _, le := range egoisticLedgers {
go func(le LedgerID) {
errsE <- func() error {
id := le.MapKey()
lf, ok := f.funders[id]
if !ok {
return fmt.Errorf("Funder not found for ledger %v", id)
}

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

for i := 0; i < n; i++ {
err := <-errs
for i := 0; i < ne; i++ {
err := <-errsE
if err != nil {
return err
}
Expand Down
12 changes: 12 additions & 0 deletions client/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ 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 := r.client.funder.(*multi.Funder)
mf.SetEgoisticChain(egoistic, true)
}

// RemoveEgoisticChain removes the egoistic chain flag for a given ledger.
func (r *ProposalResponder) RemoveEgoisticChain(egoistic multi.LedgerID) {
mf := r.client.funder.(*multi.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 eece54c

Please sign in to comment.