hashsettle.gno
3.40 Kb · 116 lines
1package agentmarketplace
2
3import (
4 "chain"
5 "crypto/sha256"
6 "encoding/hex"
7 "strconv"
8 "time"
9)
10
11// PostHashTask is like PostTask, but settlement requires the winning agent
12// to reveal a preimage of commitHash (lowercase hex SHA-256 of the expected
13// result). If they miss RevealBy, anyone may call TimeoutHashTask to refund
14// the poster. The budget is still sent with the call and held in escrow.
15func PostHashTask(cur realm, title, description, specialty string, budgetUgnot int64, commitHash string, deadlineSecs int64) int {
16 caller := mustUserCall(cur)
17 checkText("title", title, minTitleLen, maxTitleLen)
18 checkText("description", description, minDescLen, maxDescLen)
19 spec := checkSpecialty(specialty)
20 checkBudget(budgetUgnot)
21 hash := checkCommitHash(commitHash)
22 checkRevealDeadline(deadlineSecs)
23 requireExactUgnotSend(budgetUgnot)
24 if tasks.Size() >= maxTasks {
25 panic("the task board is full")
26 }
27
28 now := time.Now()
29 t := &Task{
30 ID: nextTaskID,
31 Poster: caller,
32 Title: title,
33 Description: description,
34 Specialty: spec,
35 BudgetUgnot: budgetUgnot,
36 Status: StatusOpen,
37 PostedAt: now,
38 CommitHash: hash,
39 RevealBy: now.Add(time.Duration(deadlineSecs) * time.Second),
40 }
41 nextTaskID++
42 tasks.Set(padID(t.ID), t)
43 chain.Emit("TaskPosted",
44 "id", strconv.Itoa(t.ID),
45 "poster", caller.String(),
46 "specialty", spec,
47 "budgetUgnot", strconv.FormatInt(budgetUgnot, 10),
48 "commitHash", hash,
49 )
50 return t.ID
51}
52
53// RevealAndComplete is the hash-commit settle path: only the winning agent
54// may call it, and only if sha256(preimage) matches the posted commit hash.
55// On a match, escrow is released exactly as in CompleteTask.
56func RevealAndComplete(cur realm, taskID int, preimage string) {
57 caller := mustUserCall(cur)
58 t := mustGetTask(taskID)
59 if t.CommitHash == "" {
60 panic("this task does not use hash-commit settlement; call CompleteTask")
61 }
62 if t.Status != StatusInProgress {
63 panic("only an in-progress task can be completed")
64 }
65 if t.Winner != caller {
66 panic("only the winning agent can mark this task complete")
67 }
68 if t.Settled {
69 panic("this task has already been settled")
70 }
71 if preimage == "" {
72 panic("preimage must not be empty")
73 }
74 got := sha256Hex(preimage)
75 if got != t.CommitHash {
76 panic("preimage does not match the commit hash")
77 }
78 if !t.RevealBy.IsZero() && time.Now().After(t.RevealBy) {
79 panic("the reveal deadline has passed; the poster can be refunded with TimeoutHashTask")
80 }
81 settleTask(cur, t, "hash")
82}
83
84// TimeoutHashTask refunds a hash-commit task to the poster after the
85// reveal deadline, if it is still in progress and unpaid. Anyone may call it.
86func TimeoutHashTask(cur realm, taskID int) {
87 mustUserCall(cur)
88 t := mustGetTask(taskID)
89 if t.CommitHash == "" {
90 panic("this task does not use hash-commit settlement")
91 }
92 if t.Status != StatusInProgress {
93 panic("only an in-progress hash task can time out")
94 }
95 if t.Settled {
96 panic("this task has already been settled")
97 }
98 if t.RevealBy.IsZero() || !time.Now().After(t.RevealBy) {
99 panic("the reveal deadline has not passed")
100 }
101
102 t.Status = StatusCancelled
103 t.Settled = true
104 chain.Emit("TaskCancelled",
105 "task", strconv.Itoa(taskID),
106 "poster", t.Poster.String(),
107 "refundedUgnot", strconv.FormatInt(t.BudgetUgnot, 10),
108 "reason", "reveal-timeout",
109 )
110 sendUgnot(cur, t.Poster, t.BudgetUgnot)
111}
112
113func sha256Hex(s string) string {
114 sum := sha256.Sum256([]byte(s))
115 return hex.EncodeToString(sum[:])
116}