rep.gno
4.21 Kb · 166 lines
1// Package rep keeps a public work record for each address: how many tasks
2// it completed and was paid for, and how many tasks it posted and paid out.
3//
4// A Book holds no coins and makes no calls. A realm owns a Book and writes
5// one Record each time it pays for finished work. Any realm can import this
6// package to keep its own records, or read another realm's records through
7// that realm's public functions.
8package rep
9
10import (
11 "time"
12
13 "gno.land/p/nt/avl/v0"
14)
15
16// DefaultRecent is the number of recent records kept per address when
17// NewBook is given a value below 1.
18const DefaultRecent = 20
19
20// Record is one paid piece of work.
21type Record struct {
22 TaskID string
23 Title string
24 Poster address
25 Doer address
26 Amount int64
27 Denom string
28 At time.Time
29}
30
31// Profile is the record of one address. It is returned by value, so callers
32// cannot change the Book through it.
33type Profile struct {
34 Completed int // tasks this address finished and was paid for
35 Earned int64 // total paid to this address
36 Posted int // tasks this address paid out to someone else
37 Spent int64 // total this address paid out
38 recent []Record
39}
40
41// Recent returns the newest records first. The slice is a copy.
42func (p Profile) Recent() []Record {
43 out := make([]Record, len(p.recent))
44 copy(out, p.recent)
45 return out
46}
47
48// Book is a set of profiles, keyed by address.
49type Book struct {
50 profiles avl.Tree // address string -> *Profile
51 total int
52 volume int64
53 maxRecent int
54}
55
56// NewBook returns an empty Book that keeps up to maxRecent records per
57// address.
58func NewBook(maxRecent int) *Book {
59 if maxRecent < 1 {
60 maxRecent = DefaultRecent
61 }
62 return &Book{maxRecent: maxRecent}
63}
64
65// Add stores a paid record for both the doer and the poster.
66// It panics if the record is not valid, so a realm cannot store a bad record
67// by mistake.
68func (b *Book) Add(r Record) {
69 if !r.Doer.IsValid() {
70 panic("rep: invalid doer address")
71 }
72 if !r.Poster.IsValid() {
73 panic("rep: invalid poster address")
74 }
75 if r.Doer == r.Poster {
76 panic("rep: doer and poster must differ")
77 }
78 if r.Amount <= 0 {
79 panic("rep: amount must be positive")
80 }
81 if r.TaskID == "" {
82 panic("rep: task id is required")
83 }
84
85 doer := b.mustProfile(r.Doer)
86 if doer.Earned > maxInt64-r.Amount {
87 panic("rep: earned overflow")
88 }
89 doer.Completed++
90 doer.Earned += r.Amount
91 doer.recent = b.prepend(doer.recent, r)
92
93 poster := b.mustProfile(r.Poster)
94 if poster.Spent > maxInt64-r.Amount {
95 panic("rep: spent overflow")
96 }
97 poster.Posted++
98 poster.Spent += r.Amount
99 poster.recent = b.prepend(poster.recent, r)
100
101 b.total++
102 if b.volume <= maxInt64-r.Amount {
103 b.volume += r.Amount
104 }
105}
106
107// Profile returns a copy of the profile for addr. Unknown addresses return
108// an empty profile.
109func (b *Book) Profile(addr address) Profile {
110 v := b.profiles.Get(addr.String())
111 if v == nil {
112 return Profile{}
113 }
114 p := v.(*Profile)
115 return Profile{
116 Completed: p.Completed,
117 Earned: p.Earned,
118 Posted: p.Posted,
119 Spent: p.Spent,
120 recent: p.recent,
121 }
122}
123
124// Completed returns how many tasks addr finished and was paid for.
125func (b *Book) Completed(addr address) int { return b.Profile(addr).Completed }
126
127// Earned returns the total paid to addr.
128func (b *Book) Earned(addr address) int64 { return b.Profile(addr).Earned }
129
130// HasCompleted reports whether addr finished at least n tasks. Realms can use
131// it to gate features by proven work.
132func (b *Book) HasCompleted(addr address, n int) bool {
133 return b.Completed(addr) >= n
134}
135
136// Total returns the number of paid records in the Book.
137func (b *Book) Total() int { return b.total }
138
139// Volume returns the total amount recorded, capped at the int64 maximum.
140func (b *Book) Volume() int64 { return b.volume }
141
142// Size returns the number of addresses with a profile.
143func (b *Book) Size() int { return b.profiles.Size() }
144
145const maxInt64 = int64(^uint64(0) >> 1)
146
147func (b *Book) mustProfile(addr address) *Profile {
148 key := addr.String()
149 if v := b.profiles.Get(key); v != nil {
150 return v.(*Profile)
151 }
152 p := &Profile{}
153 b.profiles.Set(key, p)
154 return p
155}
156
157func (b *Book) prepend(list []Record, r Record) []Record {
158 n := len(list) + 1
159 if n > b.maxRecent {
160 n = b.maxRecent
161 }
162 out := make([]Record, n)
163 out[0] = r
164 copy(out[1:], list)
165 return out
166}