<commit_before>﻿using System.Linq;
using NBitcoin;

namespace Stratis.SmartContracts.Core
{
    public static class SmartContractScript
    {
        public static bool IsSmartContractExec(this Script script)
        {
            return script.IsSmartContractCall() || script.IsSmartContractCreate();

        }

        public static bool IsSmartContractCall(this Script script)
        {
            return TestFirstByte(script, (byte)ScOpcodeType.OP_CALLCONTRACT);
        }

        public static bool IsSmartContractCreate(this Script script)
        {
            return TestFirstByte(script, (byte)ScOpcodeType.OP_CREATECONTRACT);

        }

        public static bool IsSmartContractSpend(this Script script)
        {
            return TestFirstByte(script, (byte)ScOpcodeType.OP_SPEND);

        }

        public static bool IsSmartContractInternalCall(this Script script)
        {
            return TestFirstByte(script, (byte) ScOpcodeType.OP_INTERNALCONTRACTTRANSFER);
        }

        private static bool TestFirstByte(Script script, byte opcode)
        {
            var scriptBytes = script.ToBytes();

            if (scriptBytes.Length == 0)
                return false;

            var firstOp = script.ToBytes().FirstOrDefault();

            return firstOp == opcode;
        }
    }

    public enum ScOpcodeType : byte
    {
        // smart contracts
        OP_CREATECONTRACT = 0xc0,
        OP_CALLCONTRACT = 0xc1,
        OP_SPEND = 0xc2,
        OP_INTERNALCONTRACTTRANSFER = 0xc3
    }
}<commit_msg>Prevent duplicate call to script.ToBytes() (#2535)<commit_after>﻿using System.Linq;
using NBitcoin;

namespace Stratis.SmartContracts.Core
{
    public static class SmartContractScript
    {
        public static bool IsSmartContractExec(this Script script)
        {
            return script.IsSmartContractCall() || script.IsSmartContractCreate();

        }

        public static bool IsSmartContractCall(this Script script)
        {
            return TestFirstByte(script, (byte)ScOpcodeType.OP_CALLCONTRACT);
        }

        public static bool IsSmartContractCreate(this Script script)
        {
            return TestFirstByte(script, (byte)ScOpcodeType.OP_CREATECONTRACT);

        }

        public static bool IsSmartContractSpend(this Script script)
        {
            return TestFirstByte(script, (byte)ScOpcodeType.OP_SPEND);

        }

        public static bool IsSmartContractInternalCall(this Script script)
        {
            return TestFirstByte(script, (byte) ScOpcodeType.OP_INTERNALCONTRACTTRANSFER);
        }

        private static bool TestFirstByte(Script script, byte opcode)
        {
            var scriptBytes = script.ToBytes();

            if (scriptBytes.Length == 0)
                return false;

            return scriptBytes[0] == opcode;
        }
    }

    public enum ScOpcodeType : byte
    {
        // smart contracts
        OP_CREATECONTRACT = 0xc0,
        OP_CALLCONTRACT = 0xc1,
        OP_SPEND = 0xc2,
        OP_INTERNALCONTRACTTRANSFER = 0xc3
    }
}
{'fraction_non_alphanumeric': 0.07124183006535947, 'fraction_numerical': 0.007516339869281046, 'mean_word_length': 2.2357293868921775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1367326', 'n_tokens_mistral': 870, 'n_tokens_neox': 829, 'n_words': 184}