Skip to content

Commit 20c7518

Browse files
committed
Add get_witness, get_input_script, get_output_script.
1 parent a27fb45 commit 20c7518

File tree

5 files changed

+186
-55
lines changed

5 files changed

+186
-55
lines changed

include/bitcoin/database/impl/query/objects.ipp

Lines changed: 67 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,6 @@ typename CLASS::transaction::cptr CLASS::get_transaction(const tx_link& link,
192192
return ptr;
193193
}
194194

195-
TEMPLATE
196-
typename CLASS::output::cptr CLASS::get_output(
197-
const output_link& link) const NOEXCEPT
198-
{
199-
table::output::only out{};
200-
if (!store_.output.get(link, out))
201-
return {};
202-
203-
return out.output;
204-
}
205-
206195
// static/protected
207196
TEMPLATE
208197
typename CLASS::point::cptr CLASS::make_point(hash_digest&& hash,
@@ -216,6 +205,50 @@ typename CLASS::point::cptr CLASS::make_point(hash_digest&& hash,
216205
return system::to_shared<point>(std::move(hash), index);
217206
}
218207

208+
TEMPLATE
209+
typename CLASS::point CLASS::get_point(
210+
const point_link& link) const NOEXCEPT
211+
{
212+
table::point::record point{};
213+
if (!store_.point.get(link, point))
214+
return {};
215+
216+
return { point.hash, point.index };
217+
}
218+
219+
TEMPLATE
220+
typename CLASS::witness::cptr CLASS::get_witness(
221+
const point_link& link) const NOEXCEPT
222+
{
223+
table::input::get_witness in{};
224+
table::ins::get_input ins{};
225+
if (!store_.ins.get(link, ins) ||
226+
!store_.input.get(ins.input_fk, in))
227+
return {};
228+
229+
return in.witness;
230+
}
231+
232+
TEMPLATE
233+
typename CLASS::script::cptr CLASS::get_input_script(
234+
const point_link& link) const NOEXCEPT
235+
{
236+
table::input::get_script in{};
237+
table::ins::get_input ins{};
238+
if (!store_.ins.get(link, ins) ||
239+
!store_.input.get(ins.input_fk, in))
240+
return {};
241+
242+
return in.script;
243+
}
244+
245+
TEMPLATE
246+
typename CLASS::input::cptr CLASS::get_input(const tx_link& link,
247+
uint32_t index, bool witness) const NOEXCEPT
248+
{
249+
return get_input(to_point(link, index), witness);
250+
}
251+
219252
TEMPLATE
220253
typename CLASS::input::cptr CLASS::get_input(const point_link& link,
221254
bool witness) const NOEXCEPT
@@ -243,14 +276,32 @@ typename CLASS::input::cptr CLASS::get_input(const point_link& link,
243276
}
244277

245278
TEMPLATE
246-
typename CLASS::point CLASS::get_point(
247-
const point_link& link) const NOEXCEPT
279+
typename CLASS::script::cptr CLASS::get_output_script(
280+
const output_link& link) const NOEXCEPT
248281
{
249-
table::point::record point{};
250-
if (!store_.point.get(link, point))
282+
table::output::get_script out{};
283+
if (!store_.output.get(link, out))
251284
return {};
252285

253-
return { point.hash, point.index };
286+
return out.script;
287+
}
288+
289+
TEMPLATE
290+
typename CLASS::output::cptr CLASS::get_output(const tx_link& link,
291+
uint32_t index) const NOEXCEPT
292+
{
293+
return get_output(to_output(link, index));
294+
}
295+
296+
TEMPLATE
297+
typename CLASS::output::cptr CLASS::get_output(
298+
const output_link& link) const NOEXCEPT
299+
{
300+
table::output::only out{};
301+
if (!store_.output.get(link, out))
302+
return {};
303+
304+
return out.output;
254305
}
255306

256307
TEMPLATE
@@ -270,27 +321,6 @@ typename CLASS::inputs_ptr CLASS::get_spenders(
270321
return inputs;
271322
}
272323

273-
TEMPLATE
274-
typename CLASS::input::cptr CLASS::get_input(const tx_link& link,
275-
uint32_t input_index, bool witness) const NOEXCEPT
276-
{
277-
return get_input(to_point(link, input_index), witness);
278-
}
279-
280-
TEMPLATE
281-
typename CLASS::output::cptr CLASS::get_output(const tx_link& link,
282-
uint32_t output_index) const NOEXCEPT
283-
{
284-
return get_output(to_output(link, output_index));
285-
}
286-
287-
TEMPLATE
288-
typename CLASS::inputs_ptr CLASS::get_spenders_index(const tx_link& link,
289-
uint32_t output_index, bool witness) const NOEXCEPT
290-
{
291-
return get_spenders(to_output(link, output_index), witness);
292-
}
293-
294324
// Populate prevout objects.
295325
// ----------------------------------------------------------------------------
296326

include/bitcoin/database/query.hpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ class query
6464
using block = system::chain::block;
6565
using point = system::chain::point;
6666
using input = system::chain::input;
67-
using script = system::chain::script;
6867
using output = system::chain::output;
6968
using header = system::chain::header;
69+
using script = system::chain::script;
70+
using witness = system::chain::witness;
7071
using headers = system::chain::header_cptrs;
7172
using transaction = system::chain::transaction;
7273
using transactions = system::chain::transaction_cptrs;
@@ -379,20 +380,22 @@ class query
379380

380381
header::cptr get_header(const header_link& link) const NOEXCEPT;
381382
block::cptr get_block(const header_link& link, bool witness) const NOEXCEPT;
382-
transaction::cptr get_transaction(const tx_link& link, bool witness) const NOEXCEPT;
383+
transaction::cptr get_transaction(const tx_link& link,
384+
bool witness) const NOEXCEPT;
385+
386+
point get_point(const point_link& link) const NOEXCEPT;
387+
witness::cptr get_witness(const point_link& link) const NOEXCEPT;
388+
script::cptr get_input_script(const point_link& link) const NOEXCEPT;
383389
input::cptr get_input(const point_link& link, bool witness) const NOEXCEPT;
390+
input::cptr get_input(const tx_link& link, uint32_t index,
391+
bool witness) const NOEXCEPT;
392+
393+
script::cptr get_output_script(const output_link& link) const NOEXCEPT;
384394
output::cptr get_output(const output_link& link) const NOEXCEPT;
395+
output::cptr get_output(const tx_link& link, uint32_t index) const NOEXCEPT;
385396
inputs_ptr get_spenders(const output_link& link,
386397
bool witness) const NOEXCEPT;
387398

388-
input::cptr get_input(const tx_link& link,
389-
uint32_t input_index, bool witness) const NOEXCEPT;
390-
output::cptr get_output(const tx_link& link,
391-
uint32_t output_index) const NOEXCEPT;
392-
point get_point(const point_link& link) const NOEXCEPT;
393-
inputs_ptr get_spenders_index(const tx_link& link,
394-
uint32_t output_index, bool witness) const NOEXCEPT;
395-
396399
/// False implies missing prevouts, node input.metadata is populated.
397400
bool populate_with_metadata(const input& input) const NOEXCEPT;
398401
bool populate_with_metadata(const block& block) const NOEXCEPT;

include/bitcoin/database/tables/archives/input.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,45 @@ struct input
100100
system::chain::witness::cptr witness{};
101101
};
102102

