Skip to content

Commit

Permalink
Include p2pk into addressindex (dashpay#1839)
Browse files Browse the repository at this point in the history
* Add simple test for p2pk script

refs dashpay#1767

* Add p2pk transaction addresses indexing

Addresses from p2pk transaction are now indexed by transations pubkeys
hashes. Even if it isn't strict behavior and the p2pk are rare
and obsolete, it is transaction that should be able to be looked up.

fixes dashpay#1767

* Add p2pk script checking tests

refs dashpay#1767
  • Loading branch information
wozniakk authored and CryptoCentric committed Feb 26, 2019
1 parent e5bdb82 commit b8c2e88
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ BITCOIN_TESTS =\
test/sanity_tests.cpp \
test/scheduler_tests.cpp \
test/script_P2SH_tests.cpp \
test/script_P2PK_tests.cpp \
test/script_P2PKH_tests.cpp \
test/script_tests.cpp \
test/scriptnum_tests.cpp \
Expand Down
15 changes: 15 additions & 0 deletions src/script/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,21 @@ bool CScript::IsPayToScriptHash() const
(*this)[22] == OP_EQUAL);
}

bool CScript::IsPayToPublicKey() const
{
// Test for pay-to-pubkey CScript with both
// compressed or uncompressed pubkey
if (this->size() == 35) {
return ((*this)[1] == 0x02 || (*this)[1] == 0x03) &&
(*this)[34] == OP_CHECKSIG;
}
if (this->size() == 67) {
return (*this)[1] == 0x04 &&
(*this)[66] == OP_CHECKSIG;
}
return false;
}

bool CScript::IsPushOnly(const_iterator pc) const
{
while (pc < end())
Expand Down
3 changes: 3 additions & 0 deletions src/script/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,9 @@ class CScript : public CScriptBase

bool IsPayToScriptHash() const;

/** Used for obsolete pay-to-pubkey addresses indexing. */
bool IsPayToPublicKey() const;

/** Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical). */
bool IsPushOnly(const_iterator pc) const;
bool IsPushOnly() const;
Expand Down
50 changes: 50 additions & 0 deletions src/test/script_P2PK_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2012-2015 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "script/script.h"
#include "test/test_dash.h"

#include <boost/test/unit_test.hpp>

using namespace std;

BOOST_FIXTURE_TEST_SUITE(script_P2PK_tests, BasicTestingSetup)

BOOST_AUTO_TEST_CASE(IsPayToPublicKey)
{
// Test CScript::IsPayToPublicKey()
static const unsigned char p2pkcompressedeven[] = {
0x41, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, OP_CHECKSIG
};
BOOST_CHECK(CScript(p2pkcompressedeven, p2pkcompressedeven+sizeof(p2pkcompressedeven)).IsPayToPublicKey());

static const unsigned char p2pkcompressedodd[] = {
0x41, 0x03, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, OP_CHECKSIG
};
BOOST_CHECK(CScript(p2pkcompressedodd, p2pkcompressedodd+sizeof(p2pkcompressedodd)).IsPayToPublicKey());

static const unsigned char p2pkuncompressed[] = {
0x41, 0x04, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, OP_CHECKSIG
};
BOOST_CHECK(CScript(p2pkuncompressed, p2pkuncompressed+sizeof(p2pkuncompressed)).IsPayToPublicKey());

static const unsigned char missingop[] = {
0x41, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
BOOST_CHECK(!CScript(missingop, missingop+sizeof(missingop)).IsPayToPublicKey());

static const unsigned char wrongop[] = {
0x41, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, OP_EQUALVERIFY
};
BOOST_CHECK(!CScript(wrongop, wrongop+sizeof(wrongop)).IsPayToPublicKey());

static const unsigned char tooshort[] = {
0x41, 0x02, 0, 0, OP_CHECKSIG
};
BOOST_CHECK(!CScript(tooshort, tooshort+sizeof(tooshort)).IsPayToPublicKey());

}

BOOST_AUTO_TEST_SUITE_END()
15 changes: 15 additions & 0 deletions src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,12 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC
CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
mapAddress.insert(make_pair(key, delta));
inserted.push_back(key);
} else if (prevout.scriptPubKey.IsPayToPublicKey()) {
uint160 hashBytes(Hash160(prevout.scriptPubKey.begin()+1, prevout.scriptPubKey.end()-1));
CMempoolAddressDeltaKey key(1, hashBytes, txhash, j, 1);
CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
mapAddress.insert(make_pair(key, delta));
inserted.push_back(key);
}
}

