Exemple #1
0
        /// <summary>
        /// When a block is created
        /// </summary>
        /// <param name="source">Source.</param>
        /// <param name="newIndex">the new block index</param>
        public async Task <bool> OnBlockCreated(IPeer <TData> source, int newIndex)
        {
            var currentIndex = Chains.Count - 1;
            var hasNewBlocks = newIndex > currentIndex;

            if (hasNewBlocks)
            {
                while (currentIndex > 0 &&
                       Chains[currentIndex].Hash != await source.GetBlockHash(currentIndex))
                {
                    currentIndex--;
                }

                currentIndex++;

                // download the new branch for replacement
                var newBlocks = await source.GetBlocks(currentIndex);

                Chains.RemoveRange(currentIndex, Chains.Count - currentIndex);
                Chains.AddRange(newBlocks);

                BroadCastNewBlockEvent(newIndex);
            }

            return(hasNewBlocks);
        }