Example #1
0
        public virtual async Task ProcessTransactionAsync(string transactionHash, BlockWithTransactionHashes block)
        {
            var transactionSource = await GetTransaction(transactionHash).ConfigureAwait(false);

            var transactionReceipt = await GetTransactionReceipt(transactionHash).ConfigureAwait(false);

            if (_contractCreationTransactionProcessor.IsTransactionForContractCreation(transactionSource, transactionReceipt))
            {
                if (EnabledContractCreationProcessing)
                {
                    await
                    _contractCreationTransactionProcessor.ProcessTransactionAsync(transactionSource, transactionReceipt,
                                                                                  block.Timestamp).ConfigureAwait(false);
                }
            }
            else
            {
                if (await _contractTransactionProcessor.IsTransactionForContractAsync(transactionSource))
                {
                    if (EnabledContractProcessing)
                    {
                        await
                        _contractTransactionProcessor.ProcessTransactionAsync(transactionSource, transactionReceipt,
                                                                              block.Timestamp).ConfigureAwait(false);
                    }
                }
                else if (EnabledValueProcessing)
                {
                    await
                    _valueTransactionProcessor.ProcessTransactionAsync(transactionSource, transactionReceipt,
                                                                       block.Timestamp).ConfigureAwait(false);
                }
            }
        }
Example #2
0
 public BlockInformation(BlockWithTransactionHashes block)
 {
     BlockId   = block.BlockHash;
     Height    = (ulong)block.Number.Value;
     Timestamp = DateTimeOffset.FromUnixTimeSeconds((long)block.Timestamp.Value);
     IsEmpty   = block.TransactionHashes.Length == 0;
 }
Example #3
0
        protected void InsertBlock(BlockWithTransactionHashes block)
        {
            try
            {
                var isblock = blockRepository.Get(x => x.BlockNumber == (int)block.Number.Value).FirstOrDefault();

                if (isblock != null) //block has already in DB
                {
                    return;
                }

                var _nodeBlock = new NodeBlock()
                {
                    BlockNumber      = (int)block.Number.Value,
                    BlockTime        = new Helpers().UnixTimeStampToDateTime((double)block.Timestamp.Value),
                    BlockHash        = block.BlockHash,
                    ParentHash       = block.ParentHash,
                    TransactionCount = block.TransactionHashes.Length,
                    Nonce            = block.Nonce,
                };

                blockRepository.Add(_nodeBlock);
                repositoryBase.Commit();
            }
            catch (Exception e)
            {
                new Helpers().AddLog(LogType.Failure, $"Block-{block.Number.Value} DB Write Error => " + e.InnerException);
            }
        }
Example #4
0
        public async Task UpsertBlockAsync(BlockWithTransactionHashes block)
        {
            var blockEntity = MapBlock(block, new Block());

            //await blockEntity.InsertOrReplaceAsync().ConfigureAwait(false);
            await InsertOrUpdate(blockEntity);
        }
Example #5
0
 protected async Task ProcessTransactions(BlockWithTransactionHashes block)
 {
     foreach (var txnHash in block.TransactionHashes)
     {
         await TransactionProcessor.ProcessTransactionAsync(txnHash, block).ConfigureAwait(false);
     }
 }
Example #6
0
        public void BlockWithTransactionHashes_TransactionCount_Returns_Length_Of_Hashes()
        {
            var blockWithTransactionHashes = new BlockWithTransactionHashes
            {
                TransactionHashes = new[] { "0xc185cc7b9f7862255b82fd41be561fdc94d030567d0b41292008095bf31c39b9" }
            };

            Assert.Equal(blockWithTransactionHashes.TransactionHashes.Length, blockWithTransactionHashes.TransactionCount());
        }
        protected async Task ProcessTransactions(BlockWithTransactionHashes block)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            foreach (var txnHash in block.TransactionHashes)
            {
                await TransactionProcessor.ProcessTransactionAsync(txnHash, block).ConfigureAwait(false);
            }
            stopwatch.Stop();
            System.Console.WriteLine("Total Transaction : " + block.TransactionHashes.Length);
            System.Console.WriteLine("Blok içindeki transaction bilgilerini alma : " + stopwatch.Elapsed.TotalSeconds);


            await _transactionRepository.AddTransactions();
        }
