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.

hashsettle_test.gno

5.07 Kb · 134 lines
  1package agentmarketplace
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/nt/uassert/v0"
  7	"gno.land/p/nt/urequire/v0"
  8)
  9
 10const knownPhrase = "the expected result phrase"
 11
 12func TestHashCommitHappyPath(cur realm, t *testing.T) {
 13	reset()
 14	register(cur, carol, "Research Bot", "research", RateHourly, 500000)
 15	hash := sha256Hex(knownPhrase)
 16	id := postHash(cur, alice, "Known answer task", "Reveal the phrase I already committed to.", "research", 2000000, hash, 3600)
 17	tk := mustGetTask(id)
 18	uassert.Equal(t, hash, tk.CommitHash)
 19	uassert.False(t, tk.RevealBy.IsZero())
 20
 21	bidID := bid(cur, carol, id, 1500000, "I know it")
 22	accept(cur, alice, id, bidID)
 23
 24	urequire.AbortsWithMessage(t, cur, "this task requires a hash reveal: call RevealAndComplete", func() {
 25		complete(cur, carol, id)
 26	})
 27	urequire.AbortsWithMessage(t, cur, "preimage does not match the commit hash", func() {
 28		reveal(cur, carol, id, "wrong phrase")
 29	})
 30	urequire.AbortsWithMessage(t, cur, "preimage must not be empty", func() {
 31		reveal(cur, carol, id, "")
 32	})
 33	urequire.AbortsWithMessage(t, cur, "only the winning agent can mark this task complete", func() {
 34		reveal(cur, alice, id, knownPhrase)
 35	})
 36
 37	reveal(cur, carol, id, knownPhrase)
 38	tk = mustGetTask(id)
 39	uassert.Equal(t, StatusCompleted, tk.Status)
 40	uassert.True(t, tk.Settled)
 41	uassert.Equal(t, 1, mustGetAgent(carol).Completed)
 42
 43	// poster can rate after a hash settlement just like a normal one
 44	rid := rate(cur, alice, id, 5, "Hash matched")
 45	uassert.Equal(t, 1, rid)
 46	_, trust, _, _ := ScoreOf(carol)
 47	uassert.Equal(t, int64(5000), trust)
 48}
 49
 50func TestHashCommitRefusesOrdinaryCompleteAndViceVersa(cur realm, t *testing.T) {
 51	reset()
 52	register(cur, carol, "Research Bot", "research", RateHourly, 500000)
 53	ordinary := settleJob(cur, alice, carol, "Ordinary research", "This task has no commit hash.", "research", 2000000)
 54	urequire.AbortsWithMessage(t, cur, "this task does not use hash-commit settlement; call CompleteTask", func() {
 55		reveal(cur, carol, ordinary, knownPhrase)
 56	})
 57
 58	hash := sha256Hex(knownPhrase)
 59	id := postHash(cur, alice, "Known answer task", "Reveal the phrase I already committed to.", "research", 1000000, hash, 3600)
 60	bidID := bid(cur, carol, id, 1000000, "ready")
 61	accept(cur, alice, id, bidID)
 62	urequire.AbortsWithMessage(t, cur, "this task requires a hash reveal: call RevealAndComplete", func() {
 63		complete(cur, carol, id)
 64	})
 65}
 66
 67func TestHashCommitTimeoutRefund(cur realm, t *testing.T) {
 68	reset()
 69	register(cur, carol, "Research Bot", "research", RateHourly, 500000)
 70	hash := sha256Hex(knownPhrase)
 71	id := postHash(cur, alice, "Known answer task", "Reveal the phrase I already committed to.", "research", 2000000, hash, minRevealSecs)
 72	bidID := bid(cur, carol, id, 2000000, "ready")
 73	accept(cur, alice, id, bidID)
 74
 75	urequire.AbortsWithMessage(t, cur, "the reveal deadline has not passed", func() {
 76		timeoutHash(cur, bob, id)
 77	})
 78
 79	// min reveal is 60s; each height is 5s, so 13 heights is past the deadline.
 80	testing.SkipHeights(13)
 81	urequire.AbortsWithMessage(t, cur, "the reveal deadline has passed; the poster can be refunded with TimeoutHashTask", func() {
 82		reveal(cur, carol, id, knownPhrase)
 83	})
 84
 85	timeoutHash(cur, bob, id) // anyone may time it out
 86	tk := mustGetTask(id)
 87	uassert.Equal(t, StatusCancelled, tk.Status)
 88	uassert.True(t, tk.Settled)
 89
 90	urequire.AbortsWithMessage(t, cur, "only an in-progress hash task can time out", func() {
 91		timeoutHash(cur, alice, id)
 92	})
 93	urequire.AbortsWithMessage(t, cur, "only a settled, completed task can be rated", func() {
 94		rate(cur, alice, id, 5, "timed out")
 95	})
 96}
 97
 98func TestHashCommitValidation(cur realm, t *testing.T) {
 99	reset()
100	urequire.AbortsWithMessage(t, cur, "commit hash must be a 64-character hex SHA-256 digest", func() {
101		postHash(cur, alice, "Known answer task", "Reveal the phrase I already committed to.", "research", 1000000, "abc", 3600)
102	})
103	urequire.AbortsWithMessage(t, cur, "commit hash must be a 64-character hex SHA-256 digest", func() {
104		postHash(cur, alice, "Known answer task", "Reveal the phrase I already committed to.", "research", 1000000, "gg"+sha256Hex(knownPhrase)[2:], 3600)
105	})
106	urequire.AbortsWithMessage(t, cur, "reveal deadline must be between 60 seconds and 30 days", func() {
107		postHash(cur, alice, "Known answer task", "Reveal the phrase I already committed to.", "research", 1000000, sha256Hex(knownPhrase), 59)
108	})
109	// uppercase hex is accepted and stored in lowercase
110	lower := sha256Hex(knownPhrase)
111	upper := toUpperHex(lower)
112	id := postHash(cur, alice, "Known answer task", "Reveal the phrase I already committed to.", "research", 1000000, upper, 3600)
113	uassert.Equal(t, lower, mustGetTask(id).CommitHash)
114}
115
116func TestHashCommitOpenCancelStillWorks(cur realm, t *testing.T) {
117	reset()
118	id := postHash(cur, alice, "Known answer task", "Reveal the phrase I already committed to.", "research", 2000000, sha256Hex(knownPhrase), 3600)
119	cancel(cur, alice, id)
120	uassert.Equal(t, StatusCancelled, mustGetTask(id).Status)
121	uassert.True(t, mustGetTask(id).Settled)
122}
123
124func toUpperHex(s string) string {
125	out := ""
126	for _, c := range s {
127		if c >= 'a' && c <= 'f' {
128			out += string(c - ('a' - 'A'))
129		} else {
130			out += string(c)
131		}
132	}
133	return out
134}