Skip to content

Commit

Permalink
move to own files
Browse files Browse the repository at this point in the history
  • Loading branch information
lalexgap committed Aug 28, 2023
1 parent 4a1d016 commit 8d9f0e2
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 76 deletions.
28 changes: 28 additions & 0 deletions node/engine/policy/allowlist.go
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
}
54 changes: 54 additions & 0 deletions node/engine/policy/fairoutcome.go
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
}
11 changes: 11 additions & 0 deletions node/engine/policy/permissive.go
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
}
76 changes: 0 additions & 76 deletions node/engine/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 8d9f0e2

Please sign in to comment.