コード例 #1
0
        public async Task Query(QueryRequest request, ChannelWriter <Page> channelWriter, ServerCallContext context)
        {
            var parameters = ParameterDecoder.DecodeParameters(request.Parameters);

            QueryResult queryResult;

            try
            {
                queryResult = await _koraliumExecutor.Execute(request.Query, parameters, context.GetHttpContext());
            }
            catch (AuthorizationFailedException authFailed)
            {
                throw new RpcException(new Status(StatusCode.Unauthenticated, authFailed.Message));
            }

            var enumerator = queryResult.Result.GetEnumerator();

            Page page = new Page()
            {
                Metadata = new QueryMetadata()
            };

            foreach (var metadata in queryResult.Metadata)
            {
                page.Metadata.CustomMetadata.Add(new KeyValue()
                {
                    Name = metadata.Key, Value = ScalarEncoder.EncodeScalarResult(metadata.Value)
                });
            }

            IEncoder[] encoders = new IEncoder[queryResult.Columns.Count];
            Func <object, object>[] propertyGetters = new Func <object, object> [encoders.Length];

            int indexCounter = 0;

            for (int i = 0; i < queryResult.Columns.Count; i++)
            {
                var column = queryResult.Columns[i];

                var columnMetadata = ToMetadata(ref indexCounter, column);

                encoders[i]        = EncoderHelper.GetEncoder(column.Type, columnMetadata, column.Children);
                propertyGetters[i] = column.GetFunction;
                page.Metadata.Columns.Add(columnMetadata);
            }
            System.Diagnostics.Stopwatch encodeWatch = new System.Diagnostics.Stopwatch();
            encodeWatch.Start();
            await EncoderHelper.ReadData(_logger, page, encoders, propertyGetters, enumerator, request.MaxBatchSize, channelWriter);

            encodeWatch.Stop();
            _logger.LogTrace($"Encode time: {encodeWatch.ElapsedMilliseconds} ms");
        }
コード例 #2
0
        private static AbiOutputParameter[] Decode(IEnumerable <IAbiParameterDefinition> parameters, byte[] data)
        {
            var outputs = new ParameterDecoder().DecodeDefaultData(
                data,
                parameters
                .Select(parameter => new Parameter(parameter.AbiType, parameter.Name))
                .ToArray());

            return(outputs.Select(output => new AbiOutputParameter(output.Parameter.Type, output.Parameter.Name)
            {
                Result = output.Result
            }).ToArray());
        }
コード例 #3
0
        public ValueTask <object> ExecuteScalar(QueryRequest queryRequest, ServerCallContext context)
        {
            var sqlParameters = ParameterDecoder.DecodeParameters(queryRequest.Parameters);

            Dictionary <string, object> extraDatas = new Dictionary <string, object>();

            foreach (var extraData in queryRequest.ExtraData)
            {
                extraDatas.Add(extraData.Name, ScalarDecoder.DecodeScalar(extraData.Value));
            }

            return(_koraliumExecutor.ExecuteScalar(queryRequest.Query, sqlParameters, context.GetHttpContext()));
        }
コード例 #4
0
 public TupleTypeDecoder()
 {
     parameterDecoder = new ParameterDecoder();
 }
