revision.gno
2.20 Kb · 85 lines
1package wiki
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "time"
7)
8
9// Kind labels what a revision did, for the recent-changes feed.
10type Kind string
11
12const (
13 KindCreate Kind = "create"
14 KindEdit Kind = "edit"
15 KindRevert Kind = "revert"
16 KindMove Kind = "move"
17 KindBlank Kind = "blank"
18 KindProtect Kind = "protect"
19)
20
21// Revision is one entry in a page's append-only history.
22//
23// The exported fields are the permanent spine: they are never evicted, and
24// they are what makes the history tamper-evident. Hash is the SHA-256 of the
25// body bytes as submitted, so a body recovered from transaction history can be
26// checked against the chain's own record of it.
27//
28// body is the only field under a retention policy. When a revision falls out
29// of the page's body window it is evicted, which releases the storage deposit
30// those bytes locked.
31type Revision struct {
32 ID uint64
33 Prev uint64 // 0 for the first revision of a page
34 Kind Kind
35 Author address
36 Time time.Time
37 Height int64
38 Summary string
39 Hash string // hex-encoded SHA-256 of the body
40 Size int // body length in bytes
41 Minor bool
42
43 body string
44 kept bool
45}
46
47// Body returns the revision's text and whether it is still held on chain. A
48// false second return is not corruption: the body aged out of the retention
49// window and must be recovered from the transaction that wrote it, then
50// checked against Hash.
51func (r *Revision) Body() (string, bool) { return r.body, r.kept }
52
53// Kept reports whether this revision's body is still on chain.
54func (r *Revision) Kept() bool { return r.kept }
55
56// evict drops the body and returns the number of bytes released.
57func (r *Revision) evict() int {
58 if !r.kept {
59 return 0
60 }
61 n := len(r.body)
62 r.body = ""
63 r.kept = false
64 return n
65}
66
67// hashBody is the content address of a revision body.
68func hashBody(body string) string {
69 sum := sha256.Sum256([]byte(body))
70 return hex.EncodeToString(sum[:])
71}
72
73// ShortHash is the first 12 hex characters of Hash, for display.
74func (r *Revision) ShortHash() string {
75 if len(r.Hash) < 12 {
76 return r.Hash
77 }
78 return r.Hash[:12]
79}
80
81// Change is a recent-changes entry: a revision plus the page it landed on.
82type Change struct {
83 Title Title
84 Rev *Revision
85}