コード例 #1
0
        private IList <Repository.Dto.BlockDto> GetLatest()
        {
            var block = repository.TryGetLastBlock();

            if (block == null)
            {
                throw new System.ApplicationException("No blocks");
            }

            return(new List <Repository.Dto.BlockDto>
            {
                block
            });
        }
コード例 #2
0
        private Tuple <bool, IList <string> > TryUploadBlock(int index,
                                                             string minedBy,
                                                             string data,
                                                             string previousHash,
                                                             int nonce,
                                                             string hash)
        {
            var lastBlock = _repository.TryGetLastBlock();

            if (lastBlock == null)
            {
                throw new ApplicationException("No blocks in blockchain");
            }

            Types.Block block = new Types.Block(
                index,
                minedBy,
                data,
                previousHash,
                nonce);

            var blockWithHash = new Types.BlockWithHash(block, hash);
            var isValidBlock  = Miner.isValidBlock(blockWithHash, DtoHelpers.DtoToBlock(lastBlock));

            if (isValidBlock.IsInvalid)
            {
                var invalid = isValidBlock as Types.IsValidBlock.Invalid;
                var errors  = invalid.Item.ToList();

                return(Tuple.Create <bool, IList <string> >(false, errors));
            }

            _repository.Save(DtoHelpers.BlockToDto(blockWithHash));

            return(Tuple.Create <bool, IList <string> >(true, new List <string>()));
        }