Skip to content

Commit

Permalink
Better Errors (#402)
Browse files Browse the repository at this point in the history
* fixing some error messages and such

* lint 🤯

* fixing casing
  • Loading branch information
TheMarstonConnell authored Jan 9, 2024
1 parent 2789101 commit d78caa6
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 18 deletions.
1 change: 1 addition & 0 deletions x/filetree/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func GetTxCmd() *cobra.Command {
}

cmd.AddCommand(CmdPostFile())
cmd.AddCommand(CmdPostFilePublic())
cmd.AddCommand(CmdAddViewers())
cmd.AddCommand(CmdPostkey())
cmd.AddCommand(CmdDeleteFile())
Expand Down
70 changes: 70 additions & 0 deletions x/filetree/client/cli/tx_post_file_public.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cli

import (
"crypto/sha256"
"fmt"
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
uuid "github.com/google/uuid"
filetypes "github.com/jackalLabs/canine-chain/v3/x/filetree/types"
"github.com/spf13/cobra"
)

var _ = strconv.Itoa(0)

func CmdPostFilePublic() *cobra.Command {
cmd := &cobra.Command{
Use: "post-file-no-key [path] [account] [contents]",
Short: "post a new file to an account's file explorer",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argHashpath := args[0]
argAccount := args[1]
argContents := args[2]

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

parentHash, childHash := merkleHelper(argHashpath)

trackingNumber := uuid.NewString()

viewers := make(map[string]string)
editors := make(map[string]string)

// Marshall everybody
jsonViewers, jsonEditors, err := JSONMarshalViewersAndEditors(viewers, editors)
if err != nil {
return err
}
H := sha256.New()
H.Write([]byte(argAccount))
hash := H.Sum(nil)
accountHash := fmt.Sprintf("%x", hash)

msg := filetypes.NewMsgPostFile(
clientCtx.GetFromAddress().String(),
accountHash,
parentHash,
childHash,
argContents,
string(jsonViewers),
string(jsonEditors),
trackingNumber,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
12 changes: 11 additions & 1 deletion x/storage/keeper/msg_server_cancel_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ func (k Keeper) CanContract(ctx sdk.Context, root string, creator string) error
fid = d.Fid
fileSize, err = strconv.ParseInt(d.Filesize, 10, 64)
if err != nil {
return err
return sdkerrors.Wrapf(err, "cannot parse file size from deal")
}
endBlock, err := strconv.ParseInt(d.Endblock, 10, 64)
if err != nil {
return sdkerrors.Wrapf(err, "cannot parse end block from deal")
}
if endBlock > ctx.BlockHeight() {
return sdkerrors.Wrapf(types.ErrCancelContract, "this is a persistent file that can't be cancelled until it expires")
}
} else if strayFound {
signee = s.Signee
Expand All @@ -35,6 +42,9 @@ func (k Keeper) CanContract(ctx sdk.Context, root string, creator string) error
if err != nil {
return err
}
if s.End > ctx.BlockHeight() {
return sdkerrors.Wrapf(types.ErrCancelContract, "this is a persistent file that can't be cancelled until it expires")
}
} else {
return types.ErrNoCid
}
Expand Down
49 changes: 48 additions & 1 deletion x/storage/keeper/msg_server_contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ func (suite *KeeperTestSuite) TestCancelContract() {
Signee: user,
Creator: user,
Filesize: "10",
Endblock: "0",
}
sKeeper.SetActiveDeals(suite.ctx, d)

Expand All @@ -412,6 +413,7 @@ func (suite *KeeperTestSuite) TestCancelContract() {
Signee: user,
Creator: user,
Filesize: "10",
Endblock: "0",
}
sKeeper.SetActiveDeals(suite.ctx, k)
}
Expand Down Expand Up @@ -447,6 +449,7 @@ func (suite *KeeperTestSuite) TestCancelContract() {
Signee: user,
Fid: "jklf1j3p63s42w7ywaczlju626st55mzu5z39w2rx9x",
Filesize: "10",
Endblock: "0",
}
sKeeper.SetActiveDeals(suite.ctx, d)

Expand All @@ -468,11 +471,54 @@ func (suite *KeeperTestSuite) TestCancelContract() {
},
expErr: false,
},
{
name: "failed_cancel_200",
preRun: func() *types.MsgCancelContract {
dcid := tst
h := sha256.New()
_, err := io.WriteString(h, dcid)
suite.Require().NoError(err)
hashName := h.Sum(nil)

dcid, err = keeper.MakeCid(hashName)
suite.Require().NoError(err)

cids := []string{dcid}

d := types.ActiveDeals{
Cid: dcid,
Creator: user,
Signee: user,
Fid: "jklf1j3p63s42w7ywaczlji626st55mzu5z39w2rx9x",
Filesize: "10",
Endblock: "2000",
}
sKeeper.SetActiveDeals(suite.ctx, d)

b, err := json.Marshal(cids)
suite.Require().NoError(err)

ftc := types.FidCid{
Fid: "jklf1j3p63s42w7ywaczlji626st55mzu5z39w2rx9x",
Cids: string(b),
}
sKeeper.SetFidCid(suite.ctx, ftc)

suite.Require().NoError(err)

return &types.MsgCancelContract{
Creator: user,
Cid: d.Cid,
}
},
expErr: true,
expErrMsg: types.ErrCancelContract.Error(),
},

{
name: "successfully_cancelled_contract_with_strays",
preRun: func() *types.MsgCancelContract {
dcid := tst
dcid := "test_stray_cid"
h := sha256.New()
_, err := io.WriteString(h, dcid)
suite.Require().NoError(err)
Expand All @@ -488,6 +534,7 @@ func (suite *KeeperTestSuite) TestCancelContract() {
Fid: "jklf1j3p63s42w7ywaczlju626st55mzu5z39w2rx9x",
Signee: user,
Filesize: "10",
End: 0,
}
sKeeper.SetStrays(suite.ctx, d)

Expand Down
2 changes: 1 addition & 1 deletion x/storage/keeper/msg_server_provider_claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (suite *KeeperTestSuite) TestAddProviderClaimer() {
ClaimAddress: claimAddress,
},
expErr: true,
errMsg: "Provider not found. Please init your provider.",
errMsg: types.ErrProviderNotFound.Error(),
},
}
for _, tcs := range cases {
Expand Down
8 changes: 4 additions & 4 deletions x/storage/keeper/msg_server_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (suite *KeeperTestSuite) TestMsgSetProviderIP() {
)
},
expErr: true,
expErrMsg: "Provider not found. Please init your provider.",
expErrMsg: types.ErrProviderNotFound.Error(),
name: "set provider ip fail",
},
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func (suite *KeeperTestSuite) TestMsgSetProviderTotalSpace() {
)
},
expErr: true,
expErrMsg: "Provider not found. Please init your provider.",
expErrMsg: types.ErrProviderNotFound.Error(),
name: "set provider total space fail",
},
{
Expand All @@ -181,7 +181,7 @@ func (suite *KeeperTestSuite) TestMsgSetProviderTotalSpace() {
)
},
expErr: true,
expErrMsg: "Not a valid total space. Please enter total number of bytes to provide.",
expErrMsg: types.ErrNotValidTotalSpace.Error(),
name: "invalid space param",
},
}
Expand Down Expand Up @@ -247,7 +247,7 @@ func (suite *KeeperTestSuite) TestMsgSetProviderKeybase() {
)
},
expErr: true,
expErrMsg: "Provider not found. Please init your provider.",
expErrMsg: types.ErrProviderNotFound.Error(),
name: "set provider keybase fail",
},
}
Expand Down
21 changes: 10 additions & 11 deletions x/storage/types/errors.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package types

