Пример #1
0
        public void ExecuteTxesInSeveralBlocks(List <TransactionReceipt> txes)
        {
            txes = txes.OrderBy(x => x, new ReceiptComparer()).ToList();
            foreach (var tx in txes)
            {
                _transactionPool.Add(tx);
            }
            int total = 10;

            for (int it = 0; it < total; it++)
            {
                var remBlocks  = total - it;
                var txesToTake = txes.Count / remBlocks;
                var takenTxes  = _transactionPool.Peek(txesToTake, txesToTake, _blockManager.GetHeight() + 1);
                var block      = BuildNextBlock(takenTxes.ToArray());
                var result     = ExecuteBlock(block, takenTxes.ToArray());
                Assert.AreEqual(result, OperatingError.Ok);
                var executedBlock = _stateManager.LastApprovedSnapshot.Blocks.GetBlockByHeight(block.Header.Index);
                Assert.AreEqual(executedBlock !.TransactionHashes.Count, takenTxes.Count);

                // check if the txes are executed properly
                foreach (var tx in takenTxes)
                {
                    var executedTx = _stateManager.LastApprovedSnapshot.Transactions.GetTransactionByHash(tx.Hash);
                    Assert.AreNotEqual(null, executedTx, $"Transaction {tx.Hash.ToHex()} not found");
                    Assert.AreEqual(TransactionStatus.Executed, executedTx !.Status,
                                    "Transaction {tx.Hash.ToHex()} was not executed properly");
                }
            }
        }
Пример #2
0
        private void _Bench_Tx_Pool(
            ITransactionBuilder transactionBuilder,
            ITransactionSigner transactionSigner,
            EcdsaKeyPair keyPair)
        {
            const int txGenerate = 1000;
            const int txPerBlock = 1000;

            Logger.LogInformation($"Setting initial balance for the 'From' address");
            _stateManager.LastApprovedSnapshot.Balances.AddBalance(keyPair.PublicKey.GetAddress(),
                                                                   Money.Parse("200000"));

            var txReceipts = new List <TransactionReceipt>();

            for (int i = 0; i < txGenerate; i++)
            {
                var randomValue = new Random().Next(1, 100);
                var amount      = Money.Parse($"{randomValue}.0").ToUInt256();

                byte[] random = new byte[32];
                RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                rng.GetBytes(random);

                var tx = new Transaction
                {
                    To       = random.Slice(0, 20).ToUInt160(),
                    From     = keyPair.PublicKey.GetAddress(),
                    GasPrice = (ulong)Money.Parse("0.0000001").ToWei(),
                    GasLimit = 100000000,
                    Nonce    = (ulong)i,
                    Value    = amount
                };
                txReceipts.Add(transactionSigner.Sign(tx, keyPair, true));
            }

            ITransactionPool transactionPool = _container.Resolve <ITransactionPool>();
            var watch = System.Diagnostics.Stopwatch.StartNew();

            foreach (var txr in txReceipts)
            {
                transactionPool.Add(txr, false);
            }
            watch.Stop();
            Console.WriteLine($"Time to Add {transactionPool.Transactions.Count} Tx to pool: {watch.ElapsedMilliseconds} ms");

            watch.Restart();
            var txs = transactionPool.Peek(txGenerate, txGenerate);

            watch.Stop();
            Console.WriteLine($"Time to Peek {txs.Count} Tx from pool: {watch.ElapsedMilliseconds} ms");
        }
Пример #3
0
        // Given an era, returns a proposed set of transaction receipts
        public IEnumerable <TransactionReceipt> GetTransactionsToPropose(long era)
        {
            var n     = _validatorManager.GetValidators(era - 1) !.N;
            var txNum = (BatchSize + n - 1) / n;

            if (era < 0)
            {
                Logger.LogError($"era : {era} should not be negative");
                throw new ArgumentException("era is negative");
            }
            // the number of transactions in a block is controlled by this "BatchSize"
            // variable. Every validator considers BatchSize number of transactions
            // and takes ceil(BatchSize / validatorCount) number of transactions. If the
            // transactions are selected randomly, then the expected number of transactions
            // in a block is BatchSize.
            var taken = _transactionPool.Peek(BatchSize, txNum, (ulong)era);

            Logger.LogTrace($"Proposed Transactions Count: {taken.Count()}");
            return(taken);
        }
Пример #4
0
 private TransactionReceipt[] GetCurrentPoolTxes(ulong era)
 {
     return(_transactionPool.Peek(1000, 1000, era).ToArray());
 }