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.

idlewatt_test.gno

16.71 Kb · 497 lines
  1package idlewatt
  2
  3import (
  4	"strings"
  5	"testing"
  6	"time"
  7
  8	"gno.land/p/nt/avl/v0"
  9	"gno.land/p/nt/testutils/v0"
 10	"gno.land/p/nt/uassert/v0"
 11	"gno.land/p/nt/urequire/v0"
 12)
 13
 14var (
 15	alice = testutils.TestAddress("alice")
 16	bob   = testutils.TestAddress("bob")
 17	carol = testutils.TestAddress("carol")
 18	boss  = testutils.TestAddress("boss")
 19
 20	who address // the account the next call is signed by
 21)
 22
 23// reset puts the realm back to a clean state between tests.
 24func reset() {
 25	admin = boss
 26	paused = false
 27	onlineWindow = 30 * time.Minute
 28	nextNodeID = 1
 29	nextJobID = 1
 30	nodes = avl.NewTree()
 31	byOperator = avl.NewTree()
 32	leaderboard = avl.NewTree()
 33	jobs = avl.NewTree()
 34	nodeJobs = avl.NewTree()
 35	totalGrossCents = 0
 36	poolCents = 0
 37	poolPaidCents = 0
 38	bonusRounds = 0
 39	lastDistribution = time.Time{}
 40}
 41
 42func registerGPU(cur realm, op address, name string, rate int64) int {
 43	who = op
 44	testing.SetRealm(testing.NewUserRealm(who))
 45	return RegisterNode(cross(cur), name, KindHomeGPU, "RTX 4090 24GB", "US-NY Buffalo", PowerGrid, rate)
 46}
 47
 48// bringOnline heartbeats a node and makes it available.
 49func bringOnline(cur realm, op address, id int) {
 50	who = op
 51	testing.SetRealm(testing.NewUserRealm(who))
 52	Heartbeat(cross(cur), id, "")
 53	SetAvailability(cross(cur), id, ModeAvailable)
 54}
 55
 56func TestRegisterNode(cur realm, t *testing.T) {
 57	reset()
 58	who = alice
 59	testing.SetRealm(testing.NewUserRealm(who))
 60	id := RegisterNode(cross(cur), "Tonawanda Cybertruck (simulated)", KindVehicle, "AMD Ryzen infotainment + AI4", "US-NY Tonawanda", PowerPowershare, 30)
 61	uassert.Equal(t, 1, id)
 62	uassert.Equal(t, 1, NodeCount())
 63
 64	n := mustGetNode(id)
 65	uassert.Equal(t, alice, n.Operator)
 66	uassert.Equal(t, KindVehicle, n.Kind)
 67	uassert.Equal(t, ModePaused, n.Mode, "new nodes start paused")
 68	uassert.Equal(t, int64(30), n.RateCents)
 69	uassert.True(t, n.LastHeartbeat.IsZero())
 70	uassert.Equal(t, 1, operatorNodeCount(alice))
 71	uassert.Equal(t, 1, leaderboard.Size())
 72
 73	id2 := registerGPU(cur, bob, "Garage 4090", 45)
 74	uassert.Equal(t, 2, id2, "ids are sequential")
 75}
 76
 77func TestRegisterValidation(cur realm, t *testing.T) {
 78	reset()
 79	who = alice
 80	testing.SetRealm(testing.NewUserRealm(who))
 81	cases := []struct {
 82		name, kind, hw, region, power string
 83		rate                          int64
 84		want                          string
 85	}{
 86		{"ab", KindCPU, "Xeon", "US", PowerGrid, 10, "name must be 3-48 characters"},
 87		{strings.Repeat("x", 49), KindCPU, "Xeon", "US", PowerGrid, 10, "name must be 3-48 characters"},
 88		{" lead", KindCPU, "Xeon", "US", PowerGrid, 10, "name must not start or end with a space"},
 89		{"bad|pipe", KindCPU, "Xeon", "US", PowerGrid, 10, "name contains an unsupported character: only letters, digits, spaces and - . , ( ) ' / + : @ # % & = are allowed"},
 90		{"<script>", KindCPU, "Xeon", "US", PowerGrid, 10, "name contains an unsupported character: only letters, digits, spaces and - . , ( ) ' / + : @ # % & = are allowed"},
 91		{"[link](x)", KindCPU, "Xeon", "US", PowerGrid, 10, "name contains an unsupported character: only letters, digits, spaces and - . , ( ) ' / + : @ # % & = are allowed"},
 92		{"Box", "datacenter", "Xeon", "US", PowerGrid, 10, "kind must be one of: home-gpu, vehicle, cpu"},
 93		{"Box", KindCPU, "Xeon", "US", "coal", 10, "power must be one of: grid, solar, powershare-backup"},
 94		{"Box", KindCPU, "Xeon", "U", PowerGrid, 10, "region must be 2-40 characters"},
 95		{"Box", KindCPU, "X", "US", PowerGrid, 10, "hardware must be 3-80 characters"},
 96		{"Box", KindCPU, "Xeon", "US", PowerGrid, 0, "rate must be between 1 and 100000 US cents per hour"},
 97		{"Box", KindCPU, "Xeon", "US", PowerGrid, 100001, "rate must be between 1 and 100000 US cents per hour"},
 98	}
 99	for _, c := range cases {
100		urequire.AbortsWithMessage(t, cur, c.want, func() {
101			testing.SetRealm(testing.NewUserRealm(who))
102
103			RegisterNode(cross(cur), c.name, c.kind, c.hw, c.region, c.power, c.rate)
104		})
105	}
106	uassert.Equal(t, 0, NodeCount(), "no invalid node was stored")
107}
108
109func TestOperatorLimit(cur realm, t *testing.T) {
110	reset()
111	for i := 0; i < maxNodesPerOperator; i++ {
112		registerGPU(cur, alice, "Node number "+string(rune('A'+i)), 10)
113	}
114	who = alice
115	testing.SetRealm(testing.NewUserRealm(who))
116	urequire.AbortsWithMessage(t, cur, "an operator can register at most 10 nodes", func() {
117		testing.SetRealm(testing.NewUserRealm(who))
118
119		RegisterNode(cross(cur), "One too many", KindCPU, "Xeon", "US", PowerGrid, 10)
120	})
121	// Another operator is unaffected.
122	registerGPU(cur, bob, "Bob box", 10)
123	uassert.Equal(t, maxNodesPerOperator+1, NodeCount())
124}
125
126func TestAccessControl(cur realm, t *testing.T) {
127	reset()
128	id := registerGPU(cur, alice, "Alice box", 40)
129	who = bob
130	testing.SetRealm(testing.NewUserRealm(who))
131	const msg = "only the node's operator can do this"
132	urequire.AbortsWithMessage(t, cur, msg, func() {
133		testing.SetRealm(testing.NewUserRealm(who))
134		Heartbeat(cross(cur), id, "")
135	})
136	urequire.AbortsWithMessage(t, cur, msg, func() {
137		testing.SetRealm(testing.NewUserRealm(who))
138		SetAvailability(cross(cur), id, ModeAvailable)
139	})
140	urequire.AbortsWithMessage(t, cur, msg, func() {
141		testing.SetRealm(testing.NewUserRealm(who))
142		SetRate(cross(cur), id, 1)
143	})
144	urequire.AbortsWithMessage(t, cur, msg, func() {
145		testing.SetRealm(testing.NewUserRealm(who))
146
147		UpdateNode(cross(cur), id, "Stolen", "RTX", "US", PowerGrid, 1)
148	})
149	urequire.AbortsWithMessage(t, cur, msg, func() {
150		testing.SetRealm(testing.NewUserRealm(who))
151		RecordJob(cross(cur), id, "steal", 60)
152	})
153	urequire.AbortsWithMessage(t, cur, msg, func() {
154		testing.SetRealm(testing.NewUserRealm(who))
155		RemoveNode(cross(cur), id)
156	})
157	urequire.AbortsWithMessage(t, cur, "node 99 not found", func() {
158		testing.SetRealm(testing.NewUserRealm(who))
159		Heartbeat(cross(cur), 99, "")
160	})
161
162	n := mustGetNode(id)
163	uassert.Equal(t, int64(40), n.RateCents, "bob changed nothing")
164	uassert.Equal(t, "Alice box", n.Name)
165}
166
167func TestUpdateAndRate(cur realm, t *testing.T) {
168	reset()
169	id := registerGPU(cur, alice, "Alice box", 40)
170	who = alice
171	testing.SetRealm(testing.NewUserRealm(who))
172	UpdateNode(cross(cur), id, "Alice solar box", "RTX 3090", "US-NY Amherst", PowerSolar, 35)
173	n := mustGetNode(id)
174	uassert.Equal(t, "Alice solar box", n.Name)
175	uassert.Equal(t, PowerSolar, n.Power)
176	uassert.Equal(t, int64(35), n.RateCents)
177	SetRate(cross(cur), id, 50)
178	uassert.Equal(t, int64(50), n.RateCents)
179	urequire.AbortsWithMessage(t, cur, "rate must be between 1 and 100000 US cents per hour", func() {
180		testing.SetRealm(testing.NewUserRealm(who))
181
182		SetRate(cross(cur), id, -5)
183	})
184}
185
186func TestHeartbeatAndOnline(cur realm, t *testing.T) {
187	reset()
188	id := registerGPU(cur, alice, "Alice box", 40)
189	n := mustGetNode(id)
190	uassert.False(t, isOnlineAt(n, time.Now()), "no heartbeat yet")
191
192	who = alice
193	testing.SetRealm(testing.NewUserRealm(who))
194	Heartbeat(cross(cur), id, "GPU 41C")
195	uassert.True(t, isOnlineAt(n, time.Now()))
196	uassert.Equal(t, 1, n.HeartbeatCount)
197	uassert.Equal(t, "GPU 41C", n.LastNote)
198
199	urequire.AbortsWithMessage(t, cur, "heartbeat too frequent: wait at least 10 seconds", func() {
200		testing.SetRealm(testing.NewUserRealm(who))
201
202		Heartbeat(cross(cur), id, "")
203	})
204	urequire.AbortsContains(t, cur, "note contains an unsupported character", func() {
205		testing.SetRealm(testing.NewUserRealm(who))
206
207		testing.SkipHeights(3)
208		Heartbeat(cross(cur), id, "<b>")
209	})
210
211	// Steady heartbeats extend the session.
212	start := n.SessionStart
213	testing.SkipHeights(60) // 5 minutes
214	who = alice
215	testing.SetRealm(testing.NewUserRealm(who))
216	Heartbeat(cross(cur), id, "")
217	uassert.True(t, start.Equal(n.SessionStart), "session continues")
218	uassert.Equal(t, 2, n.HeartbeatCount)
219
220	// Silence longer than the online window: offline, and a new session.
221	testing.SkipHeights(12 * 31) // 31 minutes
222	uassert.False(t, isOnlineAt(n, time.Now()))
223	who = alice
224	testing.SetRealm(testing.NewUserRealm(who))
225	Heartbeat(cross(cur), id, "")
226	uassert.True(t, n.SessionStart.After(start), "session restarted")
227	uassert.True(t, isOnlineAt(n, time.Now()))
228}
229
230func TestAvailabilityModes(cur realm, t *testing.T) {
231	reset()
232	who = alice
233	testing.SetRealm(testing.NewUserRealm(who))
234	truck := RegisterNode(cross(cur), "Truck", KindVehicle, "AI4", "US-NY", PowerPowershare, 20)
235	gpu := RegisterNode(cross(cur), "Desk GPU", KindHomeGPU, "RTX 4090", "US-NY", PowerGrid, 40)
236
237	SetAvailability(cross(cur), truck, ModeDriving)
238	uassert.Equal(t, ModeDriving, mustGetNode(truck).Mode)
239	SetAvailability(cross(cur), truck, ModeAvailable)
240	uassert.Equal(t, ModeAvailable, mustGetNode(truck).Mode)
241
242	urequire.AbortsWithMessage(t, cur, "only vehicle nodes can be set to driving", func() {
243		testing.SetRealm(testing.NewUserRealm(who))
244
245		SetAvailability(cross(cur), gpu, ModeDriving)
246	})
247	urequire.AbortsWithMessage(t, cur, "mode must be one of: available, driving, paused", func() {
248		testing.SetRealm(testing.NewUserRealm(who))
249
250		SetAvailability(cross(cur), gpu, "sleeping")
251	})
252}
253
254func TestRecordJob(cur realm, t *testing.T) {
255	reset()
256	id := registerGPU(cur, alice, "Alice box", 45) // $0.45/h
257	who = alice
258	testing.SetRealm(testing.NewUserRealm(who))
259
260	urequire.AbortsWithMessage(t, cur, "node must be available (parked & plugged in) to take jobs", func() {
261		testing.SetRealm(testing.NewUserRealm(who))
262
263		RecordJob(cross(cur), id, "inference", 60)
264	})
265	SetAvailability(cross(cur), id, ModeAvailable)
266	urequire.AbortsWithMessage(t, cur, "node is offline: send a heartbeat first", func() {
267		testing.SetRealm(testing.NewUserRealm(who))
268
269		RecordJob(cross(cur), id, "inference", 60)
270	})
271	Heartbeat(cross(cur), id, "")
272	urequire.AbortsWithMessage(t, cur, "minutes must be between 1 and 1440", func() {
273		testing.SetRealm(testing.NewUserRealm(who))
274
275		RecordJob(cross(cur), id, "inference", 0)
276	})
277	urequire.AbortsWithMessage(t, cur, "minutes must be between 1 and 1440", func() {
278		testing.SetRealm(testing.NewUserRealm(who))
279
280		RecordJob(cross(cur), id, "inference", 1441)
281	})
282
283	jid := RecordJob(cross(cur), id, "llama-3.3-70b inference", 200) // 200 min at 45c/h = 150c
284	uassert.Equal(t, 1, jid)
285	j := jobs.Get(padID(jid)).(*Job)
286	uassert.Equal(t, int64(150), j.GrossCents)
287	uassert.Equal(t, int64(21), j.PoolCents) // 14% of 150
288	uassert.Equal(t, int64(129), j.OperatorCents)
289
290	n := mustGetNode(id)
291	uassert.Equal(t, 1, n.JobCount)
292	uassert.Equal(t, int64(200), n.JobMinutes)
293	uassert.Equal(t, int64(129), n.EarnedCents)
294	uassert.Equal(t, int64(150), totalGrossCents)
295	uassert.Equal(t, int64(21), poolCents)
296
297	// Driving vehicles can't take jobs.
298	who = alice
299	testing.SetRealm(testing.NewUserRealm(who))
300	truck := RegisterNode(cross(cur), "Truck", KindVehicle, "AI4", "US-NY", PowerPowershare, 20)
301	Heartbeat(cross(cur), truck, "")
302	SetAvailability(cross(cur), truck, ModeDriving)
303	urequire.AbortsWithMessage(t, cur, "node must be available (parked & plugged in) to take jobs", func() {
304		testing.SetRealm(testing.NewUserRealm(who))
305
306		RecordJob(cross(cur), truck, "inference", 30)
307	})
308}
309
310func TestLeaderboardOrder(cur realm, t *testing.T) {
311	reset()
312	a := registerGPU(cur, alice, "Small", 10)
313	b := registerGPU(cur, bob, "Big", 100)
314	c := registerGPU(cur, carol, "Idle", 50)
315	bringOnline(cur, alice, a)
316	bringOnline(cur, bob, b)
317
318	who = alice
319	testing.SetRealm(testing.NewUserRealm(who))
320	RecordJob(cross(cur), a, "batch", 600) // 100c gross
321	who = bob
322	testing.SetRealm(testing.NewUserRealm(who))
323	RecordJob(cross(cur), b, "batch", 600) // 1000c gross
324
325	var order []int
326	leaderboard.Iterate("", "", func(_ string, v any) bool {
327		order = append(order, v.(int))
328		return false
329	})
330	urequire.Equal(t, 3, len(order), "one leaderboard entry per node")
331	uassert.Equal(t, b, order[0])
332	uassert.Equal(t, a, order[1])
333	uassert.Equal(t, c, order[2])
334	uassert.Equal(t, 1, rankOf(mustGetNode(b)))
335	uassert.Equal(t, 3, rankOf(mustGetNode(c)))
336
337	// Small overtakes Big with more work; still one entry each.
338	who = alice
339	testing.SetRealm(testing.NewUserRealm(who))
340	for i := 0; i < 12; i++ {
341		RecordJob(cross(cur), a, "batch", 1440)
342	}
343	uassert.Equal(t, 3, leaderboard.Size())
344	uassert.Equal(t, 1, rankOf(mustGetNode(a)))
345}
346
347func TestDistributeBonus(cur realm, t *testing.T) {
348	reset()
349	a := registerGPU(cur, alice, "Small", 10)
350	b := registerGPU(cur, bob, "Big", 1000)
351	c := registerGPU(cur, carol, "Offline", 50)
352	bringOnline(cur, alice, a)
353	bringOnline(cur, bob, b)
354	who = carol
355	testing.SetRealm(testing.NewUserRealm(who))
356	SetAvailability(cross(cur), c, ModeAvailable) // available but never heartbeated
357
358	who = carol
359	testing.SetRealm(testing.NewUserRealm(who))
360	urequire.AbortsWithMessage(t, cur, "the fairness pool is empty", func() {
361		testing.SetRealm(testing.NewUserRealm(who))
362		DistributeBonus(cross(cur))
363	})
364
365	who = bob
366	testing.SetRealm(testing.NewUserRealm(who))
367	RecordJob(cross(cur), b, "training", 60) // 1000c gross, 140c to the pool
368	uassert.Equal(t, int64(140), poolCents)
369
370	who = carol
371	testing.SetRealm(testing.NewUserRealm(who)) // anyone can crank it
372	shared := DistributeBonus(cross(cur))
373	uassert.Equal(t, 2, shared, "only online + available nodes share")
374	uassert.Equal(t, int64(70), mustGetNode(a).BonusCents, "small node gets an equal share")
375	uassert.Equal(t, int64(70), mustGetNode(b).BonusCents)
376	uassert.Equal(t, int64(0), mustGetNode(c).BonusCents)
377	uassert.Equal(t, int64(0), poolCents)
378	uassert.Equal(t, int64(140), poolPaidCents)
379	uassert.Equal(t, 1, bonusRounds)
380
381	// Rate limited to once per hour.
382	who = bob
383	testing.SetRealm(testing.NewUserRealm(who))
384	RecordJob(cross(cur), b, "training", 1) // 16c gross, 2c pool
385	urequire.AbortsWithMessage(t, cur, "the bonus can be distributed at most once per hour", func() {
386		testing.SetRealm(testing.NewUserRealm(who))
387
388		DistributeBonus(cross(cur))
389	})
390
391	// After an hour with everyone offline, nobody is eligible.
392	testing.SkipHeights(12 * 61)
393	urequire.AbortsWithMessage(t, cur, "no node is online and available to share the pool", func() {
394		testing.SetRealm(testing.NewUserRealm(who))
395
396		DistributeBonus(cross(cur))
397	})
398
399	// A remainder smaller than one cent per node stays in the pool.
400	bringOnline(cur, alice, a)
401	bringOnline(cur, bob, b)
402	who = carol
403	testing.SetRealm(testing.NewUserRealm(who))
404	Heartbeat(cross(cur), c, "")
405	poolCents = 2
406	urequire.AbortsWithMessage(t, cur, "the pool is too small to split", func() {
407		testing.SetRealm(testing.NewUserRealm(who))
408
409		DistributeBonus(cross(cur))
410	})
411}
412
413func TestRemoveNode(cur realm, t *testing.T) {
414	reset()
415	id := registerGPU(cur, alice, "Alice box", 60)
416	bringOnline(cur, alice, id)
417	who = alice
418	testing.SetRealm(testing.NewUserRealm(who))
419	RecordJob(cross(cur), id, "render", 60)
420	RemoveNode(cross(cur), id)
421	uassert.Equal(t, 0, NodeCount())
422	uassert.Equal(t, 0, leaderboard.Size())
423	uassert.Equal(t, 0, operatorNodeCount(alice))
424	uassert.Equal(t, 1, jobs.Size(), "job history is kept")
425	uassert.Equal(t, "", NodeSummary(id))
426}
427
428func TestAdmin(cur realm, t *testing.T) {
429	reset()
430	id := registerGPU(cur, alice, "Spam node", 60)
431
432	who = alice
433	testing.SetRealm(testing.NewUserRealm(who))
434	urequire.AbortsWithMessage(t, cur, "only the admin can do this", func() {
435		testing.SetRealm(testing.NewUserRealm(who))
436		SetPaused(cross(cur), true)
437	})
438	urequire.AbortsWithMessage(t, cur, "only the admin can do this", func() {
439		testing.SetRealm(testing.NewUserRealm(who))
440		AdminRemoveNode(cross(cur), id)
441	})
442	urequire.AbortsWithMessage(t, cur, "only the admin can do this", func() {
443		testing.SetRealm(testing.NewUserRealm(who))
444		TransferAdmin(cross(cur), alice)
445	})
446	urequire.AbortsWithMessage(t, cur, "only the admin can do this", func() {
447		testing.SetRealm(testing.NewUserRealm(who))
448		SetOnlineWindowMinutes(cross(cur), 60)
449	})
450
451	who = boss
452	testing.SetRealm(testing.NewUserRealm(who))
453	SetPaused(cross(cur), true)
454	who = bob
455	testing.SetRealm(testing.NewUserRealm(who))
456	urequire.AbortsWithMessage(t, cur, "registrations are paused by the realm admin", func() {
457		testing.SetRealm(testing.NewUserRealm(who))
458
459		RegisterNode(cross(cur), "Late", KindCPU, "Xeon", "US", PowerGrid, 5)
460	})
461	who = boss
462	testing.SetRealm(testing.NewUserRealm(who))
463	SetPaused(cross(cur), false)
464	AdminRemoveNode(cross(cur), id)
465	uassert.Equal(t, 0, NodeCount())
466
467	urequire.AbortsWithMessage(t, cur, "online window must be between 5 and 10080 minutes", func() {
468		testing.SetRealm(testing.NewUserRealm(who))
469
470		SetOnlineWindowMinutes(cross(cur), 1)
471	})
472	SetOnlineWindowMinutes(cross(cur), 120)
473	uassert.True(t, onlineWindow == 2*time.Hour)
474
475	TransferAdmin(cross(cur), carol)
476	uassert.Equal(t, carol, Admin())
477	urequire.AbortsWithMessage(t, cur, "only the admin can do this", func() {
478		testing.SetRealm(testing.NewUserRealm(who))
479		SetPaused(cross(cur), true)
480	})
481}
482
483func TestStats(cur realm, t *testing.T) {
484	reset()
485	a := registerGPU(cur, alice, "A box", 60)
486	registerGPU(cur, bob, "B box", 60)
487	bringOnline(cur, alice, a)
488	who = alice
489	testing.SetRealm(testing.NewUserRealm(who))
490	RecordJob(cross(cur), a, "render", 120)
491	total, online, gross, pool := Stats()
492	uassert.Equal(t, 2, total)
493	uassert.Equal(t, 1, online)
494	uassert.Equal(t, int64(120), gross)
495	uassert.Equal(t, int64(16), pool)
496	uassert.True(t, strings.Contains(NodeSummary(a), "A box | home-gpu"))
497}