Generate mocks, integrate send service test
Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
parent
9553b32d52
commit
bad67acb4b
@ -32,6 +32,8 @@ import (
|
|||||||
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
"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
|
// FullNode API is a low-level interface to the Filecoin network full node
|
||||||
type FullNode interface {
|
type FullNode interface {
|
||||||
Common
|
Common
|
||||||
|
2972
api/mocks/mock_full.go
Normal file
2972
api/mocks/mock_full.go
Normal file
File diff suppressed because it is too large
Load Diff
@ -3,5 +3,6 @@
|
|||||||
package build
|
package build
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
_ "github.com/golang/mock/mockgen"
|
||||||
_ "github.com/whyrusleeping/bencher"
|
_ "github.com/whyrusleeping/bencher"
|
||||||
)
|
)
|
||||||
|
42
cli/send.go
42
cli/send.go
@ -1,18 +1,15 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
cid "github.com/ipfs/go-cid"
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"golang.org/x/xerrors"
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
"github.com/filecoin-project/go-address"
|
"github.com/filecoin-project/go-address"
|
||||||
"github.com/filecoin-project/go-state-types/abi"
|
"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/actors/builtin"
|
||||||
"github.com/filecoin-project/lotus/chain/types"
|
"github.com/filecoin-project/lotus/chain/types"
|
||||||
)
|
)
|
||||||
@ -98,18 +95,26 @@ var sendCmd = &cli.Command{
|
|||||||
params.From = addr
|
params.From = addr
|
||||||
}
|
}
|
||||||
|
|
||||||
gp, err := types.BigFromString(cctx.String("gas-premium"))
|
if cctx.IsSet("gas-premium") {
|
||||||
if err != nil {
|
gp, err := types.BigFromString(cctx.String("gas-premium"))
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
params.GasPremium = &gp
|
||||||
}
|
}
|
||||||
params.GasPremium = gp
|
|
||||||
|
|
||||||
gfc, err := types.BigFromString(cctx.String("gas-feecap"))
|
if cctx.IsSet("gas-feecap") {
|
||||||
if err != nil {
|
gfc, err := types.BigFromString(cctx.String("gas-feecap"))
|
||||||
return err
|
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"))
|
params.Method = abi.MethodNum(cctx.Uint64("method"))
|
||||||
|
|
||||||
@ -134,8 +139,8 @@ var sendCmd = &cli.Command{
|
|||||||
params.Force = cctx.Bool("force")
|
params.Force = cctx.Bool("force")
|
||||||
|
|
||||||
if cctx.IsSet("nonce") {
|
if cctx.IsSet("nonce") {
|
||||||
params.Nonce.Set = true
|
n := cctx.Uint64("nonce")
|
||||||
params.Nonce.N = cctx.Uint64("nonce")
|
params.Nonce = &n
|
||||||
}
|
}
|
||||||
|
|
||||||
msgCid, err := srv.Send(ctx, params)
|
msgCid, err := srv.Send(ctx, params)
|
||||||
@ -148,12 +153,3 @@ var sendCmd = &cli.Command{
|
|||||||
return nil
|
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)
|
|
||||||
}
|
|
||||||
|
@ -18,6 +18,8 @@ import (
|
|||||||
"golang.org/x/xerrors"
|
"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 {
|
type ServicesAPI interface {
|
||||||
// Sends executes a send given SendParams
|
// Sends executes a send given SendParams
|
||||||
Send(ctx context.Context, params SendParams) (cid.Cid, error)
|
Send(ctx context.Context, params SendParams) (cid.Cid, error)
|
||||||
@ -74,14 +76,11 @@ type SendParams struct {
|
|||||||
From address.Address
|
From address.Address
|
||||||
Val abi.TokenAmount
|
Val abi.TokenAmount
|
||||||
|
|
||||||
GasPremium abi.TokenAmount
|
GasPremium *abi.TokenAmount
|
||||||
GasFeeCap abi.TokenAmount
|
GasFeeCap *abi.TokenAmount
|
||||||
GasLimit int64
|
GasLimit *int64
|
||||||
|
|
||||||
Nonce struct {
|
Nonce *uint64
|
||||||
N uint64
|
|
||||||
Set bool
|
|
||||||
}
|
|
||||||
Method abi.MethodNum
|
Method abi.MethodNum
|
||||||
Params []byte
|
Params []byte
|
||||||
|
|
||||||
@ -106,14 +105,26 @@ func (s *ServicesImpl) Send(ctx context.Context, params SendParams) (cid.Cid, er
|
|||||||
To: params.To,
|
To: params.To,
|
||||||
Value: params.Val,
|
Value: params.Val,
|
||||||
|
|
||||||
GasPremium: params.GasPremium,
|
|
||||||
GasFeeCap: params.GasFeeCap,
|
|
||||||
GasLimit: params.GasLimit,
|
|
||||||
|
|
||||||
Method: params.Method,
|
Method: params.Method,
|
||||||
Params: params.Params,
|
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 {
|
if !params.Force {
|
||||||
// Funds insufficient check
|
// Funds insufficient check
|
||||||
fromBalance, err := s.api.WalletBalance(ctx, msg.From)
|
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 {
|
if params.Nonce != nil {
|
||||||
msg.Nonce = params.Nonce.N
|
msg.Nonce = *params.Nonce
|
||||||
sm, err := s.api.WalletSignMessage(ctx, params.From, msg)
|
sm, err := s.api.WalletSignMessage(ctx, params.From, msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cid.Undef, err
|
return cid.Undef, err
|
||||||
|
64
cli/services_send_test.go
Normal file
64
cli/services_send_test.go
Normal 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
81
cli/servicesmock_test.go
Normal 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
1
go.mod
@ -50,6 +50,7 @@ require (
|
|||||||
github.com/gbrlsnchs/jwt/v3 v3.0.0-beta.1
|
github.com/gbrlsnchs/jwt/v3 v3.0.0-beta.1
|
||||||
github.com/go-kit/kit v0.10.0
|
github.com/go-kit/kit v0.10.0
|
||||||
github.com/go-ole/go-ole v1.2.4 // indirect
|
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/google/uuid v1.1.2
|
||||||
github.com/gorilla/mux v1.7.4
|
github.com/gorilla/mux v1.7.4
|
||||||
github.com/gorilla/websocket v1.4.2
|
github.com/gorilla/websocket v1.4.2
|
||||||
|
Loading…
Reference in New Issue
Block a user