Fix getGasReward and Trim

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-08-11 17:18:46 +02:00
parent 4da3aedacb
commit 56e3f7da7d
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA
2 changed files with 52 additions and 40 deletions

View File

@ -111,13 +111,14 @@ func (mp *MessagePool) selectMessagesOptimal(curTs, ts *types.TipSet, tq float64
gasLimit := int64(build.BlockGasLimit) gasLimit := int64(build.BlockGasLimit)
for nextChain < len(chains) { for nextChain < len(chains) {
chain := chains[nextChain] chain := chains[nextChain]
partitions[i] = append(partitions[i], chain)
nextChain++ nextChain++
partitions[i] = append(partitions[i], chain)
gasLimit -= chain.gasLimit gasLimit -= chain.gasLimit
if gasLimit < minGas { if gasLimit < minGas {
break break
} }
} }
} }
// 4. Compute effective performance for each chain, based on the partition they fall into // 4. Compute effective performance for each chain, based on the partition they fall into
@ -606,11 +607,11 @@ func (mp *MessagePool) getPendingMessages(curTs, ts *types.TipSet) (map[address.
} }
func (mp *MessagePool) getGasReward(msg *types.SignedMessage, baseFee types.BigInt, ts *types.TipSet) *big.Int { func (mp *MessagePool) getGasReward(msg *types.SignedMessage, baseFee types.BigInt, ts *types.TipSet) *big.Int {
gasReward := abig.Mul(msg.Message.GasPremium, types.NewInt(uint64(msg.Message.GasLimit))) maxPremium := types.BigSub(msg.Message.GasFeeCap, baseFee)
maxReward := types.BigSub(msg.Message.GasFeeCap, baseFee) if types.BigCmp(maxPremium, msg.Message.GasPremium) < 0 {
if types.BigCmp(maxReward, gasReward) < 0 { maxPremium = msg.Message.GasPremium
gasReward = maxReward
} }
gasReward := abig.Mul(maxPremium, types.NewInt(uint64(msg.Message.GasLimit)))
return gasReward.Int return gasReward.Int
} }
@ -795,7 +796,13 @@ func (mc *msgChain) Trim(gasLimit int64, mp *MessagePool, baseFee types.BigInt,
mc.gasReward = new(big.Int).Sub(mc.gasReward, gasReward) mc.gasReward = new(big.Int).Sub(mc.gasReward, gasReward)
mc.gasLimit -= mc.msgs[i].Message.GasLimit mc.gasLimit -= mc.msgs[i].Message.GasLimit
if mc.gasLimit > 0 { if mc.gasLimit > 0 {
bp := 1.0
if mc.effPerf != 0 {
bp = mc.effPerf / mc.gasPerf
}
mc.gasPerf = mp.getGasPerf(mc.gasReward, mc.gasLimit) mc.gasPerf = mp.getGasPerf(mc.gasReward, mc.gasLimit)
mc.effPerf = bp * mc.gasPerf
} else { } else {
mc.gasPerf = 0 mc.gasPerf = 0
} }

View File

@ -20,6 +20,7 @@ import (
_ "github.com/filecoin-project/lotus/lib/sigs/bls" _ "github.com/filecoin-project/lotus/lib/sigs/bls"
_ "github.com/filecoin-project/lotus/lib/sigs/secp" _ "github.com/filecoin-project/lotus/lib/sigs/secp"
logging "github.com/ipfs/go-log"
) )
func makeTestMessage(w *wallet.Wallet, from, to address.Address, nonce uint64, gasLimit int64, gasPrice uint64) *types.SignedMessage { func makeTestMessage(w *wallet.Wallet, from, to address.Address, nonce uint64, gasLimit int64, gasPrice uint64) *types.SignedMessage {
@ -930,44 +931,48 @@ func testCompetitiveMessageSelection(t *testing.T, rng *rand.Rand) {
nMiners = 1 nMiners = 1
} }
optMsgs := make(map[cid.Cid]*types.SignedMessage) logging.SetLogLevel("messagepool", "error")
for i := 0; i < nMiners; i++ { for i := 0; i < 1; i++ {
tq := rng.Float64() optMsgs := make(map[cid.Cid]*types.SignedMessage)
msgs, err := mp.SelectMessages(ts, tq) for j := 0; j < nMiners; j++ {
if err != nil { tq := rng.Float64()
t.Fatal(err) msgs, err := mp.SelectMessages(ts, tq)
if err != nil {
t.Fatal(err)
}
for _, m := range msgs {
optMsgs[m.Cid()] = m
}
} }
for _, m := range msgs {
optMsgs[m.Cid()] = m t.Logf("nMiners: %d", nMiners)
t.Logf("greedy capacity %d, optimal capacity %d (x%.1f)", len(greedyMsgs),
len(optMsgs), float64(len(optMsgs))/float64(len(greedyMsgs)))
if len(greedyMsgs) > len(optMsgs) {
t.Fatal("greedy capacity higher than optimal capacity; wtf")
}
greedyReward := big.NewInt(0)
for _, m := range greedyMsgs {
greedyReward.Add(greedyReward, mp.getGasReward(m, baseFee, ts))
}
optReward := big.NewInt(0)
for _, m := range optMsgs {
optReward.Add(optReward, mp.getGasReward(m, baseFee, ts))
}
nMinersBig := big.NewInt(int64(nMiners))
greedyAvgReward, _ := new(big.Rat).SetFrac(greedyReward, nMinersBig).Float64()
optimalAvgReward, _ := new(big.Rat).SetFrac(optReward, nMinersBig).Float64()
t.Logf("greedy reward: %.0f, optimal reward: %.0f (x%.1f)", greedyAvgReward,
optimalAvgReward, optimalAvgReward/greedyAvgReward)
if greedyReward.Cmp(optReward) > 0 {
t.Fatal("greedy reward raw higher than optimal reward; booh")
} }
} }
logging.SetLogLevel("messagepool", "info")
t.Logf("nMiners: %d", nMiners)
t.Logf("greedy capacity %d, optimal capacity %d (x%.1f)", len(greedyMsgs),
len(optMsgs), float64(len(optMsgs))/float64(len(greedyMsgs)))
if len(greedyMsgs) > len(optMsgs) {
t.Fatal("greedy capacity higher than optimal capacity; wtf")
}
greedyReward := big.NewInt(0)
for _, m := range greedyMsgs {
greedyReward.Add(greedyReward, mp.getGasReward(m, baseFee, ts))
}
optReward := big.NewInt(0)
for _, m := range optMsgs {
optReward.Add(optReward, mp.getGasReward(m, baseFee, ts))
}
nMinersBig := big.NewInt(int64(nMiners))
greedyAvgReward, _ := new(big.Rat).SetFrac(greedyReward, nMinersBig).Float64()
optimalAvgReward, _ := new(big.Rat).SetFrac(optReward, nMinersBig).Float64()
t.Logf("greedy reward: %f, optimal reward: %f (x%.1f)", greedyAvgReward,
optimalAvgReward, optimalAvgReward/greedyAvgReward)
if greedyReward.Cmp(optReward) > 0 {
t.Fatal("greedy reward raw higher than optimal reward; booh")
}
} }
func TestCompetitiveMessageSelection(t *testing.T) { func TestCompetitiveMessageSelection(t *testing.T) {