Example #1
0
        public void Can_initialize_ProtocolSettings()
        {
            var expectedMagic = 12345u;

            var dict = new Dictionary <string, string>()
            {
                { "ProtocolConfiguration:Magic", $"{expectedMagic}" }
            };

            var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build();

            ProtocolSettings.Initialize(config).Should().BeTrue();
            ProtocolSettings.Default.Magic.Should().Be(expectedMagic);
        }
Example #2
0
        public void Cant_initialize_ProtocolSettings_after_default_settings_used()
        {
            ProtocolSettings.Default.Magic.Should().Be(mainNetMagic);

            var updatedMagic = 54321u;
            var dict         = new Dictionary <string, string>()
            {
                { "ProtocolConfiguration:Magic", $"{updatedMagic}" }
            };

            var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build();

            ProtocolSettings.Initialize(config).Should().BeFalse();
            ProtocolSettings.Default.Magic.Should().Be(mainNetMagic);
        }
Example #3
0
        public void Initialize_ProtocolSettings_NativeBlockIndex()
        {
            var tempFile = Path.GetTempFileName();

            File.WriteAllText(tempFile, @"
{
 ""ProtocolConfiguration"":
    {
    ""NativeActivations"":{ ""test"":123 }
    }
}
");
            var config = new ConfigurationBuilder().AddJsonFile(tempFile).Build();

            File.Delete(tempFile);

            ProtocolSettings.Initialize(config).Should().BeTrue();
            ProtocolSettings.Default.NativeActivations.Count.Should().Be(1);
            ProtocolSettings.Default.NativeActivations["test"].Should().Be(123);
        }
Example #4
0
        public static bool InitializeProtocolSettings(this ExpressChain chain, uint secondsPerBlock = 0)
        {
            secondsPerBlock = secondsPerBlock == 0 ? 15 : secondsPerBlock;

            IEnumerable <KeyValuePair <string, string> > settings()
            {
                yield return(new KeyValuePair <string, string>(
                                 "ProtocolConfiguration:Magic", $"{chain.Magic}"));

                yield return(new KeyValuePair <string, string>(
                                 "ProtocolConfiguration:AddressVersion", $"{ExpressChain.AddressVersion}"));

                yield return(new KeyValuePair <string, string>(
                                 "ProtocolConfiguration:SecondsPerBlock", $"{secondsPerBlock}"));

                foreach (var(node, index) in chain.ConsensusNodes.Select((n, i) => (n, i)))
                {
                    var privateKey = node.Wallet.Accounts
                                     .Select(a => a.PrivateKey)
                                     .Distinct().Single().HexToBytes();
                    var encodedPublicKey = new KeyPair(privateKey).PublicKey
                                           .EncodePoint(true).ToHexString();
                    yield return(new KeyValuePair <string, string>(
                                     $"ProtocolConfiguration:StandbyValidators:{index}", encodedPublicKey));

                    yield return(new KeyValuePair <string, string>(
                                     $"ProtocolConfiguration:SeedList:{index}", $"{IPAddress.Loopback}:{node.TcpPort}"));
                }
            }

            var config = new ConfigurationBuilder()
                         .AddInMemoryCollection(settings())
                         .Build();

            return(ProtocolSettings.Initialize(config));
        }
Example #5
0
        protected internal override void OnStart(string[] args)
        {
            bool   useRPC  = false;
            string netType = "mainnet";

            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                case "/rpc":
                case "--rpc":
                case "-r":
                    useRPC = true;
                    break;

                case "/testnet":
                case "--testnet":
                case "-t":
                    netType = "testnet";
                    break;

                case "/mainnet":
                case "--mainnet":
                case "-m":
                    netType = "mainnet";
                    break;
                }
            }

            ProtocolSettings.Initialize(new ConfigurationBuilder().AddJsonFile("protocol." + netType + ".json").Build());
            Settings.Initialize(new ConfigurationBuilder().AddJsonFile("config." + netType + ".json").Build());

            store  = new LevelDBStore(Path.GetFullPath(Settings.Default.Paths.Chain));
            system = new NeoSystem(store);
            system.StartNode(new ChannelsConfig
            {
                Tcp                      = new IPEndPoint(IPAddress.Any, Settings.Default.P2P.Port),
                WebSocket                = new IPEndPoint(IPAddress.Any, Settings.Default.P2P.WsPort),
                MinDesiredConnections    = Settings.Default.P2P.MinDesiredConnections,
                MaxConnections           = Settings.Default.P2P.MaxConnections,
                MaxConnectionsPerAddress = Settings.Default.P2P.MaxConnectionsPerAddress
            });
            if (Settings.Default.UnlockWallet.IsActive)
            {
                try
                {
                    Program.Wallet = OpenWallet(Settings.Default.UnlockWallet.Path, Settings.Default.UnlockWallet.Password);
                }
                catch (CryptographicException)
                {
                    Console.WriteLine($"failed to open file \"{Settings.Default.UnlockWallet.Path}\"");
                }
                if (Settings.Default.UnlockWallet.StartConsensus && Program.Wallet != null)
                {
                    OnStartConsensusCommand(null);
                }
            }
            if (useRPC)
            {
                system.StartRpc(Settings.Default.RPC.BindAddress,
                                Settings.Default.RPC.Port,
                                wallet: Program.Wallet,
                                sslCert: Settings.Default.RPC.SslCert,
                                password: Settings.Default.RPC.SslCertPassword,
                                maxGasInvoke: Settings.Default.RPC.MaxGasInvoke);
            }
        }
