Skip to content

Commit 9c5ca6d

Browse files
committed
Tests for address.get_history().
1 parent 94240c8 commit 9c5ca6d

3 files changed

Lines changed: 150 additions & 16 deletions

File tree

test/protocols/blocks.cpp

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,33 +94,33 @@ bool setup_ten_block_store(query_t& query) NOEXCEPT
9494
query.set(block7, database::context{ 0, 7, 0 }, false, false) &&
9595
query.set(block8, database::context{ 0, 8, 0 }, false, false) &&
9696
query.set(block9, database::context{ 0, 9, 0 }, false, false) &&
97-
query.push_confirmed(query.to_header(block1_hash), false) &&
98-
query.push_confirmed(query.to_header(block2_hash), false) &&
99-
query.push_confirmed(query.to_header(block3_hash), false) &&
100-
query.push_confirmed(query.to_header(block4_hash), false) &&
101-
query.push_confirmed(query.to_header(block5_hash), false) &&
102-
query.push_confirmed(query.to_header(block6_hash), false) &&
103-
query.push_confirmed(query.to_header(block7_hash), false) &&
104-
query.push_confirmed(query.to_header(block8_hash), false) &&
105-
query.push_confirmed(query.to_header(block9_hash), false);
97+
query.push_confirmed(query.to_header(block1_hash), true) &&
98+
query.push_confirmed(query.to_header(block2_hash), true) &&
99+
query.push_confirmed(query.to_header(block3_hash), true) &&
100+
query.push_confirmed(query.to_header(block4_hash), true) &&
101+
query.push_confirmed(query.to_header(block5_hash), true) &&
102+
query.push_confirmed(query.to_header(block6_hash), true) &&
103+
query.push_confirmed(query.to_header(block7_hash), true) &&
104+
query.push_confirmed(query.to_header(block8_hash), true) &&
105+
query.push_confirmed(query.to_header(block9_hash), true);
106106
}
107107

108108
bool setup_three_block_store(query_t& query) NOEXCEPT
109109
{
110110
return query.initialize(genesis) &&
111111
query.set(block1, database::context{ 0, 1, 0 }, false, false) &&
112112
query.set(block2, database::context{ 0, 2, 0 }, false, false) &&
113-
query.push_confirmed(query.to_header(block1_hash), false) &&
114-
query.push_confirmed(query.to_header(block2_hash), false);
113+
query.push_confirmed(query.to_header(block1_hash), true) &&
114+
query.push_confirmed(query.to_header(block2_hash), true);
115115
}
116116

117117
bool setup_three_block_witness_store(query_t& query) NOEXCEPT
118118
{
119119
return query.initialize(genesis) &&
120120
query.set(block1a, database::context{ 0, 1, 0 }, false, false) &&
121121
query.set(block2a, database::context{ 0, 2, 0 }, false, false) &&
122-
query.push_confirmed(query.to_header(block1a.hash()), false) &&
123-
query.push_confirmed(query.to_header(block2a.hash()), false);
122+
query.push_confirmed(query.to_header(block1a.hash()), true) &&
123+
query.push_confirmed(query.to_header(block2a.hash()), true);
124124
}
125125

