/// <summary> /// utility function to wait on a transaction to be confirmed using algod v2 API /// </summary> /// <param name="instance">The algod api instance using algod v2 API</param> /// <param name="txID">transaction ID</param> /// <param name="timeout">how many rounds do you wish to check pending transactions for</param> /// <returns>The pending transaction response</returns> public static PendingTransactionResponse WaitTransactionToComplete(V2.AlgodApi instance, string txID, int timeout = 3) { if (instance == null || txID == null || txID.Length == 0 || timeout < 0) { throw new ArgumentException("Bad arguments for waitForConfirmation."); } NodeStatusResponse nodeStatusResponse = instance.GetStatus(); var startRound = nodeStatusResponse.LastRound + 1; var currentRound = startRound; while (currentRound < (startRound + timeout)) { var pendingInfo = instance.PendingTransactionInformation(txID); if (pendingInfo != null) { if (pendingInfo.ConfirmedRound != null && pendingInfo.ConfirmedRound > 0) { // Got the completed Transaction return(pendingInfo); } if (pendingInfo.PoolError != null && pendingInfo.PoolError.Length > 0) { // If there was a pool error, then the transaction has been rejected! throw new Exception("The transaction has been rejected with a pool error: " + pendingInfo.PoolError); } } instance.WaitForBlock(currentRound); currentRound++; } throw new Exception("Transaction not confirmed after " + timeout + " rounds!"); }
public string?GetServerName() { NodeStatusRequest request = new NodeStatusRequest { Header = { QDCount = 1 }, Question = { Name = "*".PadRight(16, '\0') } }; NodeStatusResponse response = SendNodeStatusRequest(request); return((from entry in response.Names let suffix = NetBiosUtils.GetSuffixFromMSNetBiosName(entry.Key) where suffix == NetBiosSuffix.FileServiceService select entry.Key).FirstOrDefault()); }
public static NodeStatusResponse Unmarshall(UnmarshallerContext context) { NodeStatusResponse nodeStatusResponse = new NodeStatusResponse(); nodeStatusResponse.HttpResponse = context.HttpResponse; nodeStatusResponse.ErrorCode = context.IntegerValue("NodeStatus.ErrorCode"); nodeStatusResponse.ErrorMessage = context.StringValue("NodeStatus.ErrorMessage"); nodeStatusResponse.Success = context.BooleanValue("NodeStatus.Success"); nodeStatusResponse.RequestId = context.StringValue("NodeStatus.RequestId"); nodeStatusResponse.InstanceId = context.StringValue("NodeStatus.InstanceId"); nodeStatusResponse.AutoInstall = context.BooleanValue("NodeStatus.AutoInstall"); nodeStatusResponse.Status = context.StringValue("NodeStatus.Status"); return(nodeStatusResponse); }
/// <summary> /// Gets Status Information For The Target Node /// </summary> public async Task <NodeStatusResponse> GetNodeStatus() { try { NodeStatusResponse response = await base.SendGet <NodeStatusResponse>("api/Node/status"); Guard.Null(response, nameof(response), "'api/Node/status' API Response Was Null!"); logger.LogDebug("Got Node Status Response!"); return(response); } catch (Exception ex) { logger.LogDebug($"An Error '{ex.Message}' Occured When Getting The Node Status!", ex); return(null); } //end of try-catch } //end of public async Task<NodeStatusResponse> GetNodeStatus()
public string GetServerName() { NodeStatusRequest request = new NodeStatusRequest(); request.Header.QDCount = 1; request.Question.Name = "*".PadRight(16, '\0'); NodeStatusResponse response = SendNodeStatusRequest(request); foreach (KeyValuePair <string, NameFlags> entry in response.Names) { NetBiosSuffix suffix = NetBiosUtils.GetSuffixFromMSNetBiosName(entry.Key); if (suffix == NetBiosSuffix.FileServiceService) { return(entry.Key); } } return(null); }
/// <summary> /// Gets Status Information For The Target Node /// </summary> public async Task <NodeStatusResponse> GetNodeStatus() { try { NodeStatusResponse response = await base.SendGet <NodeStatusResponse>("api/Node/status"); Guard.Null(response, nameof(response), "'api/Node/status' API Response Was Null!"); Logger.Debug($"Got Node Status Response!"); return(response); } catch (Exception ex) { Logger.Fatal($"An Error '{ex.Message}' Occured When Getting The Node Status!", ex); throw; //pass it back up the stack? .. this seems memory intensive to me }//end of try-catch }//end of public async Task<NodeStatusResponse> GetNodeStatus()
private void ReceiveCallback(IAsyncResult result) { if (!m_listening) { return; } IPEndPoint remoteEP = null; byte[] buffer; try { buffer = m_client.EndReceive(result, ref remoteEP); } catch (ObjectDisposedException) { return; } catch (SocketException) { return; } // Process buffer if (buffer.Length > NameServicePacketHeader.Length) { NameServicePacketHeader header = new NameServicePacketHeader(buffer, 0); if (header.OpCode == NameServiceOperation.QueryRequest) { NameQueryRequest request = null; try { request = new NameQueryRequest(buffer, 0); } catch { } if (request != null) { if (request.Question.Type == NameRecordType.NB) { string name = NetBiosUtils.GetNameFromMSNetBiosName(request.Question.Name); NetBiosSuffix suffix = (NetBiosSuffix)request.Question.Name[15]; bool nameMatch = String.Equals(name, Environment.MachineName, StringComparison.OrdinalIgnoreCase); if (nameMatch && ((suffix == NetBiosSuffix.WorkstationService) || (suffix == NetBiosSuffix.FileServiceService))) { PositiveNameQueryResponse response = new PositiveNameQueryResponse(); response.Header.TransactionID = request.Header.TransactionID; response.Resource.Name = request.Question.Name; NameFlags nameFlags = new NameFlags(); response.Addresses.Add(m_serverAddress.GetAddressBytes(), nameFlags); byte[] responseBytes = response.GetBytes(); m_client.Send(responseBytes, responseBytes.Length, remoteEP); } } else // NBStat { NodeStatusResponse response = new NodeStatusResponse(); response.Header.TransactionID = request.Header.TransactionID; response.Resource.Name = request.Question.Name; NameFlags nameFlags = new NameFlags(); string name1 = NetBiosUtils.GetMSNetBiosName(Environment.MachineName, NetBiosSuffix.WorkstationService); string name2 = NetBiosUtils.GetMSNetBiosName(Environment.MachineName, NetBiosSuffix.FileServiceService); NameFlags nameFlags3 = new NameFlags(); nameFlags3.WorkGroup = true; string name3 = NetBiosUtils.GetMSNetBiosName(WorkgroupName, NetBiosSuffix.WorkstationService); response.Names.Add(name1, nameFlags); response.Names.Add(name2, nameFlags); response.Names.Add(name3, nameFlags3); byte[] responseBytes = response.GetBytes(); try { m_client.Send(responseBytes, responseBytes.Length, remoteEP); } catch (ObjectDisposedException) { } } } } } try { m_client.BeginReceive(ReceiveCallback, null); } catch (ObjectDisposedException) { } catch (SocketException) { } }
//The Workhorse which refreshes all Node Data public async Task UpdateNodeData(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { try { //are we connected?? if (ConnectionMethod == ConnectionType.Disconnected) { logger.LogDebug( $"Node '{Name}' ({Address}:{Port}), Aborting 'Status' Update. Internal State Is Disconnected!"); } //############ Status Data ################# NodeStatusResponse statusData = await restClient.GetNodeStatus(); if (statusData == null) { logger.LogDebug( $"Node '{Name}' ({Address}:{Port}) An Error Occured Getting Node Status!"); Status = ConnectionStatus.Offline; } else { //we have a new block, so fire off an event if (statusData.consensusHeight > BlockTIP) { OnNewBlock(statusData.consensusHeight); } //update current height (use consensus because they have been fully validated) BlockTIP = statusData.consensusHeight; DataDirectory = statusData.dataDirectoryPath; NodeVersion = statusData.version; ProtocolVersion = $"{statusData.protocolVersion}"; IsTestNet = statusData.testnet; Status = ConnectionStatus.Online; } //############ Update Peers ################# List <GetPeerInfoResponse> peersResponse = await restClient.GetPeerInfo(); if (peersResponse == null) { logger.LogDebug( $"Node '{Name}' ({Address}:{Port}) An Error Occured Getting The Node Peer List!"); } else { Peers = peersResponse.ToPeersList(); } //end of if-else (_Peers == null) //############ TX History Processing ################# UpdateWalletTXs(); //############ Staking Info ################# UpdateStakingInformation(); } catch (HttpRequestException ex) //API is not accessible or responding { OnDisconnected(Address, Port); logger.LogDebug( $"Node '{Name}' ({Address}:{Port}) Something Happened & The Node API Is Not Accessible", ex); Status = ConnectionStatus.Offline; } catch (Exception ex) { logger.LogDebug($"Node '{Name}' ({Address}:{Port}) An Error Occured When Polling For Data!", ex); Status = ConnectionStatus.Offline; } } }