MultiversX Tracker is Live!

Script. OP_PICK/OP_ROLL

Bitcoin Stack Exchange

Bitcoin News / Bitcoin Stack Exchange 196 Views

Here's that source code, annotated. Each annotation refers to the code after it.


This means that this part of the code handles both OP_PICK and OP_ROLL. This is because OP_PICK and OP_ROLL are very similar. The only difference is that PICK copies and ROLL moves.

 case OP_PICK: case OP_ROLL: {

These are two examples of the stack before and after. This is sometimes called a 'precondition' and a 'postcondition.'

This one documents OP_PICK.

Precondition: xn ... x2 x1 x0 n

Postcondition: xn ... x2 x1 x0 xn

 // (xn ... x2 x1 x0 n - xn ... x2 x1 x0 xn)

This one documents OP_ROLL.

Precondition: xn ... x2 x1 x0 n

Postcondition: ... x2 x1 x0 xn (Note the lack of xn at the beginning.)

 // (xn ... x2 x1 x0 n - ... x2 x1 x0 xn)

You need to have at least two elements on the stack, or this operation makes no sense.

 if (stack.size() < 2) return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);

Copy (but do not remove) the top element on the stack. Interpret it as a number. Convert it to a 32-bit integer, so it's easier to work with.

 int n = CScriptNum(stacktop(-1), fRequireMinimal).getint();

Now remove the top element on the stack. (Which should be n, remember.)

 popstack(stack);

If n is negative, fail. If n is larger than the stack, fail. (This is where I think your script is having an error.)

 if (n < 0 || n >= (int)stack.size()) return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);

Copy the value we're after. Remember, 1 2 3 0 OP_PICK should get the element just below n, 3. Also, stacktop(-1) refers to the top. So therefore, we need to invert n and subtract 1.

 valtype vch = stacktop(-n-1);

Remember, OP_ROLL deletes the element from where it was originally. This code is not executed for OP_PICK.

 if (opcode == OP_ROLL) stack.erase(stack.end()-n-1);

Now add the element that we just got back on the top of the stack.

 stack.push_back(vch);

We're done handling this case.

 } break;

Does that help?


Get BONUS $200 for FREE!

You can get bonuses upto $100 FREE BONUS when you:
πŸ’° Install these recommended apps:
πŸ’² SocialGood - 100% Crypto Back on Everyday Shopping
πŸ’² xPortal - The DeFi For The Next Billion
πŸ’² CryptoTab Browser - Lightweight, fast, and ready to mine!
πŸ’° Register on these recommended exchanges:
🟑 Binance🟑 Bitfinex🟑 Bitmart🟑 Bittrex🟑 Bitget
🟑 CoinEx🟑 Crypto.com🟑 Gate.io🟑 Huobi🟑 Kucoin.



Comments