Generate mocks, integrate send service test

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2021-02-10 16:46:21 +01:00
parent 9553b32d52
commit bad67acb4b
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA
8 changed files with 3164 additions and 36 deletions

View File

@ -32,6 +32,8 @@ import (
"github.com/filecoin-project/lotus/node/modules/dtypes"
)
//go:generate go run github.com/golang/mock/mockgen -destination=mocks/mock_full.go -package=mocks . FullNode
// FullNode API is a low-level interface to the Filecoin network full node
type FullNode interface {
Common

2972
api/mocks/mock_full.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -3,5 +3,6 @@
package build
import (
_ "github.com/golang/mock/mockgen"
_ "github.com/whyrusleeping/bencher"
)

View File

@ -1,18 +1,15 @@
package cli
import (
"context"
"encoding/hex"
"fmt"
cid "github.com/ipfs/go-cid"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/types"
)
@ -98,18 +95,26 @@ var sendCmd = &cli.Command{
params.From = addr
}
gp, err := types.BigFromString(cctx.String("gas-premium"))
if err != nil {
return err
if cctx.IsSet("gas-premium") {
gp, err := types.BigFromString(cctx.String("gas-premium"))
if err != nil {
return err
}
params.GasPremium = &gp
}
params.GasPremium = gp
gfc, err := types.BigFromString(cctx.String("gas-feecap"))
if err != nil {
return err
if cctx.IsSet("gas-feecap") {
gfc, err := types.BigFromString(cctx.String("gas-feecap"))
if err != nil {
return err
}
params.GasFeeCap = &gfc
}
if cctx.IsSet("gas-limit") {
limit := cctx.Int64("gas-limit")
params.GasLimit = &limit
}
params.GasFeeCap = gfc
params.GasLimit = cctx.Int64("gas-limit")
params.Method = abi.MethodNum(cctx.Uint64("method"))
@ -134,8 +139,8 @@ var sendCmd = &cli.Command{
params.Force = cctx.Bool("force")
if cctx.IsSet("nonce") {
params.Nonce.Set = true
params.Nonce.N = cctx.Uint64("nonce")
n := cctx.Uint64("nonce")
params.Nonce = &n
}
msgCid, err := srv.Send(ctx, params)
@ -148,12 +153,3 @@ var sendCmd = &cli.Command{
return nil
},
}
type sendAPIs interface {
MpoolPush(context.Context, *types.SignedMessage) (cid.Cid, error)
MpoolPushMessage(ctx context.Context, msg *types.Message, spec *api.MessageSendSpec) (*types.SignedMessage, error)
WalletBalance(context.Context, address.Address) (types.BigInt, error)
WalletDefaultAddress(context.Context) (address.Address, error)
WalletSignMessage(context.Context, address.Address, *types.Message) (*types.SignedMessage, error)
}

View File

@ -18,6 +18,8 @@ import (
"golang.org/x/xerrors"
)
//go:generate go run github.com/golang/mock/mockgen -destination=servicesmock_test.go -package=cli -self_package github.com/filecoin-project/lotus/cli . ServicesAPI
type ServicesAPI interface {
// Sends executes a send given SendParams
Send(ctx context.Context, params SendParams) (cid.Cid, error)
@ -74,14 +76,11 @@ type SendParams struct {
From address.Address
Val abi.TokenAmount
GasPremium abi.TokenAmount
GasFeeCap abi.TokenAmount
GasLimit int64
GasPremium *abi.TokenAmount
GasFeeCap *abi.TokenAmount
GasLimit *int64
Nonce struct {
N uint64
Set bool
}
Nonce *uint64
Method abi.MethodNum
Params []byte
@ -106,14 +105,26 @@ func (s *ServicesImpl) Send(ctx context.Context, params SendParams) (cid.Cid, er
To: params.To,
Value: params.Val,
GasPremium: params.GasPremium,
GasFeeCap: params.GasFeeCap,
GasLimit: params.GasLimit,
Method: params.Method,
Params: params.Params,
}
if params.GasPremium != nil {
msg.GasPremium = *params.GasPremium
} else {
msg.GasPremium = types.NewInt(0)
}
if params.GasFeeCap != nil {
msg.GasFeeCap = *params.GasFeeCap
} else {
msg.GasFeeCap = types.NewInt(0)
}
if params.GasLimit != nil {
msg.GasLimit = *params.GasLimit
} else {
msg.GasLimit = 0
}
if !params.Force {
// Funds insufficient check
fromBalance, err := s.api.WalletBalance(ctx, msg.From)
@ -128,8 +139,8 @@ func (s *ServicesImpl) Send(ctx context.Context, params SendParams) (cid.Cid, er
}
}
if params.Nonce.Set {
msg.Nonce = params.Nonce.N
if params.Nonce != nil {
msg.Nonce = *params.Nonce
sm, err := s.api.WalletSignMessage(ctx, params.From, msg)
if err != nil {
return cid.Undef, err

64
cli/services_send_test.go Normal file
View File

@ -0,0 +1,64 @@
package cli
import (
"context"
"testing"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/lotus/api/mocks"
types "github.com/filecoin-project/lotus/chain/types"
gomock "github.com/golang/mock/gomock"
cid "github.com/ipfs/go-cid"
"github.com/stretchr/testify/assert"
)
func TestSendService(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockApi := mocks.NewMockFullNode(mockCtrl)
srvcs := &ServicesImpl{
api: mockApi,
closer: func() {},
}
addrGen := address.NewForTestGetter()
a1 := addrGen()
a2 := addrGen()
const balance = 10000
params := SendParams{
From: a1,
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)
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)
}

81
cli/servicesmock_test.go Normal file
View File

@ -0,0 +1,81 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/filecoin-project/lotus/cli (interfaces: ServicesAPI)
// Package cli is a generated GoMock package.
package cli
import (
context "context"
go_address "github.com/filecoin-project/go-address"
abi "github.com/filecoin-project/go-state-types/abi"
gomock "github.com/golang/mock/gomock"
go_cid "github.com/ipfs/go-cid"
reflect "reflect"
)
// MockServicesAPI is a mock of ServicesAPI interface
type MockServicesAPI struct {
ctrl *gomock.Controller
recorder *MockServicesAPIMockRecorder
}
// MockServicesAPIMockRecorder is the mock recorder for MockServicesAPI
type MockServicesAPIMockRecorder struct {
mock *MockServicesAPI
}
// NewMockServicesAPI creates a new mock instance
func NewMockServicesAPI(ctrl *gomock.Controller) *MockServicesAPI {
mock := &MockServicesAPI{ctrl: ctrl}
mock.recorder = &MockServicesAPIMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockServicesAPI) EXPECT() *MockServicesAPIMockRecorder {
return m.recorder
}
// Close mocks base method
func (m *MockServicesAPI) Close() error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Close")
ret0, _ := ret[0].(error)
return ret0
}
// Close indicates an expected call of Close
func (mr *MockServicesAPIMockRecorder) Close() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockServicesAPI)(nil).Close))
}
// DecodeTypedParamsFromJSON mocks base method
func (m *MockServicesAPI) DecodeTypedParamsFromJSON(arg0 context.Context, arg1 go_address.Address, arg2 abi.MethodNum, arg3 string) ([]byte, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "DecodeTypedParamsFromJSON", arg0, arg1, arg2, arg3)
ret0, _ := ret[0].([]byte)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// DecodeTypedParamsFromJSON indicates an expected call of DecodeTypedParamsFromJSON
func (mr *MockServicesAPIMockRecorder) DecodeTypedParamsFromJSON(arg0, arg1, arg2, arg3 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DecodeTypedParamsFromJSON", reflect.TypeOf((*MockServicesAPI)(nil).DecodeTypedParamsFromJSON), arg0, arg1, arg2, arg3)
}
// Send mocks base method
func (m *MockServicesAPI) Send(arg0 context.Context, arg1 SendParams) (go_cid.Cid, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Send", arg0, arg1)
ret0, _ := ret[0].(go_cid.Cid)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Send indicates an expected call of Send
func (mr *MockServicesAPIMockRecorder) Send(arg0, arg1 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Send", reflect.TypeOf((*MockServicesAPI)(nil).Send), arg0, arg1)
}

1
go.mod
View File

@ -50,6 +50,7 @@ require (
github.com/gbrlsnchs/jwt/v3 v3.0.0-beta.1
github.com/go-kit/kit v0.10.0
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/golang/mock v1.4.4
github.com/google/uuid v1.1.2
github.com/gorilla/mux v1.7.4
github.com/gorilla/websocket v1.4.2