public virtual async Task StaticCall(Func <Task> taskProducer, CallDescription callDescription)
        {
            Task task = await ExecuteTaskWaitForOtherTasks(
                () => ExecuteTaskWithTimeout(taskProducer, this.configuration.StaticCallTimeout)
                );

            await task;
        }
        public virtual async Task <T> StaticCall <T>(Func <Task <T> > taskProducer, CallDescription callDescription)
        {
            Task <T> task = (Task <T>) await ExecuteTaskWaitForOtherTasks(
                () => ExecuteTaskWithTimeout(taskProducer, this.configuration.StaticCallTimeout)
                );

            return(await task);
        }
        public virtual async Task Call(Func <Task> taskProducer, CallDescription callDescription)
        {
            Task task = await ExecuteTaskWithRetryOnInvalidTxNonceException(
                () => ExecuteTaskWaitForOtherTasks(
                    () => ExecuteTaskWithTimeout(taskProducer, this.configuration.CallTimeout)
                    ));

            await task;
        }
        public virtual async Task <T> Call <T>(Func <Task <T> > taskProducer, CallDescription callDescription)
        {
            Task <T> task = (Task <T>) await ExecuteTaskWithRetryOnInvalidTxNonceException(
                () => ExecuteTaskWaitForOtherTasks(
                    () => ExecuteTaskWithTimeout(taskProducer, this.configuration.CallTimeout)
                    ));

            return(await task);
        }
        /// <summary>
        /// Commits a transaction to the DAppChain.
        /// </summary>
        /// <param name="tx">Transaction to commit.</param>
        /// <param name="callDescription">Call high-level description.</param>
        /// <returns>Commit metadata.</returns>
        /// <exception cref="InvalidTxNonceException">Thrown when transaction is rejected by the DAppChain due to a bad nonce.</exception>
        internal async Task <BroadcastTxResult> CommitTxAsync(IMessage tx, CallDescription callDescription)
        {
            if (this.writeClient == null)
            {
                throw new InvalidOperationException("Write client was not set");
            }

            return(await this.CallExecutor.Call(
                       async() =>
            {
                await EnsureConnected();

                byte[] txBytes = tx.ToByteArray();
                if (this.TxMiddleware != null)
                {
                    txBytes = await this.TxMiddleware.Handle(txBytes);
                }

                try
                {
                    string payload = CryptoBytes.ToBase64String(txBytes);
                    var result = await this.writeClient.SendAsync <BroadcastTxResult, string[]>("broadcast_tx_commit", new[] { payload });
                    if (result == null)
                    {
                        return null;
                    }

                    CheckForTxError(result.CheckTx);
                    CheckForTxError(result.DeliverTx);

                    if (this.TxMiddleware != null)
                    {
                        this.TxMiddleware.HandleTxResult(result);
                    }

                    return result;
                }
                catch (LoomException e)
                {
                    if (this.TxMiddleware != null)
                    {
                        this.TxMiddleware.HandleTxException(e);
                    }

                    throw;
                }
            },
                       callDescription
                       ));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Calls a smart contract method that mutates state.
        /// The call into the smart contract is accomplished by committing a transaction to the DAppChain.
        /// </summary>
        /// <typeparam name="T">Smart contract method return type.</typeparam>
        /// <param name="tx">Transaction message.</param>
        /// <param name="callDescription">Call high-level description.</param>
        /// <returns>The return value of the smart contract method.</returns>
        private async Task <T> CallAsync <T>(Transaction tx, CallDescription callDescription) where T : IMessage, new()
        {
            var result = await this.Client.CommitTxAsync(tx, callDescription);

            if (result != null && result.DeliverTx.Data != null)
            {
                var resp = new Response();
                resp.MergeFrom(result.DeliverTx.Data);
                if (resp.Body != null)
                {
                    T msg = new T();
                    msg.MergeFrom(resp.Body);
                    return(msg);
                }
            }
            return(default(T));
        }
        /// <summary>
        /// Queries the current state of a contract.
        /// </summary>
        /// <typeparam name="T">The expected response type, must be deserializable with Newtonsoft.Json.</typeparam>
        /// <param name="contract">Address of the contract to query.</param>
        /// <param name="query">Raw query parameters data.</param>
        /// <param name="caller">Optional caller address.</param>
        /// <param name="vmType">Virtual machine type.</param>
        /// <param name="callDescription">Call high-level description.</param>
        /// <returns>Deserialized response.</returns>
        internal async Task <T> QueryAsync <T>(Address contract, byte[] query, Address caller, VMType vmType, CallDescription callDescription)
        {
            if (this.readClient == null)
            {
                throw new InvalidOperationException("Read client is not set");
            }

            var queryParams = new QueryParams
            {
                ContractAddress = contract.LocalAddress,
                Params          = query,
                VmType          = vmType
            };

            if (caller.LocalAddress != null && caller.ChainId != null)
            {
                queryParams.CallerAddress = caller.QualifiedAddress;
            }

            return(await this.CallExecutor.StaticCall(
                       async() =>
            {
                await EnsureConnected();
                return await this.readClient.SendAsync <T, QueryParams>("query", queryParams);
            },
                       callDescription
                       ));
        }
 /// <summary>
 /// Queries the current state of a contract.
 /// </summary>
 /// <typeparam name="T">The expected response type, must be deserializable with Newtonsoft.Json.</typeparam>
 /// <param name="contract">Address of the contract to query.</param>
 /// <param name="query">Query parameters object.</param>
 /// <param name="caller">Optional caller address.</param>
 /// <param name="vmType">Virtual machine type.</param>
 /// <param name="callDescription">Call high-level description.</param>
 /// <returns>Deserialized response.</returns>
 internal async Task <T> QueryAsync <T>(Address contract, IMessage query, Address caller, VMType vmType, CallDescription callDescription)
 {
     return(await QueryAsync <T>(contract, query.ToByteArray(), caller, vmType, callDescription));
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Calls a smart contract method that mutates state.
 /// The call into the smart contract is accomplished by committing a transaction to the DAppChain.
 /// </summary>
 /// <param name="tx">Transaction message.</param>
 /// <param name="callDescription">Call high-level description.</param>
 /// <returns>Nothing.</returns>
 internal async Task <BroadcastTxResult> CallAsync(Transaction tx, CallDescription callDescription)
 {
     return(await this.Client.CommitTxAsync(tx, callDescription));
 }
        public virtual async Task NonBlockingStaticCall(Func <Task> taskProducer, CallDescription callDescription)
        {
            Task task = await ExecuteTaskWithTimeout(taskProducer, this.configuration.StaticCallTimeout);

            await task;
        }
        public virtual async Task <T> NonBlockingStaticCall <T>(Func <Task <T> > taskProducer, CallDescription callDescription)
        {
            Task <T> task = (Task <T>) await ExecuteTaskWithTimeout(taskProducer, this.configuration.StaticCallTimeout);

            return(await task);
        }