Skip to content

Commit

Permalink
Merge branch 'master' into api-routing
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarstonConnell authored Jun 18, 2023
2 parents 705dda1 + 80212f1 commit b81ac08
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 48 deletions.
91 changes: 47 additions & 44 deletions x/storage/keeper/msg_server_cancel_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,83 @@ package keeper
import (
"context"
"encoding/json"
"strconv"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/jackalLabs/canine-chain/x/storage/types"
)

type contract struct {
Signee string
Cid string
Fid string
Filesize string
}

func CanContract(ctx sdk.Context, root string, creator string, k Keeper) error {
var c contract

func (k Keeper) CanContract(ctx sdk.Context, root string, creator string) error {
d, dealFound := k.GetActiveDeals(ctx, root)

s, found := k.GetStrays(ctx, root)
if !found {
if !dealFound {
return sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, "no deal found")
s, strayFound := k.GetStrays(ctx, root)

var fileSize int64
var fid string
var signee string
var err error

// nolint
if dealFound {
signee = d.Signee
fid = d.Fid
fileSize, err = strconv.ParseInt(d.Filesize, 10, 64)
if err != nil {
return err
}
} else if strayFound {
signee = s.Signee
fid = s.Fid
fileSize, err = strconv.ParseInt(s.Filesize, 10, 64)
if err != nil {
return err
}
c.Cid = d.Cid
c.Signee = d.Signee
c.Fid = d.Fid
c.Filesize = d.Filesize
} else {
c.Cid = s.Cid
c.Signee = s.Signee
c.Fid = s.Fid
c.Filesize = s.Filesize
return types.ErrNoCid
}

if creator != c.Signee {
return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "cannot cancel a contract that isn't yours. %s is not %s", creator, c.Signee)
if creator != signee {
return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "cannot cancel a contract that isn't yours. %s is not %s", creator, signee)
}

k.RemoveStrays(ctx, c.Cid)
k.RemoveActiveDeals(ctx, c.Cid)
k.RemoveStrays(ctx, root)
k.RemoveActiveDeals(ctx, root)

newFidCid := types.FidCid{
Fid: fid,
Cids: "",
}

ftc, found := k.GetFidCid(ctx, c.Fid)
if found {
ftc, found := k.GetFidCid(ctx, fid) // get existing FIDCID Mapping
cids := make([]string, 0) // create new home for CID list
if found { // if found we remove the existing cid from the list
var ncids []string
err := json.Unmarshal([]byte(ftc.Cids), &ncids)
err := json.Unmarshal([]byte(ftc.Cids), &ncids) // getting all cids from the existing fid_cid
if err != nil {
return err
}

cids := make([]string, 0)
for _, v := range ncids {
for _, v := range ncids { // all all cids to the list again if they aren't the root
if v != root {
cids = append(cids, v)
}
}
b, err := json.Marshal(cids)
if err != nil {
return err
}
ftc.Cids = string(b)
}

k.SetFidCid(ctx, ftc)
b, err := json.Marshal(cids) // put em all back
if err != nil {
return err
}
newFidCid.Cids = string(b)

k.SetFidCid(ctx, newFidCid)

info, found := k.GetStoragePaymentInfo(ctx, creator)
if !found {
return sdkerrors.Wrapf(sdkerrors.ErrNotFound, "cannot find storage payment")
}
size, ok := sdk.NewIntFromString(c.Filesize)
if !ok {
return sdkerrors.Wrap(sdkerrors.ErrInvalidType, "cannot parse file size")
}
info.SpaceUsed -= size.Int64()
info.SpaceUsed -= fileSize
k.SetStoragePaymentInfo(ctx, info)

return nil
Expand All @@ -87,7 +90,7 @@ func (k msgServer) CancelContract(goCtx context.Context, msg *types.MsgCancelCon

root := msg.Cid

err := CanContract(ctx, root, msg.Creator, k.Keeper)
err := k.Keeper.CanContract(ctx, root, msg.Creator)

return &types.MsgCancelContractResponse{}, err
}
11 changes: 10 additions & 1 deletion x/storage/keeper/msg_server_contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (suite *KeeperTestSuite) TestCancelContract() {
}
},
expErr: true,
expErrMsg: "no deal found: key not found",
expErrMsg: "cid does not exist",
},

{
Expand Down Expand Up @@ -517,11 +517,20 @@ func (suite *KeeperTestSuite) TestCancelContract() {
suite.Require().NotNil(tc.preRun)
c := tc.preRun()
_, err := msgSrvr.CancelContract(goCtx, c)

if tc.expErr {
suite.Require().Error(err)
suite.Require().Contains(err.Error(), tc.expErrMsg)
} else {
suite.Require().NoError(err)
fidCid, found := suite.storageKeeper.GetFidCid(suite.ctx, "jklf1j3p63s42w7ywaczlju626st55mzu5z39w2rx9x")
suite.Require().True(found)
var cids []string
err := json.Unmarshal([]byte(fidCid.Cids), &cids) // getting all cids from the existing fid_cid
if err != nil {
suite.Require().NoError(err)
}
suite.Require().Equal(0, len(cids))
}

if tc.postRun != nil {
Expand Down
6 changes: 3 additions & 3 deletions x/storage/keeper/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (k Keeper) loopDeals(ctx sdk.Context, allDeals []types.ActiveDeals, network
info, found := k.GetStoragePaymentInfo(ctx, deal.Signee)
if !found {
ctx.Logger().Debug(fmt.Sprintf("Removing %s due to no payment info", deal.Cid))
cerr := CanContract(ctx, deal.Cid, deal.Signee, k)
cerr := k.CanContract(ctx, deal.Cid, deal.Signee)
if cerr != nil {
ctx.Logger().Error(cerr.Error())
}
Expand All @@ -180,7 +180,7 @@ func (k Keeper) loopDeals(ctx sdk.Context, allDeals []types.ActiveDeals, network
grace := info.End.Add(time.Hour * 24 * 30)
if grace.Before(ctx.BlockTime()) {
ctx.Logger().Debug(fmt.Sprintf("Removing %s after grace period", deal.Cid))
cerr := CanContract(ctx, deal.Cid, deal.Signee, k)
cerr := k.CanContract(ctx, deal.Cid, deal.Signee)
if cerr != nil {
ctx.Logger().Error(cerr.Error())
}
Expand All @@ -189,7 +189,7 @@ func (k Keeper) loopDeals(ctx sdk.Context, allDeals []types.ActiveDeals, network

if info.SpaceUsed > info.SpaceAvailable { // remove file if the user doesn't have enough space
ctx.Logger().Debug(fmt.Sprintf("Removing %s for space used", deal.Cid))
err := CanContract(ctx, deal.Cid, deal.Signee, k)
err := k.CanContract(ctx, deal.Cid, deal.Signee)
if err != nil {
ctx.Logger().Error(err.Error())
}
Expand Down
1 change: 1 addition & 0 deletions x/storage/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ var (
ErrProviderNotFound = sdkerrors.Register(ModuleName, 1111, "Provider not found. Please init your provider.")
ErrNotValidTotalSpace = sdkerrors.Register(ModuleName, 1112, "Not a valid total space. Please enter total number of bytes to provide.")
ErrCannotVerifyProof = sdkerrors.Register(ModuleName, 1113, "Cannot verify Proof")
ErrNoCid = sdkerrors.Register(ModuleName, 1114, "cid does not exist")
)

0 comments on commit b81ac08

Please sign in to comment.