Loading...
Searching...
No Matches
utxo_client.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
21
22#include <glog/logging.h>
23
24#include "proto/utxo/rpc.pb.h"
25
26namespace resdb {
27namespace utxo {
28
31
32int UTXOClient::Transfer(const UTXO& utxo) {
33 UTXORequest request;
34 UTXOResponse response;
35 *request.mutable_utxo() = utxo;
36
37 int ret = SendRequest(request, &response);
38 if (ret != 0 || response.ret() < 0) {
39 return -1;
40 }
41 return response.ret();
42}
43
44std::vector<UTXO> UTXOClient::GetList(int64_t end_id, int num) {
45 UTXOQuery query;
46 query.set_query_transaction(true);
47 query.set_end_id(end_id);
48 query.set_num(num);
49
50 CustomQueryResponse response;
51
52 int ret = SendRequest(query, Request::TYPE_CUSTOM_QUERY);
53 if (ret) {
54 LOG(ERROR) << "send request fail";
55 return std::vector<UTXO>();
56 }
57
58 ret = RecvRawMessage(&response);
59 if (ret) {
60 LOG(ERROR) << "recv response fail";
61 return std::vector<UTXO>();
62 }
63
64 UTXOQueryResponse utxo_response;
65 utxo_response.ParseFromString(response.resp_str());
66 std::vector<UTXO> utxo_list;
67 for (const auto& utxo : utxo_response.utxos()) {
68 utxo_list.push_back(utxo);
69 }
70 return utxo_list;
71}
72
73int64_t UTXOClient::GetWallet(const std::string& address) {
74 UTXOQuery query;
75 query.set_query_transaction(false);
76 query.set_address(address);
77
78 CustomQueryResponse response;
79
80 int ret = SendRequest(query, Request::TYPE_CUSTOM_QUERY);
81 if (ret) {
82 LOG(ERROR) << "send request fail";
83 return -1;
84 }
85
86 ret = RecvRawMessage(&response);
87 if (ret) {
88 LOG(ERROR) << "recv response fail";
89 return -1;
90 }
91
92 UTXOQueryResponse utxo_response;
93 utxo_response.ParseFromString(response.resp_str());
94 return utxo_response.value();
95}
96
97} // namespace utxo
98} // namespace resdb
virtual int RecvRawMessage(google::protobuf::Message *message)
int SendRequest(const google::protobuf::Message &message, Request::Type type=Request::TYPE_CLIENT_REQUEST)
std::vector< UTXO > GetList(int64_t end_id, int num)
int Transfer(const UTXO &utxo)
int64_t GetWallet(const std::string &address)
UTXOClient(const ResDBConfig &config)