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-15 14:50:32 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|