2020-08-06 14:08:02 +00:00
|
|
|
package messagepool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-08-14 07:09:25 +00:00
|
|
|
"math"
|
2020-08-11 12:05:55 +00:00
|
|
|
"math/big"
|
|
|
|
"math/rand"
|
2020-08-06 14:08:02 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/messagepool/gasguess"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types/mock"
|
|
|
|
"github.com/filecoin-project/lotus/chain/wallet"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/crypto"
|
2020-08-11 12:05:55 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-08-06 14:08:02 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
|
|
|
|
_ "github.com/filecoin-project/lotus/lib/sigs/bls"
|
|
|
|
_ "github.com/filecoin-project/lotus/lib/sigs/secp"
|
2020-08-11 15:18:46 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2020-08-06 14:08:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func makeTestMessage(w *wallet.Wallet, from, to address.Address, nonce uint64, gasLimit int64, gasPrice uint64) *types.SignedMessage {
|
|
|
|
msg := &types.Message{
|
2020-08-06 19:25:30 +00:00
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Method: 2,
|
|
|
|
Value: types.FromFil(0),
|
|
|
|
Nonce: nonce,
|
|
|
|
GasLimit: gasLimit,
|
2020-08-06 22:40:25 +00:00
|
|
|
GasFeeCap: types.NewInt(100 + gasPrice),
|
2020-08-06 19:25:30 +00:00
|
|
|
GasPremium: types.NewInt(gasPrice),
|
2020-08-06 14:08:02 +00:00
|
|
|
}
|
|
|
|
sig, err := w.Sign(context.TODO(), from, msg.Cid().Bytes())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return &types.SignedMessage{
|
|
|
|
Message: *msg,
|
|
|
|
Signature: *sig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeTestMpool() (*MessagePool, *testMpoolAPI) {
|
|
|
|
tma := newTestMpoolAPI()
|
|
|
|
ds := datastore.NewMapDatastore()
|
|
|
|
mp, err := New(tma, ds, "test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mp, tma
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMessageChains(t *testing.T) {
|
|
|
|
mp, tma := makeTestMpool()
|
|
|
|
|
|
|
|
// the actors
|
|
|
|
w1, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:22:01 +00:00
|
|
|
a1, err := w1.GenerateKey(crypto.SigTypeSecp256k1)
|
2020-08-06 14:08:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w2, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:22:01 +00:00
|
|
|
a2, err := w2.GenerateKey(crypto.SigTypeSecp256k1)
|
2020-08-06 14:08:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-25 02:12:33 +00:00
|
|
|
block := tma.nextBlock()
|
2020-08-06 14:08:02 +00:00
|
|
|
ts := mock.TipSet(block)
|
|
|
|
|
2020-08-12 19:05:09 +00:00
|
|
|
gasLimit := gasguess.Costs[gasguess.CostKey{Code: builtin.StorageMarketActorCodeID, M: 2}]
|
2020-08-06 14:08:02 +00:00
|
|
|
|
|
|
|
tma.setBalance(a1, 1) // in FIL
|
|
|
|
|
|
|
|
// test chain aggregations
|
|
|
|
|
|
|
|
// test1: 10 messages from a1 to a2, with increasing gasPerf; it should
|
|
|
|
// make a single chain with 10 messages given enough balance
|
|
|
|
mset := make(map[uint64]*types.SignedMessage)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
m := makeTestMessage(w1, a1, a2, uint64(i), gasLimit, uint64(i+1))
|
|
|
|
mset[uint64(i)] = m
|
|
|
|
}
|
2020-08-06 22:40:25 +00:00
|
|
|
baseFee := types.NewInt(0)
|
2020-08-06 14:08:02 +00:00
|
|
|
|
2020-08-06 22:40:25 +00:00
|
|
|
chains := mp.createMessageChains(a1, mset, baseFee, ts)
|
2020-08-06 14:08:02 +00:00
|
|
|
if len(chains) != 1 {
|
|
|
|
t.Fatal("expected a single chain")
|
|
|
|
}
|
|
|
|
if len(chains[0].msgs) != 10 {
|
|
|
|
t.Fatalf("expected 10 messages in the chain but got %d", len(chains[0].msgs))
|
|
|
|
}
|
|
|
|
for i, m := range chains[0].msgs {
|
|
|
|
if m.Message.Nonce != uint64(i) {
|
|
|
|
t.Fatalf("expected nonce %d but got %d", i, m.Message.Nonce)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test2 : 10 messages from a1 to a2, with decreasing gasPerf; it should
|
|
|
|
// make 10 chains with 1 message each
|
|
|
|
mset = make(map[uint64]*types.SignedMessage)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
m := makeTestMessage(w1, a1, a2, uint64(i), gasLimit, uint64(10-i))
|
|
|
|
mset[uint64(i)] = m
|
|
|
|
}
|
|
|
|
|
2020-08-06 22:40:25 +00:00
|
|
|
chains = mp.createMessageChains(a1, mset, baseFee, ts)
|
2020-08-06 14:08:02 +00:00
|
|
|
if len(chains) != 10 {
|
|
|
|
t.Fatal("expected 10 chains")
|
|
|
|
}
|
|
|
|
for i, chain := range chains {
|
|
|
|
if len(chain.msgs) != 1 {
|
|
|
|
t.Fatalf("expected 1 message in chain %d but got %d", i, len(chain.msgs))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, chain := range chains {
|
|
|
|
m := chain.msgs[0]
|
|
|
|
if m.Message.Nonce != uint64(i) {
|
|
|
|
t.Fatalf("expected nonce %d but got %d", i, m.Message.Nonce)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 15:37:28 +00:00
|
|
|
// test3a: 10 messages from a1 to a2, with gasPerf increasing in groups of 3; it should
|
|
|
|
// merge them in two chains, one with 9 messages and one with the last message
|
2020-08-06 14:08:02 +00:00
|
|
|
mset = make(map[uint64]*types.SignedMessage)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
m := makeTestMessage(w1, a1, a2, uint64(i), gasLimit, uint64(1+i%3))
|
|
|
|
mset[uint64(i)] = m
|
|
|
|
}
|
|
|
|
|
2020-08-06 22:40:25 +00:00
|
|
|
chains = mp.createMessageChains(a1, mset, baseFee, ts)
|
2020-08-06 15:37:28 +00:00
|
|
|
if len(chains) != 2 {
|
|
|
|
t.Fatal("expected 1 chain")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(chains[0].msgs) != 9 {
|
|
|
|
t.Fatalf("expected 9 messages in the chain but got %d", len(chains[0].msgs))
|
|
|
|
}
|
|
|
|
if len(chains[1].msgs) != 1 {
|
|
|
|
t.Fatalf("expected 1 messages in the chain but got %d", len(chains[1].msgs))
|
|
|
|
}
|
|
|
|
nextNonce := 0
|
|
|
|
for _, chain := range chains {
|
|
|
|
for _, m := range chain.msgs {
|
|
|
|
if m.Message.Nonce != uint64(nextNonce) {
|
|
|
|
t.Fatalf("expected nonce %d but got %d", nextNonce, m.Message.Nonce)
|
|
|
|
}
|
|
|
|
nextNonce++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test3b: 10 messages from a1 to a2, with gasPerf decreasing in groups of 3 with a bias for the
|
|
|
|
// earlier chains; it should make 4 chains, the first 3 with 3 messages and the last with
|
|
|
|
// a single message
|
|
|
|
mset = make(map[uint64]*types.SignedMessage)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
bias := (12 - i) / 3
|
|
|
|
m := makeTestMessage(w1, a1, a2, uint64(i), gasLimit, uint64(1+i%3+bias))
|
|
|
|
mset[uint64(i)] = m
|
|
|
|
}
|
|
|
|
|
2020-08-06 22:40:25 +00:00
|
|
|
chains = mp.createMessageChains(a1, mset, baseFee, ts)
|
2020-08-06 14:08:02 +00:00
|
|
|
if len(chains) != 4 {
|
|
|
|
t.Fatal("expected 4 chains")
|
|
|
|
}
|
|
|
|
for i, chain := range chains {
|
|
|
|
expectedLen := 3
|
|
|
|
if i > 2 {
|
|
|
|
expectedLen = 1
|
|
|
|
}
|
|
|
|
if len(chain.msgs) != expectedLen {
|
|
|
|
t.Fatalf("expected %d message in chain %d but got %d", expectedLen, i, len(chain.msgs))
|
|
|
|
}
|
|
|
|
}
|
2020-08-06 15:37:28 +00:00
|
|
|
nextNonce = 0
|
2020-08-06 14:08:02 +00:00
|
|
|
for _, chain := range chains {
|
|
|
|
for _, m := range chain.msgs {
|
|
|
|
if m.Message.Nonce != uint64(nextNonce) {
|
|
|
|
t.Fatalf("expected nonce %d but got %d", nextNonce, m.Message.Nonce)
|
|
|
|
}
|
|
|
|
nextNonce++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test chain breaks
|
|
|
|
|
|
|
|
// test4: 10 messages with non-consecutive nonces; it should make a single chain with just
|
|
|
|
// the first message
|
|
|
|
mset = make(map[uint64]*types.SignedMessage)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
m := makeTestMessage(w1, a1, a2, uint64(i*2), gasLimit, uint64(i+1))
|
|
|
|
mset[uint64(i)] = m
|
|
|
|
}
|
|
|
|
|
2020-08-06 22:40:25 +00:00
|
|
|
chains = mp.createMessageChains(a1, mset, baseFee, ts)
|
2020-08-06 14:08:02 +00:00
|
|
|
if len(chains) != 1 {
|
|
|
|
t.Fatal("expected a single chain")
|
|
|
|
}
|
|
|
|
if len(chains[0].msgs) != 1 {
|
|
|
|
t.Fatalf("expected 1 message in the chain but got %d", len(chains[0].msgs))
|
|
|
|
}
|
|
|
|
for i, m := range chains[0].msgs {
|
|
|
|
if m.Message.Nonce != uint64(i) {
|
|
|
|
t.Fatalf("expected nonce %d but got %d", i, m.Message.Nonce)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test5: 10 messages with increasing gasLimit, except for the 6th message which has less than
|
|
|
|
// the epoch gasLimit; it should create a single chain with the first 5 messages
|
|
|
|
mset = make(map[uint64]*types.SignedMessage)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
var m *types.SignedMessage
|
|
|
|
if i != 5 {
|
|
|
|
m = makeTestMessage(w1, a1, a2, uint64(i), gasLimit, uint64(i+1))
|
|
|
|
} else {
|
|
|
|
m = makeTestMessage(w1, a1, a2, uint64(i), 1, uint64(i+1))
|
|
|
|
}
|
|
|
|
mset[uint64(i)] = m
|
|
|
|
}
|
|
|
|
|
2020-08-06 22:40:25 +00:00
|
|
|
chains = mp.createMessageChains(a1, mset, baseFee, ts)
|
2020-08-06 14:08:02 +00:00
|
|
|
if len(chains) != 1 {
|
|
|
|
t.Fatal("expected a single chain")
|
|
|
|
}
|
|
|
|
if len(chains[0].msgs) != 5 {
|
|
|
|
t.Fatalf("expected 5 message in the chain but got %d", len(chains[0].msgs))
|
|
|
|
}
|
|
|
|
for i, m := range chains[0].msgs {
|
|
|
|
if m.Message.Nonce != uint64(i) {
|
|
|
|
t.Fatalf("expected nonce %d but got %d", i, m.Message.Nonce)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test6: one more message than what can fit in a block according to gas limit, with increasing
|
|
|
|
// gasPerf; it should create a single chain with the max messages
|
|
|
|
maxMessages := int(build.BlockGasLimit / gasLimit)
|
|
|
|
nMessages := maxMessages + 1
|
|
|
|
|
|
|
|
mset = make(map[uint64]*types.SignedMessage)
|
|
|
|
for i := 0; i < nMessages; i++ {
|
|
|
|
mset[uint64(i)] = makeTestMessage(w1, a1, a2, uint64(i), gasLimit, uint64(i+1))
|
|
|
|
}
|
|
|
|
|
2020-08-06 22:40:25 +00:00
|
|
|
chains = mp.createMessageChains(a1, mset, baseFee, ts)
|
2020-08-06 14:08:02 +00:00
|
|
|
if len(chains) != 1 {
|
|
|
|
t.Fatal("expected a single chain")
|
|
|
|
}
|
|
|
|
if len(chains[0].msgs) != maxMessages {
|
|
|
|
t.Fatalf("expected %d message in the chain but got %d", maxMessages, len(chains[0].msgs))
|
|
|
|
}
|
|
|
|
for i, m := range chains[0].msgs {
|
|
|
|
if m.Message.Nonce != uint64(i) {
|
|
|
|
t.Fatalf("expected nonce %d but got %d", i, m.Message.Nonce)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test5: insufficient balance for all messages
|
2020-08-06 22:40:25 +00:00
|
|
|
tma.setBalanceRaw(a1, types.NewInt(uint64((300)*gasLimit+1)))
|
2020-08-06 14:08:02 +00:00
|
|
|
|
|
|
|
mset = make(map[uint64]*types.SignedMessage)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
mset[uint64(i)] = makeTestMessage(w1, a1, a2, uint64(i), gasLimit, uint64(i+1))
|
|
|
|
}
|
|
|
|
|
2020-08-06 22:40:25 +00:00
|
|
|
chains = mp.createMessageChains(a1, mset, baseFee, ts)
|
2020-08-06 14:08:02 +00:00
|
|
|
if len(chains) != 1 {
|
2020-08-06 21:14:09 +00:00
|
|
|
t.Fatalf("expected a single chain: got %d", len(chains))
|
2020-08-06 14:08:02 +00:00
|
|
|
}
|
|
|
|
if len(chains[0].msgs) != 2 {
|
|
|
|
t.Fatalf("expected %d message in the chain but got %d", 2, len(chains[0].msgs))
|
|
|
|
}
|
|
|
|
for i, m := range chains[0].msgs {
|
|
|
|
if m.Message.Nonce != uint64(i) {
|
|
|
|
t.Fatalf("expected nonce %d but got %d", i, m.Message.Nonce)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-08-06 17:15:15 +00:00
|
|
|
|
2020-08-08 08:16:50 +00:00
|
|
|
func TestMessageChainSkipping(t *testing.T) {
|
|
|
|
// regression test for chain skip bug
|
|
|
|
|
|
|
|
mp, tma := makeTestMpool()
|
|
|
|
|
|
|
|
// the actors
|
|
|
|
w1, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:22:01 +00:00
|
|
|
a1, err := w1.GenerateKey(crypto.SigTypeSecp256k1)
|
2020-08-08 08:16:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w2, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:22:01 +00:00
|
|
|
a2, err := w2.GenerateKey(crypto.SigTypeSecp256k1)
|
2020-08-08 08:16:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-25 02:12:33 +00:00
|
|
|
block := tma.nextBlock()
|
2020-08-08 08:16:50 +00:00
|
|
|
ts := mock.TipSet(block)
|
|
|
|
|
2020-08-12 19:05:09 +00:00
|
|
|
gasLimit := gasguess.Costs[gasguess.CostKey{Code: builtin.StorageMarketActorCodeID, M: 2}]
|
2020-08-08 08:16:50 +00:00
|
|
|
baseFee := types.NewInt(0)
|
|
|
|
|
|
|
|
tma.setBalance(a1, 1) // in FIL
|
|
|
|
tma.setStateNonce(a1, 10)
|
|
|
|
|
|
|
|
mset := make(map[uint64]*types.SignedMessage)
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
bias := (20 - i) / 3
|
|
|
|
m := makeTestMessage(w1, a1, a2, uint64(i), gasLimit, uint64(1+i%3+bias))
|
|
|
|
mset[uint64(i)] = m
|
|
|
|
}
|
|
|
|
|
|
|
|
chains := mp.createMessageChains(a1, mset, baseFee, ts)
|
|
|
|
if len(chains) != 4 {
|
|
|
|
t.Fatalf("expected 4 chains, got %d", len(chains))
|
|
|
|
}
|
|
|
|
for i, chain := range chains {
|
|
|
|
var expectedLen int
|
|
|
|
switch {
|
|
|
|
case i == 0:
|
|
|
|
expectedLen = 2
|
|
|
|
case i > 2:
|
|
|
|
expectedLen = 2
|
|
|
|
default:
|
|
|
|
expectedLen = 3
|
|
|
|
}
|
|
|
|
if len(chain.msgs) != expectedLen {
|
|
|
|
t.Fatalf("expected %d message in chain %d but got %d", expectedLen, i, len(chain.msgs))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nextNonce := 10
|
|
|
|
for _, chain := range chains {
|
|
|
|
for _, m := range chain.msgs {
|
|
|
|
if m.Message.Nonce != uint64(nextNonce) {
|
|
|
|
t.Fatalf("expected nonce %d but got %d", nextNonce, m.Message.Nonce)
|
|
|
|
}
|
|
|
|
nextNonce++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-06 17:15:15 +00:00
|
|
|
func TestBasicMessageSelection(t *testing.T) {
|
|
|
|
mp, tma := makeTestMpool()
|
|
|
|
|
|
|
|
// the actors
|
|
|
|
w1, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:22:01 +00:00
|
|
|
a1, err := w1.GenerateKey(crypto.SigTypeSecp256k1)
|
2020-08-06 17:15:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w2, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:22:01 +00:00
|
|
|
a2, err := w2.GenerateKey(crypto.SigTypeSecp256k1)
|
2020-08-06 17:15:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-25 02:12:33 +00:00
|
|
|
block := tma.nextBlock()
|
2020-08-06 17:15:15 +00:00
|
|
|
ts := mock.TipSet(block)
|
|
|
|
tma.applyBlock(t, block)
|
|
|
|
|
2020-08-12 19:05:09 +00:00
|
|
|
gasLimit := gasguess.Costs[gasguess.CostKey{Code: builtin.StorageMarketActorCodeID, M: 2}]
|
2020-08-06 17:15:15 +00:00
|
|
|
|
|
|
|
tma.setBalance(a1, 1) // in FIL
|
|
|
|
tma.setBalance(a2, 1) // in FIL
|
|
|
|
|
|
|
|
// we create 10 messages from each actor to another, with the first actor paying higher
|
|
|
|
// gas prices than the second; we expect message selection to order his messages first
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
m := makeTestMessage(w1, a1, a2, uint64(i), gasLimit, uint64(2*i+1))
|
|
|
|
mustAdd(t, mp, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
m := makeTestMessage(w2, a2, a1, uint64(i), gasLimit, uint64(i+1))
|
|
|
|
mustAdd(t, mp, m)
|
|
|
|
}
|
|
|
|
|
2020-08-11 09:33:17 +00:00
|
|
|
msgs, err := mp.SelectMessages(ts, 1.0)
|
2020-08-06 17:15:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(msgs) != 20 {
|
|
|
|
t.Fatalf("exptected 20 messages, got %d", len(msgs))
|
|
|
|
}
|
|
|
|
|
|
|
|
nextNonce := 0
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
if msgs[i].Message.From != a1 {
|
|
|
|
t.Fatalf("expected message from actor a1")
|
|
|
|
}
|
|
|
|
if msgs[i].Message.Nonce != uint64(nextNonce) {
|
|
|
|
t.Fatalf("expected nonce %d, got %d", msgs[i].Message.Nonce, nextNonce)
|
|
|
|
}
|
|
|
|
nextNonce++
|
|
|
|
}
|
|
|
|
|
|
|
|
nextNonce = 0
|
|
|
|
for i := 10; i < 20; i++ {
|
|
|
|
if msgs[i].Message.From != a2 {
|
|
|
|
t.Fatalf("expected message from actor a2")
|
|
|
|
}
|
|
|
|
if msgs[i].Message.Nonce != uint64(nextNonce) {
|
|
|
|
t.Fatalf("expected nonce %d, got %d", msgs[i].Message.Nonce, nextNonce)
|
|
|
|
}
|
|
|
|
nextNonce++
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we make a block with all the messages and advance the chain
|
2020-08-25 02:12:33 +00:00
|
|
|
block2 := tma.nextBlock()
|
2020-08-06 17:15:15 +00:00
|
|
|
tma.setBlockMessages(block2, msgs...)
|
|
|
|
tma.applyBlock(t, block2)
|
|
|
|
|
|
|
|
// we should have no pending messages in the mpool
|
2020-08-25 02:12:33 +00:00
|
|
|
pend, _ := mp.Pending()
|
2020-08-06 17:15:15 +00:00
|
|
|
if len(pend) != 0 {
|
|
|
|
t.Fatalf("expected no pending messages, but got %d", len(pend))
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a block and advance the chain without applying to the mpool
|
|
|
|
msgs = nil
|
|
|
|
for i := 10; i < 20; i++ {
|
|
|
|
m := makeTestMessage(w1, a1, a2, uint64(i), gasLimit, uint64(2*i+1))
|
|
|
|
msgs = append(msgs, m)
|
|
|
|
m = makeTestMessage(w2, a2, a1, uint64(i), gasLimit, uint64(i+1))
|
|
|
|
msgs = append(msgs, m)
|
|
|
|
}
|
2020-08-25 02:12:33 +00:00
|
|
|
block3 := tma.nextBlock()
|
2020-08-06 17:15:15 +00:00
|
|
|
tma.setBlockMessages(block3, msgs...)
|
|
|
|
ts3 := mock.TipSet(block3)
|
|
|
|
|
|
|
|
// now create another set of messages and add them to the mpool
|
|
|
|
for i := 20; i < 30; i++ {
|
2020-08-25 02:02:06 +00:00
|
|
|
m := makeTestMessage(w1, a1, a2, uint64(i), gasLimit, uint64(2*i+200))
|
2020-08-06 17:15:15 +00:00
|
|
|
mustAdd(t, mp, m)
|
|
|
|
m = makeTestMessage(w2, a2, a1, uint64(i), gasLimit, uint64(i+1))
|
|
|
|
mustAdd(t, mp, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// select messages in the last tipset; this should include the missed messages as well as
|
|
|
|
// the last messages we added, with the first actor's messages first
|
|
|
|
// first we need to update the nonce on the tma
|
|
|
|
tma.setStateNonce(a1, 10)
|
|
|
|
tma.setStateNonce(a2, 10)
|
|
|
|
|
2020-08-11 09:33:17 +00:00
|
|
|
msgs, err = mp.SelectMessages(ts3, 1.0)
|
2020-08-06 17:15:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-08-25 02:02:06 +00:00
|
|
|
if len(msgs) != 20 {
|
|
|
|
t.Fatalf("expected 20 messages, got %d", len(msgs))
|
2020-08-06 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 02:02:06 +00:00
|
|
|
nextNonce = 20
|
|
|
|
for i := 0; i < 10; i++ {
|
2020-08-06 17:15:15 +00:00
|
|
|
if msgs[i].Message.From != a1 {
|
|
|
|
t.Fatalf("expected message from actor a1")
|
|
|
|
}
|
|
|
|
if msgs[i].Message.Nonce != uint64(nextNonce) {
|
|
|
|
t.Fatalf("expected nonce %d, got %d", msgs[i].Message.Nonce, nextNonce)
|
|
|
|
}
|
|
|
|
nextNonce++
|
|
|
|
}
|
|
|
|
|
2020-08-25 02:02:06 +00:00
|
|
|
nextNonce = 20
|
|
|
|
for i := 10; i < 20; i++ {
|
2020-08-06 17:15:15 +00:00
|
|
|
if msgs[i].Message.From != a2 {
|
|
|
|
t.Fatalf("expected message from actor a2")
|
|
|
|
}
|
|
|
|
if msgs[i].Message.Nonce != uint64(nextNonce) {
|
|
|
|
t.Fatalf("expected nonce %d, got %d", msgs[i].Message.Nonce, nextNonce)
|
|
|
|
}
|
|
|
|
nextNonce++
|
|
|
|
}
|
|
|
|
}
|
2020-08-07 07:10:54 +00:00
|
|
|
|
|
|
|
func TestMessageSelectionTrimming(t *testing.T) {
|
|
|
|
mp, tma := makeTestMpool()
|
|
|
|
|
|
|
|
// the actors
|
|
|
|
w1, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:22:01 +00:00
|
|
|
a1, err := w1.GenerateKey(crypto.SigTypeSecp256k1)
|
2020-08-07 07:10:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w2, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:22:01 +00:00
|
|
|
a2, err := w2.GenerateKey(crypto.SigTypeSecp256k1)
|
2020-08-07 07:10:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-25 02:12:33 +00:00
|
|
|
block := tma.nextBlock()
|
2020-08-07 07:10:54 +00:00
|
|
|
ts := mock.TipSet(block)
|
|
|
|
tma.applyBlock(t, block)
|
|
|
|
|
2020-08-12 19:05:09 +00:00
|
|
|
gasLimit := gasguess.Costs[gasguess.CostKey{Code: builtin.StorageMarketActorCodeID, M: 2}]
|
2020-08-07 07:10:54 +00:00
|
|
|
|
|
|
|
tma.setBalance(a1, 1) // in FIL
|
|
|
|
tma.setBalance(a2, 1) // in FIL
|
|
|
|
|
|
|
|
// make many small chains for the two actors
|
|
|
|
nMessages := int((build.BlockGasLimit / gasLimit) + 1)
|
|
|
|
for i := 0; i < nMessages; i++ {
|
|
|
|
bias := (nMessages - i) / 3
|
|
|
|
m := makeTestMessage(w1, a1, a2, uint64(i), gasLimit, uint64(1+i%3+bias))
|
|
|
|
mustAdd(t, mp, m)
|
|
|
|
m = makeTestMessage(w2, a2, a1, uint64(i), gasLimit, uint64(1+i%3+bias))
|
|
|
|
mustAdd(t, mp, m)
|
|
|
|
}
|
|
|
|
|
2020-08-11 09:33:17 +00:00
|
|
|
msgs, err := mp.SelectMessages(ts, 1.0)
|
2020-08-07 07:10:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := int(build.BlockGasLimit / gasLimit)
|
|
|
|
if len(msgs) != expected {
|
|
|
|
t.Fatalf("expected %d messages, bug 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!")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-08-08 08:22:47 +00:00
|
|
|
|
|
|
|
func TestPriorityMessageSelection(t *testing.T) {
|
|
|
|
mp, tma := makeTestMpool()
|
|
|
|
|
|
|
|
// the actors
|
|
|
|
w1, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:22:01 +00:00
|
|
|
a1, err := w1.GenerateKey(crypto.SigTypeSecp256k1)
|
2020-08-08 08:22:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w2, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:22:01 +00:00
|
|
|
a2, err := w2.GenerateKey(crypto.SigTypeSecp256k1)
|
2020-08-08 08:22:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-25 02:12:33 +00:00
|
|
|
block := tma.nextBlock()
|
2020-08-08 08:22:47 +00:00
|
|
|
ts := mock.TipSet(block)
|
|
|
|
tma.applyBlock(t, block)
|
|
|
|
|
2020-08-12 19:05:09 +00:00
|
|
|
gasLimit := gasguess.Costs[gasguess.CostKey{Code: builtin.StorageMarketActorCodeID, M: 2}]
|
2020-08-08 08:22:47 +00:00
|
|
|
|
|
|
|
tma.setBalance(a1, 1) // in FIL
|
|
|
|
tma.setBalance(a2, 1) // in FIL
|
|
|
|
|
|
|
|
mp.cfg.PriorityAddrs = []address.Address{a1}
|
|
|
|
|
|
|
|
nMessages := 10
|
|
|
|
for i := 0; i < nMessages; i++ {
|
|
|
|
bias := (nMessages - i) / 3
|
|
|
|
m := makeTestMessage(w1, a1, a2, uint64(i), gasLimit, uint64(1+i%3+bias))
|
|
|
|
mustAdd(t, mp, m)
|
|
|
|
m = makeTestMessage(w2, a2, a1, uint64(i), gasLimit, uint64(1+i%3+bias))
|
|
|
|
mustAdd(t, mp, m)
|
|
|
|
}
|
|
|
|
|
2020-08-11 09:33:17 +00:00
|
|
|
msgs, err := mp.SelectMessages(ts, 1.0)
|
2020-08-08 08:22:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(msgs) != 20 {
|
|
|
|
t.Fatalf("expected 20 messages but got %d", len(msgs))
|
|
|
|
}
|
|
|
|
|
|
|
|
// messages from a1 must be first
|
|
|
|
nextNonce := uint64(0)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
m := msgs[i]
|
|
|
|
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++
|
|
|
|
}
|
|
|
|
|
|
|
|
nextNonce = 0
|
|
|
|
for i := 10; i < 20; i++ {
|
|
|
|
m := msgs[i]
|
|
|
|
if m.Message.From != a2 {
|
|
|
|
t.Fatal("expected messages from a2 after messages from a1")
|
|
|
|
}
|
|
|
|
if m.Message.Nonce != nextNonce {
|
|
|
|
t.Fatalf("expected nonce %d but got %d", nextNonce, m.Message.Nonce)
|
|
|
|
}
|
|
|
|
nextNonce++
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 11:22:01 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
// from the start
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-08-25 02:12:33 +00:00
|
|
|
block := tma.nextBlock()
|
2020-08-11 11:22:01 +00:00
|
|
|
ts := mock.TipSet(block)
|
|
|
|
tma.applyBlock(t, block)
|
|
|
|
|
2020-08-12 19:05:09 +00:00
|
|
|
gasLimit := gasguess.Costs[gasguess.CostKey{Code: builtin.StorageMarketActorCodeID, M: 2}]
|
2020-08-11 11:22:01 +00:00
|
|
|
|
|
|
|
tma.setBalance(a1, 1) // in FIL
|
|
|
|
tma.setBalance(a2, 1) // in FIL
|
|
|
|
|
|
|
|
nMessages := int(10 * build.BlockGasLimit / gasLimit)
|
|
|
|
for i := 0; i < nMessages; i++ {
|
|
|
|
bias := (nMessages - i) / 3
|
|
|
|
m := makeTestMessage(w1, a1, a2, uint64(i), gasLimit, uint64(1+i%3+bias))
|
|
|
|
mustAdd(t, mp, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
msgs, err := mp.SelectMessages(ts, 0.25)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedMsgs := int(build.BlockGasLimit / gasLimit)
|
|
|
|
if len(msgs) != expectedMsgs {
|
|
|
|
t.Fatalf("expected %d messages, but got %d", expectedMsgs, len(msgs))
|
|
|
|
}
|
|
|
|
|
|
|
|
nextNonce := uint64(0)
|
|
|
|
for _, m := range msgs {
|
|
|
|
if m.Message.From != a1 {
|
|
|
|
t.Fatal("expected message from a1")
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.Message.Nonce != nextNonce {
|
|
|
|
t.Fatalf("expected nonce %d but got %d", nextNonce, m.Message.Nonce)
|
|
|
|
}
|
|
|
|
nextNonce++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOptimalMessageSelection2(t *testing.T) {
|
|
|
|
// this test uses two actors sending messages to each other, with the first
|
|
|
|
// actor paying (much) higher gas premium than the second.
|
|
|
|
// We select with a low ticket quality; the chain depenent merging algorithm should pick
|
|
|
|
// messages from the second actor from the start
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-08-25 02:12:33 +00:00
|
|
|
block := tma.nextBlock()
|
2020-08-11 11:22:01 +00:00
|
|
|
ts := mock.TipSet(block)
|
|
|
|
tma.applyBlock(t, block)
|
|
|
|
|
2020-08-12 19:05:09 +00:00
|
|
|
gasLimit := gasguess.Costs[gasguess.CostKey{Code: builtin.StorageMarketActorCodeID, M: 2}]
|
2020-08-11 11:22:01 +00:00
|
|
|
|
|
|
|
tma.setBalance(a1, 1) // in FIL
|
|
|
|
tma.setBalance(a2, 1) // in FIL
|
|
|
|
|
|
|
|
nMessages := int(5 * build.BlockGasLimit / gasLimit)
|
|
|
|
for i := 0; i < nMessages; i++ {
|
|
|
|
bias := (nMessages - i) / 3
|
2020-08-13 09:52:15 +00:00
|
|
|
m := makeTestMessage(w1, a1, a2, uint64(i), gasLimit, uint64(200000+i%3+bias))
|
2020-08-11 11:22:01 +00:00
|
|
|
mustAdd(t, mp, m)
|
2020-08-13 09:52:15 +00:00
|
|
|
m = makeTestMessage(w2, a2, a1, uint64(i), gasLimit, uint64(190000+i%3+bias))
|
2020-08-11 11:22:01 +00:00
|
|
|
mustAdd(t, mp, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
msgs, err := mp.SelectMessages(ts, 0.1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedMsgs := int(build.BlockGasLimit / gasLimit)
|
|
|
|
if len(msgs) != expectedMsgs {
|
|
|
|
t.Fatalf("expected %d messages, but got %d", expectedMsgs, len(msgs))
|
|
|
|
}
|
|
|
|
|
2020-08-13 17:02:08 +00:00
|
|
|
var nFrom1, nFrom2 int
|
|
|
|
var nextNonce1, nextNonce2 uint64
|
2020-08-11 11:22:01 +00:00
|
|
|
for _, m := range msgs {
|
2020-08-13 17:02:08 +00:00
|
|
|
if m.Message.From == a1 {
|
|
|
|
if m.Message.Nonce != nextNonce1 {
|
|
|
|
t.Fatalf("expected nonce %d but got %d", nextNonce1, m.Message.Nonce)
|
|
|
|
}
|
|
|
|
nextNonce1++
|
|
|
|
nFrom1++
|
|
|
|
} else {
|
|
|
|
if m.Message.Nonce != nextNonce2 {
|
|
|
|
t.Fatalf("expected nonce %d but got %d", nextNonce2, m.Message.Nonce)
|
|
|
|
}
|
|
|
|
nextNonce2++
|
|
|
|
nFrom2++
|
2020-08-11 11:22:01 +00:00
|
|
|
}
|
2020-08-13 17:02:08 +00:00
|
|
|
}
|
2020-08-11 11:22:01 +00:00
|
|
|
|
2020-08-13 17:02:08 +00:00
|
|
|
if nFrom1 > nFrom2 {
|
|
|
|
t.Fatalf("expected more messages from a2 than a1; nFrom1=%d nFrom2=%d", nFrom1, nFrom2)
|
2020-08-11 11:22:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOptimalMessageSelection3(t *testing.T) {
|
|
|
|
// this test uses 10 actors sending a block of messages to each other, with the the first
|
|
|
|
// actors paying higher gas premium than the subsequent actors.
|
|
|
|
// We select with a low ticket quality; the chain depenent merging algorithm should pick
|
|
|
|
// messages from the median actor from the start
|
|
|
|
mp, tma := makeTestMpool()
|
|
|
|
|
|
|
|
nActors := 10
|
|
|
|
// the actors
|
|
|
|
var actors []address.Address
|
|
|
|
var wallets []*wallet.Wallet
|
|
|
|
|
|
|
|
for i := 0; i < nActors; i++ {
|
|
|
|
w, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a, err := w.GenerateKey(crypto.SigTypeSecp256k1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actors = append(actors, a)
|
|
|
|
wallets = append(wallets, w)
|
|
|
|
}
|
|
|
|
|
2020-08-25 02:12:33 +00:00
|
|
|
block := tma.nextBlock()
|
2020-08-11 11:22:01 +00:00
|
|
|
ts := mock.TipSet(block)
|
|
|
|
tma.applyBlock(t, block)
|
|
|
|
|
2020-08-12 19:05:09 +00:00
|
|
|
gasLimit := gasguess.Costs[gasguess.CostKey{Code: builtin.StorageMarketActorCodeID, M: 2}]
|
2020-08-11 11:22:01 +00:00
|
|
|
|
|
|
|
for _, a := range actors {
|
|
|
|
tma.setBalance(a, 1) // in FIL
|
|
|
|
}
|
|
|
|
|
|
|
|
nMessages := int(build.BlockGasLimit/gasLimit) + 1
|
|
|
|
for i := 0; i < nMessages; i++ {
|
|
|
|
for j := 0; j < nActors; j++ {
|
2020-08-18 19:24:07 +00:00
|
|
|
premium := 500000 + 10000*(nActors-j) + (nMessages+2-i)/(30*nActors) + i%3
|
2020-08-13 10:04:36 +00:00
|
|
|
m := makeTestMessage(wallets[j], actors[j], actors[j%nActors], uint64(i), gasLimit, uint64(premium))
|
2020-08-11 11:22:01 +00:00
|
|
|
mustAdd(t, mp, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msgs, err := mp.SelectMessages(ts, 0.1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedMsgs := int(build.BlockGasLimit / gasLimit)
|
|
|
|
if len(msgs) != expectedMsgs {
|
|
|
|
t.Fatalf("expected %d messages, but got %d", expectedMsgs, len(msgs))
|
|
|
|
}
|
|
|
|
|
2020-08-13 10:04:36 +00:00
|
|
|
whoIs := func(a address.Address) int {
|
|
|
|
for i, aa := range actors {
|
|
|
|
if a == aa {
|
|
|
|
return i
|
2020-08-11 11:22:01 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-13 10:04:36 +00:00
|
|
|
return -1
|
|
|
|
}
|
2020-08-11 11:22:01 +00:00
|
|
|
|
2020-08-13 10:04:36 +00:00
|
|
|
nonces := make([]uint64, nActors)
|
|
|
|
for _, m := range msgs {
|
|
|
|
who := whoIs(m.Message.From)
|
|
|
|
if who < 3 {
|
|
|
|
t.Fatalf("got message from %dth actor", who)
|
|
|
|
}
|
|
|
|
|
|
|
|
nextNonce := nonces[who]
|
2020-08-11 11:22:01 +00:00
|
|
|
if m.Message.Nonce != nextNonce {
|
|
|
|
t.Fatalf("expected nonce %d but got %d", nextNonce, m.Message.Nonce)
|
|
|
|
}
|
2020-08-13 10:04:36 +00:00
|
|
|
nonces[who]++
|
2020-08-11 11:22:01 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-11 12:05:55 +00:00
|
|
|
|
2020-08-18 18:19:17 +00:00
|
|
|
func testCompetitiveMessageSelection(t *testing.T, rng *rand.Rand, getPremium func() uint64) (float64, float64, float64) {
|
2020-08-14 07:09:25 +00:00
|
|
|
// in this test we use 300 actors and send 10 blocks of messages.
|
|
|
|
// actors send with an randomly distributed premium dictated by the getPremium function.
|
2020-08-11 12:05:55 +00:00
|
|
|
// a number of miners select with varying ticket quality and we compare the
|
|
|
|
// capacity and rewards of greedy selection -vs- optimal selection
|
|
|
|
mp, tma := makeTestMpool()
|
|
|
|
|
2020-08-11 12:59:27 +00:00
|
|
|
nActors := 300
|
2020-08-11 12:05:55 +00:00
|
|
|
// the actors
|
|
|
|
var actors []address.Address
|
|
|
|
var wallets []*wallet.Wallet
|
|
|
|
|
|
|
|
for i := 0; i < nActors; i++ {
|
|
|
|
w, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a, err := w.GenerateKey(crypto.SigTypeSecp256k1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actors = append(actors, a)
|
|
|
|
wallets = append(wallets, w)
|
|
|
|
}
|
|
|
|
|
2020-08-25 02:12:33 +00:00
|
|
|
block := tma.nextBlock()
|
2020-08-11 12:05:55 +00:00
|
|
|
ts := mock.TipSet(block)
|
|
|
|
tma.applyBlock(t, block)
|
|
|
|
|
2020-08-12 19:05:09 +00:00
|
|
|
gasLimit := gasguess.Costs[gasguess.CostKey{Code: builtin.StorageMarketActorCodeID, M: 2}]
|
2020-08-11 12:05:55 +00:00
|
|
|
baseFee := types.NewInt(0)
|
|
|
|
|
|
|
|
for _, a := range actors {
|
|
|
|
tma.setBalance(a, 1) // in FIL
|
|
|
|
}
|
|
|
|
|
|
|
|
nMessages := 10 * int(build.BlockGasLimit/gasLimit)
|
2020-08-11 12:59:27 +00:00
|
|
|
t.Log("nMessages", nMessages)
|
2020-08-11 12:05:55 +00:00
|
|
|
nonces := make([]uint64, nActors)
|
|
|
|
for i := 0; i < nMessages; i++ {
|
2020-08-11 12:59:27 +00:00
|
|
|
from := rng.Intn(nActors)
|
|
|
|
to := rng.Intn(nActors)
|
2020-08-11 12:05:55 +00:00
|
|
|
nonce := nonces[from]
|
|
|
|
nonces[from]++
|
2020-08-14 07:09:25 +00:00
|
|
|
premium := getPremium()
|
2020-08-14 07:12:55 +00:00
|
|
|
m := makeTestMessage(wallets[from], actors[from], actors[to], nonce, gasLimit, premium)
|
2020-08-11 12:05:55 +00:00
|
|
|
mustAdd(t, mp, m)
|
|
|
|
}
|
|
|
|
|
2020-08-13 08:00:42 +00:00
|
|
|
logging.SetLogLevel("messagepool", "error")
|
|
|
|
|
2020-08-11 12:05:55 +00:00
|
|
|
// 1. greedy selection
|
|
|
|
greedyMsgs, err := mp.selectMessagesGreedy(ts, ts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-18 17:18:53 +00:00
|
|
|
totalGreedyCapacity := 0.0
|
|
|
|
totalGreedyReward := 0.0
|
|
|
|
totalOptimalCapacity := 0.0
|
|
|
|
totalOptimalReward := 0.0
|
2020-08-18 18:19:17 +00:00
|
|
|
totalBestTQReward := 0.0
|
2020-08-18 19:08:57 +00:00
|
|
|
const runs = 1
|
2020-08-13 12:00:55 +00:00
|
|
|
for i := 0; i < runs; i++ {
|
2020-08-13 00:23:52 +00:00
|
|
|
// 2. optimal selection
|
|
|
|
minersRand := rng.Float64()
|
|
|
|
winerProba := noWinnersProb()
|
|
|
|
i := 0
|
|
|
|
for ; i < MaxBlocks && minersRand > 0; i++ {
|
|
|
|
minersRand -= winerProba[i]
|
|
|
|
}
|
2020-08-18 17:18:53 +00:00
|
|
|
nMiners := i - 1
|
|
|
|
if nMiners < 1 {
|
2020-08-13 00:23:52 +00:00
|
|
|
nMiners = 1
|
|
|
|
}
|
|
|
|
|
2020-08-11 15:18:46 +00:00
|
|
|
optMsgs := make(map[cid.Cid]*types.SignedMessage)
|
2020-08-18 18:19:17 +00:00
|
|
|
bestTq := 0.0
|
|
|
|
var bestMsgs []*types.SignedMessage
|
2020-08-11 15:18:46 +00:00
|
|
|
for j := 0; j < nMiners; j++ {
|
|
|
|
tq := rng.Float64()
|
|
|
|
msgs, err := mp.SelectMessages(ts, tq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-08-18 18:19:17 +00:00
|
|
|
if tq > bestTq {
|
|
|
|
bestMsgs = msgs
|
|
|
|
}
|
|
|
|
|
2020-08-11 15:18:46 +00:00
|
|
|
for _, m := range msgs {
|
|
|
|
optMsgs[m.Cid()] = m
|
|
|
|
}
|
2020-08-11 12:05:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 17:18:53 +00:00
|
|
|
totalGreedyCapacity += float64(len(greedyMsgs))
|
|
|
|
totalOptimalCapacity += float64(len(optMsgs))
|
2020-08-13 08:00:42 +00:00
|
|
|
boost := float64(len(optMsgs)) / float64(len(greedyMsgs))
|
|
|
|
|
2020-08-11 15:18:46 +00:00
|
|
|
t.Logf("nMiners: %d", nMiners)
|
2020-08-13 00:23:52 +00:00
|
|
|
t.Logf("greedy capacity %d, optimal capacity %d (x %.1f )", len(greedyMsgs),
|
2020-08-13 08:00:42 +00:00
|
|
|
len(optMsgs), boost)
|
2020-08-11 15:18:46 +00:00
|
|
|
if len(greedyMsgs) > len(optMsgs) {
|
2020-08-18 17:18:53 +00:00
|
|
|
t.Errorf("greedy capacity higher than optimal capacity; wtf")
|
2020-08-11 15:18:46 +00:00
|
|
|
}
|
2020-08-11 12:05:55 +00:00
|
|
|
|
2020-08-11 15:18:46 +00:00
|
|
|
greedyReward := big.NewInt(0)
|
|
|
|
for _, m := range greedyMsgs {
|
|
|
|
greedyReward.Add(greedyReward, mp.getGasReward(m, baseFee, ts))
|
|
|
|
}
|
2020-08-11 12:05:55 +00:00
|
|
|
|
2020-08-11 15:18:46 +00:00
|
|
|
optReward := big.NewInt(0)
|
|
|
|
for _, m := range optMsgs {
|
|
|
|
optReward.Add(optReward, mp.getGasReward(m, baseFee, ts))
|
|
|
|
}
|
2020-08-11 12:05:55 +00:00
|
|
|
|
2020-08-18 18:19:17 +00:00
|
|
|
bestTqReward := big.NewInt(0)
|
|
|
|
for _, m := range bestMsgs {
|
|
|
|
bestTqReward.Add(bestTqReward, mp.getGasReward(m, baseFee, ts))
|
|
|
|
}
|
|
|
|
|
|
|
|
totalBestTQReward += float64(bestTqReward.Uint64())
|
|
|
|
|
2020-08-11 15:18:46 +00:00
|
|
|
nMinersBig := big.NewInt(int64(nMiners))
|
|
|
|
greedyAvgReward, _ := new(big.Rat).SetFrac(greedyReward, nMinersBig).Float64()
|
2020-08-18 17:18:53 +00:00
|
|
|
totalGreedyReward += greedyAvgReward
|
2020-08-11 15:18:46 +00:00
|
|
|
optimalAvgReward, _ := new(big.Rat).SetFrac(optReward, nMinersBig).Float64()
|
2020-08-18 17:18:53 +00:00
|
|
|
totalOptimalReward += optimalAvgReward
|
2020-08-13 08:00:42 +00:00
|
|
|
|
|
|
|
boost = optimalAvgReward / greedyAvgReward
|
2020-08-13 00:23:52 +00:00
|
|
|
t.Logf("greedy reward: %.0f, optimal reward: %.0f (x %.1f )", greedyAvgReward,
|
2020-08-13 08:00:42 +00:00
|
|
|
optimalAvgReward, boost)
|
2020-08-11 12:59:27 +00:00
|
|
|
|
2020-08-11 12:05:55 +00:00
|
|
|
}
|
2020-08-13 08:00:42 +00:00
|
|
|
|
2020-08-18 17:18:53 +00:00
|
|
|
capacityBoost := totalOptimalCapacity / totalGreedyCapacity
|
|
|
|
rewardBoost := totalOptimalReward / totalGreedyReward
|
2020-08-13 08:00:42 +00:00
|
|
|
t.Logf("Average capacity boost: %f", capacityBoost)
|
|
|
|
t.Logf("Average reward boost: %f", rewardBoost)
|
2020-08-18 18:27:25 +00:00
|
|
|
t.Logf("Average best tq reward: %f", totalBestTQReward/runs/1e12)
|
2020-08-13 08:00:42 +00:00
|
|
|
|
2020-08-11 15:18:46 +00:00
|
|
|
logging.SetLogLevel("messagepool", "info")
|
2020-08-13 08:00:42 +00:00
|
|
|
|
2020-08-18 18:27:25 +00:00
|
|
|
return capacityBoost, rewardBoost, totalBestTQReward / runs / 1e12
|
2020-08-11 12:05:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 07:09:25 +00:00
|
|
|
func makeExpPremiumDistribution(rng *rand.Rand) func() uint64 {
|
|
|
|
return func() uint64 {
|
|
|
|
premium := 20000*math.Exp(-3.*rng.Float64()) + 5000
|
|
|
|
return uint64(premium)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeZipfPremiumDistribution(rng *rand.Rand) func() uint64 {
|
|
|
|
zipf := rand.NewZipf(rng, 1.001, 1, 40000)
|
|
|
|
return func() uint64 {
|
|
|
|
return zipf.Uint64() + 10000
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompetitiveMessageSelectionExp(t *testing.T) {
|
2020-08-18 18:19:17 +00:00
|
|
|
var capacityBoost, rewardBoost, tqReward float64
|
2020-08-14 07:09:25 +00:00
|
|
|
seeds := []int64{1947, 1976, 2020, 2100, 10000, 143324, 432432, 131, 32, 45}
|
|
|
|
for _, seed := range seeds {
|
|
|
|
t.Log("running competitive message selection with Exponential premium distribution and seed", seed)
|
|
|
|
rng := rand.New(rand.NewSource(seed))
|
2020-08-18 18:19:17 +00:00
|
|
|
cb, rb, tqR := testCompetitiveMessageSelection(t, rng, makeExpPremiumDistribution(rng))
|
2020-08-14 07:09:25 +00:00
|
|
|
capacityBoost += cb
|
|
|
|
rewardBoost += rb
|
2020-08-18 18:19:17 +00:00
|
|
|
tqReward += tqR
|
2020-08-14 07:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
capacityBoost /= float64(len(seeds))
|
|
|
|
rewardBoost /= float64(len(seeds))
|
2020-08-18 18:19:17 +00:00
|
|
|
tqReward /= float64(len(seeds))
|
2020-08-14 07:09:25 +00:00
|
|
|
t.Logf("Average capacity boost across all seeds: %f", capacityBoost)
|
|
|
|
t.Logf("Average reward boost across all seeds: %f", rewardBoost)
|
2020-08-18 18:19:17 +00:00
|
|
|
t.Logf("Average reward of best ticket across all seeds: %f", tqReward)
|
2020-08-14 07:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompetitiveMessageSelectionZipf(t *testing.T) {
|
2020-08-18 18:19:17 +00:00
|
|
|
var capacityBoost, rewardBoost, tqReward float64
|
2020-08-13 00:23:52 +00:00
|
|
|
seeds := []int64{1947, 1976, 2020, 2100, 10000, 143324, 432432, 131, 32, 45}
|
2020-08-11 12:05:55 +00:00
|
|
|
for _, seed := range seeds {
|
2020-08-14 07:09:25 +00:00
|
|
|
t.Log("running competitive message selection with Zipf premium distribution and seed", seed)
|
|
|
|
rng := rand.New(rand.NewSource(seed))
|
2020-08-18 18:19:17 +00:00
|
|
|
cb, rb, tqR := testCompetitiveMessageSelection(t, rng, makeZipfPremiumDistribution(rng))
|
2020-08-13 08:00:42 +00:00
|
|
|
capacityBoost += cb
|
|
|
|
rewardBoost += rb
|
2020-08-18 18:19:17 +00:00
|
|
|
tqReward += tqR
|
2020-08-11 12:05:55 +00:00
|
|
|
}
|
2020-08-13 08:00:42 +00:00
|
|
|
|
2020-08-18 18:19:17 +00:00
|
|
|
tqReward /= float64(len(seeds))
|
2020-08-13 08:00:42 +00:00
|
|
|
capacityBoost /= float64(len(seeds))
|
|
|
|
rewardBoost /= float64(len(seeds))
|
|
|
|
t.Logf("Average capacity boost across all seeds: %f", capacityBoost)
|
|
|
|
t.Logf("Average reward boost across all seeds: %f", rewardBoost)
|
2020-08-18 18:19:17 +00:00
|
|
|
t.Logf("Average reward of best ticket across all seeds: %f", tqReward)
|
2020-08-11 12:05:55 +00:00
|
|
|
}
|