2019-09-26 20:47:34 +00:00
|
|
|
package miner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-07-31 12:23:56 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
2020-07-15 14:50:32 +00:00
|
|
|
|
2020-07-31 12:23:56 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2020-08-01 22:54:21 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/messagepool/gasguess"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-07-15 19:21:53 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-09-26 20:47:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func mustIDAddr(i uint64) address.Address {
|
|
|
|
a, err := address.NewIDAddress(i)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2020-07-31 12:23:56 +00:00
|
|
|
func TestSelectNotOverLimited(t *testing.T) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
a1 := mustIDAddr(1)
|
|
|
|
a2 := mustIDAddr(2)
|
|
|
|
a3 := mustIDAddr(3)
|
|
|
|
|
|
|
|
actors := map[address.Address]*types.Actor{
|
|
|
|
a1: {
|
|
|
|
Code: builtin.AccountActorCodeID,
|
|
|
|
Nonce: 1,
|
|
|
|
Balance: types.FromFil(1200),
|
|
|
|
},
|
|
|
|
a2: {
|
|
|
|
Code: builtin.AccountActorCodeID,
|
|
|
|
Nonce: 1,
|
|
|
|
Balance: types.FromFil(1200),
|
|
|
|
},
|
|
|
|
a3: {
|
|
|
|
Code: builtin.StorageMinerActorCodeID,
|
|
|
|
Nonce: 0,
|
|
|
|
Balance: types.FromFil(1000),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
af := func(ctx context.Context, addr address.Address, tsk types.TipSetKey) (*types.Actor, error) {
|
|
|
|
return actors[addr], nil
|
|
|
|
}
|
|
|
|
|
2020-08-01 22:54:21 +00:00
|
|
|
gasUsed := gasguess.Costs[gasguess.CostKey{builtin.StorageMinerActorCodeID, 4}]
|
2020-07-31 12:23:56 +00:00
|
|
|
|
|
|
|
var goodMsgs []types.Message
|
|
|
|
for i := int64(0); i < build.BlockGasLimit/gasUsed+10; i++ {
|
|
|
|
goodMsgs = append(goodMsgs, types.Message{
|
|
|
|
From: a1,
|
|
|
|
To: a3,
|
|
|
|
Method: 4,
|
|
|
|
Nonce: uint64(1 + i),
|
|
|
|
Value: types.FromFil(0),
|
|
|
|
GasLimit: gasUsed + 1000,
|
|
|
|
GasPrice: types.NewInt(1),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
var badMsgs []types.Message
|
|
|
|
for i := int64(0); i < build.BlockGasLimit/gasUsed+10; i++ {
|
|
|
|
badMsgs = append(badMsgs, types.Message{
|
|
|
|
From: a2,
|
|
|
|
To: a3,
|
|
|
|
Method: 4,
|
|
|
|
Nonce: uint64(1 + i),
|
|
|
|
Value: types.FromFil(0),
|
|
|
|
GasLimit: 10 * gasUsed,
|
2020-07-31 12:26:43 +00:00
|
|
|
GasPrice: types.NewInt(9),
|
2020-07-31 12:23:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
outmsgs, err := SelectMessages(ctx, af, &types.TipSet{}, wrapMsgs(append(goodMsgs, badMsgs...)))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, v := range outmsgs {
|
|
|
|
if v.Message.From == a2 {
|
|
|
|
t.Errorf("included bad actor message: %v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 20:47:34 +00:00
|
|
|
func TestMessageFiltering(t *testing.T) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
a1 := mustIDAddr(1)
|
|
|
|
a2 := mustIDAddr(2)
|
|
|
|
|
|
|
|
actors := map[address.Address]*types.Actor{
|
2020-04-08 19:37:04 +00:00
|
|
|
a1: {
|
2019-09-26 20:47:34 +00:00
|
|
|
Nonce: 3,
|
2020-07-15 19:21:53 +00:00
|
|
|
Balance: types.FromFil(1200),
|
2019-09-26 20:47:34 +00:00
|
|
|
},
|
2020-04-08 19:37:04 +00:00
|
|
|
a2: {
|
2019-09-26 20:47:34 +00:00
|
|
|
Nonce: 1,
|
2020-07-15 19:21:53 +00:00
|
|
|
Balance: types.FromFil(1000),
|
2019-09-26 20:47:34 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
af := func(ctx context.Context, addr address.Address, tsk types.TipSetKey) (*types.Actor, error) {
|
2019-09-26 20:47:34 +00:00
|
|
|
return actors[addr], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
msgs := []types.Message{
|
2020-04-08 19:37:04 +00:00
|
|
|
{
|
2019-09-26 20:47:34 +00:00
|
|
|
From: a1,
|
2019-10-14 04:32:55 +00:00
|
|
|
To: a1,
|
2019-09-26 20:47:34 +00:00
|
|
|
Nonce: 3,
|
2020-07-15 19:21:53 +00:00
|
|
|
Value: types.FromFil(500),
|
2020-07-15 17:53:27 +00:00
|
|
|
GasLimit: 100_000_000,
|
2019-09-26 20:47:34 +00:00
|
|
|
GasPrice: types.NewInt(1),
|
|
|
|
},
|
2020-04-08 19:37:04 +00:00
|
|
|
{
|
2019-09-26 20:47:34 +00:00
|
|
|
From: a1,
|
2019-10-14 04:32:55 +00:00
|
|
|
To: a1,
|
2019-09-26 20:47:34 +00:00
|
|
|
Nonce: 4,
|
2020-07-15 19:21:53 +00:00
|
|
|
Value: types.FromFil(500),
|
2020-07-15 17:53:27 +00:00
|
|
|
GasLimit: 100_000_000,
|
2019-09-26 20:47:34 +00:00
|
|
|
GasPrice: types.NewInt(1),
|
|
|
|
},
|
2020-04-08 19:37:04 +00:00
|
|
|
{
|
2019-09-26 20:47:34 +00:00
|
|
|
From: a2,
|
2019-10-14 04:32:55 +00:00
|
|
|
To: a1,
|
2019-09-26 20:47:34 +00:00
|
|
|
Nonce: 1,
|
2020-07-15 19:21:53 +00:00
|
|
|
Value: types.FromFil(800),
|
2020-07-15 17:53:27 +00:00
|
|
|
GasLimit: 100_000_000,
|
2019-09-26 20:47:34 +00:00
|
|
|
GasPrice: types.NewInt(1),
|
|
|
|
},
|
2020-04-08 19:37:04 +00:00
|
|
|
{
|
2019-09-26 20:47:34 +00:00
|
|
|
From: a2,
|
2019-10-14 04:32:55 +00:00
|
|
|
To: a1,
|
2019-09-26 20:47:34 +00:00
|
|
|
Nonce: 0,
|
2020-07-15 19:21:53 +00:00
|
|
|
Value: types.FromFil(800),
|
2020-07-15 17:53:27 +00:00
|
|
|
GasLimit: 100_000_000,
|
2019-09-26 20:47:34 +00:00
|
|
|
GasPrice: types.NewInt(1),
|
|
|
|
},
|
2020-04-08 19:37:04 +00:00
|
|
|
{
|
2019-09-26 20:47:34 +00:00
|
|
|
From: a2,
|
2019-10-14 04:32:55 +00:00
|
|
|
To: a1,
|
2019-09-26 20:47:34 +00:00
|
|
|
Nonce: 2,
|
2020-07-15 19:21:53 +00:00
|
|
|
Value: types.FromFil(150),
|
2020-07-15 14:50:32 +00:00
|
|
|
GasLimit: 100,
|
2019-09-26 20:47:34 +00:00
|
|
|
GasPrice: types.NewInt(1),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-05-14 19:28:33 +00:00
|
|
|
outmsgs, err := SelectMessages(ctx, af, &types.TipSet{}, wrapMsgs(msgs))
|
2019-09-26 20:47:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-07-15 19:21:53 +00:00
|
|
|
assert.Len(t, outmsgs, 3, "filtering didnt work as expected")
|
2019-09-26 20:47:34 +00:00
|
|
|
|
2020-07-15 14:50:32 +00:00
|
|
|
was, expected := outmsgs[0].Message, msgs[2]
|
|
|
|
if was.From != expected.From || was.Nonce != expected.Nonce {
|
2019-09-26 20:47:34 +00:00
|
|
|
t.Fatal("filtering bad")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func wrapMsgs(msgs []types.Message) []*types.SignedMessage {
|
|
|
|
var out []*types.SignedMessage
|
|
|
|
for _, m := range msgs {
|
|
|
|
out = append(out, &types.SignedMessage{Message: m})
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
2020-08-04 00:51:22 +00:00
|
|
|
|
|
|
|
func TestMessageFilteringSenderHopping(t *testing.T) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
a1 := mustIDAddr(1)
|
|
|
|
a2 := mustIDAddr(2)
|
|
|
|
|
|
|
|
actors := map[address.Address]*types.Actor{
|
|
|
|
a1: {
|
|
|
|
Nonce: 0,
|
|
|
|
Balance: types.FromFil(1200),
|
|
|
|
},
|
|
|
|
a2: {
|
|
|
|
Nonce: 0,
|
|
|
|
Balance: types.FromFil(1000),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
af := func(ctx context.Context, addr address.Address, tsk types.TipSetKey) (*types.Actor, error) {
|
|
|
|
return actors[addr], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
msgs := []types.Message{
|
|
|
|
{
|
|
|
|
From: a1,
|
|
|
|
To: a1,
|
|
|
|
Nonce: 0,
|
|
|
|
Value: types.FromFil(500),
|
|
|
|
GasLimit: 2_000_000_000,
|
|
|
|
GasPrice: types.NewInt(100),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
From: a1,
|
|
|
|
To: a1,
|
|
|
|
Nonce: 1,
|
|
|
|
Value: types.FromFil(500),
|
|
|
|
GasLimit: 2_000_000_000,
|
|
|
|
GasPrice: types.NewInt(1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
From: a2,
|
|
|
|
To: a1,
|
|
|
|
Nonce: 0,
|
|
|
|
Value: types.FromFil(1),
|
|
|
|
GasLimit: 1_000_000_000,
|
|
|
|
GasPrice: types.NewInt(10),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
From: a2,
|
|
|
|
To: a1,
|
|
|
|
Nonce: 1,
|
|
|
|
Value: types.FromFil(1),
|
|
|
|
GasLimit: 1_000_000_000,
|
|
|
|
GasPrice: types.NewInt(1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
From: a2,
|
|
|
|
To: a1,
|
|
|
|
Nonce: 2,
|
|
|
|
Value: types.FromFil(1),
|
|
|
|
GasLimit: 100_000_000,
|
|
|
|
GasPrice: types.NewInt(1),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
outmsgs, err := SelectMessages(ctx, af, &types.TipSet{}, wrapMsgs(msgs))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, outmsgs, 5, "filtering didnt work as expected")
|
|
|
|
}
|