Example #1
0
        /// <summary>
        /// Internal implementation of delete topic method.
        /// </summary>
        private async Task <TransactionReceipt> DeleteTopicImplementationAsync(Address topicToDelete, Signatory?signatory, Action <IContext>?configure)
        {
            topicToDelete           = RequireInputParameter.AddressToDelete(topicToDelete);
            await using var context = CreateChildContext(configure);
            RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var signatories     = Transactions.GatherSignatories(context, signatory);
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateTransactionBody(context, transactionId);

            transactionBody.ConsensusDeleteTopic = new ConsensusDeleteTopicTransactionBody
            {
                TopicID = Protobuf.ToTopicID(topicToDelete)
            };
            var request = await Transactions.SignTransactionAsync(transactionBody, signatories);

            var precheck = await Transactions.ExecuteSignedRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to Delete Topic, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TransactionReceipt();

            Protobuf.FillReceiptProperties(transactionId, receipt, result);
            return(result);
Example #2
0
        /// <summary>
        /// Retreives the accounts that are proxy staking to this account.
        /// </summary>
        /// <param name="address">
        /// The Hedera Network Address to retrieve the stakers of.
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// A dictionary mapping account addresses to the amount of stake.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        ///
        /// <remarks>
        /// Marked internal at this point because it is not yet implemented by the network
        /// </remarks>
        internal async Task <Dictionary <Address, long> > GetStakers(Address address, Action <IContext>?configure = null)
        {
            address = RequireInputParameter.Address(address);
            await using var context = CreateChildContext(configure);
            var query = new Query
            {
                CryptoGetProxyStakers = new CryptoGetStakersQuery
                {
                    Header    = Transactions.CreateAskCostHeader(),
                    AccountID = Protobuf.ToAccountID(address)
                }
            };
            var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

            long cost = (long)response.CryptoGetProxyStakers.Header.Cost;

            if (cost > 0)
            {
                var transactionId = Transactions.GetOrCreateTransactionID(context);
                query.CryptoGetProxyStakers.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost, transactionId);

                response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

                ValidateResult.ResponseHeader(transactionId, getResponseHeader(response));
            }
            return(response.CryptoGetProxyStakers.Stakers.ProxyStaker.ToDictionary(ps => Protobuf.FromAccountID(ps.AccountID), ps => ps.Amount));
Example #3
0
        /// <summary>
        /// Retrieves the contents of a file from the network.
        /// </summary>
        /// <param name="file">
        /// The address of the file contents to retrieve.
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// The contents of the file as a blob of bytes.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        /// <exception cref="ConsensusException">If the network was unable to come to consensus before the duration of the transaction expired.</exception>
        /// <exception cref="TransactionException">If the network rejected the create request as invalid or had missing data.</exception>
        public async Task <ReadOnlyMemory <byte> > GetFileContentAsync(Address file, Action <IContext>?configure = null)
        {
            file = RequireInputParameter.File(file);
            await using var context = CreateChildContext(configure);
            var query = new Query
            {
                FileGetContents = new FileGetContentsQuery
                {
                    Header = Transactions.CreateAskCostHeader(),
                    FileID = Protobuf.ToFileId(file)
                }
            };
            var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

            long cost = (long)response.FileGetContents.Header.Cost;

            if (cost > 0)
            {
                var transactionId = Transactions.GetOrCreateTransactionID(context);
                query.FileGetContents.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost, transactionId);

                response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

                ValidateResult.ResponseHeader(transactionId, getResponseHeader(response));
            }
            return(new ReadOnlyMemory <byte>(response.FileGetContents.FileContents.Contents.ToByteArray()));
Example #4
0
        /// <summary>
        /// Retrieves the bytecode for the specified contract.
        /// </summary>
        /// <param name="contract">
        /// The Hedera Network Address of the Contract.
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// The bytecode for the specified contract instance.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        ///
        /// <remarks>
        /// For now this is marked internal because this method cannot be implemented
        /// in a safe maner that does not waste hBars.  The cost of the query is variable
        /// and there is no public information on how to efficiently compute the cost prior
        /// to calling the method.  So the query fee must be in excess of the actual cost.
        /// </remarks>
        internal async Task <ReadOnlyMemory <byte> > GetContractBytecodeAsync(Address contract, Action <IContext>?configure = null)
        {
            contract = RequireInputParameter.Contract(contract);
            await using var context = CreateChildContext(configure);
            var query = new Query
            {
                ContractGetBytecode = new ContractGetBytecodeQuery
                {
                    Header     = Transactions.CreateAskCostHeader(),
                    ContractID = Protobuf.ToContractID(contract)
                }
            };
            var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

            long cost = (long)response.ContractGetBytecodeResponse.Header.Cost;

            if (cost > 0)
            {
                var transactionId = Transactions.GetOrCreateTransactionID(context);
                query.ContractGetBytecode.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost, transactionId);

                response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

                ValidateResult.ResponseHeader(transactionId, getResponseHeader(response));
            }
            return(response.ContractGetBytecodeResponse.Bytecode.ToByteArray());
Example #5
0
        /// <summary>
        /// Retrieves the transaction record for a given transaction ID.
        /// </summary>
        /// <param name="transaction">
        /// Transaction identifier of the record
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// A transaction record with the specified id, or an exception if not found.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        /// <exception cref="TransactionException">If the network has no record of the transaction or request has invalid or had missing data.</exception>
        public async Task <TransactionRecord> GetTransactionRecordAsync(TxId transaction, Action <IContext>?configure = null)
        {
            transaction             = RequireInputParameter.Transaction(transaction);
            await using var context = CreateChildContext(configure);
            var transactionId = Protobuf.ToTransactionID(transaction);
            // For the public version of this method, we do not know
            // if the transaction in question has come to consensus so
            // we need to get the receipt first (and wait if necessary).
            var query = new Query
            {
                TransactionGetReceipt = new TransactionGetReceiptQuery
                {
                    TransactionID = transactionId
                }
            };
            await Transactions.ExecuteNetworkRequestWithRetryAsync(context, query, getServerMethod, shouldRetry);

            // The Receipt status returned does notmatter in this case.
            // We may be retrieving a failed record (the status would not equal OK).
            var record = await GetTransactionRecordAsync(context, transactionId);

            var result = new TransactionRecord();

            Protobuf.FillRecordProperties(record, result);
            return(result);
Example #6
0
        /// <summary>
        /// Retrieves the records associated with an contract that are presently
        /// held within the network.
        /// </summary>
        /// <param name="contract">
        /// The Hedera Network Contract Address to retrieve associated records.
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// A detailed description of the account.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        public async Task <TransactionRecord[]> GetContractRecordsAsync(Address contract, Action <IContext>?configure = null)
        {
            contract = RequireInputParameter.Contract(contract);
            await using var context = CreateChildContext(configure);
            var query = new Query
            {
                ContractGetRecords = new ContractGetRecordsQuery
                {
                    Header     = Transactions.CreateAskCostHeader(),
                    ContractID = Protobuf.ToContractID(contract)
                }
            };
            var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

            long cost = (long)response.ContractGetRecordsResponse.Header.Cost;

            if (cost > 0)
            {
                var transactionId = Transactions.GetOrCreateTransactionID(context);
                query.ContractGetRecords.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost, transactionId);

                response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

                ValidateResult.ResponseHeader(transactionId, getResponseHeader(response));
            }
            return(response.ContractGetRecordsResponse.Records.Select(record =>
            {
                var result = new TransactionRecord();
                Protobuf.FillRecordProperties(record, result);
                return result;
            }).ToArray());
        /// <summary>
        /// Retrieves the network address associated with the specified smart contract id.
        /// </summary>
        /// <param name="smartContractId">
        /// The smart contract ID to look up.
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// The network address associated with the smart contract ID.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        public async Task <Address> GetAddressFromSmartContractId(string smartContractId, Action <IContext>?configure = null)
        {
            smartContractId = RequireInputParameter.SmartContractId(smartContractId);
            var context         = CreateChildContext(configure);
            var gateway         = RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var transfers       = Transactions.CreateCryptoTransferList((payer, -context.FeeLimit), (gateway, context.FeeLimit));
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateCryptoTransferTransactionBody(context, transfers, transactionId, "Get Contract By Solidity ID");
            var query           = new Query
            {
                GetBySolidityID = new GetBySolidityIDQuery
                {
                    Header     = Transactions.SignQueryHeader(transactionBody, payer),
                    SolidityID = smartContractId
                }
            };
            var response = await Transactions.ExecuteRequestWithRetryAsync(context, query, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, getResponseCode(response));
            var data = response.GetBySolidityID;

            if (data.ContractID != null)
            {
                return(Protobuf.FromContractID(data.ContractID));
            }
            if (data.AccountID != null)
            {
                return(Protobuf.FromAccountID(data.AccountID));
            }
            if (data.FileID != null)
            {
                return(Protobuf.FromFileID(data.FileID));
            }
            throw new TransactionException($"Address from Smart Contract ID {smartContractId} was not found.", Protobuf.FromTransactionId(transactionId), ResponseCode.Unknown);
Example #8
0
        /// <summary>
        /// Retrieves the account records associated with an account that are presently
        /// held within the network.
        /// </summary>
        /// <param name="address">
        /// The Hedera Network Address to retrieve associated records.
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// A detailed description of the account.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        public async Task <TransactionRecord[]> GetAccountRecordsAsync(Address address, Action <IContext>?configure = null)
        {
            address = RequireInputParameter.Address(address);
            var context         = CreateChildContext(configure);
            var gateway         = RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var transfers       = Transactions.CreateCryptoTransferList((payer, -context.FeeLimit), (gateway, context.FeeLimit));
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateCryptoTransferTransactionBody(context, transfers, transactionId, "Get Account Records");
            var query           = new Query
            {
                CryptoGetAccountRecords = new CryptoGetAccountRecordsQuery
                {
                    Header    = Transactions.SignQueryHeader(transactionBody, payer),
                    AccountID = Protobuf.ToAccountID(address)
                }
            };
            var response = await Transactions.ExecuteRequestWithRetryAsync(context, query, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, getResponseCode(response));
            return(response.CryptoGetAccountRecords.Records.Select(record =>
            {
                var result = new TransactionRecord();
                Protobuf.FillRecordProperties(record, result);
                return result;
            }).ToArray());
Example #9
0
        /// <summary>
        /// Deletes a contract instance from the network returning the remaining
        /// crypto balance to the specified address.  Must be signed
        /// by the admin key.
        /// </summary>
        /// <param name="contractToDelete">
        /// The Contract instance that will be deleted.
        /// </param>
        /// <param name="transferToAddress">
        /// The address that will receive any remaining balance from the deleted Contract.
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// A transaction receipt indicating a successful operation.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        /// <exception cref="ConsensusException">If the network was unable to come to consensus before the duration of the transaction expired.</exception>
        /// <exception cref="TransactionException">If the network rejected the create request as invalid or had missing data.</exception>
        public async Task <TransactionReceipt> DeleteContractAsync(Address contractToDelete, Address transferToAddress, Action <IContext>?configure = null)
        {
            contractToDelete  = RequireInputParameter.ContractToDelete(contractToDelete);
            transferToAddress = RequireInputParameter.TransferToAddress(transferToAddress);
            var context = CreateChildContext(configure);

            RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateEmptyTransactionBody(context, transactionId, "Delete Contract");

            transactionBody.ContractDeleteInstance = new ContractDeleteTransactionBody
            {
                ContractID        = Protobuf.ToContractID(contractToDelete),
                TransferAccountID = Protobuf.ToAccountID(transferToAddress)
            };
            var request  = Transactions.SignTransaction(transactionBody, payer);
            var precheck = await Transactions.ExecuteRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck.NodeTransactionPrecheckCode);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to delete contract, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TransactionReceipt();

            Protobuf.FillReceiptProperties(transactionId, receipt, result);
            return(result);
Example #10
0
        /// <summary>
        /// Calls a smart contract function locally on the gateway node.
        /// </summary>
        /// <remarks>
        /// This is performed locally on the gateway node. It cannot change the state of the contract instance
        /// (and so, cannot spend anything from the instance's cryptocurrency account). It will not have a
        /// consensus timestamp nor a record or a receipt. The response will contain the output returned
        /// by the function call.
        /// </remarks>
        /// <param name="queryParameters">
        /// The parameters identifying the contract and function method to call.
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// The results from the local contract query call.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        public async Task <ContractCallResult> QueryContractAsync(QueryContractParams queryParameters, Action <IContext>?configure = null)
        {
            queryParameters         = RequireInputParameter.QueryParameters(queryParameters);
            await using var context = CreateChildContext(configure);
            var query = new Query
            {
                ContractCallLocal = new ContractCallLocalQuery
                {
                    Header             = Transactions.CreateAskCostHeader(),
                    ContractID         = Protobuf.ToContractID(queryParameters.Contract),
                    Gas                = queryParameters.Gas,
                    FunctionParameters = Abi.EncodeFunctionWithArguments(queryParameters.FunctionName, queryParameters.FunctionArgs).ToByteString(),
                    MaxResultSize      = queryParameters.MaxAllowedReturnSize
                }
            };
            var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

            long cost = (long)response.ContractCallLocal.Header.Cost;

            if (cost > 0)
            {
                var transactionId = Transactions.GetOrCreateTransactionID(context);
                query.ContractCallLocal.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost + queryParameters.ReturnValueCharge, transactionId);

                response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

                ValidateResult.ResponseHeader(transactionId, getResponseHeader(response));
            }
            return(Protobuf.FromContractCallResult(response.ContractCallLocal.FunctionResult));
Example #11
0
        /// <summary>
        /// Internal implementation of the update Contract functionality.
        /// </summary>
        private async Task <TResult> UpdateContractImplementationAsync <TResult>(UpdateContractParams updateParameters, Action <IContext>?configure) where TResult : new()
        {
            updateParameters        = RequireInputParameter.UpdateParameters(updateParameters);
            await using var context = CreateChildContext(configure);
            RequireInContext.Gateway(context);
            var payer              = RequireInContext.Payer(context);
            var signatory          = Transactions.GatherSignatories(context, updateParameters.Signatory);
            var updateContractBody = new ContractUpdateTransactionBody
            {
                ContractID = Protobuf.ToContractID(updateParameters.Contract)
            };

            if (updateParameters.Expiration.HasValue)
            {
                updateContractBody.ExpirationTime = Protobuf.ToTimestamp(updateParameters.Expiration.Value);
            }
            if (!(updateParameters.Administrator is null))
            {
                updateContractBody.AdminKey = Protobuf.ToPublicKey(updateParameters.Administrator);
            }
            if (updateParameters.RenewPeriod.HasValue)
            {
                updateContractBody.AutoRenewPeriod = Protobuf.ToDuration(updateParameters.RenewPeriod.Value);
            }
            if (!(updateParameters.File is null))
            {
                updateContractBody.FileID = Protobuf.ToFileId(updateParameters.File);
            }
            if (!string.IsNullOrWhiteSpace(updateParameters.Memo))
            {
                updateContractBody.Memo = updateParameters.Memo;
            }
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateTransactionBody(context, transactionId);

            transactionBody.ContractUpdateInstance = updateContractBody;
            var request = await Transactions.SignTransactionAsync(transactionBody, signatory);

            var precheck = await Transactions.ExecuteSignedRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to update Contract, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is TransactionRecord arec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, arec);
            }
            else if (result is TransactionReceipt arcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, arcpt);
            }
            return(result);
Example #12
0
        public void ExposesValidStartNano()
        {
            var transactionId = Generator.TransactionID();
            var txId          = Protobuf.FromTransactionId(transactionId);

            Assert.Equal(txId.ValidStartNanos, transactionId.TransactionValidStart.Nanos);
        }
Example #13
0
        /// <summary>
        /// Retrieves the account records associated with an account that are presently
        /// held within the network because they exceeded the receive or send threshold
        /// values for autogeneration of records.
        /// </summary>
        /// <param name="address">
        /// The Hedera Network Address to retrieve associated records.
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// A detailed description of the account.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        public async Task <TransactionRecord[]> GetAccountRecordsAsync(Address address, Action <IContext>?configure = null)
        {
            address = RequireInputParameter.Address(address);
            await using var context = CreateChildContext(configure);
            var query = new Query
            {
                CryptoGetAccountRecords = new CryptoGetAccountRecordsQuery
                {
                    Header    = Transactions.CreateAskCostHeader(),
                    AccountID = Protobuf.ToAccountID(address)
                }
            };
            var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

            long cost = (long)response.CryptoGetAccountRecords.Header.Cost;

            if (cost > 0)
            {
                var transactionId = Transactions.GetOrCreateTransactionID(context);
                query.CryptoGetAccountRecords.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost, transactionId);

                response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

                var precheckCode = getResponseHeader(response)?.NodeTransactionPrecheckCode ?? ResponseCodeEnum.Unknown;
                if (precheckCode != ResponseCodeEnum.Ok)
                {
                    throw new TransactionException("Unable to retrieve transaction records.", Protobuf.FromTransactionId(transactionId), (ResponseCode)precheckCode);
                }
            }
            return(response.CryptoGetAccountRecords.Records.Select(record =>
            {
                var result = new TransactionRecord();
                Protobuf.FillRecordProperties(record, result);
                return result;
            }).ToArray());
Example #14
0
        /// <summary>
        /// Internal implementation for Multi Account Transfer Crypto.
        /// Returns either a receipt or record or throws an exception.
        /// </summary>
        private async Task <TResult> TransferImplementationAsync <TResult>(Dictionary <Account, long> sendAccounts, Dictionary <Address, long> receiveAddresses, Action <IContext>?configure) where TResult : new()
        {
            var transferList = RequireInputParameter.MultiTransfers(sendAccounts, receiveAddresses);
            var context      = CreateChildContext(configure);

            RequireInContext.Gateway(context);
            var payers          = sendAccounts.Keys.ToArray <ISigner>().Append(RequireInContext.Payer(context)).ToArray();
            var transfers       = Transactions.CreateCryptoTransferList(transferList);
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateCryptoTransferTransactionBody(context, transfers, transactionId, "Transfer Crypto");
            var request         = Transactions.SignTransaction(transactionBody, payers);
            var precheck        = await Transactions.ExecuteRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck.NodeTransactionPrecheckCode);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to execute crypto transfer, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is TransferRecord rec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, rec);
                rec.Transfers = Protobuf.FromTransferList(record.TransferList);
            }
            else if (result is TransactionReceipt rcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, rcpt);
            }
            return(result);