Example #8
0
        public Block MapBlock(BlockWithTransactionHashes blockSource, Block blockOutput)
        {
            blockOutput.SetBlockNumber(blockSource.Number);
            blockOutput.SetDifficulty(blockSource.Difficulty);
            blockOutput.SetGasLimit(blockSource.GasLimit);
            blockOutput.SetGasUsed(blockSource.GasUsed);
            blockOutput.SetSize(blockSource.Size);
            blockOutput.SetTimeStamp(blockSource.Timestamp);
            blockOutput.SetTotalDifficulty(blockSource.TotalDifficulty);
            blockOutput.ExtraData        = blockSource.ExtraData ?? string.Empty;
            blockOutput.Hash             = blockSource.BlockHash ?? string.Empty;
            blockOutput.ParentHash       = blockSource.ParentHash ?? string.Empty;
            blockOutput.Miner            = blockOutput.Miner ?? string.Empty;
            blockOutput.Nonce            = blockOutput.Nonce ?? string.Empty;
            blockOutput.TransactionCount = blockSource.TransactionHashes.Length;

            return(blockOutput);
        }
Example #9
0
        /// <summary>
        /// 检查 ETH 确认次数
        /// </summary>
        /// <param name="num"></param>
        /// <param name="ethTransRspList"></param>
        /// <param name="index"></param>
        /// <param name="web3"></param>
        /// <returns></returns>
        private static void CheckEthConfirm(List <TransactionInfo> ethTransRspList, ulong index, Web3Geth web3)
        {
            foreach (var ethTran in ethTransRspList)
            {
                if (index > ethTran.height)
                {
                    BlockWithTransactionHashes hashes = web3.Eth.Blocks.GetBlockWithTransactionsHashesByNumber.SendRequestAsync(new HexBigInteger(ethTran.height)).Result;

                    //如果原区块中还包含该交易,则确认数 = 当前区块高度 - 交易所在区块高度 + 1,不包含该交易,确认数统一记为 0
                    if (hashes.TransactionHashes.Length > 0 && hashes.TransactionHashes.Contains(ethTran.txid))
                    {
                        ethTran.confirmCount = (uint)(index - ethTran.height + 1);
                    }
                    else
                    {
                        ethTran.confirmCount = 0;
                    }
                }
            }
        }
Example #10
0
        public async Task UpsertAsync()
        {
            var source = new BlockWithTransactionHashes
            {
                Number            = new HexBigInteger(DateTime.Now.Ticks),
                Difficulty        = new HexBigInteger("2"),
                GasLimit          = new HexBigInteger("4712388"),
                GasUsed           = new HexBigInteger("1886574"),
                Size              = new HexBigInteger("608"),
                Timestamp         = Utils.CreateBlockTimestamp(),
                TotalDifficulty   = new HexBigInteger("2027"),
                ExtraData         = "0xd983010802846765746887676f312e392e328777696e646f7773000000000000823caa2ecfd32e52827d5fc58e9a6c203c5599e730d0d47c1b10f60ddcff40cb65ef4906023e2e0d32b3814345cce246d3ee56eae47afdb51308b40653940fce01",
                BlockHash         = "0x337cd6feedafac6abba40eff40fb1957e08985180f5a03016924ef72fc7b04b9",
                ParentHash        = "0x60dd93c2acf312d9379613249d0cdf822a878018ec2d6ecca1e40b9c3ec9cc25",
                Miner             = "0xe6de16a66e5cd7270cc36a851818bc092884fe64",
                Nonce             = "4",
                TransactionHashes = new [] { "0xcb00b69d2594a3583309f332ada97d0df48bae00170e36a4f7bbdad7783fc7e5" }
            };

            await _repo.UpsertBlockAsync(source);

            var storedBlock = await _repo.FindByBlockNumberAsync(source.Number);

            Assert.NotNull(storedBlock);

            Assert.Equal(source.Number.Value.ToString(), storedBlock.BlockNumber);
            Assert.Equal(source.Difficulty.ToLong(), storedBlock.Difficulty);
            Assert.Equal(source.GasLimit.ToLong(), storedBlock.GasLimit);
            Assert.Equal(source.GasUsed.ToLong(), storedBlock.GasUsed);
            Assert.Equal(source.Size.ToLong(), storedBlock.Size);
            Assert.Equal(source.Timestamp.ToLong(), storedBlock.TimeStamp);
            Assert.Equal(source.TotalDifficulty.ToLong(), storedBlock.TotalDifficulty);
            Assert.Equal(source.ExtraData, storedBlock.ExtraData);
            Assert.Equal(source.BlockHash, storedBlock.Hash);
            Assert.Equal(source.ParentHash, storedBlock.ParentHash);
            Assert.Equal(source.Miner, storedBlock.Miner);
            Assert.Equal(new HexBigInteger(source.Nonce).ToLong(), storedBlock.Nonce);
            Assert.Equal(source.TransactionHashes.Length, storedBlock.TransactionCount);
        }
        public virtual async Task ProcessTransactionAsync(BlockWithTransactionHashes block)
        {
            nodeBlock = blockRepository.Get(x => x.BlockNumber == block.Number.Value).FirstOrDefault();

            if (nodeBlock?.NodeTransactions != null)
            {
                return;
            }

            nodeBlock.NodeTransactions = new List <NodeTransaction>();

            foreach (var _hash in block.TransactionHashes)
            {
                await CheckandFillTransactionAsync(_hash, block.Timestamp.Value);
            }
            try
            {
                repositoryBase.Commit();
            }
            catch (Exception e)
            {
                new Helpers().AddLog(LogType.Failure, $"Block-{block.Number.Value} Transactions DB Write Error => " + e.InnerException);
            }
        }
