Пример #1
0
        /// <summary>
        /// Broadcast a transaction on the hub.
        /// </summary>
        /// <param name="transaction">The transaction to broadcast.</param>
        /// <returns>The cause of the rejection or <c>null</c>.</returns>
        public Task <RejectPayload> BroadcastTransactionAsync(Transaction transaction)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException("transaction");
            }

            TaskCompletionSource <RejectPayload> completion = new TaskCompletionSource <RejectPayload>();
            uint256 hash = transaction.GetHash();

            if (this.BroadcastedTransaction.TryAdd(hash, transaction))
            {
                TransactionBroadcastedDelegate broadcasted = null;
                TransactionRejectedDelegate    rejected    = null;
                broadcasted = (t) =>
                {
                    if (t.GetHash() == hash)
                    {
                        completion.SetResult(null);
                        this.TransactionRejected    -= rejected;
                        this.TransactionBroadcasted -= broadcasted;
                    }
                };

                this.TransactionBroadcasted += broadcasted;

                rejected = (t, r) =>
                {
                    if (r.Hash == hash)
                    {
                        completion.SetResult(r);
                        this.TransactionRejected    -= rejected;
                        this.TransactionBroadcasted -= broadcasted;
                    }
                };

                this.TransactionRejected += rejected;
                this.OnBroadcastTransaction(transaction);
            }

            return(completion.Task);
        }
        /// <summary>
        /// Broadcast a transaction on the hub
        /// </summary>
        /// <param name="transaction">The transaction to broadcast</param>
        /// <returns>The cause of the rejection or null</returns>
        public Task <RejectPayload> BroadcastTransactionAsync(Transaction transaction)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException(nameof(transaction));
            }
#if NO_RCA
            TaskCompletionSource <RejectPayload> completion = new TaskCompletionSource <RejectPayload>();
#else
            TaskCompletionSource <RejectPayload> completion = new TaskCompletionSource <RejectPayload>(TaskCreationOptions.RunContinuationsAsynchronously);
#endif
            var hash = transaction.GetHash();
            if (BroadcastedTransaction.TryAdd(hash, transaction))
            {
                TransactionBroadcastedDelegate broadcasted = null;
                TransactionRejectedDelegate    rejected    = null;
                broadcasted = (t) =>
                {
                    if (t.GetHash() == hash)
                    {
                        completion.SetResult(null);
                        TransactionRejected    -= rejected;
                        TransactionBroadcasted -= broadcasted;
                    }
                };
                TransactionBroadcasted += broadcasted;
                rejected = (t, r) =>
                {
                    if (r.Hash == hash)
                    {
                        completion.SetResult(r);
                        TransactionRejected    -= rejected;
                        TransactionBroadcasted -= broadcasted;
                    }
                };
                TransactionRejected += rejected;
                OnBroadcastTransaction(transaction);
            }
            return(completion.Task);
        }
        /// <summary>
        /// Broadcast a transaction on the hub
        /// </summary>
        /// <param name="transaction">The transaction to broadcast</param>
        /// <returns>The cause of the rejection or null</returns>
        public Task <RejectPayload> BroadcastTransactionAsync(Types.Transaction transaction)        //todo: make private
        {
            if (transaction == null)
            {
                throw new ArgumentNullException("transaction");
            }

            TaskCompletionSource <RejectPayload> completion = new TaskCompletionSource <RejectPayload>();
            var hash = GetHash(transaction);

            if (BroadcastedTransaction.TryAdd(hash, transaction))
            {
                TransactionBroadcastedDelegate broadcasted = null;
                TransactionRejectedDelegate    rejected    = null;
                broadcasted = (t) =>
                {
                    if (GetHash(t) == hash)
                    {
                        completion.SetResult(null);
                        TransactionRejected    -= rejected;
                        TransactionBroadcasted -= broadcasted;
                    }
                };
                TransactionBroadcasted += broadcasted;
                rejected = (t, r) =>
                {
                    if (r.Hash == hash)
                    {
                        completion.SetResult(r);
                        TransactionRejected    -= rejected;
                        TransactionBroadcasted -= broadcasted;
                    }
                };
                TransactionRejected += rejected;
                OnBroadcastTransaction(transaction);
            }
            return(completion.Task);
        }