Example #15
0
        public void ExposesAccountValue()
        {
            var transactionId = Generator.TransactionID();
            var txId          = Protobuf.FromTransactionId(transactionId);

            Assert.Equal(txId.Address, Protobuf.FromAccountID(transactionId.AccountID));
        }
Example #16
0
        /// <summary>
        /// Retrieves detailed information regarding a Topic Instance.
        /// </summary>
        /// <param name="topic">
        /// The Hedera Network Address of the Topic instance to retrieve.
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// A detailed description of the contract instance.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        public async Task <TopicInfo> GetTopicInfoAsync(Address topic, Action <IContext>?configure = null)
        {
            topic = RequireInputParameter.Topic(topic);
            await using var context = CreateChildContext(configure);
            var query = new Query
            {
                ConsensusGetTopicInfo = new ConsensusGetTopicInfoQuery
                {
                    Header  = Transactions.CreateAskCostHeader(),
                    TopicID = Protobuf.ToTopicID(topic)
                }
            };
            var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

            long cost = (long)response.ConsensusGetTopicInfo.Header.Cost;

            if (cost > 0)
            {
                var transactionId = Transactions.GetOrCreateTransactionID(context);
                query.ConsensusGetTopicInfo.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost, transactionId);

                response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

                ValidateResult.ResponseHeader(transactionId, getResponseHeader(response));
            }
            return(Protobuf.FromTopicInfo(response.ConsensusGetTopicInfo.TopicInfo));
