From 8d4427a8bc1276cb4f186d4c71bcc3da6f4939b7 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 18 Aug 2020 20:19:17 +0200 Subject: [PATCH] add best ticket reward to tests Signed-off-by: Jakub Sztandera --- chain/messagepool/selection_test.go | 33 +++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/chain/messagepool/selection_test.go b/chain/messagepool/selection_test.go index a1a287c25..e714fb77e 100644 --- a/chain/messagepool/selection_test.go +++ b/chain/messagepool/selection_test.go @@ -874,7 +874,7 @@ func TestOptimalMessageSelection3(t *testing.T) { } } -func testCompetitiveMessageSelection(t *testing.T, rng *rand.Rand, getPremium func() uint64) (float64, float64) { +func testCompetitiveMessageSelection(t *testing.T, rng *rand.Rand, getPremium func() uint64) (float64, float64, float64) { // in this test we use 300 actors and send 10 blocks of messages. // actors send with an randomly distributed premium dictated by the getPremium function. // a number of miners select with varying ticket quality and we compare the @@ -938,6 +938,7 @@ func testCompetitiveMessageSelection(t *testing.T, rng *rand.Rand, getPremium fu totalGreedyReward := 0.0 totalOptimalCapacity := 0.0 totalOptimalReward := 0.0 + totalBestTQReward := 0.0 const runs = 50 for i := 0; i < runs; i++ { // 2. optimal selection @@ -953,12 +954,18 @@ func testCompetitiveMessageSelection(t *testing.T, rng *rand.Rand, getPremium fu } optMsgs := make(map[cid.Cid]*types.SignedMessage) + bestTq := 0.0 + var bestMsgs []*types.SignedMessage for j := 0; j < nMiners; j++ { tq := rng.Float64() msgs, err := mp.SelectMessages(ts, tq) if err != nil { t.Fatal(err) } + if tq > bestTq { + bestMsgs = msgs + } + for _, m := range msgs { optMsgs[m.Cid()] = m } @@ -985,6 +992,13 @@ func testCompetitiveMessageSelection(t *testing.T, rng *rand.Rand, getPremium fu optReward.Add(optReward, mp.getGasReward(m, baseFee, ts)) } + bestTqReward := big.NewInt(0) + for _, m := range bestMsgs { + bestTqReward.Add(bestTqReward, mp.getGasReward(m, baseFee, ts)) + } + + totalBestTQReward += float64(bestTqReward.Uint64()) + nMinersBig := big.NewInt(int64(nMiners)) greedyAvgReward, _ := new(big.Rat).SetFrac(greedyReward, nMinersBig).Float64() totalGreedyReward += greedyAvgReward @@ -1001,10 +1015,11 @@ func testCompetitiveMessageSelection(t *testing.T, rng *rand.Rand, getPremium fu rewardBoost := totalOptimalReward / totalGreedyReward t.Logf("Average capacity boost: %f", capacityBoost) t.Logf("Average reward boost: %f", rewardBoost) + t.Logf("Average best tq reward: %f", totalBestTQReward/runs/1e18) logging.SetLogLevel("messagepool", "info") - return capacityBoost, rewardBoost + return capacityBoost, rewardBoost, totalBestTQReward / runs / 1e18 } func makeExpPremiumDistribution(rng *rand.Rand) func() uint64 { @@ -1022,35 +1037,41 @@ func makeZipfPremiumDistribution(rng *rand.Rand) func() uint64 { } func TestCompetitiveMessageSelectionExp(t *testing.T) { - var capacityBoost, rewardBoost float64 + var capacityBoost, rewardBoost, tqReward float64 seeds := []int64{1947, 1976, 2020, 2100, 10000, 143324, 432432, 131, 32, 45} for _, seed := range seeds { t.Log("running competitive message selection with Exponential premium distribution and seed", seed) rng := rand.New(rand.NewSource(seed)) - cb, rb := testCompetitiveMessageSelection(t, rng, makeExpPremiumDistribution(rng)) + cb, rb, tqR := testCompetitiveMessageSelection(t, rng, makeExpPremiumDistribution(rng)) capacityBoost += cb rewardBoost += rb + tqReward += tqR } capacityBoost /= float64(len(seeds)) rewardBoost /= float64(len(seeds)) + tqReward /= float64(len(seeds)) t.Logf("Average capacity boost across all seeds: %f", capacityBoost) t.Logf("Average reward boost across all seeds: %f", rewardBoost) + t.Logf("Average reward of best ticket across all seeds: %f", tqReward) } func TestCompetitiveMessageSelectionZipf(t *testing.T) { - var capacityBoost, rewardBoost float64 + var capacityBoost, rewardBoost, tqReward float64 seeds := []int64{1947, 1976, 2020, 2100, 10000, 143324, 432432, 131, 32, 45} for _, seed := range seeds { t.Log("running competitive message selection with Zipf premium distribution and seed", seed) rng := rand.New(rand.NewSource(seed)) - cb, rb := testCompetitiveMessageSelection(t, rng, makeZipfPremiumDistribution(rng)) + cb, rb, tqR := testCompetitiveMessageSelection(t, rng, makeZipfPremiumDistribution(rng)) capacityBoost += cb rewardBoost += rb + tqReward += tqR } + tqReward /= float64(len(seeds)) capacityBoost /= float64(len(seeds)) rewardBoost /= float64(len(seeds)) t.Logf("Average capacity boost across all seeds: %f", capacityBoost) t.Logf("Average reward boost across all seeds: %f", rewardBoost) + t.Logf("Average reward of best ticket across all seeds: %f", tqReward) }