Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

rule.gno

2.28 Kb · 85 lines
 1// Package rulecalc holds the evaluation rule schema for a grant round and the
 2// deterministic computation of its outcome. It has no state and moves no coins.
 3package rulecalc
 4
 5import (
 6	"errors"
 7	"strconv"
 8)
 9
10// Method is how evaluator score sheets are aggregated into a winner.
11type Method int
12
13const (
14	// WeightedSum: the winner has the highest sum of weight*score over all sheets.
15	WeightedSum Method = iota + 1
16	// MajorityFirst: each sheet's first place is its highest weighted app;
17	// the winner has the most first places.
18	MajorityFirst
19)
20
21// TieBreak decides a top-level tie.
22type TieBreak int
23
24const (
25	// LowestID: the tied app with the lowest ID wins.
26	LowestID TieBreak = iota + 1
27	// Refund: no winner; the grant returns to the customer.
28	Refund
29)
30
31const (
32	MinCriteria = 1
33	MaxCriteria = 4
34	MinWeight   = 1
35	MaxWeight   = 10
36	MinScore    = 0
37	MaxScore    = 10
38)
39
40// Criterion is one named, weighted evaluation axis.
41type Criterion struct {
42	Name   string
43	Weight int
44}
45
46// Rule is the procedure the evaluators agree on before applications are revealed.
47// Rationale is shown to readers and never affects computation.
48type Rule struct {
49	Method    Method
50	Criteria  []Criterion
51	Quorum    int
52	Tie       TieBreak
53	Rationale string
54}
55
56// Validate checks the rule against the schema for a round with nEvaluators joined evaluators.
57func (r Rule) Validate(nEvaluators int) error {
58	if r.Method != WeightedSum && r.Method != MajorityFirst {
59		return errors.New("rulecalc: unknown method " + strconv.Itoa(int(r.Method)))
60	}
61	if r.Tie != LowestID && r.Tie != Refund {
62		return errors.New("rulecalc: unknown tie-break " + strconv.Itoa(int(r.Tie)))
63	}
64	n := len(r.Criteria)
65	if n < MinCriteria || n > MaxCriteria {
66		return errors.New("rulecalc: need 1-4 criteria, got " + strconv.Itoa(n))
67	}
68	seen := map[string]bool{}
69	for _, c := range r.Criteria {
70		if c.Name == "" {
71			return errors.New("rulecalc: criterion name is empty")
72		}
73		if seen[c.Name] {
74			return errors.New("rulecalc: duplicate criterion " + c.Name)
75		}
76		seen[c.Name] = true
77		if c.Weight < MinWeight || c.Weight > MaxWeight {
78			return errors.New("rulecalc: weight of " + c.Name + " must be 1-10")
79		}
80	}
81	if r.Quorum < 1 || r.Quorum > nEvaluators {
82		return errors.New("rulecalc: quorum must be between 1 and " + strconv.Itoa(nEvaluators))
83	}
84	return nil
85}