Example #1
0
 public static ApplicationEngine Run(byte[] script, IScriptContainer container = null, Block persistingBlock = null, bool testMode = false, Fixed8 extraGAS = default(Fixed8))
 {
     using (Snapshot snapshot = Blockchain.Singleton.GetSnapshot())
     {
         return(Run(script, snapshot, container, persistingBlock, testMode, extraGAS));
     }
 }
 public static SimpleApplicationEngine Run(byte[] script, Store store, IScriptContainer container = null, Block persistingBlock = null, bool testMode = false, Fixed8 extraGAS = default(Fixed8))
 {
     using (Snapshot snapshot = store.GetSnapshot())
     {
         return(Run(script, snapshot as DbSnapshot, container, persistingBlock, testMode, extraGAS));
     }
 }
Example #3
0
 public ApplicationEngine(TriggerType trigger, IScriptContainer container, IScriptTable table, InteropService service, Fixed8 gas, bool testMode = false)
     : base(container, Cryptography.Crypto.Default, table, service)
 {
     this.gas_amount = gas_free + gas.GetData();
     this.testMode   = testMode;
     this.Trigger    = trigger;
 }
Example #4
0
 public ExecutionEngine(IScriptContainer container, ICryptography crypto, IScriptTable table = null, InteropService service = null)
 {
     this.ScriptContainer = container;
     this.Crypto          = crypto;
     this.table           = table;
     this.Service         = service ?? new InteropService();
 }
Example #5
0
 public static ApplicationEngine Run(byte[] script, Blockchain blockchain, IScriptContainer container = null, Block persistingBlock = null, bool testMode = false)
 {
     using (Snapshot snapshot = blockchain.GetSnapshot())
     {
         return(Run(script, snapshot, container, persistingBlock, testMode));
     }
 }
Example #6
0
        public static ApplicationEngine Run(byte[] script, IScriptContainer container = null, Block persisting_block = null)
        {
            if (persisting_block == null)
            {
                persisting_block = new Block
                {
                    Version       = 0,
                    PrevHash      = Blockchain.Default.CurrentBlockHash,
                    MerkleRoot    = new UInt256(),
                    Timestamp     = Blockchain.Default.GetHeader(Blockchain.Default.Height).Timestamp + Blockchain.SecondsPerBlock,
                    Index         = Blockchain.Default.Height + 1,
                    ConsensusData = 0,
                    NextConsensus = Blockchain.Default.GetHeader(Blockchain.Default.Height).NextConsensus,
                    Script        = new Witness
                    {
                        InvocationScript   = new byte[0],
                        VerificationScript = new byte[0]
                    },
                    Transactions = new Transaction[0]
                }
            }
            ;
            DataCache <UInt160, AccountState>   accounts  = Blockchain.Default.GetStates <UInt160, AccountState>();
            DataCache <UInt256, AssetState>     assets    = Blockchain.Default.GetStates <UInt256, AssetState>();
            DataCache <UInt160, ContractState>  contracts = Blockchain.Default.GetStates <UInt160, ContractState>();
            DataCache <StorageKey, StorageItem> storages  = Blockchain.Default.GetStates <StorageKey, StorageItem>();
            CachedScriptTable script_table = new CachedScriptTable(contracts);
            StateMachine      service      = new StateMachine(persisting_block, accounts, assets, contracts, storages);
            ApplicationEngine engine       = new ApplicationEngine(TriggerType.Application, container, script_table, service, Fixed8.Zero, true);

            engine.LoadScript(script, false);
            engine.Execute();
            return(engine);
        }
Example #7
0
 public ApplicationEngine(TriggerType trigger, IScriptContainer container, Snapshot snapshot, Fixed8 gas, bool testMode = false)
     : base(container, Cryptography.Crypto.Default, snapshot, new NeoService(trigger, snapshot))
 {
     this.gas_amount = gas_free + gas.GetData();
     this.testMode   = testMode;
     this.snapshot   = snapshot;
 }