Example #17
0
 public byte[] Encode(Protobuf protobuf)
 {
     try
     {
         MemoryStream serializeStream = new MemoryStream();
         ProtoSerializer.Serialize(protobuf.ProtoID, serializeStream, protobuf.Proto);
         // 整个数据流结构为
         // 4个字节的包长度(不算在长度中), 2个字节的包长度, 2个字节的请求类型
         MemoryStream sendStream = new MemoryStream();
         sendStream.SetLength(serializeStream.Length + 8);
         sendStream.Position = 0;
         sendStream.Write(BitConverter.GetBytes((int)serializeStream.Length + 4), 0, 4);
         sendStream.Write(BitConverter.GetBytes((short)serializeStream.Length + 4), 0, 2);
         sendStream.Write(BitConverter.GetBytes((short)protobuf.ProtoID), 0, 2);
         sendStream.Write(serializeStream.GetBuffer(), 0, (int)serializeStream.Length);
         var res = new byte[sendStream.Length];
         Array.Copy(sendStream.GetBuffer(), res, sendStream.Length);
         return(res);
     }
     catch (Exception ex)
     {
         UnityEngine.Debug.LogError("Encode protobuf fail!");
         UnityEngine.Debug.LogException(ex);
     }
     return(null);
 }
Example #18
0
        /// <summary>
        /// Retrieves the balance in tinybars from the network for a given contract.
        /// </summary>
        /// <param name="contract">
        /// The hedera network contract address to retrieve the balance of.
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// The balance of the associated contract.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        public async Task <ulong> GetContractBalanceAsync(Address contract, Action <IContext>?configure = null)
        {
            contract = RequireInputParameter.Contract(contract);
            await using var context = CreateChildContext(configure);
            var query = new Query
            {
                CryptogetAccountBalance = new CryptoGetAccountBalanceQuery
                {
                    Header     = Transactions.CreateAskCostHeader(),
                    ContractID = Protobuf.ToContractID(contract)
                }
            };
            var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

            var cost = (long)response.CryptogetAccountBalance.Header.Cost;

            if (cost > 0)
            {
                var transactionId = Transactions.GetOrCreateTransactionID(context);
                query.CryptogetAccountBalance.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost, transactionId);

                response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

                ValidateResult.ResponseHeader(transactionId, getResponseHeader(response));
            }
            return(response.CryptogetAccountBalance.Balance);
