BitSharper.PeerGroup.BroadcastTransaction C# (CSharp) Méthode

BroadcastTransaction() public méthode

Broadcast a transaction to all connected peers.
public BroadcastTransaction ( Transaction tx ) : bool
tx Transaction
Résultat bool
        public bool BroadcastTransaction(Transaction tx)
        {
            var success = false;
            lock (_peers)
            {
                foreach (var peer in _peers)
                {
                    try
                    {
                        peer.BroadcastTransaction(tx);
                        success = true;
                    }
                    catch (IOException e)
                    {
                        _log.Error("failed to broadcast to " + peer, e);
                    }
                }
            }
            return success;
        }

Usage Example

        /// <summary>
        /// Sends coins to the given address, via the given <see cref="PeerGroup"/>.
        /// Change is returned to the first key in the wallet.
        /// </summary>
        /// <param name="peerGroup">The peer group to send via.</param>
        /// <param name="to">Which address to send coins to.</param>
        /// <param name="nanocoins">How many nanocoins to send. You can use Utils.toNanoCoins() to calculate this.</param>
        /// <returns>
        /// The <see cref="Transaction"/> that was created or null if there was insufficient balance to send the coins.
        /// </returns>
        /// <exception cref="IOException">If there was a problem broadcasting the transaction.</exception>
        public Transaction SendCoins(PeerGroup peerGroup, Address to, ulong nanocoins)
        {
            lock (this)
            {
                var tx = CreateSend(to, nanocoins);
                if (tx == null) // Not enough money! :-(
                    return null;
                if (!peerGroup.BroadcastTransaction(tx))
                {
                    throw new IOException("Failed to broadcast tx to all connected peers");
                }

                // TODO - retry logic
                ConfirmSend(tx);
                return tx;
            }
        }