github.com/turingchain2020/turingchain@v1.1.21/client/rpc_ctx_test.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package client_test
     6  
     7  import (
     8  	"context"
     9  	"encoding/json"
    10  
    11  	"github.com/turingchain2020/turingchain/rpc/jsonclient"
    12  	"github.com/turingchain2020/turingchain/types"
    13  
    14  	"fmt"
    15  
    16  	"github.com/pkg/errors"
    17  	"google.golang.org/grpc"
    18  )
    19  
    20  // TODO: SetPostRunCb()
    21  type JSONRPCCtx struct {
    22  	Addr   string
    23  	Method string
    24  	Params interface{}
    25  	Res    interface{}
    26  
    27  	cb Callback
    28  
    29  	jsonClient *jsonclient.JSONClient
    30  }
    31  
    32  type Callback func(res interface{}) (interface{}, error)
    33  
    34  func NewJSONRPCCtx(methed string, params, res interface{}) *JSONRPCCtx {
    35  	return &JSONRPCCtx{
    36  		Addr:   jrpcsite,
    37  		Method: methed,
    38  		Params: params,
    39  		Res:    res,
    40  	}
    41  }
    42  
    43  func (c *JSONRPCCtx) SetResultCb(cb Callback) {
    44  	c.cb = cb
    45  }
    46  
    47  func (c *JSONRPCCtx) Run() (err error) {
    48  	if c.jsonClient == nil {
    49  		c.jsonClient, err = jsonclient.NewJSONClient(c.Addr)
    50  		if err != nil {
    51  			return
    52  		}
    53  	}
    54  	err = c.jsonClient.Call(c.Method, c.Params, c.Res)
    55  	if err != nil {
    56  		return
    57  	}
    58  	// maybe format rpc result
    59  	var result interface{}
    60  	if c.cb != nil {
    61  		result, err = c.cb(c.Res)
    62  		if err != nil {
    63  			return
    64  		}
    65  	} else {
    66  		result = c.Res
    67  	}
    68  
    69  	_, err = json.MarshalIndent(result, "", "    ")
    70  	if err != nil {
    71  		return
    72  	}
    73  	return
    74  }
    75  
    76  type GrpcCtx struct {
    77  	Method string
    78  	Params interface{}
    79  	Res    interface{}
    80  }
    81  
    82  func NewGRpcCtx(method string, params, res interface{}) *GrpcCtx {
    83  	return &GrpcCtx{
    84  		Method: method,
    85  		Params: params,
    86  		Res:    res,
    87  	}
    88  }
    89  
    90  func (c *GrpcCtx) Run() (err error) {
    91  	conn, errRet := grpc.Dial(grpcsite, grpc.WithInsecure())
    92  	if errRet != nil {
    93  		return errRet
    94  	}
    95  	defer conn.Close()
    96  
    97  	rpc := types.NewTuringchainClient(conn)
    98  	switch c.Method {
    99  	case "GetBlocks":
   100  		reply, err := rpc.GetBlocks(context.Background(), c.Params.(*types.ReqBlocks))
   101  		if err == nil {
   102  			*c.Res.(*types.Reply) = *reply
   103  		}
   104  		errRet = err
   105  	case "GetLastHeader":
   106  		reply, err := rpc.GetLastHeader(context.Background(), c.Params.(*types.ReqNil))
   107  		if err == nil {
   108  			*c.Res.(*types.Header) = *reply
   109  		}
   110  		errRet = err
   111  	case "CreateRawTransaction":
   112  		reply, err := rpc.CreateRawTransaction(context.Background(), c.Params.(*types.CreateTx))
   113  		if err == nil {
   114  			*c.Res.(*types.UnsignTx) = *reply
   115  		}
   116  		errRet = err
   117  	case "QueryTransaction":
   118  		reply, err := rpc.QueryTransaction(context.Background(), c.Params.(*types.ReqHash))
   119  		if err == nil {
   120  			*c.Res.(*types.TransactionDetail) = *reply
   121  		}
   122  		errRet = err
   123  	case "SendTransaction":
   124  		reply, err := rpc.SendTransaction(context.Background(), c.Params.(*types.Transaction))
   125  		if err == nil {
   126  			*c.Res.(*types.Reply) = *reply
   127  		}
   128  	case "GetTransactionByAddr":
   129  		reply, err := rpc.GetTransactionByAddr(context.Background(), c.Params.(*types.ReqAddr))
   130  		if err == nil {
   131  			*c.Res.(*types.ReplyTxInfos) = *reply
   132  		}
   133  		errRet = err
   134  	case "GetTransactionByHashes":
   135  		reply, err := rpc.GetTransactionByHashes(context.Background(), c.Params.(*types.ReqHashes))
   136  		if err == nil {
   137  			*c.Res.(*types.TransactionDetails) = *reply
   138  		}
   139  		errRet = err
   140  	case "GetMemPool":
   141  		reply, err := rpc.GetMemPool(context.Background(), c.Params.(*types.ReqGetMempool))
   142  		if err == nil {
   143  			*c.Res.(*types.ReplyTxList) = *reply
   144  		}
   145  		errRet = err
   146  	case "GetAccounts":
   147  		reply, err := rpc.GetAccounts(context.Background(), c.Params.(*types.ReqNil))
   148  		if err == nil {
   149  			*c.Res.(*types.WalletAccounts) = *reply
   150  		}
   151  		errRet = err
   152  	case "NewAccount":
   153  		reply, err := rpc.NewAccount(context.Background(), c.Params.(*types.ReqNewAccount))
   154  		if err == nil {
   155  			*c.Res.(*types.WalletAccount) = *reply
   156  		}
   157  		errRet = err
   158  	case "WalletTransactionList":
   159  		reply, err := rpc.WalletTransactionList(context.Background(), c.Params.(*types.ReqWalletTransactionList))
   160  		if err == nil {
   161  			*c.Res.(*types.WalletTxDetails) = *reply
   162  		}
   163  		errRet = err
   164  	case "ImportPrivkey":
   165  		reply, err := rpc.ImportPrivkey(context.Background(), c.Params.(*types.ReqWalletImportPrivkey))
   166  		if err == nil {
   167  			*c.Res.(*types.WalletAccount) = *reply
   168  		}
   169  	case "SendToAddress":
   170  		reply, err := rpc.SendToAddress(context.Background(), c.Params.(*types.ReqWalletSendToAddress))
   171  		if err == nil {
   172  			*c.Res.(*types.ReplyHash) = *reply
   173  		}
   174  		errRet = err
   175  	case "SetTxFee":
   176  		reply, err := rpc.SetTxFee(context.Background(), c.Params.(*types.ReqWalletSetFee))
   177  		if err == nil {
   178  			*c.Res.(*types.Reply) = *reply
   179  		}
   180  		errRet = err
   181  	case "SetLabl":
   182  		reply, err := rpc.SetLabl(context.Background(), c.Params.(*types.ReqWalletSetLabel))
   183  		if err == nil {
   184  			*c.Res.(*types.WalletAccount) = *reply
   185  		}
   186  		errRet = err
   187  	case "MergeBalance":
   188  		reply, err := rpc.MergeBalance(context.Background(), c.Params.(*types.ReqWalletMergeBalance))
   189  		if err == nil {
   190  			*c.Res.(*types.ReplyHashes) = *reply
   191  		}
   192  		errRet = err
   193  	case "SetPasswd":
   194  		reply, err := rpc.SetPasswd(context.Background(), c.Params.(*types.ReqWalletSetPasswd))
   195  		if err == nil {
   196  			*c.Res.(*types.Reply) = *reply
   197  		}
   198  	case "Lock":
   199  		reply, err := rpc.Lock(context.Background(), c.Params.(*types.ReqNil))
   200  		if err == nil {
   201  			*c.Res.(*types.Reply) = *reply
   202  		}
   203  		errRet = err
   204  	case "UnLock":
   205  		reply, err := rpc.UnLock(context.Background(), c.Params.(*types.WalletUnLock))
   206  		if err == nil {
   207  			*c.Res.(*types.Reply) = *reply
   208  		}
   209  		errRet = err
   210  	case "GetPeerInfo":
   211  		reply, err := rpc.GetPeerInfo(context.Background(), c.Params.(*types.P2PGetPeerReq))
   212  		if err == nil {
   213  			*c.Res.(*types.PeerList) = *reply
   214  		}
   215  		errRet = err
   216  	case "GetLastMemPool":
   217  		reply, err := rpc.GetLastMemPool(context.Background(), c.Params.(*types.ReqNil))
   218  		if err == nil {
   219  			*c.Res.(*types.ReplyTxList) = *reply
   220  		}
   221  		errRet = err
   222  	case "GetProperFee":
   223  		reply, err := rpc.GetProperFee(context.Background(), c.Params.(*types.ReqProperFee))
   224  		if err == nil {
   225  			*c.Res.(*types.ReplyProperFee) = *reply
   226  		}
   227  		errRet = err
   228  	case "GetWalletStatus":
   229  		reply, err := rpc.GetWalletStatus(context.Background(), c.Params.(*types.ReqNil))
   230  		if err == nil {
   231  			*c.Res.(*types.WalletStatus) = *reply
   232  		}
   233  		errRet = err
   234  	case "GetBlockOverview":
   235  		reply, err := rpc.GetBlockOverview(context.Background(), c.Params.(*types.ReqHash))
   236  		if err == nil {
   237  			*c.Res.(*types.BlockOverview) = *reply
   238  		}
   239  		errRet = err
   240  	case "GetAddrOverview":
   241  		reply, err := rpc.GetAddrOverview(context.Background(), c.Params.(*types.ReqAddr))
   242  		if err == nil {
   243  			*c.Res.(*types.AddrOverview) = *reply
   244  		}
   245  		errRet = err
   246  	case "GetBlockHash":
   247  		reply, err := rpc.GetBlockHash(context.Background(), c.Params.(*types.ReqInt))
   248  		if err == nil {
   249  			*c.Res.(*types.ReplyHash) = *reply
   250  		}
   251  		errRet = err
   252  	case "GenSeed":
   253  		reply, err := rpc.GenSeed(context.Background(), c.Params.(*types.GenSeedLang))
   254  		if err == nil {
   255  			*c.Res.(*types.ReplySeed) = *reply
   256  		}
   257  		errRet = err
   258  	case "GetSeed":
   259  		reply, err := rpc.GetSeed(context.Background(), c.Params.(*types.GetSeedByPw))
   260  		if err == nil {
   261  			*c.Res.(*types.ReplySeed) = *reply
   262  		}
   263  		errRet = err
   264  	case "SaveSeed":
   265  		reply, err := rpc.SaveSeed(context.Background(), c.Params.(*types.SaveSeedByPw))
   266  		if err == nil {
   267  			*c.Res.(*types.Reply) = *reply
   268  		}
   269  		errRet = err
   270  	case "GetBalance":
   271  		reply, err := rpc.GetBalance(context.Background(), c.Params.(*types.ReqBalance))
   272  		if err == nil {
   273  			*c.Res.(*types.Accounts) = *reply
   274  		}
   275  		errRet = err
   276  	case "QueryChain":
   277  		reply, err := rpc.QueryChain(context.Background(), c.Params.(*types.ChainExecutor))
   278  		if err == nil {
   279  			*c.Res.(*types.Reply) = *reply
   280  		}
   281  		errRet = err
   282  	case "GetHexTxByHash":
   283  		reply, err := rpc.GetHexTxByHash(context.Background(), c.Params.(*types.ReqHash))
   284  		if err == nil {
   285  			*c.Res.(*types.HexTx) = *reply
   286  		}
   287  		errRet = err
   288  	case "DumpPrivkey":
   289  		reply, err := rpc.DumpPrivkey(context.Background(), c.Params.(*types.ReqString))
   290  		if err == nil {
   291  			*c.Res.(*types.ReplyString) = *reply
   292  		}
   293  		errRet = err
   294  	case "DumpPrivkeysFile":
   295  		reply, err := rpc.DumpPrivkeysFile(context.Background(), c.Params.(*types.ReqPrivkeysFile))
   296  		if err == nil {
   297  			*c.Res.(*types.Reply) = *reply
   298  		}
   299  		errRet = err
   300  	case "ImportPrivkeysFile":
   301  		reply, err := rpc.ImportPrivkeysFile(context.Background(), c.Params.(*types.ReqPrivkeysFile))
   302  		if err == nil {
   303  			*c.Res.(*types.Reply) = *reply
   304  		}
   305  		errRet = err
   306  	case "Version":
   307  		reply, err := rpc.Version(context.Background(), c.Params.(*types.ReqNil))
   308  		if err == nil {
   309  			*c.Res.(*types.VersionInfo) = *reply
   310  		}
   311  		errRet = err
   312  	case "IsSync":
   313  		reply, err := rpc.IsSync(context.Background(), c.Params.(*types.ReqNil))
   314  		if err == nil {
   315  			*c.Res.(*types.Reply) = *reply
   316  		}
   317  		errRet = err
   318  	case "IsNtpClockSync":
   319  		reply, err := rpc.IsNtpClockSync(context.Background(), c.Params.(*types.ReqNil))
   320  		if err == nil {
   321  			*c.Res.(*types.Reply) = *reply
   322  		}
   323  		errRet = err
   324  	case "NetInfo":
   325  		reply, err := rpc.NetInfo(context.Background(), c.Params.(*types.P2PGetNetInfoReq))
   326  		if err == nil {
   327  			*c.Res.(*types.NodeNetInfo) = *reply
   328  		}
   329  		errRet = err
   330  	case "GetSequenceByHash":
   331  		reply, err := rpc.GetSequenceByHash(context.Background(), c.Params.(*types.ReqHash))
   332  		if err == nil {
   333  			*c.Res.(*types.Int64) = *reply
   334  		}
   335  		errRet = err
   336  	case "GetBlockBySeq":
   337  		reply, err := rpc.GetBlockBySeq(context.Background(), c.Params.(*types.Int64))
   338  		if err == nil {
   339  			*c.Res.(*types.BlockSeq) = *reply
   340  		}
   341  		errRet = err
   342  	case "GetParaTxByTitle":
   343  		reply, err := rpc.GetParaTxByTitle(context.Background(), c.Params.(*types.ReqParaTxByTitle))
   344  		if err == nil {
   345  			*c.Res.(*types.ParaTxDetails) = *reply
   346  		}
   347  		errRet = err
   348  
   349  	case "LoadParaTxByTitle":
   350  		reply, err := rpc.LoadParaTxByTitle(context.Background(), c.Params.(*types.ReqHeightByTitle))
   351  		if err == nil {
   352  			*c.Res.(*types.ReplyHeightByTitle) = *reply
   353  		}
   354  		errRet = err
   355  	case "GetParaTxByHeight":
   356  		reply, err := rpc.GetParaTxByHeight(context.Background(), c.Params.(*types.ReqParaTxByHeight))
   357  		if err == nil {
   358  			*c.Res.(*types.ParaTxDetails) = *reply
   359  		}
   360  		errRet = err
   361  
   362  	default:
   363  		errRet = errors.New(fmt.Sprintf("Unsupport method %v", c.Method))
   364  	}
   365  	return errRet
   366  }