diff --git a/client/record/cli/download.go b/client/record/cli/download.go index 1c632bd75..4b8508b7c 100644 --- a/client/record/cli/download.go +++ b/client/record/cli/download.go @@ -6,81 +6,56 @@ import ( "path/filepath" "github.com/cosmos/cosmos-sdk/wire" + "github.com/irisnet/irishub/client/context" + "github.com/irisnet/irishub/modules/record" "github.com/spf13/cobra" "github.com/spf13/viper" + cmn "github.com/tendermint/tendermint/libs/common" "github.com/tendermint/tmlibs/cli" shell "github.com/ipfs/go-ipfs-api" ) -func FileExists(path string) (bool, error) { - _, err := os.Stat(path) - if err == nil { - return true, nil - } - if os.IsNotExist(err) { - return false, nil - } - return false, err -} - -func GetCmdDownload(cdc *wire.Codec) *cobra.Command { +func GetCmdDownload(storeName string, cdc *wire.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "download [hash]", Short: "download specified file with tx hash", RunE: func(cmd *cobra.Command, args []string) error { - // trustNode := viper.GetBool(client.FlagTrustNode) - - // hashHexStr := viper.GetString(FlagTxHash) + cliCtx := context.NewCLIContext().WithCodec(cdc) downloadFileName := viper.GetString(FlagFileName) home := viper.GetString(cli.HomeFlag) + hashHexStr := viper.GetString(FlagTxHash) - //cliCtx := context.NewCLIContext().WithCodec(cdc) - - var err error - var record RecordMetadata + var tmpkey = cmn.HexBytes{} + res, err := cliCtx.QueryStore(tmpkey /*record.KeyProposal(hashHexStr)*/, storeName) + if len(res) == 0 || err != nil { + return fmt.Errorf("Record hash [%s] is not existed", hashHexStr) + } - // record, err := queryRecordMetadata(cdc, cliCtx, hashHexStr, trustNode) - // if err != nil { - // return err - // } + var submitFile record.MsgSubmitFile + cdc.MustUnmarshalBinary(res, &submitFile) - // if len(record.DataHash) == 0 { - // fmt.Printf("Request file was not found on the blockchain.\n") - // return nil - // } + if len(submitFile.DataHash) == 0 { + fmt.Errorf("Request file was not found on the blockchain.\n") + return nil + } - // WIP - record.DataHash = "QmTp2hEo8eXRp6wg7jXv1BLCMh5a4F3B7buAUZNZUu772j" // this data hash will be replaced in the future filePath := filepath.Join(home, downloadFileName) sh := shell.NewShell("localhost:5001") //Begin to download file from ipfs - exist, _ := FileExists(filePath) - if exist == true { + if _, err := os.Stat("/path/to/whatever"); !os.IsNotExist(err) { fmt.Printf("%v already exists, please try another file name.\n", filePath) - return nil + return err } - // fhandle, ferr := os.Open(filePath) // For read access. - // if ferr != nil { - // log.Fatal(err) - // } - - // fmt.Printf("Uploading %v ...\n", filePath) - // cid, err := sh.Add(bufio.NewReader(fhandle)) - // if err != nil { - // return err - // } - // fmt.Printf("this is uploadfile hash :%v\n", cid) - fmt.Printf("Downloading %v ...\n", filePath) - err = sh.Get(record.DataHash, filePath) + err = sh.Get(submitFile.DataHash, filePath) if err != nil { return err } - fmt.Printf("Download file complete.\n") + fmt.Println("Download file complete.") return nil }, diff --git a/client/record/cli/query.go b/client/record/cli/query.go index 3102ceca6..55c090192 100644 --- a/client/record/cli/query.go +++ b/client/record/cli/query.go @@ -4,41 +4,56 @@ import ( "fmt" "github.com/cosmos/cosmos-sdk/wire" - "github.com/irisnet/irishub/client" "github.com/irisnet/irishub/client/context" "github.com/irisnet/irishub/modules/record" "github.com/spf13/cobra" "github.com/spf13/viper" + + sdk "github.com/cosmos/cosmos-sdk/types" + recordClient "github.com/irisnet/irishub/client/record" + cmn "github.com/tendermint/tendermint/libs/common" ) type RecordMetadata struct { - OwnerAddress string - SubmitTime string + OwnerAddress sdk.AccAddress + SubmitTime int64 DataHash string - DataSize string - PinedNode string + DataSize int64 + //PinedNode string } -func GetCmdQureyHash(cdc *wire.Codec) *cobra.Command { +func GetCmdQureyHash(storeName string, cdc *wire.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "query [hash]", Short: "query specified file with tx hash", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - trustNode := viper.GetBool(client.FlagTrustNode) - + cliCtx := context.NewCLIContext().WithCodec(cdc) hashHexStr := viper.GetString(FlagTxHash) - cliCtx := context.NewCLIContext().WithCodec(cdc) + var tmpkey = cmn.HexBytes{} + res, err := cliCtx.QueryStore(tmpkey /*record.KeyProposal(hashHexStr)*/, storeName) + if len(res) == 0 || err != nil { + return fmt.Errorf("Record hash [%s] is not existed", hashHexStr) + } + + var submitFile record.MsgSubmitFile + cdc.MustUnmarshalBinary(res, &submitFile) + + recordResponse, err := recordClient.ConvertRecordToRecordOutput(cliCtx, submitFile) + if err != nil { + return err + } - record, err := queryRecordMetadata(cdc, cliCtx, hashHexStr, trustNode) + output, err := wire.MarshalJSONIndent(cdc, recordResponse) if err != nil { return err } - fmt.Println("Record metadata %v", record) + fmt.Println(string(output)) return nil + }, } @@ -49,7 +64,8 @@ func GetCmdQureyHash(cdc *wire.Codec) *cobra.Command { func queryRecordMetadata(cdc *wire.Codec, cliCtx context.CLIContext, hashHexStr string, trustNode bool) (RecordMetadata, error) { - tx, err := queryTx(cdc, cliCtx, hashHexStr, trustNode) + tx, err := QueryTx(cdc, cliCtx, hashHexStr, trustNode) + if err != nil { return RecordMetadata{}, err } @@ -57,11 +73,18 @@ func queryRecordMetadata(cdc *wire.Codec, cliCtx context.CLIContext, hashHexStr msgs := tx.GetMsgs() for i := 0; i < len(msgs); i++ { - if msgs[i].Type() == "record" { + if msgs[i].Type() == record.MsgType { var ok bool var m record.MsgSubmitFile if m, ok = msgs[i].(record.MsgSubmitFile); ok { - return GetMetadata(m) + + var metadata RecordMetadata + metadata.OwnerAddress = m.OwnerAddress + metadata.DataHash = m.DataHash + metadata.DataSize = m.DataSize + metadata.SubmitTime = m.SubmitTime + + return metadata, nil } return RecordMetadata{}, nil } @@ -70,14 +93,7 @@ func queryRecordMetadata(cdc *wire.Codec, cliCtx context.CLIContext, hashHexStr return RecordMetadata{}, nil } -func GetMetadata(msg record.MsgSubmitFile) (RecordMetadata, error) { - // Get record msg from record type msg (TO DO) - var metadata RecordMetadata - metadata.OwnerAddress = "address from record type msg" - metadata.DataHash = "data hash from record type msg" - metadata.DataSize = "data size from record type msg" - metadata.PinedNode = "pined node from record type msg" - metadata.SubmitTime = "submit time from record type msg" +func QueryTx(cdc *wire.Codec, cliCtx context.CLIContext, hashHexStr string, trustNode bool) (sdk.Tx, error) { - return metadata, nil + return nil, nil } diff --git a/client/record/cli/sim.go b/client/record/cli/sim.go deleted file mode 100644 index c77e32d4b..000000000 --- a/client/record/cli/sim.go +++ /dev/null @@ -1,45 +0,0 @@ -package cli - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" -) - -type MsgRecord struct { - OwnerAddress string - SubmitTime string - DataHash string - DataSize int - PinedNode string -} - -var tmp_msg = MsgRecord{ - - OwnerAddress: "this is owner address", - SubmitTime: "this is submit time", - DataHash: "this is data hash", - DataSize: 1000, - PinedNode: "this is pinednode", -} - -func (msg MsgRecord) Type() string { return "" } - -func (msg MsgRecord) ValidateBasic() sdk.Error { - // TO DO - return nil -} - -func (msg MsgRecord) String() string { - return "" -} - -func (msg MsgRecord) Get(key interface{}) (value interface{}) { - return nil -} - -func (msg MsgRecord) GetSignBytes() []byte { - return []byte("") -} - -func (msg MsgRecord) GetSigners() []sdk.AccAddress { - return []sdk.AccAddress{} -} diff --git a/client/record/cli/tx.go b/client/record/cli/tx.go deleted file mode 100644 index dc7944fe6..000000000 --- a/client/record/cli/tx.go +++ /dev/null @@ -1,83 +0,0 @@ -package cli - -import ( - "encoding/hex" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/wire" - "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/irisnet/irishub/client/context" - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/libs/common" - ctypes "github.com/tendermint/tendermint/rpc/core/types" -) - -type txInfo struct { - Hash common.HexBytes `json:"hash"` - Height int64 `json:"height"` - Tx sdk.Tx `json:"tx"` - Result abci.ResponseDeliverTx `json:"result"` -} - -func parseTx(cdc *wire.Codec, txBytes []byte) (sdk.Tx, error) { - var tx auth.StdTx - err := cdc.UnmarshalBinary(txBytes, &tx) - if err != nil { - return nil, err - } - return tx, nil -} - -func formatTxResult(cdc *wire.Codec, res *ctypes.ResultTx) (txInfo, error) { - // TODO: verify the proof if requested - tx, err := parseTx(cdc, res.Tx) - if err != nil { - return txInfo{}, err - } - - info := txInfo{ - Hash: res.Hash, - Height: res.Height, - Tx: tx, - Result: res.TxResult, - } - return info, nil -} - -func queryTx(cdc *wire.Codec, cliCtx context.CLIContext, hashHexStr string, trustNode bool) (sdk.Tx, error) { - hash, err := hex.DecodeString(hashHexStr) - if err != nil { - return nil, err - } - - node, err := cliCtx.GetNode() - if err != nil { - return nil, err - } - - res, err := node.Tx(hash, !trustNode) - if err != nil { - return nil, err - } - - tx, err := parseTx(cdc, res.Tx) - if err != nil { - return nil, err - } - - return tx, nil -} - -// ValidateTxResult performs transaction verification -func ValidateTxResult(cliCtx context.CLIContext, res *ctypes.ResultTx) error { - check, err := cliCtx.Certify(res.Height) - if err != nil { - return err - } - - err = res.Proof.Validate(check.Header.DataHash) - if err != nil { - return err - } - return nil -} diff --git a/client/record/common.go b/client/record/common.go new file mode 100644 index 000000000..3112716f4 --- /dev/null +++ b/client/record/common.go @@ -0,0 +1,34 @@ +package gov + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/irisnet/irishub/client/context" + "github.com/irisnet/irishub/modules/record" +) + +type RecordOutput struct { + Filename string `json:"Filename"` // Filename of the File + Filepath string `json:"Filepath"` // full path of the File + Description string `json:"Description"` // Description of the File + SubmitTime int64 `json:"SubmitTime"` // File submit unix timestamp + OwnerAddress sdk.AccAddress `json:"OwnerAddress"` // Address of the owner + DataHash string `json:"DataHash"` // ipfs hash of file + DataSize int64 `json:"DataSize"` // File Size in bytes + //PinedNode string `json:"PinedNode"` //pined node of ipfs +} + +func ConvertRecordToRecordOutput(cliCtx context.CLIContext, r record.MsgSubmitFile) (RecordOutput, error) { + + // TODO : Currently we only copy values from record msg, we can call related methods later + recordOutput := RecordOutput{ + Filename: r.Filename, + Filepath: r.Filepath, + Description: r.Description, + SubmitTime: r.SubmitTime, + OwnerAddress: r.OwnerAddress, + DataHash: r.DataHash, + DataSize: r.DataSize, + } + + return recordOutput, nil +} diff --git a/cmd/iriscli/main.go b/cmd/iriscli/main.go index 4c48898f7..c079bd626 100644 --- a/cmd/iriscli/main.go +++ b/cmd/iriscli/main.go @@ -177,8 +177,8 @@ func main() { recordCmd.AddCommand( client.GetCommands( - recordcmd.GetCmdQureyHash(cdc), - recordcmd.GetCmdDownload(cdc), + recordcmd.GetCmdQureyHash("record", cdc), + recordcmd.GetCmdDownload("record", cdc), )...) recordCmd.AddCommand(