Ejemplo n.º 1
0
        /// <inheritdoc />
        /// <summary>
        /// Mine process.
        /// </summary>
        /// <returns></returns>
        public async Task <Block> MineAsync(Hash previousBlockHash, long previousBlockHeight, Timestamp blockTime,
                                            Duration blockExecutionTime)
        {
            var limit = await _blockTransactionLimitProvider.GetLimitAsync();

            var executableTransactionSet =
                await _txHub.GetExecutableTransactionSetAsync(_transactionPackingService.IsTransactionPackingEnabled()
                                                              ?limit
                                                              : -1);

            var pending = new List <Transaction>();

            if (executableTransactionSet.PreviousBlockHash == previousBlockHash)
            {
                pending = executableTransactionSet.Transactions;
            }
            else
            {
                Logger.LogWarning($"Transaction pool gives transactions to be appended to " +
                                  $"{executableTransactionSet.PreviousBlockHash} which doesn't match the current " +
                                  $"best chain hash {previousBlockHash}.");
            }

            Logger.LogTrace(
                $"Start mining with previous hash: {previousBlockHash}, previous height: {previousBlockHeight}.");
            return(await _miningService.MineAsync(
                       new RequestMiningDto
            {
                PreviousBlockHash = previousBlockHash,
                PreviousBlockHeight = previousBlockHeight,
                BlockExecutionTime = blockExecutionTime
            }, pending, blockTime));
        }
Ejemplo n.º 2
0
        public async Task Broadcast_Transaction_Success()
        {
            // Generate a transaction
            var transaction = await _osTestHelper.GenerateTransferTransaction();

            var transactionHash = transaction.GetHash();

            var response = await JsonCallAsJObject("/chain", "BroadcastTransaction",
                                                   new { rawTransaction = transaction.ToByteArray().ToHex() });

            var responseTransactionId = response["result"]["TransactionId"].ToString();

            responseTransactionId.ShouldBe(transactionHash.ToHex());

            var existTransaction = await _txHub.GetExecutableTransactionSetAsync();

            existTransaction.Transactions[0].GetHash().ShouldBe(transactionHash);
        }
Ejemplo n.º 3
0
        public async Task Broadcast_Transaction_Success()
        {
            // Generate a transaction
            var transaction = await _osTestHelper.GenerateTransferTransaction();

            var transactionHash = transaction.GetHash();

            var parameters = new Dictionary <string, string>
            {
                { "rawTransaction", transaction.ToByteArray().ToHex() }
            };

            var broadcastTransactionResponse =
                await PostResponseAsObjectAsync <BroadcastTransactionOutput>("/api/blockChain/broadcastTransaction", parameters);

            broadcastTransactionResponse.TransactionId.ShouldBe(transactionHash.ToHex());

            var existTransaction = await _txHub.GetExecutableTransactionSetAsync();

            existTransaction.Transactions[0].GetHash().ShouldBe(transactionHash);
        }
Ejemplo n.º 4
0
        public async Task GetExecutableTransactionSet_Test()
        {
            var chain = await _blockchainService.GetChainAsync();

            await _txHub.UpdateTransactionPoolByBestChainAsync(chain.BestChainHash, chain.BestChainHeight);

            var transaction =
                _kernelTestHelper.GenerateTransaction(chain.BestChainHeight, chain.BestChainHash);

            await AddTransactionsAsync(new List <Transaction>
            {
                transaction
            });

            await _txHub.UpdateTransactionPoolByBestChainAsync(chain.BestChainHash, chain.BestChainHeight);

            var executableTransactionSet = await _txHub.GetExecutableTransactionSetAsync(chain.BestChainHash, 0);

            executableTransactionSet.PreviousBlockHash.ShouldBe(chain.BestChainHash);
            executableTransactionSet.PreviousBlockHeight.ShouldBe(chain.BestChainHeight);
            executableTransactionSet.Transactions.Count.ShouldBe(0);

            executableTransactionSet = await _txHub.GetExecutableTransactionSetAsync(chain.BestChainHash, int.MaxValue);

            executableTransactionSet.PreviousBlockHash.ShouldBe(chain.BestChainHash);
            executableTransactionSet.PreviousBlockHeight.ShouldBe(chain.BestChainHeight);
            executableTransactionSet.Transactions[0].ShouldBe(transaction);

            var wrongBlockHash = HashHelper.ComputeFrom("WrongBlockHash");

            executableTransactionSet = await _txHub.GetExecutableTransactionSetAsync(wrongBlockHash, int.MaxValue);

            executableTransactionSet.PreviousBlockHash.ShouldBe(chain.BestChainHash);
            executableTransactionSet.PreviousBlockHeight.ShouldBe(chain.BestChainHeight);
            executableTransactionSet.Transactions.Count.ShouldBe(0);
        }
Ejemplo n.º 5
0
        /// <inheritdoc />
        /// <summary>
        /// Mine process.
        /// </summary>
        /// <returns></returns>
        public async Task <Block> MineAsync(Hash previousBlockHash, long previousBlockHeight, DateTime dateTime,
                                            TimeSpan timeSpan)
        {
            var executableTransactionSet = await _txHub.GetExecutableTransactionSetAsync();

            var pending = new List <Transaction>();

            if (executableTransactionSet.PreviousBlockHash == previousBlockHash)
            {
                pending = executableTransactionSet.Transactions;
            }
            else
            {
                Logger.LogWarning($"Transaction pool gives transactions to be appended to " +
                                  $"{executableTransactionSet.PreviousBlockHash} which doesn't match the current " +
                                  $"best chain hash {previousBlockHash}.");
            }

            return(await _miningService.MineAsync(previousBlockHash, previousBlockHeight, pending, dateTime, timeSpan));
        }
Ejemplo n.º 6
0
 public async Task <ExecutableTransactionSet> GetExecutableTransactionSetAsync(Hash blockHash, int transactionCount = 0)
 {
     return(await _txHub.GetExecutableTransactionSetAsync(blockHash, transactionCount));
 }