Skip to content

Commit c01f783

Browse files
author
Braydon Fuller
committed
mempool: same address and index for an input and output bug
fixes a bug that would happen when an output would match an input with the same address and index, and would lead to the outputs not appearing in results.
1 parent 4dcf3e8 commit c01f783

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

qa/rpc-tests/addressindex.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,34 @@ def run_test(self):
293293
assert_equal(mempool3[1]["prevtxid"], memtxid2)
294294
assert_equal(mempool3[1]["prevout"], 1)
295295

296+
# sending and receiving to the same address
297+
privkey1 = "cQY2s58LhzUCmEXN8jtAp1Etnijx78YRZ466w4ikX1V4UpTpbsf8"
298+
address1 = "myAUWSHnwsQrhuMWv4Br6QsCnpB41vFwHn"
299+
address1hash = "c192bff751af8efec15135d42bfeedf91a6f3e34".decode("hex")
300+
address1script = CScript([OP_DUP, OP_HASH160, address1hash, OP_EQUALVERIFY, OP_CHECKSIG])
301+
302+
self.nodes[0].sendtoaddress(address1, 10)
303+
self.nodes[0].generate(1)
304+
self.sync_all()
305+
306+
utxos = self.nodes[1].getaddressutxos({"addresses": [address1]})
307+
assert_equal(len(utxos), 1)
308+
309+
tx = CTransaction()
310+
tx.vin = [
311+
CTxIn(COutPoint(int(utxos[0]["txid"], 16), utxos[0]["outputIndex"]))
312+
]
313+
amount = utxos[0]["satoshis"] - 1000
314+
tx.vout = [CTxOut(amount, address1script)]
315+
tx.rehash()
316+
self.nodes[0].importprivkey(privkey1)
317+
signed_tx = self.nodes[0].signrawtransaction(binascii.hexlify(tx.serialize()).decode("utf-8"))
318+
mem_txid = self.nodes[0].sendrawtransaction(signed_tx["hex"], True)
319+
320+
self.sync_all()
321+
mempool_deltas = self.nodes[2].getaddressmempool({"addresses": [address1]})
322+
assert_equal(len(mempool_deltas), 2)
323+
296324
print "Passed\n"
297325

298326

src/addressindex.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ struct CMempoolAddressDeltaKey
3737
uint160 addressBytes;
3838
uint256 txhash;
3939
unsigned int index;
40-
bool spending;
40+
int spending;
4141

42-
CMempoolAddressDeltaKey(int addressType, uint160 addressHash, uint256 hash, unsigned int i, bool s) {
42+
CMempoolAddressDeltaKey(int addressType, uint160 addressHash, uint256 hash, unsigned int i, int s) {
4343
type = addressType;
4444
addressBytes = addressHash;
4545
txhash = hash;
@@ -52,6 +52,7 @@ struct CMempoolAddressDeltaKey
5252
addressBytes = addressHash;
5353
txhash.SetNull();
5454
index = 0;
55+
spending = 0;
5556
}
5657
};
5758

@@ -61,7 +62,11 @@ struct CMempoolAddressDeltaKeyCompare
6162
if (a.type == b.type) {
6263
if (a.addressBytes == b.addressBytes) {
6364
if (a.txhash == b.txhash) {
64-
return a.index < b.index;
65+
if (a.index == b.index) {
66+
return a.spending < b.spending;
67+
} else {
68+
return a.index < b.index;
69+
}
6570
} else {
6671
return a.txhash < b.txhash;
6772
}

src/txmempool.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,13 +434,13 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC
434434
const CTxOut &prevout = view.GetOutputFor(input);
435435
if (prevout.scriptPubKey.IsPayToScriptHash()) {
436436
vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+2, prevout.scriptPubKey.begin()+22);
437-
CMempoolAddressDeltaKey key(2, uint160(hashBytes), txhash, j, true);
437+
CMempoolAddressDeltaKey key(2, uint160(hashBytes), txhash, j, 1);
438438
CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
439439
mapAddress.insert(make_pair(key, delta));
440440
inserted.push_back(key);
441441
} else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
442442
vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23);
443-
CMempoolAddressDeltaKey key(1, uint160(hashBytes), txhash, j, true);
443+
CMempoolAddressDeltaKey key(1, uint160(hashBytes), txhash, j, 1);
444444
CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
445445
mapAddress.insert(make_pair(key, delta));
446446
inserted.push_back(key);
@@ -451,13 +451,13 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC
451451
const CTxOut &out = tx.vout[k];
452452
if (out.scriptPubKey.IsPayToScriptHash()) {
453453
vector<unsigned char> hashBytes(out.scriptPubKey.begin()+2, out.scriptPubKey.begin()+22);
454-
CMempoolAddressDeltaKey key(2, uint160(hashBytes), txhash, k, false);
454+
CMempoolAddressDeltaKey key(2, uint160(hashBytes), txhash, k, 0);
455455
mapAddress.insert(make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue)));
456456
inserted.push_back(key);
457457
} else if (out.scriptPubKey.IsPayToPublicKeyHash()) {
458458
vector<unsigned char> hashBytes(out.scriptPubKey.begin()+3, out.scriptPubKey.begin()+23);
459459
std::pair<addressDeltaMap::iterator,bool> ret;
460-
CMempoolAddressDeltaKey key(1, uint160(hashBytes), txhash, k, false);
460+
CMempoolAddressDeltaKey key(1, uint160(hashBytes), txhash, k, 0);
461461
mapAddress.insert(make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue)));
462462
inserted.push_back(key);
463463
}

0 commit comments

Comments
 (0)