1 Star 0 Fork 0

艾鸥科技 / go-aiou

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
block.go 1.65 KB
Copy Edit Raw Blame History
张卓 authored 2020-05-10 14:55 . init
package iwallet
import (
"fmt"
"strconv"
"gitee.com/aiou-official/go-aiou/sdk"
"gitee.com/aiou-official/go-aiou/rpc/pb"
"github.com/spf13/cobra"
)
var method string
var complete bool
var methodMap = map[string]func(string) (*rpcpb.BlockResponse, error){
"num": func(arg string) (*rpcpb.BlockResponse, error) {
num, err := strconv.ParseInt(arg, 10, 64)
if err != nil {
err = fmt.Errorf("invalid block number: %v", err)
return nil, err
}
return iwalletSDK.GetBlockByNum(num, complete)
},
"hash": func(arg string) (*rpcpb.BlockResponse, error) {
return iwalletSDK.GetBlockByHash(arg, complete)
},
}
// blockCmd represents the block command.
var blockCmd = &cobra.Command{
Use: "block blockNum|blockHash",
Short: "Print block info",
Long: `Print block info by block number or hash`,
Example: ` iwallet block 0
iwallet block 5dEgmyMURGfe7GxvTLajmaLXTkcqs5JwiJ2C2DE5VvVX -m hash`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errorWithHelp(cmd, "please enter the block number or hash")
}
_, ok := methodMap[method]
if !ok {
return errorWithHelp(cmd, "wrong method: %v", method)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
blockInfo, err := methodMap[method](args[0])
if err != nil {
return err
}
fmt.Println(sdk.MarshalTextString(blockInfo))
return nil
},
}
func init() {
rootCmd.AddCommand(blockCmd)
blockCmd.Flags().StringVarP(&method, "method", "m", "num", `find by block num (set as "num") or hash (set as "hash")`)
blockCmd.Flags().BoolVarP(&complete, "complete", "c", false, "indicate whether to fetch all the transactions in the block or not")
}
1
https://gitee.com/aiou-official/go-aiou.git
git@gitee.com:aiou-official/go-aiou.git
aiou-official
go-aiou
go-aiou
376a44096468

Search