예제 #1
0
        // changed from private to public: GetBlockRawByNumberBatch()
        public void Test_Web3BlockBatch()
        {
            ulong total = 5;

            GenerateBlocksWithGenesis(total);
            var listOfBlockNo  = new List <ulong>();
            var listOfBlockTag = new List <String>();

            for (ulong iter = 0; iter <= total + 5; iter++)
            {
                listOfBlockNo.Add(iter);
                listOfBlockTag.Add(iter.ToHex());
            }
            var rawBlockList = _apiService.GetBlockRawByNumberBatch(listOfBlockTag);

            foreach (var item in rawBlockList)
            {
                CheckHex(item.ToString());
            }
            var BlockToJArray = new JArray();

            foreach (var item in listOfBlockNo)
            {
                var block = _blockManager.GetByHeight(item);
                if (block == null)
                {
                    continue;
                }
                BlockToJArray.Add(Web3DataFormatUtils.Web3BlockRaw(block !));
            }

            Assert.AreEqual(BlockToJArray, rawBlockList);
        }
예제 #2
0
        /*
         * GetBlock
         * blockHash, UInt256
         */
        public string?GetBlock(string[] arguments)
        {
            if (arguments.Length != 2)
            {
                return("Wrong number of arguments");
            }
            var value = EraseHexPrefix(arguments[1]);

            return(ulong.TryParse(value, out var blockHeight)
                ? ProtoUtils.ParsedObject(_blockManager.GetByHeight(blockHeight) ??
                                          throw new InvalidOperationException())
                : ProtoUtils.ParsedObject(_blockManager.GetByHash(arguments[1].HexToUInt256()) ??
                                          throw new InvalidOperationException()));
        }
예제 #3
0
        public JArray SyncBlocks(ulong id, BlockchainEventFilterParams filter, bool poll)
        {
            var results         = new JArray();
            var lastSyncedBlock = filter.LastSyncedBlock;
            var highestBlock    = _stateManager.LastApprovedSnapshot.Blocks.GetTotalBlockHeight();

            for (var i = lastSyncedBlock; i < highestBlock; i++)
            {
                var block = _blockManager.GetByHeight(i);
                if (block is null)
                {
                    continue;
                }
                results.Add(Web3DataFormatUtils.Web3Data(block !.Hash));
            }

            if (poll)
            {
                filter.LastSyncedBlock = highestBlock;
                filter.PollingTime     = TimeUtils.CurrentTimeMillis();
                _filters[id]           = filter;
            }
            return(results);
        }
예제 #4
0
        private JObject?GetBlockByHeight(uint blockHeight)
        {
            var block = _blockManager.GetByHeight(blockHeight);

            return(block?.ToJson());
        }
예제 #5
0
        public JObject?GetBlockByNumber(string blockTag, bool fullTx)
        {
            var blockNumber = GetBlockNumberByTag(blockTag);

            if (blockNumber == null)
            {
                return(null);
            }
            var block = _blockManager.GetByHeight((ulong)blockNumber);

            if (block == null)
            {
                return(null);
            }

            ulong gasUsed = 0;
            var   txArray = new JArray();

            if (block.TransactionHashes.Count <= 0)
            {
                return(Web3DataFormatUtils.Web3Block(block !, gasUsed, txArray));
            }
            List <TransactionReceipt> txs = new List <TransactionReceipt>();

            try
            {
                foreach (var txHash in block.TransactionHashes)
                {
                    Logger.LogInformation($"Transaction hash {txHash.ToHex()} in block {blockNumber}");
                    TransactionReceipt?tx = _transactionManager.GetByHash(txHash);
                    if (tx is null)
                    {
                        Logger.LogWarning($"Transaction not found in DB {txHash.ToHex()}");
                    }
                    else
                    {
                        txs.Add(tx);
                    }
                }
            }
            catch (Exception e)
            {
                Logger.LogWarning($"Exception {e}");
                Logger.LogWarning($"block {block!.Hash},  {block.Header.Index}, {block.TransactionHashes.Count}");
                foreach (var txhash in block.TransactionHashes)
                {
                    Logger.LogWarning($"txhash {txhash.ToHex()}");
                }
            }

            try
            {
                gasUsed = txs.Aggregate(gasUsed, (current, tx) => current + tx.GasUsed);
            }
            catch (Exception e)
            {
                Logger.LogWarning($"Exception {e}");
                Logger.LogWarning($"txs {txs}");
                foreach (var tx in txs)
                {
                    if (tx is null)
                    {
                        continue;
                    }
                    Logger.LogWarning($"tx {tx.Hash.ToHex()} {tx.GasUsed} {tx.Status} {tx.IndexInBlock}");
                }
            }

            if (fullTx)
            {
                txArray = Web3DataFormatUtils.Web3BlockTransactionArray(txs, block !.Hash, block !.Header.Index);
            }
            else
            {
                foreach (var tx in txs)
                {
                    txArray.Add(Web3DataFormatUtils.Web3Data(tx.Hash));
                }
            }
            return(Web3DataFormatUtils.Web3Block(block !, gasUsed, txArray));
        }