Пример #1
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)
        {
            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.GetSnapshot();
            NeoBlock neoBlock;

            if (request.BlockIdentifier is null ||
                (request.BlockIdentifier.Index is null && request.BlockIdentifier.Hash is null))
            {
                neoBlock = NativeContract.Ledger.GetBlock(snapshot, NativeContract.Ledger.CurrentHash(snapshot));
            }
Пример #2
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());
        }