Example #19
0
        /// <summary>
        /// Retrieves the current USD to hBar exchange rate information from the
        /// network.
        /// </summary>
        /// <remarks>
        /// NOTE: this method incours a charge to retrieve the file from the network.
        /// </remarks>
        /// <param name="client">Client Object</param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// An Exchange Rates object providing the current and next
        /// exchange rates.
        /// </returns>
        public static async Task <ExchangeRates> GetExchangeRatesAsync(this Client client, Action <IContext>?configure = null)
        {
            var file = await client.GetFileContentAsync(EXCHANGE_RATE_FILE_ADDRESS, configure);

            var set = Proto.ExchangeRateSet.Parser.ParseFrom(file.ToArray());

            return(new ExchangeRates(Protobuf.FromExchangeRate(set.CurrentRate), Protobuf.FromExchangeRate(set.NextRate)));
        }
Example #20
0
        /// <summary>
        /// Retrieves the metrics for calculating fees from the network.
        /// network.
        /// </summary>
        /// <remarks>
        /// NOTE: this method incours a charge to retrieve the file from the network.
        /// </remarks>
        /// <param name="client">Client Object</param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// The structure representing the metrics of the Network's Fee Schedule.
        /// </returns>
        public static async Task <FeeSchedules> GetFeeScheduleAsync(this Client client, Action <IContext>?configure = null)
        {
            var file = await client.GetFileContentAsync(FEE_SCHEDULE_FILE_ADDRESS, configure);

            var set = Proto.CurrentAndNextFeeSchedule.Parser.ParseFrom(file.ToArray());

            return(new FeeSchedules(Protobuf.FromFeeSchedule(set.CurrentFeeSchedule), Protobuf.FromFeeSchedule(set.NextFeeSchedule)));
        }