103+
struct get_script
104+
: public schema::input
105+
{
106+
link count() const NOEXCEPT
107+
{
108+
BC_ASSERT(false);
109+
return {};
110+
}
111+
112+
inline bool from_data(reader& source) NOEXCEPT
113+
{
114+
using namespace system;
115+
script = std::make_shared<const chain::script>(source, true);
116+
return source;
117+
}
118+
119+
system::chain::script::cptr script{};
120+
};
121+
122+
struct get_witness
123+
: public schema::input
124+
{
125+
link count() const NOEXCEPT
126+
{
127+
BC_ASSERT(false);
128+
return {};
129+
}
130+
131+
inline bool from_data(reader& source) NOEXCEPT
132+
{
133+
using namespace system;
134+
source.skip_bytes(source.read_size());
135+
witness = std::make_shared<const chain::witness>(source, true);
136+
return source;
137+
}
138+
139+
system::chain::witness::cptr witness{};
140+
};
141+
103142
struct put_ref
104143
: public schema::input
105144
{

include/bitcoin/database/tables/archives/output.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,27 @@ struct output
112112
system::chain::output::cptr output{};
113113
};
114114

115+
struct get_script
116+
: public schema::output
117+
{
118+
link count() const NOEXCEPT
119+
{
120+
BC_ASSERT(false);
121+
return {};
122+
}
123+
124+
inline bool from_data(reader& source) NOEXCEPT
125+
{
126+
using namespace system;
127+
source.skip_bytes(tx::size);
128+
source.skip_variable();
129+
script = std::make_shared<const chain::script>(source, true);
130+
return source;
131+
}
132+
133+
system::chain::script::cptr script{};
134+
};
135+
115136
struct get_parent
116137
: public schema::output
117138
{

test/query/archive_read.cpp

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -470,14 +470,37 @@ BOOST_AUTO_TEST_CASE(query_archive_read__get_tx_count__coinbase__1)
470470
BOOST_REQUIRE_EQUAL(query.get_tx_count(0), 1u);
471471
}
472472

