Loading...
Searching...
No Matches
utxo_executor.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
29UTXOExecutor::UTXOExecutor(const Config& config, Transaction* transaction,
30 Wallet* wallet)
31 : transaction_(transaction) {}
32
34
35std::unique_ptr<std::string> UTXOExecutor::ExecuteData(
36 const std::string& client_request) {
37 UTXORequest utxo_request;
38 UTXOResponse response;
39
40 if (!utxo_request.ParseFromString(client_request)) {
41 LOG(ERROR) << "parse data fail";
42 return nullptr;
43 }
44
45 int64_t ret = transaction_->AddTransaction(utxo_request.utxo());
46 response.set_ret(ret);
47 std::unique_ptr<std::string> ret_str = std::make_unique<std::string>();
48 response.SerializeToString(ret_str.get());
49 return ret_str;
50}
51
53 : transaction_(transaction), wallet_(wallet) {}
54
55std::unique_ptr<std::string> QueryExecutor::Query(
56 const std::string& request_str) {
57 UTXOQuery query;
58 if (!query.ParseFromString(request_str)) {
59 LOG(ERROR) << "parse query fail";
60 return nullptr;
61 }
62
63 UTXOQueryResponse resp;
64 if (query.query_transaction()) {
65 std::vector<UTXO> utxos =
66 transaction_->GetUTXO(query.end_id(), query.num());
67 for (const UTXO& utxo : utxos) {
68 *resp.add_utxos() = utxo;
69 }
70 } else {
71 resp.set_value(wallet_->GetCoin(query.address()));
72 }
73 std::unique_ptr<std::string> resp_str = std::make_unique<std::string>();
74 resp.SerializeToString(resp_str.get());
75 return resp_str;
76}
77
78} // namespace utxo
79} // namespace resdb
QueryExecutor(Transaction *transaction, Wallet *wallet)
virtual std::unique_ptr< std::string > Query(const std::string &request_str) override
std::vector< UTXO > GetUTXO(int64_t end_id, int num)
int64_t AddTransaction(const std::string &utxo_string)
std::unique_ptr< std::string > ExecuteData(const std::string &request) override
UTXOExecutor(const Config &config, Transaction *transaction, Wallet *wallet)
int64_t GetCoin(const std::string &address)
Definition wallet.cpp:35