Expand All @@ -475,6 +481,12 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC
CMempoolAddressDeltaKey key(1, uint160(hashBytes), txhash, k, 0);
mapAddress.insert(make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue)));
inserted.push_back(key);
} else if (out.scriptPubKey.IsPayToPublicKey()) {
uint160 hashBytes(Hash160(out.scriptPubKey.begin()+1, out.scriptPubKey.end()-1));
std::pair<addressDeltaMap::iterator,bool> ret;
CMempoolAddressDeltaKey key(1, hashBytes, txhash, k, 0);
mapAddress.insert(make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue)));
inserted.push_back(key);
}
}

Expand Down Expand Up @@ -532,6 +544,9 @@ void CTxMemPool::addSpentIndex(const CTxMemPoolEntry &entry, const CCoinsViewCac
} else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
addressHash = uint160(vector<unsigned char> (prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23));
addressType = 1;
} else if (prevout.scriptPubKey.IsPayToPublicKey()) {
addressHash = Hash160(prevout.scriptPubKey.begin()+1, prevout.scriptPubKey.end()-1);
addressType = 1;
} else {
addressHash.SetNull();
addressType = 0;
Expand Down
15 changes: 15 additions & 0 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,10 @@ static DisconnectResult DisconnectBlock(const CBlock& block, CValidationState& s
// undo unspent index
addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, uint160(hashBytes), hash, k), CAddressUnspentValue()));

} else if (out.scriptPubKey.IsPayToPublicKey()) {
uint160 hashBytes(Hash160(out.scriptPubKey.begin()+1, out.scriptPubKey.end()-1));
addressIndex.push_back(make_pair(CAddressIndexKey(1, hashBytes, pindex->nHeight, i, hash, k, false), out.nValue));
addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, hashBytes, hash, k), CAddressUnspentValue()));
} else {
continue;
}
Expand Down Expand Up @@ -1847,6 +1851,10 @@ static DisconnectResult DisconnectBlock(const CBlock& block, CValidationState& s
// restore unspent index
addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, uint160(hashBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue(prevout.nValue, prevout.scriptPubKey, undoHeight)));

} else if (prevout.scriptPubKey.IsPayToPublicKey()) {
uint160 hashBytes(Hash160(prevout.scriptPubKey.begin()+1, prevout.scriptPubKey.end()-1));
addressIndex.push_back(make_pair(CAddressIndexKey(1, hashBytes, pindex->nHeight, i, hash, j, false), prevout.nValue));
addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, hashBytes, hash, j), CAddressUnspentValue()));
} else {
continue;
}
Expand Down Expand Up @@ -2171,6 +2179,9 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd
} else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
hashBytes = uint160(vector <unsigned char>(prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23));
addressType = 1;
} else if (prevout.scriptPubKey.IsPayToPublicKey()) {
hashBytes = Hash160(prevout.scriptPubKey.begin()+1, prevout.scriptPubKey.end()-1);
addressType = 1;
} else {
hashBytes.SetNull();
addressType = 0;
Expand Down Expand Up @@ -2236,6 +2247,10 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd
// record unspent output
addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, uint160(hashBytes), txhash, k), CAddressUnspentValue(out.nValue, out.scriptPubKey, pindex->nHeight)));

} else if (out.scriptPubKey.IsPayToPublicKey()) {
uint160 hashBytes(Hash160(out.scriptPubKey.begin()+1, out.scriptPubKey.end()-1));
addressIndex.push_back(make_pair(CAddressIndexKey(1, hashBytes, pindex->nHeight, i, txhash, k, false), out.nValue));
addressUnspentIndex.push_back(make_pair(CAddressUnspentKey(1, hashBytes, txhash, k), CAddressUnspentValue(out.nValue, out.scriptPubKey, pindex->nHeight)));
} else {
continue;
}
Expand Down

0 comments on commit b8c2e88

Please sign in to comment.