main: index address index sorted by height

This commit is contained in:
Braydon Fuller 2016-03-09 11:27:30 -05:00 committed by Simon
parent ffa949e98a
commit a9948959b5
5 changed files with 60 additions and 19 deletions

View File

@ -41,14 +41,19 @@ class AddressIndexTest(BitcoinTestFramework):
assert_equal(self.nodes[1].getbalance(), 0) assert_equal(self.nodes[1].getbalance(), 0)
assert_equal(self.nodes[2].getbalance(), 0) assert_equal(self.nodes[2].getbalance(), 0)
txid1 = self.nodes[0].sendtoaddress("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", 10) txid0 = self.nodes[0].sendtoaddress("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", 10)
txid2 = self.nodes[0].sendtoaddress("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", 15) self.nodes[0].generate(1)
txid3 = self.nodes[0].sendtoaddress("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", 20) 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.nodes[0].generate(1)
self.sync_all() self.sync_all()
txids = self.nodes[1].getaddresstxids("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs"); txids = self.nodes[1].getaddresstxids("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs");
assert_equal(len(txids), 3); assert_equal(len(txids), 3);
assert_equal(txids[0], txid0);
assert_equal(txids[1], txid1);
assert_equal(txids[2], txid2);
if __name__ == '__main__': if __name__ == '__main__':
AddressIndexTest().main() AddressIndexTest().main()

View File

@ -2515,10 +2515,10 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
const CTxOut &prevout = view.GetOutputFor(tx.vin[j]); const CTxOut &prevout = view.GetOutputFor(tx.vin[j]);
if (prevout.scriptPubKey.IsPayToScriptHash()) { if (prevout.scriptPubKey.IsPayToScriptHash()) {
vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+2, prevout.scriptPubKey.begin()+22); vector<unsigned char> 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()) { } else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23); vector<unsigned char> 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 { } else {
continue; continue;
} }
@ -2552,10 +2552,10 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
if (out.scriptPubKey.IsPayToScriptHash()) { if (out.scriptPubKey.IsPayToScriptHash()) {
vector<unsigned char> hashBytes(out.scriptPubKey.begin()+2, out.scriptPubKey.begin()+22); vector<unsigned char> 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()) { } else if (out.scriptPubKey.IsPayToPublicKeyHash()) {
vector<unsigned char> hashBytes(out.scriptPubKey.begin()+3, out.scriptPubKey.begin()+23); vector<unsigned char> 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 { } else {
continue; continue;
} }
@ -2563,7 +2563,6 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
} }
} }
CTxUndo undoDummy; CTxUndo undoDummy;
if (i > 0) { if (i > 0) {
blockundo.vtxundo.push_back(CTxUndo()); blockundo.vtxundo.push_back(CTxUndo());

View File

@ -270,26 +270,33 @@ struct CNodeStateStats {
}; };
struct CAddressIndexKey { struct CAddressIndexKey {
uint160 hashBytes;
unsigned int type; unsigned int type;
uint160 hashBytes;
int blockHeight;
unsigned int txindex;
uint256 txhash; uint256 txhash;
size_t index; size_t outindex;
ADD_SERIALIZE_METHODS; ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(hashBytes);
READWRITE(type); READWRITE(type);
READWRITE(hashBytes);
READWRITE(blockHeight);
READWRITE(txindex);
READWRITE(txhash); READWRITE(txhash);
READWRITE(index); READWRITE(outindex);
} }
CAddressIndexKey(uint160 addressHash, unsigned int addressType, uint256 txid, size_t txindex) { CAddressIndexKey(unsigned int addressType, uint160 addressHash, int height, int blockindex,
hashBytes = addressHash; uint256 txid, size_t outputIndex) {
type = addressType; type = addressType;
hashBytes = addressHash;
blockHeight = height;
txindex = blockindex;
txhash = txid; txhash = txid;
index = txindex; outindex = outputIndex;
} }
CAddressIndexKey() { CAddressIndexKey() {
@ -297,14 +304,43 @@ struct CAddressIndexKey {
} }
void SetNull() { void SetNull() {
hashBytes.SetNull();
type = 0; type = 0;
hashBytes.SetNull();
blockHeight = 0;
txindex = 0;
txhash.SetNull(); txhash.SetNull();
index = 0; outindex = 0;
} }
}; };
struct CAddressIndexIteratorKey {
unsigned int type;
uint160 hashBytes;
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
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 struct CDiskTxPos : public CDiskBlockPos
{ {
unsigned int nTxOffset; // after header unsigned int nTxOffset; // after header

View File

@ -305,7 +305,7 @@ bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, int type, std::vector<s
boost::scoped_ptr<CDBIterator> pcursor(NewIterator()); boost::scoped_ptr<CDBIterator> pcursor(NewIterator());
pcursor->Seek(make_pair(DB_ADDRESSINDEX, addressHash)); //TODO include type pcursor->Seek(make_pair(DB_ADDRESSINDEX, CAddressIndexIteratorKey(type, addressHash)));
while (pcursor->Valid()) { while (pcursor->Valid()) {
boost::this_thread::interruption_point(); boost::this_thread::interruption_point();

View File

@ -18,6 +18,7 @@ class CBlockFileInfo;
class CBlockIndex; class CBlockIndex;
struct CDiskTxPos; struct CDiskTxPos;
struct CAddressIndexKey; struct CAddressIndexKey;
struct CAddressIndexIteratorKey;
class uint256; class uint256;
//! -dbcache default (MiB) //! -dbcache default (MiB)