示例#1
0
func getLastBlocks(c *gcli.Context) error {
	num := c.Args().First()
	if num == "" {
		num = "1"
	}

	n, err := strconv.ParseUint(num, 10, 64)
	if err != nil {
		return fmt.Errorf("invalid block number, %s", err)
	}

	if n <= 0 {
		return errors.New("block number must >= 0")
	}

	param := []uint64{n}
	req, err := webrpc.NewRequest("get_lastblocks", param, "1")
	if err != nil {
		return fmt.Errorf("do rpc request failed: %v", err)
	}

	rsp, err := webrpc.Do(req, cfg.RPCAddress)
	if err != nil {
		return err
	}

	if rsp.Error != nil {
		return fmt.Errorf("do rpc request failed: %+v", *rsp.Error)
	}

	fmt.Println(string(rsp.Result))
	return nil
}
示例#2
0
文件: status.go 项目: skycoin/skycoin
func statusCMD() gcli.Command {
	name := "status"
	return gcli.Command{
		Name:         name,
		Usage:        "Check the status of current skycoin node",
		ArgsUsage:    " ",
		OnUsageError: onCommandUsageError(name),
		Action: func(c *gcli.Context) error {
			var status = struct {
				webrpc.StatusResult
				RPCAddress string `json:"webrpc_address"`
			}{
				RPCAddress: cfg.RPCAddress,
			}

			req, err := webrpc.NewRequest("get_status", nil, "1")
			if err != nil {
				return fmt.Errorf("create rpc request failed: %v", err)
			}

			rsp, err := webrpc.Do(req, cfg.RPCAddress)
			if err != nil {
				return fmt.Errorf("do rpc request failed: %v", err)
			}

			if rsp.Error != nil {
				return fmt.Errorf("do rpc request failed: %+v", *rsp.Error)
			}

			var rlt webrpc.StatusResult
			if err := json.NewDecoder(bytes.NewBuffer(rsp.Result)).Decode(&rlt); err != nil {
				return errJSONUnmarshal
			}

			status.StatusResult = rlt

			d, err := json.MarshalIndent(status, "", "    ")
			if err != nil {
				return errJSONMarshal
			}
			fmt.Println(string(d))
			return nil
		},
	}
}
示例#3
0
文件: blocks.go 项目: skycoin/skycoin
func getBlocks(c *gcli.Context) error {
	// get start
	start := c.Args().Get(0)
	end := c.Args().Get(1)
	if end == "" {
		end = start
	}

	if start == "" {
		gcli.ShowSubcommandHelp(c)
		return nil
	}

	s, err := strconv.ParseUint(start, 10, 64)
	if err != nil {
		return fmt.Errorf("invalid block seq: %v, must be unsigned integer", start)
	}

	e, err := strconv.ParseUint(end, 10, 64)
	if err != nil {
		return fmt.Errorf("invalid block seq: %v, must be unsigned integer", end)
	}

	param := []uint64{s, e}

	req, err := webrpc.NewRequest("get_blocks", param, "1")
	if err != nil {
		return fmt.Errorf("create rpc request failed: %v", err)
	}

	rsp, err := webrpc.Do(req, cfg.RPCAddress)
	if err != nil {
		return fmt.Errorf("do rpc request failed: %v", err)
	}

	if rsp.Error != nil {
		return fmt.Errorf("rpc response error: %+v", *rsp.Error)
	}

	fmt.Println(string(rsp.Result))
	return nil
}
示例#4
0
文件: blocks.go 项目: skycoin/skycoin
func getBlocksBySeq(ss []uint64) (*visor.ReadableBlocks, error) {
	req, err := webrpc.NewRequest("get_blocks_by_seq", ss, "1")
	if err != nil {
		return nil, fmt.Errorf("create rpc request failed: %v", err)
	}

	rsp, err := webrpc.Do(req, cfg.RPCAddress)
	if err != nil {
		return nil, fmt.Errorf("do rpc request failed: %v", err)
	}

	if rsp.Error != nil {
		return nil, fmt.Errorf("rpc response error: %+v", *rsp.Error)
	}

	blks := visor.ReadableBlocks{}
	if err := json.NewDecoder(bytes.NewReader(rsp.Result)).Decode(&blks); err != nil {
		return nil, err
	}
	return &blks, nil
}
示例#5
0
func getTransactionByID(txid string) (*webrpc.TxnResult, error) {
	req, err := webrpc.NewRequest("get_transaction", []string{txid}, "1")
	if err != nil {
		return nil, fmt.Errorf("create rpc request failed:%v", err)
	}

	rsp, err := webrpc.Do(req, cfg.RPCAddress)
	if err != nil {
		return nil, fmt.Errorf("do rpc request failed:%v", err)
	}

	if rsp.Error != nil {
		return nil, fmt.Errorf("do rpc request failed:%+v", *rsp.Error)
	}

	rlt := webrpc.TxnResult{}
	if err := json.NewDecoder(bytes.NewReader(rsp.Result)).Decode(&rlt); err != nil {
		return nil, err
	}
	return &rlt, nil
}
示例#6
0
func getAddrUxOuts(addrs []string) ([]webrpc.AddrUxoutResult, error) {
	req, err := webrpc.NewRequest("get_address_uxouts", addrs, "1")
	if err != nil {
		return nil, fmt.Errorf("create rpc request failed:%v", err)
	}

	rsp, err := webrpc.Do(req, cfg.RPCAddress)
	if err != nil {
		return nil, fmt.Errorf("do rpc request failed:%v", err)
	}

	if rsp.Error != nil {
		return nil, fmt.Errorf("do rpc request failed:%+v", *rsp.Error)
	}

	uxouts := []webrpc.AddrUxoutResult{}
	if err := json.NewDecoder(bytes.NewReader(rsp.Result)).Decode(&uxouts); err != nil {
		return nil, fmt.Errorf("decode result failed, err:%v", err)
	}

	return uxouts, nil
}
示例#7
0
文件: cli.go 项目: skycoin/skycoin
func getUnspent(addrs []string) (unspentOutSet, error) {
	req, err := webrpc.NewRequest("get_outputs", addrs, "1")
	if err != nil {
		return unspentOutSet{}, fmt.Errorf("create webrpc request failed:%v", err)
	}

	rsp, err := webrpc.Do(req, cfg.RPCAddress)
	if err != nil {
		return unspentOutSet{}, fmt.Errorf("do rpc request failed:%v", err)
	}

	if rsp.Error != nil {
		return unspentOutSet{}, fmt.Errorf("rpc request failed, %+v", *rsp.Error)
	}

	var rlt webrpc.OutputsResult
	if err := json.NewDecoder(bytes.NewBuffer(rsp.Result)).Decode(&rlt); err != nil {
		return unspentOutSet{}, errJSONUnmarshal
	}

	return unspentOutSet{rlt.Outputs}, nil
}
示例#8
0
func broadcastTx(rawtx string) (string, error) {
	params := []string{rawtx}
	req, err := webrpc.NewRequest("inject_transaction", params, "1")
	if err != nil {
		return "", fmt.Errorf("create rpc request failed, %v", err)
	}

	rsp, err := webrpc.Do(req, cfg.RPCAddress)
	if err != nil {
		return "", fmt.Errorf("do rpc request failed, %v", err)
	}

	if rsp.Error != nil {
		return "", fmt.Errorf("rpc request failed, %+v", *rsp.Error)
	}

	var rlt webrpc.InjectResult
	if err := json.NewDecoder(bytes.NewBuffer(rsp.Result)).Decode(&rlt); err != nil {
		return "", fmt.Errorf("decode inject result failed")
	}

	return rlt.Txid, nil
}