Skip to content

Commit

Permalink
consensus: correct verification of transactions pre p2sh-asset activa…
Browse files Browse the repository at this point in the history
…tion (#1019)

Handle pre-fork padded P2SH-ish transactions like v4.3.2.1.
  • Loading branch information
fdoving authored Jun 12, 2021
1 parent 8037ac9 commit 46aad1a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
16 changes: 9 additions & 7 deletions src/consensus/tx_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,17 +653,19 @@ bool Consensus::CheckTxAssets(const CTransaction& tx, CValidationState& state, c
int i = 0;
for (const auto& txout : tx.vout) {
i++;
bool fIsAsset = false;

// Values are subject to change, by isAssetScript a few lines down.
int nType = 0;
int nScriptType = 0;
int nStart = 0;
bool fIsOwner = false;
if (txout.scriptPubKey.IsAssetScript(nType, nScriptType, fIsOwner))
fIsAsset = true;

if (fIsAsset && nScriptType == TX_SCRIPTHASH) {
if (!AreP2SHAssetsAllowed())
return state.DoS(0, false, REJECT_INVALID, "bad-txns-p2sh-assets-not-active");
}
// False until BIP9 consensus activates P2SH for Assets.
bool fP2Active = AreP2SHAssetsAllowed();

// Returns true if operations on assets are found in the script.
// It will also possibly change the values of the arguments, as they are passed by reference.
bool fIsAsset = txout.scriptPubKey.IsAssetScript(nType, nScriptType, fIsOwner, nStart, fP2Active);

if (assetCache) {
if (fIsAsset && !AreAssetsDeployed())
Expand Down
8 changes: 6 additions & 2 deletions src/script/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,15 @@ bool CScript::IsAssetScript(int& nType, int& nScriptType, bool& isOwner) const
return IsAssetScript(nType, nScriptType, isOwner, start);
}

bool CScript::IsAssetScript(int& nType, int& nScriptType, bool& fIsOwner, int& nStartingIndex) const
bool CScript::IsAssetScript(int& nType, int& nScriptType, bool& fIsOwner, int& nStartingIndex, bool nP2Active) const
{
if (this->size() > 31) {
// Extra-fast test for pay-to-script-hash CScripts:
if ( (*this)[0] == OP_HASH160 && (*this)[1] == 0x14 && (*this)[22] == OP_EQUAL) {
if ( (*this)[0] == OP_HASH160
&& (*this)[1] == 0x14
&& (*this)[22] == OP_EQUAL
&& nP2Active == true
) {

// If this is of the P2SH type, we need to return this type so we know how to interact and solve it
nScriptType = TX_SCRIPTHASH;
Expand Down
2 changes: 1 addition & 1 deletion src/script/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ class CScript : public CScriptBase
bool IsAssetScript() const;
bool IsAssetScript(int& nType, bool& fIsOwner) const;
bool IsAssetScript(int& nType, int& nScriptType, bool& fIsOwner) const;
bool IsAssetScript(int& nTXType, int& nScriptType, bool& fIsOwner, int& nStartingIndex) const;
bool IsAssetScript(int& nTXType, int& nScriptType, bool& fIsOwner, int& nStartingIndex, bool nP2Active = true) const;
bool IsP2SHAssetScript() const;
bool IsNewAsset() const;
bool IsOwnerAsset() const;
Expand Down

0 comments on commit 46aad1a

Please sign in to comment.