Skip to content

Commit

Permalink
merge changes
Browse files Browse the repository at this point in the history
Signed-off-by: Deepanshu Tripathi <[email protected]>
  • Loading branch information
deepanshutr committed Sep 2, 2024
1 parent 362514f commit a4142b7
Show file tree
Hide file tree
Showing 10 changed files with 1,854 additions and 969 deletions.
529 changes: 383 additions & 146 deletions x/assets/transactions/burn/transaction_keeper_test.go

Large diffs are not rendered by default.

339 changes: 197 additions & 142 deletions x/assets/transactions/define/transaction_keeper_test.go

Large diffs are not rendered by default.

337 changes: 206 additions & 131 deletions x/assets/transactions/deputize/transaction_keeper_test.go

Large diffs are not rendered by default.

468 changes: 323 additions & 145 deletions x/assets/transactions/mint/transaction_keeper_test.go

Large diffs are not rendered by default.

311 changes: 234 additions & 77 deletions x/assets/transactions/mutate/transaction_keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,110 +4,267 @@
package mutate

import (
"fmt"
storeTypes "github.com/cosmos/cosmos-sdk/store/types"
"reflect"
"testing"

tendermintDB "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/libs/log"
protoTendermintTypes "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/store"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
paramsKeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
"github.com/stretchr/testify/require"

"context"
"github.com/AssetMantle/modules/helpers"
baseHelpers "github.com/AssetMantle/modules/helpers/base"
errorConstants "github.com/AssetMantle/modules/helpers/constants"
"github.com/AssetMantle/modules/x/assets/constants"
"github.com/AssetMantle/modules/x/assets/mapper"
"github.com/AssetMantle/modules/x/assets/parameters"
recordassets "github.com/AssetMantle/modules/x/assets/record"
"github.com/AssetMantle/modules/x/classifications/auxiliaries/conform"
"github.com/AssetMantle/modules/x/identities/auxiliaries/authenticate"
"github.com/AssetMantle/modules/x/maintainers/auxiliaries/maintain"
baseDocuments "github.com/AssetMantle/schema/documents/base"
"github.com/AssetMantle/schema/ids"
baseIDs "github.com/AssetMantle/schema/ids/base"
"github.com/AssetMantle/schema/lists"
baseLists "github.com/AssetMantle/schema/lists/base"
baseQualified "github.com/AssetMantle/schema/qualified/base"
tendermintDB "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cometbft/cometbft/libs/log"
protoTendermintTypes "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/store"
storeTypes "github.com/cosmos/cosmos-sdk/store/types"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
authKeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authTypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankKeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
bankTypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govTypes "github.com/cosmos/cosmos-sdk/x/gov/types"
paramsTypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/stretchr/testify/mock"
"reflect"
"testing"
)

var (
authenticateAuxiliary helpers.Auxiliary
maintainAuxiliary helpers.Auxiliary
conformAuxiliary helpers.Auxiliary
)
type MockAuxiliary struct {
mock.Mock
}

