From 206882cd4b7a93c08579134986814b76c49564e2 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 23 Mar 2016 14:41:08 -0400 Subject: [PATCH] main: fixed bug with overlapping address index keys There was a bug where the spending address index could have the same key as the receiving address index if the input and output indexes matched. This lead to the output always overwriting the input index leading to incorrect balances with missing spent amounts. This patch separates the two so that they have unique keys so balances will be correctly calculated. --- qa/rpc-tests/addressindex.py | 2 +- src/main.cpp | 8 ++++---- src/main.h | 9 ++++++++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/qa/rpc-tests/addressindex.py b/qa/rpc-tests/addressindex.py index 59837f783..0ffbcffd7 100755 --- a/qa/rpc-tests/addressindex.py +++ b/qa/rpc-tests/addressindex.py @@ -145,7 +145,7 @@ class AddressIndexTest(BitcoinTestFramework): tx.vin = [CTxIn(COutPoint(int(spending_txid, 16), 0))] send_amount = 1 * 100000000 + 12840 change_amount = amount - send_amount - 10000 - tx.vout = [CTxOut(send_amount, scriptPubKey), CTxOut(change_amount, scriptPubKey2)] + tx.vout = [CTxOut(change_amount, scriptPubKey2), CTxOut(send_amount, scriptPubKey)] tx.rehash() signed_tx = self.nodes[0].signrawtransaction(binascii.hexlify(tx.serialize()).decode("utf-8")) diff --git a/src/main.cpp b/src/main.cpp index de65db40a..3826bc198 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2384,10 +2384,10 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin const CTxOut &prevout = view.GetOutputFor(tx.vin[j]); if (prevout.scriptPubKey.IsPayToScriptHash()) { vector hashBytes(prevout.scriptPubKey.begin()+2, prevout.scriptPubKey.begin()+22); - addressIndex.push_back(make_pair(CAddressIndexKey(2, uint160(hashBytes), pindex->nHeight, i, txhash, j), prevout.nValue * -1)); + addressIndex.push_back(make_pair(CAddressIndexKey(2, uint160(hashBytes), pindex->nHeight, i, txhash, j, true), prevout.nValue * -1)); } else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) { vector hashBytes(prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23); - addressIndex.push_back(make_pair(CAddressIndexKey(1, uint160(hashBytes), pindex->nHeight, i, txhash, j), prevout.nValue * -1)); + addressIndex.push_back(make_pair(CAddressIndexKey(1, uint160(hashBytes), pindex->nHeight, i, txhash, j, true), prevout.nValue * -1)); } else { continue; } @@ -2421,10 +2421,10 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin if (out.scriptPubKey.IsPayToScriptHash()) { vector hashBytes(out.scriptPubKey.begin()+2, out.scriptPubKey.begin()+22); - addressIndex.push_back(make_pair(CAddressIndexKey(2, uint160(hashBytes), pindex->nHeight, i, txhash, k), out.nValue)); + addressIndex.push_back(make_pair(CAddressIndexKey(2, uint160(hashBytes), pindex->nHeight, i, txhash, k, false), out.nValue)); } else if (out.scriptPubKey.IsPayToPublicKeyHash()) { vector hashBytes(out.scriptPubKey.begin()+3, out.scriptPubKey.begin()+23); - addressIndex.push_back(make_pair(CAddressIndexKey(1, uint160(hashBytes), pindex->nHeight, i, txhash, k), out.nValue)); + addressIndex.push_back(make_pair(CAddressIndexKey(1, uint160(hashBytes), pindex->nHeight, i, txhash, k, false), out.nValue)); } else { continue; } diff --git a/src/main.h b/src/main.h index fe0a3dd10..3378b24df 100644 --- a/src/main.h +++ b/src/main.h @@ -360,6 +360,7 @@ struct CAddressIndexKey { unsigned int txindex; uint256 txhash; size_t outindex; + bool spending; size_t GetSerializeSize(int nType, int nVersion) const { return 65; @@ -373,6 +374,8 @@ struct CAddressIndexKey { ser_writedata32be(s, txindex); txhash.Serialize(s, nType, nVersion); ser_writedata32(s, outindex); + char f = spending; + ser_writedata8(s, f); } template void Unserialize(Stream& s, int nType, int nVersion) { @@ -382,16 +385,19 @@ struct CAddressIndexKey { txindex = ser_readdata32be(s); txhash.Unserialize(s, nType, nVersion); outindex = ser_readdata32(s); + char f = ser_readdata8(s); + spending = f; } CAddressIndexKey(unsigned int addressType, uint160 addressHash, int height, int blockindex, - uint256 txid, size_t outputIndex) { + uint256 txid, size_t outputIndex, bool isSpending) { type = addressType; hashBytes = addressHash; blockHeight = height; txindex = blockindex; txhash = txid; outindex = outputIndex; + spending = isSpending; } CAddressIndexKey() { @@ -405,6 +411,7 @@ struct CAddressIndexKey { txindex = 0; txhash.SetNull(); outindex = 0; + spending = false; } };