public void TestDuplicateOracle() { // Fake balance var snapshot = Blockchain.Singleton.GetSnapshot(); ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, null, long.MaxValue); BigInteger balance = NativeContract.GAS.BalanceOf(snapshot, UInt160.Zero); NativeContract.GAS.Burn(engine, UInt160.Zero, balance); NativeContract.GAS.Mint(engine, UInt160.Zero, 8, false); // Test TransactionVerificationContext verificationContext = new TransactionVerificationContext(); var tx = CreateTransactionWithFee(1, 2); tx.Attributes = new TransactionAttribute[] { new OracleResponse() { Code = OracleResponseCode.ConsensusUnreachable, Id = 1, Result = new byte[0] } }; verificationContext.CheckTransaction(tx, snapshot).Should().BeTrue(); verificationContext.AddTransaction(tx); tx = CreateTransactionWithFee(2, 1); tx.Attributes = new TransactionAttribute[] { new OracleResponse() { Code = OracleResponseCode.ConsensusUnreachable, Id = 1, Result = new byte[0] } }; verificationContext.CheckTransaction(tx, snapshot).Should().BeFalse(); }
public void TestTransactionSenderFee() { var snapshot = Blockchain.Singleton.GetSnapshot(); ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, null, long.MaxValue); BigInteger balance = NativeContract.GAS.BalanceOf(snapshot, UInt160.Zero); NativeContract.GAS.Burn(engine, UInt160.Zero, balance); NativeContract.GAS.Mint(engine, UInt160.Zero, 8, true); TransactionVerificationContext verificationContext = new TransactionVerificationContext(); var tx = CreateTransactionWithFee(1, 2); verificationContext.CheckTransaction(tx, snapshot).Should().BeTrue(); verificationContext.AddTransaction(tx); verificationContext.CheckTransaction(tx, snapshot).Should().BeTrue(); verificationContext.AddTransaction(tx); verificationContext.CheckTransaction(tx, snapshot).Should().BeFalse(); verificationContext.RemoveTransaction(tx); verificationContext.CheckTransaction(tx, snapshot).Should().BeTrue(); verificationContext.AddTransaction(tx); verificationContext.CheckTransaction(tx, snapshot).Should().BeFalse(); }
/// <summary> /// Prevent that block exceed the max size /// </summary> /// <param name="txs">Ordered transactions</param> internal void EnsureMaxBlockLimitation(IEnumerable <Transaction> txs) { uint maxBlockSize = NativeContract.Policy.GetMaxBlockSize(Snapshot); long maxBlockSystemFee = NativeContract.Policy.GetMaxBlockSystemFee(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>(); VerificationContext = new TransactionVerificationContext(); // Expected block size var blockSize = GetExpectedBlockSizeWithoutTransactions(txs.Count()); var blockSystemFee = 0L; // Iterate transaction until reach the size or maximum system fee 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; } // Check if maximum block system fee has been already exceeded with the current selected set blockSystemFee += tx.SystemFee; if (blockSystemFee > maxBlockSystemFee) { break; } hashes.Add(tx.Hash); Transactions.Add(tx.Hash, tx); VerificationContext.AddTransaction(tx); } TransactionHashes = hashes.ToArray(); }
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 <ExtensiblePayload>(ProtocolSettings.Default.ValidatorsCount); CommitPayloads = reader.ReadNullableArray <ExtensiblePayload>(ProtocolSettings.Default.ValidatorsCount); ChangeViewPayloads = reader.ReadNullableArray <ExtensiblePayload>(ProtocolSettings.Default.ValidatorsCount); LastChangeViewPayloads = reader.ReadNullableArray <ExtensiblePayload>(ProtocolSettings.Default.ValidatorsCount); if (TransactionHashes.Length == 0 && !RequestSentOrReceived) { TransactionHashes = null; } Transactions = transactions.Length == 0 && !RequestSentOrReceived ? null : transactions.ToDictionary(p => p.Hash); VerificationContext = new TransactionVerificationContext(); if (Transactions != null) { foreach (Transaction tx in Transactions.Values) { VerificationContext.AddTransaction(tx); } } }