Example #21
0
        /// <summary>
        /// Retrieves the current USD to hBar exchange rate information from the
        /// network.
        /// </summary>
        /// <remarks>
        /// NOTE: this method incours a charge to retrieve the file from the network.
        /// </remarks>
        /// <param name="client">Client Object</param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// An Exchange Rates object providing the current and next
        /// exchange rates.
        /// </returns>
        public static async Task <NodeInfo[]> GetAddressBookAsync(this Client client, Action <IContext>?configure = null)
        {
            var file = await client.GetFileContentAsync(ADDRESS_BOOK_FILE_ADDRESS, configure);

            var book = Proto.NodeAddressBook.Parser.ParseFrom(file.ToArray());

            return(Protobuf.FromNodeAddressBook(book));
        }
Example #22
0
        /// <summary>
        /// Creates a new Transaction Id
        /// </summary>
        /// <param name="configure"></param>
        /// <returns></returns>
        public TxId CreateNewTxId(Action <IContext>?configure = null)
        {
            var context = CreateChildContext(configure);
            var result  = Protobuf.FromTransactionId(Transactions.GetOrCreateTransactionID(context));

            _ = context.DisposeAsync();
            return(result);
        }
Example #23
0
        public void DisimilarTxIdesAreNotConsideredEqual()
        {
            var txId1 = Protobuf.FromTransactionId(Generator.TransactionID());
            var txId2 = Protobuf.FromTransactionId(Generator.TransactionID());

            Assert.NotEqual(txId1, txId2);
            Assert.False(txId1 == txId2);
            Assert.True(txId1 != txId2);
            Assert.NotEqual(txId1.GetHashCode(), txId2.GetHashCode());
        }
Example #24
0
        private static void Main(string[] args)
        {
            //Tests.RunTest(1);
            //Tests.RunTest(2);
            //Tests.RunTest(3);

            //Capability.Run();

            Protobuf.Run();
        }
Example #25
0
        public void EquivalentTxIdAreConsideredEqual()
        {
            var transactionId = Generator.TransactionID();
            var txId1         = Protobuf.FromTransactionId(transactionId);
            var txId2         = Protobuf.FromTransactionId(transactionId);

            Assert.Equal(txId1, txId2);
            Assert.True(txId1 == txId2);
            Assert.False(txId1 != txId2);
            Assert.Equal(txId1.GetHashCode(), txId2.GetHashCode());
        }
