From 8d9f0e223fee819063fb0c969275c82a9bb06e46 Mon Sep 17 00:00:00 2001 From: lalexgap Date: Mon, 28 Aug 2023 12:06:21 -0700 Subject: [PATCH] move to own files --- node/engine/policy/allowlist.go | 28 ++++++++++++ node/engine/policy/fairoutcome.go | 54 ++++++++++++++++++++++ node/engine/policy/permissive.go | 11 +++++ node/engine/policy/policy.go | 76 ------------------------------- 4 files changed, 93 insertions(+), 76 deletions(-) create mode 100644 node/engine/policy/allowlist.go create mode 100644 node/engine/policy/fairoutcome.go create mode 100644 node/engine/policy/permissive.go diff --git a/node/engine/policy/allowlist.go b/node/engine/policy/allowlist.go new file mode 100644 index 000000000..43957223c --- /dev/null +++ b/node/engine/policy/allowlist.go @@ -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 +} diff --git a/node/engine/policy/fairoutcome.go b/node/engine/policy/fairoutcome.go new file mode 100644 index 000000000..0c7b12a67 --- /dev/null +++ b/node/engine/policy/fairoutcome.go @@ -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 +} diff --git a/node/engine/policy/permissive.go b/node/engine/policy/permissive.go new file mode 100644 index 000000000..48395178e --- /dev/null +++ b/node/engine/policy/permissive.go @@ -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 +} diff --git a/node/engine/policy/policy.go b/node/engine/policy/policy.go index 5fe5f8ee6..f8d1509f0 100644 --- a/node/engine/policy/policy.go +++ b/node/engine/policy/policy.go @@ -3,13 +3,9 @@ package policy import ( "fmt" "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" ) // PolicyMaker is used to decide whether to approve or reject an objective @@ -39,75 +35,3 @@ func (p *Policies) ShouldApprove(o protocols.Objective) bool { return true } - -// 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 -} - -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 -} - -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 -}