Esempio n. 1
0
        public JObject ToJson()
        {
            JObject json = new JObject();

            json["network_identifier"]     = NetworkIdentifier.ToJson();
            json["block_identifier"]       = BlockIdentifier.ToJson();
            json["transaction_identifier"] = TransactionIdentifier.ToJson();
            return(json);
        }
Esempio n. 2
0
 public Block(BlockIdentifier blockIdentifier, BlockIdentifier parentBlockIdentifier,
              long timestamp, Transaction[] transactions, Metadata metadata = null)
 {
     BlockIdentifier       = blockIdentifier;
     ParentBlockIdentifier = parentBlockIdentifier;
     Timestamp             = timestamp;
     Transactions          = transactions;
     Metadata = metadata;
 }
 public NetworkStatusResponse(BlockIdentifier currentBlockIdentifier, long currentBlockTimestamp, BlockIdentifier genesisBlockIdentifier,
                              Peer[] peers, BlockIdentifier oldestBlockIdentifier = null)
 {
     CurrentBlockIdentifier = currentBlockIdentifier;
     CurrentBlockTimestamp  = currentBlockTimestamp;
     GenesisBlockIdentifier = genesisBlockIdentifier;
     Peers = peers;
     OldestBlockIdentifier = oldestBlockIdentifier;
 }
Esempio n. 4
0
 public static Block FromJson(JObject json)
 {
     return(new(
                BlockIdentifier.FromJson(json["block_identifier"]),
                BlockIdentifier.FromJson(json["parent_block_identifier"]),
                (long)json["timestamp"].GetNumber(),
                json["transactions"].GetArray().Select(p => Transaction.FromJson(p)).ToArray(),
                Metadata.FromJson(json["metadata"])
                ));
 }
        public JObject ToJson()
        {
            JObject json = new JObject();

            json["block_identifier"] = BlockIdentifier.ToJson();
            json["balances"]         = Balances.Select(p => p.ToJson()).ToArray();
            if (Metadata != null && Metadata.ToJson() != null)
            {
                json["metadata"] = Metadata.ToJson();
            }
            return(json);
        }
Esempio n. 6
0
        public JObject ToJson()
        {
            JObject json = new JObject();

            json["block_identifier"]        = BlockIdentifier.ToJson();
            json["parent_block_identifier"] = ParentBlockIdentifier.ToJson();
            json["timestamp"]    = Timestamp.ToString();
            json["transactions"] = Transactions.Select(p => p.ToJson()).ToArray();
            if (Metadata != null && Metadata.ToJson() != null)
            {
                json["metadata"] = Metadata.ToJson();
            }
            return(json);
        }
        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());
        }
Esempio n. 8
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());
        }
        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());
        }
 public AccountBalanceResponse(BlockIdentifier blockIdentifier, Amount[] balances, Metadata metadata = null)
 {
     BlockIdentifier = blockIdentifier;
     Balances        = balances;
     Metadata        = metadata;
 }
Esempio n. 11
0
        /// <summary>
        /// Get a block by its Block Identifier.
        /// If transactions are returned in the same call to the node as fetching the block,
        /// the response should include these transactions in the Block object.
        /// If not, an array of Transaction Identifiers should be returned.
        /// So /block/transaction fetches can be done to get all transaction information.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JObject Block(BlockRequest request)
        {
            var index = request.BlockIdentifier.Index;
            var hash  = request.BlockIdentifier.Hash;

            if (index == null && hash == null)
            {
                return(Error.BLOCK_IDENTIFIER_INVALID.ToJson());
            }
            if (index != null && index < 0)
            {
                return(Error.BLOCK_INDEX_INVALID.ToJson());
            }

            NeoBlock neoBlock;
            UInt256  blockHash;

            if (hash != null)
            {
                if (!UInt256.TryParse(hash, out blockHash))
                {
                    return(Error.BLOCK_HASH_INVALID.ToJson());
                }
            }
            else
            {
                blockHash = Blockchain.Singleton.GetBlockHash((uint)index);
                if (blockHash == null)
                {
                    return(Error.BLOCK_INDEX_INVALID.ToJson());
                }
            }
            neoBlock = Blockchain.Singleton.GetBlock(blockHash);
            if (neoBlock == null)
            {
                return(Error.BLOCK_NOT_FOUND.ToJson());
            }
            BlockIdentifier blockIdentifier = new BlockIdentifier(neoBlock.Index, neoBlock.Hash.ToString());

            // get parent block
            BlockIdentifier parentBlockIdentifier;

            if (neoBlock.Index == 0)
            {
                parentBlockIdentifier = blockIdentifier;
            }
            else
            {
                var parentBlockHash = Blockchain.Singleton.GetBlockHash(neoBlock.Index - 1);
                if (parentBlockHash == null)
                {
                    return(Error.BLOCK_INDEX_INVALID.ToJson());
                }

                var parentBlock = Blockchain.Singleton.GetBlock(parentBlockHash);
                if (parentBlock == null)
                {
                    return(Error.BLOCK_NOT_FOUND.ToJson());
                }

                parentBlockIdentifier = new BlockIdentifier(parentBlock.Index, parentBlock.Hash.ToString());
            }

            // handle transactions
            Transaction[]           transactions      = new Transaction[] { };
            TransactionIdentifier[] otherTransactions = new TransactionIdentifier[] { };
            foreach (var neoTx in neoBlock.Transactions)
            {
                var tx = ConvertTx(neoTx);
                if (tx == null)
                {
                    continue;
                }
                if (tx.Operations.Length > 0)
                {
                    transactions = transactions.Append(tx).ToArray();
                }
                else
                {
                    otherTransactions = otherTransactions.Append(new TransactionIdentifier(neoTx.Hash.ToString())).ToArray();
                }
            }

            Block         block    = new Block(blockIdentifier, parentBlockIdentifier, neoBlock.Timestamp * 1000, transactions);
            BlockResponse response = new BlockResponse(block, otherTransactions.Length > 0 ? otherTransactions : null);

            return(response.ToJson());
        }
Esempio n. 12
0
 public static BlockTransactionRequest FromJson(JObject json)
 {
     return(new BlockTransactionRequest(NetworkIdentifier.FromJson(json["network_identifier"]),
                                        BlockIdentifier.FromJson(json["block_identifier"]),
                                        TransactionIdentifier.FromJson(json["transaction_identifier"])));
 }
Esempio n. 13
0
 public BlockTransactionRequest(NetworkIdentifier networkIdentifier, BlockIdentifier blockIdentifier, TransactionIdentifier transactionIdentifier)
 {
     NetworkIdentifier     = networkIdentifier;
     BlockIdentifier       = blockIdentifier;
     TransactionIdentifier = transactionIdentifier;
 }