type TestKeepers struct {
MutateKeeper helpers.TransactionKeeper
var _ helpers.Auxiliary = (*MockAuxiliary)(nil)

func (mockAuxiliary *MockAuxiliary) GetName() string { panic(mockAuxiliary) }
func (mockAuxiliary *MockAuxiliary) GetKeeper() helpers.AuxiliaryKeeper {
args := mockAuxiliary.Called()
return args.Get(0).(helpers.AuxiliaryKeeper)
}
func (mockAuxiliary *MockAuxiliary) Initialize(_ helpers.Mapper, _ helpers.ParameterManager, _ ...interface{}) helpers.Auxiliary {
panic(mockAuxiliary)
}

func createTestInput(t *testing.T) (sdkTypes.Context, TestKeepers, helpers.Mapper, helpers.ParameterManager) {
// var legacyAmino = codec.NewLegacyAmino()
// schemaCodec.RegisterLegacyAminoCodec(legacyAmino)
// std.RegisterLegacyAminoCodec(legacyAmino)
// legacyAmino.Seal()
codec := baseHelpers.CodecPrototype()
storeKey := sdkTypes.NewKVStoreKey("test")
paramsStoreKey := sdkTypes.NewKVStoreKey("testParams")
paramsTransientStoreKeys := sdkTypes.NewTransientStoreKey("testParamsTransient")
Mapper := mapper.Prototype().Initialize(storeKey)
ParamsKeeper := paramsKeeper.NewKeeper(
codec.GetProtoCodec(),
codec.GetLegacyAmino(),
paramsStoreKey,
paramsTransientStoreKeys,
)
parameterManager := parameters.Prototype().Initialize(ParamsKeeper.Subspace("test"))
type MockAuxiliaryKeeper struct {
mock.Mock
}

memDB := tendermintDB.NewMemDB()
commitMultiStore := store.NewCommitMultiStore(memDB)
commitMultiStore.MountStoreWithDB(storeKey, storeTypes.StoreTypeIAVL, memDB)
commitMultiStore.MountStoreWithDB(paramsStoreKey, storeTypes.StoreTypeIAVL, memDB)
commitMultiStore.MountStoreWithDB(paramsTransientStoreKeys, storeTypes.StoreTypeTransient, memDB)
err := commitMultiStore.LoadLatestVersion()
require.Nil(t, err)
var _ helpers.AuxiliaryKeeper = (*MockAuxiliaryKeeper)(nil)

context := sdkTypes.NewContext(commitMultiStore, protoTendermintTypes.Header{
ChainID: "test",
}, false, log.NewNopLogger())
func (mockAuxiliaryKeeper *MockAuxiliaryKeeper) Help(context context.Context, auxiliaryRequest helpers.AuxiliaryRequest) (helpers.AuxiliaryResponse, error) {
args := mockAuxiliaryKeeper.Called(context, auxiliaryRequest)
return args.Get(0).(helpers.AuxiliaryResponse), args.Error(1)
}
func (mockAuxiliaryKeeper *MockAuxiliaryKeeper) Initialize(mapper helpers.Mapper, parameterManager helpers.ParameterManager, i []interface{}) helpers.Keeper {
args := mockAuxiliaryKeeper.Called(mapper, parameterManager, i)
return args.Get(0).(helpers.Keeper)
}

const (
TestMinterModuleName = "testMinter"
Denom = "stake"
ChainID = "testChain"
GenesisSupply = 1000000000000
)

var (
moduleStoreKey = sdkTypes.NewKVStoreKey(constants.ModuleName)

authenticateAuxiliaryKeeper = new(MockAuxiliaryKeeper)
authenticateAuxiliaryFailureAddress = sdkTypes.AccAddress(ed25519.GenPrivKey().PubKey().Address())
_ = authenticateAuxiliaryKeeper.On("Help", mock.Anything, authenticate.NewAuxiliaryRequest(authenticateAuxiliaryFailureAddress, baseIDs.PrototypeIdentityID())).Return(new(helpers.AuxiliaryResponse), errorConstants.MockError)
_ = authenticateAuxiliaryKeeper.On("Help", mock.Anything, mock.Anything).Return(new(helpers.AuxiliaryResponse), nil)
authenticateAuxiliary = new(MockAuxiliary)
_ = authenticateAuxiliary.On("GetKeeper").Return(authenticateAuxiliaryKeeper)

maintainFailureClassificationID = testAsset.GetClassificationID()
maintainAuxiliaryKeeper = new(MockAuxiliaryKeeper)
maintainFailureID = baseIDs.NewIdentityID(classificationID, immutables)
maintainAuxiliary = new(MockAuxiliary)
_ = maintainAuxiliary.On("GetKeeper").Return(maintainAuxiliaryKeeper)

conformAuxiliaryKeeper = new(MockAuxiliaryKeeper)
conformAuxiliary = new(MockAuxiliary)
_ = conformAuxiliary.On("GetKeeper").Return(conformAuxiliaryKeeper)

testAsset = baseDocuments.NewAsset(baseIDs.NewClassificationID(immutables, mutables), baseQualified.NewImmutables(baseLists.NewPropertyList()), baseQualified.NewMutables(baseLists.NewPropertyList()))
testNewAssetID = baseIDs.NewAssetID(testAsset.GetClassificationID(), testAsset.GetImmutables()).(*baseIDs.AssetID)

codec = baseHelpers.TestCodec()

paramsStoreKey = sdkTypes.NewKVStoreKey(paramsTypes.StoreKey)
paramsTransientStoreKeys = sdkTypes.NewTransientStoreKey(paramsTypes.TStoreKey)

authStoreKey = sdkTypes.NewKVStoreKey(authTypes.StoreKey)
moduleAccountPermissions = map[string][]string{TestMinterModuleName: {authTypes.Minter}, constants.ModuleName: nil}
AuthKeeper = authKeeper.NewAccountKeeper(codec, authStoreKey, authTypes.ProtoBaseAccount, moduleAccountPermissions, sdkTypes.GetConfig().GetBech32AccountAddrPrefix(), authTypes.NewModuleAddress(govTypes.ModuleName).String())

bankStoreKey = sdkTypes.NewKVStoreKey(bankTypes.StoreKey)
blacklistedAddresses = map[string]bool{authTypes.NewModuleAddress(TestMinterModuleName).String(): false, authTypes.NewModuleAddress(constants.ModuleName).String(): false}
BankKeeper = bankKeeper.NewBaseKeeper(codec, bankStoreKey, AuthKeeper, blacklistedAddresses, authTypes.NewModuleAddress(govTypes.ModuleName).String())

maintainAuxiliary = maintain.Auxiliary.Initialize(Mapper, parameterManager)
conformAuxiliary = conform.Auxiliary.Initialize(Mapper, parameterManager)
authenticateAuxiliary = authenticate.Auxiliary.Initialize(Mapper, parameterManager)
Context = setContext()

keepers := TestKeepers{
MutateKeeper: keeperPrototype().Initialize(Mapper, parameterManager, []interface{}{}).(helpers.TransactionKeeper),
coinSupply = sdkTypes.NewCoins(sdkTypes.NewCoin(Denom, sdkTypes.NewInt(GenesisSupply)))
_ = BankKeeper.MintCoins(Context, TestMinterModuleName, coinSupply)

genesisAddress = sdkTypes.AccAddress(ed25519.GenPrivKey().PubKey().Address())
_ = BankKeeper.SendCoinsFromModuleToAccount(Context, TestMinterModuleName, genesisAddress, coinSupply)

TransactionKeeper = transactionKeeper{mapper.Prototype().Initialize(moduleStoreKey),
authenticateAuxiliary,
maintainAuxiliary,
conformAuxiliary,
}
)

return context, keepers, Mapper, parameterManager
func setContext() sdkTypes.Context {
memDB := tendermintDB.NewMemDB()
commitMultiStore := store.NewCommitMultiStore(memDB)
commitMultiStore.MountStoreWithDB(moduleStoreKey, storeTypes.StoreTypeIAVL, memDB)
commitMultiStore.MountStoreWithDB(authStoreKey, storeTypes.StoreTypeIAVL, memDB)
commitMultiStore.MountStoreWithDB(bankStoreKey, storeTypes.StoreTypeIAVL, memDB)
commitMultiStore.MountStoreWithDB(paramsStoreKey, storeTypes.StoreTypeIAVL, memDB)
commitMultiStore.MountStoreWithDB(paramsTransientStoreKeys, storeTypes.StoreTypeTransient, memDB)
_ = commitMultiStore.LoadLatestVersion()
return sdkTypes.NewContext(commitMultiStore, protoTendermintTypes.Header{ChainID: ChainID}, false, log.NewNopLogger())
}

func Test_transactionKeeper_Initialize(t *testing.T) {
_, _, Mapper, parameterManager := createTestInput(t)
type fields struct {
mapper helpers.Mapper
authenticateAuxiliary helpers.Auxiliary
maintainAuxiliary helpers.Auxiliary
conformAuxiliary helpers.Auxiliary
}
func TestTransactionKeeperTransact(t *testing.T) {
type args struct {
mapper helpers.Mapper
parameterManager helpers.ParameterManager
auxiliaries []interface{}
from sdkTypes.AccAddress
fromID ids.IdentityID
assetID ids.AssetID
mutableMetaProps lists.PropertyList
mutableProps lists.PropertyList
}

tests := []struct {
name string
fields fields
args args
want helpers.Keeper
name string
args args
setup func()
want *TransactionResponse
wantErr helpers.Error
}{
{"+ve", fields{Mapper, authenticateAuxiliary, maintainAuxiliary, conformAuxiliary}, args{Mapper, parameterManager, []interface{}{}}, transactionKeeper{Mapper, authenticateAuxiliary, maintainAuxiliary, conformAuxiliary}},
{
"MutateTransactionKeeeperSuccess",
args{
from: genesisAddress,
fromID: baseIDs.PrototypeIdentityID(),
assetID: testNewAssetID,
mutableMetaProps: baseLists.NewPropertyList(),
mutableProps: baseLists.NewPropertyList(),
},
func() {
conformAuxiliaryKeeper.On("Help", mock.Anything, mock.Anything).Return(new(helpers.AuxiliaryResponse), nil).Once()
maintainAuxiliaryKeeper.On("Help", mock.Anything, mock.Anything).Return(new(helpers.AuxiliaryResponse), nil).Once()
},
newTransactionResponse(),
nil,
},
{
"MutateValidAsset",
args{
from: genesisAddress,
fromID: baseIDs.PrototypeIdentityID(),
assetID: testNewAssetID,
mutableMetaProps: baseLists.NewPropertyList(),
mutableProps: baseLists.NewPropertyList(),
},
func() {
conformAuxiliaryKeeper.On("Help", mock.Anything, mock.Anything).Return(new(helpers.AuxiliaryResponse), nil).Once()
maintainAuxiliaryKeeper.On("Help", mock.Anything, mock.Anything).Return(new(helpers.AuxiliaryResponse), nil).Once()
},
newTransactionResponse(),
nil,
},
{
"EntityNotFoundError",
args{
from: genesisAddress,
fromID: baseIDs.PrototypeIdentityID(),
assetID: baseIDs.PrototypeAssetID(),
mutableMetaProps: baseLists.NewPropertyList(),
mutableProps: baseLists.NewPropertyList(),
},
func() {},
nil,
errorConstants.EntityNotFound,
},
{
"AuthenticateAuxiliaryFailure",
args{
from: authenticateAuxiliaryFailureAddress,
fromID: baseIDs.PrototypeIdentityID(),
assetID: baseIDs.PrototypeAssetID(),
mutableMetaProps: baseLists.NewPropertyList(),
mutableProps: baseLists.NewPropertyList(),
},
func() {},
nil,
errorConstants.MockError,
},
{
"MaintainAuxiliaryFailure",
args{
from: genesisAddress,
fromID: maintainFailureID,
assetID: testNewAssetID,
mutableMetaProps: baseLists.NewPropertyList(),
mutableProps: baseLists.NewPropertyList(),
},
func() {
maintainAuxiliaryKeeper.On("Help", mock.Anything, maintain.NewAuxiliaryRequest(maintainFailureClassificationID, maintainFailureID, baseQualified.NewMutables(baseLists.NewPropertyList()))).Return(new(helpers.AuxiliaryResponse), errorConstants.MockError)
},
nil,
errorConstants.MockError,
},
{
"ConformAuxiliaryFailure",
args{
from: genesisAddress,
fromID: baseIDs.PrototypeIdentityID(),
assetID: testNewAssetID,
mutableMetaProps: baseLists.NewPropertyList(),
mutableProps: baseLists.NewPropertyList(),
},
func() {
maintainAuxiliaryKeeper.On("Help", mock.Anything, mock.Anything).Return(new(helpers.AuxiliaryResponse), nil).Once()
conformAuxiliaryKeeper.On("Help", mock.Anything, conform.NewAuxiliaryRequest(testAsset.GetClassificationID(), testAsset.GetImmutables(), baseQualified.NewMutables(baseLists.NewPropertyList()))).Return(new(helpers.AuxiliaryResponse), errorConstants.MockError)
},
nil,
errorConstants.MockError,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
transactionKeeper := transactionKeeper{
mapper: tt.fields.mapper,
authenticateAuxiliary: tt.fields.authenticateAuxiliary,
maintainAuxiliary: tt.fields.maintainAuxiliary,
conformAuxiliary: tt.fields.conformAuxiliary,
tt.setup()

TransactionKeeper.mapper.NewCollection(sdkTypes.WrapSDKContext(Context)).Add(recordassets.NewRecord(testAsset))

got, err := TransactionKeeper.Transact(sdkTypes.WrapSDKContext(Context),
NewMessage(tt.args.from,
tt.args.fromID,
tt.args.assetID,
tt.args.mutableMetaProps.(lists.PropertyList),
tt.args.mutableProps.(lists.PropertyList)).(helpers.Message),
)

if (tt.wantErr != nil && !tt.wantErr.Is(err)) || (tt.wantErr == nil && err != nil) {
t.Errorf("unexpected error: %v", err)
}
if got := transactionKeeper.Initialize(tt.args.mapper, tt.args.parameterManager, tt.args.auxiliaries); !reflect.DeepEqual(fmt.Sprint(got), fmt.Sprint(tt.want)) {
t.Errorf("Initialize() = %v, want %v", got, tt.want)

if !reflect.DeepEqual(got, tt.want) {
t.Error("unexpected response")
}
})
}
Expand Down
Loading

0 comments on commit a4142b7

Please sign in to comment.