Skip to content

Commit 3351c91

Browse files
committed
refactor: Make CScriptVisitor stateless
1 parent 24f7029 commit 3351c91

File tree

1 file changed

+20
-32
lines changed

1 file changed

+20
-32
lines changed

src/script/standard.cpp

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -239,59 +239,47 @@ bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::
239239

240240
namespace
241241
{
242-
class CScriptVisitor : public boost::static_visitor<bool>
242+
class CScriptVisitor : public boost::static_visitor<CScript>
243243
{
244-
private:
245-
CScript *script;
246244
public:
247-
explicit CScriptVisitor(CScript *scriptin) { script = scriptin; }
248-
249-
bool operator()(const CNoDestination &dest) const {
250-
script->clear();
251-
return false;
245+
CScript operator()(const CNoDestination& dest) const
246+
{
247+
return CScript();
252248
}
253249

254-
bool operator()(const PKHash &keyID) const {
255-
script->clear();
256-
*script << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG;
257-
return true;
250+
CScript operator()(const PKHash& keyID) const
251+
{
252+
return CScript() << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG;
258253
}
259254

260-
bool operator()(const ScriptHash &scriptID) const {
261-
script->clear();
262-
*script << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL;
263-
return true;
255+
CScript operator()(const ScriptHash& scriptID) const
256+
{
257+
return CScript() << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL;
264258
}
265259

266-
bool operator()(const WitnessV0KeyHash& id) const
260+
CScript operator()(const WitnessV0KeyHash& id) const
267261
{
268-
script->clear();
269-
*script << OP_0 << ToByteVector(id);
270-
return true;
262+
return CScript() << OP_0 << ToByteVector(id);
271263
}
272264

273-
bool operator()(const WitnessV0ScriptHash& id) const
265+
CScript operator()(const WitnessV0ScriptHash& id) const
274266
{
275-
script->clear();
276-
*script << OP_0 << ToByteVector(id);
277-
return true;
267+
return CScript() << OP_0 << ToByteVector(id);
278268
}
279269

280-
bool operator()(const WitnessUnknown& id) const
270+
CScript operator()(const WitnessUnknown& id) const
281271
{
282-
script->clear();
283-
*script << CScript::EncodeOP_N(id.version) << std::vector<unsigned char>(id.program, id.program + id.length);
284-
return true;
272+
return CScript() << CScript::EncodeOP_N(id.version) << std::vector<unsigned char>(id.program, id.program + id.length);
285273
}
286274
};
275+
276+
const CScriptVisitor g_script_visitor;
277+
287278
} // namespace
288279

289280
CScript GetScriptForDestination(const CTxDestination& dest)
290281
{
291-
CScript script;
292-
293-
boost::apply_visitor(CScriptVisitor(&script), dest);
294-
return script;
282+
return boost::apply_visitor(::g_script_visitor, dest);
295283
}
296284

297285
CScript GetScriptForRawPubKey(const CPubKey& pubKey)

0 commit comments

Comments
 (0)