2019-12-01 23:11:43 +00:00
|
|
|
package messagepool
|
2019-12-01 22:22:10 +00:00
|
|
|
|
|
|
|
import (
|
2020-04-17 20:44:13 +00:00
|
|
|
"context"
|
2019-12-02 22:28:28 +00:00
|
|
|
"fmt"
|
2019-12-01 22:22:10 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2019-12-01 22:22:10 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types/mock"
|
|
|
|
"github.com/filecoin-project/lotus/chain/wallet"
|
2020-01-31 00:20:04 +00:00
|
|
|
_ "github.com/filecoin-project/lotus/lib/sigs/bls"
|
|
|
|
_ "github.com/filecoin-project/lotus/lib/sigs/secp"
|
2020-08-06 14:08:02 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
2020-02-23 07:49:15 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/crypto"
|
2019-12-01 22:22:10 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/ipfs/go-datastore"
|
2020-08-01 23:29:26 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2019-12-01 22:22:10 +00:00
|
|
|
)
|
|
|
|
|
2020-08-01 23:29:26 +00:00
|
|
|
func init() {
|
|
|
|
_ = logging.SetLogLevel("*", "INFO")
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
type testMpoolAPI struct {
|
2019-12-01 22:22:10 +00:00
|
|
|
cb func(rev, app []*types.TipSet) error
|
|
|
|
|
|
|
|
bmsgs map[cid.Cid][]*types.SignedMessage
|
|
|
|
statenonce map[address.Address]uint64
|
2020-08-06 14:08:02 +00:00
|
|
|
balance map[address.Address]types.BigInt
|
2019-12-02 22:28:28 +00:00
|
|
|
|
|
|
|
tipsets []*types.TipSet
|
2019-12-01 22:22:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 16:38:41 +00:00
|
|
|
func newTestMpoolAPI() *testMpoolAPI {
|
2020-06-02 14:29:39 +00:00
|
|
|
return &testMpoolAPI{
|
2019-12-01 22:22:10 +00:00
|
|
|
bmsgs: make(map[cid.Cid][]*types.SignedMessage),
|
|
|
|
statenonce: make(map[address.Address]uint64),
|
2020-08-06 14:08:02 +00:00
|
|
|
balance: make(map[address.Address]types.BigInt),
|
2019-12-01 22:22:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (tma *testMpoolAPI) applyBlock(t *testing.T, b *types.BlockHeader) {
|
2019-12-01 22:22:10 +00:00
|
|
|
t.Helper()
|
|
|
|
if err := tma.cb(nil, []*types.TipSet{mock.TipSet(b)}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (tma *testMpoolAPI) revertBlock(t *testing.T, b *types.BlockHeader) {
|
2019-12-02 22:28:28 +00:00
|
|
|
t.Helper()
|
|
|
|
if err := tma.cb([]*types.TipSet{mock.TipSet(b)}, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (tma *testMpoolAPI) setStateNonce(addr address.Address, v uint64) {
|
2019-12-01 22:22:10 +00:00
|
|
|
tma.statenonce[addr] = v
|
|
|
|
}
|
|
|
|
|
2020-08-06 14:08:02 +00:00
|
|
|
func (tma *testMpoolAPI) setBalance(addr address.Address, v uint64) {
|
|
|
|
tma.balance[addr] = types.FromFil(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tma *testMpoolAPI) setBalanceRaw(addr address.Address, v types.BigInt) {
|
|
|
|
tma.balance[addr] = v
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (tma *testMpoolAPI) setBlockMessages(h *types.BlockHeader, msgs ...*types.SignedMessage) {
|
2019-12-01 22:22:10 +00:00
|
|
|
tma.bmsgs[h.Cid()] = msgs
|
2019-12-02 22:28:28 +00:00
|
|
|
tma.tipsets = append(tma.tipsets, mock.TipSet(h))
|
2019-12-01 22:22:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (tma *testMpoolAPI) SubscribeHeadChanges(cb func(rev, app []*types.TipSet) error) *types.TipSet {
|
2019-12-01 22:22:10 +00:00
|
|
|
tma.cb = cb
|
2019-12-03 21:09:39 +00:00
|
|
|
return nil
|
2019-12-01 22:22:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (tma *testMpoolAPI) PutMessage(m types.ChainMsg) (cid.Cid, error) {
|
2019-12-01 22:22:10 +00:00
|
|
|
return cid.Undef, nil
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (tma *testMpoolAPI) PubSubPublish(string, []byte) error {
|
2019-12-01 22:22:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (tma *testMpoolAPI) StateGetActor(addr address.Address, ts *types.TipSet) (*types.Actor, error) {
|
2020-08-06 14:08:02 +00:00
|
|
|
balance, ok := tma.balance[addr]
|
|
|
|
if !ok {
|
2020-08-06 19:25:30 +00:00
|
|
|
balance = types.NewInt(1000e6)
|
2020-08-06 14:08:02 +00:00
|
|
|
tma.balance[addr] = balance
|
|
|
|
}
|
2019-12-01 22:22:10 +00:00
|
|
|
return &types.Actor{
|
2020-08-06 14:08:02 +00:00
|
|
|
Code: builtin.StorageMarketActorCodeID,
|
2019-12-01 22:22:10 +00:00
|
|
|
Nonce: tma.statenonce[addr],
|
2020-08-06 14:08:02 +00:00
|
|
|
Balance: balance,
|
2019-12-01 22:22:10 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (tma *testMpoolAPI) StateAccountKey(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error) {
|
2020-04-17 20:44:13 +00:00
|
|
|
if addr.Protocol() != address.BLS && addr.Protocol() != address.SECP256K1 {
|
|
|
|
return address.Undef, fmt.Errorf("given address was not a key addr")
|
|
|
|
}
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (tma *testMpoolAPI) MessagesForBlock(h *types.BlockHeader) ([]*types.Message, []*types.SignedMessage, error) {
|
2019-12-01 22:22:10 +00:00
|
|
|
return nil, tma.bmsgs[h.Cid()], nil
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (tma *testMpoolAPI) MessagesForTipset(ts *types.TipSet) ([]types.ChainMsg, error) {
|
2019-12-01 22:22:10 +00:00
|
|
|
if len(ts.Blocks()) != 1 {
|
|
|
|
panic("cant deal with multiblock tipsets in this test")
|
|
|
|
}
|
|
|
|
|
|
|
|
bm, sm, err := tma.MessagesForBlock(ts.Blocks()[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-25 19:13:09 +00:00
|
|
|
var out []types.ChainMsg
|
2019-12-01 22:22:10 +00:00
|
|
|
for _, m := range bm {
|
|
|
|
out = append(out, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range sm {
|
|
|
|
out = append(out, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (tma *testMpoolAPI) LoadTipSet(tsk types.TipSetKey) (*types.TipSet, error) {
|
2019-12-02 22:28:28 +00:00
|
|
|
for _, ts := range tma.tipsets {
|
2019-12-16 19:22:56 +00:00
|
|
|
if types.CidArrsEqual(tsk.Cids(), ts.Cids()) {
|
2019-12-02 22:28:28 +00:00
|
|
|
return ts, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("tipset not found")
|
2019-12-02 20:46:25 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 22:40:25 +00:00
|
|
|
func (tma *testMpoolAPI) ChainComputeBaseFee(ctx context.Context, ts *types.TipSet) (types.BigInt, error) {
|
|
|
|
return types.NewInt(100), nil
|
|
|
|
}
|
|
|
|
|
2019-12-01 22:22:10 +00:00
|
|
|
func assertNonce(t *testing.T, mp *MessagePool, addr address.Address, val uint64) {
|
|
|
|
t.Helper()
|
|
|
|
n, err := mp.GetNonce(addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n != val {
|
|
|
|
t.Fatalf("expected nonce of %d, got %d", val, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustAdd(t *testing.T, mp *MessagePool, msg *types.SignedMessage) {
|
|
|
|
t.Helper()
|
|
|
|
if err := mp.Add(msg); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMessagePool(t *testing.T) {
|
2020-06-02 16:38:41 +00:00
|
|
|
tma := newTestMpoolAPI()
|
2019-12-01 22:22:10 +00:00
|
|
|
|
|
|
|
w, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ds := datastore.NewMapDatastore()
|
|
|
|
|
2020-03-31 23:13:37 +00:00
|
|
|
mp, err := New(tma, ds, "mptest")
|
2019-12-01 22:22:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a := mock.MkBlock(nil, 1, 1)
|
|
|
|
|
2020-02-23 07:49:15 +00:00
|
|
|
sender, err := w.GenerateKey(crypto.SigTypeBLS)
|
2019-12-01 22:22:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
target := mock.Address(1001)
|
|
|
|
|
|
|
|
var msgs []*types.SignedMessage
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
msgs = append(msgs, mock.MkMessage(sender, target, uint64(i), w))
|
|
|
|
}
|
|
|
|
|
|
|
|
tma.setStateNonce(sender, 0)
|
|
|
|
assertNonce(t, mp, sender, 0)
|
|
|
|
mustAdd(t, mp, msgs[0])
|
|
|
|
assertNonce(t, mp, sender, 1)
|
|
|
|
mustAdd(t, mp, msgs[1])
|
|
|
|
assertNonce(t, mp, sender, 2)
|
|
|
|
|
|
|
|
tma.setBlockMessages(a, msgs[0], msgs[1])
|
|
|
|
tma.applyBlock(t, a)
|
|
|
|
|
|
|
|
assertNonce(t, mp, sender, 2)
|
|
|
|
}
|
2019-12-02 22:28:28 +00:00
|
|
|
|
|
|
|
func TestRevertMessages(t *testing.T) {
|
2020-06-02 16:38:41 +00:00
|
|
|
tma := newTestMpoolAPI()
|
2019-12-02 22:28:28 +00:00
|
|
|
|
|
|
|
w, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ds := datastore.NewMapDatastore()
|
|
|
|
|
2020-03-31 23:13:37 +00:00
|
|
|
mp, err := New(tma, ds, "mptest")
|
2019-12-02 22:28:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a := mock.MkBlock(nil, 1, 1)
|
|
|
|
b := mock.MkBlock(mock.TipSet(a), 1, 1)
|
|
|
|
|
2020-02-23 07:49:15 +00:00
|
|
|
sender, err := w.GenerateKey(crypto.SigTypeBLS)
|
2019-12-02 22:28:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
target := mock.Address(1001)
|
|
|
|
|
|
|
|
var msgs []*types.SignedMessage
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
msgs = append(msgs, mock.MkMessage(sender, target, uint64(i), w))
|
|
|
|
}
|
|
|
|
|
|
|
|
tma.setBlockMessages(a, msgs[0])
|
|
|
|
tma.setBlockMessages(b, msgs[1], msgs[2], msgs[3])
|
|
|
|
|
|
|
|
mustAdd(t, mp, msgs[0])
|
|
|
|
mustAdd(t, mp, msgs[1])
|
|
|
|
mustAdd(t, mp, msgs[2])
|
|
|
|
mustAdd(t, mp, msgs[3])
|
|
|
|
|
|
|
|
tma.setStateNonce(sender, 0)
|
|
|
|
tma.applyBlock(t, a)
|
|
|
|
assertNonce(t, mp, sender, 4)
|
|
|
|
|
|
|
|
tma.setStateNonce(sender, 1)
|
|
|
|
tma.applyBlock(t, b)
|
|
|
|
assertNonce(t, mp, sender, 4)
|
|
|
|
tma.setStateNonce(sender, 0)
|
|
|
|
tma.revertBlock(t, b)
|
|
|
|
|
|
|
|
assertNonce(t, mp, sender, 4)
|
|
|
|
|
2019-12-03 21:09:39 +00:00
|
|
|
p, _ := mp.Pending()
|
|
|
|
if len(p) != 3 {
|
2019-12-02 22:28:28 +00:00
|
|
|
t.Fatal("expected three messages in mempool")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-08-01 22:54:21 +00:00
|
|
|
|
|
|
|
func TestPruningSimple(t *testing.T) {
|
|
|
|
tma := newTestMpoolAPI()
|
|
|
|
|
|
|
|
w, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ds := datastore.NewMapDatastore()
|
|
|
|
|
|
|
|
mp, err := New(tma, ds, "mptest")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-01 23:25:13 +00:00
|
|
|
a := mock.MkBlock(nil, 1, 1)
|
|
|
|
tma.applyBlock(t, a)
|
|
|
|
|
2020-08-01 22:54:21 +00:00
|
|
|
sender, err := w.GenerateKey(crypto.SigTypeBLS)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
target := mock.Address(1001)
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
smsg := mock.MkMessage(sender, target, uint64(i), w)
|
|
|
|
if err := mp.Add(smsg); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 10; i < 50; i++ {
|
|
|
|
smsg := mock.MkMessage(sender, target, uint64(i), w)
|
|
|
|
if err := mp.Add(smsg); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp.maxTxPoolSizeHi = 40
|
|
|
|
mp.maxTxPoolSizeLo = 10
|
|
|
|
|
|
|
|
mp.Prune()
|
|
|
|
|
|
|
|
msgs, _ := mp.Pending()
|
|
|
|
if len(msgs) != 5 {
|
2020-08-01 23:25:13 +00:00
|
|
|
t.Fatal("expected only 5 messages in pool, got: ", len(msgs))
|
2020-08-01 22:54:21 +00:00
|
|
|
}
|
|
|
|
}
|