コード例 #1
0
        /// <summary>
        /// Prevent that block exceed the max size
        /// </summary>
        /// <param name="txs">Ordered transactions</param>
        internal void EnsureMaxBlockSize(IEnumerable <Transaction> txs)
        {
            uint maxBlockSize            = NativeContract.Policy.GetMaxBlockSize(Snapshot);
            uint maxTransactionsPerBlock = NativeContract.Policy.GetMaxTransactionsPerBlock(Snapshot);

            // Limit Speaker proposal to the limit `MaxTransactionsPerBlock` or all available transactions of the mempool
            txs = txs.Take((int)maxTransactionsPerBlock);
            List <UInt256> hashes = new List <UInt256>();

            Transactions      = new Dictionary <UInt256, Transaction>();
            SendersFeeMonitor = new SendersFeeMonitor();

            // Expected block size
            var blockSize = GetExpectedBlockSizeWithoutTransactions(txs.Count());

            // Iterate transaction until reach the size
            foreach (Transaction tx in txs)
            {
                // Check if maximum block size has been already exceeded with the current selected set
                blockSize += tx.Size;
                if (blockSize > maxBlockSize)
                {
                    break;
                }

                hashes.Add(tx.Hash);
                Transactions.Add(tx.Hash, tx);
                SendersFeeMonitor.AddSenderFee(tx);
            }

            TransactionHashes = hashes.ToArray();
        }
コード例 #2
0
        public void TestMemPoolSenderFee()
        {
            Transaction       transaction       = CreateTransactionWithFee(1, 2);
            SendersFeeMonitor sendersFeeMonitor = new SendersFeeMonitor();

            sendersFeeMonitor.GetSenderFee(transaction.Sender).Should().Be(0);
            sendersFeeMonitor.AddSenderFee(transaction);
            sendersFeeMonitor.GetSenderFee(transaction.Sender).Should().Be(3);
            sendersFeeMonitor.AddSenderFee(transaction);
            sendersFeeMonitor.GetSenderFee(transaction.Sender).Should().Be(6);
            sendersFeeMonitor.RemoveSenderFee(transaction);
            sendersFeeMonitor.GetSenderFee(transaction.Sender).Should().Be(3);
            sendersFeeMonitor.RemoveSenderFee(transaction);
            sendersFeeMonitor.GetSenderFee(transaction.Sender).Should().Be(0);
        }
コード例 #3
0
 public void Deserialize(BinaryReader reader)
 {
     Reset(0);
     if (reader.ReadUInt32() != Block.Version)
     {
         throw new FormatException();
     }
     if (reader.ReadUInt32() != Block.Index)
     {
         throw new InvalidOperationException();
     }
     Block.Timestamp     = reader.ReadUInt64();
     Block.NextConsensus = reader.ReadSerializable <UInt160>();
     if (Block.NextConsensus.Equals(UInt160.Zero))
     {
         Block.NextConsensus = null;
     }
     Block.ConsensusData = reader.ReadSerializable <ConsensusData>();
     ViewNumber          = reader.ReadByte();
     TransactionHashes   = reader.ReadSerializableArray <UInt256>();
     Transaction[] transactions = reader.ReadSerializableArray <Transaction>(Block.MaxTransactionsPerBlock);
     PreparationPayloads    = reader.ReadNullableArray <ConsensusPayload>(ProtocolSettings.Default.MaxValidatorsCount);
     CommitPayloads         = reader.ReadNullableArray <ConsensusPayload>(ProtocolSettings.Default.MaxValidatorsCount);
     ChangeViewPayloads     = reader.ReadNullableArray <ConsensusPayload>(ProtocolSettings.Default.MaxValidatorsCount);
     LastChangeViewPayloads = reader.ReadNullableArray <ConsensusPayload>(ProtocolSettings.Default.MaxValidatorsCount);
     if (TransactionHashes.Length == 0 && !RequestSentOrReceived)
     {
         TransactionHashes = null;
     }
     Transactions      = transactions.Length == 0 && !RequestSentOrReceived ? null : transactions.ToDictionary(p => p.Hash);
     SendersFeeMonitor = new SendersFeeMonitor();
     if (Transactions != null)
     {
         foreach (Transaction tx in Transactions.Values)
         {
             SendersFeeMonitor.AddSenderFee(tx);
         }
     }
 }