public List <BlockHeader> GetAllBlocks() { List <BlockHeader> blockHeaders = new List <BlockHeader>(); bool getPreviousBlock = true; LastBlockInfo lastBlockInfo = _repositoryManager.GetLastBlockInfo(); String blockIriToObtain = lastBlockInfo.BlockIri; while (getPreviousBlock) { Block block = GetBlockByIri(blockIriToObtain); blockHeaders.Add(block.BlockHeader); blockIriToObtain = block.BlockHeader.PreviousBlock; if (blockIriToObtain.Equals(block.BlockIri)) { _logger.LogDebug("Block '{0}' is the first block.", blockIriToObtain); getPreviousBlock = false; } } return(blockHeaders); }
public LastBlockInfo GetLastBlockInfo() { LastBlockInfo lastBlockInfo = _repository.GetLastBlockInfo(); if (lastBlockInfo == null) { throw new ReadingBlockException("Unable to create last block info."); } _logger.LogDebug("Last block info: {0}", lastBlockInfo); return(lastBlockInfo); }
public Block CreateBlock(string dataGraphIri, string rawRdf, RdfFormat rdfFormat) { _logger.LogDebug("Creating block with graphIri '{0}' and rdfFormat '{1}'...", dataGraphIri, rdfFormat.GetJenaName()); try { HashSet <Triple> triples = GetTriplesFromSerializedModel(rawRdf, rdfFormat); long newIndex = GenerateNewIndex(); string newBlockIri = GenerateNewBlockIri(newIndex); LastBlockInfo lastBlockInfo = _repositoryManager.GetLastBlockInfo(); string previousBlock = lastBlockInfo.BlockIri; string previousHash = lastBlockInfo.BlockHash; string timestamp = TimestampCreator.CreateTimestampString(); string dataHash = _hashingService.CalculateHash(triples); string stringToCalculateHash = (newIndex + previousBlock + previousHash + timestamp + dataGraphIri + dataHash).Trim(); string hash = _hashCalculator.CalculateHash(stringToCalculateHash).ToLower(); BlockHeader blockHeader = new BlockHeader( dataGraphIri, dataHash, hash, newIndex.ToString(), previousBlock, previousHash, timestamp); BlockContent blockContent = new BlockContent(dataGraphIri, triples); Block blockToStore = new Block(newBlockIri, blockHeader, blockContent); _repositoryManager.PersistBlock(blockToStore, true); // MAYBE: Maybe this should be obtained from Triple Store in order to avoid some kind of inconsistency. _lastIndex++; return(GetBlock(newIndex.ToString())); } catch (ReadingBlockException ex) { string msg = "Exception was thrown while getting information about the last block."; throw new CreatingBlockException(msg, ex); } catch (RdfSerializationException ex) { string msg = String.Format("Exception was thrown while deserializing RDF model from '{0}' format.", rdfFormat); throw new CreatingBlockException(msg, ex); } catch (CalculatingHashException ex) { throw new CreatingBlockException("Exception was thrown while calculating hash.", ex); } }
public GraphChainValidationResult ValidateGraphChain() { bool isValid = false; var blocks = new List <BlockValidationResult>(); try { LastBlockInfo lastBlockInfo = _repositoryManager.GetLastBlockInfo(); _logger.LogDebug("Last block IRI: '{0}'", lastBlockInfo.BlockIri); bool getPreviousBlock = true; string blockIriToObtain = lastBlockInfo.BlockIri; while (getPreviousBlock) { Block currentBlock = _repositoryManager.GetBlockByIri(blockIriToObtain); HashSet <Triple> triples = _hashingService.HandleTriplesBeforeHashing(currentBlock.BlockContent.Triples); string calculatedDataHash = _hashingService.CalculateHash(triples); BlockValidationResult blockValidationResult = _blockHashValidator.ValidateBlock(currentBlock, calculatedDataHash); if (blockValidationResult.IsValid) { _logger.LogInformation("Block '{0}' is valid.", currentBlock.BlockIri); } else { _logger.LogWarning("Block '{0}' is not valid. Details: {1}", currentBlock.BlockIri, blockValidationResult); getPreviousBlock = false; } blocks.Add(blockValidationResult); blockIriToObtain = currentBlock.BlockHeader.PreviousBlock; if (IsGenesisBlock(currentBlock)) { // for the first block IRI and its previous block IRI are the same, // so we do not go further _logger.LogDebug("Block '{0}' has '{1}' as the previous block; they are equal.", blockIriToObtain, currentBlock.BlockIri); getPreviousBlock = false; isValid = true; } } } catch (ReadingBlockException ex) { _logger.LogError("Exception was thrown while normalizing rdf graph.", ex); } catch (CalculatingHashException ex) { _logger.LogError("Exception was thrown while normalizing rdf graph.", ex); } return(new GraphChainValidationResult(isValid, blocks)); }
public void Init() { _logger.LogDebug("[CONFIG] Initializing the block service..."); try { LastBlockInfo lastBlockInfo = _repositoryManager.GetLastBlockInfo(); _lastIndex = Int64.Parse(lastBlockInfo.BlockIndex); } catch (ReadingBlockException ex) { _logger.LogError("Exception occurred while tried to determine last index.", ex); throw new Exception("Unable to determine the last index value. Aborting initialization of BlockService."); } _logger.LogInformation("The block service initialized successfully."); }
public async Task <Download <LastBlockInfo> > DownloadLastBlockInfo(ChainType chainType, int chainId, uint chainIndex) { try { var data = await DownloadBinary($"dynamic/{GetChainTypeName(chainType)}/{chainId}/{chainIndex}/block/lastblockinfo/result.data"); using (var unpacker = new Unpacker(data)) { var sliceInfo = new LastBlockInfo(unpacker); return(new Download <LastBlockInfo>(sliceInfo)); } } catch (Exception exception) { return(Download <LastBlockInfo> .HandleException(exception)); } }
public void UpdateLastBlockInfo(LastBlockInfo blockState) { if (blockState == null) { return; } if (BlockState == null) { BlockState = blockState; return; } if (blockState.LastBlockId > BlockState.LastBlockId) { BlockState = blockState; } }
public RemoteSyncItem(NodeClient client, LastBlockInfo blockState) { Client = client; LastBlockInfo = blockState; }