2022-08-29 14:25:30 +00:00
|
|
|
// stm: ignore
|
|
|
|
// stm: #unit
|
2021-02-23 14:50:47 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2022-06-15 10:06:22 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2021-02-23 14:50:47 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2021-03-25 14:24:09 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
2021-02-23 14:50:47 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2022-06-15 10:06:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/mocks"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2021-02-23 14:50:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type markerKeyType struct{}
|
|
|
|
|
|
|
|
var markerKey = markerKeyType{}
|
|
|
|
|
|
|
|
type contextMatcher struct {
|
|
|
|
marker *int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matches returns whether x is a match.
|
|
|
|
func (cm contextMatcher) Matches(x interface{}) bool {
|
|
|
|
ctx, ok := x.(context.Context)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
maybeMarker, ok := ctx.Value(markerKey).(*int)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return cm.marker == maybeMarker
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm contextMatcher) String() string {
|
|
|
|
return fmt.Sprintf("Context with Value(%v/%T, %p)", markerKey, markerKey, cm.marker)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ContextWithMarker(ctx context.Context) (context.Context, gomock.Matcher) {
|
|
|
|
marker := new(int)
|
|
|
|
outCtx := context.WithValue(ctx, markerKey, marker)
|
|
|
|
return outCtx, contextMatcher{marker: marker}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupMockSrvcs(t *testing.T) (*ServicesImpl, *mocks.MockFullNode) {
|
|
|
|
mockCtrl := gomock.NewController(t)
|
|
|
|
|
|
|
|
mockApi := mocks.NewMockFullNode(mockCtrl)
|
|
|
|
|
|
|
|
srvcs := &ServicesImpl{
|
|
|
|
api: mockApi,
|
|
|
|
closer: mockCtrl.Finish,
|
|
|
|
}
|
|
|
|
return srvcs, mockApi
|
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
// linter doesn't like dead code, so these are commented out.
|
2021-03-25 14:24:09 +00:00
|
|
|
func fakeSign(msg *types.Message) *types.SignedMessage {
|
|
|
|
return &types.SignedMessage{
|
|
|
|
Message: *msg,
|
|
|
|
Signature: crypto.Signature{Type: crypto.SigTypeSecp256k1, Data: make([]byte, 32)},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//func makeMessageSigner() (*cid.Cid, interface{}) {
|
|
|
|
//smCid := cid.Undef
|
|
|
|
//return &smCid,
|
|
|
|
//func(_ context.Context, msg *types.Message, _ *api.MessageSendSpec) (*types.SignedMessage, error) {
|
|
|
|
//sm := fakeSign(msg)
|
|
|
|
//smCid = sm.Cid()
|
|
|
|
//return sm, nil
|
|
|
|
//}
|
|
|
|
//}
|
2021-02-23 14:50:47 +00:00
|
|
|
|
|
|
|
type MessageMatcher SendParams
|
|
|
|
|
|
|
|
var _ gomock.Matcher = MessageMatcher{}
|
|
|
|
|
|
|
|
// Matches returns whether x is a match.
|
|
|
|
func (mm MessageMatcher) Matches(x interface{}) bool {
|
2021-03-27 14:35:46 +00:00
|
|
|
proto, ok := x.(*api.MessagePrototype)
|
2021-02-23 14:50:47 +00:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
m := &proto.Message
|
|
|
|
|
2021-02-23 14:50:47 +00:00
|
|
|
if mm.From != address.Undef && mm.From != m.From {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if mm.To != address.Undef && mm.To != m.To {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if types.BigCmp(mm.Val, m.Value) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if mm.Nonce != nil && *mm.Nonce != m.Nonce {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if mm.GasPremium != nil && big.Cmp(*mm.GasPremium, m.GasPremium) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if mm.GasPremium == nil && m.GasPremium.Sign() != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if mm.GasFeeCap != nil && big.Cmp(*mm.GasFeeCap, m.GasFeeCap) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if mm.GasFeeCap == nil && m.GasFeeCap.Sign() != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if mm.GasLimit != nil && *mm.GasLimit != m.GasLimit {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if mm.GasLimit == nil && m.GasLimit != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// handle rest of options
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// String describes what the matcher matches.
|
|
|
|
func (mm MessageMatcher) String() string {
|
|
|
|
return fmt.Sprintf("%#v", SendParams(mm))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendService(t *testing.T) {
|
|
|
|
addrGen := address.NewForTestGetter()
|
|
|
|
a1 := addrGen()
|
|
|
|
a2 := addrGen()
|
|
|
|
|
|
|
|
const balance = 10000
|
|
|
|
|
|
|
|
params := SendParams{
|
|
|
|
From: a1,
|
|
|
|
To: a2,
|
|
|
|
Val: types.NewInt(balance - 100),
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, ctxM := ContextWithMarker(context.Background())
|
|
|
|
|
|
|
|
t.Run("happy", func(t *testing.T) {
|
|
|
|
params := params
|
2021-03-24 14:12:35 +00:00
|
|
|
srvcs, _ := setupMockSrvcs(t)
|
2021-02-23 14:50:47 +00:00
|
|
|
defer srvcs.Close() //nolint:errcheck
|
|
|
|
|
2021-03-24 14:12:35 +00:00
|
|
|
proto, err := srvcs.MessageForSend(ctx, params)
|
2021-02-23 14:50:47 +00:00
|
|
|
assert.NoError(t, err)
|
2021-03-24 14:12:35 +00:00
|
|
|
assert.True(t, MessageMatcher(params).Matches(proto))
|
2021-02-23 14:50:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("default-from", func(t *testing.T) {
|
|
|
|
params := params
|
|
|
|
params.From = address.Undef
|
|
|
|
mm := MessageMatcher(params)
|
|
|
|
mm.From = a1
|
|
|
|
|
|
|
|
srvcs, mockApi := setupMockSrvcs(t)
|
|
|
|
defer srvcs.Close() //nolint:errcheck
|
2021-03-24 14:12:35 +00:00
|
|
|
|
2021-02-23 14:50:47 +00:00
|
|
|
gomock.InOrder(
|
|
|
|
mockApi.EXPECT().WalletDefaultAddress(ctxM).Return(a1, nil),
|
|
|
|
)
|
|
|
|
|
2021-03-24 14:12:35 +00:00
|
|
|
proto, err := srvcs.MessageForSend(ctx, params)
|
2021-02-23 14:50:47 +00:00
|
|
|
assert.NoError(t, err)
|
2021-03-24 14:12:35 +00:00
|
|
|
assert.True(t, mm.Matches(proto))
|
2021-02-23 14:50:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("set-nonce", func(t *testing.T) {
|
|
|
|
params := params
|
|
|
|
n := uint64(5)
|
|
|
|
params.Nonce = &n
|
|
|
|
mm := MessageMatcher(params)
|
|
|
|
|
2021-03-24 14:12:35 +00:00
|
|
|
srvcs, _ := setupMockSrvcs(t)
|
2021-02-23 14:50:47 +00:00
|
|
|
defer srvcs.Close() //nolint:errcheck
|
|
|
|
|
2021-03-24 14:12:35 +00:00
|
|
|
proto, err := srvcs.MessageForSend(ctx, params)
|
2021-02-23 14:50:47 +00:00
|
|
|
assert.NoError(t, err)
|
2021-03-24 14:12:35 +00:00
|
|
|
assert.True(t, mm.Matches(proto))
|
2021-02-23 14:50:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("gas-params", func(t *testing.T) {
|
|
|
|
params := params
|
|
|
|
limit := int64(1)
|
|
|
|
params.GasLimit = &limit
|
|
|
|
gfc := big.NewInt(100)
|
|
|
|
params.GasFeeCap = &gfc
|
|
|
|
gp := big.NewInt(10)
|
|
|
|
params.GasPremium = &gp
|
|
|
|
|
2021-03-24 14:12:35 +00:00
|
|
|
mm := MessageMatcher(params)
|
|
|
|
|
|
|
|
srvcs, _ := setupMockSrvcs(t)
|
2021-02-23 14:50:47 +00:00
|
|
|
defer srvcs.Close() //nolint:errcheck
|
|
|
|
|
2021-03-24 14:12:35 +00:00
|
|
|
proto, err := srvcs.MessageForSend(ctx, params)
|
2021-02-23 14:50:47 +00:00
|
|
|
assert.NoError(t, err)
|
2021-03-24 14:12:35 +00:00
|
|
|
assert.True(t, mm.Matches(proto))
|
|
|
|
|
2021-02-23 14:50:47 +00:00
|
|
|
})
|
|
|
|
}
|