コード例 #5
0
ファイル: Blockchain.cs プロジェクト: dumians/Conferences
        public static async Task <HttpResponseMessage> CreateRespone(HttpRequestMessage req, string functionName)
        {
            // Get request body
            var jsonBody = await req.Content.ReadAsStringAsync();

            if (string.IsNullOrWhiteSpace(jsonBody))
            {
                jsonBody = @"{}";
            }
            var body = JsonConvert.DeserializeObject <JObject>(jsonBody);

            // Get parameters
            var inputParameters = body.Values();
            var arguments       = new object[inputParameters.Count()];
            var i = 0;

            foreach (var p in inputParameters.Values())
            {
                arguments[i++] = p.Value <string>();
            }

            var web3     = new Nethereum.Web3.Web3(SmartContract.BlockchainRpcEndpoint);
            var contract = web3.Eth.GetContract(SmartContract.Abi, SmartContract.ContractAddress);

            var functionABI = contract.ContractBuilder.ContractABI.Functions
                              .FirstOrDefault(f => f.Name == functionName);

            if (functionABI == null)
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, "Function not found!"));
            }

            var functionParameters = functionABI.InputParameters;

            if (functionParameters?.Count() != inputParameters.Count())
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, "Parameters do not match!"));
            }

            Function function   = contract.GetFunction(functionName);
            Type     returnType = GetFunctionReturnType(functionABI);
            IEthCall ethCall    = contract.Eth.Transactions.Call;
            var      result     = await ethCall.SendRequestAsync(function.CreateCallInput(arguments), contract.Eth.DefaultBlock)
                                  .ConfigureAwait(false);

            FunctionBase functionBase        = function;
            PropertyInfo builderBaseProperty = functionBase.GetType()
                                               .GetProperty("FunctionBuilderBase", BindingFlags.Instance | BindingFlags.NonPublic);

            if (builderBaseProperty != null)
            {
                FunctionBuilderBase builderBase             = (FunctionBuilderBase)builderBaseProperty.GetValue(functionBase);
                PropertyInfo        funcCallDecoderProperty = builderBase.GetType()
                                                              .GetProperty("FunctionCallDecoder", BindingFlags.Instance | BindingFlags.NonPublic);
                if (funcCallDecoderProperty != null)
                {
                    ParameterDecoder decoder = (ParameterDecoder)funcCallDecoderProperty.GetValue(builderBase);
                    var results = decoder.DecodeDefaultData(result, functionABI.OutputParameters);

                    if (results.Count == 1)
                    {
                        var resultValue = JsonConvert.SerializeObject(results[0].Result);
                        return(req.CreateResponse(HttpStatusCode.OK, resultValue));
                    }

                    var resultMultiValue = Activator.CreateInstance(returnType, results.Select(r => r.Result).ToArray());
                    return(req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(resultMultiValue)));
                }
            }

            return(req.CreateResponse(HttpStatusCode.InternalServerError));
        }
        public async Task <HttpResponseMessage> CreateResponse(string parameters, string functionName)
        {
            HttpResponseMessage response = new HttpResponseMessage();

            var body = JsonConvert.DeserializeObject <JObject>(parameters);

            // Get parameters
            var inputParameters = body.Values();
            var arguments       = new object[inputParameters.Count()];
            var i = 0;

            foreach (var p in inputParameters.Values())
            {
                arguments[i++] = p.Value <string>();
            }

            Nethereum.Web3.Web3 web3     = new Nethereum.Web3.Web3(BlockchainRpcEndpoint);
            Contract            contract = web3.Eth.GetContract(Abi, ContractAddress);

            var functionABI = contract.ContractBuilder.ContractABI.Functions
                              .FirstOrDefault(f => f.Name == functionName);


            if (functionABI == null)
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            var functionParameters = functionABI.InputParameters;

            if (functionParameters?.Count() != inputParameters.Count())
            {
                var fp = functionParameters.Count();
                var ip = inputParameters.Count();
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            Function function   = contract.GetFunction(functionName);
            Type     returnType = GetFunctionReturnType(functionABI);
            EthCall  ethCall    = contract.Eth.Transactions.Call;
            var      result     = await ethCall.SendRequestAsync(function.CreateCallInput(arguments), contract.Eth.DefaultBlock).ConfigureAwait(false);

            FunctionBase functionBase        = function;
            PropertyInfo builderBaseProperty = functionBase.GetType()
                                               .GetProperty("FunctionBuilderBase", BindingFlags.Instance | BindingFlags.NonPublic);

            if (builderBaseProperty != null)
            {
                FunctionBuilderBase builderBase             = (FunctionBuilderBase)builderBaseProperty.GetValue(functionBase);
                PropertyInfo        funcCallDecoderProperty = builderBase.GetType()
                                                              .GetProperty("FunctionCallDecoder", BindingFlags.Instance | BindingFlags.NonPublic);
                if (funcCallDecoderProperty != null)
                {
                    ParameterDecoder decoder = (ParameterDecoder)funcCallDecoderProperty.GetValue(builderBase);
                    var results = decoder.DecodeDefaultData(result, functionABI.OutputParameters[0]);

                    if (results.Count == 1)
                    {
                        var resultValue = JsonConvert.SerializeObject(results[0].Result);
                        return(new HttpResponseMessage(HttpStatusCode.OK));
                    }

                    var resultMultiValue = Activator.CreateInstance(returnType, results.Select(r => r.Result).ToArray());
                    return(new HttpResponseMessage(HttpStatusCode.OK));
                }
            }

            return(new HttpResponseMessage(HttpStatusCode.OK));
        }