hearthbounty_test.gno
22.54 Kb · 544 lines
1package hearthbounty
2
3import (
4 "chain"
5 "strings"
6 "testing"
7
8 "gno.land/p/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthcore/v0"
9 "gno.land/p/nt/avl/v0"
10 "gno.land/p/nt/testutils/v0"
11 "gno.land/p/nt/uassert/v0"
12 "gno.land/r/gnoland/wugnot"
13)
14
15const (
16 testRealmPath = "gno.land/r/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthbounty/v1"
17 wugnotPkgPath = "gno.land/r/gnoland/wugnot"
18)
19
20func resetState(initialArbiter address) {
21 arbiter = initialArbiter
22 realmAddress = chain.PackageAddress(testRealmPath)
23 realmPkgPath = testRealmPath
24 realmWebBase = "/r/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthbounty/v1"
25 bountySeq = 0
26 bounties = avl.NewTree()
27 profiles = avl.NewTree()
28 openCount = 0
29 claimedCount = 0
30 submittedCount = 0
31 disputedCount = 0
32 settledCount = 0
33 cancelledCount = 0
34 agentCount = 0
35 totalEscrowUgnot = 0
36 totalSettledUgnot = 0
37 totalRefundUgnot = 0
38 initRouter()
39}
40
41func prepCoins(user address, amount int64) {
42 wugnotAddr := chain.PackageAddress(wugnotPkgPath)
43 testing.SetOriginCaller(user)
44 testing.SetOriginSend(chain.Coins{{Denom: "ugnot", Amount: amount}})
45 testing.IssueCoins(wugnotAddr, chain.Coins{{Denom: "ugnot", Amount: amount}})
46}
47
48func assertEscrowInvariant(t *testing.T, realmAddr address, initialRealmBal int64) {
49 t.Helper()
50 uassert.Equal(t, initialRealmBal+totalEscrowUgnot, EscrowBalance(realmAddr))
51}
52
53func TestFullBountyLifecycleAndReputation(cur realm, t *testing.T) {
54 arb := testutils.TestAddress("arbiter")
55 sponsor := testutils.TestAddress("sponsor")
56 agent := testutils.TestAddress("agent_hunter")
57 donor := testutils.TestAddress("crowdfund_donor")
58 realmAddr := chain.PackageAddress(testRealmPath)
59
60 testing.SetRealm(testing.NewUserRealm(arb))
61 resetState(arb)
62 baseRealmBal := EscrowBalance(realmAddr)
63
64 testing.SetRealm(testing.NewUserRealm(agent))
65 RegisterProfile(cross(cur), "anvil-ai", "Autonomous Gno realm builder", "gno,audit,mcp", "mcp://anvil.local")
66 prof, ok := GetProfile(agent)
67 uassert.True(t, ok)
68 uassert.True(t, prof.IsAgent)
69 uassert.Equal(t, "anvil-ai", prof.Handle)
70 uassert.Equal(t, 1, GetStats().AgentCount)
71
72 testing.SetRealm(testing.NewUserRealm(sponsor))
73 prepCoins(sponsor, 2000000)
74 wugnot.Deposit(cross(cur))
75 testing.SetOriginSend(chain.Coins{})
76 wugnot.Approve(cross(cur), realmAddr, 2000000)
77 uassert.Equal(t, int64(2000000), EscrowAllowance(sponsor))
78
79 id := PostBounty(cross(cur), "Build AVL Leaderboard Helper", "Provide a deterministic AVL sorting helper with full unit test coverage.", "realm", 2000000, 200)
80 uassert.Equal(t, uint64(1), id)
81 uassert.Equal(t, int64(2000000), GetStats().TotalEscrowUgnot)
82 assertEscrowInvariant(t, realmAddr, baseRealmBal)
83
84 testing.SetRealm(testing.NewUserRealm(donor))
85 prepCoins(donor, 500000)
86 wugnot.Deposit(cross(cur))
87 testing.SetOriginSend(chain.Coins{})
88 wugnot.Approve(cross(cur), realmAddr, 500000)
89 newEscrow := TopUpBounty(cross(cur), id, 500000)
90 uassert.Equal(t, int64(2500000), newEscrow)
91 uassert.Equal(t, int64(500000), GetDonorContribution(id, donor))
92 assertEscrowInvariant(t, realmAddr, baseRealmBal)
93
94 testing.SetOriginCaller(sponsor)
95 testing.SetRealm(testing.NewUserRealm(sponsor))
96 uassert.AbortsWithMessage(t, cur, "hearthbounty: sponsor cannot claim own bounty", func() {
97 ClaimBounty(cross(cur), id, "self claim")
98 })
99
100 testing.SetOriginCaller(agent)
101 testing.SetRealm(testing.NewUserRealm(agent))
102 ClaimBounty(cross(cur), id, "Claiming via hearthbounty-mcp; ETA 40 blocks.")
103 b, ok := GetBounty(id)
104 uassert.True(t, ok)
105 uassert.Equal(t, string(hearthcore.StatusClaimed), string(b.Status))
106 uassert.Equal(t, agent, b.Claimant)
107
108 SubmitProof(cross(cur), id, "https://github.com/hearthbounty/demo/pull/42", "sha256:9f86d081884c7d659a2feaa0c55ad015", "Implemented AVL leaderboard helper and passed all tests.")
109 b, _ = GetBounty(id)
110 uassert.Equal(t, string(hearthcore.StatusSubmitted), string(b.Status))
111 uassert.Equal(t, uint64(1), b.SubmissionCount)
112 uassert.Equal(t, b.SubmittedHeight+b.DeadlineBlocks, b.ReviewDeadlineHeight)
113
114 testing.SetOriginCaller(sponsor)
115 testing.SetRealm(testing.NewUserRealm(sponsor))
116 RequestRevision(cross(cur), id, "Please add edge-case tests for empty trees.")
117 b, _ = GetBounty(id)
118 uassert.Equal(t, string(hearthcore.StatusClaimed), string(b.Status))
119 uassert.Equal(t, "Please add edge-case tests for empty trees.", b.RevisionFeedback)
120
121 testing.SetOriginCaller(agent)
122 testing.SetRealm(testing.NewUserRealm(agent))
123 SubmitProof(cross(cur), id, "https://github.com/hearthbounty/demo/pull/42", "sha256:4b227777d4dd1fc61c6f884f48641d02", "Added empty-tree and tie-breaker unit tests.")
124 b, _ = GetBounty(id)
125 uassert.Equal(t, uint64(2), b.SubmissionCount)
126
127 uassert.AbortsWithMessage(t, cur, "hearthbounty: only the sponsor may approve and settle", func() {
128 ApproveAndSettle(cross(cur), id, 5, "unauthorized")
129 })
130
131 agentBalBefore := EscrowBalance(agent)
132 testing.SetOriginCaller(sponsor)
133 testing.SetRealm(testing.NewUserRealm(sponsor))
134 ApproveAndSettle(cross(cur), id, 5, "Clean idiomatic Gno code and crisp tests.")
135
136 agentBalAfter := EscrowBalance(agent)
137 uassert.Equal(t, agentBalBefore+2500000, agentBalAfter)
138 assertEscrowInvariant(t, realmAddr, baseRealmBal)
139
140 b, _ = GetBounty(id)
141 uassert.Equal(t, string(hearthcore.StatusSettled), string(b.Status))
142 uassert.Equal(t, int64(0), b.EscrowUgnot)
143 uassert.Equal(t, int64(2500000), b.PayoutUgnot)
144 uassert.Equal(t, uint8(5), b.Rating)
145
146 prof, _ = GetProfile(agent)
147 uassert.Equal(t, uint64(1), prof.SettledCount)
148 uassert.Equal(t, int64(2500000), prof.EarnedUgnot)
149 uassert.Equal(t, uint64(5), prof.RatingSum)
150 uassert.Equal(t, uint64(1), prof.RatingCount)
151}
152
153// H-1 regression proof: CancelBounty must refund external top-ups strictly to
154// their original donors and NEVER transfer third-party coins to the sponsor.
155func TestCancelRefundsThirdPartyDonorsWithoutSponsorTheft(cur realm, t *testing.T) {
156 arb := testutils.TestAddress("h1_arb")
157 sponsor := testutils.TestAddress("h1_sponsor")
158 donorA := testutils.TestAddress("h1_donor_a")
159 donorB := testutils.TestAddress("h1_donor_b")
160 realmAddr := chain.PackageAddress(testRealmPath)
161
162 testing.SetRealm(testing.NewUserRealm(arb))
163 resetState(arb)
164 baseRealmBal := EscrowBalance(realmAddr)
165
166 // Sponsor posts 0.50 GNOT.
167 testing.SetRealm(testing.NewUserRealm(sponsor))
168 prepCoins(sponsor, 500000)
169 wugnot.Deposit(cross(cur))
170 testing.SetOriginSend(chain.Coins{})
171 wugnot.Approve(cross(cur), realmAddr, 500000)
172 id := PostBounty(cross(cur), "Crowd-Funded Escrow Custody Test", "Verify third-party top-ups are returned to donors on cancel.", "audit", 500000, 100)
173
174 // Donor A tops up 1.50 GNOT in two contributions; Donor B tops up 0.40 GNOT.
175 testing.SetRealm(testing.NewUserRealm(donorA))
176 prepCoins(donorA, 1500000)
177 wugnot.Deposit(cross(cur))
178 testing.SetOriginSend(chain.Coins{})
179 wugnot.Approve(cross(cur), realmAddr, 1500000)
180 TopUpBounty(cross(cur), id, 1000000)
181 TopUpBounty(cross(cur), id, 500000)
182
183 testing.SetRealm(testing.NewUserRealm(donorB))
184 prepCoins(donorB, 400000)
185 wugnot.Deposit(cross(cur))
186 testing.SetOriginSend(chain.Coins{})
187 wugnot.Approve(cross(cur), realmAddr, 400000)
188 TopUpBounty(cross(cur), id, 400000)
189
190 // External donor cannot claim the bounty they funded (blocks open->topup->claim wash).
191 uassert.AbortsWithMessage(t, cur, "hearthbounty: escrow contributor cannot claim own funded bounty", func() {
192 ClaimBounty(cross(cur), id, "wash attempt")
193 })
194
195 b, _ := GetBounty(id)
196 uassert.Equal(t, int64(2400000), b.EscrowUgnot)
197 uassert.Equal(t, int64(500000), b.SponsorFundedUgnot)
198 uassert.Equal(t, int64(1900000), b.ExternalTopUpUgnot)
199 uassert.Equal(t, 2, b.ExternalDonorCount)
200
201 sponsorBefore := EscrowBalance(sponsor)
202 donorABefore := EscrowBalance(donorA)
203 donorBBefore := EscrowBalance(donorB)
204
205 // Sponsor cancels -> sponsor receives ONLY their own 0.50 GNOT; donors receive exact refunds.
206 testing.SetOriginCaller(sponsor)
207 testing.SetRealm(testing.NewUserRealm(sponsor))
208 CancelBounty(cross(cur), id)
209
210 uassert.Equal(t, sponsorBefore+500000, EscrowBalance(sponsor))
211 uassert.Equal(t, donorABefore+1500000, EscrowBalance(donorA))
212 uassert.Equal(t, donorBBefore+400000, EscrowBalance(donorB))
213 assertEscrowInvariant(t, realmAddr, baseRealmBal)
214
215 // Phantom SponsoredUgnot credit must be zeroed after full refund.
216 spProf, _ := GetProfile(sponsor)
217 dAProf, _ := GetProfile(donorA)
218 dBProf, _ := GetProfile(donorB)
219 uassert.Equal(t, int64(0), spProf.SponsoredUgnot)
220 uassert.Equal(t, int64(0), dAProf.SponsoredUgnot)
221 uassert.Equal(t, int64(0), dBProf.SponsoredUgnot)
222}
223
224// H-1 claimant wash proof: active claimant cannot top up their own claimed bounty.
225func TestClaimantCannotWashTopUpIntoEarned(cur realm, t *testing.T) {
226 arb := testutils.TestAddress("wash_arb")
227 sponsor := testutils.TestAddress("wash_sponsor")
228 hunter := testutils.TestAddress("wash_hunter")
229 realmAddr := chain.PackageAddress(testRealmPath)
230
231 testing.SetRealm(testing.NewUserRealm(arb))
232 resetState(arb)
233
234 testing.SetRealm(testing.NewUserRealm(sponsor))
235 prepCoins(sponsor, 500000)
236 wugnot.Deposit(cross(cur))
237 testing.SetOriginSend(chain.Coins{})
238 wugnot.Approve(cross(cur), realmAddr, 500000)
239 id := PostBounty(cross(cur), "Wash Guard Check", "Verify active claimant cannot top up own claimed bounty.", "tool", 500000, 50)
240
241 testing.SetOriginCaller(hunter)
242 testing.SetRealm(testing.NewUserRealm(hunter))
243 ClaimBounty(cross(cur), id, "Claiming")
244
245 uassert.PanicsWithMessage(t, cur, "hearthbounty: active claimant cannot top up own claimed bounty", func() {
246 validateTopUpTarget(hunter, id, 200000)
247 })
248 // Non-origin cross-realm TopUpBounty is also blocked by AssertOriginCall.
249 uassert.AbortsWithMessage(t, cur, "invalid non-origin call", func() {
250 TopUpBounty(cross(cur), id, 200000)
251 })
252}
253
254func TestCancelAndExpiredClaimRelease(cur realm, t *testing.T) {
255 arb := testutils.TestAddress("arbiter2")
256 sponsor := testutils.TestAddress("sponsor2")
257 hunter := testutils.TestAddress("hunter2")
258 bystander := testutils.TestAddress("bystander2")
259 realmAddr := chain.PackageAddress(testRealmPath)
260
261 testing.SetRealm(testing.NewUserRealm(arb))
262 resetState(arb)
263 baseRealmBal := EscrowBalance(realmAddr)
264
265 testing.SetRealm(testing.NewUserRealm(sponsor))
266 prepCoins(sponsor, 1000000)
267 wugnot.Deposit(cross(cur))
268 testing.SetOriginSend(chain.Coins{})
269 wugnot.Approve(cross(cur), realmAddr, 1000000)
270 id := PostBounty(cross(cur), "Write gnoweb SVG Guide", "Document deterministic SVG rendering in gnoweb v1.8.", "docs", 1000000, 20)
271
272 testing.SetOriginCaller(hunter)
273 testing.SetRealm(testing.NewUserRealm(hunter))
274 ClaimBounty(cross(cur), id, "On it")
275 SubmitProof(cross(cur), id, "https://github.com/hearthbounty/docs/pull/1", "sha256:0123456789abcdef", "Initial draft submitted before revision.")
276
277 // Sponsor requests revision -> back to Claimed with stale proof fields still set until release.
278 testing.SetOriginCaller(sponsor)
279 testing.SetRealm(testing.NewUserRealm(sponsor))
280 RequestRevision(cross(cur), id, "Needs cleaner examples.")
281
282 uassert.AbortsWithMessage(t, cur, "hearthbounty: only open bounties can be cancelled", func() {
283 CancelBounty(cross(cur), id)
284 })
285
286 testing.SetOriginCaller(bystander)
287 testing.SetRealm(testing.NewUserRealm(bystander))
288 uassert.AbortsWithMessage(t, cur, "hearthbounty: claim deadline has not expired yet", func() {
289 ReleaseExpiredClaim(cross(cur), id)
290 })
291
292 // Advance exactly to DeadlineHeight (20 blocks) -> bystander can release on exact deadline block.
293 testing.SkipHeights(20)
294 ReleaseExpiredClaim(cross(cur), id)
295 b, _ := GetBounty(id)
296 uassert.Equal(t, string(hearthcore.StatusOpen), string(b.Status))
297 uassert.Equal(t, address(""), b.Claimant)
298 uassert.Equal(t, "", b.ProofURL)
299 uassert.Equal(t, "", b.ProofDigest)
300 uassert.Equal(t, "", b.ProofSummary)
301
302 // ClaimedCount must be decremented on release so claim-and-release cannot inflate it.
303 hp, _ := GetProfile(hunter)
304 uassert.Equal(t, uint64(0), hp.ClaimedCount)
305
306 sponsorBalBefore := EscrowBalance(sponsor)
307 testing.SetOriginCaller(sponsor)
308 testing.SetRealm(testing.NewUserRealm(sponsor))
309 CancelBounty(cross(cur), id)
310
311 uassert.Equal(t, sponsorBalBefore+1000000, EscrowBalance(sponsor))
312 assertEscrowInvariant(t, realmAddr, baseRealmBal)
313 uassert.Equal(t, int64(1000000), GetStats().TotalRefundUgnot)
314}
315
316// M-1 proof #1: ExpireSubmission prevents sponsors from holding delivered proofs hostage.
317func TestSubmissionReviewTimeoutAutoSettle(cur realm, t *testing.T) {
318 arb := testutils.TestAddress("m1_arb")
319 sponsor := testutils.TestAddress("m1_sponsor")
320 hunter := testutils.TestAddress("m1_hunter")
321 bystander := testutils.TestAddress("m1_bystander")
322 realmAddr := chain.PackageAddress(testRealmPath)
323
324 testing.SetRealm(testing.NewUserRealm(arb))
325 resetState(arb)
326 baseRealmBal := EscrowBalance(realmAddr)
327
328 testing.SetRealm(testing.NewUserRealm(sponsor))
329 prepCoins(sponsor, 1200000)
330 wugnot.Deposit(cross(cur))
331 testing.SetOriginSend(chain.Coins{})
332 wugnot.Approve(cross(cur), realmAddr, 1200000)
333 id := PostBounty(cross(cur), "Review Timeout Test", "Verify ExpireSubmission settles 100% after review deadline.", "realm", 1200000, 30)
334
335 testing.SetOriginCaller(hunter)
336 testing.SetRealm(testing.NewUserRealm(hunter))
337 ClaimBounty(cross(cur), id, "Working on review timeout test")
338 SubmitProof(cross(cur), id, "https://github.com/hearthbounty/v1/pull/7", "sha256:fedcba9876543210", "Delivered complete solution and test suite.")
339
340 // Cannot expire before ReviewDeadlineHeight.
341 testing.SetOriginCaller(bystander)
342 testing.SetRealm(testing.NewUserRealm(bystander))
343 uassert.AbortsWithMessage(t, cur, "hearthbounty: submission review window has not expired yet", func() {
344 ExpireSubmission(cross(cur), id)
345 })
346
347 // Advance to exact ReviewDeadlineHeight -> bystander triggers permissionless settlement.
348 hunterBefore := EscrowBalance(hunter)
349 testing.SkipHeights(30)
350 ExpireSubmission(cross(cur), id)
351
352 uassert.Equal(t, hunterBefore+1200000, EscrowBalance(hunter))
353 assertEscrowInvariant(t, realmAddr, baseRealmBal)
354 b, _ := GetBounty(id)
355 uassert.Equal(t, string(hearthcore.StatusSettled), string(b.Status))
356 uassert.Equal(t, int64(1200000), b.PayoutUgnot)
357}
358
359// M-1 proof #2 + H-1 pro-rata dispute proof:
360// - Arbiter cannot resolve a bounty where arbiter == sponsor or arbiter == claimant.
361// - Dispute split refunds third-party donors pro-rata instead of sending their refund to sponsor.
362// - ExpireDispute triggers a permissionless 50/50 split if the arbiter stalls or is conflicted.
363func TestDisputeSplitConflictGuardAndTimeoutFallback(cur realm, t *testing.T) {
364 arb := testutils.TestAddress("arbiter3")
365 arb2 := testutils.TestAddress("arbiter3_new")
366 sponsor := testutils.TestAddress("sponsor3")
367 donor := testutils.TestAddress("donor3")
368 hunter := testutils.TestAddress("hunter3")
369 bystander := testutils.TestAddress("bystander3")
370 realmAddr := chain.PackageAddress(testRealmPath)
371
372 testing.SetRealm(testing.NewUserRealm(arb))
373 resetState(arb)
374 baseRealmBal := EscrowBalance(realmAddr)
375
376 SetArbiter(cross(cur), arb2)
377 uassert.Equal(t, arb2, Arbiter())
378
379 // Bounty 1: 1.00 GNOT sponsor + 0.50 GNOT donor -> 70/30 dispute split.
380 testing.SetRealm(testing.NewUserRealm(sponsor))
381 prepCoins(sponsor, 1000000)
382 wugnot.Deposit(cross(cur))
383 testing.SetOriginSend(chain.Coins{})
384 wugnot.Approve(cross(cur), realmAddr, 1000000)
385 id1 := PostBounty(cross(cur), "Audit Interrealm Payment Guard", "Check all crossing guards and produce formal report.", "audit", 1000000, 40)
386
387 testing.SetRealm(testing.NewUserRealm(donor))
388 prepCoins(donor, 500000)
389 wugnot.Deposit(cross(cur))
390 testing.SetOriginSend(chain.Coins{})
391 wugnot.Approve(cross(cur), realmAddr, 500000)
392 TopUpBounty(cross(cur), id1, 500000)
393
394 testing.SetOriginCaller(hunter)
395 testing.SetRealm(testing.NewUserRealm(hunter))
396 ClaimBounty(cross(cur), id1, "Auditing now")
397 SubmitProof(cross(cur), id1, "https://github.com/hearthbounty/audit/blob/main/report.md", "sha256:abcdef0123456789", "Delivered partial audit covering 70% of scope.")
398 OpenDispute(cross(cur), id1, "Core findings delivered; requesting 70/30 partial settlement.")
399
400 testing.SetOriginCaller(arb)
401 testing.SetRealm(testing.NewUserRealm(arb))
402 uassert.AbortsWithMessage(t, cur, "hearthbounty: only the arbiter may resolve disputes", func() {
403 ResolveDispute(cross(cur), id1, 7000, "unauthorized")
404 })
405
406 hunterBalBefore := EscrowBalance(hunter)
407 sponsorBalBefore := EscrowBalance(sponsor)
408 donorBalBefore := EscrowBalance(donor)
409
410 testing.SetOriginCaller(arb2)
411 testing.SetRealm(testing.NewUserRealm(arb2))
412 ResolveDispute(cross(cur), id1, 7000, "70% of specification verified; 30% refunded pro-rata.")
413
414 // 70% of 1.50 GNOT = 1.05 GNOT hunter; 30% of 1.00 GNOT = 0.30 GNOT sponsor; 30% of 0.50 GNOT = 0.15 GNOT donor.
415 uassert.Equal(t, hunterBalBefore+1050000, EscrowBalance(hunter))
416 uassert.Equal(t, sponsorBalBefore+300000, EscrowBalance(sponsor))
417 uassert.Equal(t, donorBalBefore+150000, EscrowBalance(donor))
418 assertEscrowInvariant(t, realmAddr, baseRealmBal)
419
420 // Bounty 2: Arbiter IS the claimant -> conflicted arbiter blocked, ExpireDispute falls back to 50/50.
421 testing.SetRealm(testing.NewUserRealm(sponsor))
422 prepCoins(sponsor, 1000000)
423 wugnot.Deposit(cross(cur))
424 testing.SetOriginSend(chain.Coins{})
425 wugnot.Approve(cross(cur), realmAddr, 1000000)
426 id2 := PostBounty(cross(cur), "Conflicted Arbiter Test", "Verify conflicted arbiter cannot self-settle dispute.", "audit", 1000000, 25)
427
428 testing.SetOriginCaller(arb2)
429 testing.SetRealm(testing.NewUserRealm(arb2))
430 ClaimBounty(cross(cur), id2, "Arbiter claiming as hunter")
431 SubmitProof(cross(cur), id2, "https://github.com/hearthbounty/v1/pull/9", "sha256:1234567890abcdef", "Submitted by arbiter address.")
432 OpenDispute(cross(cur), id2, "Testing conflict of interest block.")
433
434 uassert.AbortsWithMessage(t, cur, "hearthbounty: arbiter cannot resolve a bounty they are party to", func() {
435 ResolveDispute(cross(cur), id2, 10000, "self payout attempt")
436 })
437
438 testing.SetOriginCaller(bystander)
439 testing.SetRealm(testing.NewUserRealm(bystander))
440 uassert.AbortsWithMessage(t, cur, "hearthbounty: dispute resolution window has not expired yet", func() {
441 ExpireDispute(cross(cur), id2)
442 })
443
444 arb2Before := EscrowBalance(arb2)
445 sponsorBefore2 := EscrowBalance(sponsor)
446 testing.SkipHeights(25)
447 ExpireDispute(cross(cur), id2)
448
449 uassert.Equal(t, arb2Before+500000, EscrowBalance(arb2))
450 uassert.Equal(t, sponsorBefore2+500000, EscrowBalance(sponsor))
451 assertEscrowInvariant(t, realmAddr, baseRealmBal)
452}
453
454// M-2 proof + security guards: Unicode U+0130 ('İ') must NEVER desync tag neutralization.
455func TestUnicodeNeutralizeGnoTagsAndSecurityGuards(cur realm, t *testing.T) {
456 arb := testutils.TestAddress("arbiter4")
457 sponsor := testutils.TestAddress("sponsor4")
458 realmAddr := chain.PackageAddress(testRealmPath)
459
460 testing.SetRealm(testing.NewUserRealm(arb))
461 resetState(arb)
462
463 // Direct NeutralizeGnoTags Unicode expansion vectors.
464 unicodePayload := "İ<gno-form exec=\"PostBounty\">\n</gno-form> İ<GNO-BUTTON href=\"/r/evil\" />"
465 cleaned := NeutralizeGnoTags(unicodePayload)
466 uassert.False(t, strings.Contains(cleaned, "<gno-form"))
467 uassert.False(t, strings.Contains(cleaned, "</gno-form"))
468 uassert.False(t, strings.Contains(cleaned, "<GNO-BUTTON"))
469
470 // ValidateProofURL enforces https:// only (rejects ipfs:// and http://).
471 uassert.Equal(t, "https://github.com/org/repo/pull/1", ValidateProofURL(" https://github.com/org/repo/pull/1 "))
472 uassert.PanicsWithMessage(t, cur, "hearthbounty: proof URL must start with https://", func() {
473 ValidateProofURL("ipfs://bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi")
474 })
475 uassert.PanicsWithMessage(t, cur, "hearthbounty: proof URL must start with https://", func() {
476 ValidateProofURL("http://insecure.example.com/proof")
477 })
478
479 // Cross-realm wrapper calls rejected by runtime.AssertOriginCall().
480 testing.SetOriginCaller(sponsor)
481 testing.SetRealm(testing.NewUserRealm(sponsor))
482 uassert.AbortsWithMessage(t, cur, "invalid non-origin call", func() {
483 PostBounty(cross(cur), "Cross Call", "Should abort because called through uassert closure.", "tool", 1000000, 100)
484 })
485
486 // Reject dust below 0.10 GNOT and non-wugnot token keys (such as uatom).
487 uassert.PanicsWithMessage(t, cur, "hearthbounty: escrow amount is below minimum required (100000 wugnot)", func() {
488 validateEscrowAmount(50000, hearthcore.MinRewardUgnot)
489 })
490 uassert.PanicsWithMessage(t, cur, "hearthbounty: only 1:1 ugnot-backed wugnot escrow is accepted", func() {
491 ValidateEscrowToken("uatom")
492 })
493 ValidateEscrowToken(EscrowToken())
494
495 // End-to-end Render() check with Turkish dotted İ + raw <gno-form> injection.
496 testing.SetRealm(testing.NewUserRealm(sponsor))
497 prepCoins(sponsor, 1500000)
498 wugnot.Deposit(cross(cur))
499 testing.SetOriginSend(chain.Coins{})
500 wugnot.Approve(cross(cur), realmAddr, 1500000)
501 id := PostBounty(
502 cross(cur),
503 `İ<gno-button href="/r/evil" label="Steal" /> $GNOT/$ATOM`,
504 "İ<gno-form exec=\"PostBounty\">\n</gno-form>\nNormal spec with $100 and <gno-form exec=\"Drain\"> attempt.",
505 "agent",
506 1500000,
507 100,
508 )
509
510 home := Render("")
511 uassert.True(t, strings.Contains(home, "HearthBounty v1"))
512 uassert.True(t, strings.Contains(home, "data:image/svg+xml;base64,"))
513 uassert.False(t, strings.Contains(home, `<gno-button href="/r/evil"`))
514 uassert.True(t, strings.Contains(home, `\$GNOT/\$ATOM`))
515
516 detail := Render("bounty/1")
517 uassert.True(t, strings.Contains(detail, "Bounty #1:"))
518 uassert.True(t, strings.Contains(detail, `<gno-form exec="ClaimBounty">`))
519 uassert.False(t, strings.Contains(detail, `<gno-form exec="PostBounty">`))
520 uassert.False(t, strings.Contains(detail, `<gno-form exec="Drain">`))
521
522 // Verify pagination, auxiliary routes, and canonical realm_path + escrow_token in ExportStatsJSON.
523 uassert.True(t, strings.Contains(Render("page/1"), "Bounty Board (Page 1)"))
524 uassert.True(t, strings.Contains(Render("post"), `<gno-form exec="PostBounty">`))
525 uassert.True(t, strings.Contains(Render("post"), `/r/gnoland/wugnot$help&func=Approve`))
526 uassert.True(t, strings.Contains(Render("leaderboard"), "Guild Reputation Leaderboard"))
527 uassert.True(t, strings.Contains(Render("agents"), "AI Agent"))
528 uassert.True(t, strings.Contains(Render("category/agent"), "AI Agent Bounties"))
529 uassert.True(t, strings.Contains(Render("profile/"+string(sponsor)), string(sponsor)))
530 uassert.True(t, strings.Contains(Render("unknown/path"), "Route Not Found"))
531
532 statsJSON := ExportStatsJSON()
533 uassert.True(t, strings.Contains(statsJSON, `"realm_path":"`+testRealmPath+`"`))
534 uassert.True(t, strings.Contains(statsJSON, `"escrow_token":"`+EscrowTokenKey+`"`))
535 uassert.True(t, strings.Contains(statsJSON, `"escrow_address":"`+string(realmAddr)+`"`))
536 uassert.True(t, strings.Contains(statsJSON, `"total_bounties":1`))
537 bountyJSON := ExportBountyJSON(id)
538 uassert.True(t, strings.Contains(bountyJSON, `"found":true`))
539 uassert.True(t, strings.Contains(bountyJSON, `"sponsor_funded_ugnot":1500000`))
540 listJSON := ExportBountiesJSON("open", "agent", 1000000, 10)
541 uassert.True(t, strings.Contains(listJSON, `"count":1`))
542 lbJSON := ExportLeaderboardJSON(10)
543 uassert.True(t, strings.Contains(lbJSON, `"count":1`))
544}