Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
751break;753case OP_2ROT:754{755// (x1 x2 x3 x4 x5 x6 -- x3 x4 x5 x6 x1 x2)756if (stack.size() < 6)757return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);758valtype vch1 = stacktop(-6);759valtype vch2 = stacktop(-5);760stack.erase(stack.end()-6, stack.end()-4);761stack.push_back(vch1);762stack.push_back(vch2);763}764break;766case OP_2SWAP:767{768// (x1 x2 x3 x4 -- x3 x4 x1 x2)769if (stack.size() < 4)770return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);771swap(stacktop(-4), stacktop(-2));772swap(stacktop(-3), stacktop(-1));773}774break;776case OP_IFDUP:777{778// (x - 0 | x x)779if (stack.size() < 1)780return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);781valtype vch = stacktop(-1);782if (CastToBool(vch))783stack.push_back(vch);784}785break;787case OP_DEPTH:788{789// -- stacksize790CScriptNum bn(stack.size());791stack.push_back(bn.getvch());792}793break;795case OP_DROP:796{797// (x -- )798if (stack.size() < 1)799return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);800popstack(stack);801}802break;804case OP_DUP:805{806// (x -- x x)807if (stack.size() < 1)808return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);809valtype vch = stacktop(-1);810stack.push_back(vch);811}812break;814case OP_NIP:815{816// (x1 x2 -- x2)817if (stack.size() < 2)818return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);819stack.erase(stack.end() - 2);820}821break;823case OP_OVER:824{825// (x1 x2 -- x1 x2 x1)826if (stack.size() < 2)827return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);828valtype vch = stacktop(-2);829stack.push_back(vch);830}831break;833case OP_PICK:834case OP_ROLL:835{836// (xn ... x2 x1 x0 n - xn ... x2 x1 x0 xn)837// (xn ... x2 x1 x0 n - ... x2 x1 x0 xn)838if (stack.size() < 2)839return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);840int n = CScriptNum(stacktop(-1), fRequireMinimal).getint();841popstack(stack);842if (n < 0 || n >= (int)stack.size())843return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);844valtype vch = stacktop(-n-1);845if (opcode == OP_ROLL)846stack.erase(stack.end()-n-1);847stack.push_back(vch);848}849break;851case OP_ROT:852{853// (x1 x2 x3 -- x2 x3 x1)854// x2 x1 x3 after first swap855// x2 x3 x1 after second swap856if (stack.size() < 3)857return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);858swap(stacktop(-3), stacktop(-2));859swap(stacktop(-2), stacktop(-1));860}861break;863case OP_SWAP:864{865// (x1 x2 -- x2 x1)866if (stack.size() < 2)867return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);868swap(stacktop(-2), stacktop(-1));869}870break;872case OP_TUCK:873{874// (x1 x2 -- x2 x1 x2)875if (stack.size() < 2)876return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);877valtype vch = stacktop(-1);878stack.insert(stack.end()-2, vch);879}880break;883case OP_SIZE:884{885// (in -- in size)886if (stack.size() < 1)887return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);888CScriptNum bn(stacktop(-1).size());889stack.push_back(bn.getvch());890}891break;894//895// Bitwise logic896//897case OP_EQUAL:898case OP_EQUALVERIFY:899//case OP_NOTEQUAL: // use OP_NUMNOTEQUAL900{901// (x1 x2 - bool)902if (stack.size() < 2)903return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);904valtype& vch1 = stacktop(-2);905valtype& vch2 = stacktop(-1);906bool fEqual = (vch1 == vch2);907// OP_NOTEQUAL is disabled because it would be too easy to say908// something like n != 1 and have some wiseguy pass in 1 with extra909// zero bytes after it (numerically, 0x01 == 0x0001 == 0x000001)910//if (opcode == OP_NOTEQUAL)911// fEqual = !fEqual;912popstack(stack);913popstack(stack);914stack.push_back(fEqual ? vchTrue : vchFalse);915if (opcode == OP_EQUALVERIFY)916{917if (fEqual)918popstack(stack);919else920return set_error(serror, SCRIPT_ERR_EQUALVERIFY);921}922}923break;926//927// Numeric928//929case OP_1ADD:930case OP_1SUB:931case OP_NEGATE:932case OP_ABS:933case OP_NOT:934case OP_0NOTEQUAL:935{936// (in -- out)937if (stack.size() < 1)938return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);939CScriptNum bn(stacktop(-1), fRequireMinimal);940switch (opcode)941{942case OP_1ADD: bn += bnOne; break;943case OP_1SUB: bn -= bnOne; break;944case OP_NEGATE: bn = -bn; break;945case OP_ABS: if (bn < bnZero) bn = -bn; break;946case OP_NOT: bn = (bn == bnZero); break;947case OP_0NOTEQUAL: bn = (bn != bnZero); break;948default: assert(!"invalid opcode"); break;949}950popstack(stack);951stack.push_back(bn.getvch());952}953break;955case OP_ADD:956case OP_SUB:957case OP_BOOLAND:958case OP_BOOLOR:959case OP_NUMEQUAL:960case OP_NUMEQUALVERIFY:961case OP_NUMNOTEQUAL:962case OP_LESSTHAN:963case OP_GREATERTHAN:964case OP_LESSTHANOREQUAL:965case OP_GREATERTHANOREQUAL:966case OP_MIN:967case OP_MAX:968{969// (x1 x2 -- out)970if (stack.size() < 2)971return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);972CScriptNum bn1(stacktop(-2), fRequireMinimal);973CScriptNum bn2(stacktop(-1), fRequireMinimal);974CScriptNum bn(0);975switch (opcode)976{977case OP_ADD:978bn = bn1 + bn2;979break;981case OP_SUB:982bn = bn1 - bn2;983break;985case OP_BOOLAND: bn = (bn1 != bnZero && bn2 != bnZero); break;986case OP_BOOLOR: bn = (bn1 != bnZero || bn2 != bnZero); break;987case OP_NUMEQUAL: bn = (bn1 == bn2); break;988case OP_NUMEQUALVERIFY: bn = (bn1 == bn2); break;989case OP_NUMNOTEQUAL: bn = (bn1 != bn2); break;990case OP_LESSTHAN: bn = (bn1 < bn2); break;991case OP_GREATERTHAN: bn = (bn1 > bn2); break;992case OP_LESSTHANOREQUAL: bn = (bn1 <= bn2); break;993case OP_GREATERTHANOREQUAL: bn = (bn1 >= bn2); break;994case OP_MIN: bn = (bn1 < bn2 ? bn1 : bn2); break;995case OP_MAX: bn = (bn1 > bn2 ? bn1 : bn2); break;996default: assert(!"invalid opcode"); break;997}998popstack(stack);999popstack(stack);1000stack.push_back(bn.getvch());