Skip to content

Commit 84e74dc

Browse files
committed
Merge #839: [0.18 backport] Index the genesis block with txindex
b23efb2 Improve coinbase and issuance tx testing for txindex (Steven Roose) 91ad748 Index the genesis block with txindex (Steven Roose) Pull request description: Backport of #838. Tree-SHA512: 19597a30d117efd33bf76709c3428def9bfa374f356a5e52a81d617794fb985d3e663ed8911422916d4af113f618d276442fdb0e503cf4df6a1a09e04b63a64a
2 parents 49d3c1c + b23efb2 commit 84e74dc

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

src/index/txindex.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ bool TxIndex::Init()
246246
bool TxIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
247247
{
248248
// Exclude genesis block transaction because outputs are not spendable.
249-
if (pindex->nHeight == 0) return true;
249+
//ELEMENTS: we do index the genesis block
250+
//if (pindex->nHeight == 0) return true;
250251

251252
CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
252253
std::vector<std::pair<uint256, CDiskTxPos>> vPos;

src/test/txindex_tests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
4040
}
4141

4242
// Check that txindex excludes genesis block transactions.
43+
//ELEMENTS: we do include them
4344
const CBlock& genesis_block = Params().GenesisBlock();
4445
for (const auto& txn : genesis_block.vtx) {
45-
BOOST_CHECK(!txindex.FindTx(txn->GetHash(), block_hash, tx_disk));
46+
BOOST_CHECK(txindex.FindTx(txn->GetHash(), block_hash, tx_disk));
4647
}
4748

4849
// Check that txindex has all txs that were in the chain before it started.

test/functional/feature_connect_genesis_outputs.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def set_test_params(self):
1616
self.num_nodes = 2
1717
self.setup_clean_chain = True
1818
# First node doesn't connect coinbase output to db, second does
19-
self.extra_args = [["-con_connect_genesis_outputs=0", "-initialfreecoins={}".format(NUM_INITIAL_COINS * COIN)],
19+
self.extra_args = [["-con_connect_genesis_outputs=0", "-initialfreecoins={}".format(NUM_INITIAL_COINS * COIN), '-txindex=1'],
2020
["-con_connect_genesis_outputs=1", "-initialfreecoins={}".format(NUM_INITIAL_COINS * COIN), '-anyonecanspendaremine=1']]
2121

2222
def skip_test_if_missing_module(self):
@@ -49,15 +49,22 @@ def run_test(self):
4949
assert_equal(unspent[0]["amount"], NUM_INITIAL_COINS)
5050
assert_equal(unspent[0]["confirmations"], 1)
5151

52-
# Test rpc getraw functionality
52+
# Test rpc getraw functionality.
5353

54-
# Coinbase transaction is provably unspendable (OP_RETURN), so even AddCoin won't add it
55-
assert_raises_rpc_error(-5, "No such mempool transaction. Use -txindex to enable blockchain transaction queries. Use gettransaction for wallet transactions.", self.nodes[0].getrawtransaction, coinbase_tx)
56-
assert_raises_rpc_error(-5, "No such mempool transaction. Use -txindex to enable blockchain transaction queries. Use gettransaction for wallet transactions.", self.nodes[1].getrawtransaction, coinbase_tx)
54+
# Node 0 has txindex.
55+
self.nodes[0].getrawtransaction(coinbase_tx, False)
56+
self.nodes[0].getrawtransaction(issuance_tx, False)
5757

58-
# Issuance transaction is an OP_TRUE, so will be available to second node
59-
assert_raises_rpc_error(-5, "No such mempool transaction. Use -txindex to enable blockchain transaction queries. Use gettransaction for wallet transactions.", self.nodes[0].getrawtransaction, issuance_tx)
58+
# Node 1 doesn't.
59+
assert_raises_rpc_error(-5, "No such mempool transaction. Use -txindex to enable blockchain transaction queries. Use gettransaction for wallet transactions.", self.nodes[1].getrawtransaction, coinbase_tx)
60+
assert_raises_rpc_error(-5, "No such mempool transaction. Use -txindex to enable blockchain transaction queries. Use gettransaction for wallet transactions.", self.nodes[1].getrawtransaction, issuance_tx)
61+
# But it can still access them by providing the genesis block hash.
62+
self.nodes[1].getrawtransaction(coinbase_tx, False, self.nodes[0].getblockhash(0))
6063
self.nodes[1].getrawtransaction(issuance_tx, False, self.nodes[0].getblockhash(0))
6164

65+
# Because the issuance tx is OP_TRUE, node 1 (with anyonecanspendaremine) has them in the wallet.
66+
assert_raises_rpc_error(-5, "Invalid or non-wallet transaction id", self.nodes[0].gettransaction, issuance_tx)
67+
self.nodes[1].gettransaction(issuance_tx)
68+
6269
if __name__ == '__main__':
6370
ConnectGenesisTest().main()

0 commit comments

Comments
 (0)