Loading...
Searching...
No Matches
tx_mempool_test.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#include <gtest/gtest.h>
24
26
27namespace resdb {
28namespace utxo {
29namespace {
30
31using ::resdb::testing::EqualsProto;
32using ::testing::Test;
33
34TEST(TxMempoolTest, AddTx) {
35 UTXO utxo;
36 auto out = utxo.add_out();
37 out->set_value(100);
38 out->set_address("0000");
39 utxo.set_address("0001");
40
41 TxMempool pool;
42 EXPECT_EQ(pool.AddUTXO(utxo), 0);
43
44 int64_t value = pool.GetUTXOOutValue(0, 0, "0000");
45 EXPECT_EQ(value, 100);
46}
47
48TEST(TxMempoolTest, AddTxNoIdx) {
49 UTXO utxo;
50 auto out = utxo.add_out();
51 out->set_value(100);
52 out->set_address("0000");
53
54 utxo.set_address("0001");
55
56 TxMempool pool;
57 EXPECT_EQ(pool.AddUTXO(utxo), 0);
58
59 int64_t value = pool.GetUTXOOutValue(1, 1, "0000");
60 EXPECT_EQ(value, -1);
61}
62
63TEST(TxMempoolTest, AddTxNoOutAddr) {
64 UTXO utxo;
65 auto out = utxo.add_out();
66 out->set_value(100);
67 out->set_address("0000");
68
69 utxo.set_address("0001");
70
71 TxMempool pool;
72 EXPECT_EQ(pool.AddUTXO(utxo), 0);
73
74 int64_t value = pool.GetUTXOOutValue(1, 0, "0002");
75 EXPECT_EQ(value, -1);
76}
77
78TEST(TxMempoolTest, AddTxNoAddr) {
79 UTXO utxo;
80 auto out = utxo.add_out();
81 out->set_value(100);
82 out->set_address("0000");
83
84 utxo.set_address("0001");
85
86 TxMempool pool;
87 EXPECT_EQ(pool.AddUTXO(utxo), 0);
88
89 int64_t value = pool.GetUTXOOutValue(2, 0, "0000");
90 EXPECT_EQ(value, -1);
91}
92
93TEST(TxMempoolTest, AddTxMarkSpent) {
94 UTXO utxo;
95 auto out = utxo.add_out();
96 out->set_value(100);
97 out->set_address("0000");
98
99 utxo.set_address("0001");
100
101 TxMempool pool;
102 EXPECT_EQ(pool.AddUTXO(utxo), 0);
103
104 int64_t value = pool.MarkSpend(0, 0, "0000");
105 EXPECT_EQ(value, 100);
106
107 EXPECT_EQ(pool.GetUTXOOutValue(0, 0, "0000"), -1);
108}
109
110TEST(TxMempoolTest, AddTxAndGet) {
111 UTXO utxo;
112 auto out = utxo.add_out();
113 out->set_value(100);
114 out->set_address("0000");
115
116 utxo.set_address("0001");
117
118 TxMempool pool;
119 EXPECT_EQ(pool.AddUTXO(utxo), 0);
120
121 auto value_or = pool.GetUTXO(0, 0, "0000");
122 EXPECT_TRUE(value_or.ok());
123
124 EXPECT_EQ((*value_or).value(), 100);
125}
126
127TEST(TxMempoolTest, GetList) {
128 UTXO utxo1;
129 UTXO utxo2;
130 UTXO utxo3;
131
132 {
133 auto out = utxo1.add_out();
134 out->set_value(100);
135 out->set_address("0002");
136 utxo1.set_address("0001");
137 }
138
139 {
140 auto in = utxo2.add_in();
141 in->set_prev_id(0);
142
143 auto out = utxo2.add_out();
144 out->set_value(100);
145 out->set_address("0003");
146 utxo2.set_address("0002");
147 }
148 {
149 auto in = utxo3.add_in();
150 in->set_prev_id(1);
151
152 auto out = utxo3.add_out();
153 out->set_value(100);
154 out->set_address("0004");
155 utxo3.set_address("0003");
156 }
157
158 TxMempool pool;
159 EXPECT_EQ(pool.AddUTXO(utxo1), 0);
160 EXPECT_EQ(pool.AddUTXO(utxo2), 1);
161 EXPECT_EQ(pool.AddUTXO(utxo3), 2);
162
163 utxo1.set_transaction_id(0);
164 utxo2.set_transaction_id(1);
165 utxo3.set_transaction_id(2);
166 {
167 auto list = pool.GetUTXO(-1, 2);
168 EXPECT_EQ(list.size(), 2);
169 EXPECT_THAT(list[0], EqualsProto(utxo2));
170 EXPECT_THAT(list[1], EqualsProto(utxo3));
171 }
172 {
173 auto list = pool.GetUTXO(-1, 3);
174 EXPECT_EQ(list.size(), 3);
175 EXPECT_THAT(list[0], EqualsProto(utxo1));
176 EXPECT_THAT(list[1], EqualsProto(utxo2));
177 EXPECT_THAT(list[2], EqualsProto(utxo3));
178 }
179 {
180 auto list = pool.GetUTXO(1, 2);
181 EXPECT_EQ(list.size(), 2);
182 EXPECT_THAT(list[0], EqualsProto(utxo1));
183 EXPECT_THAT(list[1], EqualsProto(utxo2));
184 }
185 {
186 auto list = pool.GetUTXO(2, 4);
187 EXPECT_EQ(list.size(), 3);
188 EXPECT_THAT(list[0], EqualsProto(utxo1));
189 EXPECT_THAT(list[1], EqualsProto(utxo2));
190 EXPECT_THAT(list[2], EqualsProto(utxo3));
191 }
192}
193
194} // namespace
195} // namespace utxo
196} // namespace resdb
TEST(NetworkUtilsTest, GetDNSName)
void GetList(UTXOClient *client, int64_t end_id, int num)