예제 #1
0
        private bool CalculateValidity()
        {
            string difficultyMarker = new string('0', this.Difficulty);

            for (int i = 0; i < this.Length; i++)
            {
                Bloq <T> currentBloq = this.chain[i];

                if (!currentBloq.Hash.StartsWith(difficultyMarker))
                {
                    return(false);
                }

                if (currentBloq.Hash != currentBloq.CalculateHash())
                {
                    return(false);
                }

                if (i < this.Length - 1)
                {
                    Bloq <T> nextBloq = this.chain[i + 1];
                    if (nextBloq.PreviousHash != currentBloq.Hash)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
예제 #2
0
        public void AddBloq(T data)
        {
            if (EqualityComparer <T> .Default.Equals(data, default(T)))
            {
                throw new ArgumentException("The data you are trying to add is the default for the type. Use actual data for the bloq", nameof(data));
            }

            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            Bloq <T> bloqToAdd = new Bloq <T>(this.Length, 0, DateTime.UtcNow, data, this.chain[this.Length - 1].Hash);

            bloqToAdd.Mine(this.Difficulty);

            this.chain.Add(bloqToAdd);
        }
예제 #3
0
        public BloqChain(int difficulty)
        {
            if (difficulty < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(difficulty), "Difficulty must be zero a positive number");
            }

            this.Difficulty = difficulty;

            var genesisBloq = new Bloq <T>();

            genesisBloq.Mine(this.Difficulty);

            this.chain = new List <Bloq <T> >
            {
                genesisBloq
            };
        }