Example #8
0
        public static ApplicationEngine Run(byte[] script, Snapshot snapshot,
                                            IScriptContainer container = null, Block persistingBlock = null, bool testMode = false, Fixed8 extraGAS = default(Fixed8))
        {
            snapshot.PersistingBlock = persistingBlock ?? snapshot.PersistingBlock ?? new Block
            {
                Version       = 0,
                PrevHash      = snapshot.CurrentBlockHash,
                MerkleRoot    = new UInt256(),
                Timestamp     = snapshot.Blocks[snapshot.CurrentBlockHash].TrimmedBlock.Timestamp + Blockchain.SecondsPerBlock,
                Index         = snapshot.Height + 1,
                ConsensusData = 0,
                NextConsensus = snapshot.Blocks[snapshot.CurrentBlockHash].TrimmedBlock.NextConsensus,
                Witness       = new Witness
                {
                    InvocationScript   = new byte[0],
                    VerificationScript = new byte[0]
                },
                Transactions = new Transaction[0]
            };
            ApplicationEngine engine = new ApplicationEngine(TriggerType.Application, container, snapshot, extraGAS, testMode);

            engine.LoadScript(script);
            engine.Execute();
            return(engine);
        }
        /// <summary>
        /// INIT HISTORY OF TRANSACTIONS
        /// </summary>
        /// <param name="scriptHash"></param>
        /// <param name="value"></param>
        /// <param name="inputAmount"></param>
        public static void InitTransactionContext(string scriptHash, int value)
        {
            Transaction       initialTransaction = new CustomTransaction(TransactionType.ContractTransaction);
            Transaction       currentTransaction = new CustomTransaction(TransactionType.ContractTransaction);
            TransactionOutput transactionOutput;
            CoinReference     coinRef;

            /** CREATE FAKE PREVIOUS TRANSACTION */
            transactionOutput = new TransactionOutput
            {
                ScriptHash = UInt160.Parse(scriptHash),
                Value      = new Fixed8(value),
                AssetId    = UInt256.Parse(NEO_ASSET_ID)
            };

            initialTransaction.Outputs = new TransactionOutput[] { transactionOutput };
            /** CREATE FAKE CURRENT TRANSACTION */
            coinRef = new CoinReference
            {
                PrevHash  = initialTransaction.Hash,
                PrevIndex = 0
            };

            currentTransaction.Inputs = new CoinReference[] { coinRef };

            /**INIT CONTEXT*/
            service.transactions[initialTransaction.Hash] = initialTransaction;
            service.transactions[currentTransaction.Hash] = currentTransaction;
            scriptContainer = initialTransaction;
        }
Example #10
0
        /// <summary>
        /// Execute this test
        /// </summary>
        /// <param name="ut">Test</param>
        public void ExecuteTest(VMUT ut)
        {
            foreach (var test in ut.Tests)
            {
                // Interop service

                IInteropService service = new InteropService();

                // Message provider

                IScriptContainer scriptContainer = null;

                if (test.Message != null)
                {
                    scriptContainer = new MessageProvider(test.Message);
                }

                // Create engine

                using (var engine = new ExecutionEngine(scriptContainer, Crypto.Default, service))
                {
                    Debugger debugger = new Debugger(engine);
                    engine.LoadScript(test.Script);

                    // Execute Steps

                    if (test.Steps != null)
                    {
                        foreach (var step in test.Steps)
                        {
                            // Actions

                            if (step.Actions != null)
                            {
                                foreach (var run in step.Actions)
                                {
                                    switch (run)
                                    {
                                    case VMUTActionType.Execute: debugger.Execute(); break;

                                    case VMUTActionType.StepInto: debugger.StepInto(); break;

                                    case VMUTActionType.StepOut: debugger.StepOut(); break;

                                    case VMUTActionType.StepOver: debugger.StepOver(); break;
                                    }
                                }
                            }

                            // Review results

                            var add = string.IsNullOrEmpty(step.Name) ? "" : "-" + step.Name;

                            AssertResult(engine, step.Result, $"{ut.Category}-{ut.Name}-{test.Name}{add}: ");
                        }
                    }
                }
            }
        }
