"github.com/spf13/cobra"
jww "github.com/spf13/jwalterweatherman"
+
+ "github.com/bytom/util"
)
var createAccessTokenCmd = &cobra.Command{
var token accessToken
token.ID = args[0]
- data, exitCode := clientCall("/create-access-token", &token)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/create-access-token", &token)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
printJSON(data)
Short: "List the existing access tokens",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
- data, exitCode := clientCall("/list-access-tokens")
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/list-access-tokens")
+ if exitCode != util.Success {
os.Exit(exitCode)
}
var token accessToken
token.ID = args[0]
- if _, exitCode := clientCall("/delete-access-token", &token); exitCode != Success {
+ if _, exitCode := util.ClientCall("/delete-access-token", &token); exitCode != util.Success {
os.Exit(exitCode)
}
jww.FEEDBACK.Println("Successfully delete access token")
token.ID = args[0]
token.Secret = args[1]
- if _, exitCode := clientCall("/check-access-token", &token); exitCode != Success {
+ if _, exitCode := util.ClientCall("/check-access-token", &token); exitCode != util.Success {
os.Exit(exitCode)
}
jww "github.com/spf13/jwalterweatherman"
"github.com/bytom/crypto/ed25519/chainkd"
+ "github.com/bytom/util"
)
func init() {
xpub := chainkd.XPub{}
if err := xpub.UnmarshalText([]byte(x)); err != nil {
jww.ERROR.Println(err)
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
ins.RootXPubs = append(ins.RootXPubs, xpub)
}
tags := strings.Split(accountTags, ":")
if len(tags) != 2 {
jww.ERROR.Println("Invalid tags")
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
ins.Tags = map[string]interface{}{tags[0]: tags[1]}
}
ins.AccessToken = accountToken
- data, exitCode := clientCall("/create-account", &ins)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/create-account", &ins)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
ID string `json:"id"`
}{ID: accountID}
- data, exitCode := clientCall("/list-accounts", &filter)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/list-accounts", &filter)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
AccountInfo string `json:"account_info"`
}{AccountInfo: args[0]}
- if _, exitCode := clientCall("/delete-account", accountInfo); exitCode != Success {
+ if _, exitCode := util.ClientCall("/delete-account", accountInfo); exitCode != util.Success {
os.Exit(exitCode)
}
tags := strings.Split(accountUpdateTags, ":")
if len(tags) != 2 {
jww.ERROR.Println("Invalid tags")
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
updateTag.Tags = map[string]interface{}{tags[0]: tags[1]}
}
updateTag.AccountInfo = args[0]
- if _, exitCode := clientCall("/update-account-tags", &updateTag); exitCode != Success {
+ if _, exitCode := util.ClientCall("/update-account-tags", &updateTag); exitCode != util.Success {
os.Exit(exitCode)
}
ExpiresAt time.Time `json:"expires_at,omitempty"`
}{AccountInfo: args[0]}
- data, exitCode := clientCall("/create-account-receiver", &ins)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/create-account-receiver", &ins)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
ExpiresAt time.Time `json:"expires_at,omitempty"`
}{AccountInfo: args[0]}
- data, exitCode := clientCall("/create-account-address", &ins)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/create-account-address", &ins)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
Short: "List the accounts balances",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
- data, exitCode := clientCall("/list-balances")
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/list-balances")
+ if exitCode != util.Success {
os.Exit(exitCode)
}
ID string `json:"id"`
}{ID: outputID}
- data, exitCode := clientCall("/list-unspent-outputs", &filter)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/list-unspent-outputs", &filter)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
jww "github.com/spf13/jwalterweatherman"
"github.com/bytom/crypto/ed25519/chainkd"
+ "github.com/bytom/util"
)
func init() {
xpub := chainkd.XPub{}
if err := xpub.UnmarshalText([]byte(x)); err != nil {
jww.ERROR.Println(err)
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
ins.RootXPubs = append(ins.RootXPubs, xpub)
}
tags := strings.Split(assetTags, ":")
if len(tags) != 2 {
jww.ERROR.Println("Invalid tags")
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
ins.Tags = map[string]interface{}{tags[0]: tags[1]}
}
definition := strings.Split(assetDefiniton, ":")
if len(definition) != 2 {
jww.ERROR.Println("Invalid definition")
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
ins.Definition = map[string]interface{}{definition[0]: definition[1]}
}
- data, exitCode := clientCall("/create-asset", &ins)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/create-asset", &ins)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
ID string `json:"id"`
}{ID: assetID}
- data, exitCode := clientCall("/list-assets", &filter)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/list-assets", &filter)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
tags := strings.Split(assetUpdateTags, ":")
if len(tags) != 2 {
jww.ERROR.Println("Invalid tags")
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
updateTag.Tags = map[string]interface{}{tags[0]: tags[1]}
}
updateTag.AssetInfo = args[0]
- if _, exitCode := clientCall("/update-asset-tags", &updateTag); exitCode != Success {
+ if _, exitCode := util.ClientCall("/update-asset-tags", &updateTag); exitCode != util.Success {
os.Exit(exitCode)
}
"github.com/spf13/cobra"
jww "github.com/spf13/jwalterweatherman"
-
- "github.com/bytom/blockchain"
- "github.com/bytom/env"
-)
-
-var (
- home = blockchain.HomeDirFromEnvironment()
- coreURL = env.String("BYTOM_URL", "http://localhost:9888")
+ "github.com/bytom/util"
)
var blockHashCmd = &cobra.Command{
Short: "Get the hash of most recent block",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
- data, exitCode := clientCall("block-hash")
- if exitCode != Success {
+ data, exitCode := util.ClientCall("block-hash")
+ if exitCode != util.Success {
os.Exit(exitCode)
}
printJSON(data)
Short: "Get the number of most recent block",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
- data, exitCode := clientCall("/block-height")
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/block-height")
+ if exitCode != util.Success {
os.Exit(exitCode)
}
printJSON(data)
Short: "Get a whole block matching the given hash",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
- data, exitCode := clientCall("/get-block-by-hash", args[0])
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/get-block-by-hash", args[0])
+ if exitCode != util.Success {
os.Exit(exitCode)
}
printJSON(data)
Short: "Get the header of a block matching the given hash",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
- data, exitCode := clientCall("/get-block-header-by-hash", args[0])
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/get-block-header-by-hash", args[0])
+ if exitCode != util.Success {
os.Exit(exitCode)
}
printJSON(data)
Short: "Get the transactions count of a block matching the given hash",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
- data, exitCode := clientCall("/get-block-transactions-count-by-hash", args[0])
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/get-block-transactions-count-by-hash", args[0])
+ if exitCode != util.Success {
os.Exit(exitCode)
}
printJSON(data)
height, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
jww.ERROR.Printf("Invalid height value")
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
- data, exitCode := clientCall("/get-block-by-height", height)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/get-block-by-height", height)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
ui64, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
jww.ERROR.Printf("Invalid height value")
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
- data, exitCode := clientCall("/get-block-transactions-count-by-height", ui64)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/get-block-transactions-count-by-height", ui64)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
package commands
import (
- "context"
"fmt"
- "net"
- "net/http"
"os"
- "path/filepath"
"regexp"
- "strings"
- "time"
"github.com/spf13/cobra"
- jww "github.com/spf13/jwalterweatherman"
- "github.com/bytom/blockchain"
- "github.com/bytom/blockchain/rpc"
-)
-
-const (
- // Success indicates the rpc calling is successful.
- Success = iota
- // ErrLocalExe indicates error occurs before the rpc calling.
- ErrLocalExe
- // ErrConnect indicates error occurs connecting to the bytomd, e.g.,
- // bytomd can't parse the received arguments.
- ErrConnect
- // ErrLocalParse indicates error occurs locally when parsing the response.
- ErrLocalParse
- // ErrRemote indicates error occurs in bytomd.
- ErrRemote
+ "github.com/bytom/util"
)
// commandError is an error used to signal different error situations in command handling.
AddCommands()
if _, err := BytomcliCmd.ExecuteC(); err != nil {
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
}
BytomcliCmd.AddCommand(versionCmd)
}
-func mustRPCClient() *rpc.Client {
- // TODO(kr): refactor some of this cert-loading logic into bytom/blockchain
- // and use it from cored as well.
- // Note that this function, unlike maybeUseTLS in cored,
- // does not load the cert and key from env vars,
- // only from the filesystem.
- certFile := filepath.Join(home, "tls.crt")
- keyFile := filepath.Join(home, "tls.key")
- config, err := blockchain.TLSConfig(certFile, keyFile, "")
- if err == blockchain.ErrNoTLS {
- return &rpc.Client{BaseURL: *coreURL}
- } else if err != nil {
- jww.ERROR.Println("loading TLS cert:", err)
- }
-
- t := &http.Transport{
- DialContext: (&net.Dialer{
- Timeout: 30 * time.Second,
- KeepAlive: 30 * time.Second,
- DualStack: true,
- }).DialContext,
- MaxIdleConns: 100,
- IdleConnTimeout: 90 * time.Second,
- TLSClientConfig: config,
- TLSHandshakeTimeout: 10 * time.Second,
- ExpectContinueTimeout: 1 * time.Second,
- }
-
- url := *coreURL
- if strings.HasPrefix(url, "http:") {
- url = "https:" + url[5:]
- }
-
- return &rpc.Client{
- BaseURL: url,
- Client: &http.Client{Transport: t},
- }
-}
-
-func clientCall(path string, req ...interface{}) (interface{}, int) {
-
- var response = &blockchain.Response{}
- var request interface{}
-
- if req != nil {
- request = req[0]
- }
- client := mustRPCClient()
- client.Call(context.Background(), path, request, response)
-
- switch response.Status {
- case blockchain.FAIL:
- jww.ERROR.Println(response.Msg)
- return nil, ErrRemote
- case "":
- jww.ERROR.Println("Unable to connect to the bytomd")
- return nil, ErrConnect
- }
-
- return response.Data, Success
-}
"github.com/bytom/crypto/ed25519/chainkd"
"github.com/bytom/crypto/sha3pool"
+ "github.com/bytom/util"
)
var createKeyCmd = &cobra.Command{
Password string `json:"password"`
}{Alias: args[0], Password: "123456"}
- data, exitCode := clientCall("/create-key", &key)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/create-key", &key)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
xpub := new(chainkd.XPub)
if err := xpub.UnmarshalText([]byte(args[0])); err != nil {
jww.ERROR.Println("delete-key:", err)
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
var key = struct {
XPub chainkd.XPub `json:"xpubs"`
}{XPub: *xpub, Password: "123456"}
- if _, exitCode := clientCall("/delete-key", &key); exitCode != Success {
+ if _, exitCode := util.ClientCall("/delete-key", &key); exitCode != util.Success {
os.Exit(exitCode)
}
jww.FEEDBACK.Println("Successfully delete key")
Short: "List the existing keys",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
- data, exitCode := clientCall("/list-keys")
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/list-keys")
+ if exitCode != util.Success {
os.Exit(exitCode)
}
key.XPub = *xpub
key.Password = args[1]
- data, exitCode := clientCall("/export-private-key", &key)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/export-private-key", &key)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
privHash, err := base58.Decode(args[1])
if err != nil {
jww.ERROR.Println("wif priv decode error")
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
if len(privHash) != 68 {
jww.ERROR.Println("wif priv length error")
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
var hashed [32]byte
if res := bytes.Compare(hashed[:4], privHash[64:]); res != 0 {
jww.ERROR.Println("wif priv hash error")
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
var key Key
key.Index, _ = strconv.ParseUint(args[2], 10, 64)
copy(key.XPrv[:], privHash[:64])
- data, exitCode := clientCall("/import-private-key", &key)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/import-private-key", &key)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
printJSON(data)
"os"
"github.com/spf13/cobra"
+ "github.com/bytom/util"
)
var isMiningCmd = &cobra.Command{
Short: "If client is actively mining new blocks",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
- data, exitCode := clientCall("/is-mining")
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/is-mining")
+ if exitCode != util.Success {
os.Exit(exitCode)
}
printJSON(data)
"github.com/spf13/cobra"
jww "github.com/spf13/jwalterweatherman"
+
+ "github.com/bytom/util"
)
var netInfoCmd = &cobra.Command{
Short: "Print the summary of network",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
- data, exitCode := clientCall("/net-info")
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/net-info")
+ if exitCode != util.Success {
os.Exit(exitCode)
}
printJSON(data)
Use: "net-listening",
Short: "If client is actively listening for network connections",
Run: func(cmd *cobra.Command, args []string) {
- data, exitCode := clientCall("/net-info")
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/net-info")
+ if exitCode != util.Success {
os.Exit(exitCode)
}
Short: "Number of peers currently connected to the client",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
- data, exitCode := clientCall("/net-info")
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/net-info")
+ if exitCode != util.Success {
os.Exit(exitCode)
}
Short: "If the network is still syncing",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
- data, exitCode := clientCall("/net-info")
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/net-info")
+ if exitCode != util.Success {
os.Exit(exitCode)
}
"github.com/bytom/blockchain"
"github.com/bytom/blockchain/txbuilder"
+ "github.com/bytom/util"
)
func init() {
buildReqStr = fmt.Sprintf(buildControlAddressReqFmt, btmGas, accountInfo, assetInfo, amount, accountInfo, assetInfo, amount, address)
default:
jww.ERROR.Println("Invalid transaction template type")
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
var buildReq blockchain.BuildRequest
if err := json.Unmarshal([]byte(buildReqStr), &buildReq); err != nil {
jww.ERROR.Println(err)
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
- data, exitCode := clientCall("/build-transaction", &buildReq)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/build-transaction", &buildReq)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
dataMap, ok := data.(map[string]interface{})
if ok != true {
jww.ERROR.Println("invalid type assertion")
- os.Exit(ErrLocalParse)
+ os.Exit(util.ErrLocalParse)
}
rawTemplate, err := json.Marshal(dataMap)
if err != nil {
jww.ERROR.Println(err)
- os.Exit(ErrLocalParse)
+ os.Exit(util.ErrLocalParse)
}
jww.FEEDBACK.Printf("Template Type: %s\n%s\n", buildType, string(rawTemplate))
err := json.Unmarshal([]byte(args[0]), &template)
if err != nil {
jww.ERROR.Println(err)
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
var req = struct {
}{Auth: "123456", Txs: template}
jww.FEEDBACK.Printf("\n\n")
- data, exitCode := clientCall("/sign-transaction", &req)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/sign-transaction", &req)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
dataMap, ok := data.(map[string]interface{})
if ok != true {
jww.ERROR.Println("invalid type assertion")
- os.Exit(ErrLocalParse)
+ os.Exit(util.ErrLocalParse)
}
rawSign, err := json.Marshal(dataMap)
if err != nil {
jww.ERROR.Println(err)
- os.Exit(ErrLocalParse)
+ os.Exit(util.ErrLocalParse)
}
jww.FEEDBACK.Printf("\nSign Template:\n%s\n", string(rawSign))
},
err := json.Unmarshal([]byte(args[0]), &template)
if err != nil {
jww.ERROR.Println(err)
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
jww.FEEDBACK.Printf("\n\n")
- data, exitCode := clientCall("/submit-transaction", &template)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/submit-transaction", &template)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
err := json.Unmarshal([]byte(args[0]), &template)
if err != nil {
jww.ERROR.Println(err)
- os.Exit(ErrLocalExe)
+ os.Exit(util.ErrLocalExe)
}
var req = struct {
}{Auth: "123456", Txs: template}
jww.FEEDBACK.Printf("\n\n")
- data, exitCode := clientCall("/sign-submit-transaction", &req)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/sign-submit-transaction", &req)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
AccountID string `json:"account_id"`
}{ID: txID, AccountID: account}
- data, exitCode := clientCall("/list-transactions", &filter)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/list-transactions", &filter)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
Short: "Print the current gas rate",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
- data, exitCode := clientCall("/gas-rate")
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/gas-rate")
+ if exitCode != util.Success {
os.Exit(exitCode)
}
printJSON(data)
"github.com/spf13/cobra"
jww "github.com/spf13/jwalterweatherman"
+
+ "github.com/bytom/util"
)
var createTransactionFeedCmd = &cobra.Command{
in.Alias = args[0]
in.Filter = args[1]
- _, exitCode := clientCall("/create-transaction-feed", &in)
- if exitCode != Success {
+ _, exitCode := util.ClientCall("/create-transaction-feed", &in)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
Short: "list all of transaction feeds",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
- data, exitCode := clientCall("/list-transaction-feeds")
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/list-transaction-feeds")
+ if exitCode != util.Success {
os.Exit(exitCode)
}
printJSONList(data)
var in txFeed
in.Alias = args[0]
- _, exitCode := clientCall("/delete-transaction-feed", &in)
- if exitCode != Success {
+ _, exitCode := util.ClientCall("/delete-transaction-feed", &in)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
var in txFeed
in.Alias = args[0]
- data, exitCode := clientCall("/get-transaction-feed", &in)
- if exitCode != Success {
+ data, exitCode := util.ClientCall("/get-transaction-feed", &in)
+ if exitCode != util.Success {
os.Exit(exitCode)
}
printJSON(data)
in.Alias = args[0]
in.Filter = args[1]
- if _, exitCode := clientCall("/update-transaction-feed", &in); exitCode != Success {
+ if _, exitCode := util.ClientCall("/update-transaction-feed", &in); exitCode != util.Success {
os.Exit(exitCode)
}
jww.FEEDBACK.Println("Successfully updated transaction feed")
"github.com/bytom/crypto/ed25519/chainkd"
"github.com/bytom/encoding/json"
+ "github.com/bytom/util"
)
// accountIns is used for account related request.
dataMap, ok := data.(map[string]interface{})
if ok != true {
jww.ERROR.Println("invalid type assertion")
- os.Exit(ErrLocalParse)
+ os.Exit(util.ErrLocalParse)
}
rawData, err := stdjson.MarshalIndent(dataMap, "", " ")
if err != nil {
jww.ERROR.Println(err)
- os.Exit(ErrLocalParse)
+ os.Exit(util.ErrLocalParse)
}
jww.FEEDBACK.Println(string(rawData))
dataList, ok := data.([]interface{})
if ok != true {
jww.ERROR.Println("invalid type assertion")
- os.Exit(ErrLocalParse)
+ os.Exit(util.ErrLocalParse)
}
for idx, item := range dataList {
rawData, err := stdjson.MarshalIndent(item, "", " ")
if err != nil {
jww.ERROR.Println(err)
- os.Exit(ErrLocalParse)
+ os.Exit(util.ErrLocalParse)
}
jww.FEEDBACK.Println(string(rawData))
// Default configurable rpc parameters.
func DefaultRPCConfig() *RPCConfig {
return &RPCConfig{
- ListenAddress: "tcp://0.0.0.0:46657",
+ ListenAddress: "tcp://0.0.0.0:9888",
GRPCListenAddress: "",
Unsafe: false,
}
TLSNextProto: map[string]func(*http.Server, *tls.Conn, http.Handler){},
}
listenAddr := env.String("LISTEN", config.ApiAddress)
+ log.WithField("api address:", config.ApiAddress).Info("Rpc listen")
listener, err := net.Listen("tcp", *listenAddr)
if err != nil {
cmn.Exit(cmn.Fmt("Failed to register tcp port: %v", err))
package integration
import (
- "os"
+ "fmt"
"testing"
+ "time"
+ "os"
+
+ cfg "github.com/bytom/config"
+ "github.com/bytom/crypto/ed25519/chainkd"
+ "github.com/bytom/node"
+ "github.com/bytom/util"
)
-func TestMain(m *testing.M) {
- os.Exit(m.Run())
+// Mock config.
+func mockConfig() *cfg.Config {
+ var config = cfg.DefaultConfig()
+ config.Wallet.Disable = false
+ config.Mining = true
+ config.ApiAddress = "127.0.0.1:9888"
+ return config
+}
+
+// Test net-info call api.
+func testNet() bool {
+ data, exitCode := util.ClientCall("/net-info")
+ if exitCode != util.Success {
+ return false
+ }
+ dataMap, ok := data.(map[string]interface{})
+ if ok && dataMap["listening"].(bool) && dataMap["syncing"].(bool) && dataMap["mining"].(bool) {
+ return true
+ }
+ return false
+}
+
+// Test create-key delete-key list-key api and function.
+func testKey() bool {
+ var key = struct {
+ Alias string `json:"alias"`
+ Password string `json:"password"`
+ }{Alias: "alice", Password: "123456"}
+
+ data, exitCode := util.ClientCall("/create-key", &key)
+ if exitCode != util.Success {
+ return false
+ }
+ dataMap, ok := data.(map[string]interface{})
+ if (ok && dataMap["alias"].(string) == "alice") == false {
+ return false
+ }
+
+ _, exitCode1 := util.ClientCall("/list-keys")
+ if exitCode1 != util.Success {
+ return false
+ }
+
+ fmt.Println("dataMap", dataMap)
+ xpub := new(chainkd.XPub)
+ if err := xpub.UnmarshalText([]byte(dataMap["xpub"].(string))); err != nil {
+ return false
+ }
+
+ var key1 = struct {
+ Password string
+ XPub chainkd.XPub `json:"xpubs"`
+ }{XPub: *xpub, Password: "123456"}
+
+ if _, exitCode := util.ClientCall("/delete-key", &key1); exitCode != util.Success {
+ return false
+ }
+
+ return true
+}
+
+// Test node running.
+func TestRunNode(t *testing.T) {
+ // Create & start node
+ config := mockConfig()
+ n := node.NewNodeDefault(config)
+ if _, err := n.Start(); err != nil {
+ t.Fatalf("Failed to start node: %v", err)
+ }
+
+ go func() {
+ time.Sleep(3000 * time.Millisecond)
+ if testNet() && testKey() {
+ os.RemoveAll("./data")
+ os.RemoveAll("./keystore")
+ os.Exit(0)
+ } else {
+ os.RemoveAll("./data")
+ os.RemoveAll("./keystore")
+ os.Exit(1)
+ }
+ }()
+ // Trap signal, run forever.
+ n.RunForever()
}
--- /dev/null
+package util
+
+import (
+ "context"
+ "net"
+ "net/http"
+ "path/filepath"
+ "strings"
+ "time"
+
+ "github.com/bytom/blockchain"
+ "github.com/bytom/blockchain/rpc"
+ "github.com/bytom/env"
+ jww "github.com/spf13/jwalterweatherman"
+)
+
+const (
+ // Success indicates the rpc calling is successful.
+ Success = iota
+ // ErrLocalExe indicates error occurs before the rpc calling.
+ ErrLocalExe
+ // ErrConnect indicates error occurs connecting to the bytomd, e.g.,
+ // bytomd can't parse the received arguments.
+ ErrConnect
+ // ErrLocalParse indicates error occurs locally when parsing the response.
+ ErrLocalParse
+ // ErrRemote indicates error occurs in bytomd.
+ ErrRemote
+)
+
+var (
+ home = blockchain.HomeDirFromEnvironment()
+ coreURL = env.String("BYTOM_URL", "http://localhost:9888")
+)
+
+// Wraper rpc's client
+func mustRPCClient() *rpc.Client {
+ // TODO(kr): refactor some of this cert-loading logic into bytom/blockchain
+ // and use it from cored as well.
+ // Note that this function, unlike maybeUseTLS in cored,
+ // does not load the cert and key from env vars,
+ // only from the filesystem.
+ certFile := filepath.Join(home, "tls.crt")
+ keyFile := filepath.Join(home, "tls.key")
+ config, err := blockchain.TLSConfig(certFile, keyFile, "")
+ if err == blockchain.ErrNoTLS {
+ return &rpc.Client{BaseURL: *coreURL}
+ } else if err != nil {
+ jww.ERROR.Println("loading TLS cert:", err)
+ }
+
+ t := &http.Transport{
+ DialContext: (&net.Dialer{
+ Timeout: 30 * time.Second,
+ KeepAlive: 30 * time.Second,
+ DualStack: true,
+ }).DialContext,
+ MaxIdleConns: 100,
+ IdleConnTimeout: 90 * time.Second,
+ TLSClientConfig: config,
+ TLSHandshakeTimeout: 10 * time.Second,
+ ExpectContinueTimeout: 1 * time.Second,
+ }
+
+ url := *coreURL
+ if strings.HasPrefix(url, "http:") {
+ url = "https:" + url[5:]
+ }
+
+ return &rpc.Client{
+ BaseURL: url,
+ Client: &http.Client{Transport: t},
+ }
+}
+
+// Wrapper rpc call api.
+func ClientCall(path string, req ...interface{}) (interface{}, int) {
+
+ var response = &blockchain.Response{}
+ var request interface{}
+
+ if req != nil {
+ request = req[0]
+ }
+
+ client := mustRPCClient()
+ client.Call(context.Background(), path, request, response)
+
+ switch response.Status {
+ case blockchain.FAIL:
+ jww.ERROR.Println(response.Msg)
+ return nil, ErrRemote
+ case "":
+ jww.ERROR.Println("Unable to connect to the bytomd")
+ return nil, ErrConnect
+ }
+
+ return response.Data, Success
+}