Ejemplo n.º 1
0
        /// <summary>
        /// Main update loop
        /// </summary>
        private async void Update()
        {
            // Loop as long as the wallet is connected
            while (Connected)
            {
                try
                {
                    // Ensure network connection is still alive
                    if (!Local && !TurtleCoin.Ping(Address, Port))
                    {
                        // Connection was lost
                        await Exit();

                        ThrowError(ErrorCode.CONNECTION_LOST);
                        LogLine("Connection lost");
                        OnDisconnect?.Invoke(this, EventArgs.Empty);
                        break;
                    }
                    else if (Local && Process.HasExited)
                    {
                        // Connection was lost
                        await Exit();

                        ThrowError(ErrorCode.CONNECTION_LOST);
                        LogLine("Connection lost");
                        OnDisconnect?.Invoke(this, EventArgs.Empty);
                        break;
                    }

                    // Create a result object to recycle throughout updates
                    JObject Result = new JObject();

                    // Update status
                    await SendRequestAsync(RequestMethod.GET_STATUS, new JObject { }, out Result);

                    PeerCount       = (double)Result["peerCount"];
                    BlockCount      = (double)Result["blockCount"];
                    LastBlockHash   = (string)Result["lastBlockHash"];
                    KnownBlockCount = (double)Result["knownBlockCount"];

                    // Check if wallet is synced
                    if (!Synced && Daemon.Synced && BlockCount >= Daemon.NetworkHeight - 2) // Fuzzy sync status
                    {
                        // Set ready status
                        Synced = true;

                        // Trigger ready event handler
                        LogLine("Synced");
                        OnSynced?.Invoke(this, EventArgs.Empty);
                    }
                    else
                    {
                        Synced = false;
                    }

                    // Update balance
                    await SendRequestAsync(RequestMethod.GET_BALANCE, new JObject { }, out Result);

                    AvailableBalance = (double)Result["availableBalance"] / 100;
                    LockedAmount     = (double)Result["lockedAmount"] / 100;

                    // Invoke update event
                    OnUpdate?.Invoke(this, EventArgs.Empty);

                    // Wait for specified amount of time
                    await Task.Delay(RefreshRate, CancellationSource.Token);
                }
                catch { }
            }
        }
        /// <summary>
        /// Main update loop
        /// </summary>
        private async void Update()
        {
            // Loop as long as the daemon is connected
            while (Connected)
            {
                try
                {
                    // Ensure network connection is still alive
                    if (!Local && !TurtleCoin.Ping(Address, Port))
                    {
                        // Connection was lost
                        await Exit();

                        ThrowError(ErrorCode.CONNECTION_LOST);
                        LogLine("Connection lost");
                        OnDisconnect?.Invoke(this, EventArgs.Empty);
                        break;
                    }
                    else if (Local && Process.HasExited)
                    {
                        // Connection was lost
                        await Exit();

                        ThrowError(ErrorCode.CONNECTION_LOST);
                        LogLine("Connection lost");
                        OnDisconnect?.Invoke(this, EventArgs.Empty);
                        break;
                    }

                    // Create a result object to recycle throughout updates
                    JObject Result = new JObject();

                    // Update status
                    await SendRequestAsync(RequestMethod.GET_INFO, new JObject { }, out Result);

                    // Make sure daemon is correct version
                    if (Result["synced"] == null)
                    {
                        // Incorrect daemon version
                        await Exit();

                        ThrowError(ErrorCode.INCORRECT_DAEMON_VERSION);
                        LogLine("Daemon is wrong version, use a newer version");
                        OnDisconnect?.Invoke(this, EventArgs.Empty);
                        break;
                    }

                    // Populate network info
                    AltBlocksCount           = (double)Result["alt_blocks_count"];
                    Difficulty               = (double)Result["difficulty"];
                    GreyPeerlistSize         = (double)Result["grey_peerlist_size"];
                    Hashrate                 = (double)Result["hashrate"];
                    Height                   = (double)Result["height"];
                    IncomingConnectionsCount = (double)Result["incoming_connections_count"];
                    LastKnownBlockIndex      = (double)Result["last_known_block_index"];
                    NetworkHeight            = (double)Result["network_height"];
                    OutgoingConnectionsCount = (double)Result["outgoing_connections_count"];
                    Status                   = (string)Result["status"];
                    TransactionCount         = (double)Result["tx_count"];
                    TransactionPoolSize      = (double)Result["tx_pool_size"];
                    WhitePeerlistSize        = (double)Result["white_peerlist_size"];

                    // Check if daemon is ready
                    if (!Synced && (bool)Result["synced"] == true)
                    {
                        // Set ready status
                        Synced = true;

                        // Trigger ready event handler
                        LogLine("Synced");
                        OnSynced?.Invoke(this, EventArgs.Empty);
                    }
                    else
                    {
                        Synced = false;
                    }

                    // Do updating

                    // Invoke update event
                    OnUpdate?.Invoke(this, EventArgs.Empty);

                    // Wait for specified amount of time
                    await Task.Delay(RefreshRate, CancellationSource.Token);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }