Mempool: add selection tests
This commit is contained in:
parent
1f1e341c36
commit
012efed117
@ -63,6 +63,10 @@ func (mp *MessagePool) SelectMessages(ctx context.Context, ts *types.TipSet, tq
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if sm == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// one last sanity check
|
||||
if len(sm.msgs) > build.BlockMessageLimit {
|
||||
sm.msgs = sm.msgs[:build.BlockMessageLimit]
|
||||
|
@ -13,6 +13,10 @@ import (
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/filecoin-project/go-state-types/crypto"
|
||||
|
||||
cbg "github.com/whyrusleeping/cbor-gen"
|
||||
|
||||
"github.com/filecoin-project/go-address"
|
||||
"github.com/ipfs/go-cid"
|
||||
"github.com/ipfs/go-datastore"
|
||||
@ -527,7 +531,7 @@ func TestBasicMessageSelection(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageSelectionTrimming(t *testing.T) {
|
||||
func TestMessageSelectionTrimmingGas(t *testing.T) {
|
||||
mp, tma := makeTestMpool()
|
||||
|
||||
// the actors
|
||||
@ -590,6 +594,199 @@ func TestMessageSelectionTrimming(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestMessageSelectionTrimmingMsgsBasic(t *testing.T) {
|
||||
mp, tma := makeTestMpool()
|
||||
|
||||
// the actors
|
||||
w1, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
a1, err := w1.WalletNew(context.Background(), types.KTSecp256k1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
block := tma.nextBlock()
|
||||
ts := mock.TipSet(block)
|
||||
tma.applyBlock(t, block)
|
||||
|
||||
tma.setBalance(a1, 1) // in FIL
|
||||
|
||||
// create a larger than selectable chain
|
||||
for i := 0; i < build.BlockMessageLimit; i++ {
|
||||
m := makeTestMessage(w1, a1, a1, uint64(i), 300000, 100)
|
||||
mustAdd(t, mp, m)
|
||||
}
|
||||
|
||||
msgs, err := mp.SelectMessages(context.Background(), ts, 1.0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expected := cbg.MaxLength
|
||||
if len(msgs) != expected {
|
||||
t.Fatalf("expected %d messages, but got %d", expected, len(msgs))
|
||||
}
|
||||
|
||||
mGasLimit := int64(0)
|
||||
for _, m := range msgs {
|
||||
mGasLimit += m.Message.GasLimit
|
||||
}
|
||||
if mGasLimit > build.BlockGasLimit {
|
||||
t.Fatal("selected messages gas limit exceeds block gas limit!")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMessageSelectionTrimmingMsgsTwoSendersBasic(t *testing.T) {
|
||||
mp, tma := makeTestMpool()
|
||||
|
||||
// the actors
|
||||
w1, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
a1, err := w1.WalletNew(context.Background(), types.KTSecp256k1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
w2, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
a2, err := w2.WalletNew(context.Background(), types.KTBLS)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
block := tma.nextBlock()
|
||||
ts := mock.TipSet(block)
|
||||
tma.applyBlock(t, block)
|
||||
|
||||
tma.setBalance(a1, 1) // in FIL
|
||||
tma.setBalance(a2, 1) // in FIL
|
||||
|
||||
// create 2 larger than selectable chains
|
||||
for i := 0; i < build.BlockMessageLimit; i++ {
|
||||
m := makeTestMessage(w1, a1, a2, uint64(i), 300000, 100)
|
||||
mustAdd(t, mp, m)
|
||||
// a2's messages are preferred
|
||||
m = makeTestMessage(w2, a2, a1, uint64(i), 300000, 1000)
|
||||
mustAdd(t, mp, m)
|
||||
}
|
||||
|
||||
msgs, err := mp.SelectMessages(context.Background(), ts, 1.0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
mGasLimit := int64(0)
|
||||
counts := make(map[crypto.SigType]uint)
|
||||
for _, m := range msgs {
|
||||
mGasLimit += m.Message.GasLimit
|
||||
counts[m.Signature.Type]++
|
||||
}
|
||||
|
||||
if mGasLimit > build.BlockGasLimit {
|
||||
t.Fatal("selected messages gas limit exceeds block gas limit!")
|
||||
}
|
||||
|
||||
expected := build.BlockMessageLimit
|
||||
if len(msgs) != expected {
|
||||
t.Fatalf("expected %d messages, but got %d", expected, len(msgs))
|
||||
}
|
||||
|
||||
if counts[crypto.SigTypeBLS] != cbg.MaxLength {
|
||||
t.Fatalf("expected %d bls messages, but got %d", cbg.MaxLength, len(msgs))
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageSelectionTrimmingMsgsTwoSendersAdvanced(t *testing.T) {
|
||||
mp, tma := makeTestMpool()
|
||||
|
||||
// the actors
|
||||
w1, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
a1, err := w1.WalletNew(context.Background(), types.KTSecp256k1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
w2, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
a2, err := w2.WalletNew(context.Background(), types.KTBLS)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
block := tma.nextBlock()
|
||||
ts := mock.TipSet(block)
|
||||
tma.applyBlock(t, block)
|
||||
|
||||
tma.setBalance(a1, 1) // in FIL
|
||||
tma.setBalance(a2, 1) // in FIL
|
||||
|
||||
// create 2 almost max-length chains of equal value
|
||||
i := 0
|
||||
for i = 0; i < cbg.MaxLength-1; i++ {
|
||||
m := makeTestMessage(w1, a1, a2, uint64(i), 300000, 100)
|
||||
mustAdd(t, mp, m)
|
||||
// a2's messages are preferred
|
||||
m = makeTestMessage(w2, a2, a1, uint64(i), 300000, 100)
|
||||
mustAdd(t, mp, m)
|
||||
}
|
||||
|
||||
// a1's 8192th message is worth more than a2's
|
||||
m := makeTestMessage(w1, a1, a2, uint64(i), 300000, 1000)
|
||||
mustAdd(t, mp, m)
|
||||
|
||||
m = makeTestMessage(w2, a2, a1, uint64(i), 300000, 100)
|
||||
mustAdd(t, mp, m)
|
||||
|
||||
i++
|
||||
|
||||
// a2's (unselectable) 8193rd message is worth SO MUCH
|
||||
m = makeTestMessage(w2, a2, a1, uint64(i), 300000, 1000000)
|
||||
mustAdd(t, mp, m)
|
||||
|
||||
msgs, err := mp.SelectMessages(context.Background(), ts, 1.0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
mGasLimit := int64(0)
|
||||
counts := make(map[crypto.SigType]uint)
|
||||
for _, m := range msgs {
|
||||
mGasLimit += m.Message.GasLimit
|
||||
counts[m.Signature.Type]++
|
||||
}
|
||||
|
||||
if mGasLimit > build.BlockGasLimit {
|
||||
t.Fatal("selected messages gas limit exceeds block gas limit!")
|
||||
}
|
||||
|
||||
expected := build.BlockMessageLimit
|
||||
if len(msgs) != expected {
|
||||
t.Fatalf("expected %d messages, but got %d", expected, len(msgs))
|
||||
}
|
||||
|
||||
// we should have taken the secp chain
|
||||
if counts[crypto.SigTypeSecp256k1] != cbg.MaxLength {
|
||||
t.Fatalf("expected %d bls messages, but got %d", cbg.MaxLength, len(msgs))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPriorityMessageSelection(t *testing.T) {
|
||||
mp, tma := makeTestMpool()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user