Skip to content

Commit

Permalink
Merge pull request #3580 from filecoin-project/fix/mpool-selection-bug
Browse files Browse the repository at this point in the history
fix selection bug; priority messages were not included if other chains were negative
  • Loading branch information
magik6k authored Sep 5, 2020
2 parents c074c8d + 5037282 commit 5e97e29
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
5 changes: 4 additions & 1 deletion chain/messagepool/messagepool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ type testMpoolAPI struct {
tipsets []*types.TipSet

published int

baseFee types.BigInt
}

func newTestMpoolAPI() *testMpoolAPI {
tma := &testMpoolAPI{
bmsgs: make(map[cid.Cid][]*types.SignedMessage),
statenonce: make(map[address.Address]uint64),
balance: make(map[address.Address]types.BigInt),
baseFee: types.NewInt(100),
}
genesis := mock.MkBlock(nil, 1, 1)
tma.tipsets = append(tma.tipsets, mock.TipSet(genesis))
Expand Down Expand Up @@ -182,7 +185,7 @@ func (tma *testMpoolAPI) LoadTipSet(tsk types.TipSetKey) (*types.TipSet, error)
}

func (tma *testMpoolAPI) ChainComputeBaseFee(ctx context.Context, ts *types.TipSet) (types.BigInt, error) {
return types.NewInt(100), nil
return tma.baseFee, nil
}

func assertNonce(t *testing.T, mp *MessagePool, addr address.Address, val uint64) {
Expand Down
4 changes: 2 additions & 2 deletions chain/messagepool/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (mp *MessagePool) selectMessagesOptimal(curTs, ts *types.TipSet, tq float64

if len(chains) != 0 && chains[0].gasPerf < 0 {
log.Warnw("all messages in mpool have non-positive gas performance", "bestGasPerf", chains[0].gasPerf)
return nil, nil
return result, nil
}

// 3. Parition chains into blocks (without trimming)
Expand Down Expand Up @@ -351,7 +351,7 @@ func (mp *MessagePool) selectMessagesGreedy(curTs, ts *types.TipSet) ([]*types.S

if len(chains) != 0 && chains[0].gasPerf < 0 {
log.Warnw("all messages in mpool have non-positive gas performance", "bestGasPerf", chains[0].gasPerf)
return nil, nil
return result, nil
}

// 3. Merge the head chains to produce the list of messages selected for inclusion, subject to
Expand Down
94 changes: 94 additions & 0 deletions chain/messagepool/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,100 @@ func TestPriorityMessageSelection2(t *testing.T) {
}
}

func TestPriorityMessageSelection3(t *testing.T) {
mp, tma := makeTestMpool()

// the actors
w1, err := wallet.NewWallet(wallet.NewMemKeyStore())
if err != nil {
t.Fatal(err)
}

a1, err := w1.GenerateKey(crypto.SigTypeSecp256k1)
if err != nil {
t.Fatal(err)
}

w2, err := wallet.NewWallet(wallet.NewMemKeyStore())
if err != nil {
t.Fatal(err)
}

a2, err := w2.GenerateKey(crypto.SigTypeSecp256k1)
if err != nil {
t.Fatal(err)
}

block := tma.nextBlock()
ts := mock.TipSet(block)
tma.applyBlock(t, block)

gasLimit := gasguess.Costs[gasguess.CostKey{Code: builtin.StorageMarketActorCodeID, M: 2}]

tma.setBalance(a1, 1) // in FIL
tma.setBalance(a2, 1) // in FIL

mp.cfg.PriorityAddrs = []address.Address{a1}

tma.baseFee = types.NewInt(1000)
nMessages := 10
for i := 0; i < nMessages; i++ {
bias := (nMessages - i) / 3
m := makeTestMessage(w1, a1, a2, uint64(i), gasLimit, uint64(1000+i%3+bias))
mustAdd(t, mp, m)
// messages from a2 have negative performance
m = makeTestMessage(w2, a2, a1, uint64(i), gasLimit, 100)
mustAdd(t, mp, m)
}

// test greedy selection
msgs, err := mp.SelectMessages(ts, 1.0)
if err != nil {
t.Fatal(err)
}

expectedMsgs := 10
if len(msgs) != expectedMsgs {
t.Fatalf("expected %d messages but got %d", expectedMsgs, len(msgs))
}

// all messages must be from a1
nextNonce := uint64(0)
for _, m := range msgs {
if m.Message.From != a1 {
t.Fatal("expected messages from a1 before messages from a2")
}
if m.Message.Nonce != nextNonce {
t.Fatalf("expected nonce %d but got %d", nextNonce, m.Message.Nonce)
}
nextNonce++
}

// test optimal selection
msgs, err = mp.SelectMessages(ts, 0.1)
if err != nil {
t.Fatal(err)
}

expectedMsgs = 10
if len(msgs) != expectedMsgs {
t.Fatalf("expected %d messages but got %d", expectedMsgs, len(msgs))
}

// all messages must be from a1
nextNonce = uint64(0)
for _, m := range msgs {
if m.Message.From != a1 {
t.Fatal("expected messages from a1 before messages from a2")
}
if m.Message.Nonce != nextNonce {
t.Fatalf("expected nonce %d but got %d", nextNonce, m.Message.Nonce)
}
nextNonce++
}

}

func TestOptimalMessageSelection1(t *testing.T) {
// this test uses just a single actor sending messages with a low tq
// the chain depenent merging algorithm should pick messages from the actor
Expand Down

0 comments on commit 5e97e29

Please sign in to comment.