Skip to content

Commit e412120

Browse files
committed
add test to verify max_magnitude join
1 parent 44641ff commit e412120

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

pkg/rewards/operatorAllocationSnapshots_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,111 @@ func Test_OperatorAllocationSnapshots(t *testing.T) {
449449
assert.True(t, count > 0, "Expected allocation to round up based on effective block (2025-02-05) to 2025-02-06, not emission block")
450450
})
451451

452+
t.Run("Allocations change multiple times while max_magnitude stays constant", func(t *testing.T) {
453+
// This test verifies that allocations can change independently of max_magnitude
454+
455+
// Day 1 (2025-03-01): Set max_magnitude = 800 for strategy
456+
day1 := time.Date(2025, 3, 1, 10, 0, 0, 0, time.UTC)
457+
block1Num := uint64(800)
458+
res := grm.Exec(`
459+
INSERT INTO blocks (number, hash, block_time, block_date, state_root, created_at, updated_at)
460+
VALUES (?, ?, ?, ?, '', NOW(), NOW())
461+
`, block1Num, fmt.Sprintf("hash_%d", block1Num), day1, day1.Format("2006-01-02"))
462+
assert.Nil(t, res.Error)
463+
464+
// Insert max_magnitude event on day 1
465+
res = grm.Exec(`
466+
INSERT INTO operator_max_magnitudes (operator, strategy, max_magnitude, block_number, transaction_hash, log_index)
467+
VALUES (?, ?, ?, ?, ?, ?)
468+
`, "0xoperator8", "0xstrategy8", "800", block1Num, "tx_800", 1)
469+
assert.Nil(t, res.Error)
470+
471+
// Day 2 (2025-03-02): Allocate 100 (first allocation)
472+
day2 := time.Date(2025, 3, 2, 14, 0, 0, 0, time.UTC)
473+
block2Num := uint64(801)
474+
res = grm.Exec(`
475+
INSERT INTO blocks (number, hash, block_time, block_date, state_root, created_at, updated_at)
476+
VALUES (?, ?, ?, ?, '', NOW(), NOW())
477+
`, block2Num, fmt.Sprintf("hash_%d", block2Num), day2, day2.Format("2006-01-02"))
478+
assert.Nil(t, res.Error)
479+
480+
res = grm.Exec(`
481+
INSERT INTO operator_allocations (operator, avs, strategy, operator_set_id, magnitude, effective_block, block_number, transaction_hash, log_index, created_at, updated_at)
482+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
483+
`, "0xoperator8", "0xavs8", "0xstrategy8", 8, "100", block2Num, block2Num, "tx_801", 1)
484+
assert.Nil(t, res.Error)
485+
486+
// Day 4 (2025-03-04): Allocate 200 (allocation increases)
487+
day4 := time.Date(2025, 3, 4, 14, 0, 0, 0, time.UTC)
488+
block4Num := uint64(803)
489+
res = grm.Exec(`
490+
INSERT INTO blocks (number, hash, block_time, block_date, state_root, created_at, updated_at)
491+
VALUES (?, ?, ?, ?, '', NOW(), NOW())
492+
`, block4Num, fmt.Sprintf("hash_%d", block4Num), day4, day4.Format("2006-01-02"))
493+
assert.Nil(t, res.Error)
494+
495+
res = grm.Exec(`
496+
INSERT INTO operator_allocations (operator, avs, strategy, operator_set_id, magnitude, effective_block, block_number, transaction_hash, log_index, created_at, updated_at)
497+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
498+
`, "0xoperator8", "0xavs8", "0xstrategy8", 8, "200", block4Num, block4Num, "tx_803", 1)
499+
assert.Nil(t, res.Error)
500+
501+
// Day 6 (2025-03-06): Allocate 150 (allocation decreases)
502+
day6 := time.Date(2025, 3, 6, 14, 0, 0, 0, time.UTC)
503+
block6Num := uint64(805)
504+
res = grm.Exec(`
505+
INSERT INTO blocks (number, hash, block_time, block_date, state_root, created_at, updated_at)
506+
VALUES (?, ?, ?, ?, '', NOW(), NOW())
507+
`, block6Num, fmt.Sprintf("hash_%d", block6Num), day6, day6.Format("2006-01-02"))
508+
assert.Nil(t, res.Error)
509+
510+
res = grm.Exec(`
511+
INSERT INTO operator_allocations (operator, avs, strategy, operator_set_id, magnitude, effective_block, block_number, transaction_hash, log_index, created_at, updated_at)
512+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
513+
`, "0xoperator8", "0xavs8", "0xstrategy8", 8, "150", block6Num, block6Num, "tx_805", 1)
514+
assert.Nil(t, res.Error)
515+
516+
sog := stakerOperators.NewStakerOperatorGenerator(grm, l, cfg)
517+
calculator, err := NewRewardsCalculator(cfg, grm, nil, sog, sink, l)
518+
assert.Nil(t, err)
519+
520+
err = calculator.GenerateAndInsertOperatorAllocationSnapshots("2025-03-07")
521+
assert.Nil(t, err)
522+
523+
// Query all snapshots for this operator
524+
var snapshots []struct {
525+
Snapshot string
526+
Magnitude string
527+
MaxMagnitude string
528+
}
529+
res = grm.Raw(`
530+
SELECT snapshot, magnitude, max_magnitude
531+
FROM operator_allocation_snapshots
532+
WHERE operator = ? AND avs = ? AND strategy = ? AND operator_set_id = ?
533+
ORDER BY snapshot ASC
534+
`, "0xoperator8", "0xavs8", "0xstrategy8", 8).Scan(&snapshots)
535+
assert.Nil(t, res.Error)
536+
537+
// Verify exactly 3 snapshots
538+
assert.Equal(t, 3, len(snapshots), "Expected 3 snapshots")
539+
540+
// Verify each snapshot has correct magnitude and max_magnitude=800
541+
// Day 3: allocation 100, max_magnitude 800
542+
assert.Equal(t, "2025-03-03", snapshots[0].Snapshot, "First snapshot should be 2025-03-03 (day 2 allocation rounded up)")
543+
assert.Equal(t, "100", snapshots[0].Magnitude, "First snapshot should have magnitude 100")
544+
assert.Equal(t, "800", snapshots[0].MaxMagnitude, "First snapshot should have max_magnitude 800")
545+
546+
// Day 5: allocation 200, max_magnitude 800
547+
assert.Equal(t, "2025-03-05", snapshots[1].Snapshot, "Second snapshot should be 2025-03-05 (day 4 allocation rounded up)")
548+
assert.Equal(t, "200", snapshots[1].Magnitude, "Second snapshot should have magnitude 200")
549+
assert.Equal(t, "800", snapshots[1].MaxMagnitude, "Second snapshot should have max_magnitude 800")
550+
551+
// Day 6: allocation 150, max_magnitude 800 (decrease rounds down)
552+
assert.Equal(t, "2025-03-06", snapshots[2].Snapshot, "Third snapshot should be 2025-03-06 (day 6 allocation rounded down)")
553+
assert.Equal(t, "150", snapshots[2].Magnitude, "Third snapshot should have magnitude 150")
554+
assert.Equal(t, "800", snapshots[2].MaxMagnitude, "Third snapshot should have max_magnitude 800")
555+
})
556+
452557
t.Cleanup(func() {
453558
teardownOperatorAllocationSnapshot(dbFileName, cfg, grm, l)
454559
})

0 commit comments

Comments
 (0)