github.com/turingchain2020/turingchain@v1.1.21/rpc/grpchandler.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 rpc 6 7 import ( 8 "time" 9 10 "strings" 11 12 "github.com/turingchain2020/turingchain/common" 13 pb "github.com/turingchain2020/turingchain/types" 14 "golang.org/x/net/context" 15 ) 16 17 // SendTransactionSync send transaction by network and query 18 func (g *Grpc) SendTransactionSync(ctx context.Context, in *pb.Transaction) (*pb.Reply, error) { 19 reply, err := g.cli.SendTx(in) 20 if err != nil { 21 return reply, err 22 } 23 hash := in.Hash() 24 for i := 0; i < 100; i++ { 25 detail, err := g.cli.QueryTx(&pb.ReqHash{Hash: hash}) 26 if err == pb.ErrInvalidParam || err == pb.ErrTypeAsset { 27 return nil, err 28 } 29 if detail != nil { 30 return &pb.Reply{IsOk: true, Msg: hash}, nil 31 } 32 time.Sleep(time.Second / 3) 33 } 34 return nil, pb.ErrTimeout 35 } 36 37 // SendTransaction send transaction by network 38 func (g *Grpc) SendTransaction(ctx context.Context, in *pb.Transaction) (*pb.Reply, error) { 39 return g.cli.SendTx(in) 40 } 41 42 // CreateNoBalanceTxs create multiple transaction with no balance 43 func (g *Grpc) CreateNoBalanceTxs(ctx context.Context, in *pb.NoBalanceTxs) (*pb.ReplySignRawTx, error) { 44 reply, err := g.cli.CreateNoBalanceTxs(in) 45 if err != nil { 46 return nil, err 47 } 48 tx := pb.Encode(reply) 49 return &pb.ReplySignRawTx{TxHex: common.ToHex(tx)}, nil 50 } 51 52 // CreateNoBalanceTransaction create transaction with no balance 53 func (g *Grpc) CreateNoBalanceTransaction(ctx context.Context, in *pb.NoBalanceTx) (*pb.ReplySignRawTx, error) { 54 params := &pb.NoBalanceTxs{ 55 TxHexs: []string{in.GetTxHex()}, 56 PayAddr: in.GetPayAddr(), 57 Privkey: in.GetPrivkey(), 58 Expire: in.GetExpire(), 59 } 60 reply, err := g.cli.CreateNoBalanceTxs(params) 61 if err != nil { 62 return nil, err 63 } 64 tx := pb.Encode(reply) 65 return &pb.ReplySignRawTx{TxHex: common.ToHex(tx)}, nil 66 } 67 68 // CreateRawTransaction create rawtransaction of grpc 69 func (g *Grpc) CreateRawTransaction(ctx context.Context, in *pb.CreateTx) (*pb.UnsignTx, error) { 70 reply, err := g.cli.CreateRawTransaction(in) 71 if err != nil { 72 return nil, err 73 } 74 return &pb.UnsignTx{Data: reply}, nil 75 } 76 77 // ReWriteRawTx re-write raw tx parameters of grpc 78 func (g *Grpc) ReWriteRawTx(ctx context.Context, in *pb.ReWriteRawTx) (*pb.UnsignTx, error) { 79 reply, err := g.cli.ReWriteRawTx(in) 80 if err != nil { 81 return nil, err 82 } 83 return &pb.UnsignTx{Data: reply}, nil 84 } 85 86 // CreateTransaction create transaction of grpc 87 func (g *Grpc) CreateTransaction(ctx context.Context, in *pb.CreateTxIn) (*pb.UnsignTx, error) { 88 pb.AssertConfig(g.cli) 89 cfg := g.cli.GetConfig() 90 execer := cfg.ExecName(string(in.Execer)) 91 exec := pb.LoadExecutorType(execer) 92 if exec == nil { 93 log.Error("callExecNewTx", "Error", "exec not found") 94 return nil, pb.ErrNotSupport 95 } 96 msg, err := exec.GetAction(in.ActionName) 97 if err != nil { 98 return nil, err 99 } 100 //decode protocol buffer 101 err = pb.Decode(in.Payload, msg) 102 if err != nil { 103 return nil, err 104 } 105 reply, err := pb.CallCreateTx(cfg, execer, in.ActionName, msg) 106 if err != nil { 107 return nil, err 108 } 109 return &pb.UnsignTx{Data: reply}, nil 110 } 111 112 // CreateRawTxGroup create rawtransaction for group 113 func (g *Grpc) CreateRawTxGroup(ctx context.Context, in *pb.CreateTransactionGroup) (*pb.UnsignTx, error) { 114 reply, err := g.cli.CreateRawTxGroup(in) 115 if err != nil { 116 return nil, err 117 } 118 return &pb.UnsignTx{Data: reply}, nil 119 } 120 121 // QueryTransaction query transaction by grpc 122 func (g *Grpc) QueryTransaction(ctx context.Context, in *pb.ReqHash) (*pb.TransactionDetail, error) { 123 return g.cli.QueryTx(in) 124 } 125 126 // GetBlocks get blocks by grpc 127 func (g *Grpc) GetBlocks(ctx context.Context, in *pb.ReqBlocks) (*pb.Reply, error) { 128 reply, err := g.cli.GetBlocks(&pb.ReqBlocks{ 129 Start: in.Start, 130 End: in.End, 131 IsDetail: in.IsDetail, 132 }) 133 if err != nil { 134 return nil, err 135 } 136 return &pb.Reply{ 137 IsOk: true, 138 Msg: pb.Encode(reply)}, 139 nil 140 } 141 142 // GetLastHeader get lastheader information 143 func (g *Grpc) GetLastHeader(ctx context.Context, in *pb.ReqNil) (*pb.Header, error) { 144 return g.cli.GetLastHeader() 145 } 146 147 // GetTransactionByAddr get transaction by address 148 func (g *Grpc) GetTransactionByAddr(ctx context.Context, in *pb.ReqAddr) (*pb.ReplyTxInfos, error) { 149 return g.cli.GetTransactionByAddr(in) 150 } 151 152 // GetHexTxByHash get hex transaction by hash 153 func (g *Grpc) GetHexTxByHash(ctx context.Context, in *pb.ReqHash) (*pb.HexTx, error) { 154 reply, err := g.cli.QueryTx(in) 155 if err != nil { 156 return nil, err 157 } 158 tx := reply.GetTx() 159 if tx == nil { 160 return &pb.HexTx{}, nil 161 } 162 return &pb.HexTx{Tx: common.ToHex(pb.Encode(reply.GetTx()))}, nil 163 } 164 165 // GetTransactionByHashes get transaction by hashes 166 func (g *Grpc) GetTransactionByHashes(ctx context.Context, in *pb.ReqHashes) (*pb.TransactionDetails, error) { 167 return g.cli.GetTransactionByHash(in) 168 } 169 170 // GetMemPool get mempool contents 171 func (g *Grpc) GetMemPool(ctx context.Context, in *pb.ReqGetMempool) (*pb.ReplyTxList, error) { 172 return g.cli.GetMempool(in) 173 } 174 175 // GetAccounts get accounts 176 func (g *Grpc) GetAccounts(ctx context.Context, in *pb.ReqNil) (*pb.WalletAccounts, error) { 177 req := &pb.ReqAccountList{WithoutBalance: false} 178 reply, err := g.cli.ExecWalletFunc("wallet", "WalletGetAccountList", req) 179 if err != nil { 180 return nil, err 181 } 182 return reply.(*pb.WalletAccounts), nil 183 } 184 185 // NewAccount produce new account 186 func (g *Grpc) NewAccount(ctx context.Context, in *pb.ReqNewAccount) (*pb.WalletAccount, error) { 187 reply, err := g.cli.ExecWalletFunc("wallet", "NewAccount", in) 188 if err != nil { 189 return nil, err 190 } 191 return reply.(*pb.WalletAccount), nil 192 } 193 194 // WalletTransactionList transaction list of wallet 195 func (g *Grpc) WalletTransactionList(ctx context.Context, in *pb.ReqWalletTransactionList) (*pb.WalletTxDetails, error) { 196 reply, err := g.cli.ExecWalletFunc("wallet", "WalletTransactionList", in) 197 if err != nil { 198 return nil, err 199 } 200 return reply.(*pb.WalletTxDetails), nil 201 } 202 203 // ImportPrivkey import privkey 204 func (g *Grpc) ImportPrivkey(ctx context.Context, in *pb.ReqWalletImportPrivkey) (*pb.WalletAccount, error) { 205 reply, err := g.cli.ExecWalletFunc("wallet", "WalletImportPrivkey", in) 206 if err != nil { 207 return nil, err 208 } 209 return reply.(*pb.WalletAccount), nil 210 } 211 212 // SendToAddress send to address of coins 213 func (g *Grpc) SendToAddress(ctx context.Context, in *pb.ReqWalletSendToAddress) (*pb.ReplyHash, error) { 214 reply, err := g.cli.ExecWalletFunc("wallet", "WalletSendToAddress", in) 215 if err != nil { 216 return nil, err 217 } 218 return reply.(*pb.ReplyHash), nil 219 } 220 221 // SetTxFee set tx fee 222 func (g *Grpc) SetTxFee(ctx context.Context, in *pb.ReqWalletSetFee) (*pb.Reply, error) { 223 reply, err := g.cli.ExecWalletFunc("wallet", "WalletSetFee", in) 224 if err != nil { 225 return nil, err 226 } 227 return reply.(*pb.Reply), nil 228 } 229 230 // SetLabl set labl 231 func (g *Grpc) SetLabl(ctx context.Context, in *pb.ReqWalletSetLabel) (*pb.WalletAccount, error) { 232 reply, err := g.cli.ExecWalletFunc("wallet", "WalletSetLabel", in) 233 if err != nil { 234 return nil, err 235 } 236 return reply.(*pb.WalletAccount), nil 237 } 238 239 // MergeBalance merge balance of wallet 240 func (g *Grpc) MergeBalance(ctx context.Context, in *pb.ReqWalletMergeBalance) (*pb.ReplyHashes, error) { 241 reply, err := g.cli.ExecWalletFunc("wallet", "WalletMergeBalance", in) 242 if err != nil { 243 return nil, err 244 } 245 return reply.(*pb.ReplyHashes), nil 246 } 247 248 // SetPasswd set password 249 func (g *Grpc) SetPasswd(ctx context.Context, in *pb.ReqWalletSetPasswd) (*pb.Reply, error) { 250 reply, err := g.cli.ExecWalletFunc("wallet", "WalletSetPasswd", in) 251 if err != nil { 252 return nil, err 253 } 254 return reply.(*pb.Reply), nil 255 } 256 257 // Lock wallet lock 258 func (g *Grpc) Lock(ctx context.Context, in *pb.ReqNil) (*pb.Reply, error) { 259 reply, err := g.cli.ExecWalletFunc("wallet", "WalletLock", in) 260 if err != nil { 261 return nil, err 262 } 263 return reply.(*pb.Reply), nil 264 } 265 266 // UnLock wallet unlock 267 func (g *Grpc) UnLock(ctx context.Context, in *pb.WalletUnLock) (*pb.Reply, error) { 268 reply, err := g.cli.ExecWalletFunc("wallet", "WalletUnLock", in) 269 if err != nil { 270 return nil, err 271 } 272 return reply.(*pb.Reply), nil 273 } 274 275 // GetPeerInfo get peer information 276 func (g *Grpc) GetPeerInfo(ctx context.Context, req *pb.P2PGetPeerReq) (*pb.PeerList, error) { 277 return g.cli.PeerInfo(req) 278 } 279 280 // GetHeaders return headers 281 func (g *Grpc) GetHeaders(ctx context.Context, in *pb.ReqBlocks) (*pb.Headers, error) { 282 return g.cli.GetHeaders(in) 283 } 284 285 // GetLastMemPool return last mempool contents 286 func (g *Grpc) GetLastMemPool(ctx context.Context, in *pb.ReqNil) (*pb.ReplyTxList, error) { 287 return g.cli.GetLastMempool() 288 } 289 290 // GetProperFee return last mempool proper fee 291 func (g *Grpc) GetProperFee(ctx context.Context, in *pb.ReqProperFee) (*pb.ReplyProperFee, error) { 292 return g.cli.GetProperFee(in) 293 } 294 295 // GetBlockOverview get block overview 296 // GetBlockOverview(parm *types.ReqHash) (*types.BlockOverview, error) //add by hyb 297 func (g *Grpc) GetBlockOverview(ctx context.Context, in *pb.ReqHash) (*pb.BlockOverview, error) { 298 return g.cli.GetBlockOverview(in) 299 } 300 301 // GetAddrOverview get address overview 302 func (g *Grpc) GetAddrOverview(ctx context.Context, in *pb.ReqAddr) (*pb.AddrOverview, error) { 303 return g.cli.GetAddrOverview(in) 304 } 305 306 // GetBlockHash get block hash 307 func (g *Grpc) GetBlockHash(ctx context.Context, in *pb.ReqInt) (*pb.ReplyHash, error) { 308 return g.cli.GetBlockHash(in) 309 } 310 311 // GenSeed seed 312 func (g *Grpc) GenSeed(ctx context.Context, in *pb.GenSeedLang) (*pb.ReplySeed, error) { 313 reply, err := g.cli.ExecWalletFunc("wallet", "GenSeed", in) 314 if err != nil { 315 return nil, err 316 } 317 return reply.(*pb.ReplySeed), nil 318 } 319 320 // GetSeed get seed 321 func (g *Grpc) GetSeed(ctx context.Context, in *pb.GetSeedByPw) (*pb.ReplySeed, error) { 322 reply, err := g.cli.ExecWalletFunc("wallet", "GetSeed", in) 323 if err != nil { 324 return nil, err 325 } 326 return reply.(*pb.ReplySeed), nil 327 } 328 329 // SaveSeed save seed 330 func (g *Grpc) SaveSeed(ctx context.Context, in *pb.SaveSeedByPw) (*pb.Reply, error) { 331 reply, err := g.cli.ExecWalletFunc("wallet", "SaveSeed", in) 332 if err != nil { 333 return nil, err 334 } 335 return reply.(*pb.Reply), nil 336 } 337 338 // GetWalletStatus get wallet status 339 func (g *Grpc) GetWalletStatus(ctx context.Context, in *pb.ReqNil) (*pb.WalletStatus, error) { 340 reply, err := g.cli.ExecWalletFunc("wallet", "GetWalletStatus", in) 341 if err != nil { 342 return nil, err 343 } 344 return reply.(*pb.WalletStatus), nil 345 } 346 347 // GetBalance get balance 348 func (g *Grpc) GetBalance(ctx context.Context, in *pb.ReqBalance) (*pb.Accounts, error) { 349 reply, err := g.cli.GetBalance(in) 350 if err != nil { 351 return nil, err 352 } 353 return &pb.Accounts{Acc: reply}, nil 354 } 355 356 // GetAllExecBalance get balance of exec 357 func (g *Grpc) GetAllExecBalance(ctx context.Context, in *pb.ReqAllExecBalance) (*pb.AllExecBalance, error) { 358 return g.cli.GetAllExecBalance(in) 359 } 360 361 // QueryConsensus query consensus 362 func (g *Grpc) QueryConsensus(ctx context.Context, in *pb.ChainExecutor) (*pb.Reply, error) { 363 msg, err := g.cli.QueryConsensus(in) 364 if err != nil { 365 return nil, err 366 } 367 var reply pb.Reply 368 reply.IsOk = true 369 reply.Msg = pb.Encode(msg) 370 return &reply, nil 371 } 372 373 // QueryChain query chain 374 func (g *Grpc) QueryChain(ctx context.Context, in *pb.ChainExecutor) (*pb.Reply, error) { 375 msg, err := g.cli.QueryChain(in) 376 if err != nil { 377 return nil, err 378 } 379 var reply pb.Reply 380 reply.IsOk = true 381 reply.Msg = pb.Encode(msg) 382 return &reply, nil 383 } 384 385 // ExecWallet exec wallet 386 func (g *Grpc) ExecWallet(ctx context.Context, in *pb.ChainExecutor) (*pb.Reply, error) { 387 msg, err := g.cli.ExecWallet(in) 388 if err != nil { 389 return nil, err 390 } 391 var reply pb.Reply 392 reply.IsOk = true 393 reply.Msg = pb.Encode(msg) 394 return &reply, nil 395 } 396 397 // DumpPrivkey dump Privkey 398 func (g *Grpc) DumpPrivkey(ctx context.Context, in *pb.ReqString) (*pb.ReplyString, error) { 399 reply, err := g.cli.ExecWalletFunc("wallet", "DumpPrivkey", in) 400 if err != nil { 401 return nil, err 402 } 403 return reply.(*pb.ReplyString), nil 404 } 405 406 // DumpPrivkeysFile dumps private key to file. 407 func (g *Grpc) DumpPrivkeysFile(ctx context.Context, in *pb.ReqPrivkeysFile) (*pb.Reply, error) { 408 reply, err := g.cli.ExecWalletFunc("wallet", "DumpPrivkeysFile", in) 409 if err != nil { 410 return nil, err 411 } 412 return reply.(*pb.Reply), nil 413 } 414 415 // ImportPrivkeysFile imports private key from file. 416 func (g *Grpc) ImportPrivkeysFile(ctx context.Context, in *pb.ReqPrivkeysFile) (*pb.Reply, error) { 417 reply, err := g.cli.ExecWalletFunc("wallet", "ImportPrivkeysFile", in) 418 if err != nil { 419 return nil, err 420 } 421 return reply.(*pb.Reply), nil 422 } 423 424 // Version version 425 func (g *Grpc) Version(ctx context.Context, in *pb.ReqNil) (*pb.VersionInfo, error) { 426 427 return g.cli.Version() 428 } 429 430 // IsSync is the sync 431 func (g *Grpc) IsSync(ctx context.Context, in *pb.ReqNil) (*pb.Reply, error) { 432 433 return g.cli.IsSync() 434 } 435 436 // IsNtpClockSync is ntp clock sync 437 func (g *Grpc) IsNtpClockSync(ctx context.Context, in *pb.ReqNil) (*pb.Reply, error) { 438 439 return g.cli.IsNtpClockSync() 440 } 441 442 // NetInfo net information 443 func (g *Grpc) NetInfo(ctx context.Context, in *pb.P2PGetNetInfoReq) (*pb.NodeNetInfo, error) { 444 445 return g.cli.GetNetInfo(in) 446 } 447 448 // GetFatalFailure return fatal of failure 449 func (g *Grpc) GetFatalFailure(ctx context.Context, in *pb.ReqNil) (*pb.Int32, error) { 450 reply, err := g.cli.ExecWalletFunc("wallet", "GetFatalFailure", in) 451 if err != nil { 452 return nil, err 453 } 454 return reply.(*pb.Int32), nil 455 } 456 457 // CloseQueue close queue 458 func (g *Grpc) CloseQueue(ctx context.Context, in *pb.ReqNil) (*pb.Reply, error) { 459 go func() { 460 time.Sleep(time.Millisecond * 100) 461 _, err := g.cli.CloseQueue() 462 if err != nil { 463 log.Error("CloseQueue", "Error", err) 464 } 465 }() 466 467 return &pb.Reply{IsOk: true}, nil 468 } 469 470 // GetLastBlockSequence get last block sequence 471 func (g *Grpc) GetLastBlockSequence(ctx context.Context, in *pb.ReqNil) (*pb.Int64, error) { 472 return g.cli.GetLastBlockSequence() 473 } 474 475 // GetBlockByHashes get block by hashes 476 func (g *Grpc) GetBlockByHashes(ctx context.Context, in *pb.ReqHashes) (*pb.BlockDetails, error) { 477 return g.cli.GetBlockByHashes(in) 478 } 479 480 // GetSequenceByHash get block sequece by hash 481 func (g *Grpc) GetSequenceByHash(ctx context.Context, in *pb.ReqHash) (*pb.Int64, error) { 482 return g.cli.GetSequenceByHash(in) 483 } 484 485 // GetBlockBySeq get block with hash by seq 486 func (g *Grpc) GetBlockBySeq(ctx context.Context, in *pb.Int64) (*pb.BlockSeq, error) { 487 return g.cli.GetBlockBySeq(in) 488 } 489 490 // SignRawTx signature rawtransaction 491 func (g *Grpc) SignRawTx(ctx context.Context, in *pb.ReqSignRawTx) (*pb.ReplySignRawTx, error) { 492 reply, err := g.cli.ExecWalletFunc("wallet", "SignRawTx", in) 493 if err != nil { 494 return nil, err 495 } 496 return reply.(*pb.ReplySignRawTx), nil 497 } 498 499 // QueryRandNum query randHash from ticket 500 func (g *Grpc) QueryRandNum(ctx context.Context, in *pb.ReqRandHash) (*pb.ReplyHash, error) { 501 reply, err := g.cli.Query(in.ExecName, "RandNumHash", in) 502 if err != nil { 503 return nil, err 504 } 505 return reply.(*pb.ReplyHash), nil 506 } 507 508 // GetFork get fork height by fork key 509 func (g *Grpc) GetFork(ctx context.Context, in *pb.ReqKey) (*pb.Int64, error) { 510 pb.AssertConfig(g.cli) 511 cfg := g.cli.GetConfig() 512 keys := strings.Split(string(in.Key), "-") 513 if len(keys) == 2 { 514 return &pb.Int64{Data: cfg.GetDappFork(keys[0], keys[1])}, nil 515 } 516 return &pb.Int64{Data: cfg.GetFork(string(in.Key))}, nil 517 } 518 519 // GetParaTxByTitle 通过seq以及title获取对应平行连的交易 520 func (g *Grpc) GetParaTxByTitle(ctx context.Context, in *pb.ReqParaTxByTitle) (*pb.ParaTxDetails, error) { 521 return g.cli.GetParaTxByTitle(in) 522 } 523 524 // LoadParaTxByTitle //获取拥有此title交易的区块高度 525 func (g *Grpc) LoadParaTxByTitle(ctx context.Context, in *pb.ReqHeightByTitle) (*pb.ReplyHeightByTitle, error) { 526 return g.cli.LoadParaTxByTitle(in) 527 } 528 529 // GetParaTxByHeight //通过区块高度列表+title获取平行链交易 530 func (g *Grpc) GetParaTxByHeight(ctx context.Context, in *pb.ReqParaTxByHeight) (*pb.ParaTxDetails, error) { 531 return g.cli.GetParaTxByHeight(in) 532 } 533 534 //GetAccount 通过地址标签获取账户地址以及账户余额信息 535 func (g *Grpc) GetAccount(ctx context.Context, in *pb.ReqGetAccount) (*pb.WalletAccount, error) { 536 acc, err := g.cli.ExecWalletFunc("wallet", "WalletGetAccount", in) 537 if err != nil { 538 return nil, err 539 } 540 541 return acc.(*pb.WalletAccount), nil 542 } 543 544 // GetServerTime get server time 545 func (g *Grpc) GetServerTime(ctx context.Context, in *pb.ReqNil) (*pb.ServerTime, error) { 546 serverTime := &pb.ServerTime{ 547 CurrentTimestamp: pb.Now().Unix(), 548 } 549 return serverTime, nil 550 }