123 lines
3.3 KiB
Go
123 lines
3.3 KiB
Go
package base
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
gogoproto "github.com/cosmos/gogoproto/proto"
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/protobuf/runtime/protoiface"
|
|
|
|
signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1"
|
|
"cosmossdk.io/collections"
|
|
"cosmossdk.io/core/appmodule/v2"
|
|
"cosmossdk.io/core/event"
|
|
"cosmossdk.io/core/header"
|
|
"cosmossdk.io/core/store"
|
|
"cosmossdk.io/x/accounts/accountstd"
|
|
accountsv1 "cosmossdk.io/x/accounts/v1"
|
|
"cosmossdk.io/x/tx/signing"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
"github.com/cosmos/cosmos-sdk/runtime"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/tx"
|
|
)
|
|
|
|
type ProtoMsg = protoiface.MessageV1
|
|
|
|
// mock statecodec
|
|
type mockStateCodec struct {
|
|
codec.Codec
|
|
}
|
|
|
|
var _ codec.Codec = mockStateCodec{}
|
|
|
|
func (c mockStateCodec) Marshal(m gogoproto.Message) ([]byte, error) {
|
|
// Size() check can catch the typed nil value.
|
|
if m == nil || gogoproto.Size(m) == 0 {
|
|
// return empty bytes instead of nil, because nil has special meaning in places like store.Set
|
|
return []byte{}, nil
|
|
}
|
|
|
|
return gogoproto.Marshal(m)
|
|
}
|
|
|
|
func (c mockStateCodec) Unmarshal(bz []byte, ptr gogoproto.Message) error {
|
|
err := gogoproto.Unmarshal(bz, ptr)
|
|
|
|
return err
|
|
}
|
|
|
|
// mock address codec
|
|
type addressCodec struct{}
|
|
|
|
func (a addressCodec) StringToBytes(text string) ([]byte, error) { return []byte(text), nil }
|
|
func (a addressCodec) BytesToString(bz []byte) (string, error) { return string(bz), nil }
|
|
|
|
func newMockContext(t *testing.T) (context.Context, store.KVStoreService) {
|
|
t.Helper()
|
|
return accountstd.NewMockContext(
|
|
0, []byte("mock_base_account"), []byte("sender"), nil, func(ctx context.Context, sender []byte, msg, msgResp ProtoMsg) error {
|
|
return nil
|
|
}, func(ctx context.Context, sender []byte, msg ProtoMsg) (ProtoMsg, error) {
|
|
return nil, nil
|
|
}, func(ctx context.Context, req, resp ProtoMsg) error {
|
|
_, ok := req.(*accountsv1.AccountNumberRequest)
|
|
require.True(t, ok)
|
|
gogoproto.Merge(resp.(gogoproto.Message), &accountsv1.AccountNumberResponse{
|
|
Number: 1,
|
|
})
|
|
return nil
|
|
},
|
|
)
|
|
}
|
|
|
|
func makeMockDependencies(storeservice store.KVStoreService) accountstd.Dependencies {
|
|
sb := collections.NewSchemaBuilder(storeservice)
|
|
|
|
return accountstd.Dependencies{
|
|
SchemaBuilder: sb,
|
|
AddressCodec: addressCodec{},
|
|
LegacyStateCodec: mockStateCodec{},
|
|
Environment: appmodule.Environment{
|
|
EventService: eventService{},
|
|
HeaderService: headerService{},
|
|
},
|
|
}
|
|
}
|
|
|
|
type headerService struct{}
|
|
|
|
func (h headerService) HeaderInfo(context.Context) header.Info {
|
|
return header.Info{
|
|
ChainID: "test",
|
|
}
|
|
}
|
|
|
|
type eventService struct{}
|
|
|
|
// EventManager implements event.Service.
|
|
func (eventService) EventManager(context.Context) event.Manager {
|
|
return runtime.EventService{Events: runtime.Events{EventManagerI: sdk.NewEventManager()}}
|
|
}
|
|
|
|
var _ signing.SignModeHandler = directHandler{}
|
|
|
|
type directHandler struct{}
|
|
|
|
func (s directHandler) Mode() signingv1beta1.SignMode {
|
|
return signingv1beta1.SignMode_SIGN_MODE_DIRECT
|
|
}
|
|
|
|
func (s directHandler) GetSignBytes(_ context.Context, signerData signing.SignerData, txData signing.TxData) ([]byte, error) {
|
|
txDoc := tx.SignDoc{
|
|
BodyBytes: txData.BodyBytes,
|
|
AuthInfoBytes: txData.AuthInfoBytes,
|
|
ChainId: signerData.ChainID,
|
|
AccountNumber: signerData.AccountNumber,
|
|
}
|
|
|
|
return txDoc.Marshal()
|
|
}
|