Пример #1
0
        async Task StartSynchronizer()
        {
            "Start network synchronization.".Log(LogFile);

            while (true)
            {
                await Task.Delay(3000).ConfigureAwait(false);

                if (!Blockchain.TryLock())
                {
                    continue;
                }

                if (!TryGetPeerNotSynchronized(
                        out Peer peer))
                {
                    Blockchain.ReleaseLock();
                    continue;
                }

                try
                {
                    string.Format(
                        "Synchronize with peer {0}",
                        peer.GetID())
                    .Log(LogFile);

                    await peer.Synchronize();

                    string.Format(
                        "Synchronization with peer {0} completed.",
                        peer.GetID())
                    .Log(LogFile);
                }
                catch (Exception ex)
                {
                    string.Format(
                        "Exception {0} when syncing with peer {1}: \n{2}",
                        ex.GetType(),
                        peer.GetID(),
                        ex.Message)
                    .Log(LogFile);

                    peer.FlagDispose = true;
                }

                ReleasePeer(peer);

                Blockchain.ReleaseLock();
            }
        }
Пример #2
0
            public async Task StartMessageListener()
            {
                try
                {
                    while (true)
                    {
                        await ReadMessage(Cancellation.Token);

                        string.Format(
                            "{0} received message {1}",
                            GetID(),
                            Command)
                        .Log(LogFile);

                        switch (Command)
                        {
                        case "ping":
                            await SendMessage(new PongMessage(
                                                  BitConverter.ToUInt64(Payload, 0)));

                            break;

                        case "addr":
                            AddressMessage addressMessage =
                                new AddressMessage(Payload);
                            break;

                        case "sendheaders":
                            await SendMessage(new SendHeadersMessage());

                            break;

                        case "feefilter":
                            FeeFilterMessage feeFilterMessage =
                                new FeeFilterMessage(Payload);
                            FeeFilterValue = feeFilterMessage.FeeFilterValue;
                            break;

                        case "block":

                            byte[] blockBytes = Payload
                                                .Take(PayloadLength)
                                                .ToArray();

                            Block block = ParserToken.ParseBlock(blockBytes);

                            if (IsStateIdle())
                            {
                                string.Format(
                                    "{0}: Receives unsolicited block {1}.",
                                    GetID(),
                                    block.Header.Hash.ToHexString())
                                .Log(LogFile);

                                Console.Beep();

                                if (!Blockchain.TryLock())
                                {
                                    break;
                                }

                                try
                                {
                                    ProcessHeaderUnsolicited(
                                        block.Header,
                                        out bool flagHeaderExtendsChain);

                                    if (flagHeaderExtendsChain)
                                    {
                                        if (!Blockchain.TryInsertBlock(
                                                block,
                                                flagValidateHeader: true))
                                        {
                                            // Blockchain insert sollte doch einfach Ex. schmeissen
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Blockchain.ReleaseLock();

                                    throw ex;
                                }

                                Blockchain.ReleaseLock();
                            }
                            else if (IsStateAwaitingBlockDownload())
                            {
                                BlockDownload.InsertBlock(block);

                                if (BlockDownload.IsDownloadCompleted)
                                {
                                    SignalProtocolTaskCompleted.Post(true);

                                    lock (LOCK_StateProtocol)
                                    {
                                        State = StateProtocol.IDLE;
                                    }

                                    break;
                                }

                                Cancellation.CancelAfter(
                                    TIMEOUT_RESPONSE_MILLISECONDS);
                            }

                            break;

                        case "headers":

                            Header header = null;
                            int    index  = 0;

                            int countHeaders = VarInt.GetInt32(
                                Payload,
                                ref index);

                            string.Format(
                                "{0}: Receiving {1} headers.",
                                GetID(),
                                countHeaders)
                            .Log(LogFile);

                            if (IsStateIdle())
                            {
                                header = Token.ParseHeader(
                                    Payload,
                                    ref index);

                                string.Format(
                                    "Received unsolicited header {0}",
                                    header.Hash.ToHexString())
                                .Log(LogFile);

                                index += 1;

                                if (!Blockchain.TryLock())
                                {
                                    break;
                                }

                                try
                                {
                                    ProcessHeaderUnsolicited(
                                        header,
                                        out bool flagHeaderExtendsChain);

                                    if (flagHeaderExtendsChain)
                                    {
                                        List <Inventory> inventories = new()
                                        {
                                            new Inventory(
                                                InventoryType.MSG_BLOCK,
                                                header.Hash)
                                        };

                                        SendMessage(new GetDataMessage(inventories));
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Blockchain.ReleaseLock();

                                    throw ex;
                                }

                                Blockchain.ReleaseLock();
                            }
                            else if (IsStateGetHeaders())
                            {
                                if (countHeaders > 0)
                                {
                                    while (index < PayloadLength)
                                    {
                                        header = Token.ParseHeader(
                                            Payload,
                                            ref index);

                                        index += 1;

                                        HeaderDownload.InsertHeader(header, Token);
                                    }
                                }

                                SignalProtocolTaskCompleted.Post(true);
                            }

                            break;

                        case "notfound":

                            Debug.WriteLine(
                                "Command notfound not implemented yet.");

                            break;

                        case "inv":

                            var invMessage = new InvMessage(Payload);

                            var getDataMessage = new GetDataMessage(
                                invMessage.Inventories);

                            break;

                        case "getdata":

                            getDataMessage = new GetDataMessage(Payload);

                            foreach (Inventory inventory in getDataMessage.Inventories)
                            {
                                if (inventory.Type == InventoryType.MSG_TX)
                                {
                                    if (Token.TryRequestTX(
                                            inventory.Hash,
                                            out byte[] tXRaw))