示例#1
0
        /// <inheritdoc />
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var network = (Network)value;

            string networkName = null;

            if (network == NetworkRegistration.GetNetwork("MainNet"))
            {
                networkName = "MainNet";
            }
            else if (network == NetworkRegistration.GetNetwork("TestNet"))
            {
                networkName = "TestNet";
            }
            else if (network == NetworkRegistration.GetNetwork("RegTest"))
            {
                networkName = "RegTest";
            }
            else if (network != null)
            {
                networkName = network.ToString();
            }

            if (networkName != null)
            {
                writer.WriteValue(networkName);
            }
        }
示例#2
0
        /// <summary>
        ///     Get the network on which to operate.
        /// </summary>
        /// <param name="network">The network</param>
        /// <returns>A <see cref="Network" /> object.</returns>
        public static Network GetNetwork(string network)
        {
            Guard.NotEmpty(network, nameof(network));

            var selectNetwork = NetworkRegistration.GetNetwork(network.ToLowerInvariant());

            if (selectNetwork == null)
            {
                throw new ArgumentException($"Network '{network}' is not a valid network.");
            }

            return(selectNetwork);
        }
示例#3
0
        public void CanGetNetworkFromName()
        {
            Network redstoneMain    = RedstoneNetworks.Main;
            Network redstoneTest    = RedstoneNetworks.TestNet;
            Network redstoneRegtest = RedstoneNetworks.RegTest;

            Assert.Equal(NetworkRegistration.GetNetwork("redstoneMain"), redstoneMain);
            Assert.Equal(NetworkRegistration.GetNetwork("RedstoneMain"), redstoneMain);
            Assert.Equal(NetworkRegistration.GetNetwork("RedstoneTest"), redstoneTest);
            Assert.Equal(NetworkRegistration.GetNetwork("RedstoneTest"), redstoneTest);
            Assert.Equal(NetworkRegistration.GetNetwork("redstoneRegtest"), redstoneRegtest);
            Assert.Equal(NetworkRegistration.GetNetwork("RedstoneRegtest"), redstoneRegtest);
            Assert.Null(NetworkRegistration.GetNetwork("invalid"));
        }
示例#4
0
 public static void ReadWriteC(this BitcoinStream bs, ref Network network)
 {
     if (bs.Serializing)
     {
         var str = network.ToString();
         bs.ReadWriteC(ref str);
     }
     else
     {
         var str = string.Empty;
         bs.ReadWriteC(ref str);
         network = NetworkRegistration.GetNetwork(str);
     }
 }
