public JObject NetworkStatus(NetworkRequest request)
        {
            if (request.NetworkIdentifier?.Blockchain?.ToLower() != "neo n3")
            {
                return(Error.NETWORK_IDENTIFIER_INVALID.ToJson());
            }
            if (request.NetworkIdentifier?.Network?.ToLower() != network)
            {
                return(Error.NETWORK_IDENTIFIER_INVALID.ToJson());
            }

            var      snapshot      = system.StoreView;
            uint     currentHeight = NativeContract.Ledger.CurrentIndex(snapshot);
            NeoBlock currentBlock  = NativeContract.Ledger.GetBlock(snapshot, currentHeight);

            if (currentBlock == null)
            {
                return(Error.BLOCK_NOT_FOUND.ToJson());
            }

            string currentBlockHash      = currentBlock.Hash.ToString();
            long   currentBlockTimestamp = (long)currentBlock.Timestamp;

            BlockIdentifier currentBlockIdentifier = new BlockIdentifier(currentHeight, currentBlockHash);
            BlockIdentifier genesisBlockIdentifier = new BlockIdentifier(system.GenesisBlock.Index, system.GenesisBlock.Hash.ToString());

            var localNode = system.LocalNode.Ask <LocalNode>(new LocalNode.GetInstance()).Result;

            var connected = localNode.GetRemoteNodes().Select(p => new Peer(p.GetHashCode().IntToHash160String(),
                                                                            new Metadata(new Dictionary <string, JObject>
            {
                { "connected", true.ToString().ToLower() },
                { "address", p.Listener.ToString() },
                { "height", p.LastBlockIndex }
            })
                                                                            ));

            var unconnected = localNode.GetUnconnectedPeers().Select(p => new Peer(p.GetHashCode().IntToHash160String(),
                                                                                   new Metadata(new Dictionary <string, JObject>
            {
                { "unconnected", false.ToString().ToLower() },
                { "address", p.ToString() }
            })
                                                                                   ));

            Peer[] peers = connected.Concat(unconnected).ToArray();
            NetworkStatusResponse response = new NetworkStatusResponse(currentBlockIdentifier, currentBlockTimestamp, genesisBlockIdentifier, peers);

            return(response.ToJson());
        }
Exemple #2
0
        public JObject AccountBalance(AccountBalanceRequest request)
        {
            if (request.AccountIdentifier is null)
            {
                return(Error.ACCOUNT_IDENTIFIER_INVALID.ToJson());
            }

            UInt160 account;

            try
            {
                account = request.AccountIdentifier.Address.ToScriptHash();
            }
            catch (Exception)
            {
                return(Error.ACCOUNT_ADDRESS_INVALID.ToJson());
            }

            // can only get current balance
            Amount[] balances = GetUtxoBalance(account);
            if (balances is null)
            {
                return(Error.ACCOUNT_NOT_FOUND.ToJson());
            }

            if (request.AccountIdentifier.SubAccountIdentifier != null) // then need to get the nep5 balance
            {
                if (!UInt160.TryParse(request.AccountIdentifier.SubAccountIdentifier.Address, out UInt160 scriptHash))
                {
                    return(Error.CONTRACT_ADDRESS_INVALID.ToJson());
                }
                Amount[] nep5Balances = GetNep5Balance(scriptHash, account);
                if (nep5Balances is null)
                {
                    return(Error.VM_FAULT.ToJson());
                }
                balances = balances.Concat(nep5Balances).ToArray();
            }

            NeoBlock               currentBlock    = Blockchain.Singleton.GetBlock(Blockchain.Singleton.CurrentBlockHash);
            BlockIdentifier        blockIdentifier = new BlockIdentifier(currentBlock.Index, currentBlock.Hash.ToString());
            AccountBalanceResponse response        = new AccountBalanceResponse(blockIdentifier, balances);

            return(response.ToJson());
        }
