Finish Send CLI service tests

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2021-02-12 17:41:22 +01:00
parent bad67acb4b
commit 3de893542b
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA
3 changed files with 203 additions and 25 deletions

View File

@ -2,6 +2,7 @@ package cli
import (
"encoding/hex"
"errors"
"fmt"
"github.com/urfave/cli/v2"
@ -146,6 +147,10 @@ var sendCmd = &cli.Command{
msgCid, err := srv.Send(ctx, params)
if err != nil {
if errors.Is(err, ErrSendBalanceTooLow) {
fmt.Printf("%s\n", err.Error())
return fmt.Errorf("--force must be specified for this action to have an effect; you have been warned: %w", err)
}
return xerrors.Errorf("executing send: %w", err)
}

View File

@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
@ -91,6 +92,8 @@ type SendParams struct {
// There might be room for generic Send that other commands can use to send their messages
// We will see
var ErrSendBalanceTooLow = errors.New("balance too low")
func (s *ServicesImpl) Send(ctx context.Context, params SendParams) (cid.Cid, error) {
if params.From == address.Undef {
defaddr, err := s.api.WalletDefaultAddress(ctx)
@ -134,8 +137,8 @@ func (s *ServicesImpl) Send(ctx context.Context, params SendParams) (cid.Cid, er
totalCost := types.BigAdd(types.BigMul(msg.GasFeeCap, types.NewInt(uint64(msg.GasLimit))), msg.Value)
if fromBalance.LessThan(totalCost) {
fmt.Printf("WARNING: From balance %s less than total cost %s\n", types.FIL(fromBalance), types.FIL(totalCost))
return cid.Undef, fmt.Errorf("--force must be specified for this action to have an effect; you have been warned")
return cid.Undef, xerrors.Errorf("From balance %s less than total cost %s: %w", types.FIL(fromBalance), types.FIL(totalCost), ErrSendBalanceTooLow)
}
}

View File

@ -2,10 +2,13 @@ package cli
import (
"context"
"fmt"
"testing"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/mocks"
types "github.com/filecoin-project/lotus/chain/types"
gomock "github.com/golang/mock/gomock"
@ -13,17 +16,92 @@ import (
"github.com/stretchr/testify/assert"
)
func TestSendService(t *testing.T) {
func setupMockSrvcs(t *testing.T) (*ServicesImpl, *mocks.MockFullNode) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockApi := mocks.NewMockFullNode(mockCtrl)
srvcs := &ServicesImpl{
api: mockApi,
closer: func() {},
closer: mockCtrl.Finish,
}
return srvcs, mockApi
}
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
}
}
type MessageMatcher SendParams
var _ gomock.Matcher = MessageMatcher{}
// Matches returns whether x is a match.
func (mm MessageMatcher) Matches(x interface{}) bool {
m, ok := x.(*types.Message)
if !ok {
return false
}
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()
@ -35,30 +113,122 @@ func TestSendService(t *testing.T) {
To: a2,
Val: types.NewInt(balance - 100),
}
ctx, done := context.WithCancel(context.Background())
defer done()
msgCid := cid.Undef
gomock.InOrder(
mockApi.EXPECT().WalletBalance(ctx, params.From).Return(types.NewInt(balance), nil),
mockApi.EXPECT().MpoolPushMessage(ctx, gomock.Any(), nil).DoAndReturn(
func(_ context.Context, msg *types.Message, _ interface{}) (*types.SignedMessage, error) {
msgCid = msg.Cid()
assert.Equal(t, params.From, msg.From)
assert.Equal(t, params.To, msg.To)
assert.Equal(t, params.Val, msg.Value)
t.Run("happy", func(t *testing.T) {
params := params
srvcs, mockApi := setupMockSrvcs(t)
defer srvcs.Close()
msgCid, sign := makeMessageSigner()
gomock.InOrder(
mockApi.EXPECT().WalletBalance(ctx, params.From).Return(types.NewInt(balance), nil),
mockApi.EXPECT().MpoolPushMessage(ctx, MessageMatcher(params), nil).DoAndReturn(sign),
)
sm := types.SignedMessage{
Message: *msg,
Signature: crypto.Signature{1, []byte{1}},
}
msgCid = sm.Cid()
return &sm, nil
}),
)
c, err := srvcs.Send(ctx, params)
assert.NoError(t, err)
assert.Equal(t, *msgCid, c)
})
c, err := srvcs.Send(ctx, params)
assert.NoError(t, err)
assert.Equal(t, msgCid, c)
t.Run("balance-too-low", func(t *testing.T) {
params := params
srvcs, mockApi := setupMockSrvcs(t)
defer srvcs.Close()
gomock.InOrder(
mockApi.EXPECT().WalletBalance(ctx, a1).Return(types.NewInt(balance-200), nil),
// no MpoolPushMessage
)
c, err := srvcs.Send(ctx, params)
assert.Equal(t, c, cid.Undef)
assert.ErrorIs(t, err, ErrSendBalanceTooLow)
})
t.Run("force", func(t *testing.T) {
params := params
params.Force = true
srvcs, mockApi := setupMockSrvcs(t)
defer srvcs.Close()
msgCid, sign := makeMessageSigner()
gomock.InOrder(
mockApi.EXPECT().WalletBalance(ctx, a1).Return(types.NewInt(balance-200), nil).AnyTimes(),
mockApi.EXPECT().MpoolPushMessage(ctx, MessageMatcher(params), nil).DoAndReturn(sign),
)
c, err := srvcs.Send(ctx, params)
assert.NoError(t, err)
assert.Equal(t, *msgCid, c)
})
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()
msgCid, sign := makeMessageSigner()
gomock.InOrder(
mockApi.EXPECT().WalletDefaultAddress(ctx).Return(a1, nil),
mockApi.EXPECT().WalletBalance(ctx, a1).Return(types.NewInt(balance), nil),
mockApi.EXPECT().MpoolPushMessage(ctx, mm, nil).DoAndReturn(sign),
)
c, err := srvcs.Send(ctx, params)
assert.NoError(t, err)
assert.Equal(t, *msgCid, c)
})
t.Run("set-nonce", func(t *testing.T) {
params := params
n := uint64(5)
params.Nonce = &n
mm := MessageMatcher(params)
srvcs, mockApi := setupMockSrvcs(t)
defer srvcs.Close()
_, _ = mm, mockApi
var sm *types.SignedMessage
gomock.InOrder(
mockApi.EXPECT().WalletBalance(ctx, a1).Return(types.NewInt(balance), nil),
mockApi.EXPECT().WalletSignMessage(ctx, a1, mm).DoAndReturn(
func(_ context.Context, _ address.Address, msg *types.Message) (*types.SignedMessage, error) {
sm = fakeSign(msg)
// now we expect MpoolPush with that SignedMessage
mockApi.EXPECT().MpoolPush(ctx, sm).Return(sm.Cid(), nil)
return sm, nil
}),
)
c, err := srvcs.Send(ctx, params)
assert.NoError(t, err)
assert.Equal(t, sm.Cid(), c)
})
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
srvcs, mockApi := setupMockSrvcs(t)
defer srvcs.Close()
msgCid, sign := makeMessageSigner()
gomock.InOrder(
mockApi.EXPECT().WalletBalance(ctx, params.From).Return(types.NewInt(balance), nil),
mockApi.EXPECT().MpoolPushMessage(ctx, MessageMatcher(params), nil).DoAndReturn(sign),
)
c, err := srvcs.Send(ctx, params)
assert.NoError(t, err)
assert.Equal(t, *msgCid, c)
})
}