From 3e23b3653b24f696ec2da8984505ec2efaf8f348 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 4 Aug 2020 02:36:16 +0200 Subject: [PATCH 1/3] Correctly account for gas of included messages Signed-off-by: Jakub Sztandera --- miner/selectmessages.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/miner/selectmessages.go b/miner/selectmessages.go index 65f0154cb..a20625d34 100644 --- a/miner/selectmessages.go +++ b/miner/selectmessages.go @@ -29,6 +29,7 @@ func SelectMessages(ctx context.Context, al gasguess.ActorLookup, ts *types.TipS gasReward []abi.TokenAmount gasLimit []int64 + gasOffset int64 msgs []*types.SignedMessage } @@ -155,7 +156,7 @@ func SelectMessages(ctx context.Context, al gasguess.ActorLookup, ts *types.TipS continue } for n := range meta.msgs { - if meta.gasLimit[n] > gasLimitLeft { + if meta.gasLimit[n]-meta.gasOffset > gasLimitLeft { break } @@ -164,7 +165,7 @@ func SelectMessages(ctx context.Context, al gasguess.ActorLookup, ts *types.TipS } gasToReward, _ := new(big2.Float).SetInt(meta.gasReward[n].Int).Float64() - gasToReward /= float64(meta.gasLimit[n]) + gasToReward /= float64(meta.gasLimit[n] - meta.gasOffset) if gasToReward >= bestGasToReward { bestSender = sender @@ -180,8 +181,9 @@ func SelectMessages(ctx context.Context, al gasguess.ActorLookup, ts *types.TipS { out = append(out, outBySender[bestSender].msgs[:nBest]...) - gasLimitLeft -= outBySender[bestSender].gasLimit[nBest-1] + gasLimitLeft -= outBySender[bestSender].gasLimit[nBest-1] - outBySender[bestSender].gasOffset + outBySender[bestSender].gasOffset += outBySender[bestSender].gasLimit[nBest-1] outBySender[bestSender].msgs = outBySender[bestSender].msgs[nBest:] outBySender[bestSender].gasLimit = outBySender[bestSender].gasLimit[nBest:] outBySender[bestSender].gasReward = outBySender[bestSender].gasReward[nBest:] From 50ae04e6b72dc848f61e323d9886b238a3f7fc38 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Mon, 3 Aug 2020 17:51:22 -0700 Subject: [PATCH 2/3] add test to show that select messages isnt working as expected Signed-off-by: Jakub Sztandera --- miner/miner_test.go | 71 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/miner/miner_test.go b/miner/miner_test.go index c6741531b..2c07c542e 100644 --- a/miner/miner_test.go +++ b/miner/miner_test.go @@ -170,3 +170,74 @@ func wrapMsgs(msgs []types.Message) []*types.SignedMessage { } return out } + +func TestMessageFilteringSenderHopping(t *testing.T) { + ctx := context.TODO() + a1 := mustIDAddr(1) + a2 := mustIDAddr(2) + + actors := map[address.Address]*types.Actor{ + a1: { + Nonce: 0, + Balance: types.FromFil(1200), + }, + a2: { + Nonce: 0, + Balance: types.FromFil(1000), + }, + } + + af := func(ctx context.Context, addr address.Address, tsk types.TipSetKey) (*types.Actor, error) { + return actors[addr], nil + } + + msgs := []types.Message{ + { + From: a1, + To: a1, + Nonce: 0, + Value: types.FromFil(500), + GasLimit: 2_000_000_000, + GasPrice: types.NewInt(100), + }, + { + From: a1, + To: a1, + Nonce: 1, + Value: types.FromFil(500), + GasLimit: 2_000_000_000, + GasPrice: types.NewInt(1), + }, + { + From: a2, + To: a1, + Nonce: 0, + Value: types.FromFil(1), + GasLimit: 1_000_000_000, + GasPrice: types.NewInt(10), + }, + { + From: a2, + To: a1, + Nonce: 1, + Value: types.FromFil(1), + GasLimit: 1_000_000_000, + GasPrice: types.NewInt(1), + }, + { + From: a2, + To: a1, + Nonce: 2, + Value: types.FromFil(1), + GasLimit: 100_000_000, + GasPrice: types.NewInt(1), + }, + } + + outmsgs, err := SelectMessages(ctx, af, &types.TipSet{}, wrapMsgs(msgs)) + if err != nil { + t.Fatal(err) + } + + assert.Len(t, outmsgs, 5, "filtering didnt work as expected") +} From 57b931b06a98a59faa3b177b7065568aa7a21af9 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Mon, 3 Aug 2020 18:24:56 -0700 Subject: [PATCH 3/3] apply less nice but working fix --- miner/selectmessages.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/miner/selectmessages.go b/miner/selectmessages.go index a20625d34..834a955f2 100644 --- a/miner/selectmessages.go +++ b/miner/selectmessages.go @@ -29,7 +29,6 @@ func SelectMessages(ctx context.Context, al gasguess.ActorLookup, ts *types.TipS gasReward []abi.TokenAmount gasLimit []int64 - gasOffset int64 msgs []*types.SignedMessage } @@ -88,6 +87,7 @@ func SelectMessages(ctx context.Context, al gasguess.ActorLookup, ts *types.TipS getbal += build.Clock.Since(getBalStart) if inclBalances[from].LessThan(msg.Message.RequiredFunds()) { + log.Warnf("message had too low funds: %s %d %s %s %s", from, msg.Message.Nonce, inclBalances[from], msg.Message.Value, msg.Message.RequiredFunds()) tooLowFundMsgs++ // todo: drop from mpool continue @@ -156,7 +156,7 @@ func SelectMessages(ctx context.Context, al gasguess.ActorLookup, ts *types.TipS continue } for n := range meta.msgs { - if meta.gasLimit[n]-meta.gasOffset > gasLimitLeft { + if meta.gasLimit[n] > gasLimitLeft { break } @@ -165,7 +165,7 @@ func SelectMessages(ctx context.Context, al gasguess.ActorLookup, ts *types.TipS } gasToReward, _ := new(big2.Float).SetInt(meta.gasReward[n].Int).Float64() - gasToReward /= float64(meta.gasLimit[n] - meta.gasOffset) + gasToReward /= float64(meta.gasLimit[n]) if gasToReward >= bestGasToReward { bestSender = sender @@ -176,18 +176,27 @@ func SelectMessages(ctx context.Context, al gasguess.ActorLookup, ts *types.TipS } if nBest == 0 { + log.Warning("nBest is zero: ", gasLimitLeft) break // block gas limit reached } { out = append(out, outBySender[bestSender].msgs[:nBest]...) - gasLimitLeft -= outBySender[bestSender].gasLimit[nBest-1] - outBySender[bestSender].gasOffset - outBySender[bestSender].gasOffset += outBySender[bestSender].gasLimit[nBest-1] + seqGasLimit := outBySender[bestSender].gasLimit[nBest-1] + seqGasReward := outBySender[bestSender].gasReward[nBest-1] + + gasLimitLeft -= seqGasLimit + outBySender[bestSender].msgs = outBySender[bestSender].msgs[nBest:] outBySender[bestSender].gasLimit = outBySender[bestSender].gasLimit[nBest:] outBySender[bestSender].gasReward = outBySender[bestSender].gasReward[nBest:] + for i := range outBySender[bestSender].gasLimit { + outBySender[bestSender].gasLimit[i] -= seqGasLimit + outBySender[bestSender].gasReward[i] = big.Sub(outBySender[bestSender].gasReward[i], seqGasReward) + } + if len(outBySender[bestSender].msgs) == 0 { delete(outBySender, bestSender) }