Skip to content

Commit a793424

Browse files
author
Jeff Garzik
committed
Relay OP_RETURN data TxOut as standard transaction type
1 parent 28f6b8d commit a793424

File tree

5 files changed

+56
-10
lines changed

5 files changed

+56
-10
lines changed

src/main.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,17 +497,28 @@ bool IsStandardTx(const CTransaction& tx, string& reason)
497497
return false;
498498
}
499499
}
500+
501+
unsigned int nDataOut = 0;
502+
txnouttype whichType;
500503
BOOST_FOREACH(const CTxOut& txout, tx.vout) {
501-
if (!::IsStandard(txout.scriptPubKey)) {
504+
if (!::IsStandard(txout.scriptPubKey, whichType)) {
502505
reason = "scriptpubkey";
503506
return false;
504507
}
505-
if (txout.IsDust(CTransaction::nMinRelayTxFee)) {
508+
if (whichType == TX_NULL_DATA)
509+
nDataOut++;
510+
else if (txout.IsDust(CTransaction::nMinRelayTxFee)) {
506511
reason = "dust";
507512
return false;
508513
}
509514
}
510515

516+
// only one OP_RETURN txout is permitted
517+
if (nDataOut > 1) {
518+
reason = "mucho-data";
519+
return false;
520+
}
521+
511522
return true;
512523
}
513524

src/script.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ const char* GetTxnOutputType(txnouttype t)
7979
case TX_PUBKEYHASH: return "pubkeyhash";
8080
case TX_SCRIPTHASH: return "scripthash";
8181
case TX_MULTISIG: return "multisig";
82+
case TX_NULL_DATA: return "nulldata";
8283
}
8384
return NULL;
8485
}
@@ -220,6 +221,7 @@ const char* GetOpName(opcodetype opcode)
220221
// template matching params
221222
case OP_PUBKEYHASH : return "OP_PUBKEYHASH";
222223
case OP_PUBKEY : return "OP_PUBKEY";
224+
case OP_SMALLDATA : return "OP_SMALLDATA";
223225

224226
case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
225227
default:
@@ -1148,6 +1150,9 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, vector<vector<unsi
11481150

11491151
// Sender provides N pubkeys, receivers provides M signatures
11501152
mTemplates.insert(make_pair(TX_MULTISIG, CScript() << OP_SMALLINTEGER << OP_PUBKEYS << OP_SMALLINTEGER << OP_CHECKMULTISIG));
1153+
1154+
// Empty, provably prunable, data-carrying output
1155+
mTemplates.insert(make_pair(TX_NULL_DATA, CScript() << OP_RETURN << OP_SMALLDATA));
11511156
}
11521157