Example #12
0
        private async Task <bool> ValidateBlockHash(string block_hash, string tx_hash)
        {
            BlockWithTransactionHashes block = await _web3geth_service.BlockchainManager.GetBlockWithTransactionsHashesByHash(block_hash);

            return((block.TransactionHashes.Contains(tx_hash)) ? true : false);
        }
        public async Task <IReadOnlyList <TransactionReceipt> > GetTransactionReceiptsAsync(BlockWithTransactionHashes blockWithTransaction)
        {
            var getTransactionReceiptBatches = blockWithTransaction.TransactionHashes.Batch(_batchSize);

            var result = new List <TransactionReceipt>();

            var api = EthApi();

            foreach (var transactionReceiptBatch in getTransactionReceiptBatches)
            {
                var dataFetchTasks = transactionReceiptBatch
                                     .Select(x => api.Transactions.GetTransactionReceipt.SendRequestAsync(x))
                                     .ToArray();

                await Task.WhenAll(dataFetchTasks);

                result.AddRange(dataFetchTasks.Select(x => x.Result));
            }

            return(result);
        }
 public BlockViewModel(BlockWithTransactionHashes block)
 {
     Initialise(block);
     _transactionCount = block.TransactionHashes.Length;
 }
        public void GetBlocksWithTransactionHashes_Start()
        {
            // The test works as following:
            // There are already 2 blocks. The method then gets called with
            // a `from` value of 1, and while doing so `latestBlock` becomes
            // 2 because another `updateStream` received a new block.
            // This means that the old stream should consist of blocks 1 and 2,
            // and for completeness we assume a new block number 3 comes afterwards.

            // Setup.
            var sched = new TestScheduler();

            var poller        = new Mock <IObservable <Unit> >();
            var filterService = new Mock <IEthApiFilterService>();
            var blockService  = new Mock <IEthApiBlockService>();

            var provider = new BlockStreamProvider(
                poller.Object,
                filterService.Object,
                blockService.Object);

            blockService
            .Setup(x => x.GetBlockNumber.SendRequestAsync(null))
            .Returns(Task.FromResult(new HexBigInteger(2)));

            // Setup old blocks.
            var oldBlock = new BlockWithTransactionHashes
            {
                BlockHash = "0x1",
                Number    = new HexBigInteger(1)
            };

            blockService
            .Setup(x => x.GetBlockWithTransactionsHashesByNumber.SendRequestAsync(new HexBigInteger(new BigInteger(1)), null))
            .Returns(Task.FromResult(oldBlock));

            // Setup new blocks.
            var newBlocks = new[]
            {
                new BlockWithTransactionHashes
                {
                    BlockHash = "0x2",
                    Number    = new HexBigInteger(2)
                },
                new BlockWithTransactionHashes
                {
                    BlockHash = "0x3",
                    Number    = new HexBigInteger(3)
                }
            };

            var newBlockSource = sched.CreateHotObservable(
                OnNext(100, newBlocks[0]),
                OnNext(200, newBlocks[1]));

            // Record incoming data.
            var res = sched.Start(
                () => provider.GetBlocksWithTransactionHashes(new BlockParameter(1), newBlockSource),
                0,
                50,
                Disposed);

            res.Messages.AssertEqual(
                OnNext(50, oldBlock),
                OnNext(100, newBlocks[0]),
                OnNext(200, newBlocks[1]));
        }