Example #1
0
        //send signed tx hex to bitcoin network
        public static string BroadcastTx(string SignedTxHex, bool testnet)
        {
            CheckNullOrEmpty(new object[] { SignedTxHex, BTChost }, new string[] { "SignedTxHex", "BTChost" });

            NBitcoin.Transaction tx = NBitcoin.Transaction.Parse(SignedTxHex);
            Network ntwk            = testnet ? Network.TestNet : Network.Main;
            string  err             = "";

            var nd = Node.Connect(ntwk, BTChost + ":" + BTCport);             //DACC bitcoin server (BitcoinD)

            //bitcoin node sends responses asynchronously
            nd.MessageReceived += (node, message) =>
            {
                NBitcoin.Protocol.IncomingMessage msgx = message;
                if (msgx.Message.Payload is RejectPayload)                         //error message
                {
                    RejectPayload py = (RejectPayload)msgx.Message.Payload;
                    Console.WriteLine("Rejected:" + py.Message + " " + py.Reason);
                    err += py.Message + " " + py.Reason;
                }
            };
            nd.VersionHandshake();             //must send node version first
            Thread.Sleep(1000);
            nd.SendMessage(new InvPayload(tx));
            Thread.Sleep(1000);
            nd.SendMessage(new TxPayload(tx));
            Thread.Sleep(5000);             //wait for any error msgs
            string msg = tx.GetHash().ToString();

            try
            {
                bool broadcasted = false;
                //search mempool for transaction
                foreach (var txid in nd.GetMempool())                 //throws error for some servers
                {
                    if (txid.Equals(tx.GetHash()))
                    {
                        broadcasted = true;
                    }
                }
                nd.Disconnect();
                //return json message
                if (!broadcasted)
                {
                    msg = "Broadcast Failed (tx hash = " + tx.GetHash().ToString() + ")" + err + "\"}";
                }
                else
                {
                    msg = tx.GetHash().ToString();
                }
            }
            catch (Exception ex) { msg = "Error (BroadcastTx): " + ex.Message; }
            return(msg);
        }
Example #2
0
        private void ProcessMessageCore(IncomingMessage message)
        {
            if (message.Message.Payload is VersionPayload)
            {
                var version         = message.AssertPayload <VersionPayload>();
                var connectedToSelf = version.Nonce == Nonce;
                if (message.Node != null && connectedToSelf)
                {
                    NodeServerTrace.ConnectionToSelfDetected();
                    message.Node.DisconnectAsync();
                    return;
                }

                if (message.Node == null)
                {
                    var remoteEndpoint = version.AddressFrom;
                    if (!remoteEndpoint.Address.IsRoutable(AllowLocalPeers))
                    {
                        //Send his own endpoint
                        remoteEndpoint = new IPEndPoint(((IPEndPoint)message.Socket.RemoteEndPoint).Address, Network.DefaultPort);
                    }

                    var peer = new NetworkAddress()
                    {
                        Endpoint = remoteEndpoint,
                        Time     = DateTimeOffset.UtcNow
                    };
                    var node = new Node(peer, Network, CreateNodeConnectionParameters(), message.Socket, version);

                    if (connectedToSelf)
                    {
                        node.SendMessage(CreateNodeConnectionParameters().CreateVersion(node.Peer.Endpoint, Network));
                        NodeServerTrace.ConnectionToSelfDetected();
                        node.Disconnect();
                        return;
                    }

                    CancellationTokenSource cancel = new CancellationTokenSource();
                    cancel.CancelAfter(TimeSpan.FromSeconds(10.0));
                    try
                    {
                        ConnectedNodes.Add(node);
                        node.StateChanged += node_StateChanged;
                        node.RespondToHandShake(cancel.Token);
                    }
                    catch (OperationCanceledException ex)
                    {
                        NodeServerTrace.Error("The remote node did not respond fast enough (10 seconds) to the handshake completion, dropping connection", ex);
                        node.DisconnectAsync();
                        throw;
                    }
                    catch (Exception)
                    {
                        node.DisconnectAsync();
                        throw;
                    }
                }
            }

            var messageReceived = MessageReceived;

            if (messageReceived != null)
            {
                messageReceived(this, message);
            }
        }