Exemplo n.º 1
0
        /// <summary>
        /// Duplicates top stack item if its value is not 0. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 1)
            {
                error = "There was no item left in the stack to check and duplicate.";
                return(false);
            }

            byte[] data = opData.Peek();
            if (OpHelper.IsNotZero(data))
            {
                opData.Push(data);
            }

            error = null;
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Removes top stack item only passes if it is true. Return value indicates success.
        /// </summary>
        /// <param name="opData">Data to use</param>
        /// <param name="error">Error message (null if sucessful, otherwise will contain information about the failure)</param>
        /// <returns>True if operation was successful, false if otherwise</returns>
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 1)
            {
                error = "There was not enough items left in the stack.";
                return(false);
            }
            // Check the top stack value, only fail if False
            bool b = OpHelper.IsNotZero(opData.Pop());

            if (!b)
            {
                error = "Top stack item value was 'false'.";
                return(false);
            }

            error = null;
            return(true);
        }
        public override bool Run(IOpData opData, out string error)
        {
            if (opData.ItemCount < 1)
            {
                error = "Invalid number of elements in stack.";
                return(false);
            }

            // TODO: check what this part is
            // https://github.com/bitcoin/bitcoin/blob/a822a0e4f6317f98cde6f0d5abe952b4e8992ac9/src/script/interpreter.cpp#L478-L483
            // probably have to include it in whever we run these operations.

            // Remove top stack item and interpret it as bool
            // OP_IF: runs for True
            // OP_NOTIF: runs for false
            bool isRunnable = OpHelper.IsNotZero(opData.Pop()) & ShouldRunIfTopItemIs; // Note: this is a bitwise operation not logical

            if (isRunnable)
            {
                foreach (var op in mainOps)
                {
                    if (!op.Run(opData, out error))
                    {
                        return(false);
                    }
                }
            }
            else if (elseOps != null && elseOps.Length != 0)
            {
                foreach (var op in elseOps)
                {
                    if (!op.Run(opData, out error))
                    {
                        return(false);
                    }
                }
            }

            error = null;
            return(true);
        }