Example #11
0
 public ExecutionEngine(IScriptContainer container, ICrypto crypto, int max_steps, IScriptTable table = null, InteropService service = null)
 {
     this.ScriptContainer = container;
     this.Crypto          = crypto;
     this.table           = table;
     this.service         = service ?? new InteropService();
     this.max_steps       = max_steps;
 }
Example #12
0
 public NotifyEventArgs(IScriptContainer container, UInt160 script_hash, StackItem state)
 {
     TR.Enter();
     this.ScriptContainer = container;
     this.ScriptHash      = script_hash;
     this.State           = state;
     TR.Exit();
 }
 public ApplicationEngine(TriggerType trigger, IScriptContainer container, Snapshot snapshot, Fixed8 gas, bool testMode = false)
     : base(container, Cryptography.Crypto.Default, snapshot, new NeoService(trigger, snapshot))
 {
     if (snapshot.Height < Blockchain.FreeGasChangeHeight)
     {
         this.gas_amount = gas_free + gas.GetData();
     }
     else
     {
         this.gas_amount = gas_free_new + gas.GetData();
     }
     this.testMode = testMode;
     this.snapshot = snapshot;
 }
Example #14
0
        public static ApplicationEngine Run(byte[] script, IScriptContainer container = null)
        {
            DataCache <UInt160, AccountState>   accounts   = blockchain.GetTable <UInt160, AccountState>();
            DataCache <ECPoint, ValidatorState> validators = blockchain.GetTable <ECPoint, ValidatorState>();
            DataCache <UInt256, AssetState>     assets     = blockchain.GetTable <UInt256, AssetState>();
            DataCache <UInt160, ContractState>  contracts  = blockchain.GetTable <UInt160, ContractState>();
            DataCache <StorageKey, StorageItem> storages   = blockchain.GetTable <StorageKey, StorageItem>();
            CachedScriptTable script_table = new CachedScriptTable(contracts);
            StateMachine      service      = new StateMachine(accounts, validators, assets, contracts, storages);
            ApplicationEngine engine       = new ApplicationEngine(TriggerType.Application, container, script_table, service, Fixed8.Zero, true);

            engine.LoadScript(script, false);
            return(engine.Execute() ? engine : null);
        }
Example #15
0
        /// <summary>
        /// Create Engine from config
        /// </summary>
        public NeoEngine CreateEngine()
        {
            IScriptContainer container = null;

            DataCache <UInt160, AccountState>   accounts;
            DataCache <ECPoint, ValidatorState> validators;
            DataCache <UInt256, AssetState>     assets;
            DataCache <UInt160, ContractState>  contracts;
            DataCache <StorageKey, StorageItem> storages;

            if (Blockchain.Default != null && !(Blockchain.Default is NullBlockChain))
            {
                // Real Blockchain

                accounts   = Blockchain.Default.CreateCache <UInt160, AccountState>();
                validators = Blockchain.Default.CreateCache <ECPoint, ValidatorState>();
                assets     = Blockchain.Default.CreateCache <UInt256, AssetState>();
                contracts  = Blockchain.Default.CreateCache <UInt160, ContractState>();
                storages   = Blockchain.Default.CreateCache <StorageKey, StorageItem>();
            }
            else
            {
                // Fake Blockchain

                accounts   = new NeoFakeDbCache <UInt160, AccountState>();
                validators = new NeoFakeDbCache <ECPoint, ValidatorState>();
                assets     = new NeoFakeDbCache <UInt256, AssetState>();
                contracts  = new NeoFakeDbCache <UInt160, ContractState>();
                storages   = new NeoFakeDbCache <StorageKey, StorageItem>();
            }

            // Create Engine
            IScriptTable script_table = new CachedScriptTable(contracts);
            StateMachine service      = new StateMachine(accounts, validators, assets, contracts, storages);

            TriggerType t;

            switch (TriggerType)
            {
            case ETriggerType.Application: t = Neo.SmartContract.TriggerType.Application; break;

            case ETriggerType.Verification: t = Neo.SmartContract.TriggerType.Verification; break;

            default: return(null);
            }

            return(new NeoEngine(t, container, script_table, service, Fixed8.Zero, true));
        }
