private void AddGraphToChain(IGraph g, DateTime genesisDate) { GraphBlock graphBlock = new GraphBlock(); graphBlock.PoAHeader.AuthorityHash = uint256.One; Encoding utf8Encoding = Encoding.UTF8; byte[] graphBytes; using (var s = new MemoryStream()) using (var sw = new StreamWriter(s)) { g.SaveToStream(sw, new CompressingTurtleWriter()); graphBytes = s.ToArray(); } GraphContent content = new GraphContent() { Version = 1, Headers = new GraphContentHeaders(), Graph = graphBytes }; content.Headers.AddHeader("GraphFormat", "ttl"); graphBlock.AddGraph(content); graphBlock.Header.Nonce = RandomUtils.GetUInt32(); graphBlock.Header.Version = 1; graphBlock.Header.Time = (uint)(DateTime.UtcNow.Ticks - genesisDate.Ticks); graphBlock.Header.HashPrevBlock = (_smallBlockChain.LastOrDefault()?.GetHash()) ?? uint256.Zero; graphBlock.UpdateMerkleRoot(); Assert.NotEqual(graphBlock.Header.GetPoWHash(), uint256.Zero); Assert.Equal(graphBlock.Header.HashMerkleRoot, graphBlock.GetMerkleRoot().Hash); _smallBlockChain.Add(graphBlock); }
/// <summary> /// Parse the document in order to proceed to the alimentation of all blocks into the given container. /// </summary> /// <param name="container">Container to build.</param> public virtual void ParseDocument(OpenXmlPartContainer container) { IList <BlockItem> blocks = GetBlocks(container); foreach (BlockItem block in blocks) { BlockConfiguration config = GetBlockConfiguration(block); try { if (TextBlock.IsMatching(config.Type)) { TextBlock.BuildContent(ReportData, container, block, config.Name, config.Options); } else if (TableBlock.IsMatching(config.Type)) { TableBlock.BuildContent(ReportData, container, block, config.Name, config.Options); } else if (GraphBlock.IsMatching(config.Type)) { GraphBlock.BuildContent(ReportData, Package, block, config.Name, config.Options); } else { LogHelper.Instance.LogWarnFormat("Block type '{0}' not found.", config.Type); } } catch (Exception exception) { string logMessage = $"Exception thrown during document parsing (BlockType : {(null != config ? config.Type : string.Empty)}, BlockName : {(null != config ? config.Name : string.Empty)})"; LogHelper.Instance.LogError(logMessage, exception); } } }
private void LoadBlockChainFromStrings() { var genesisDate = DateTime.UtcNow; var files = new DirectoryInfo(@"data\rdf\").GetFiles("*.ttl", SearchOption.TopDirectoryOnly); _filesCount = files.Length; fixture.StartWatch(); foreach (var file in files) { GraphBlock graphBlock = new GraphBlock(); graphBlock.PoAHeader.AuthorityHash = uint256.One; var graphString = File.ReadAllText(file.FullName); Encoding utf8Encoding = Encoding.UTF8; var graphBytes = utf8Encoding.GetBytes(graphString); GraphContent content = new GraphContent() { Version = 1, Headers = new GraphContentHeaders(), Graph = graphBytes }; content.Headers.AddHeader("GraphFormat", "ttl"); graphBlock.AddGraph(content); graphBlock.Header.Nonce = RandomUtils.GetUInt32(); graphBlock.Header.Version = 1; graphBlock.Header.Time = (uint)(DateTime.UtcNow.Ticks - genesisDate.Ticks); graphBlock.Header.HashPrevBlock = (_smallBlockChain.LastOrDefault()?.GetHash()) ?? uint256.Zero; graphBlock.UpdateMerkleRoot(); Assert.NotEqual(graphBlock.Header.GetPoWHash(), uint256.Zero); Assert.Equal(graphBlock.Header.HashMerkleRoot, graphBlock.GetMerkleRoot().Hash); _smallBlockChain.Add(graphBlock); } fixture.Log("Loaded {0} graphs into {1} blocks. Elapsed time: {2} miliseconds.", "LoadBlockChain", _filesCount, _smallBlockChain.Count, fixture.StopWatch()); }
public void CanCalculateMerkleRoot_Complex() { GraphBlock graphBlock = new GraphBlock(); graphBlock.PoAHeader.AuthorityHash = uint256.One; foreach (var file in new DirectoryInfo(@"data\rdf\").GetFiles()) { var graphString = File.ReadAllText(file.FullName); Encoding utf8Encoding = Encoding.UTF8; var graphBytes = utf8Encoding.GetBytes(graphString); GraphContent content = new GraphContent() { Version = 1, Headers = new GraphContentHeaders(), Graph = graphBytes }; content.Headers.AddHeader("GraphFormat", "ttl"); graphBlock.AddGraph(content); } fixture.StartWatch(); graphBlock.UpdateMerkleRoot(); Assert.Equal(graphBlock.Header.HashMerkleRoot, graphBlock.GetMerkleRoot().Hash); fixture.Log("Calculated merkle root hash for {0} graph's block. Elapsed time: {1} miliseconds.", "CanCalculateMerkleRoot_Complex", graphBlock.Graphs.Count, fixture.StopWatch()); }
private void CompromiseBlock(GraphBlock block) { var sourceGraphBytes = block.Graphs.First().Graph; var sourceGraphTurtle = _utf8Encoding.GetString(sourceGraphBytes); string compromisedGraphTurtle = sourceGraphTurtle.Replace("http://lei.info", "http://lei.inf"); var compromisedGraphBytes = _utf8Encoding.GetBytes(compromisedGraphTurtle); block.Graphs.First().Graph = compromisedGraphBytes; block.UpdateMerkleRoot(); }
public void CanCalculateMerkleRoot() { var graphString = File.ReadAllText(@"data\rdf\2594007XIACKNMUAW223.ttl"); var graphBytes = _utf8Encoding.GetBytes(graphString); GraphBlock graphBlock = new GraphBlock(); graphBlock.PoAHeader.AuthorityHash = uint256.One; GraphContent content = new GraphContent() { Version = 1, Headers = new GraphContentHeaders(), Graph = graphBytes }; content.Headers.AddHeader("GraphFormat", "ttl"); graphBlock.AddGraph(content); fixture.StartWatch(); graphBlock.UpdateMerkleRoot(); Assert.Equal(graphBlock.Header.HashMerkleRoot, graphBlock.GetMerkleRoot().Hash); fixture.Log("Calculated merkle root hash for single graph's block. Elapsed time: {0} miliseconds.", "CanCalculateMerkleRoot", fixture.StopWatch()); }
private void LoadBlockChainFromGraphs() { var genesisDate = DateTime.UtcNow; var files = new DirectoryInfo(@"data\rdf\").GetFiles("*.ttl", SearchOption.TopDirectoryOnly); _filesCount = files.Length; fixture.StartWatch(); foreach (var file in files) { GraphBlock graphBlock = new GraphBlock(); graphBlock.PoAHeader.AuthorityHash = uint256.One; var graphString = File.ReadAllText(file.FullName); IGraph graph = new Graph(); graph.BaseUri = new Uri(String.Format("graph://lei.info/{0}", Path.GetFileNameWithoutExtension(file.Name))); graph.LoadFromString(graphString, new TurtleParser()); MemoryStream graphStream = new MemoryStream(); TextWriter writer = new StreamWriter(graphStream); graph.SaveToStream(writer, new CompressingTurtleWriter()); var graphBytes = graphStream.ToArray(); GraphContent content = new GraphContent() { Version = 1, Headers = new GraphContentHeaders(), Graph = graphBytes }; content.Headers.AddHeader("GraphFormat", "ttl"); graphBlock.AddGraph(content); graphBlock.Header.Nonce = RandomUtils.GetUInt32(); graphBlock.Header.Version = 1; graphBlock.Header.Time = (uint)(DateTime.UtcNow.Ticks - genesisDate.Ticks); graphBlock.Header.HashPrevBlock = (_smallBlockChain.LastOrDefault()?.GetHash()) ?? uint256.Zero; graphBlock.UpdateMerkleRoot(); Assert.NotEqual(graphBlock.Header.GetPoWHash(), uint256.Zero); Assert.Equal(graphBlock.Header.HashMerkleRoot, graphBlock.GetMerkleRoot().Hash); _smallBlockChain.Add(graphBlock); } fixture.Log("Loaded {0} graphs into {1} blocks. Elapsed time: {2} miliseconds.", "LoadBlockChain", _filesCount, _smallBlockChain.Count, fixture.StopWatch()); }
public RootQuery(IGraphService queryService) { Field <StringGraphType>("bestblockhash", resolve: context => queryService.GetBestBlockHash()); Field <BlockType>("block", arguments: new QueryArguments( new QueryArgument <GraphQLPlugin.ModelType.UIntGraphType> { Name = "index" }, new QueryArgument <StringGraphType> { Name = "hash" } ), resolve: context => { string hash = context.GetArgument <string>("hash"); uint index = context.GetArgument <uint>("index"); bool isVerbose = true; try { if (hash != null) { return(GraphBlock.FromJson(queryService.GetBlock(hash, isVerbose))); } else { return(GraphBlock.FromJson(queryService.GetBlock(index, isVerbose))); } } catch (Exception ex) { context.Errors.Add(new ExecutionError(ex.Message)); return(false); } }); Field <IntGraphType>("blockcount", resolve: context => { return(queryService.GetBlockCount()); }); Field <StringGraphType>("blocksysfee", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <GraphQLPlugin.ModelType.UIntGraphType> > { Name = "index" } ), resolve: context => { var index = context.GetArgument <uint>("index"); return(queryService.GetBlockSysFee(index)); }); Field <TransactionsType>("transaction", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "txid" } ), resolve: context => { var txid = UInt256.Parse(context.GetArgument <string>("txid")); bool verbose = true; return(GraphTransaction.FromJson(queryService.GetRawTransaction(txid, verbose))); }); Field <GraphQLPlugin.ModelType.UIntGraphType>("transactionheight", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "txid" } ), resolve: context => { var txid = UInt256.Parse(context.GetArgument <string>("txid")); return(queryService.GetTransactionHeight(txid)); }); Field <ContractsType>("contract", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "scripthash" } ), resolve: context => { var script_hash = UInt160.Parse(context.GetArgument <string>("scripthash")); return(ContractState.FromJson(queryService.GetContractState(script_hash))); }); Field <ListGraphType <ValidatorsType> >("validators", resolve: context => { return(((JArray)queryService.GetValidators()).Select(p => GraphValidator.FromJson(p)).ToArray()); }); Field <PeersType>("peers", resolve: context => { return(GraphPeers.FromJson(queryService.GetPeers())); }); Field <VersionType>("version", resolve: context => { return(queryService.GetVersion()); }); Field <IntGraphType>("connectioncount", resolve: context => { return(queryService.GetConnectionCount()); }); Field <ListGraphType <PluginsType> >("plugins", resolve: context => { return(((JArray)queryService.ListPlugins()).Select(p => GraphPlugin.FromJson(p)).ToArray()); }); Field <BooleanGraphType>("addressverification", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "address" } ), resolve: context => { var address = context.GetArgument <string>("address"); return(queryService.ValidateAddress(address)["isvalid"].AsBoolean()); }); Field <InvokeResultType>("scriptinvocation", // arguments: new QueryArguments( new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "script" }, new QueryArgument <ListGraphType <StringGraphType> > { Name = "hashes" } ), resolve: context => { byte[] script = context.GetArgument <string>("script").HexToBytes(); var hashes = context.GetArgument <List <string> >("scripthashes")?.ToArray(); UInt160[] scriptHashesForVerifying = null; if (hashes != null && hashes.Length > 0) { scriptHashesForVerifying = hashes.Select(u => UInt160.Parse(u)).ToArray(); } return(GraphInvokeResult.FromJson(queryService.InvokeScript(script, scriptHashesForVerifying))); }); Field <InvokeResultType>("functioninvocation", // arguments: new QueryArguments( new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "scripthash" }, new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "operation" }, new QueryArgument <ListGraphType <RpcStackInputType> > { Name = "params" } ), resolve: context => { UInt160 script_hash = UInt160.Parse(context.GetArgument <string>("scripthash")); string operation = context.GetArgument <string>("operation"); ContractParameter[] args = context.GetArgument <List <RpcStack> >("params")?.Select(p => ContractParameter.FromJson(p.ToJson()))?.ToArray() ?? new ContractParameter[0]; return(GraphInvokeResult.FromJson(queryService.InvokeFunction(script_hash, operation, args))); }); Field <StringGraphType>("getstorage", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "scripthash" }, new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "key" } ), resolve: context => { var scriptHash = UInt160.Parse(context.GetArgument <string>("scripthash")); var key = context.GetArgument <string>("key").HexToBytes(); return(queryService.GetStorage(scriptHash, key)?.AsString()); }); Field <BooleanGraphType>("closewallet", resolve: context => { return(queryService.CloseWallet()); }); Field <StringGraphType>("dumpprivkey", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "address" } ), resolve: context => { var address = context.GetArgument <string>("address"); return(queryService.DumpPrivKey(address)); }); Field <StringGraphType>("getbalance", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "assetID" } ), resolve: context => { var assetID = context.GetArgument <string>("assetID"); return(queryService.GetBalance(assetID)); }); Field <StringGraphType>("newaddress", resolve: context => { return(queryService.GetNewAddress()); }); Field <StringGraphType>("unclaimedgas", resolve: context => { return(queryService.GetUnclaimedGas()); }); Field <AccountType>("importprivkey", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "privkey" } ), resolve: context => { var privkey = context.GetArgument <string>("privkey"); return(Account.FromJson(queryService.ImportPrivKey(privkey))); }); Field <ListGraphType <AccountType> >("listaddress", resolve: context => { return(((JArray)queryService.ListAddress()).Select(p => Account.FromJson(p)).ToArray()); }); Field <BooleanGraphType>("openwallet", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "path" }, new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "password" } ), resolve: context => { var path = context.GetArgument <string>("path"); var password = context.GetArgument <string>("password"); return(queryService.OpenWallet(path, password).AsBoolean()); }); }