add best ticket reward to tests

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-08-18 20:19:17 +02:00
parent 8bca545cab
commit 8d4427a8bc
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA

View File

@ -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. // 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. // 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 // 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 totalGreedyReward := 0.0
totalOptimalCapacity := 0.0 totalOptimalCapacity := 0.0
totalOptimalReward := 0.0 totalOptimalReward := 0.0
totalBestTQReward := 0.0
const runs = 50 const runs = 50
for i := 0; i < runs; i++ { for i := 0; i < runs; i++ {
// 2. optimal selection // 2. optimal selection
@ -953,12 +954,18 @@ func testCompetitiveMessageSelection(t *testing.T, rng *rand.Rand, getPremium fu
} }
optMsgs := make(map[cid.Cid]*types.SignedMessage) optMsgs := make(map[cid.Cid]*types.SignedMessage)
bestTq := 0.0
var bestMsgs []*types.SignedMessage
for j := 0; j < nMiners; j++ { for j := 0; j < nMiners; j++ {
tq := rng.Float64() tq := rng.Float64()
msgs, err := mp.SelectMessages(ts, tq) msgs, err := mp.SelectMessages(ts, tq)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if tq > bestTq {
bestMsgs = msgs
}
for _, m := range msgs { for _, m := range msgs {
optMsgs[m.Cid()] = m 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)) 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)) nMinersBig := big.NewInt(int64(nMiners))
greedyAvgReward, _ := new(big.Rat).SetFrac(greedyReward, nMinersBig).Float64() greedyAvgReward, _ := new(big.Rat).SetFrac(greedyReward, nMinersBig).Float64()
totalGreedyReward += greedyAvgReward totalGreedyReward += greedyAvgReward
@ -1001,10 +1015,11 @@ func testCompetitiveMessageSelection(t *testing.T, rng *rand.Rand, getPremium fu
rewardBoost := totalOptimalReward / totalGreedyReward rewardBoost := totalOptimalReward / totalGreedyReward
t.Logf("Average capacity boost: %f", capacityBoost) t.Logf("Average capacity boost: %f", capacityBoost)
t.Logf("Average reward boost: %f", rewardBoost) t.Logf("Average reward boost: %f", rewardBoost)
t.Logf("Average best tq reward: %f", totalBestTQReward/runs/1e18)
logging.SetLogLevel("messagepool", "info") logging.SetLogLevel("messagepool", "info")
return capacityBoost, rewardBoost return capacityBoost, rewardBoost, totalBestTQReward / runs / 1e18
} }
func makeExpPremiumDistribution(rng *rand.Rand) func() uint64 { func makeExpPremiumDistribution(rng *rand.Rand) func() uint64 {
@ -1022,35 +1037,41 @@ func makeZipfPremiumDistribution(rng *rand.Rand) func() uint64 {
} }
func TestCompetitiveMessageSelectionExp(t *testing.T) { 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} seeds := []int64{1947, 1976, 2020, 2100, 10000, 143324, 432432, 131, 32, 45}
for _, seed := range seeds { for _, seed := range seeds {
t.Log("running competitive message selection with Exponential premium distribution and seed", seed) t.Log("running competitive message selection with Exponential premium distribution and seed", seed)
rng := rand.New(rand.NewSource(seed)) rng := rand.New(rand.NewSource(seed))
cb, rb := testCompetitiveMessageSelection(t, rng, makeExpPremiumDistribution(rng)) cb, rb, tqR := testCompetitiveMessageSelection(t, rng, makeExpPremiumDistribution(rng))
capacityBoost += cb capacityBoost += cb
rewardBoost += rb rewardBoost += rb
tqReward += tqR
} }
capacityBoost /= float64(len(seeds)) capacityBoost /= float64(len(seeds))
rewardBoost /= float64(len(seeds)) rewardBoost /= float64(len(seeds))
tqReward /= float64(len(seeds))
t.Logf("Average capacity boost across all seeds: %f", capacityBoost) t.Logf("Average capacity boost across all seeds: %f", capacityBoost)
t.Logf("Average reward boost across all seeds: %f", rewardBoost) 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) { 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} seeds := []int64{1947, 1976, 2020, 2100, 10000, 143324, 432432, 131, 32, 45}
for _, seed := range seeds { for _, seed := range seeds {
t.Log("running competitive message selection with Zipf premium distribution and seed", seed) t.Log("running competitive message selection with Zipf premium distribution and seed", seed)
rng := rand.New(rand.NewSource(seed)) rng := rand.New(rand.NewSource(seed))
cb, rb := testCompetitiveMessageSelection(t, rng, makeZipfPremiumDistribution(rng)) cb, rb, tqR := testCompetitiveMessageSelection(t, rng, makeZipfPremiumDistribution(rng))
capacityBoost += cb capacityBoost += cb
rewardBoost += rb rewardBoost += rb
tqReward += tqR
} }
tqReward /= float64(len(seeds))
capacityBoost /= float64(len(seeds)) capacityBoost /= float64(len(seeds))
rewardBoost /= float64(len(seeds)) rewardBoost /= float64(len(seeds))
t.Logf("Average capacity boost across all seeds: %f", capacityBoost) t.Logf("Average capacity boost across all seeds: %f", capacityBoost)
t.Logf("Average reward boost across all seeds: %f", rewardBoost) t.Logf("Average reward boost across all seeds: %f", rewardBoost)
t.Logf("Average reward of best ticket across all seeds: %f", tqReward)
} }