Skip to content

Commit

Permalink
Mempool: add selection tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Sep 12, 2021
1 parent 4208fc2 commit 4a1eadb
Showing 1 changed file with 198 additions and 1 deletion.
199 changes: 198 additions & 1 deletion chain/messagepool/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -527,7 +531,7 @@ func TestBasicMessageSelection(t *testing.T) {
}
}

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

// the actors
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 4a1eadb

Please sign in to comment.