rpc: update getaddresstxids for uniqueness
This commit is contained in:
parent
098c127ae6
commit
fd09de01f0
|
@ -9,6 +9,9 @@
|
|||
|
||||
from test_framework.test_framework import BitcoinTestFramework
|
||||
from test_framework.util import *
|
||||
from test_framework.script import *
|
||||
from test_framework.mininode import *
|
||||
import binascii
|
||||
|
||||
class AddressIndexTest(BitcoinTestFramework):
|
||||
|
||||
|
@ -41,6 +44,9 @@ class AddressIndexTest(BitcoinTestFramework):
|
|||
assert_equal(self.nodes[1].getbalance(), 0)
|
||||
assert_equal(self.nodes[2].getbalance(), 0)
|
||||
|
||||
# Check p2pkh and p2sh address indexes
|
||||
print "Testing p2pkh and p2sh address index..."
|
||||
|
||||
txid0 = self.nodes[0].sendtoaddress("mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", 10)
|
||||
txidb0 = self.nodes[0].sendtoaddress("2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br", 10)
|
||||
self.nodes[0].generate(1)
|
||||
|
@ -67,6 +73,28 @@ class AddressIndexTest(BitcoinTestFramework):
|
|||
assert_equal(txidsb[1], txidb1);
|
||||
assert_equal(txidsb[2], txidb2);
|
||||
|
||||
# Check that outputs with the same address will only return one txid
|
||||
print "Testing for txid uniqueness..."
|
||||
addressHash = "6349a418fc4578d10a372b54b45c280cc8c4382f".decode("hex")
|
||||
scriptPubKey = CScript([OP_HASH160, addressHash, OP_EQUAL])
|
||||
unspent = self.nodes[0].listunspent()
|
||||
tx = CTransaction()
|
||||
tx.vin = [CTxIn(COutPoint(int(unspent[0]["txid"], 16), unspent[0]["vout"]))]
|
||||
tx.vout = [CTxOut(10, scriptPubKey), CTxOut(11, scriptPubKey)]
|
||||
tx.rehash()
|
||||
|
||||
signed_tx = self.nodes[0].signrawtransaction(binascii.hexlify(tx.serialize()).decode('utf-8'))
|
||||
sent_txid = self.nodes[0].sendrawtransaction(signed_tx['hex'], True)
|
||||
|
||||
self.nodes[0].generate(1)
|
||||
self.sync_all()
|
||||
|
||||
txidsmany = self.nodes[1].getaddresstxids("2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br");
|
||||
assert_equal(len(txidsmany), 4);
|
||||
assert_equal(txidsmany[3], sent_txid);
|
||||
|
||||
print "Passed\n"
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
AddressIndexTest().main()
|
||||
|
|
|
@ -532,9 +532,15 @@ UniValue getaddresstxids(const UniValue& params, bool fHelp)
|
|||
if (!GetAddressIndex(hashBytes, type, addressIndex))
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
|
||||
|
||||
std::set<std::string> txids;
|
||||
|
||||
UniValue result(UniValue::VARR);
|
||||
for (std::vector<std::pair<CAddressIndexKey, CAmount> >::const_iterator it=addressIndex.begin(); it!=addressIndex.end(); it++)
|
||||
result.push_back(it->first.txhash.GetHex());
|
||||
for (std::vector<std::pair<CAddressIndexKey, CAmount> >::const_iterator it=addressIndex.begin(); it!=addressIndex.end(); it++) {
|
||||
std::string txid = it->first.txhash.GetHex();
|
||||
if (txids.insert(txid).second) {
|
||||
result.push_back(txid);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
|
|
Loading…
Reference in New Issue