lotus/cli/services_send_test.go

220 lines
4.7 KiB
Go
Raw Normal View History

feat: Add additional test annotations (#8272) * Annotate api,proxy_util,blockstore_badger, policy tests * Annotate splitstore: bsbadger / markset * Annotate splitstore feature * Annotate union/timed blockstore tests * Annotate openrpc, diff_adt tests * Annotate error,drand,events tests * Annotate predicates_test * Fix annotations * Annotate tscache, gen tests * Annotate fundmanager test * Annotate repub and selection tests * Annotate statetree_test * Annotate forks_test * Annotate searchwait_test.go * Fix duplicated @@ symbols * Annotate chain stmgr/store tests * Annotate more (types) tests * More tests annotated * Annotate conformance chaos actor tests * Annotate more integration tests * Annotate journal system tests * Annotate more tests. * Annotate gas,head buffer behaviors * Fix markset annotations * doc: test annotations for the markets dagstore wrapper * Annotate miner_api test in dagstore * Annotate more test files * Remove bad annotations from fsrepo * Annotate wdpost system * Remove bad annotations * Renamce "conformance" to "chaos_actor" tests * doc: stm annotations for blockheader & election proof tests * Annotate remaining "A" tests * annotate: stm for error_test * memrepo_test.go * Annotate "b" file tests * message_test.go * doc: stm annotate for fsrepo_test * Annotate "c" file tests * Annotate "D" test files * message_test.go * doc: stm annotate for chain, node/config & client * docs: stm annotate node_test * Annotate u,v,wl tests * doc: stm annotations for various test files * Annotate "T" test files * doc: stm annotate for proxy_util_test & policy_test * doc: stm annotate for various tests * doc: final few stm annotations * Add mempool unit tests * Add two more memPool Add tests * Update submodules * Add check function tests * Add stm annotations, refactor test helper * Annotate api,proxy_util,blockstore_badger, policy tests * Annotate splitstore: bsbadger / markset solving merge conflicts * Annotate splitstore feature * Annotate union/timed blockstore tests * Annotate openrpc, diff_adt tests * Annotate error,drand,events tests * Annotate predicates_test * Fix annotations * Annotate tscache, gen tests * Annotate fundmanager test * Annotate statetree_test * Annotate forks_test * Annotate searchwait_test.go * Fix duplicated @@ symbols * Annotate chain stmgr/store tests * Annotate more (types) tests * More tests annotated * Annotate conformance chaos actor tests * Annotate more integration tests * Annotate journal system tests * Annotate more tests. * Annotate gas,head buffer behaviors solve merge conflict * Fix markset annotations * Annotate miner_api test in dagstore * Annotate more test files * doc: test annotations for the markets dagstore wrapper * Annotate wdpost system * Renamce "conformance" to "chaos_actor" tests * Annotate remaining "A" tests * doc: stm annotations for blockheader & election proof tests * annotate: stm for error_test * Annotate "b" file tests * memrepo_test.go * Annotate "c" file tests * message_test.go * Annotate "D" test files * doc: stm annotate for fsrepo_test * Annotate u,v,wl tests * message_test.go * doc: stm annotate for chain, node/config & client * docs: stm annotate node_test * Annotate "T" test files * doc: stm annotations for various test files * Add mempool unit tests solve merge conflict * doc: stm annotate for proxy_util_test & policy_test * doc: stm annotate for various tests * doc: final few stm annotations * Add two more memPool Add tests * Update submodules * Add check function tests solve conflict * Add stm annotations, refactor test helper solve merge conflict * Change CLI test kinds to "unit" * Fix double merged test * Fix ccupgrade_test merge * Fix lint issues * Add stm annotation to types_Test * Test vectors submodule * Add file annotation to burn_test Co-authored-by: Nikola Divic <divicnikola@gmail.com> Co-authored-by: TheMenko <themenkoprojects@gmail.com>
2022-03-16 17:37:34 +00:00
//stm: ignore
//stm: #unit
package cli
import (
"context"
"fmt"
"testing"
"github.com/golang/mock/gomock"
2022-06-14 15:00:51 +00:00
"github.com/stretchr/testify/assert"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/crypto"
2022-06-14 15:00:51 +00:00
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/mocks"
"github.com/filecoin-project/lotus/chain/types"
)
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
}
// linter doesn't like dead code, so these are commented out.
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 {
proto, ok := x.(*api.MessagePrototype)
if !ok {
return false
}
m := &proto.Message
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
srvcs, _ := setupMockSrvcs(t)
defer srvcs.Close() //nolint:errcheck
proto, err := srvcs.MessageForSend(ctx, params)
assert.NoError(t, err)
assert.True(t, MessageMatcher(params).Matches(proto))
})
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
gomock.InOrder(
mockApi.EXPECT().WalletDefaultAddress(ctxM).Return(a1, nil),
)
proto, err := srvcs.MessageForSend(ctx, params)
assert.NoError(t, err)
assert.True(t, mm.Matches(proto))
})
t.Run("set-nonce", func(t *testing.T) {
params := params
n := uint64(5)
params.Nonce = &n
mm := MessageMatcher(params)
srvcs, _ := setupMockSrvcs(t)
defer srvcs.Close() //nolint:errcheck
proto, err := srvcs.MessageForSend(ctx, params)
assert.NoError(t, err)
assert.True(t, mm.Matches(proto))
})
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
mm := MessageMatcher(params)
srvcs, _ := setupMockSrvcs(t)
defer srvcs.Close() //nolint:errcheck
proto, err := srvcs.MessageForSend(ctx, params)
assert.NoError(t, err)
assert.True(t, mm.Matches(proto))
})
}