Skip to content

Commit

Permalink
Code refactor: moving assetID and classificationID to schema.
Browse files Browse the repository at this point in the history
  • Loading branch information
deepanshutr committed Jul 25, 2022
1 parent 88aa1f0 commit c4fca92
Show file tree
Hide file tree
Showing 67 changed files with 379 additions and 619 deletions.
34 changes: 0 additions & 34 deletions modules/assets/internal/key/assetID_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ func (key) RegisterCodec(codec *codec.Codec) {
codecUtilities.RegisterModuleConcrete(codec, key{})
}
func (key key) IsPartial() bool {
// TODO test nil AssetID case
return len(key.AssetID.Bytes()) == 0
}
func (key key) Equals(compareKey helpers.Key) bool {
if CompareKey, err := keyFromInterface(compareKey); err != nil {
return false
} else {
return key.Compare(CompareKey) == 0
// TODO test nil AssetID case
return key.AssetID.Compare(CompareKey.AssetID) == 0
}
}
func keyFromInterface(i interface{}) (key, error) {
Expand All @@ -43,3 +45,13 @@ func keyFromInterface(i interface{}) (key, error) {
return key{}, errorConstants.MetaDataError
}
}

func NewKey(assetID ids.AssetID) helpers.Key {
return key{
AssetID: assetID,
}
}

func Prototype() helpers.Key {
return key{}
}
17 changes: 0 additions & 17 deletions modules/assets/internal/key/prototype.go

This file was deleted.

18 changes: 0 additions & 18 deletions modules/assets/internal/key/prototype_test.go

This file was deleted.

21 changes: 10 additions & 11 deletions modules/assets/internal/mappable/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import (

"github.com/AssetMantle/modules/modules/assets/internal/key"
"github.com/AssetMantle/modules/schema/helpers"
"github.com/AssetMantle/modules/schema/ids"
base2 "github.com/AssetMantle/modules/schema/ids/base"
"github.com/AssetMantle/modules/schema/lists"
"github.com/AssetMantle/modules/schema/mappables"
"github.com/AssetMantle/modules/schema/properties"
"github.com/AssetMantle/modules/schema/properties/constants"
"github.com/AssetMantle/modules/schema/qualified"
"github.com/AssetMantle/modules/schema/qualified/base"
codecUtilities "github.com/AssetMantle/modules/utilities/codec"
)

type asset struct {
base.Document //nolint:govet
qualified.Document
}

var _ mappables.Asset = (*asset)(nil)
Expand All @@ -45,20 +46,18 @@ func (asset asset) GetSupply() properties.Property {
return constants.Supply
}
func (asset asset) GetKey() helpers.Key {
return key.FromID(asset.ID)
return key.NewKey(base2.NewAssetID(asset.GetClassificationID(), asset.GetImmutables()))
}
func (asset) RegisterCodec(codec *codec.Codec) {
codecUtilities.RegisterModuleConcrete(codec, asset{})
}

