-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package policy | ||
|
||
import ( | ||
"github.com/statechannels/go-nitro/protocols" | ||
"github.com/statechannels/go-nitro/types" | ||
) | ||
|
||
type AllowListPolicy struct { | ||
allowed map[types.Address]bool | ||
} | ||
|
||
func NewAllowListPolicy(allowed []types.Address) *AllowListPolicy { | ||
allowedMap := make(map[types.Address]bool) | ||
for _, a := range allowed { | ||
allowedMap[a] = true | ||
} | ||
return &AllowListPolicy{allowed: allowedMap} | ||
} | ||
|
||
// ShouldApprove decides to approve o if it is currently unapproved | ||
func (ap *AllowListPolicy) ShouldApprove(o protocols.Objective) bool { | ||
for _, p := range o.GetParticipants() { | ||
if !ap.allowed[p] { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package policy | ||
|
||
import ( | ||
"log/slog" | ||
"math/big" | ||
|
||
"github.com/statechannels/go-nitro/internal/logging" | ||
"github.com/statechannels/go-nitro/protocols" | ||
"github.com/statechannels/go-nitro/protocols/directfund" | ||
"github.com/statechannels/go-nitro/protocols/virtualfund" | ||
"github.com/statechannels/go-nitro/types" | ||
) | ||
|
||
type FairOutcomePolicy struct { | ||
me types.Address | ||
} | ||
|
||
func NewFairOutcomePolicy(me types.Address) *FairOutcomePolicy { | ||
return &FairOutcomePolicy{me: me} | ||
} | ||
|
||
// ShouldApprove decides to approve o if it is currently unapproved | ||
func (fp *FairOutcomePolicy) ShouldApprove(o protocols.Objective) bool { | ||
df, isDf := o.(*directfund.Objective) | ||
if isDf { | ||
for _, e := range df.C.PreFundState().Outcome { | ||
forMe := e.TotalAllocatedFor(types.AddressToDestination(fp.me)) | ||
for _, a := range e.Allocations { | ||
if a.Amount.Cmp(forMe) != 0 { | ||
slog.Warn("FairOutcomePolicy: rejecting directfund objective with unequal allocations", "objective", o.Id(), "allocations", e.Allocations, logging.WithObjectiveIdAttribute(o.Id())) | ||
return false | ||
} | ||
} | ||
} | ||
} | ||
vf, isVf := o.(*virtualfund.Objective) | ||
if isVf { | ||
for _, e := range vf.V.PreFundState().Outcome { | ||
|
||
total := e.TotalAllocated() | ||
for i, a := range e.Allocations { | ||
if i == 0 && a.Amount.Cmp(total) != 0 { | ||
slog.Warn("FairOutcomePolicy: rejecting virtualfund objective, expected payer to start with full amount", "allocations", e.Allocations, logging.WithObjectiveIdAttribute(o.Id())) | ||
return false | ||
} else if i > 0 && a.Amount.Cmp(big.NewInt(0)) != 0 { | ||
slog.Warn("FairOutcomePolicy: rejecting virtualfund objective, expected payee to start with 0", "allocations", e.Allocations, logging.WithObjectiveIdAttribute(o.Id())) | ||
return false | ||
} | ||
} | ||
} | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package policy | ||
|
||
import "github.com/statechannels/go-nitro/protocols" | ||
|
||
// PermissivePolicy is a policy maker that decides to approve every unapproved objective | ||
type PermissivePolicy struct{} | ||
|
||
// ShouldApprove decides to approve o if it is currently unapproved | ||
func (pp *PermissivePolicy) ShouldApprove(o protocols.Objective) bool { | ||
return o.GetStatus() == protocols.Unapproved | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters