diff --git a/qa/rpc-tests/addressindex.py b/qa/rpc-tests/addressindex.py index c4353d92a6..f3cbc2ef84 100755 --- a/qa/rpc-tests/addressindex.py +++ b/qa/rpc-tests/addressindex.py @@ -41,14 +41,19 @@ class AddressIndexTest(BitcoinTestFramework): assert_equal(self.nodes[1].getbalance(), 0) assert_equal(self.nodes[2].getbalance(), 0) - txid1 = self.nodes[0].sendtoaddress("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", 10) - txid2 = self.nodes[0].sendtoaddress("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", 15) - txid3 = self.nodes[0].sendtoaddress("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", 20) + txid0 = self.nodes[0].sendtoaddress("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", 10) + self.nodes[0].generate(1) + txid1 = self.nodes[0].sendtoaddress("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", 15) + self.nodes[0].generate(1) + txid2 = self.nodes[0].sendtoaddress("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", 20) self.nodes[0].generate(1) self.sync_all() txids = self.nodes[1].getaddresstxids("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs"); assert_equal(len(txids), 3); + assert_equal(txids[0], txid0); + assert_equal(txids[1], txid1); + assert_equal(txids[2], txid2); if __name__ == '__main__': AddressIndexTest().main() diff --git a/src/main.cpp b/src/main.cpp index adad65640b..0ad52abbb6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2372,10 +2372,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(uint160(hashBytes), 2, txhash, j), prevout.nValue * -1)); + addressIndex.push_back(make_pair(CAddressIndexKey(2, uint160(hashBytes), pindex->nHeight, i, txhash, j), prevout.nValue * -1)); } else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) { vector hashBytes(prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23); - addressIndex.push_back(make_pair(CAddressIndexKey(uint160(hashBytes), 1, txhash, j), prevout.nValue * -1)); + addressIndex.push_back(make_pair(CAddressIndexKey(1, uint160(hashBytes), pindex->nHeight, i, txhash, j), prevout.nValue * -1)); } else { continue; } @@ -2409,10 +2409,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(uint160(hashBytes), 2, txhash, k), out.nValue)); + addressIndex.push_back(make_pair(CAddressIndexKey(2, uint160(hashBytes), pindex->nHeight, i, txhash, k), out.nValue)); } else if (out.scriptPubKey.IsPayToPublicKeyHash()) { vector hashBytes(out.scriptPubKey.begin()+3, out.scriptPubKey.begin()+23); - addressIndex.push_back(make_pair(CAddressIndexKey(uint160(hashBytes), 1, txhash, k), out.nValue)); + addressIndex.push_back(make_pair(CAddressIndexKey(1, uint160(hashBytes), pindex->nHeight, i, txhash, k), out.nValue)); } else { continue; } @@ -2420,7 +2420,6 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin } } - CTxUndo undoDummy; if (i > 0) { blockundo.vtxundo.push_back(CTxUndo()); diff --git a/src/main.h b/src/main.h index 159099036d..c2196fcbd7 100644 --- a/src/main.h +++ b/src/main.h @@ -292,26 +292,33 @@ struct CNodeStateStats { }; struct CAddressIndexKey { - uint160 hashBytes; unsigned int type; + uint160 hashBytes; + int blockHeight; + unsigned int txindex; uint256 txhash; - size_t index; + size_t outindex; ADD_SERIALIZE_METHODS; template inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - READWRITE(hashBytes); READWRITE(type); + READWRITE(hashBytes); + READWRITE(blockHeight); + READWRITE(txindex); READWRITE(txhash); - READWRITE(index); + READWRITE(outindex); } - CAddressIndexKey(uint160 addressHash, unsigned int addressType, uint256 txid, size_t txindex) { - hashBytes = addressHash; + CAddressIndexKey(unsigned int addressType, uint160 addressHash, int height, int blockindex, + uint256 txid, size_t outputIndex) { type = addressType; + hashBytes = addressHash; + blockHeight = height; + txindex = blockindex; txhash = txid; - index = txindex; + outindex = outputIndex; } CAddressIndexKey() { @@ -319,14 +326,43 @@ struct CAddressIndexKey { } void SetNull() { - hashBytes.SetNull(); type = 0; + hashBytes.SetNull(); + blockHeight = 0; + txindex = 0; txhash.SetNull(); - index = 0; + outindex = 0; } }; +struct CAddressIndexIteratorKey { + unsigned int type; + uint160 hashBytes; + + ADD_SERIALIZE_METHODS; + + template + inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + READWRITE(type); + READWRITE(hashBytes); + } + + CAddressIndexIteratorKey(unsigned int addressType, uint160 addressHash) { + type = addressType; + hashBytes = addressHash; + } + + CAddressIndexIteratorKey() { + SetNull(); + } + + void SetNull() { + type = 0; + hashBytes.SetNull(); + } +}; + struct CDiskTxPos : public CDiskBlockPos { unsigned int nTxOffset; // after header diff --git a/src/txdb.cpp b/src/txdb.cpp index 153d7b84c9..e93594b9ed 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -167,7 +167,7 @@ bool CBlockTreeDB::WriteTxIndex(const std::vector bool CBlockTreeDB::WriteAddressIndex(const std::vector >&vect) { CDBBatch batch(&GetObfuscateKey()); for (std::vector >::const_iterator it=vect.begin(); it!=vect.end(); it++) - batch.Write(make_pair(DB_ADDRESSINDEX, it->first), it->second); + batch.Write(make_pair(DB_ADDRESSINDEX, it->first), it->second); return WriteBatch(batch); } @@ -175,7 +175,7 @@ bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, int type, std::vector pcursor(NewIterator()); - pcursor->Seek(make_pair(DB_ADDRESSINDEX, addressHash)); //TODO include type + pcursor->Seek(make_pair(DB_ADDRESSINDEX, CAddressIndexIteratorKey(type, addressHash))); while (pcursor->Valid()) { boost::this_thread::interruption_point(); diff --git a/src/txdb.h b/src/txdb.h index 3d2ace581a..ca32869ccc 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -18,6 +18,7 @@ class CBlockFileInfo; class CBlockIndex; struct CDiskTxPos; struct CAddressIndexKey; +struct CAddressIndexIteratorKey; class uint256; //! -dbcache default (MiB)