Loading...
Searching...
No Matches
tx_mempool.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
24namespace resdb {
25namespace utxo {
26
29
30int64_t TxMempool::AddUTXO(const UTXO& utxo) {
31 LOG(ERROR) << "add utxo id:" << id_;
32 int64_t cur_id = id_++;
33 txs_[cur_id] = std::make_unique<UTXO>(utxo);
34 return cur_id;
35}
36
37absl::StatusOr<UTXOOut> TxMempool::GetUTXO(int64_t id, int out_idx,
38 const std::string& address) {
39 if (txs_.find(id) == txs_.end()) {
40 LOG(ERROR) << "no utxo:" << id;
41 return absl::InvalidArgumentError("id invalid.");
42 }
43
44 UTXO* utxo = txs_[id].get();
45
46 if (utxo->out_size() <= out_idx) {
47 LOG(ERROR) << " idx not found:" << out_idx << " id:" << id;
48 return absl::InvalidArgumentError("id invalid.");
49 }
50
51 if (utxo->out(out_idx).spent()) {
52 LOG(ERROR) << " value has been spent:" << id << " idx:" << out_idx;
53 return absl::InvalidArgumentError("id has been spent.");
54 }
55
56 if (utxo->out(out_idx).address() != address) {
57 LOG(ERROR) << " address not match:" << address
58 << " utxo addr:" << utxo->out(out_idx).address();
59 return absl::InvalidArgumentError("address invalid.");
60 }
61 return utxo->out(out_idx);
62}
63
64int64_t TxMempool::GetUTXOOutValue(int64_t id, int out_idx,
65 const std::string& address) {
66 if (txs_.find(id) == txs_.end()) {
67 LOG(ERROR) << "no utxo:" << id;
68 return -1;
69 }
70
71 UTXO* utxo = txs_[id].get();
72
73 if (utxo->out_size() <= out_idx) {
74 LOG(ERROR) << " idx not found:" << out_idx << " id:" << id;
75 return -1;
76 }
77
78 if (utxo->out(out_idx).spent()) {
79 LOG(ERROR) << " value has been spent:" << id << " idx:" << out_idx;
80 return -1;
81 }
82 if (utxo->out(out_idx).address() != address) {
83 LOG(ERROR) << " address not match:" << address
84 << " utxo addr:" << utxo->out(out_idx).address();
85 return -1;
86 }
87 return utxo->out(out_idx).value();
88}
89
90int64_t TxMempool::MarkSpend(int64_t id, int out_idx,
91 const std::string& address) {
92 if (txs_.find(id) == txs_.end()) {
93 LOG(ERROR) << "no utxo:" << id;
94 return -1;
95 }
96
97 UTXO* utxo = txs_[id].get();
98 if (utxo->out_size() <= out_idx) {
99 LOG(ERROR) << " idx not found:" << out_idx << " id:" << id;
100 return -1;
101 }
102
103 utxo->mutable_out(out_idx)->set_spent(true);
104 return utxo->out(out_idx).value();
105}
106
107std::vector<UTXO> TxMempool::GetUTXO(int64_t end_idx, int num) {
108 if (end_idx == -1) {
109 int64_t max_id = ((--txs_.end())->first);
110 end_idx = max_id;
111 }
112
113 std::vector<UTXO> resp;
114 for (int i = 0; i < num; ++i) {
115 if (txs_.find(end_idx - i) == txs_.end()) {
116 break;
117 }
118 resp.push_back(*txs_[end_idx - i]);
119 resp.back().set_transaction_id(end_idx - i);
120 }
121
122 std::reverse(resp.begin(), resp.end());
123
124 return resp;
125}
126
127} // namespace utxo
128} // namespace resdb
int64_t AddUTXO(const UTXO &utxo)
int64_t GetUTXOOutValue(int64_t id, int out_idx, const std::string &address)
int64_t MarkSpend(int64_t id, int out_idx, const std::string &address)
absl::StatusOr< UTXOOut > GetUTXO(int64_t id, int out_idx, const std::string &address)