Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix delete model version #599

Merged
merged 9 commits into from
Sep 2, 2024
5 changes: 5 additions & 0 deletions integration_tests/grpc_rest/model/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func TestDeleteModelVersionGRPC(t *testing.T) {
model.DeleteModelVersion(&suite)
}

func TestDeleteModelVersionBeforeDeletingModelGRPC(t *testing.T) {
suite := utils.SetupTest(t, testconstants.ChainID, false)
model.DeleteModelVersionBeforeDeletingModel(&suite)
}

func TestDeleteModelVersionDifferentVidGRPC(t *testing.T) {
suite := utils.SetupTest(t, testconstants.ChainID, false)
model.DeleteModelVersionDifferentVid(&suite)
Expand Down
72 changes: 72 additions & 0 deletions integration_tests/grpc_rest/model/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,78 @@ func DeleteModelVersion(suite *utils.TestSuite) {
require.Nil(suite.T, model)
}

func DeleteModelVersionBeforeDeletingModel(suite *utils.TestSuite) {
// Alice and Bob are predefined Trustees
aliceName := testconstants.AliceAccount
aliceKeyInfo, err := suite.Kr.Key(aliceName)
require.NoError(suite.T, err)
address, err := aliceKeyInfo.GetAddress()
require.NoError(suite.T, err)
aliceAccount, err := testDclauth.GetAccount(suite, address)
require.NoError(suite.T, err)

bobName := testconstants.BobAccount
bobKeyInfo, err := suite.Kr.Key(bobName)
require.NoError(suite.T, err)
address, err = bobKeyInfo.GetAddress()
require.NoError(suite.T, err)
bobAccount, err := testDclauth.GetAccount(suite, address)
require.NoError(suite.T, err)

// Register new Vendor account
vid := int32(tmrand.Uint16())
vendorName := utils.RandString()
pid := int32(tmrand.Uint16())
vendorAccount := testDclauth.CreateVendorAccount(
suite,
vendorName,
dclauthtypes.AccountRoles{dclauthtypes.Vendor},
vid,
[]*commontypes.Uint16Range{{Min: pid, Max: pid}},
aliceName,
aliceAccount,
bobName,
bobAccount,
testconstants.Info,
)
require.NotNil(suite.T, vendorAccount)

// New vendor adds a model
createModelMsg := NewMsgCreateModel(vid, pid, vendorAccount.Address)
_, err = suite.BuildAndBroadcastTx([]sdk.Msg{createModelMsg}, vendorName, vendorAccount)
require.NoError(suite.T, err)

createModelVersion1Msg := NewMsgCreateModelVersion(vid, pid, 1, "1", vendorAccount.Address)
_, err = suite.BuildAndBroadcastTx([]sdk.Msg{createModelVersion1Msg}, vendorName, vendorAccount)
require.NoError(suite.T, err)

createModelVersion2Msg := NewMsgCreateModelVersion(vid, pid, 2, "2", vendorAccount.Address)
_, err = suite.BuildAndBroadcastTx([]sdk.Msg{createModelVersion2Msg}, vendorName, vendorAccount)
require.NoError(suite.T, err)

deleteModelVersionMsg := NewMsgDeleteModelVersion(vid, pid, createModelVersion1Msg.SoftwareVersion, vendorAccount.Address)
_, err = suite.BuildAndBroadcastTx([]sdk.Msg{deleteModelVersionMsg}, vendorName, vendorAccount)
require.NoError(suite.T, err)

deleteModelMsg := NewMsgDeleteModel(vid, pid, vendorAccount.Address)
_, err = suite.BuildAndBroadcastTx([]sdk.Msg{deleteModelMsg}, vendorName, vendorAccount)
require.NoError(suite.T, err)

// check if modelVersions are deleted
modelVersion, err := GetModelVersion(suite, deleteModelVersionMsg.Vid, deleteModelVersionMsg.Pid, createModelVersion1Msg.SoftwareVersion)
require.Error(suite.T, err)
require.Nil(suite.T, modelVersion)

modelVersion, err = GetModelVersion(suite, deleteModelVersionMsg.Vid, deleteModelVersionMsg.Pid, createModelVersion2Msg.SoftwareVersion)
require.Error(suite.T, err)
require.Nil(suite.T, modelVersion)

// check if model is deleted
model, err := GetModel(suite, deleteModelVersionMsg.Vid, deleteModelVersionMsg.Pid)
require.Error(suite.T, err)
require.Nil(suite.T, model)
}

func DeleteModelVersionDifferentVid(suite *utils.TestSuite) {
// Alice and Bob are predefined Trustees
aliceName := testconstants.AliceAccount
Expand Down
5 changes: 5 additions & 0 deletions integration_tests/grpc_rest/model/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ func TestDeleteModelVersionREST(t *testing.T) {
model.DeleteModelVersion(&suite)
}

func TestDeleteModelVersionBeforeDeletingModelRust(t *testing.T) {
suite := utils.SetupTest(t, testconstants.ChainID, true)
model.DeleteModelVersionBeforeDeletingModel(&suite)
}

func TestDeleteModelVersionDifferentVidREST(t *testing.T) {
suite := utils.SetupTest(t, testconstants.ChainID, true)
model.DeleteModelVersionDifferentVid(&suite)
Expand Down
94 changes: 94 additions & 0 deletions x/model/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,41 @@ func TestHandler_DeleteModel(t *testing.T) {
require.Equal(t, codes.NotFound, status.Code(err))
}

func TestHandler_DeleteModelAfterDeletingModelVersion(t *testing.T) {
setup := Setup(t)

// add new model
msgCreateModel := NewMsgCreateModel(setup.Vendor)
_, err := setup.Handler(setup.Ctx, msgCreateModel)
require.NoError(t, err)

// add two new model versions
msgCreateModelVersion1 := NewMsgCreateModelVersion(setup.Vendor, testconstants.SoftwareVersion)
_, err = setup.Handler(setup.Ctx, msgCreateModelVersion1)
require.NoError(t, err)

msgCreateModelVersion2 := NewMsgCreateModelVersion(setup.Vendor, testconstants.SoftwareVersion+1)
_, err = setup.Handler(setup.Ctx, msgCreateModelVersion2)
require.NoError(t, err)

complianceKeeper := setup.ComplianceKeeper
complianceKeeper.On("GetComplianceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(false)

msgDeleteModelVersion1 := NewMsgDeleteModelVersion(setup.Vendor)
_, err = setup.Handler(setup.Ctx, msgDeleteModelVersion1)
require.NoError(t, err)

msgDeleteModel := NewMsgDeleteModel(setup.Vendor)
// delete existing model
_, err = setup.Handler(setup.Ctx, msgDeleteModel)
require.NoError(t, err)

// query deleted model
_, err = queryModel(setup, msgDeleteModel.Vid, msgDeleteModel.Pid)
require.Error(t, err)
require.Equal(t, codes.NotFound, status.Code(err))
}

func TestHandler_DeleteModelWithAssociatedModelVersionsNotCertified(t *testing.T) {
setup := Setup(t)

Expand Down Expand Up @@ -1433,6 +1468,65 @@ func TestHandler_DeleteModelVersion(t *testing.T) {
)
require.Error(t, err)
require.Equal(t, codes.NotFound, status.Code(err))

// query model versions
ashcherbakov marked this conversation as resolved.
Show resolved Hide resolved
_, err = queryAllModelVersions(
setup,
msgDeleteModelVersion.Vid,
msgDeleteModelVersion.Pid,
)
require.Error(t, err)
require.Equal(t, codes.NotFound, status.Code(err))
}

func TestHandler_DeleteOneOfTwoModelVersions(t *testing.T) {
setup := Setup(t)

// add new model
msgCreateModel := NewMsgCreateModel(setup.Vendor)
_, err := setup.Handler(setup.Ctx, msgCreateModel)
require.NoError(t, err)

// add two new model versions
msgCreateModelVersion1 := NewMsgCreateModelVersion(setup.Vendor, testconstants.SoftwareVersion)
_, err = setup.Handler(setup.Ctx, msgCreateModelVersion1)
require.NoError(t, err)

msgCreateModelVersion2 := NewMsgCreateModelVersion(setup.Vendor, testconstants.SoftwareVersion+1)
_, err = setup.Handler(setup.Ctx, msgCreateModelVersion2)
require.NoError(t, err)

// mock model versions not to be certified
complianceKeeper := setup.ComplianceKeeper
complianceKeeper.On("GetComplianceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(false)

msgDeleteModelVersion := NewMsgDeleteModelVersion(setup.Vendor)
_, err = setup.Handler(setup.Ctx, msgDeleteModelVersion)
require.NoError(t, err)

// query deleted model version
_, err = queryModelVersion(
setup,
msgCreateModelVersion1.Vid,
msgCreateModelVersion1.Pid,
msgCreateModelVersion1.SoftwareVersion,
)
require.Error(t, err)
require.Equal(t, codes.NotFound, status.Code(err))

// query not deleted model version
modelVersion, err := queryModelVersion(
setup,
msgCreateModelVersion2.Vid,
msgCreateModelVersion2.Pid,
msgCreateModelVersion2.SoftwareVersion,
)
require.NoError(t, err)
require.NotNil(t, modelVersion)
ashcherbakov marked this conversation as resolved.
Show resolved Hide resolved

modelVersions, err := queryAllModelVersions(setup, msgCreateModel.Vid, msgCreateModel.Pid)
require.NoError(t, err)
require.Equal(t, []uint32{msgCreateModelVersion2.SoftwareVersion}, modelVersions.SoftwareVersions)
}

func TestHandler_DeleteModelVersionDifferentAccSameVid(t *testing.T) {
Expand Down
38 changes: 38 additions & 0 deletions x/model/keeper/model_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,41 @@ func (k Keeper) AddModelVersion(ctx sdk.Context, vid int32, pid int32, softwareV

k.SetModelVersions(ctx, modelVersions)
}

// RemoveSoftwareVersion deletes a softwareVersion from existing ModelVersions.
func (k Keeper) RemoveSoftwareVersion(ctx sdk.Context, vid int32, pid int32, softwareVersion uint32) {
modelVersions, found := k.GetModelVersions(ctx, vid, pid)

if !found {
return
}

// Find the index of the softwareVersion to delete
index := -1
for i, value := range modelVersions.SoftwareVersions {
if value == softwareVersion {
index = i

break
}
}

// If the softwareVersion is not found, no need to proceed
if index == -1 {
return
}

modelVersions.SoftwareVersions = append(
modelVersions.SoftwareVersions[:index],
modelVersions.SoftwareVersions[index+1:]...,
)

// Delete modelVersions if there are no softwareVersions left
if len(modelVersions.SoftwareVersions) == 0 {
k.RemoveModelVersions(ctx, vid, pid)

return
}

k.SetModelVersions(ctx, modelVersions)
}
1 change: 1 addition & 0 deletions x/model/keeper/msg_server_model_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func (k msgServer) DeleteModelVersion(goCtx context.Context, msg *types.MsgDelet

// store updated model version
k.RemoveModelVersion(ctx, msg.Vid, msg.Pid, msg.SoftwareVersion)
k.RemoveSoftwareVersion(ctx, msg.Vid, msg.Pid, msg.SoftwareVersion)

return &types.MsgDeleteModelVersionResponse{}, nil
}
Expand Down
Loading