private BaseResponse <bool> AcceptBlock(DAM.Block.BlockBase incomingBlock, string senderNodeId) { if (incomingBlock == null) { return(new ErrorResponse <bool>("The incoming block cannot be null!", false)); } if (_blockchainService.BlockExists(incomingBlock.UniqueId)) { _statisticService.RegisterRejectedBlock(); return(new ErrorResponse <bool>("The block already exists!", false)); } var mappedBlock = LocalMapper.Map <BlockBase>(incomingBlock); if (incomingBlock is DAM.Block.Block block && !incomingBlock.IsGenesis) { var parentBlockValidationResult = ValidateParentBlock(block, senderNodeId); if (!parentBlockValidationResult.IsSuccess) { _statisticService.RegisterRejectedBlock(); return(parentBlockValidationResult); } var parentBlock = _blockchainService.GetBlock(block.ParentUniqueId); if (parentBlock == null || parentBlock.Depth + 1 != mappedBlock.Depth) { _statisticService.RegisterRejectedBlock(); return(new ErrorResponse <bool>("The depth of incoming block is incorrect!", false)); } var mappedParentBlock = LocalMapper.Map <BlockBase>(parentBlock); ((Block)mappedBlock).Parent = mappedParentBlock; } var duplicatesValidationResult = ValidateTransactionsDuplicates(mappedBlock); if (!duplicatesValidationResult.IsSuccess) { return(duplicatesValidationResult); } var validationResult = _blockchainValidator.Validate(mappedBlock); if (!validationResult.IsSuccess) { _statisticService.RegisterRejectedBlock(); return(new ErrorResponse <bool>("The validation for the block failed!", false, validationResult.Errors)); } _blockchainService.AddBlock(incomingBlock); return(new SuccessResponse <bool>("The block has been accepted and appended!", true)); }
private void DistributeBlock(DAM.Block.BlockBase blockBase) { var blockJson = JsonConvert.SerializeObject(blockBase); var base64String = Convert.ToBase64String(Encoding.UTF8.GetBytes(blockJson)); var encodedBlock = new EncodedBlock { Id = Guid.NewGuid().ToString(), Base64Block = base64String, NodeSenderId = BlockchainNodeConfiguration.NodeId, NodesAcceptedIds = new List <string>() }; DistributeBlock(encodedBlock); }