Example #26
0
        private object ConvertObject(PropertyInfo property, ITable element)
        {
            var value = property.GetValue(element);

            if (value == null)
            {
                return(null);
            }
            if (property.PropertyType == typeof(DateTime))
            {
                value = ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss");
            }
            else if (property.PropertyType == typeof(Boolean))
            {
                value = Convert.ToByte(value);
            }
            else if (property.CustomAttributes.Count() > 0 && property.GetCustomAttribute <ProtoSerializeAttribute>() != null)
            {
                byte[] content = Protobuf.Serialize(value);
                return(content);
            }
            else if (CustomSerializationMethods.ContainsKey(property.PropertyType))
            {
                value = CustomSerializationMethods[property.PropertyType].Invoke(null, new object[] { value });
            }
            else if (value is Enum)
            {
                value = value.ToString();
            }
            else if (property.PropertyType.IsGenericType)
            {
                List <object> results = new List <object>();

                Type genericType = property.PropertyType.GetGenericTypeDefinition();

                if (genericType == typeof(List <>))
                {
                    var values = (IList)value;

                    foreach (var v in values)
                    {
                        results.Add(v);
                    }
                    return(string.Join(",", results));
                }
                else
                {
                    throw new Exception("Unhandled generic type " + property.PropertyType.Name);
                }
            }

            return(value);
        }
Example #27
0
        /// <summary>
        /// Retrieves the transaction record for a given transaction ID.
        /// </summary>
        /// <param name="transaction">
        /// Transaction identifier of the record
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// A transaction record with the specified id, or an exception if not found.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        /// <exception cref="TransactionException">If the network rejected the create request as invalid or had missing data.</exception>
        public async Task <TransactionRecord> GetTransactionRecordAsync(TxId transaction, Action <IContext>?configure = null)
        {
            transaction = RequireInputParameter.Transaction(transaction);
            var context       = CreateChildContext(configure);
            var transactionId = Protobuf.ToTransactionID(transaction);
            var record        = await GetTransactionRecordAsync(context, transactionId);

            var result = new TransactionRecord();

            Protobuf.FillRecordProperties(record, result);
            return(result);
        }
        private void OnApplicationQuit()
        {
            GpuManager.Shutdown();

            if (_isGlogInitialized)
            {
                Glog.Shutdown();
            }

            Protobuf.ResetLogHandler();
            Logger.SetLogger(null);
        }
Example #29
0
        public void ShouldAddAllTypesToRuntimeTypeModel_WhenCreated()
        {
            var typeMembers = RuntimeTypeModel.Default.GetTypes().Cast <MetaType>()
                              .Select(x => x.Type);

            Assert.DoesNotContain(typeof(AMessage), typeMembers);
            Assert.DoesNotContain(typeof(BMessage), typeMembers);

            _           = new Protobuf(new[] { typeof(AMessage), typeof(BMessage) });
            typeMembers = RuntimeTypeModel.Default.GetTypes().Cast <MetaType>()
                          .Select(x => x.Type);

            Assert.Contains(typeof(AMessage), typeMembers);
            Assert.Contains(typeof(BMessage), typeMembers);
        }
Example #30
0
        /// <summary>
        /// Internal helper method implementing the file update service.
        /// </summary>
        public async Task <TResult> UpdateFileImplementationAsync <TResult>(UpdateFileParams updateParameters, Action <IContext>?configure) where TResult : new()
        {
            updateParameters        = RequireInputParameter.UpdateParameters(updateParameters);
            await using var context = CreateChildContext(configure);
            RequireInContext.Gateway(context);
            var payer          = RequireInContext.Payer(context);
            var signatory      = Transactions.GatherSignatories(context, updateParameters.Signatory);
            var updateFileBody = new FileUpdateTransactionBody
            {
                FileID = Protobuf.ToFileId(updateParameters.File)
            };

            if (!(updateParameters.Endorsements is null))
            {
                updateFileBody.Keys = Protobuf.ToPublicKeyList(updateParameters.Endorsements);
            }
            if (updateParameters.Contents.HasValue)
            {
                updateFileBody.Contents = ByteString.CopyFrom(updateParameters.Contents.Value.ToArray());
            }
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateTransactionBody(context, transactionId);

            transactionBody.FileUpdate = updateFileBody;
            var request = await Transactions.SignTransactionAsync(transactionBody, signatory);

            var precheck = await Transactions.ExecuteSignedRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to update file, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is TransactionRecord rec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, rec);
            }
            else if (result is TransactionReceipt rcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, rcpt);
            }
            return(result);
