private void CreateGenesisBlock() { string dataGraphIri = DEFAULT_GENESIS_BLOCK_DATA_GRAPH_IRI; string newIndex = "0"; string newBlockIri = _chainGraphIri + "/" + newIndex; var deserializedModel = _dotNetRdfSerializationHandler.DeserializeGraph(dataGraphIri, GetRawContentOfGcoOntology(), RdfFormat.TURTLE); var triples = _dotNetRdfMapper.GraphToTriples(deserializedModel); string previousBlock = newBlockIri; string previousHash = DEFAULT_GENESIS_BLOCK_PREVIOUS_HASH; string timestamp = DEFAULT_GENESIS_BLOCK_TIME_STAMP; string dataHash = _hashingService.CalculateHash(triples); string stringToCalculateHash = (newIndex + newBlockIri + previousHash + timestamp + dataGraphIri + dataHash).Trim(); string hash = _hashCalculator.CalculateHash(stringToCalculateHash).ToLower(); BlockHeader blockHeader = new BlockHeader( dataGraphIri, dataHash, hash, newIndex, previousBlock, previousHash, timestamp); BlockContent blockContent = new BlockContent(dataGraphIri, triples); Block genesisBlock = new Block(newBlockIri, blockHeader, blockContent, true); PersistBlock(genesisBlock, true); }
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 RunBenchmark(Dictionary <string, string> filesContent) { foreach (var fileContentEntry in filesContent) { try { VDS.RDF.IGraph deserializedModel = rdf4jSerializationHandler.DeserializeGraph( fileContentEntry.Key, fileContentEntry.Value, RdfFormat.TURTLE); HashSet <Triple> triples = rdf4jMapper.GraphToTriples(deserializedModel); _hashingService.CalculateHash(triples); } catch (RdfSerializationException ex) { throw new BenchmarkException("Exception: ", ex); } catch (CalculatingHashException ex) { throw new BenchmarkException("Exception: ", ex); } } }