示例#5
0
        public void Validate_AllowClientConnection_State(bool inIBD, bool isWhiteListed, bool closeClient)
        {
            // Arrange
            var networkPeerFactory = new Mock <INetworkPeerFactory>();

            networkPeerFactory.Setup(npf => npf.CreateConnectedNetworkPeerAsync(It.IsAny <IPEndPoint>(),
                                                                                It.IsAny <NetworkPeerConnectionParameters>(),
                                                                                It.IsAny <NetworkPeerDisposer>())).Returns(Task.FromResult(new Mock <INetworkPeer>().Object));

            var initialBlockDownloadState = new Mock <IInitialBlockDownloadState>();

            initialBlockDownloadState.Setup(i => i.IsInitialBlockDownload()).Returns(inIBD);

            var nodeSettings = new NodeSettings(NetworkRegistration.GetNetwork("RegTest"));
            var connectionManagerSettings = new ConnectionManagerSettings(nodeSettings);

            var endpointAddNode = new IPEndPoint(IPAddress.Parse("::ffff:192.168.0.1"), 80);

            var networkPeerServer = new NetworkPeerServer(this.Network,
                                                          endpointAddNode, endpointAddNode, ProtocolVersion.PROTOCOL_VERSION, this.extendedLoggerFactory,
                                                          networkPeerFactory.Object, initialBlockDownloadState.Object, connectionManagerSettings);

            // Mimic external client
            const int portNumber = 80;
            var       client     = new TcpClient("www.stratisplatform.com", portNumber);

            var ipandport = client.Client.RemoteEndPoint.ToString();
            var ip        = ipandport.Replace(ipandport.Substring(ipandport.IndexOf(':')), "");

            var endpointDiscovered = new IPEndPoint(IPAddress.Parse(ip), portNumber);

            // Include the external client as a NodeServerEndpoint.
            connectionManagerSettings.Listen.Add(new NodeServerEndpoint(endpointDiscovered, isWhiteListed));

            // Act
            var result = networkPeerServer.InvokeMethod("AllowClientConnection", client);

            // Assert
            Assert.True((inIBD && !isWhiteListed) == closeClient);

            this.testOutput.WriteLine(
                $"In IBD : {inIBD.ToString()}, " +
                $"Is White Listed : {isWhiteListed.ToString()}, " +
                $"Close Client : {result.ToString()}");
        }
        /// <inheritdoc />
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
                                        JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            var networkName = (string)reader.Value;

            if (networkName == null)
            {
                return(null);
            }

            if (networkName.Equals("MainNet", StringComparison.OrdinalIgnoreCase) ||
                networkName.Equals("main", StringComparison.OrdinalIgnoreCase))
            {
                return(NetworkRegistration.GetNetwork("MainNet"));
            }

            if (networkName.Equals("TestNet", StringComparison.OrdinalIgnoreCase) ||
                networkName.Equals("test", StringComparison.OrdinalIgnoreCase))
            {
                return(NetworkRegistration.GetNetwork("TestNet"));
            }

            if (networkName.Equals("RegTest", StringComparison.OrdinalIgnoreCase) ||
                networkName.Equals("reg", StringComparison.OrdinalIgnoreCase))
            {
                return(NetworkRegistration.GetNetwork("RegTest"));
            }

            var network = NetworkRegistration.GetNetwork(networkName);

            if (networkName != null)
            {
                return(networkName);
            }

            throw new JsonObjectException("Unknown network (valid values : main, test, reg)", reader);
        }
        public void CanGetNetworkFromName()
        {
            Network bitcoinMain    = KnownNetworks.Main;
            Network bitcoinTestnet = KnownNetworks.TestNet;
            Network bitcoinRegtest = KnownNetworks.RegTest;

            Assert.Equal(NetworkRegistration.GetNetwork("main"), bitcoinMain);
            Assert.Equal(NetworkRegistration.GetNetwork("mainnet"), bitcoinMain);
            Assert.Equal(NetworkRegistration.GetNetwork("MainNet"), bitcoinMain);
            Assert.Equal(NetworkRegistration.GetNetwork("test"), bitcoinTestnet);
            Assert.Equal(NetworkRegistration.GetNetwork("testnet"), bitcoinTestnet);
            Assert.Equal(NetworkRegistration.GetNetwork("regtest"), bitcoinRegtest);
            Assert.Equal(NetworkRegistration.GetNetwork("reg"), bitcoinRegtest);
            Assert.Equal(NetworkRegistration.GetNetwork("straxmain"), this.straxMain);
            Assert.Equal(NetworkRegistration.GetNetwork("StraxMain"), this.straxMain);
            Assert.Equal(NetworkRegistration.GetNetwork("StraxTest"), this.straxTest);
            Assert.Equal(NetworkRegistration.GetNetwork("straxtest"), this.straxTest);
            Assert.Equal(NetworkRegistration.GetNetwork("StraxRegTest"), this.straxRegTest);
            Assert.Equal(NetworkRegistration.GetNetwork("straxregtest"), this.straxRegTest);
            Assert.Null(NetworkRegistration.GetNetwork("invalid"));
        }
        public void BitcoinMainnetIsInitializedCorrectly()
        {
            Assert.Equal(17, this.networkMain.Checkpoints.Count);
            Assert.Equal(6, this.networkMain.DNSSeeds.Count);
            Assert.Equal(512, this.networkMain.SeedNodes.Count);

            Assert.Equal(NetworkRegistration.GetNetwork("main"), this.networkMain);
            Assert.Equal(NetworkRegistration.GetNetwork("mainnet"), this.networkMain);

            Assert.Equal("Main", this.networkMain.Name);
            Assert.Equal(BitcoinMain.BitcoinRootFolderName, this.networkMain.RootFolderName);
            Assert.Equal(BitcoinMain.BitcoinDefaultConfigFilename, this.networkMain.DefaultConfigFilename);
            Assert.Equal(0xD9B4BEF9, this.networkMain.Magic);
            Assert.Equal(8333, this.networkMain.DefaultPort);
            Assert.Equal(8332, this.networkMain.DefaultRPCPort);
            Assert.Equal(BitcoinMain.BitcoinMaxTimeOffsetSeconds, this.networkMain.MaxTimeOffsetSeconds);
            Assert.Equal(BitcoinMain.BitcoinDefaultMaxTipAgeInSeconds, this.networkMain.MaxTipAge);
            Assert.Equal(1000, this.networkMain.MinTxFee);
            Assert.Equal(20000, this.networkMain.FallbackFee);
            Assert.Equal(1000, this.networkMain.MinRelayTxFee);
            Assert.Equal("BTC", this.networkMain.CoinTicker);

            Assert.Equal(2, this.networkMain.Bech32Encoders.Length);
            Assert.Equal(new Bech32Encoder("bc").ToString(), this.networkMain.Bech32Encoders[(int)Bech32Type.WITNESS_PUBKEY_ADDRESS].ToString());
            Assert.Equal(new Bech32Encoder("bc").ToString(), this.networkMain.Bech32Encoders[(int)Bech32Type.WITNESS_SCRIPT_ADDRESS].ToString());

            Assert.Equal(12, this.networkMain.Base58Prefixes.Length);
            Assert.Equal(new byte[] { 0 }, this.networkMain.Base58Prefixes[(int)Base58Type.PUBKEY_ADDRESS]);
            Assert.Equal(new byte[] { (5) }, this.networkMain.Base58Prefixes[(int)Base58Type.SCRIPT_ADDRESS]);
            Assert.Equal(new byte[] { (128) }, this.networkMain.Base58Prefixes[(int)Base58Type.SECRET_KEY]);
            Assert.Equal(new byte[] { 0x01, 0x42 }, this.networkMain.Base58Prefixes[(int)Base58Type.ENCRYPTED_SECRET_KEY_NO_EC]);
            Assert.Equal(new byte[] { 0x01, 0x43 }, this.networkMain.Base58Prefixes[(int)Base58Type.ENCRYPTED_SECRET_KEY_EC]);
            Assert.Equal(new byte[] { (0x04), (0x88), (0xB2), (0x1E) }, this.networkMain.Base58Prefixes[(int)Base58Type.EXT_PUBLIC_KEY]);
            Assert.Equal(new byte[] { (0x04), (0x88), (0xAD), (0xE4) }, this.networkMain.Base58Prefixes[(int)Base58Type.EXT_SECRET_KEY]);
            Assert.Equal(new byte[] { 0x2C, 0xE9, 0xB3, 0xE1, 0xFF, 0x39, 0xE2 }, this.networkMain.Base58Prefixes[(int)Base58Type.PASSPHRASE_CODE]);
            Assert.Equal(new byte[] { 0x64, 0x3B, 0xF6, 0xA8, 0x9A }, this.networkMain.Base58Prefixes[(int)Base58Type.CONFIRMATION_CODE]);
            Assert.Equal(new byte[] { 0x2a }, this.networkMain.Base58Prefixes[(int)Base58Type.STEALTH_ADDRESS]);
            Assert.Equal(new byte[] { 23 }, this.networkMain.Base58Prefixes[(int)Base58Type.ASSET_ID]);
            Assert.Equal(new byte[] { 0x13 }, this.networkMain.Base58Prefixes[(int)Base58Type.COLORED_ADDRESS]);

            Assert.Equal(210000, this.networkMain.Consensus.SubsidyHalvingInterval);
            Assert.Equal(750, this.networkMain.Consensus.MajorityEnforceBlockUpgrade);
            Assert.Equal(950, this.networkMain.Consensus.MajorityRejectBlockOutdated);
            Assert.Equal(1000, this.networkMain.Consensus.MajorityWindow);
            Assert.Equal(227931, this.networkMain.Consensus.BuriedDeployments[BuriedDeployments.BIP34]);
            Assert.Equal(388381, this.networkMain.Consensus.BuriedDeployments[BuriedDeployments.BIP65]);
            Assert.Equal(363725, this.networkMain.Consensus.BuriedDeployments[BuriedDeployments.BIP66]);
            Assert.Equal(new uint256("0x000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8"), this.networkMain.Consensus.BIP34Hash);
            Assert.Equal(new Target(new uint256("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff")), this.networkMain.Consensus.PowLimit);
            Assert.Equal(new uint256("0x0000000000000000000000000000000000000000002cb971dd56d1c583c20f90"), this.networkMain.Consensus.MinimumChainWork);
            Assert.Equal(TimeSpan.FromSeconds(14 * 24 * 60 * 60), this.networkMain.Consensus.PowTargetTimespan);
            Assert.Equal(TimeSpan.FromSeconds(10 * 60), this.networkMain.Consensus.TargetSpacing);
            Assert.False(this.networkMain.Consensus.PowAllowMinDifficultyBlocks);
            Assert.False(this.networkMain.Consensus.PowNoRetargeting);
            Assert.Equal(2016, this.networkMain.Consensus.MinerConfirmationWindow);
            Assert.Equal(28, this.networkMain.Consensus.BIP9Deployments[BitcoinBIP9Deployments.TestDummy].Bit);
            Assert.Equal(Utils.UnixTimeToDateTime(1199145601), this.networkMain.Consensus.BIP9Deployments[BitcoinBIP9Deployments.TestDummy].StartTime);
            Assert.Equal(Utils.UnixTimeToDateTime(1230767999), this.networkMain.Consensus.BIP9Deployments[BitcoinBIP9Deployments.TestDummy].Timeout);
            Assert.Equal(0, this.networkMain.Consensus.BIP9Deployments[BitcoinBIP9Deployments.CSV].Bit);
            Assert.Equal(Utils.UnixTimeToDateTime(1462060800), this.networkMain.Consensus.BIP9Deployments[BitcoinBIP9Deployments.CSV].StartTime);
            Assert.Equal(Utils.UnixTimeToDateTime(1493596800), this.networkMain.Consensus.BIP9Deployments[BitcoinBIP9Deployments.CSV].Timeout);
            Assert.Equal(1, this.networkMain.Consensus.BIP9Deployments[BitcoinBIP9Deployments.Segwit].Bit);
            Assert.Equal(Utils.UnixTimeToDateTime(1479168000), this.networkMain.Consensus.BIP9Deployments[BitcoinBIP9Deployments.Segwit].StartTime);
            Assert.Equal(Utils.UnixTimeToDateTime(1510704000), this.networkMain.Consensus.BIP9Deployments[BitcoinBIP9Deployments.Segwit].Timeout);
            Assert.Equal(0, this.networkMain.Consensus.CoinType);
            Assert.False(this.networkMain.Consensus.IsProofOfStake);
            Assert.Equal(new uint256("0x0000000000000000000f1c54590ee18d15ec70e68c8cd4cfbadb1b4f11697eee"), this.networkMain.Consensus.DefaultAssumeValid);
            Assert.Equal(100, this.networkMain.Consensus.CoinbaseMaturity);
            Assert.Equal(0, this.networkMain.Consensus.PremineReward);
            Assert.Equal(0, this.networkMain.Consensus.PremineHeight);
            Assert.Equal(Money.Coins(50), this.networkMain.Consensus.ProofOfWorkReward);
            Assert.Equal(Money.Zero, this.networkMain.Consensus.ProofOfStakeReward);
            Assert.Equal((uint)0, this.networkMain.Consensus.MaxReorgLength);
            Assert.Equal(21000000 * Money.COIN, this.networkMain.Consensus.MaxMoney);

            Block genesis = this.networkMain.GetGenesis();

            Assert.Equal(uint256.Parse("0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"), genesis.GetHash());
            Assert.Equal(uint256.Parse("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"), genesis.Header.HashMerkleRoot);
        }