Exemplo n.º 1
0
        private void button5_Click(object sender, EventArgs e)
        {
            if (tx == null)
            {
                tx = new InvocationTransaction();
            }
            tx.Version = 1;
            tx.Script  = textBox6.Text.HexToBytes();
            if (tx.Attributes == null)
            {
                tx.Attributes = new TransactionAttribute[0];
            }
            if (tx.Inputs == null)
            {
                tx.Inputs = new CoinReference[0];
            }
            if (tx.Outputs == null)
            {
                tx.Outputs = new TransactionOutput[0];
            }
            if (tx.Scripts == null)
            {
                tx.Scripts = new Witness[0];
            }
            LevelDBBlockchain blockchain = (LevelDBBlockchain)Blockchain.Default;
            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(tx, script_table, service, Fixed8.Zero, true);

            engine.LoadScript(tx.Script, false);
            if (engine.Execute())
            {
                tx.Gas = engine.GasConsumed - Fixed8.FromDecimal(10);
                if (tx.Gas < Fixed8.One)
                {
                    tx.Gas = Fixed8.One;
                }
                tx.Gas          = tx.Gas.Ceiling();
                label7.Text     = tx.Gas + " gas";
                button3.Enabled = true;
            }
            else
            {
                MessageBox.Show(Strings.ExecutionFailed);
            }
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        /**
         * run script through ApplicationEngine to determine gas price and bytecode validity
         */
        private void btnTestScript_Click(object sender, EventArgs e)
        {
            if (tx == null)
            {
                tx = new InvocationTransaction();
            }
            tx.Version = 1;
            tx.Script  = txtCustomScriptCopy.Text.HexToBytes();
            if (tx.Attributes == null)
            {
                tx.Attributes = new TransactionAttribute[0];
            }
            if (tx.Inputs == null)
            {
                tx.Inputs = new CoinReference[0];
            }
            if (tx.Outputs == null)
            {
                tx.Outputs = new TransactionOutput[0];
            }
            if (tx.Scripts == null)
            {
                tx.Scripts = new Witness[0];
            }
            LevelDBBlockchain blockchain = (LevelDBBlockchain)Blockchain.Default;
            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);

            ////////////////////////////////////////////////////////////
            ////////////////////////EXPERIMENTAL////////////////////////
            //testTx = tx;
            //testTx.Gas = Fixed8.Satoshi;
            //testTx = GetTransaction();
            //ApplicationEngine engine = new ApplicationEngine(TriggerType.Application, testTx, script_table, service, Fixed8.Zero, true);
            //engine.LoadScript(testTx.Script, false);
            ////////////////////////EXPERIMENTAL////////////////////////
            ////////////////////////////////////////////////////////////

            ApplicationEngine engine = new ApplicationEngine(TriggerType.Application, tx, script_table, service, Fixed8.Zero, true);

            engine.LoadScript(tx.Script, false);

            if (engine.Execute())
            {
                tx.Gas = engine.GasConsumed - Fixed8.FromDecimal(10);
                if (tx.Gas < Fixed8.One)
                {
                    tx.Gas = Fixed8.One;
                }
                tx.Gas            = tx.Gas.Ceiling();
                label7.Text       = tx.Gas + " gas";
                btnInvoke.Enabled = true;
                if (engine.EvaluationStack.Count != 0)
                {
                    if (engine.EvaluationStack.Peek().ToString() != "Neo.VM.Types.InteropInterface" && engine.EvaluationStack.Peek().ToString() != "Neo.VM.Types.Array")
                    {
                        MessageBox.Show(
                            "Hex: " + engine.EvaluationStack.Peek().GetByteArray().ToHexString() + "\n"
                            + "String: " + System.Text.Encoding.UTF8.GetString(engine.EvaluationStack.Peek().GetByteArray()) + "\n"
                            + "BigInt: " + new BigInteger(engine.EvaluationStack.Peek().GetByteArray()),
                            "Return from Test");
                    }
                }
            }
            else
            {
                MessageBox.Show(Strings.ExecutionFailed);
            }
        }