コード例 #1
0
        private void ImportBlocks(Stream stream, bool read_start = false)
        {
            TR.Enter();
            LevelDBBlockchain blockchain = (LevelDBBlockchain)Blockchain.Default;

            using (BinaryReader r = new BinaryReader(stream))
            {
                uint start = read_start ? r.ReadUInt32() : 0;
                uint count = r.ReadUInt32();
                uint end   = start + count - 1;
                if (end <= blockchain.Height)
                {
                    return;
                }
                for (uint height = start; height <= end; height++)
                {
                    byte[] array = r.ReadBytes(r.ReadInt32());
                    if (height > blockchain.Height)
                    {
                        Block block = array.AsSerializable <Block>();
                        blockchain.AddBlockDirectly(block);
                    }
                }
            }
            TR.Exit();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: wuqionglei/IceWallet
        static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            WindowsFormsSynchronizationContext.AutoInstall = false;
            MainForm = new MainForm();
            const string path = "nodes.dat";

            if (File.Exists(path))
            {
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    LocalNode.LoadState(fs);
                }
            }
            using (LevelDBBlockchain blockchain = new LevelDBBlockchain(Settings.Default.ChainPath))
                using (LocalNode = new LocalNode())
                {
                    Blockchain.RegisterBlockchain(blockchain);
                    LocalNode.Start();
                    Application.Run(MainForm);
                }
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                LocalNode.SaveState(fs);
            }
        }
コード例 #3
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);
            }
        }
コード例 #4
0
        /// <summary>
        ///     Starts a node and connects to the network. Once connected, starts synchornizing the blocks
        /// </summary>
        public void Start()
        {
            _configuration.CreateJsonFiles();

            _logger.LogMessage("Chain location: " + _configuration.ChainPath);
            //TODO: take in the folder, take in the ports as parameters
            var levelDbBlockchain = new LevelDBBlockchain(_configuration.ChainPath);

            _blockchain = Blockchain.RegisterBlockchain(levelDbBlockchain);
            LocalNode   = new LocalNode {
                UpnpEnabled = true
            };
            _logger.LogMessage("Starting Node");

            //start a node in the background;
            Task.Run(() => { LocalNode.Start(_configuration.NodePort, _configuration.WsPort); });

            var stopWatch = new Stopwatch();

            stopWatch.Start();
            var nodeCount = 0;

            while (LocalNode.RemoteNodeCount <= 1)
            {
                if (stopWatch.Elapsed.TotalSeconds > 30)
                {
                    throw new ApplicationException("could not connect to any peers within 30 seconds. Check network and settings and try again.");
                }
                _logger.LogMessage("Looking for peers");
                Thread.Sleep(1000); //wait to connect to peers.
            }

            _logger.LogMessage("connected to peers");
            //sync the chain. If headerHeight == 0 then we have not received any data about the height yet
            var count = 0;

            while (Blockchain.Default.HeaderHeight == 0 || Blockchain.Default.Height < Blockchain.Default.HeaderHeight)
            {
                if (count % 10 == 0) //don't spam out too many messages
                {
                    _logger.LogMessage($"Synchronizing blockchain. Processed {Blockchain.Default.Height.ToString("N0", CultureInfo.InvariantCulture)} of {Blockchain.Default.HeaderHeight.ToString("N0", CultureInfo.InvariantCulture)} blocks");
                }
                Thread.Sleep(1000);
                count++;
            }

            _logger.LogMessage("Blockchain Synchronized");
        }
コード例 #5
0
        private void ImportBlocks(Stream stream)
        {
            LevelDBBlockchain blockchain = (LevelDBBlockchain)Blockchain.Default;

            using (BinaryReader r = new BinaryReader(stream))
            {
                uint count = r.ReadUInt32();
                for (int height = 0; height < count; height++)
                {
                    byte[] array = r.ReadBytes(r.ReadInt32());
                    if (height > blockchain.Height)
                    {
                        Block block = array.AsSerializable <Block>();
                        blockchain.AddBlockDirectly(block);
                    }
                }
            }
        }
コード例 #6
0
        private void ImportBlocks(Stream stream)
        {
            LevelDBBlockchain blockchain = (LevelDBBlockchain)Neo.Core.Blockchain.Default;

            blockchain.VerifyBlocks = false;
            using (BinaryReader r = new BinaryReader(stream))
            {
                uint count = r.ReadUInt32();
                for (int height = 0; height < count; height++)
                {
                    byte[] array = r.ReadBytes(r.ReadInt32());
                    if (height > Neo.Core.Blockchain.Default.Height)
                    {
                        Block block = array.AsSerializable <Block>();
                        Neo.Core.Blockchain.Default.AddBlock(block);
                    }
                }
            }
            blockchain.VerifyBlocks = true;
        }
コード例 #7
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);
            }
        }