Esempio n. 1
0
        public Block DownloadChain(string address, string hash, Action <Block> Hook = null)
        {
            var maybeBlock = _tangleAccessor.GetSpecificFromAddress <Block>(hash, address);

            if (!maybeBlock.HasValue)
            {
                throw new ArgumentException("Provided Block doesnt exist");
            }

            var block = maybeBlock.Value;

            if (!block.Verify(_consensus.GetDifficulty(block.Height)))
            {
                throw new ArgumentException("Provided Block is NOT VALID!");
            }

            Hook?.Invoke(block);

            //we store first block! stupid hack
            _dataAccessor.AddBlock(block);

            while (true)
            {
                //first we need to get the correct way
                var startDifficulty = _consensus.GetDifficulty(block.Height + 1);
                var newBlocks       = _consensus.FindNewBlocks(block.NextAddress, block.Height + 1, startDifficulty);

                //we repeat the whole until we dont have a newer way
                if (newBlocks.Count == 0)
                {
                    break;
                }

                //we then download this whole chain
                newBlocks.ForEach(x => _dataAccessor.AddBlock(x));

                //we just jump to the latest block
                block = newBlocks.Last();

                Hook?.Invoke(block);
            }

            return(block);
        }