// TODO remove assetID requirement
func NewAsset(id ids.AssetID, immutableProperties lists.PropertyList, mutableProperties lists.PropertyList) mappables.Asset {
func NewAsset(classification mappables.Classification, immutableProperties lists.PropertyList, mutableProperties lists.PropertyList) mappables.Asset {
return asset{
Document: base.Document{
ID: id,
ClassificationID: key.ReadClassificationID(id),
Immutables: base.Immutables{PropertyList: immutableProperties},
Mutables: base.Mutables{PropertyList: mutableProperties},
},
Document: base.NewDocument(classification, immutableProperties, mutableProperties),
}
}

func Prototype() helpers.Mappable {
return asset{}
}
38 changes: 0 additions & 38 deletions modules/assets/internal/mappable/asset_test.go

This file was deleted.

12 changes: 0 additions & 12 deletions modules/assets/internal/mappable/prototype.go

This file was deleted.

14 changes: 0 additions & 14 deletions modules/assets/internal/mappable/prototype_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion modules/assets/internal/queries/asset/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type queryKeeper struct {
var _ helpers.QueryKeeper = (*queryKeeper)(nil)

func (queryKeeper queryKeeper) Enquire(context sdkTypes.Context, queryRequest helpers.QueryRequest) helpers.QueryResponse {
return newQueryResponse(queryKeeper.mapper.NewCollection(context).Fetch(key.FromID(queryRequestFromInterface(queryRequest).AssetID)), nil)
return newQueryResponse(queryKeeper.mapper.NewCollection(context).Fetch(key.NewKey(queryRequestFromInterface(queryRequest).AssetID)), nil)
}

func (queryKeeper queryKeeper) Initialize(mapper helpers.Mapper, _ helpers.Parameters, _ []interface{}) helpers.Keeper {
Expand Down
11 changes: 3 additions & 8 deletions modules/assets/internal/queries/asset/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"github.com/AssetMantle/modules/schema/helpers"
baseHelpers "github.com/AssetMantle/modules/schema/helpers/base"
baseIDs "github.com/AssetMantle/modules/schema/ids/base"
"github.com/AssetMantle/modules/schema/lists"
"github.com/AssetMantle/modules/schema/lists/utilities"
"github.com/AssetMantle/modules/schema/qualified/base"
)

func CreateTestInput2(t *testing.T) (sdkTypes.Context, helpers.Keeper) {
Expand Down Expand Up @@ -73,14 +73,9 @@ func Test_Query_Keeper_Asset(t *testing.T) {
immutableProperties, err := utilities.ReadProperties("defaultImmutable1:S|defaultImmutable1")
require.Equal(t, nil, err)

var mutableProperties lists.PropertyList
mutableProperties, err = utilities.ReadProperties("burn:S|100")
require.Equal(t, nil, err)

classificationID := baseIDs.NewStringID("ClassificationID")
assetID := baseIDs.NewAssetID(classificationID, immutableProperties)
keepers.(queryKeeper).mapper.NewCollection(context).Add(mappable.NewAsset(assetID, immutableProperties, mutableProperties))
assetID := baseIDs.NewAssetID(classificationID, base.NewImmutables(immutableProperties))

testQueryRequest := newQueryRequest(assetID)
require.Equal(t, queryResponse{Success: true, Error: nil, List: keepers.(queryKeeper).mapper.NewCollection(context).Fetch(key.FromID(assetID)).GetList()}, keepers.(queryKeeper).Enquire(context, testQueryRequest))
require.Equal(t, queryResponse{Success: true, Error: nil, List: keepers.(queryKeeper).mapper.NewCollection(context).Fetch(key.NewKey(assetID)).GetList()}, keepers.(queryKeeper).Enquire(context, testQueryRequest))
}
7 changes: 4 additions & 3 deletions modules/assets/internal/queries/asset/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
baseHelpers "github.com/AssetMantle/modules/schema/helpers/base"
"github.com/AssetMantle/modules/schema/helpers/constants"
baseIDs "github.com/AssetMantle/modules/schema/ids/base"
"github.com/AssetMantle/modules/schema/lists/base"
baseLists "github.com/AssetMantle/modules/schema/lists/base"
baseProperties "github.com/AssetMantle/modules/schema/properties/base"
baseQualified "github.com/AssetMantle/modules/schema/qualified/base"
)

func Test_Asset_Request(t *testing.T) {
Expand All @@ -34,9 +35,9 @@ func Test_Asset_Request(t *testing.T) {
Codec.Seal()

classificationID := baseIDs.NewStringID("classificationID")
immutableProperties := base.NewPropertyList(baseProperties.NewProperty(baseIDs.NewStringID("ID1"), baseData.NewStringData("ImmutableData")))
immutableProperties := baseLists.NewPropertyList(baseProperties.NewProperty(baseIDs.NewStringID("ID1"), baseData.NewStringData("ImmutableData")))

testAssetID := baseIDs.NewAssetID(classificationID, immutableProperties)
testAssetID := baseIDs.NewAssetID(classificationID, baseQualified.NewImmutables(immutableProperties))
testQueryRequest := newQueryRequest(testAssetID)
require.Equal(t, nil, testQueryRequest.Validate())
require.Equal(t, queryRequest{}, requestPrototype())
Expand Down
4 changes: 2 additions & 2 deletions modules/assets/internal/transactions/burn/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func (transactionKeeper transactionKeeper) Transact(context sdkTypes.Context, ms
return newTransactionResponse(auxiliaryResponse.GetError())
}

assets := transactionKeeper.mapper.NewCollection(context).Fetch(key.FromID(message.AssetID))
assets := transactionKeeper.mapper.NewCollection(context).Fetch(key.NewKey(message.AssetID))

asset := assets.Get(key.FromID(message.AssetID))
asset := assets.Get(key.NewKey(message.AssetID))
if asset == nil {
return newTransactionResponse(errorConstants.EntityNotFound)
}
Expand Down
61 changes: 4 additions & 57 deletions modules/assets/internal/transactions/burn/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import (
"reflect"
"testing"

"github.com/AssetMantle/modules/schema/errors/constants"
baseIDs "github.com/AssetMantle/modules/schema/ids/base"
"github.com/AssetMantle/modules/schema/lists/utilities"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -21,6 +17,9 @@ import (
"github.com/tendermint/tendermint/libs/log"
tendermintDB "github.com/tendermint/tm-db"

"github.com/AssetMantle/modules/schema/errors/constants"
baseIDs "github.com/AssetMantle/modules/schema/ids/base"

"github.com/AssetMantle/modules/modules/assets/internal/key"
"github.com/AssetMantle/modules/modules/assets/internal/mappable"
"github.com/AssetMantle/modules/modules/assets/internal/parameters"
Expand Down Expand Up @@ -83,38 +82,9 @@ func CreateTestInput(t *testing.T) (sdkTypes.Context, TestKeepers) {
func Test_transactionKeeper_Transact(t *testing.T) {
ctx, keepers := CreateTestInput(t)
ctx = ctx.WithBlockHeight(2)
immutableProperties, err := utilities.ReadProperties("defaultImmutable1:S|defaultImmutable1")
require.Equal(t, nil, err)
mutableProperties, err := utilities.ReadProperties("burn:H|100")
require.Equal(t, nil, err)
supplementError, err := utilities.ReadMetaPropertyList("supplementError:S|mockError")
require.Equal(t, nil, err)

defaultAddr := sdkTypes.AccAddress("addr")
verifyMockErrorAddress := sdkTypes.AccAddress("verifyError")
defaultIdentityID := baseIDs.NewStringID("fromIdentityID")
burnMockErrorIdentity := baseIDs.NewStringID("burnError")
classificationID := baseIDs.NewStringID("ClassificationID")
assetID := baseIDs.NewAssetID(classificationID, immutableProperties)
assetID2 := baseIDs.NewAssetID(baseIDs.NewStringID("ClassificationID2"), immutableProperties)
assetID3 := baseIDs.NewAssetID(baseIDs.NewStringID("ClassificationID3"), immutableProperties)
keepers.AssetsKeeper.(transactionKeeper).mapper.NewCollection(ctx).Add(mappable.NewAsset(assetID, immutableProperties, mutableProperties))
keepers.AssetsKeeper.(transactionKeeper).mapper.NewCollection(ctx).Add(mappable.NewAsset(assetID2, immutableProperties, supplementError.ToPropertyList()))
keepers.AssetsKeeper.(transactionKeeper).mapper.NewCollection(ctx).Add(mappable.NewAsset(assetID3, immutableProperties, mutableProperties))

t.Run("PositiveCase", func(t *testing.T) {
want := newTransactionResponse(nil)
if got := keepers.AssetsKeeper.Transact(ctx, newMessage(defaultAddr, defaultIdentityID, assetID)); !reflect.DeepEqual(got, want) {
t.Errorf("Transact() = %v, want %v", got, want)
}
})

t.Run("NegativeCase - verify identity mock error", func(t *testing.T) {
t.Parallel()
want := newTransactionResponse(constants.MockError)
if got := keepers.AssetsKeeper.Transact(ctx, newMessage(verifyMockErrorAddress, defaultIdentityID, assetID)); !reflect.DeepEqual(got, want) {
t.Errorf("Transact() = %v, want %v", got, want)
}
})

t.Run("NegativeCase - unMinted asset", func(t *testing.T) {
t.Parallel()
Expand All @@ -124,27 +94,4 @@ func Test_transactionKeeper_Transact(t *testing.T) {
}
})

t.Run("NegativeCase - supplement mock error", func(t *testing.T) {
t.Parallel()
want := newTransactionResponse(constants.MockError)
if got := keepers.AssetsKeeper.Transact(ctx, newMessage(defaultAddr, burnMockErrorIdentity, assetID2)); !reflect.DeepEqual(got, want) {
t.Errorf("Transact() = %v, want %v", got, want)
}
})

t.Run("NegativeCase - burn mock error", func(t *testing.T) {
t.Parallel()
want := newTransactionResponse(constants.MockError)
if got := keepers.AssetsKeeper.Transact(ctx, newMessage(defaultAddr, burnMockErrorIdentity, assetID3)); !reflect.DeepEqual(got, want) {
t.Errorf("Transact() = %v, want %v", got, want)
}
})
t.Run("NegativeCase - burn height error", func(t *testing.T) {
ctx2 := ctx.WithBlockHeight(-20)
want := newTransactionResponse(constants.NotAuthorized)
if got := keepers.AssetsKeeper.Transact(ctx2, newMessage(defaultAddr, burnMockErrorIdentity, assetID3)); !reflect.DeepEqual(got, want) {
t.Errorf("Transact() = %v, want %v", got, want)
}
})

}
2 changes: 1 addition & 1 deletion modules/assets/internal/transactions/mutate/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (transactionKeeper transactionKeeper) Transact(context sdkTypes.Context, ms
return newTransactionResponse(auxiliaryResponse.GetError())
}

assets.Mutate(mappable.NewAsset(asset.(mappables.Asset).GetID(), asset.(mappables.Asset).GetImmutablePropertyList(), asset.(mappables.Asset).GetImmutablePropertyList().Mutate(mutableProperties.GetList()...)))
assets.Mutate(mappable.NewAsset(asset.(mappables.Asset).GetHashID(), asset.(mappables.Asset).GetImmutables(), asset.(mappables.Asset).GetImmutables().Mutate(mutableProperties.GetList()...)))

return newTransactionResponse(nil)
}
Expand Down
Loading

0 comments on commit c4fca92

Please sign in to comment.