Skip to content

Commit 6a7456a

Browse files
committed
[MOVEONLY] Move CSCript::FindAndDelete to interpreter
1 parent 33a8ecf commit 6a7456a

File tree

5 files changed

+49
-47
lines changed

5 files changed

+49
-47
lines changed

src/script/interpreter.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,34 @@ bool static CheckMinimalPush(const valtype& data, opcodetype opcode) {
250250
return true;
251251
}
252252

253+
int FindAndDelete(CScript& script, const CScript& b)
254+
{
255+
int nFound = 0;
256+
if (b.empty())
257+
return nFound;
258+
CScript result;
259+
CScript::const_iterator pc = script.begin(), pc2 = script.begin(), end = script.end();
260+
opcodetype opcode;
261+
do
262+
{
263+
result.insert(result.end(), pc2, pc);
264+
while (static_cast<size_t>(end - pc) >= b.size() && std::equal(b.begin(), b.end(), pc))
265+
{
266+
pc = pc + b.size();
267+
++nFound;
268+
}
269+
pc2 = pc;
270+
}
271+
while (script.GetOp(pc, opcode));
272+
273+
if (nFound > 0) {
274+
result.insert(result.end(), pc2, end);
275+
script = std::move(result);
276+
}
277+
278+
return nFound;
279+
}
280+
253281
bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, unsigned int flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptError* serror)
254282
{
255283
static const CScriptNum bnZero(0);
@@ -891,7 +919,7 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
891919

892920
// Drop the signature in pre-segwit scripts but not segwit scripts
893921
if (sigversion == SigVersion::BASE) {
894-
scriptCode.FindAndDelete(CScript(vchSig));
922+
FindAndDelete(scriptCode, CScript(vchSig));
895923
}
896924

897925
if (!CheckSignatureEncoding(vchSig, flags, serror) || !CheckPubKeyEncoding(vchPubKey, flags, sigversion, serror)) {
@@ -955,7 +983,7 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
955983
{
956984
valtype& vchSig = stacktop(-isig-k);
957985
if (sigversion == SigVersion::BASE) {
958-
scriptCode.FindAndDelete(CScript(vchSig));
986+
FindAndDelete(scriptCode, CScript(vchSig));
959987
}
960988
}
961989

src/script/interpreter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,6 @@ bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const C
189189

190190
size_t CountWitnessSigOps(const CScript& scriptSig, const CScript& scriptPubKey, const CScriptWitness* witness, unsigned int flags);
191191

192+
int FindAndDelete(CScript& script, const CScript& b);
193+
192194
#endif // BITCOIN_SCRIPT_INTERPRETER_H

src/script/script.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -571,34 +571,6 @@ class CScript : public CScriptBase
571571
return (opcodetype)(OP_1+n-1);
572572
}
573573

574-
int FindAndDelete(const CScript& b)
575-
{
576-
int nFound = 0;
577-
if (b.empty())
578-
return nFound;
579-
CScript result;
580-
const_iterator pc = begin(), pc2 = begin(), end = this->end();
581-
opcodetype opcode;
582-
do
583-
{
584-
result.insert(result.end(), pc2, pc);
585-
while (static_cast<size_t>(end - pc) >= b.size() && std::equal(b.begin(), b.end(), pc))
586-
{
587-
pc = pc + b.size();
588-
++nFound;
589-
}
590-
pc2 = pc;
591-
}
592-
while (GetOp(pc, opcode));
593-
594-
if (nFound > 0) {
595-
result.insert(result.end(), pc2, end);
596-
*this = result;
597-
}
598-
599-
return nFound;
600-
}
601-
602574
/**
603575
* Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
604576
* as 20 sigops. With pay-to-script-hash, that changed:

src/test/script_tests.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,102 +1349,102 @@ BOOST_AUTO_TEST_CASE(script_FindAndDelete)
13491349
s = CScript() << OP_1 << OP_2;
13501350
d = CScript(); // delete nothing should be a no-op
13511351
expect = s;
1352-
BOOST_CHECK_EQUAL(s.FindAndDelete(d), 0);
1352+
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0);
13531353
BOOST_CHECK(s == expect);
13541354

13551355
s = CScript() << OP_1 << OP_2 << OP_3;
13561356
d = CScript() << OP_2;
13571357
expect = CScript() << OP_1 << OP_3;
1358-
BOOST_CHECK_EQUAL(s.FindAndDelete(d), 1);
1358+
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
13591359
BOOST_CHECK(s == expect);
13601360

13611361
s = CScript() << OP_3 << OP_1 << OP_3 << OP_3 << OP_4 << OP_3;
13621362
d = CScript() << OP_3;
13631363
expect = CScript() << OP_1 << OP_4;
1364-
BOOST_CHECK_EQUAL(s.FindAndDelete(d), 4);
1364+
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 4);
13651365
BOOST_CHECK(s == expect);
13661366

13671367
s = ScriptFromHex("0302ff03"); // PUSH 0x02ff03 onto stack
13681368
d = ScriptFromHex("0302ff03");
13691369
expect = CScript();
1370-
BOOST_CHECK_EQUAL(s.FindAndDelete(d), 1);
1370+
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
13711371
BOOST_CHECK(s == expect);
13721372

13731373
s = ScriptFromHex("0302ff030302ff03"); // PUSH 0x2ff03 PUSH 0x2ff03
13741374
d = ScriptFromHex("0302ff03");
13751375
expect = CScript();
1376-
BOOST_CHECK_EQUAL(s.FindAndDelete(d), 2);
1376+
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 2);
13771377
BOOST_CHECK(s == expect);
13781378

13791379
s = ScriptFromHex("0302ff030302ff03");
13801380
d = ScriptFromHex("02");
13811381
expect = s; // FindAndDelete matches entire opcodes
1382-
BOOST_CHECK_EQUAL(s.FindAndDelete(d), 0);
1382+
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0);
13831383
BOOST_CHECK(s == expect);
13841384

13851385
s = ScriptFromHex("0302ff030302ff03");
13861386
d = ScriptFromHex("ff");
13871387
expect = s;
1388-
BOOST_CHECK_EQUAL(s.FindAndDelete(d), 0);
1388+
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0);
13891389
BOOST_CHECK(s == expect);
13901390

13911391
// This is an odd edge case: strip of the push-three-bytes
13921392
// prefix, leaving 02ff03 which is push-two-bytes:
13931393
s = ScriptFromHex("0302ff030302ff03");
13941394
d = ScriptFromHex("03");
13951395
expect = CScript() << ParseHex("ff03") << ParseHex("ff03");
1396-
BOOST_CHECK_EQUAL(s.FindAndDelete(d), 2);
1396+
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 2);
13971397
BOOST_CHECK(s == expect);
13981398

13991399
// Byte sequence that spans multiple opcodes:
14001400
s = ScriptFromHex("02feed5169"); // PUSH(0xfeed) OP_1 OP_VERIFY
14011401
d = ScriptFromHex("feed51");
14021402
expect = s;
1403-
BOOST_CHECK_EQUAL(s.FindAndDelete(d), 0); // doesn't match 'inside' opcodes
1403+
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0); // doesn't match 'inside' opcodes
14041404
BOOST_CHECK(s == expect);
14051405

14061406
s = ScriptFromHex("02feed5169"); // PUSH(0xfeed) OP_1 OP_VERIFY
14071407
d = ScriptFromHex("02feed51");
14081408
expect = ScriptFromHex("69");
1409-
BOOST_CHECK_EQUAL(s.FindAndDelete(d), 1);
1409+
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
14101410
BOOST_CHECK(s == expect);
14111411

14121412
s = ScriptFromHex("516902feed5169");
14131413
d = ScriptFromHex("feed51");
14141414
expect = s;
1415-
BOOST_CHECK_EQUAL(s.FindAndDelete(d), 0);
1415+
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0);
14161416
BOOST_CHECK(s == expect);
14171417

14181418
s = ScriptFromHex("516902feed5169");
14191419
d = ScriptFromHex("02feed51");
14201420
expect = ScriptFromHex("516969");
1421-
BOOST_CHECK_EQUAL(s.FindAndDelete(d), 1);
1421+
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
14221422
BOOST_CHECK(s == expect);
14231423

14241424
s = CScript() << OP_0 << OP_0 << OP_1 << OP_1;
14251425
d = CScript() << OP_0 << OP_1;
14261426
expect = CScript() << OP_0 << OP_1; // FindAndDelete is single-pass
1427-
BOOST_CHECK_EQUAL(s.FindAndDelete(d), 1);
1427+
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
14281428
BOOST_CHECK(s == expect);
14291429

14301430
s = CScript() << OP_0 << OP_0 << OP_1 << OP_0 << OP_1 << OP_1;
14311431
d = CScript() << OP_0 << OP_1;
14321432
expect = CScript() << OP_0 << OP_1; // FindAndDelete is single-pass
1433-
BOOST_CHECK_EQUAL(s.FindAndDelete(d), 2);
1433+
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 2);
14341434
BOOST_CHECK(s == expect);
14351435

14361436
// Another weird edge case:
14371437
// End with invalid push (not enough data)...
14381438
s = ScriptFromHex("0003feed");
14391439
d = ScriptFromHex("03feed"); // ... can remove the invalid push
14401440
expect = ScriptFromHex("00");
1441-
BOOST_CHECK_EQUAL(s.FindAndDelete(d), 1);
1441+
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
14421442
BOOST_CHECK(s == expect);
14431443

14441444
s = ScriptFromHex("0003feed");
14451445
d = ScriptFromHex("00");
14461446
expect = ScriptFromHex("03feed");
1447-
BOOST_CHECK_EQUAL(s.FindAndDelete(d), 1);
1447+
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
14481448
BOOST_CHECK(s == expect);
14491449
}
14501450

src/test/sighash_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ uint256 static SignatureHashOld(CScript scriptCode, const CTransaction& txTo, un
3535

3636
// In case concatenating two scripts ends up with two codeseparators,
3737
// or an extra one at the end, this prevents all those possible incompatibilities.
38-
scriptCode.FindAndDelete(CScript(OP_CODESEPARATOR));
38+
FindAndDelete(scriptCode, CScript(OP_CODESEPARATOR));
3939

4040
// Blank out other inputs' signatures
4141
for (unsigned int i = 0; i < txTmp.vin.size(); i++)

0 commit comments

Comments
 (0)