126126
bool setup_three_block_confirmed_address_store(query_t& query) NOEXCEPT
@@ -265,8 +265,8 @@ const block bogus_block11
265265
header
266266
{
267267
0x31323334,
268-
block9_hash,
269-
hash_digest{ 0x12, 0xbb },
268+
bogus_block10.hash(),
269+
hash_digest{ 0x11, 0xbb },
270270
0x41424344,
271271
0x51525354,
272272
0x61626364
@@ -330,6 +330,52 @@ const block bogus_block11
330330
}
331331
}
332332
};
333+
const block bogus_block12
334+
{
335+
header
336+
{
337+
0x31323334,
338+
bogus_block11.hash(),
339+
hash_digest{ 0x12, 0xbb },
340+
0x41424344,
341+
0x51525354,
342+
0x61626364
343+
},
344+
transactions
345+
{
346+
transaction
347+
{
348+
0x07,
349+
inputs
350+
{
351+
input
352+
{
353+
point{ bogus_block11.transactions_ptr()->front()->hash(false), 0x03 },
354+
script{},
355+
witness{},
356+
0x08
357+
},
358+
input
359+
{
360+
point{ bogus_block11.transactions_ptr()->front()->hash(false), 0x04 },
361+
script{},
362+
witness{},
363+
0x09
364+
}
365+
},
366+
outputs
367+
{
368+
output
369+
{
370+
0x10,
371+
// "1BaMPFdqMUQ46BV8iRcwbVfsam57oBLMM"
372+
script::to_pay_key_hash_pattern({ 0x02 })
373+
}
374+
},
375+
0x0a
376+
}
377+
}
378+
};
333379
const block bogus_block
334380
{
335381
header

test/protocols/blocks.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ extern const system::chain::block block8;
8686
extern const system::chain::block block9;
8787
extern const system::chain::block bogus_block10;
8888
extern const system::chain::block bogus_block11;
89+
extern const system::chain::block bogus_block12;
8990

9091
extern const system::chain::block bogus_block;
9192
extern const system::chain::block block1a;

test/protocols/electrum/electrum_addresses.cpp

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ BOOST_AUTO_TEST_CASE(electrum__blockchain_address_get_balance__no_address_index_
3838
BOOST_REQUIRE_EQUAL(response.at("error").as_object().at("code").as_int64(), not_implemented.value());
3939
}
4040

41+
BOOST_AUTO_TEST_CASE(electrum__blockchain_address_get_history__no_address_index__not_implemented)
42+
{
43+
BOOST_REQUIRE(!query_.address_enabled());
44+
BOOST_REQUIRE(handshake(electrum::version::v1_0));
45+
46+
const auto request = R"({"id":1001,"method":"blockchain.address.get_history","params":["%1%"]})" "\n";
47+
const auto response = get((boost::format(request) % "1JqDybm2nWTENrHvMyafbSXXtTk5Uv5QAn").str());
48+
REQUIRE_NO_THROW_TRUE(response.at("error").as_object().at("code").is_int64());
49+
BOOST_REQUIRE_EQUAL(response.at("error").as_object().at("code").as_int64(), not_implemented.value());
50+
}
51+
4152
BOOST_AUTO_TEST_SUITE_END()
4253

4354
BOOST_FIXTURE_TEST_SUITE(electrum_tests, electrum_ten_block_setup_fixture)
@@ -102,14 +113,90 @@ BOOST_AUTO_TEST_CASE(electrum__blockchain_address_get_balance__confirmed_and_unc
102113
const auto& result = response.at("result").as_object();
103114
REQUIRE_NO_THROW_TRUE(result.at("confirmed").is_int64());
104115
REQUIRE_NO_THROW_TRUE(result.at("unconfirmed").is_int64());
105-
BOOST_REQUIRE_EQUAL(result.at("confirmed").as_int64(), 0x09); // bogus_block11
116+
BOOST_REQUIRE_EQUAL(result.at("confirmed").as_int64(), 0x09); // bogus_block10
106117

107118
// Currently unconfirmed ALWAYS returns zero (no graph to order conflicts).
108119
BOOST_REQUIRE_EQUAL(result.at("unconfirmed").as_int64(), 0x00);
109120
////BOOST_REQUIRE_EQUAL(result.at("unconfirmed").as_int64(), 0x10 + 0x11 + 0x12); // bogus_block11
110121
}
111122

112123
// blockchain.address.get_history
124+
125+
BOOST_AUTO_TEST_CASE(electrum__blockchain_address_get_history__missing_arguments__dropped)
126+
{
127+
BOOST_REQUIRE(query_.address_enabled());
128+
BOOST_REQUIRE(handshake(electrum::version::v1_0));
129+
130+
const auto response = get(R"({"id":1002,"method":"blockchain.address.get_history","params":[]})" "\n");
131+
REQUIRE_NO_THROW_TRUE(response.at("dropped").as_bool());
132+
}
133+
134+
BOOST_AUTO_TEST_CASE(electrum__blockchain_address_get_history__empty_v1_3__wrong_version)
135+
{
136+
BOOST_REQUIRE(handshake(electrum::version::v1_3));
137+
138+
const auto response = get(R"({"id":1003,"method":"blockchain.address.get_history","params":[""]})" "\n");
139+
REQUIRE_NO_THROW_TRUE(response.at("error").as_object().at("code").is_int64());
140+
BOOST_REQUIRE_EQUAL(response.at("error").as_object().at("code").as_int64(), wrong_version.value());
141+
}
142+
143+
BOOST_AUTO_TEST_CASE(electrum__blockchain_address_get_history__invalid_address__invalid_argument)
144+
{
145+
BOOST_REQUIRE(handshake(electrum::version::v1_1));
146+
147+
const auto response = get(R"({"id":1004,"method":"blockchain.address.get_history","params":["invalid"]})" "\n");
148+
REQUIRE_NO_THROW_TRUE(response.at("error").as_object().at("code").is_int64());
149+
BOOST_REQUIRE_EQUAL(response.at("error").as_object().at("code").as_int64(), invalid_argument.value());
150+
}
151+
152+
BOOST_AUTO_TEST_CASE(electrum__blockchain_address_get_history__not_found_address__empty)
153+
{
154+
BOOST_REQUIRE(handshake(electrum::version::v1_0));
155+
156+
const auto request = R"({"id":1005,"method":"blockchain.address.get_history","params":["%1%"]})" "\n";
157+
const auto response = get((boost::format(request) % "1JqDybm2nWTENrHvMyafbSXXtTk5Uv5QAn").str());
158+
REQUIRE_NO_THROW_TRUE(response.at("result").as_array().empty());
159+
}
160+
161+
BOOST_AUTO_TEST_CASE(electrum__blockchain_address_get_history__confirmed_address__expected)
162+
{
163+
BOOST_REQUIRE(handshake(electrum::version::v1_0));
164+
165+
// Add one confirmed p2sh/p2kh block and one unconfirmed (mirrors the get_balance confirmed test).
166+
BOOST_REQUIRE(query_.set(test::bogus_block10, database::context{ 0, 10, 0 }, false, false));
167+
BOOST_REQUIRE(query_.set(test::bogus_block11, database::context{ 0, 11, 0 }, false, false));
168+
BOOST_REQUIRE(query_.set(test::bogus_block12, database::context{ 0, 12, 0 }, false, false));
169+
BOOST_REQUIRE(query_.push_confirmed(query_.to_header(test::bogus_block10.hash()), true));
170+
171+
const auto request = R"({"id":1006,"method":"blockchain.address.get_history","params":["%1%"]})" "\n";
172+
const auto response = get((boost::format(request) % "1BaMPFdqMUQ46BV8iRcwbVfsam57oBLMM").str());
173+
REQUIRE_NO_THROW_TRUE(response.at("result").is_array());
174+
175+
const auto& history = response.at("result").as_array();
176+
BOOST_REQUIRE_EQUAL(history.size(), 3u);
177+
178+
const auto hash1 = test::bogus_block10.transactions_ptr()->at(1)->hash(false);
179+
const auto& tx1 = history.at(0).as_object();
180+
REQUIRE_NO_THROW_TRUE(tx1.at("height").is_int64());
181+
REQUIRE_NO_THROW_TRUE(tx1.at("tx_hash").is_string());
182+
BOOST_CHECK_EQUAL(tx1.at("height").as_int64(), 10);
183+
BOOST_CHECK_EQUAL(tx1.at("tx_hash").as_string(), encode_hash(hash1));
184+
185+
const auto hash2 = test::bogus_block11.transactions_ptr()->at(0)->hash(false);
186+
const auto& tx2 = history.at(1).as_object();
187+
REQUIRE_NO_THROW_TRUE(tx2.at("height").is_int64());
188+
REQUIRE_NO_THROW_TRUE(tx2.at("tx_hash").is_string());
189+
BOOST_CHECK_EQUAL(tx2.at("height").as_int64(), 0); // rooted
190+
BOOST_CHECK_EQUAL(tx2.at("tx_hash").as_string(), encode_hash(hash2));
191+
192+
const auto hash3 = test::bogus_block12.transactions_ptr()->at(0)->hash(false);
193+
const auto& tx3 = history.at(2).as_object();
194+
REQUIRE_NO_THROW_TRUE(tx3.at("height").is_int64());
195+
REQUIRE_NO_THROW_TRUE(tx3.at("tx_hash").is_string());
196+
BOOST_CHECK_EQUAL(tx3.at("height").as_int64(), -1); // not rooted
197+
BOOST_CHECK_EQUAL(tx3.at("tx_hash").as_string(), encode_hash(hash3));
198+
}
199+
113200
// blockchain.address.get_mempool
114201
// blockchain.address.list_unspent
115202
// blockchain.address.subscribe

0 commit comments

Comments
 (0)