예제 #1
0
 public void CopyFrom(Network network)
 {
     if (network == null)
     {
         throw new ArgumentNullException(nameof(network));
     }
     _Base58Prefixes.Clear();
     _Bech32Prefixes.Clear();
     for (int i = 0; i < network.base58Prefixes.Length; i++)
     {
         SetBase58Bytes((Base58Type)i, network.base58Prefixes[i]);
     }
     for (int i = 0; i < network.bech32Encoders.Length; i++)
     {
         SetBech32((Bech32Type)i, network.bech32Encoders[i]);
     }
     SetConsensus(network.Consensus).
     SetGenesis(Encoders.Hex.EncodeData(network.GetGenesis().ToBytes())).
     SetMagic(_Magic).
     SetPort(network.DefaultPort).
     SetRPCPort(network.RPCPort);
     SetNetworkStringParser(network.NetworkStringParser);
     SetNetworkSet(network.NetworkSet);
     SetChainName(network.ChainName);
 }
예제 #2
0
        /// <summary>
        /// Register an immutable <see cref="Network"/> instance so it is queryable through <see cref="GetNetwork(string)"/> and <see cref="GetNetworks()"/>.
        /// </summary>
        public static Network Register(Network network)
        {
            IEnumerable <string> networkNames = network.AdditionalNames != null ? new[] { network.Name }.Concat(network.AdditionalNames) : new[] { network.Name };

            foreach (string networkName in networkNames)
            {
                // Performs a series of checks before registering the network to the list of available networks.
                if (string.IsNullOrEmpty(networkName))
                {
                    throw new InvalidOperationException("A network name needs to be provided.");
                }

                if (GetNetwork(networkName) != null)
                {
                    throw new InvalidOperationException("The network " + networkName + " is already registered.");
                }

                if (network.GetGenesis() == null)
                {
                    throw new InvalidOperationException("A genesis block needs to be provided.");
                }

                if (network.Consensus == null)
                {
                    throw new InvalidOperationException("A consensus needs to be provided.");
                }

                NetworksContainer.TryAdd(networkName.ToLowerInvariant(), network);
            }

            return(network);
        }
예제 #3
0
        /// <summary>
        /// Register an immutable <see cref="Network"/> instance so it is queryable through <see cref="GetNetwork(string)"/> and <see cref="GetNetworks()"/>.
        /// </summary>
        internal static Network Register(Network network, string nameOverride = null)
        {
            string networkName = !string.IsNullOrEmpty(nameOverride) ? nameOverride : network.Name;

            // Performs a series of checks before registering the network to the list of available networks.
            if (string.IsNullOrEmpty(networkName))
            {
                throw new InvalidOperationException("A network name needs to be provided.");
            }

            if (GetNetwork(networkName) != null)
            {
                throw new InvalidOperationException("The network " + networkName + " is already registered.");
            }

            if (network.GetGenesis() == null)
            {
                throw new InvalidOperationException("A genesis block needs to be provided.");
            }

            if (network.Consensus == null)
            {
                throw new InvalidOperationException("A consensus needs to be provided.");
            }

            NetworksContainer.TryAdd(networkName.ToLowerInvariant(), network);

            return(network);
        }
예제 #4
0
        public void CopyFrom(Network network)
        {
            if (network == null)
            {
                throw new ArgumentNullException("network");
            }

            this.Base58Prefixes.Clear();
            this.Bech32Prefixes.Clear();

            for (int i = 0; i < network.base58Prefixes.Length; i++)
            {
                this.SetBase58Bytes((Base58Type)i, network.base58Prefixes[i]);
            }

            for (int i = 0; i < network.bech32Encoders.Length; i++)
            {
                this.SetBech32((Bech32Type)i, network.bech32Encoders[i]);
            }

            this.SetConsensus(network.Consensus)
            .SetGenesis(network.GetGenesis())
            .SetMagic(this.Magic)
            .SetPort(network.DefaultPort)
            .SetRPCPort(network.RPCPort)
            .SetTxFees(network.MinTxFee, network.FallbackFee, network.MinRelayTxFee)
            .SetMaxTimeOffsetSeconds(network.MaxTimeOffsetSeconds)
            .SetMaxTipAge(network.MaxTipAge);
        }
예제 #5
0
 public ConcurrentChain(Network network)
 {
     if (network != null)
     {
         var genesis = network.GetGenesis();
         SetTip(new ChainedBlock(genesis.Header, 0));
     }
 }
        /// <summary>
        /// Check PoW and that the blocks connect correctly
        /// </summary>
        /// <param name="network">The network being used</param>
        /// <returns>True if PoW is correct</returns>
        public bool Validate(Network network)
        {
            if (network == null)
            {
                throw new ArgumentNullException(nameof(network));
            }
            var genesisCorrect = Height != 0 || HashBlock == network.GetGenesis().GetHash();

            return(genesisCorrect && Validate(network.Consensus));
        }
예제 #7
0
        /// <summary>
        /// Check PoW and that the blocks connect correctly
        /// </summary>
        /// <param name="network">The network being used</param>
        /// <returns>True if PoW is correct</returns>
        public bool Validate(Network network)
        {
            if (network == null)
            {
                throw new ArgumentNullException("network");
            }
            if (Block.BlockSignature)
            {
                return(BlockStake.Validate(network, this));
            }

            var genesisCorrect = Height != 0 || HashBlock == network.GetGenesis().GetHash();

            return(genesisCorrect && Validate(network.Consensus));
        }