Example #16
0
        public static void InitTransactionContext(string scriptHash, int value, ushort inputAmount = 1)
        {
            Transaction initialTransaction = new CustomTransaction(TransactionType.ContractTransaction);
            Transaction currentTransaction = new CustomTransaction(TransactionType.ContractTransaction);

            initialTransaction.Outputs = new TransactionOutput[inputAmount];
            currentTransaction.Inputs  = new CoinReference[inputAmount];


            for (ushort i = 0; i < inputAmount; ++i)
            {
                /** CREATE FAKE PREVIOUS TRANSACTION */
                var transactionOutput = new TransactionOutput
                {
                    ScriptHash = UInt160.Parse(scriptHash),
                    Value      = new Fixed8(value),
                    AssetId    = UInt256.Parse(NEO_ASSET_ID)
                };

                initialTransaction.Outputs[i] = transactionOutput;
                /** CREATE FAKE CURRENT TRANSACTION */
                var coinRef = new CoinReference
                {
                    PrevHash  = initialTransaction.Hash,
                    PrevIndex = i
                };

                currentTransaction.Outputs    = new TransactionOutput[1];
                currentTransaction.Outputs[0] = new TransactionOutput
                {
                    ScriptHash = UInt160.Parse(scriptHash),
                    Value      = new Fixed8(value),
                    AssetId    = UInt256.Parse(NEO_ASSET_ID)
                };


                currentTransaction.Inputs[i] = coinRef;
            }


            /**INIT CONTEXT*/
            service.transactions[initialTransaction.Hash] = initialTransaction;
            scriptContainer = currentTransaction;
        }
Example #17
0
 public ApplicationEngine(IScriptContainer container, IScriptTable table, StateMachine state, Fixed8 gas)
     : base(container, Cryptography.Crypto.Default, table, state)
 {
     this.gas = gas_free + gas.GetData();
 }
Example #18
0
 public Breadcrumb(ViewContext viewContext, IStyleSheetContainer styleSheetContainer, IScriptContainer scriptContainer, IResponseCapturer responseCapturer)
     : base(viewContext, styleSheetContainer, scriptContainer, responseCapturer)
 {
     SeparatorText = DefaultSeparatorText;
 }
Example #19
0
 public LogEventArgs(IScriptContainer container, UInt160 script_hash, string message)
 {
     this.ScriptContainer = container;
     this.ScriptHash      = script_hash;
     this.Message         = message;
 }
Example #20
0
 public ExecutionEngine(IScriptContainer container, IScriptTable table = null, InteropService service = null)
 {
     this.ScriptContainer = container;
     this.table           = table;
     this.service         = service ?? new InteropService();
 }
 public DebugExecutionEngine(IScriptContainer container, ScriptTable scriptTable, InteropService interopService)
     : base(container, new Crypto(), scriptTable, interopService)
 {
     this.interopService = interopService;
 }
Example #22
0
 public NeoEngine(TriggerType trigger, IScriptContainer container, IScriptTable table, InteropService service, Fixed8 gas, bool testMode = false)
     : base(trigger, container, table, service, gas, testMode)
 {
 }
 public SCTrackerExecutionEngine(RichTextBox LogOutput, IScriptContainer container, ICrypto crypto, IScriptTable table = null, InteropService service = null)
     : base(container, crypto, table, service)
 {
     this.Logger = LogOutput;
 }
Example #24
0
 public NotifyEventArgs(IScriptContainer container, UIntBase script_hash, StackItem state)
 {
     this.ScriptContainer = container;
     this.ScriptHash      = script_hash;
     this.State           = state;
 }