From 110adc6aa764d744730f6be2a372f24e3a305054 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 30 Mar 2016 15:12:19 -0400 Subject: [PATCH] main: remove spent address utxo indexes --- qa/rpc-tests/addressindex.py | 8 +++----- src/main.cpp | 18 ++++++++++-------- src/main.h | 23 +++++------------------ src/rpc/misc.cpp | 1 - 4 files changed, 18 insertions(+), 32 deletions(-) diff --git a/qa/rpc-tests/addressindex.py b/qa/rpc-tests/addressindex.py index d051d170a..703f3029a 100755 --- a/qa/rpc-tests/addressindex.py +++ b/qa/rpc-tests/addressindex.py @@ -172,9 +172,8 @@ class AddressIndexTest(BitcoinTestFramework): # Check that unspent outputs can be queried print "Testing utxos..." utxos = self.nodes[1].getaddressutxos({"addresses": [address2]}) - assert_equal(len(utxos), 2) - assert_equal(utxos[0]["satoshis"], 5000000000) - assert_equal(utxos[1]["satoshis"], 4899977160) + assert_equal(len(utxos), 1) + assert_equal(utxos[0]["satoshis"], change_amount) # Check that indexes will be updated with a reorg print "Testing reorg..." @@ -190,9 +189,8 @@ class AddressIndexTest(BitcoinTestFramework): assert_equal(balance4, balance1) utxos2 = self.nodes[1].getaddressutxos({"addresses": [address2]}) - assert_equal(len(utxos2), 2) + assert_equal(len(utxos2), 1) assert_equal(utxos2[0]["satoshis"], 5000000000) - assert_equal(utxos2[1]["satoshis"], 5000000000) print "Passed\n" diff --git a/src/main.cpp b/src/main.cpp index 09a19ae89..25470caee 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2274,7 +2274,7 @@ bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex addressIndex.push_back(make_pair(CAddressIndexKey(2, uint160(hashBytes), pindex->nHeight, i, hash, k, false), out.nValue)); // undo unspent index - addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(2, uint160(hashBytes), pindex->nHeight, i, hash, k), CAddressUnspentValue())); + addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(2, uint160(hashBytes), hash, k), CAddressUnspentValue())); } else if (out.scriptPubKey.IsPayToPublicKeyHash()) { vector hashBytes(out.scriptPubKey.begin()+3, out.scriptPubKey.begin()+23); @@ -2283,7 +2283,7 @@ bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex addressIndex.push_back(make_pair(CAddressIndexKey(1, uint160(hashBytes), pindex->nHeight, i, hash, k, false), out.nValue)); // undo unspent index - addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, uint160(hashBytes), pindex->nHeight, i, hash, k), CAddressUnspentValue())); + addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, uint160(hashBytes), hash, k), CAddressUnspentValue())); } else { continue; @@ -2348,6 +2348,7 @@ bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex std::vector > addressIndex; if (fAddressIndex) { + const CTxIn input = tx.vin[j]; const CTxOut &prevout = view.GetOutputFor(tx.vin[j]); if (prevout.scriptPubKey.IsPayToScriptHash()) { vector hashBytes(prevout.scriptPubKey.begin()+2, prevout.scriptPubKey.begin()+22); @@ -2356,7 +2357,7 @@ bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex addressIndex.push_back(make_pair(CAddressIndexKey(2, uint160(hashBytes), pindex->nHeight, i, hash, j, true), prevout.nValue * -1)); // restore unspent index - addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(2, uint160(hashBytes), pindex->nHeight, i, hash, j), CAddressUnspentValue(prevout.nValue, prevout.scriptPubKey))); + addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(2, uint160(hashBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue(prevout.nValue, prevout.scriptPubKey))); } else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) { @@ -2366,7 +2367,7 @@ bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex addressIndex.push_back(make_pair(CAddressIndexKey(1, uint160(hashBytes), pindex->nHeight, i, hash, j, true), prevout.nValue * -1)); // restore unspent index - addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, uint160(hashBytes), pindex->nHeight, i, hash, j), CAddressUnspentValue(prevout.nValue, prevout.scriptPubKey))); + addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, uint160(hashBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue(prevout.nValue, prevout.scriptPubKey))); } else { continue; @@ -2618,6 +2619,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin if (fAddressIndex) { for (size_t j = 0; j < tx.vin.size(); j++) { + const CTxIn input = tx.vin[j]; const CTxOut &prevout = view.GetOutputFor(tx.vin[j]); if (prevout.scriptPubKey.IsPayToScriptHash()) { vector hashBytes(prevout.scriptPubKey.begin()+2, prevout.scriptPubKey.begin()+22); @@ -2626,7 +2628,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin addressIndex.push_back(make_pair(CAddressIndexKey(2, uint160(hashBytes), pindex->nHeight, i, txhash, j, true), prevout.nValue * -1)); // remove address from unspent index - addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(2, uint160(hashBytes), pindex->nHeight, i, txhash, j), CAddressUnspentValue())); + addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(2, uint160(hashBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue())); } else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) { vector hashBytes(prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23); @@ -2634,7 +2636,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin addressIndex.push_back(make_pair(CAddressIndexKey(1, uint160(hashBytes), pindex->nHeight, i, txhash, j, true), prevout.nValue * -1)); // remove address from unspent index - addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, uint160(hashBytes), pindex->nHeight, i, txhash, j), CAddressUnspentValue())); + addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, uint160(hashBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue())); } else { continue; @@ -2674,7 +2676,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin addressIndex.push_back(make_pair(CAddressIndexKey(2, uint160(hashBytes), pindex->nHeight, i, txhash, k, false), out.nValue)); // record unspent output - addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(2, uint160(hashBytes), pindex->nHeight, i, txhash, k), CAddressUnspentValue(out.nValue, out.scriptPubKey))); + addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(2, uint160(hashBytes), txhash, k), CAddressUnspentValue(out.nValue, out.scriptPubKey))); } else if (out.scriptPubKey.IsPayToPublicKeyHash()) { vector hashBytes(out.scriptPubKey.begin()+3, out.scriptPubKey.begin()+23); @@ -2683,7 +2685,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin addressIndex.push_back(make_pair(CAddressIndexKey(1, uint160(hashBytes), pindex->nHeight, i, txhash, k, false), out.nValue)); // record unspent output - addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, uint160(hashBytes), pindex->nHeight, i, txhash, k), CAddressUnspentValue(out.nValue, out.scriptPubKey))); + addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, uint160(hashBytes), txhash, k), CAddressUnspentValue(out.nValue, out.scriptPubKey))); } else { continue; diff --git a/src/main.h b/src/main.h index 97f7d192e..67a259e9c 100644 --- a/src/main.h +++ b/src/main.h @@ -334,21 +334,16 @@ struct CTimestampIndexKey { struct CAddressUnspentKey { unsigned int type; uint160 hashBytes; - int blockHeight; - unsigned int txindex; uint256 txhash; size_t index; size_t GetSerializeSize(int nType, int nVersion) const { - return 65; + return 57; } template void Serialize(Stream& s, int nType, int nVersion) const { ser_writedata8(s, type); hashBytes.Serialize(s, nType, nVersion); - // Heights are stored big-endian for key sorting in LevelDB - ser_writedata32be(s, blockHeight); - ser_writedata32be(s, txindex); txhash.Serialize(s, nType, nVersion); ser_writedata32(s, index); } @@ -356,18 +351,13 @@ struct CAddressUnspentKey { void Unserialize(Stream& s, int nType, int nVersion) { type = ser_readdata8(s); hashBytes.Unserialize(s, nType, nVersion); - blockHeight = ser_readdata32be(s); - txindex = ser_readdata32be(s); txhash.Unserialize(s, nType, nVersion); index = ser_readdata32(s); } - CAddressUnspentKey(unsigned int addressType, uint160 addressHash, int height, int blockindex, - uint256 txid, size_t indexValue) { + CAddressUnspentKey(unsigned int addressType, uint160 addressHash, uint256 txid, size_t indexValue) { type = addressType; hashBytes = addressHash; - blockHeight = height; - txindex = blockindex; txhash = txid; index = indexValue; } @@ -379,8 +369,6 @@ struct CAddressUnspentKey { void SetNull() { type = 0; hashBytes.SetNull(); - blockHeight = 0; - txindex = 0; txhash.SetNull(); index = 0; } @@ -408,14 +396,13 @@ struct CAddressUnspentValue { } void SetNull() { - satoshis = 0; - script = CScript(); + satoshis = -1; + script.clear(); } bool IsNull() const { - return (satoshis == 0); + return (satoshis == -1); } - }; struct CAddressIndexKey { diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index e321218fd..09956d225 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -586,7 +586,6 @@ UniValue getaddressutxos(const UniValue& params, bool fHelp) output.push_back(Pair("addressType", (int)it->first.type)); output.push_back(Pair("addressHash", it->first.hashBytes.GetHex())); output.push_back(Pair("txid", it->first.txhash.GetHex())); - output.push_back(Pair("height", it->first.blockHeight)); output.push_back(Pair("outputIndex", it->first.index)); output.push_back(Pair("script", HexStr(it->second.script.begin(), it->second.script.end()))); output.push_back(Pair("satoshis", it->second.satoshis));