473+
BOOST_AUTO_TEST_CASE(query_archive_read__get_witness__not_found__nullptr)
474+
{
475+
settings settings{};
476+
settings.path = TEST_DIRECTORY;
477+
test::chunk_store store{ settings };
478+
test::query_accessor query{ store };
479+
BOOST_REQUIRE(!store.create(events_handler));
480+
BOOST_REQUIRE(!query.get_witness(query.to_point({}, {})));
481+
BOOST_REQUIRE(!store.close(events_handler));
482+
}
483+
484+
BOOST_AUTO_TEST_CASE(query_archive_read__get_input_script__not_found__nullptr)
485+
{
486+
settings settings{};
487+
settings.path = TEST_DIRECTORY;
488+
test::chunk_store store{ settings };
489+
test::query_accessor query{ store };
490+
BOOST_REQUIRE(!store.create(events_handler));
491+
BOOST_REQUIRE(!query.get_input_script(query.to_point(tx_link{}, {})));
492+
BOOST_REQUIRE(!store.close(events_handler));
493+
}
494+
473495
BOOST_AUTO_TEST_CASE(query_archive_read__get_input__not_found__nullptr)
474496
{
475497
settings settings{};
476498
settings.path = TEST_DIRECTORY;
477499
test::chunk_store store{ settings };
478500
test::query_accessor query{ store };
479501
BOOST_REQUIRE(!store.create(events_handler));
480-
BOOST_REQUIRE(!query.get_input(query.to_tx(system::null_hash), 0u, false));
502+
BOOST_REQUIRE(!query.get_input(query.to_point(tx_link{}, {}), false));
503+
BOOST_REQUIRE(!query.get_input(query.to_tx(system::null_hash), {}, false));
481504
BOOST_REQUIRE(!store.close(events_handler));
482505
}
483506

@@ -495,8 +518,10 @@ BOOST_AUTO_TEST_CASE(query_archive_read__get_input__genesis__expected)
495518

496519
const auto tx = test::genesis.transactions_ptr()->front();
497520
const auto input = tx->inputs_ptr()->front();
498-
//// BOOST_REQUIRE(*input == *query.get_input(query.to_tx(tx->hash(false)), 0u));
499-
//// BOOST_REQUIRE(*input == *query.get_input(0));
521+
const auto point = query.to_point(query.to_tx(tx->hash(false)), 0u);
522+
BOOST_REQUIRE(*input == *query.get_input(point, false));
523+
BOOST_REQUIRE(input->witness() == *query.get_witness(point));
524+
BOOST_REQUIRE(input->script() == *query.get_input_script(point));
500525
}
501526

502527
BOOST_AUTO_TEST_CASE(query_archive_read__get_inputs__tx_not_found__nullptr)
@@ -522,6 +547,18 @@ BOOST_AUTO_TEST_CASE(query_archive_read__get_inputs__found__expected)
522547
BOOST_REQUIRE_EQUAL(query.get_inputs(1, false)->size(), 2u);
523548
}
524549

550+
BOOST_AUTO_TEST_CASE(query_archive_read__get_output_script__not_found__nullptr)
551+
{
552+
settings settings{};
553+
settings.path = TEST_DIRECTORY;
554+
test::chunk_store store{ settings };
555+
test::query_accessor query{ store };
556+
BOOST_REQUIRE(!store.create(events_handler));
557+
BOOST_REQUIRE(!query.get_output_script(query.to_output(system::null_hash, 0u)));
558+
BOOST_REQUIRE(!query.get_output(0));
559+
BOOST_REQUIRE(!store.close(events_handler));
560+
}
561+
525562
BOOST_AUTO_TEST_CASE(query_archive_read__get_output__not_found__nullptr)
526563
{
527564
settings settings{};
@@ -530,7 +567,7 @@ BOOST_AUTO_TEST_CASE(query_archive_read__get_output__not_found__nullptr)
530567
test::query_accessor query{ store };
531568
BOOST_REQUIRE(!store.create(events_handler));
532569
BOOST_REQUIRE(!query.get_output(query.to_tx(system::null_hash), 0u));
533-
BOOST_REQUIRE(!query.get_output(query.to_output(system::chain::point{ system::null_hash, 0u })));
570+
BOOST_REQUIRE(!query.get_output(query.to_output(system::null_hash, 0u)));
534571
BOOST_REQUIRE(!query.get_output(0));
535572
BOOST_REQUIRE(!store.close(events_handler));
536573
}
@@ -548,10 +585,11 @@ BOOST_AUTO_TEST_CASE(query_archive_read__get_output__genesis__expected)
548585
BOOST_REQUIRE(query.set(test::genesis, test::context, false, false));
549586

550587
const auto tx = test::genesis.transactions_ptr()->front();
551-
const auto output1 = tx->outputs_ptr()->front();
552-
BOOST_REQUIRE(*output1 == *query.get_output(query.to_tx(tx->hash(false)), 0u));
553-
BOOST_REQUIRE(*output1 == *query.get_output(query.to_output(tx->hash(false), 0u)));
554-
BOOST_REQUIRE(*output1 == *query.get_output(0));
588+
const auto output = tx->outputs_ptr()->front();
589+
BOOST_REQUIRE(*output == *query.get_output(0));
590+
BOOST_REQUIRE(*output == *query.get_output(query.to_tx(tx->hash(false)), 0u));
591+
BOOST_REQUIRE(*output == *query.get_output(query.to_output(tx->hash(false), 0u)));
592+
BOOST_REQUIRE(output->script() == *query.get_output_script(query.to_output(tx->hash(false), 0u)));
555593
}
556594

557595
BOOST_AUTO_TEST_CASE(query_archive_read__get_outputs__tx_not_found__nullptr)

0 commit comments

Comments
 (0)