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

R4R: Design changed : Query record meta data from abci db #367

Merged
merged 3 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 21 additions & 46 deletions client/record/cli/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
62 changes: 39 additions & 23 deletions client/record/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

},
}

Expand All @@ -49,19 +64,27 @@ 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
}

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
}
Expand All @@ -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
}
45 changes: 0 additions & 45 deletions client/record/cli/sim.go

This file was deleted.

83 changes: 0 additions & 83 deletions client/record/cli/tx.go

This file was deleted.

34 changes: 34 additions & 0 deletions client/record/common.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions cmd/iriscli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down