risk.go80 lines, 2.4 KB
| 1 | // Package risk decides whether a transfer may go ahead: allow it, ask a |
| 2 | // person to review it, or deny it. Every decision comes with its reasons. |
| 3 | package risk |
| 4 | |
| 5 | import "time" |
| 6 | |
| 7 | // A Decision is what we do with a transfer. They're ordered: when rules |
| 8 | // disagree, the strongest one wins. |
| 9 | type Decision int |
| 10 | |
| 11 | const ( |
| 12 | Allow Decision = iota // go ahead |
| 13 | Review // hold the money, and ask a person |
| 14 | Deny // refuse, before any money moves |
| 15 | ) |
| 16 | |
| 17 | func (d Decision) String() string { |
| 18 | return [...]string{"allow", "review", "deny"}[d] |
| 19 | } |
| 20 | |
| 21 | // Signals are what we know about a transfer when we decide. They're gathered |
| 22 | // first, so rules never do I/O: they're fast, and trivial to test. |
| 23 | type Signals struct { |
| 24 | Amount int64 // in US cents, whatever the currency |
| 25 | Recent Recent // the sender's recent transfers, this one included |
| 26 | RecentUnknown bool // Redis didn't answer |
| 27 | ReceiverFrozen bool // the receiving wallet can't be paid |
| 28 | ReceiverAge time.Duration // since the receiving wallet was opened |
| 29 | PaidBefore bool // the sender has paid this wallet before |
| 30 | } |
| 31 | |
| 32 | // A Rule looks at a transfer's signals, and asks for a decision. Anything |
| 33 | // other than Allow comes with a reason. |
| 34 | type Rule interface { |
| 35 | Evaluate(s Signals) (Decision, string) |
| 36 | } |
| 37 | |
| 38 | // Check is the usual kind of Rule: a condition, and what it asks for. |
| 39 | type Check struct { |
| 40 | Name string // the reason it gives: stable, so we can count it |
| 41 | Decision Decision |
| 42 | Match func(s Signals) bool |
| 43 | } |
| 44 | |
| 45 | func (c Check) Evaluate(s Signals) (Decision, string) { |
| 46 | if c.Match(s) { |
| 47 | return c.Decision, c.Name |
| 48 | } |
| 49 | return Allow, "" |
| 50 | } |
| 51 | |
| 52 | // An Assessment is the engine's answer: the strongest decision any rule asked |
| 53 | // for, and every reason, in the order the rules ran. |
| 54 | type Assessment struct { |
| 55 | Decision Decision |
| 56 | Reasons []string |
| 57 | } |
| 58 | |
| 59 | // An Engine runs rules, in order. |
| 60 | type Engine struct { |
| 61 | rules []Rule |
| 62 | } |
| 63 | |
| 64 | // NewEngine returns an Engine that runs rules, in that order. |
| 65 | func NewEngine(rules ...Rule) *Engine { |
| 66 | return &Engine{rules: rules} |
| 67 | } |
| 68 | |
| 69 | // Evaluate runs every rule, even after a Deny: a record of everything that |
| 70 | // was wrong is worth more than the microseconds we'd save. |
| 71 | func (e *Engine) Evaluate(s Signals) Assessment { |
| 72 | var a Assessment |
| 73 | for _, r := range e.rules { |
| 74 | if d, reason := r.Evaluate(s); d != Allow { |
| 75 | a.Decision = max(a.Decision, d) |
| 76 | a.Reasons = append(a.Reasons, reason) |
| 77 | } |
| 78 | } |
| 79 | return a |
| 80 | } |