예제 #8
0
        /// <summary>
        /// Check that the header is a valid block header including the work done for PoW blocks.
        /// </summary>
        /// <param name="network">The network to verify against.</param>
        /// <returns><c>true</c> if the header is a valid block header, <c>false</c> otherwise.</returns>
        public bool Validate(Network network)
        {
            if (network == null)
            {
                throw new ArgumentNullException("network");
            }

            if (network.Consensus.IsProofOfStake)
            {
                return(BlockStake.Validate(network, this));
            }

            bool genesisCorrect = (this.Height != 0) || this.HashBlock == network.GetGenesis().GetHash();

            return(genesisCorrect && this.Validate(network.Consensus));
        }
예제 #9
0
 public void CopyFrom(Network network)
 {
     if (network == null)
     {
         throw new ArgumentNullException("network");
     }
     _Base58Prefixes.Clear();
     for (int i = 0; i < network.base58Prefixes.Length; i++)
     {
         SetBase58Bytes((Base58Type)i, network.base58Prefixes[i]);
     }
     SetConsensus(network.Consensus).
     SetGenesis(network.GetGenesis()).
     SetMagic(_Magic).
     SetPort(network.DefaultPort).
     SetRPCPort(network.RPCPort);
 }
예제 #10
0
        /// <summary>
        /// Check PoW and that the blocks connect correctly
        /// </summary>
        /// <param name="network">The network being used</param>
        /// <returns>True if PoW is correct</returns>
        public bool Validate(Network network)
        {
            if (network == null)
            {
                throw new ArgumentNullException("network");
            }
            if (Height != 0 && Previous == null)
            {
                return(false);
            }
            var heightCorrect   = Height == 0 || Height == Previous.Height + 1;
            var genesisCorrect  = Height != 0 || HashBlock == network.GetGenesis().GetHash();
            var hashPrevCorrect = Height == 0 || Header.HashPrevBlock == Previous.HashBlock;
            var hashCorrect     = HashBlock == Header.GetHash();
            var workCorrect     = CheckProofOfWorkAndTarget(network);

            return(heightCorrect && genesisCorrect && hashPrevCorrect && hashCorrect && workCorrect);
        }
예제 #11
0
        /// <summary>
        /// Check PoW and that the blocks connect correctly
        /// </summary>
        /// <param name="network">The network being used</param>
        /// <param name="chainedHeader">Chained block header</param>
        /// <returns>True if PoW is correct</returns>
        public static bool Validate(Network network, ChainedHeader chainedHeader)
        {
            if (network == null)
            {
                throw new ArgumentNullException("network");
            }

            if (chainedHeader.Height != 0 && chainedHeader.Previous == null)
            {
                return(false);
            }

            bool heightCorrect   = chainedHeader.Height == 0 || chainedHeader.Height == chainedHeader.Previous.Height + 1;
            bool genesisCorrect  = chainedHeader.Height != 0 || chainedHeader.HashBlock == network.GetGenesis().GetHash();
            bool hashPrevCorrect = chainedHeader.Height == 0 || chainedHeader.Header.HashPrevBlock == chainedHeader.Previous.HashBlock;
            bool hashCorrect     = chainedHeader.HashBlock == chainedHeader.Header.GetHash();

            return(heightCorrect && genesisCorrect && hashPrevCorrect && hashCorrect);
        }
 public ConcurrentChain(Network network)
     : this(network.GetGenesis().Header, network)
 {
 }
예제 #13
0
        /// <summary>
        /// Check PoW and that the blocks connect correctly
        /// </summary>
        /// <param name="network">The network being used</param>
        /// <returns>True if PoW is correct</returns>
        public static bool Validate(Network network, ChainedBlock chainedBlock, StakeChain stakeChain = null)
        {
            if (network == null)
            {
                throw new ArgumentNullException("network");
            }
            if (chainedBlock.Height != 0 && chainedBlock.Previous == null)
            {
                return(false);
            }
            var heightCorrect   = chainedBlock.Height == 0 || chainedBlock.Height == chainedBlock.Previous.Height + 1;
            var genesisCorrect  = chainedBlock.Height != 0 || chainedBlock.HashBlock == network.GetGenesis().GetHash();
            var hashPrevCorrect = chainedBlock.Height == 0 || chainedBlock.Header.HashPrevBlock == chainedBlock.Previous.HashBlock;
            var hashCorrect     = chainedBlock.HashBlock == chainedBlock.Header.GetHash();

            if (stakeChain == null)
            {
                return(heightCorrect && genesisCorrect && hashPrevCorrect && hashCorrect);
            }

            var workCorrect = stakeChain.CheckPowPosAndTarget(chainedBlock, stakeChain.Get(chainedBlock.HashBlock), network);

            return(heightCorrect && genesisCorrect && hashPrevCorrect && hashCorrect && workCorrect);
        }
예제 #14
0
 public PersistantChain(Network network, ObjectStream <ChainChange> changes)
     : this(network.GetGenesis().Header, changes)
 {
 }
 public ConcurrentChain(Network network)
 {
     this.network = network;
     SetTip(new ChainedHeader(network.GetGenesis().Header, network.GetGenesis().GetHash(), 0));
 }
예제 #16
0
 public Chain BuildChain()
 {
     return(BuildChain(Network.GetGenesis().Header, 0));
 }
예제 #17
0
        public ChainIndexer(Network network) : this()
        {
            this.Network = network;

            Initialize(new ChainedHeader(network.GetGenesis().Header, network.GetGenesis().GetHash(), 0));
        }