Example #6
0
        public async void Start(string[] args)
        {
            if (NeoSystem != null)
            {
                return;
            }
            bool verifyImport = true;

            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                case "/noverify":
                case "--noverify":
                    verifyImport = false;
                    break;

                case "/testnet":
                case "--testnet":
                case "-t":
                    ProtocolSettings.Initialize(new ConfigurationBuilder().AddJsonFile("protocol.testnet.json").Build());
                    Settings.Initialize(new ConfigurationBuilder().AddJsonFile("config.testnet.json").Build());
                    break;

                case "/mainnet":
                case "--mainnet":
                case "-m":
                    ProtocolSettings.Initialize(new ConfigurationBuilder().AddJsonFile("protocol.mainnet.json").Build());
                    Settings.Initialize(new ConfigurationBuilder().AddJsonFile("config.mainnet.json").Build());
                    break;
                }
            }
            NeoSystem = new NeoSystem(Settings.Default.Storage.Engine);

            foreach (var plugin in Plugin.Plugins)
            {
                // Register plugins commands

                RegisterCommand(plugin, plugin.Name);
            }

            using (IEnumerator <Block> blocksBeingImported = GetBlocksFromFile().GetEnumerator())
            {
                while (true)
                {
                    List <Block> blocksToImport = new List <Block>();
                    for (int i = 0; i < 10; i++)
                    {
                        if (!blocksBeingImported.MoveNext())
                        {
                            break;
                        }
                        blocksToImport.Add(blocksBeingImported.Current);
                    }
                    if (blocksToImport.Count == 0)
                    {
                        break;
                    }
                    await NeoSystem.Blockchain.Ask <Blockchain.ImportCompleted>(new Blockchain.Import
                    {
                        Blocks = blocksToImport,
                        Verify = verifyImport
                    });

                    if (NeoSystem is null)
                    {
                        return;
                    }
                }
            }
            NeoSystem.StartNode(new ChannelsConfig
            {
                Tcp                      = new IPEndPoint(IPAddress.Any, Settings.Default.P2P.Port),
                WebSocket                = new IPEndPoint(IPAddress.Any, Settings.Default.P2P.WsPort),
                MinDesiredConnections    = Settings.Default.P2P.MinDesiredConnections,
                MaxConnections           = Settings.Default.P2P.MaxConnections,
                MaxConnectionsPerAddress = Settings.Default.P2P.MaxConnectionsPerAddress
            });
            if (Settings.Default.UnlockWallet.IsActive)
            {
                try
                {
                    OpenWallet(Settings.Default.UnlockWallet.Path, Settings.Default.UnlockWallet.Password);
                }
                catch (FileNotFoundException)
                {
                    Console.WriteLine($"Warning: wallet file \"{Settings.Default.UnlockWallet.Path}\" not found.");
                }
                catch (System.Security.Cryptography.CryptographicException)
                {
                    Console.WriteLine($"Failed to open file \"{Settings.Default.UnlockWallet.Path}\"");
                }
                if (Settings.Default.UnlockWallet.StartConsensus && CurrentWallet != null)
                {
                    OnStartConsensusCommand();
                }
            }
        }