11531158
// Shortcut for pay-to-script-hash, which are more constrained than the other types:
@@ -1232,6 +1237,12 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, vector<vector<unsi
12321237
else
12331238
break;
12341239
}
1240+
else if (opcode2 == OP_SMALLDATA)
1241+
{
1242+
// small pushdata, <= 80 bytes
1243+
if (vch1.size() > 80)
1244+
break;
1245+
}
12351246
else if (opcode1 != opcode2 || vch1 != vch2)
12361247
{
12371248
// Others must match exactly
@@ -1294,6 +1305,7 @@ bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash
12941305
switch (whichTypeRet)
12951306
{
12961307
case TX_NONSTANDARD:
1308+
case TX_NULL_DATA:
12971309
return false;
12981310
case TX_PUBKEY:
12991311
keyID = CPubKey(vSolutions[0]).GetID();
@@ -1325,6 +1337,8 @@ int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned c
13251337
{
13261338
case TX_NONSTANDARD:
13271339
return -1;
1340+
case TX_NULL_DATA:
1341+
return 1;
13281342
case TX_PUBKEY:
13291343
return 1;
13301344
case TX_PUBKEYHASH:
@@ -1339,10 +1353,9 @@ int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned c
13391353
return -1;
13401354
}
13411355

1342-
bool IsStandard(const CScript& scriptPubKey)
1356+
bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType)
13431357
{
13441358
vector<valtype> vSolutions;
1345-
txnouttype whichType;
13461359
if (!Solver(scriptPubKey, whichType, vSolutions))
13471360
return false;
13481361

@@ -1401,6 +1414,7 @@ bool IsMine(const CKeyStore &keystore, const CScript& scriptPubKey)
14011414
switch (whichType)
14021415
{
14031416
case TX_NONSTANDARD:
1417+
case TX_NULL_DATA:
14041418
return false;
14051419
case TX_PUBKEY:
14061420
keyID = CPubKey(vSolutions[0]).GetID();
@@ -1462,6 +1476,8 @@ bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, vecto
14621476
vector<valtype> vSolutions;
14631477
if (!Solver(scriptPubKey, typeRet, vSolutions))
14641478
return false;
1479+
if (typeRet == TX_NULL_DATA)
1480+
return true;
14651481

14661482
if (typeRet == TX_MULTISIG)
14671483
{
@@ -1677,6 +1693,7 @@ static CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo,
16771693
switch (txType)
16781694
{
16791695
case TX_NONSTANDARD:
1696+
case TX_NULL_DATA:
16801697
// Don't know anything about this, assume bigger one is correct:
16811698
if (sigs1.size() >= sigs2.size())
16821699
return PushAll(sigs1);

src/script.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ enum txnouttype
4646
TX_PUBKEYHASH,
4747
TX_SCRIPTHASH,
4848
TX_MULTISIG,
49+
TX_NULL_DATA,
4950
};
5051

5152
class CNoDestination {
@@ -202,6 +203,7 @@ enum opcodetype
202203

203204

204205
// template matching params
206+
OP_SMALLDATA = 0xf9,
205207
OP_SMALLINTEGER = 0xfa,
206208
OP_PUBKEYS = 0xfb,
207209
OP_PUBKEYHASH = 0xfd,
@@ -683,7 +685,7 @@ bool IsCanonicalSignature(const std::vector<unsigned char> &vchSig, unsigned int
683685
bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType);
684686
bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
685687
int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions);
686-
bool IsStandard(const CScript& scriptPubKey);
688+
bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType);
687689
bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
688690
bool IsMine(const CKeyStore& keystore, const CTxDestination &dest);
689691
void ExtractAffectedKeys(const CKeyStore &keystore, const CScript& scriptPubKey, std::vector<CKeyID> &vKeys);

src/test/multisig_tests.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,23 @@ BOOST_AUTO_TEST_CASE(multisig_IsStandard)
133133
for (int i = 0; i < 4; i++)
134134
key[i].MakeNewKey(true);
135135

136+
txnouttype whichType;
137+
136138
CScript a_and_b;
137139
a_and_b << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
138-
BOOST_CHECK(::IsStandard(a_and_b));
140+
BOOST_CHECK(::IsStandard(a_and_b, whichType));
139141

140142
CScript a_or_b;
141143
a_or_b << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
142-
BOOST_CHECK(::IsStandard(a_or_b));
144+
BOOST_CHECK(::IsStandard(a_or_b, whichType));
143145

144146
CScript escrow;
145147
escrow << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << OP_3 << OP_CHECKMULTISIG;
146-
BOOST_CHECK(::IsStandard(escrow));
148+
BOOST_CHECK(::IsStandard(escrow, whichType));
147149

148150
CScript one_of_four;
149151
one_of_four << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << key[3].GetPubKey() << OP_4 << OP_CHECKMULTISIG;
150-
BOOST_CHECK(!::IsStandard(one_of_four));
152+
BOOST_CHECK(!::IsStandard(one_of_four, whichType));
151153

152154
CScript malformed[6];
153155
malformed[0] << OP_3 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG;
@@ -158,7 +160,7 @@ BOOST_AUTO_TEST_CASE(multisig_IsStandard)
158160
malformed[5] << OP_1 << key[0].GetPubKey() << key[1].GetPubKey();
159161

160162
for (int i = 0; i < 6; i++)
161-
BOOST_CHECK(!::IsStandard(malformed[i]));
163+
BOOST_CHECK(!::IsStandard(malformed[i], whichType));
162164
}
163165

164166
BOOST_AUTO_TEST_CASE(multisig_Solver1)

src/test/transaction_tests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,20 @@ BOOST_AUTO_TEST_CASE(test_IsStandard)
273273

274274
t.vout[0].scriptPubKey = CScript() << OP_1;
275275
BOOST_CHECK(!IsStandardTx(t, reason));
276+
277+
// 80-byte TX_NULL_DATA (standard)
278+
t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
279+
BOOST_CHECK(IsStandardTx(t, reason));
280+
281+
// 81-byte TX_NULL_DATA (non-standard)
282+
t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3800");
283+
BOOST_CHECK(!IsStandardTx(t, reason));
284+
285+
// Only one TX_NULL_DATA permitted
286+
t.vout.resize(2);
287+
t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
288+
t.vout[1].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
289+
BOOST_CHECK(!IsStandardTx(t, reason));
276290
}
277291

278292
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)