github.com/turingchain2020/turingchain@v1.1.21/client/queueprotocol.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 系统接口客户端: 封装 Queue Event 6 package client 7 8 import ( 9 "fmt" 10 "time" 11 12 "github.com/turingchain2020/turingchain/common/log/log15" 13 14 "github.com/turingchain2020/turingchain/common/version" 15 "github.com/turingchain2020/turingchain/queue" 16 "github.com/turingchain2020/turingchain/types" 17 ) 18 19 const ( 20 mempoolKey = "mempool" // 未打包交易池 21 p2pKey = "p2p" // 22 //rpcKey = "rpc" 23 consensusKey = "consensus" // 共识系统 24 //accountKey = "accout" // 账号系统 25 executorKey = "execs" // 交易执行器 26 walletKey = "wallet" // 钱包 27 blockchainKey = "blockchain" // 区块 28 storeKey = "store" 29 ) 30 31 var log = log15.New("module", "client") 32 33 // QueueProtocolOption queue protocol option 34 type QueueProtocolOption struct { 35 // 发送请求超时时间 36 SendTimeout time.Duration 37 // 接收应答超时时间 38 WaitTimeout time.Duration 39 } 40 41 // QueueProtocol 消息通道协议实现 42 type QueueProtocol struct { 43 // 消息队列 44 client queue.Client 45 option QueueProtocolOption 46 } 47 48 // New New QueueProtocolAPI interface 49 func New(client queue.Client, option *QueueProtocolOption) (QueueProtocolAPI, error) { 50 if client == nil { 51 return nil, types.ErrInvalidParam 52 } 53 q := &QueueProtocol{} 54 q.client = client 55 if option != nil { 56 q.option = *option 57 } else { 58 q.option.SendTimeout = time.Duration(-1) 59 q.option.WaitTimeout = time.Duration(-1) 60 } 61 return q, nil 62 } 63 64 func (q *QueueProtocol) send(topic string, ty int64, data interface{}) (*queue.Message, error) { 65 client := q.client 66 msg := client.NewMessage(topic, ty, data) 67 err := client.SendTimeout(msg, true, q.option.SendTimeout) 68 if err != nil { 69 return &queue.Message{}, err 70 } 71 reply, err := client.WaitTimeout(msg, q.option.WaitTimeout) 72 if err != nil { 73 return nil, err 74 } 75 //NOTE:内部错误情况较多,只对正确流程msg回收 76 client.FreeMessage(msg) 77 return reply, nil 78 } 79 80 func (q *QueueProtocol) notify(topic string, ty int64, data interface{}) (*queue.Message, error) { 81 client := q.client 82 msg := client.NewMessage(topic, ty, data) 83 err := client.SendTimeout(msg, false, q.option.SendTimeout) 84 if err != nil { 85 return &queue.Message{}, err 86 } 87 return msg, err 88 } 89 90 // Notify new and send client message 91 func (q *QueueProtocol) Notify(topic string, ty int64, data interface{}) (*queue.Message, error) { 92 return q.notify(topic, ty, data) 93 } 94 95 // Close close client 96 func (q *QueueProtocol) Close() { 97 q.client.Close() 98 } 99 100 // NewMessage new message 101 func (q *QueueProtocol) NewMessage(topic string, msgid int64, data interface{}) *queue.Message { 102 return q.client.NewMessage(topic, msgid, data) 103 } 104 105 func (q *QueueProtocol) setOption(option *QueueProtocolOption) { 106 if option != nil { 107 q.option = *option 108 } 109 } 110 111 // SendTx send transaction to mempool 112 func (q *QueueProtocol) SendTx(param *types.Transaction) (*types.Reply, error) { 113 if param == nil { 114 err := types.ErrInvalidParam 115 log.Error("SendTx", "Error", err) 116 return nil, err 117 } 118 msg, err := q.send(mempoolKey, types.EventTx, param) 119 if err != nil { 120 log.Error("SendTx", "Error", err.Error()) 121 return nil, err 122 } 123 reply, ok := msg.GetData().(*types.Reply) 124 if ok { 125 if reply.GetIsOk() { 126 reply.Msg = param.Hash() 127 } else { 128 msg := string(reply.Msg) 129 err = fmt.Errorf(msg) 130 reply = nil 131 } 132 } else { 133 err = types.ErrTypeAsset 134 } 135 q.client.FreeMessage(msg) 136 return reply, err 137 } 138 139 // GetTxList get transactions from mempool 140 func (q *QueueProtocol) GetTxList(param *types.TxHashList) (*types.ReplyTxList, error) { 141 if param == nil { 142 err := types.ErrInvalidParam 143 log.Error("GetTxList", "Error", err) 144 return nil, err 145 } 146 msg, err := q.send(mempoolKey, types.EventTxList, param) 147 if err != nil { 148 log.Error("GetTxList", "Error", err.Error()) 149 return nil, err 150 } 151 if reply, ok := msg.GetData().(*types.ReplyTxList); ok { 152 return reply, nil 153 } 154 return nil, types.ErrTypeAsset 155 } 156 157 // GetBlocks get block detail from blockchain 158 func (q *QueueProtocol) GetBlocks(param *types.ReqBlocks) (*types.BlockDetails, error) { 159 if param == nil { 160 err := types.ErrInvalidParam 161 log.Error("GetBlocks", "Error", err) 162 return nil, err 163 } 164 msg, err := q.send(blockchainKey, types.EventGetBlocks, param) 165 if err != nil { 166 log.Error("GetBlocks", "Error", err.Error()) 167 return nil, err 168 } 169 if reply, ok := msg.GetData().(*types.BlockDetails); ok { 170 return reply, nil 171 } 172 err = types.ErrTypeAsset 173 log.Error("GetBlocks", "Error", err.Error()) 174 return nil, err 175 } 176 177 // QueryTx query transaction detail by transaction hash from blockchain 178 func (q *QueueProtocol) QueryTx(param *types.ReqHash) (*types.TransactionDetail, error) { 179 if param == nil { 180 err := types.ErrInvalidParam 181 log.Debug("QueryTx", "Error", err) 182 return nil, err 183 } 184 msg, err := q.send(blockchainKey, types.EventQueryTx, param) 185 if err != nil { 186 log.Debug("QueryTx", "Error", err.Error()) 187 return nil, err 188 } 189 if reply, ok := msg.GetData().(*types.TransactionDetail); ok { 190 return reply, nil 191 } 192 return nil, types.ErrTypeAsset 193 } 194 195 // GetTransactionByAddr get transaction by address 196 func (q *QueueProtocol) GetTransactionByAddr(param *types.ReqAddr) (*types.ReplyTxInfos, error) { 197 if param == nil { 198 err := types.ErrInvalidParam 199 log.Error("GetTransactionByAddr", "Error", err) 200 return nil, err 201 } 202 msg, err := q.send(blockchainKey, types.EventGetTransactionByAddr, param) 203 if err != nil { 204 log.Error("GetTransactionByAddr", "Error", err.Error()) 205 return nil, err 206 } 207 if reply, ok := msg.GetData().(*types.ReplyTxInfos); ok { 208 return reply, nil 209 } 210 err = types.ErrTypeAsset 211 log.Error("GetTransactionByAddr", "Error", err) 212 return nil, types.ErrTypeAsset 213 } 214 215 // GetTransactionByHash get transactions by hash from blockchain 216 func (q *QueueProtocol) GetTransactionByHash(param *types.ReqHashes) (*types.TransactionDetails, error) { 217 if param == nil { 218 err := types.ErrInvalidParam 219 log.Error("GetTransactionByHash", "Error", err) 220 return nil, err 221 } 222 msg, err := q.send(blockchainKey, types.EventGetTransactionByHash, param) 223 if err != nil { 224 log.Error("GetTransactionByHash", "Error", err.Error()) 225 return nil, err 226 } 227 if reply, ok := msg.GetData().(*types.TransactionDetails); ok { 228 return reply, nil 229 } 230 return nil, types.ErrTypeAsset 231 } 232 233 // GetMempool get transactions from mempool 234 func (q *QueueProtocol) GetMempool(req *types.ReqGetMempool) (*types.ReplyTxList, error) { 235 msg, err := q.send(mempoolKey, types.EventGetMempool, req) 236 if err != nil { 237 log.Error("GetMempool", "Error", err.Error()) 238 return nil, err 239 } 240 if reply, ok := msg.GetData().(*types.ReplyTxList); ok { 241 return reply, nil 242 } 243 return nil, types.ErrTypeAsset 244 } 245 246 // PeerInfo query peer list 247 func (q *QueueProtocol) PeerInfo(req *types.P2PGetPeerReq) (*types.PeerList, error) { 248 msg, err := q.send(p2pKey, types.EventPeerInfo, req) 249 if err != nil { 250 log.Error("PeerInfo", "Error", err.Error()) 251 return nil, err 252 } 253 if reply, ok := msg.GetData().(*types.PeerList); ok { 254 return reply, nil 255 } 256 return nil, types.ErrTypeAsset 257 } 258 259 //NetProtocols protocols list 260 func (q *QueueProtocol) NetProtocols(req *types.ReqNil) (*types.NetProtocolInfos, error) { 261 msg, err := q.send(p2pKey, types.EventNetProtocols, req) 262 if err != nil { 263 log.Error("PeerInfo", "Error", err.Error()) 264 return nil, err 265 } 266 if reply, ok := msg.GetData().(*types.NetProtocolInfos); ok { 267 return reply, nil 268 } 269 return nil, types.ErrTypeAsset 270 } 271 272 // GetHeaders get block headers by height 273 func (q *QueueProtocol) GetHeaders(param *types.ReqBlocks) (*types.Headers, error) { 274 if param == nil { 275 err := types.ErrInvalidParam 276 log.Error("GetHeaders", "Error", err) 277 return nil, err 278 } 279 msg, err := q.send(blockchainKey, types.EventGetHeaders, param) 280 if err != nil { 281 log.Error("GetHeaders", "Error", err.Error()) 282 return nil, err 283 } 284 if reply, ok := msg.GetData().(*types.Headers); ok { 285 return reply, nil 286 } 287 return nil, types.ErrTypeAsset 288 } 289 290 // GetLastMempool get transactions from last mempool 291 func (q *QueueProtocol) GetLastMempool() (*types.ReplyTxList, error) { 292 msg, err := q.send(mempoolKey, types.EventGetLastMempool, &types.ReqNil{}) 293 if err != nil { 294 log.Error("GetLastMempool", "Error", err.Error()) 295 return nil, err 296 } 297 if reply, ok := msg.GetData().(*types.ReplyTxList); ok { 298 return reply, nil 299 } 300 return nil, types.ErrTypeAsset 301 } 302 303 // GetProperFee get proper fee from mempool 304 func (q *QueueProtocol) GetProperFee(req *types.ReqProperFee) (*types.ReplyProperFee, error) { 305 msg, err := q.send(mempoolKey, types.EventGetProperFee, req) 306 if err != nil { 307 log.Error("GetProperFee", "Error", err.Error()) 308 return nil, err 309 } 310 if reply, ok := msg.GetData().(*types.ReplyProperFee); ok { 311 return reply, nil 312 } 313 return nil, types.ErrTypeAsset 314 } 315 316 // GetBlockOverview get block head detil by hash 317 func (q *QueueProtocol) GetBlockOverview(param *types.ReqHash) (*types.BlockOverview, error) { 318 if param == nil { 319 err := types.ErrInvalidParam 320 log.Error("GetBlockOverview", "Error", err) 321 return nil, err 322 } 323 msg, err := q.send(blockchainKey, types.EventGetBlockOverview, param) 324 if err != nil { 325 log.Error("GetBlockOverview", "Error", err.Error()) 326 return nil, err 327 } 328 if reply, ok := msg.GetData().(*types.BlockOverview); ok { 329 return reply, nil 330 } 331 return nil, types.ErrTypeAsset 332 } 333 334 // GetAddrOverview get block head detil by address 335 func (q *QueueProtocol) GetAddrOverview(param *types.ReqAddr) (*types.AddrOverview, error) { 336 if param == nil { 337 err := types.ErrInvalidParam 338 log.Error("GetAddrOverview", "Error", err) 339 return nil, err 340 } 341 msg, err := q.send(blockchainKey, types.EventGetAddrOverview, param) 342 if err != nil { 343 log.Error("GetAddrOverview", "Error", err.Error()) 344 return nil, err 345 } 346 if reply, ok := msg.GetData().(*types.AddrOverview); ok { 347 return reply, nil 348 } 349 return nil, types.ErrTypeAsset 350 } 351 352 // GetBlockHash get blockHash by height 353 func (q *QueueProtocol) GetBlockHash(param *types.ReqInt) (*types.ReplyHash, error) { 354 if param == nil { 355 err := types.ErrInvalidParam 356 log.Error("GetBlockHash", "Error", err) 357 return nil, err 358 } 359 msg, err := q.send(blockchainKey, types.EventGetBlockHash, param) 360 if err != nil { 361 log.Error("GetBlockHash", "Error", err.Error()) 362 return nil, err 363 } 364 if reply, ok := msg.GetData().(*types.ReplyHash); ok { 365 return reply, nil 366 } 367 return nil, types.ErrTypeAsset 368 } 369 370 // Query the query interface 371 func (q *QueueProtocol) Query(driver, funcname string, param types.Message) (types.Message, error) { 372 if types.IsNilP(param) { 373 err := types.ErrInvalidParam 374 log.Error("Query", "Error", err) 375 return nil, err 376 } 377 query := &types.ChainExecutor{Driver: driver, FuncName: funcname, Param: types.Encode(param)} 378 return q.QueryChain(query) 379 } 380 381 // QueryConsensus query consensus data 382 func (q *QueueProtocol) QueryConsensus(param *types.ChainExecutor) (types.Message, error) { 383 if param == nil { 384 err := types.ErrInvalidParam 385 log.Error("ExecWallet", "Error", err) 386 return nil, err 387 } 388 msg, err := q.send(consensusKey, types.EventConsensusQuery, param) 389 if err != nil { 390 log.Error("query QueryConsensus", "Error", err.Error()) 391 return nil, err 392 } 393 if reply, ok := msg.GetData().(types.Message); ok { 394 return reply, nil 395 } 396 return nil, types.ErrTypeAsset 397 } 398 399 // ExecWalletFunc exec wallet function interface 400 func (q *QueueProtocol) ExecWalletFunc(driver string, funcname string, param types.Message) (types.Message, error) { 401 if types.IsNilP(param) { 402 err := types.ErrInvalidParam 403 log.Error("ExecWalletFunc", "Error", err) 404 return nil, err 405 } 406 query := &types.ChainExecutor{Driver: driver, FuncName: funcname, Param: types.Encode(param)} 407 return q.ExecWallet(query) 408 } 409 410 // QueryConsensusFunc query consensus function 411 func (q *QueueProtocol) QueryConsensusFunc(driver string, funcname string, param types.Message) (types.Message, error) { 412 if types.IsNilP(param) { 413 err := types.ErrInvalidParam 414 log.Error("QueryConsensusFunc", "Error", err) 415 return nil, err 416 } 417 query := &types.ChainExecutor{Driver: driver, FuncName: funcname, Param: types.Encode(param)} 418 return q.QueryConsensus(query) 419 } 420 421 // ExecWallet exec wallet function 422 func (q *QueueProtocol) ExecWallet(param *types.ChainExecutor) (types.Message, error) { 423 if param == nil { 424 err := types.ErrInvalidParam 425 log.Error("ExecWallet", "Error", err) 426 return nil, err 427 } 428 msg, err := q.send(walletKey, types.EventWalletExecutor, param) 429 if err != nil { 430 log.Error("ExecWallet", "Error", err.Error()) 431 return nil, err 432 } 433 if reply, ok := msg.GetData().(types.Message); ok { 434 return reply, nil 435 } 436 return nil, types.ErrTypeAsset 437 } 438 439 // IsSync query the blockchain sync state 440 func (q *QueueProtocol) IsSync() (*types.Reply, error) { 441 msg, err := q.send(blockchainKey, types.EventIsSync, &types.ReqNil{}) 442 if err != nil { 443 log.Error("IsSync", "Error", err.Error()) 444 return nil, err 445 } 446 if reply, ok := msg.GetData().(*types.IsCaughtUp); ok { 447 return &types.Reply{IsOk: reply.Iscaughtup}, nil 448 } 449 err = types.ErrTypeAsset 450 log.Error("IsSync", "Error", err.Error()) 451 return nil, err 452 } 453 454 // IsNtpClockSync query the ntp clock sync state 455 func (q *QueueProtocol) IsNtpClockSync() (*types.Reply, error) { 456 msg, err := q.send(blockchainKey, types.EventIsNtpClockSync, &types.ReqNil{}) 457 if err != nil { 458 log.Error("IsNtpClockSync", "Error", err.Error()) 459 return nil, err 460 } 461 if reply, ok := msg.GetData().(*types.IsNtpClockSync); ok { 462 return &types.Reply{IsOk: reply.GetIsntpclocksync()}, nil 463 } 464 err = types.ErrTypeAsset 465 466 log.Error("IsNtpClockSync", "Error", err.Error()) 467 return nil, err 468 } 469 470 // LocalGet get value from local db by key 471 func (q *QueueProtocol) LocalGet(param *types.LocalDBGet) (*types.LocalReplyValue, error) { 472 if param == nil { 473 err := types.ErrInvalidParam 474 log.Error("LocalGet", "Error", err) 475 return nil, err 476 } 477 478 msg, err := q.send(blockchainKey, types.EventLocalGet, param) 479 if err != nil { 480 log.Error("LocalGet", "Error", err.Error()) 481 return nil, err 482 } 483 if reply, ok := msg.GetData().(*types.LocalReplyValue); ok { 484 return reply, nil 485 } 486 return nil, types.ErrTypeAsset 487 } 488 489 // LocalSet set key value in local db 490 func (q *QueueProtocol) LocalSet(param *types.LocalDBSet) error { 491 if param == nil { 492 err := types.ErrInvalidParam 493 log.Error("LocalSet", "Error", err) 494 return err 495 } 496 _, err := q.send(blockchainKey, types.EventLocalSet, param) 497 if err != nil { 498 log.Error("LocalSet", "Error", err.Error()) 499 return err 500 } 501 return nil 502 } 503 504 //LocalNew new a localdb object 505 func (q *QueueProtocol) LocalNew(readOnly bool) (*types.Int64, error) { 506 msg, err := q.send(blockchainKey, types.EventLocalNew, readOnly) 507 if err != nil { 508 log.Error("LocalNew", "Error", err.Error()) 509 return nil, err 510 } 511 if reply, ok := msg.GetData().(*types.Int64); ok { 512 return reply, nil 513 } 514 return nil, types.ErrTypeAsset 515 } 516 517 //LocalBegin begin a transaction 518 func (q *QueueProtocol) LocalBegin(param *types.Int64) error { 519 _, err := q.send(blockchainKey, types.EventLocalBegin, param) 520 if err != nil { 521 log.Error("LocalBegin", "Error", err.Error()) 522 return err 523 } 524 return nil 525 } 526 527 //LocalClose begin a transaction 528 func (q *QueueProtocol) LocalClose(param *types.Int64) error { 529 _, err := q.send(blockchainKey, types.EventLocalClose, param) 530 if err != nil { 531 log.Error("LocalClose", "Error", err.Error()) 532 return err 533 } 534 return nil 535 } 536 537 //LocalCommit commit a transaction 538 func (q *QueueProtocol) LocalCommit(param *types.Int64) error { 539 if param == nil { 540 err := types.ErrInvalidParam 541 log.Error("LocalCommit", "Error", err) 542 return err 543 } 544 _, err := q.send(blockchainKey, types.EventLocalCommit, param) 545 if err != nil { 546 log.Error("LocalCommit", "Error", err.Error()) 547 return err 548 } 549 return nil 550 } 551 552 //LocalRollback rollback a transaction 553 func (q *QueueProtocol) LocalRollback(param *types.Int64) error { 554 if param == nil { 555 err := types.ErrInvalidParam 556 log.Error("LocalRollback", "Error", err) 557 return err 558 } 559 _, err := q.send(blockchainKey, types.EventLocalRollback, param) 560 if err != nil { 561 log.Error("LocalRollback", "Error", err.Error()) 562 return err 563 } 564 return nil 565 } 566 567 // LocalList get value list from local db by key list 568 func (q *QueueProtocol) LocalList(param *types.LocalDBList) (*types.LocalReplyValue, error) { 569 if param == nil { 570 err := types.ErrInvalidParam 571 log.Error("LocalList", "Error", err) 572 return nil, err 573 } 574 msg, err := q.send(blockchainKey, types.EventLocalList, param) 575 if err != nil { 576 log.Error("LocalList", "Error", err.Error()) 577 return nil, err 578 } 579 if reply, ok := msg.GetData().(*types.LocalReplyValue); ok { 580 return reply, nil 581 } 582 return nil, types.ErrTypeAsset 583 } 584 585 // GetLastHeader get the current head detail 586 func (q *QueueProtocol) GetLastHeader() (*types.Header, error) { 587 msg, err := q.send(blockchainKey, types.EventGetLastHeader, &types.ReqNil{}) 588 if err != nil { 589 log.Error("GetLastHeader", "Error", err.Error()) 590 return nil, err 591 } 592 if reply, ok := msg.GetData().(*types.Header); ok { 593 return reply, nil 594 } 595 err = types.ErrTypeAsset 596 log.Error("GetLastHeader", "Error", err.Error()) 597 return nil, err 598 } 599 600 // Version get the software version 601 func (q *QueueProtocol) Version() (*types.VersionInfo, error) { 602 types.AssertConfig(q.client) 603 return &types.VersionInfo{ 604 Title: q.client.GetConfig().GetTitle(), 605 App: version.GetAppVersion(), 606 Turingchain: version.GetVersion(), 607 LocalDb: version.GetLocalDBVersion(), 608 ChainID: q.client.GetConfig().GetChainID(), 609 }, nil 610 } 611 612 // GetNetInfo get the net information 613 func (q *QueueProtocol) GetNetInfo(req *types.P2PGetNetInfoReq) (*types.NodeNetInfo, error) { 614 msg, err := q.send(p2pKey, types.EventGetNetInfo, req) 615 if err != nil { 616 log.Error("GetNetInfo", "Error", err.Error()) 617 return nil, err 618 } 619 if reply, ok := msg.GetData().(*types.NodeNetInfo); ok { 620 return reply, nil 621 } 622 err = types.ErrTypeAsset 623 log.Error("GetNetInfo", "Error", err.Error()) 624 return nil, err 625 } 626 627 // StoreSet set value by statehash and key to statedb 628 func (q *QueueProtocol) StoreSet(param *types.StoreSetWithSync) (*types.ReplyHash, error) { 629 if param == nil { 630 err := types.ErrInvalidParam 631 log.Error("StoreSet", "Error", err) 632 return nil, err 633 } 634 635 msg, err := q.send(storeKey, types.EventStoreSet, param) 636 if err != nil { 637 log.Error("StoreSet", "Error", err.Error()) 638 return nil, err 639 } 640 if reply, ok := msg.GetData().(*types.ReplyHash); ok { 641 return reply, nil 642 } 643 err = types.ErrTypeAsset 644 log.Error("StoreSet", "Error", err.Error()) 645 return nil, err 646 } 647 648 // StoreGet get value by statehash and key from statedb 649 func (q *QueueProtocol) StoreGet(param *types.StoreGet) (*types.StoreReplyValue, error) { 650 if param == nil { 651 err := types.ErrInvalidParam 652 log.Error("StoreGet", "Error", err) 653 return nil, err 654 } 655 656 msg, err := q.send(storeKey, types.EventStoreGet, param) 657 if err != nil { 658 log.Error("StoreGet", "Error", err.Error()) 659 return nil, err 660 } 661 if reply, ok := msg.GetData().(*types.StoreReplyValue); ok { 662 return reply, nil 663 } 664 err = types.ErrTypeAsset 665 log.Error("StoreGet", "Error", err.Error()) 666 return nil, err 667 } 668 669 // StoreMemSet Memset kvs by statehash to statedb 670 func (q *QueueProtocol) StoreMemSet(param *types.StoreSetWithSync) (*types.ReplyHash, error) { 671 if param == nil { 672 err := types.ErrInvalidParam 673 log.Error("StoreMemSet", "Error", err) 674 return nil, err 675 } 676 677 msg, err := q.send(storeKey, types.EventStoreMemSet, param) 678 if err != nil { 679 log.Error("StoreMemSet", "Error", err.Error()) 680 return nil, err 681 } 682 if reply, ok := msg.GetData().(*types.ReplyHash); ok { 683 return reply, nil 684 } 685 err = types.ErrTypeAsset 686 log.Error("StoreMemSet", "Error", err.Error()) 687 return nil, err 688 } 689 690 // StoreCommit commit kvs by statehash to statedb 691 func (q *QueueProtocol) StoreCommit(param *types.ReqHash) (*types.ReplyHash, error) { 692 if param == nil { 693 err := types.ErrInvalidParam 694 log.Error("StoreCommit", "Error", err) 695 return nil, err 696 } 697 698 msg, err := q.send(storeKey, types.EventStoreCommit, param) 699 if err != nil { 700 log.Error("StoreCommit", "Error", err.Error()) 701 return nil, err 702 } 703 if reply, ok := msg.GetData().(*types.ReplyHash); ok { 704 return reply, nil 705 } 706 err = types.ErrTypeAsset 707 log.Error("StoreCommit", "Error", err.Error()) 708 return nil, err 709 } 710 711 // StoreRollback rollback kvs by statehash to statedb 712 func (q *QueueProtocol) StoreRollback(param *types.ReqHash) (*types.ReplyHash, error) { 713 if param == nil { 714 err := types.ErrInvalidParam 715 log.Error("StoreRollback", "Error", err) 716 return nil, err 717 } 718 719 msg, err := q.send(storeKey, types.EventStoreRollback, param) 720 if err != nil { 721 log.Error("StoreRollback", "Error", err.Error()) 722 return nil, err 723 } 724 if reply, ok := msg.GetData().(*types.ReplyHash); ok { 725 return reply, nil 726 } 727 err = types.ErrTypeAsset 728 log.Error("StoreRollback", "Error", err.Error()) 729 return nil, err 730 } 731 732 // StoreDel del kvs by statehash to statedb 733 func (q *QueueProtocol) StoreDel(param *types.StoreDel) (*types.ReplyHash, error) { 734 if param == nil { 735 err := types.ErrInvalidParam 736 log.Error("StoreDel", "Error", err) 737 return nil, err 738 } 739 740 msg, err := q.send(storeKey, types.EventStoreDel, param) 741 if err != nil { 742 log.Error("StoreDel", "Error", err.Error()) 743 return nil, err 744 } 745 if reply, ok := msg.GetData().(*types.ReplyHash); ok { 746 return reply, nil 747 } 748 err = types.ErrTypeAsset 749 log.Error("StoreDel", "Error", err.Error()) 750 return nil, err 751 } 752 753 //StoreList query list from statedb 754 func (q *QueueProtocol) StoreList(param *types.StoreList) (*types.StoreListReply, error) { 755 if param == nil { 756 err := types.ErrInvalidParam 757 log.Error("StoreList", "Error", err) 758 return nil, err 759 } 760 761 msg, err := q.send(storeKey, types.EventStoreList, param) 762 if err != nil { 763 log.Error("StoreList", "Error", err.Error()) 764 return nil, err 765 } 766 if reply, ok := msg.GetData().(*types.StoreListReply); ok { 767 return reply, nil 768 } 769 return nil, types.ErrTypeAsset 770 } 771 772 // StoreGetTotalCoins get total coins from statedb 773 func (q *QueueProtocol) StoreGetTotalCoins(param *types.IterateRangeByStateHash) (*types.ReplyGetTotalCoins, error) { 774 if param == nil { 775 err := types.ErrInvalidParam 776 log.Error("StoreGetTotalCoins", "Error", err) 777 return nil, err 778 } 779 msg, err := q.send(storeKey, types.EventStoreGetTotalCoins, param) 780 if err != nil { 781 log.Error("StoreGetTotalCoins", "Error", err.Error()) 782 return nil, err 783 } 784 if reply, ok := msg.GetData().(*types.ReplyGetTotalCoins); ok { 785 return reply, nil 786 } 787 err = types.ErrTypeAsset 788 log.Error("StoreGetTotalCoins", "Error", err.Error()) 789 return nil, err 790 } 791 792 // CloseQueue close client queue 793 func (q *QueueProtocol) CloseQueue() (*types.Reply, error) { 794 return q.client.CloseQueue() 795 } 796 797 // GetLastBlockSequence 获取最新的block执行序列号 798 func (q *QueueProtocol) GetLastBlockSequence() (*types.Int64, error) { 799 msg, err := q.send(blockchainKey, types.EventGetLastBlockSequence, &types.ReqNil{}) 800 if err != nil { 801 log.Error("GetLastBlockSequence", "Error", err.Error()) 802 return nil, err 803 } 804 if reply, ok := msg.GetData().(*types.Int64); ok { 805 806 return reply, nil 807 } 808 return nil, types.ErrTypeAsset 809 } 810 811 // GetSequenceByHash 通过hash获取对应的执行序列号 812 func (q *QueueProtocol) GetSequenceByHash(param *types.ReqHash) (*types.Int64, error) { 813 if param == nil { 814 err := types.ErrInvalidParam 815 log.Error("GetSequenceByHash", "Error", err) 816 return nil, err 817 } 818 msg, err := q.send(blockchainKey, types.EventGetSeqByHash, param) 819 if err != nil { 820 log.Error("GetSequenceByHash", "Error", err.Error()) 821 return nil, err 822 } 823 if reply, ok := msg.GetData().(*types.Int64); ok { 824 825 return reply, nil 826 } 827 return nil, types.ErrTypeAsset 828 } 829 830 // GetBlockByHashes get block detail list by hash list 831 func (q *QueueProtocol) GetBlockByHashes(param *types.ReqHashes) (*types.BlockDetails, error) { 832 if param == nil { 833 err := types.ErrInvalidParam 834 log.Error("GetBlockByHashes", "Error", err) 835 return nil, err 836 } 837 msg, err := q.send(blockchainKey, types.EventGetBlockByHashes, param) 838 if err != nil { 839 log.Error("GetBlockByHashes", "Error", err.Error()) 840 return nil, err 841 } 842 if reply, ok := msg.GetData().(*types.BlockDetails); ok { 843 return reply, nil 844 } 845 err = types.ErrTypeAsset 846 log.Error("GetBlockByHashes", "Error", err.Error()) 847 return nil, err 848 } 849 850 // GetBlockBySeq get block detail and hash by seq 851 func (q *QueueProtocol) GetBlockBySeq(param *types.Int64) (*types.BlockSeq, error) { 852 if param == nil { 853 err := types.ErrInvalidParam 854 log.Error("GetBlockBySeq", "Error", err) 855 return nil, err 856 } 857 msg, err := q.send(blockchainKey, types.EventGetBlockBySeq, param) 858 if err != nil { 859 log.Error("GetBlockBySeq", "Error", err.Error()) 860 return nil, err 861 } 862 if reply, ok := msg.GetData().(*types.BlockSeq); ok { 863 return reply, nil 864 } 865 return nil, types.ErrTypeAsset 866 } 867 868 // GetBlockSequences block执行序列号 869 func (q *QueueProtocol) GetBlockSequences(param *types.ReqBlocks) (*types.BlockSequences, error) { 870 if param == nil { 871 err := types.ErrInvalidParam 872 log.Error("GetBlockSequences", "Error", err) 873 return nil, err 874 } 875 msg, err := q.send(blockchainKey, types.EventGetBlockSequences, param) 876 if err != nil { 877 log.Error("GetBlockSequences", "Error", err.Error()) 878 return nil, err 879 } 880 if reply, ok := msg.GetData().(*types.BlockSequences); ok { 881 return reply, nil 882 } 883 err = types.ErrTypeAsset 884 log.Error("GetBlockSequences", "Error", err) 885 return nil, err 886 } 887 888 // QueryChain query chain 889 func (q *QueueProtocol) QueryChain(param *types.ChainExecutor) (types.Message, error) { 890 if param == nil { 891 err := types.ErrInvalidParam 892 log.Error("QueryChain", "Error", err) 893 return nil, err 894 } 895 msg, err := q.send(executorKey, types.EventBlockChainQuery, param) 896 if err != nil { 897 log.Error("QueryChain", "Error", err, "driver", param.Driver, "func", param.FuncName) 898 return nil, err 899 } 900 if reply, ok := msg.GetData().(types.Message); ok { 901 return reply, nil 902 } 903 err = types.ErrTypeAsset 904 log.Error("QueryChain", "Error", err) 905 return nil, err 906 } 907 908 // AddPushSubscribe Add Seq CallBack 909 func (q *QueueProtocol) AddPushSubscribe(param *types.PushSubscribeReq) (*types.ReplySubscribePush, error) { 910 msg, err := q.send(blockchainKey, types.EventSubscribePush, param) 911 if err != nil { 912 log.Error("AddPushSubscribe", "Error", err.Error()) 913 return nil, err 914 } 915 if reply, ok := msg.GetData().(*types.ReplySubscribePush); ok { 916 return reply, nil 917 } 918 return nil, types.ErrTypeAsset 919 } 920 921 // ListPushes List Seq CallBacks 922 func (q *QueueProtocol) ListPushes() (*types.PushSubscribes, error) { 923 msg, err := q.send(blockchainKey, types.EventListPushes, &types.ReqNil{}) 924 if err != nil { 925 log.Error("ListPushes", "Error", err.Error()) 926 return nil, err 927 } 928 if reply, ok := msg.GetData().(*types.PushSubscribes); ok { 929 return reply, nil 930 } 931 return nil, types.ErrTypeAsset 932 } 933 934 // GetPushSeqLastNum Get Seq Call Back Last Num 935 func (q *QueueProtocol) GetPushSeqLastNum(param *types.ReqString) (*types.Int64, error) { 936 msg, err := q.send(blockchainKey, types.EventGetPushLastNum, param) 937 if err != nil { 938 log.Error("GetPushSeqLastNum", "Error", err.Error()) 939 return nil, err 940 } 941 if reply, ok := msg.GetData().(*types.Int64); ok { 942 return reply, nil 943 } 944 return nil, types.ErrTypeAsset 945 } 946 947 // GetLastBlockMainSequence 获取最新的block执行序列号 948 func (q *QueueProtocol) GetLastBlockMainSequence() (*types.Int64, error) { 949 msg, err := q.send(blockchainKey, types.EventGetLastBlockMainSequence, &types.ReqNil{}) 950 if err != nil { 951 log.Error("GetLastBlockMainSequence", "Error", err.Error()) 952 return nil, err 953 } 954 if reply, ok := msg.GetData().(*types.Int64); ok { 955 956 return reply, nil 957 } 958 return nil, types.ErrTypeAsset 959 } 960 961 // GetMainSequenceByHash 通过hash获取对应的执行序列号 962 func (q *QueueProtocol) GetMainSequenceByHash(param *types.ReqHash) (*types.Int64, error) { 963 if param == nil { 964 err := types.ErrInvalidParam 965 log.Error("GetMainSequenceByHash", "Error", err) 966 return nil, err 967 } 968 msg, err := q.send(blockchainKey, types.EventGetMainSeqByHash, param) 969 if err != nil { 970 log.Error("GetMainSequenceByHash", "Error", err.Error()) 971 return nil, err 972 } 973 if reply, ok := msg.GetData().(*types.Int64); ok { 974 975 return reply, nil 976 } 977 return nil, types.ErrTypeAsset 978 } 979 980 //GetParaTxByTitle 通过seq以及title获取对应平行连的交易 981 func (q *QueueProtocol) GetParaTxByTitle(param *types.ReqParaTxByTitle) (*types.ParaTxDetails, error) { 982 if param == nil { 983 err := types.ErrInvalidParam 984 log.Error("GetParaTxByTitle", "Error", err) 985 return nil, err 986 } 987 msg, err := q.send(blockchainKey, types.EventGetParaTxByTitle, param) 988 if err != nil { 989 log.Error("GetParaTxByTitle", "Error", err.Error()) 990 return nil, err 991 } 992 if reply, ok := msg.GetData().(*types.ParaTxDetails); ok { 993 994 return reply, nil 995 } 996 return nil, types.ErrTypeAsset 997 } 998 999 //LoadParaTxByTitle //获取拥有此title交易的区块高度 1000 func (q *QueueProtocol) LoadParaTxByTitle(param *types.ReqHeightByTitle) (*types.ReplyHeightByTitle, error) { 1001 if param == nil { 1002 err := types.ErrInvalidParam 1003 log.Error("LoadParaTxByTitle", "Error", err) 1004 return nil, err 1005 } 1006 msg, err := q.send(blockchainKey, types.EventGetHeightByTitle, param) 1007 if err != nil { 1008 log.Error("LoadParaTxByTitle", "Error", err.Error()) 1009 return nil, err 1010 } 1011 if reply, ok := msg.GetData().(*types.ReplyHeightByTitle); ok { 1012 1013 return reply, nil 1014 } 1015 return nil, types.ErrTypeAsset 1016 } 1017 1018 //GetParaTxByHeight //通过区块高度列表+title获取平行链交易 1019 func (q *QueueProtocol) GetParaTxByHeight(param *types.ReqParaTxByHeight) (*types.ParaTxDetails, error) { 1020 if param == nil { 1021 err := types.ErrInvalidParam 1022 log.Error("GetParaTxByHeight", "Error", err) 1023 return nil, err 1024 } 1025 msg, err := q.send(blockchainKey, types.EventGetParaTxByTitleAndHeight, param) 1026 if err != nil { 1027 log.Error("GetParaTxByHeight", "Error", err.Error()) 1028 return nil, err 1029 } 1030 if reply, ok := msg.GetData().(*types.ParaTxDetails); ok { 1031 1032 return reply, nil 1033 } 1034 return nil, types.ErrTypeAsset 1035 } 1036 1037 //GetConfig 通过seq以及title获取对应平行连的交易 1038 func (q *QueueProtocol) GetConfig() *types.TuringchainConfig { 1039 if q.client == nil { 1040 panic("client is nil, can not get TuringchainConfig") 1041 } 1042 cfg := q.client.GetConfig() 1043 if cfg == nil { 1044 panic("TuringchainConfig is nil") 1045 } 1046 return cfg 1047 }