From 5074b526d0c30b2540cb62018dfb05c767f99d79 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Thu, 3 Sep 2020 19:36:03 +0200 Subject: [PATCH 1/3] Fix calculation of GasReward in messagepool Signed-off-by: Jakub Sztandera --- chain/messagepool/selection.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/chain/messagepool/selection.go b/chain/messagepool/selection.go index 7f7babba0bd..6c0fcd26590 100644 --- a/chain/messagepool/selection.go +++ b/chain/messagepool/selection.go @@ -587,9 +587,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 { maxPremium := types.BigSub(msg.Message.GasFeeCap, baseFee) - if types.BigCmp(maxPremium, msg.Message.GasPremium) < 0 { + + if types.BigCmp(maxPremium, msg.Message.GasPremium) > 0 { maxPremium = msg.Message.GasPremium } + gasReward := abig.Mul(maxPremium, types.NewInt(uint64(msg.Message.GasLimit))) return gasReward.Int } From 8bb471bccd784159ee86081dc6cfd08cb56d2a27 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Thu, 3 Sep 2020 19:50:35 +0200 Subject: [PATCH 2/3] Add getGasPerf test Signed-off-by: Jakub Sztandera --- chain/messagepool/selection.go | 8 +++--- chain/messagepool/selection_test.go | 39 ++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/chain/messagepool/selection.go b/chain/messagepool/selection.go index 6c0fcd26590..7accb39ff3d 100644 --- a/chain/messagepool/selection.go +++ b/chain/messagepool/selection.go @@ -585,7 +585,7 @@ func (mp *MessagePool) getPendingMessages(curTs, ts *types.TipSet) (map[address. return result, nil } -func (mp *MessagePool) getGasReward(msg *types.SignedMessage, baseFee types.BigInt, ts *types.TipSet) *big.Int { +func (_ *MessagePool) getGasReward(msg *types.SignedMessage, baseFee types.BigInt) *big.Int { maxPremium := types.BigSub(msg.Message.GasFeeCap, baseFee) if types.BigCmp(maxPremium, msg.Message.GasPremium) > 0 { @@ -596,7 +596,7 @@ func (mp *MessagePool) getGasReward(msg *types.SignedMessage, baseFee types.BigI return gasReward.Int } -func (mp *MessagePool) getGasPerf(gasReward *big.Int, gasLimit int64) float64 { +func (_ *MessagePool) getGasPerf(gasReward *big.Int, gasLimit int64) float64 { // gasPerf = gasReward * build.BlockGasLimit / gasLimit a := new(big.Rat).SetInt(new(big.Int).Mul(gasReward, bigBlockGasLimit)) b := big.NewRat(1, gasLimit) @@ -674,7 +674,7 @@ func (mp *MessagePool) createMessageChains(actor address.Address, mset map[uint6 balance = new(big.Int).Sub(balance, value) } - gasReward := mp.getGasReward(m, baseFee, ts) + gasReward := mp.getGasReward(m, baseFee) rewards = append(rewards, gasReward) } @@ -778,7 +778,7 @@ func (mc *msgChain) Before(other *msgChain) bool { func (mc *msgChain) Trim(gasLimit int64, mp *MessagePool, baseFee types.BigInt, ts *types.TipSet) { i := len(mc.msgs) - 1 for i >= 0 && (mc.gasLimit > gasLimit || mc.gasPerf < 0) { - gasReward := mp.getGasReward(mc.msgs[i], baseFee, ts) + gasReward := mp.getGasReward(mc.msgs[i], baseFee) mc.gasReward = new(big.Int).Sub(mc.gasReward, gasReward) mc.gasLimit -= mc.msgs[i].Message.GasLimit if mc.gasLimit > 0 { diff --git a/chain/messagepool/selection_test.go b/chain/messagepool/selection_test.go index f22cd095d8b..b17ecc3edc1 100644 --- a/chain/messagepool/selection_test.go +++ b/chain/messagepool/selection_test.go @@ -2,6 +2,7 @@ package messagepool import ( "context" + "fmt" "math" "math/big" "math/rand" @@ -1055,17 +1056,17 @@ func testCompetitiveMessageSelection(t *testing.T, rng *rand.Rand, getPremium fu greedyReward := big.NewInt(0) for _, m := range greedyMsgs { - greedyReward.Add(greedyReward, mp.getGasReward(m, baseFee, ts)) + greedyReward.Add(greedyReward, mp.getGasReward(m, baseFee)) } optReward := big.NewInt(0) for _, m := range optMsgs { - optReward.Add(optReward, mp.getGasReward(m, baseFee, ts)) + optReward.Add(optReward, mp.getGasReward(m, baseFee)) } bestTqReward := big.NewInt(0) for _, m := range bestMsgs { - bestTqReward.Add(bestTqReward, mp.getGasReward(m, baseFee, ts)) + bestTqReward.Add(bestTqReward, mp.getGasReward(m, baseFee)) } totalBestTQReward += float64(bestTqReward.Uint64()) @@ -1146,3 +1147,35 @@ func TestCompetitiveMessageSelectionZipf(t *testing.T) { t.Logf("Average reward boost across all seeds: %f", rewardBoost) t.Logf("Average reward of best ticket across all seeds: %f", tqReward) } + +func TestGasReward(t *testing.T) { + tests := []struct { + Premium uint64 + FeeCap uint64 + BaseFee uint64 + GasReward int64 + }{ + {Premium: 100, FeeCap: 200, BaseFee: 100, GasReward: 100}, + {Premium: 100, FeeCap: 200, BaseFee: 210, GasReward: -10}, + {Premium: 200, FeeCap: 250, BaseFee: 210, GasReward: 40}, + {Premium: 200, FeeCap: 250, BaseFee: 2000, GasReward: -1750}, + } + + mp := new(MessagePool) + for _, test := range tests { + test := test + t.Run(fmt.Sprintf("%v", test), func(t *testing.T) { + msg := &types.SignedMessage{ + Message: types.Message{ + GasLimit: 10, + GasFeeCap: types.NewInt(test.FeeCap), + GasPremium: types.NewInt(test.Premium), + }, + } + rew := mp.getGasReward(msg, types.NewInt(test.BaseFee)) + if rew.Cmp(big.NewInt(test.GasReward*10)) != 0 { + t.Errorf("bad reward: expected %d, got %s", test.GasReward*10, rew) + } + }) + } +} From 8111b227c44077cc78900beb3d23bb035e71ce79 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Thu, 3 Sep 2020 19:53:49 +0200 Subject: [PATCH 3/3] Appease the linter Signed-off-by: Jakub Sztandera --- chain/messagepool/selection.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chain/messagepool/selection.go b/chain/messagepool/selection.go index 7accb39ff3d..5ba679d761a 100644 --- a/chain/messagepool/selection.go +++ b/chain/messagepool/selection.go @@ -585,7 +585,7 @@ func (mp *MessagePool) getPendingMessages(curTs, ts *types.TipSet) (map[address. return result, nil } -func (_ *MessagePool) getGasReward(msg *types.SignedMessage, baseFee types.BigInt) *big.Int { +func (*MessagePool) getGasReward(msg *types.SignedMessage, baseFee types.BigInt) *big.Int { maxPremium := types.BigSub(msg.Message.GasFeeCap, baseFee) if types.BigCmp(maxPremium, msg.Message.GasPremium) > 0 { @@ -596,7 +596,7 @@ func (_ *MessagePool) getGasReward(msg *types.SignedMessage, baseFee types.BigIn return gasReward.Int } -func (_ *MessagePool) getGasPerf(gasReward *big.Int, gasLimit int64) float64 { +func (*MessagePool) getGasPerf(gasReward *big.Int, gasLimit int64) float64 { // gasPerf = gasReward * build.BlockGasLimit / gasLimit a := new(big.Rat).SetInt(new(big.Int).Mul(gasReward, bigBlockGasLimit)) b := big.NewRat(1, gasLimit)