Skip to content

Commit

Permalink
Merge #838: Index the genesis block with txindex
Browse files Browse the repository at this point in the history
fd4dcf9 Improve coinbase and issuance tx testing for txindex (Steven Roose)
ac9966f Index the genesis block with txindex (Steven Roose)

Pull request description:

  This reverts commit ed12d5d in which Core removed the genesis txs from the txindex.

  Since they are spendable in Elements (as opposed to Core), they should be added to the index as well.

Tree-SHA512: a9067032f80a78f5393437d287022756816a4baa6bfb8fc382d04dc7831a536dc68959a039636aa4b1bb268b2b849e67a0d4ca59abe835c2834ac358f8175e9e
  • Loading branch information
stevenroose committed Mar 24, 2020
2 parents 95602ec + fd4dcf9 commit ac5a435
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/index/txindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ bool TxIndex::Init()
bool TxIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
{
// Exclude genesis block transaction because outputs are not spendable.
if (pindex->nHeight == 0) return true;
//ELEMENTS: we do index the genesis block
//if (pindex->nHeight == 0) return true;

CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
std::vector<std::pair<uint256, CDiskTxPos>> vPos;
Expand Down
3 changes: 2 additions & 1 deletion src/test/txindex_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
}

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

// Check that txindex has all txs that were in the chain before it started.
Expand Down
21 changes: 14 additions & 7 deletions test/functional/feature_connect_genesis_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def set_test_params(self):
self.num_nodes = 2
self.setup_clean_chain = True
# First node doesn't connect coinbase output to db, second does
self.extra_args = [["-con_connect_genesis_outputs=0", "-initialfreecoins={}".format(NUM_INITIAL_COINS * COIN)],
self.extra_args = [["-con_connect_genesis_outputs=0", "-initialfreecoins={}".format(NUM_INITIAL_COINS * COIN), '-txindex=1'],
["-con_connect_genesis_outputs=1", "-initialfreecoins={}".format(NUM_INITIAL_COINS * COIN), '-anyonecanspendaremine=1']]

def skip_test_if_missing_module(self):
Expand Down Expand Up @@ -49,15 +49,22 @@ def run_test(self):
assert_equal(unspent[0]["amount"], NUM_INITIAL_COINS)
assert_equal(unspent[0]["confirmations"], 1)

# Test rpc getraw functionality
# Test rpc getraw functionality.

# Coinbase transaction is provably unspendable (OP_RETURN), so even AddCoin won't add it
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)
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)
# Node 0 has txindex.
self.nodes[0].getrawtransaction(coinbase_tx, False)
self.nodes[0].getrawtransaction(issuance_tx, False)

# Issuance transaction is an OP_TRUE, so will be available to second node
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)
# Node 1 doesn't.
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)
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)
# But it can still access them by providing the genesis block hash.
self.nodes[1].getrawtransaction(coinbase_tx, False, self.nodes[0].getblockhash(0))
self.nodes[1].getrawtransaction(issuance_tx, False, self.nodes[0].getblockhash(0))

# Because the issuance tx is OP_TRUE, node 1 (with anyonecanspendaremine) has them in the wallet.
assert_raises_rpc_error(-5, "Invalid or non-wallet transaction id", self.nodes[0].gettransaction, issuance_tx)
self.nodes[1].gettransaction(issuance_tx)

if __name__ == '__main__':
ConnectGenesisTest().main()

0 comments on commit ac5a435

Please sign in to comment.