Example #31
0
        /// <summary>
        /// Sends request to Clusterpoint Server. Returned CPS_Response should be casted to command-specific response class.
        /// </summary>
        /// <param name="request">Request to send.</param>
        /// <returns>Command-specific CPS_Response object instance.</returns>
        public CPS_Response sendRequest(CPS_Request request)
        {
            bool firstSend = true;
            string previousRenderedStorage = "";
            string requestXml = "";
            byte[] requestBytes = null;
            string rawResponse = "";
            bool quit = true;

            if (this.p_connectionSwitcher != null)
                this.p_connectionSwitcher.newRequest(request);

            do
            {
                CPS_Exception e = null;

                if (this.p_connectionSwitcher != null)
                    this.p_connectionString = this.parseConnectionString(this.p_connectionSwitcher.getConnectionString(ref this.p_storageName));

                try
                {
                    if (this.p_transactionId != null)
                        request.setParam("transaction_id", this.p_transactionId);
                    if (firstSend || previousRenderedStorage != this.p_storageName)
                    {
                        requestXml = this.renderRequest(request);
                        requestBytes = Encoding.UTF8.GetBytes(requestXml);
                        previousRenderedStorage = this.p_storageName;

                        if (this.p_debug)
                        {
                            FileStream fs = new FileStream("request.xml", FileMode.Create);
                            fs.Write(requestBytes, 0, requestBytes.Length);
                            fs.Close();
                        }
                    }
                    firstSend = false;

                    this.p_lastRequestSize = requestXml.Length;
                    this.p_lastNetworkDuration = 0;

                    Stopwatch totTimer = new Stopwatch();
                    Stopwatch netTimer = new Stopwatch();

                    totTimer.Start();
                    if (this.p_connectionString.Scheme.ToLower() == "http")
                    {
                        // TODO: implement HMAC support when server side supports it
                        HttpWebRequest webreq = (HttpWebRequest)HttpWebRequest.Create(this.p_connectionString.OriginalString);
                        webreq.UserAgent = this.p_applicationId;
                        webreq.Method = "POST";
                        webreq.ContentType = "application/x-www-form-urlencoded";
                        webreq.ContentLength = requestBytes.Length;
                        webreq.Headers["Recipient"] = this.p_storageName;
                        webreq.Proxy = null;

                        Stream webreq_data;
                        try
                        {
                            webreq_data = webreq.GetRequestStream();
                        }
                        catch (Exception)
                        {
                            throw new CPS_Exception("Invalid connection string").SetCode(CPS_Exception.ERROR_CODE.INVALID_CONNECTION_STRING);
                        }

                        netTimer.Start();
                        webreq_data.Write(requestBytes, 0, requestBytes.Length);
                        webreq_data.Close();
                        netTimer.Stop();

                        HttpWebResponse webrsp;
                        try
                        {
                            webrsp = (HttpWebResponse)webreq.GetResponse();
                        }
                        catch (Exception)
                        {
                            throw new CPS_Exception("Invalid connection string").SetCode(CPS_Exception.ERROR_CODE.INVALID_CONNECTION_STRING);
                        }
                        Stream webrsp_data = webrsp.GetResponseStream();
                        StreamReader webrsp_reader = new StreamReader(webrsp_data);

                        netTimer.Start();
                        rawResponse = webrsp_reader.ReadToEnd();
                        webrsp_reader.Close();
                        netTimer.Stop();
                    }

                    if (this.p_connectionString.Scheme.ToLower() == "tcp" || this.p_connectionString.Scheme.ToLower() == "tcps")
                    {
                        int port = this.p_connectionString.Port;
                        if (port <= 0)
                            port = 5550;
                        TcpClient tcp;
                        try
                        {
                            netTimer.Start();
                            tcp = new TcpClient(this.p_connectionString.Host, port);
                            netTimer.Stop();
                        }
                        catch (SocketException)
                        {
                            netTimer.Stop();
                            throw new CPS_Exception("Cannot connect to specified server").SetCode(CPS_Exception.ERROR_CODE.SOCKET_ERROR);
                        }
                        catch (Exception) // all other cases
                        {
                            netTimer.Stop();
                            throw new CPS_Exception("Invalid connection string").SetCode(CPS_Exception.ERROR_CODE.INVALID_CONNECTION_STRING);
                        }

                        NetworkStream net = tcp.GetStream();
                        System.IO.Stream strm = net;

                        if (this.p_connectionString.Scheme.ToLower() == "tcps")
                        {
                            System.Net.Security.SslStream ssl = new System.Net.Security.SslStream(strm, false, new System.Net.Security.RemoteCertificateValidationCallback(ConnectionServerValidationCallback), null);

                            try
                            {
                                ssl.AuthenticateAsClient(this.p_connectionString.Host);
                            }
                            catch (Exception)
                            {
                                throw new CPS_Exception("Error establishing SSL connection").SetCode(CPS_Exception.ERROR_CODE.SSL_HANDSHAKE);
                            }

                            strm = ssl;
                        }

                        Protobuf pb = new Protobuf();
                        pb.CreateField(1, Protobuf.WireType.LengthDelimited, requestBytes);
                        pb.CreateStringField(2, this.p_storageName);

                        if (this.p_hmacUserKey != null && this.p_hmacSignKey != null)
                        {
                            string characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                            char[] tokenchars = new char[16];
                            for (int i = 0; i < 16; i++)
                                tokenchars[i] = characters[this.p_random.Next(characters.Length)];
                            string token = new string(tokenchars);

                            System.DateTime epoch = new System.DateTime(1970, 1, 1, 0, 0, 0, 0).ToUniversalTime();
                            System.DateTime validity = System.DateTime.Now.ToUniversalTime();
                            validity.AddSeconds(10);
                            System.TimeSpan dtdiff = validity - epoch;
                            UInt64 unixvalidity = (UInt64)dtdiff.TotalSeconds;

                            pb.CreateStringField(14, token);
                            pb.CreateFixed64Field(15, unixvalidity);
                            pb.CreateStringField(16, this.p_hmacUserKey);
                            pb.CreateStringField(17, CPS_Hasher.SHA1(CPS_Hasher.SHA1(requestXml) + token + unixvalidity + this.p_hmacSignKey));
                            pb.CreateStringField(18, CPS_Hasher.SHA1_HMAC(this.p_hmacSignKey, requestXml + token + unixvalidity));
                        }

                        MemoryStream ms = new MemoryStream();
                        Protobuf_Streamer pbs = new Protobuf_Streamer(ms);
                        pb.WriteToStream(pbs);

                        byte[] header = CPS_Length2Header((int)(ms.Length));

                        netTimer.Start();
                        try
                        {
                            strm.Write(header, 0, 8);
                            strm.Write(ms.GetBuffer(), 0, (int)(ms.Length));
                        }
                        catch (Exception)
                        {
                            netTimer.Stop();
                            throw new CPS_Exception("Error sending request").SetCode(CPS_Exception.ERROR_CODE.SOCKET_ERROR);
                        }
                        strm.Flush();

                        try
                        {
                            strm.Read(header, 0, 8);
                            netTimer.Stop();
                        }
                        catch (Exception)
                        {
                            netTimer.Stop();
                            throw new CPS_Exception("Error receiving response").SetCode(CPS_Exception.ERROR_CODE.SOCKET_ERROR);
                        }

                        int len = CPS_Header2Length(header);
                        if (len <= 0)
                            throw new CPS_Exception("Invalid response from server").SetCode(CPS_Exception.ERROR_CODE.INVALID_RESPONSE);

                        byte[] recv = new byte[len];
                        int got = 0;
                        netTimer.Start();
                        while (got < len)
                        {
                            int br = 0;
                            try
                            {
                                br = strm.Read(recv, got, len - got);
                                if (br == 0)
                                {
                                    netTimer.Stop();
                                    throw new CPS_Exception("Server unexpectedly closed connection").SetCode(CPS_Exception.ERROR_CODE.SOCKET_ERROR);
                                }
                            }
                            catch (Exception)
                            {
                                netTimer.Stop();
                                throw new CPS_Exception("Error receiving response").SetCode(CPS_Exception.ERROR_CODE.SOCKET_ERROR);
                            }
                            got += br;
                        }
                        strm.Close();
                        netTimer.Stop();

                        ms = new MemoryStream(recv);
                        pbs = new Protobuf_Streamer(ms);
                        pb = new Protobuf(pbs);

                        rawResponse = pb.GetStringField(1);
                    }
                    totTimer.Stop();

                    this.p_lastRequestDuration = totTimer.ElapsedMilliseconds;
                    this.p_lastRequestDuration = this.p_lastRequestDuration / 1000.0;

                    this.p_lastNetworkDuration = netTimer.ElapsedMilliseconds;
                    this.p_lastNetworkDuration = this.p_lastNetworkDuration / 1000.0;

                    this.p_lastResponseSize = rawResponse.Length;

                    if (this.p_debug)
                    {
                        FileStream fs = new FileStream("response.xml", FileMode.Create);
                        byte[] responseBytes = Encoding.UTF8.GetBytes(rawResponse);
                        fs.Write(responseBytes, 0, responseBytes.Length);
                        fs.Close();
                    }
                }
                catch (CPS_Exception e_)
                {
                    e = e_;
                }

                if (this.p_connectionSwitcher != null)
                    quit = !this.p_connectionSwitcher.shouldRetry(rawResponse, e);
                else
                    quit = true;

                if (quit && e != null)
                    throw e;
            }
            while (!quit);

            switch (request.getCommand())
            {
                case "search":
                case "similar":
                    return new CPS_SearchResponse(this, request, rawResponse);
                case "update":
                case "delete":
                case "replace":
                case "partial-replace":
                case "partial-xreplace":
                case "insert":
                case "create-alert":
                case "update-alerts":
                case "delete-alerts":
                    return new CPS_ModifyResponse(this, request, rawResponse);
                case "alternatives":
                    return new CPS_AlternativesResponse(this, request, rawResponse);
                case "list-words":
                    return new CPS_ListWordsResponse(this, request, rawResponse);
                case "status":
                    return new CPS_StatusResponse(this, request, rawResponse);
                case "retrieve":
                case "list-last":
                case "list-first":
                case "retrieve-last":
                case "retrive-first":
                case "lookup":
                case "show-history":
                    return new CPS_LookupResponse(this, request, rawResponse);
                case "search-delete":
                    return new CPS_SearchDeleteResponse(this, request, rawResponse);
                case "list-paths":
                    return new CPS_ListPathsResponse(this, request, rawResponse);
                case "list-facets":
                    return new CPS_ListFacetsResponse(this, request, rawResponse);
                case "list-alerts":
                    return new CPS_Response(this, request, rawResponse);
                    // TODO: change this !!!
                default:
                    CPS_Response ret = new CPS_Response(this, request, rawResponse);
                    // This is explicitly processed here, because of .NET limitations. PHP API changes this directly from CPS_Response constructor.
                    if (request.getCommand() == "begin-transaction" || request.getCommand() == "commit-transaction" || request.getCommand() == "rollback-transaction")
                        this.p_transactionId = ret.getTransactionId();
                    return ret;
            }
        }
 public Task<string> TestPassClass(Protobuf.Interface.TestParam param)
 {
     var requestMessage = new RequestMessage {
         InvokePayload = new IPedantic_PayloadTable.TestPassClass_Invoke { param = param }
     };
     return SendRequestAndReceive<string>(requestMessage);
 }
 void IPedantic_NoReply.TestPassClass(Protobuf.Interface.TestParam param)
 {
     var requestMessage = new RequestMessage {
         InvokePayload = new IPedantic_PayloadTable.TestPassClass_Invoke { param = param }
     };
     SendRequest(requestMessage);
 }