Exemple #3
0
        private void UpdateNodeTimes(Node node, Neo.Network.P2P.Payloads.Block block)
        {
            if (!node.FirstRuntime.HasValue)
            {
                node.FirstRuntime = block.Timestamp;
            }

            if (!node.LatestRuntime.HasValue)
            {
                node.LatestRuntime = node.FirstRuntime;
                node.SecondsOnline = 0;
            }
            else
            {
                node.SecondsOnline += this.GetTotalSecondsElapsed(block.Timestamp);
                node.LatestRuntime  = block.Timestamp;
            }
        }
        public JObject NetworkStatus(NetworkRequest request)
        {
            long     currentHeight = Blockchain.Singleton.Height;
            NeoBlock currentBlock  = Blockchain.Singleton.GetBlock(Blockchain.Singleton.CurrentBlockHash);

            if (currentBlock == null)
            {
                return(Error.BLOCK_NOT_FOUND.ToJson());
            }

            string currentBlockHash      = currentBlock.Hash.ToString();
            long   currentBlockTimestamp = currentBlock.Timestamp * 1000;

            BlockIdentifier currentBlockIdentifier = new BlockIdentifier(currentHeight, currentBlockHash);
            BlockIdentifier genesisBlockIdentifier = new BlockIdentifier(Blockchain.GenesisBlock.Index, Blockchain.GenesisBlock.Hash.ToString());

            var connected = LocalNode.Singleton.GetRemoteNodes().Select(p => new Peer(p.GetHashCode().IntToHash160String(),
                                                                                      new Metadata(new Dictionary <string, JObject>
            {
                { "connected", true.ToString().ToLower() },
                { "address", p.Listener.ToString() },
                { "height", p.LastBlockIndex.ToString() }
            })
                                                                                      ));

            var unconnected = LocalNode.Singleton.GetUnconnectedPeers().Select(p => new Peer(p.GetHashCode().IntToHash160String(),
                                                                                             new Metadata(new Dictionary <string, JObject>
            {
                { "connected", false.ToString().ToLower() },
                { "address", p.ToString() }
            })
                                                                                             ));

            Peer[] peers = connected.Concat(unconnected).ToArray();
            NetworkStatusResponse response = new NetworkStatusResponse(currentBlockIdentifier, currentBlockTimestamp, genesisBlockIdentifier, peers);

            return(response.ToJson());
        }
Exemple #5
0
        private NodeAudit NodeAudit(Node node, Neo.Network.P2P.Payloads.Block block)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            int?height = null;

            if (node.Type == StateOfNeo.Common.NodeAddressType.REST)
            {
                if (node.Service == NodeCallsConstants.NeoScan)
                {
                    var heightResponse = HttpRequester.MakeRestCall <HeightResponseObject>($@"{node.Url}get_height", HttpMethod.Get)
                                         .GetAwaiter()
                                         .GetResult();

                    if (heightResponse != null)
                    {
                        height = heightResponse.Height;
                    }
                }
                else if (node.Service == NodeCallsConstants.NeoNotification)
                {
                    var versionResponse = HttpRequester.MakeRestCall <NeoNotificationVersionResponse>($@"{node.Url}version", HttpMethod.Get)
                                          .GetAwaiter()
                                          .GetResult();

                    if (versionResponse != null)
                    {
                        height = versionResponse.Height;
                    }
                }
            }
            else
            {
                height = this.nodeCaller.GetNodeHeight(node).GetAwaiter().GetResult();
            }

            if (height.HasValue)
            {
                node.Height = height.Value;
                this.UpdateNodeTimes(node, block);
                this.UpdateCache(LatencyCacheType, node.Id, stopwatch.ElapsedMilliseconds);

                if (node.Type == StateOfNeo.Common.NodeAddressType.RPC)
                {
                    var peers = this.nodeCaller.GetNodePeers(node).GetAwaiter().GetResult();
                    if (peers != null)
                    {
                        this.UpdateCache(PeersCacheType, node.Id, peers.Connected.Count());
                    }
                }

                if (node.LastAudit == null || node.LastAudit.Value.ToUnixDate().AddHours(1) < block.Timestamp.ToUnixDate())
                {
                    decimal?peers      = null;
                    var     peersValue = this.GetAuditValue(PeersCacheType, node.Id);
                    if (peersValue != null)
                    {
                        peers = (decimal)peersValue.Value;
                    }

                    var audit = new NodeAudit
                    {
                        NodeId    = node.Id,
                        Peers     = peers,
                        Latency   = (int)this.GetAuditValue(LatencyCacheType, node.Id),
                        CreatedOn = DateTime.UtcNow,
                        Timestamp = block.Timestamp
                    };

                    node.LastAudit = block.Timestamp;
                    return(audit);
                }
            }

            return(null);
        }