// DONTCOVER

import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// x/storage module sentinel errors
// x/storage module error messages
var (
ErrDivideByZero = sdkerrors.Register(ModuleName, 1110, "DivideByZero")
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.")
ErrDealNotFound = sdkerrors.Register(ModuleName, 1114, "Cannot find active deal")
ErrFormNotFound = sdkerrors.Register(ModuleName, 1115, "Cannot find attestation form")
ErrAttestInvalid = sdkerrors.Register(ModuleName, 1116, "Cannot attest to form")
ErrAttestAlreadyExists = sdkerrors.Register(ModuleName, 1117, "Attest already exists")
ErrCannotVerifyProof = sdkerrors.Register(ModuleName, 1118, "Cannot verify Proof")
ErrDivideByZero = sdkerrors.Register(ModuleName, 1110, "divide by zero")
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")
ErrDealNotFound = sdkerrors.Register(ModuleName, 1114, "cannot find active deal")
ErrFormNotFound = sdkerrors.Register(ModuleName, 1115, "cannot find attestation form")
ErrAttestInvalid = sdkerrors.Register(ModuleName, 1116, "cannot attest to form")
ErrAttestAlreadyExists = sdkerrors.Register(ModuleName, 1117, "attest already exists")
ErrCannotVerifyProof = sdkerrors.Register(ModuleName, 1118, "cannot verify proof")
ErrNoCid = sdkerrors.Register(ModuleName, 1119, "cid does not exist")
ErrProviderExists = sdkerrors.Register(ModuleName, 1120, "provider already exists")
ErrCancelContract = sdkerrors.Register(ModuleName, 1121, "cannot cancel contract")
)

0 comments on commit d78caa6

Please sign in to comment.