From 8f53e90f708069af4a6ca887b36d1efcd4eff0c1 Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Wed, 5 Jul 2023 20:10:37 +0200 Subject: [PATCH] Remove Trimmed value --- .../SmartContract/Native/LedgerContract.cs | 7 +++--- .../SmartContract/Native/TransactionState.cs | 22 ++++++------------- .../Ledger/UT_TransactionState.cs | 13 +++++------ .../Network/P2P/Payloads/UT_Conflicts.cs | 3 +-- 4 files changed, 16 insertions(+), 29 deletions(-) diff --git a/src/Neo/SmartContract/Native/LedgerContract.cs b/src/Neo/SmartContract/Native/LedgerContract.cs index 9dbb67aa74..648739f77b 100644 --- a/src/Neo/SmartContract/Native/LedgerContract.cs +++ b/src/Neo/SmartContract/Native/LedgerContract.cs @@ -38,7 +38,6 @@ internal override ContractTask OnPersist(ApplicationEngine engine) { TransactionState[] transactions = engine.PersistingBlock.Transactions.Select(p => new TransactionState { - Trimmed = false, BlockIndex = engine.PersistingBlock.Index, Transaction = p, State = VMState.NONE @@ -50,7 +49,7 @@ internal override ContractTask OnPersist(ApplicationEngine engine) engine.Snapshot.Add(CreateStorageKey(Prefix_Transaction).Add(tx.Transaction.Hash), new StorageItem(tx)); foreach (var attr in tx.Transaction.GetAttributes()) { - engine.Snapshot.GetOrAdd(CreateStorageKey(Prefix_Transaction).Add(attr.Hash), () => new StorageItem(new TransactionState { Trimmed = true })); + engine.Snapshot.GetOrAdd(CreateStorageKey(Prefix_Transaction).Add(attr.Hash), () => new StorageItem(new TransactionState())); } } engine.SetState(transactions); @@ -145,7 +144,7 @@ public bool ContainsTransaction(DataCache snapshot, UInt256 hash) public bool ContainsConflictHash(DataCache snapshot, UInt256 hash) { var state = snapshot.TryGet(CreateStorageKey(Prefix_Transaction).Add(hash))?.GetInteroperable(); - if (state is not null && state.Trimmed) return true; + if (state is not null && state.Transaction is null) return true; return false; } @@ -241,7 +240,7 @@ public Header GetHeader(DataCache snapshot, uint index) public TransactionState GetTransactionState(DataCache snapshot, UInt256 hash) { var state = snapshot.TryGet(CreateStorageKey(Prefix_Transaction).Add(hash))?.GetInteroperable(); - if (state is not null && state.Trimmed) return null; + if (state?.Transaction is null) return null; return state; } diff --git a/src/Neo/SmartContract/Native/TransactionState.cs b/src/Neo/SmartContract/Native/TransactionState.cs index 538b474a7e..c0f5f8334d 100644 --- a/src/Neo/SmartContract/Native/TransactionState.cs +++ b/src/Neo/SmartContract/Native/TransactionState.cs @@ -21,18 +21,13 @@ namespace Neo.SmartContract.Native /// public class TransactionState : IInteroperable { - /// - /// The transaction is trimmed. - /// To indicate this state is a placeholder for a conflict transaction. - /// - public bool Trimmed; /// /// The block containing this transaction. /// public uint BlockIndex; /// - /// The transaction. + /// The transaction, if the transaction is trimmed this value will be null /// public Transaction Transaction; @@ -47,7 +42,6 @@ IInteroperable IInteroperable.Clone() { return new TransactionState { - Trimmed = Trimmed, BlockIndex = BlockIndex, Transaction = Transaction, State = State, @@ -58,7 +52,6 @@ IInteroperable IInteroperable.Clone() void IInteroperable.FromReplica(IInteroperable replica) { TransactionState from = (TransactionState)replica; - Trimmed = from.Trimmed; BlockIndex = from.BlockIndex; Transaction = from.Transaction; State = from.State; @@ -69,20 +62,19 @@ void IInteroperable.FromReplica(IInteroperable replica) void IInteroperable.FromStackItem(StackItem stackItem) { Struct @struct = (Struct)stackItem; - Trimmed = @struct[0].GetBoolean(); - if (Trimmed) return; - BlockIndex = (uint)@struct[1].GetInteger(); - _rawTransaction = ((ByteString)@struct[2]).Memory; + if (@struct.Count == 0) return; + BlockIndex = (uint)@struct[0].GetInteger(); + _rawTransaction = ((ByteString)@struct[1]).Memory; Transaction = _rawTransaction.AsSerializable(); - State = (VMState)(byte)@struct[3].GetInteger(); + State = (VMState)(byte)@struct[2].GetInteger(); } StackItem IInteroperable.ToStackItem(ReferenceCounter referenceCounter) { - if (Trimmed) return new Struct(referenceCounter) { Trimmed }; + if (Transaction is null) return new Struct(referenceCounter); if (_rawTransaction.IsEmpty) _rawTransaction = Transaction.ToArray(); - return new Struct(referenceCounter) { Trimmed, BlockIndex, _rawTransaction, (byte)State }; + return new Struct(referenceCounter) { BlockIndex, _rawTransaction, (byte)State }; } } } diff --git a/tests/Neo.UnitTests/Ledger/UT_TransactionState.cs b/tests/Neo.UnitTests/Ledger/UT_TransactionState.cs index 1e3f765dd7..fc56d7a24d 100644 --- a/tests/Neo.UnitTests/Ledger/UT_TransactionState.cs +++ b/tests/Neo.UnitTests/Ledger/UT_TransactionState.cs @@ -33,10 +33,7 @@ public void Initialize() } } } }; - originTrimmed = new TransactionState() - { - Trimmed = true, - }; + originTrimmed = new TransactionState(); } [TestMethod] @@ -45,12 +42,12 @@ public void TestDeserialize() var data = BinarySerializer.Serialize(((IInteroperable)origin).ToStackItem(null), 1024); var reader = new MemoryReader(data); - TransactionState dest = new TransactionState(); + TransactionState dest = new(); ((IInteroperable)dest).FromStackItem(BinarySerializer.Deserialize(ref reader, ExecutionEngineLimits.Default, null)); dest.BlockIndex.Should().Be(origin.BlockIndex); dest.Transaction.Hash.Should().Be(origin.Transaction.Hash); - dest.Trimmed.Should().Be(false); + dest.Transaction.Should().NotBeNull(); } [TestMethod] @@ -59,12 +56,12 @@ public void TestDeserializeTrimmed() var data = BinarySerializer.Serialize(((IInteroperable)originTrimmed).ToStackItem(null), 1024); var reader = new MemoryReader(data); - TransactionState dest = new TransactionState(); + TransactionState dest = new(); ((IInteroperable)dest).FromStackItem(BinarySerializer.Deserialize(ref reader, ExecutionEngineLimits.Default, null)); dest.BlockIndex.Should().Be(0); dest.Transaction.Should().Be(null); - dest.Trimmed.Should().Be(true); + dest.Transaction.Should().BeNull(); } } } diff --git a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Conflicts.cs b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Conflicts.cs index c955995bb1..bbf46920aa 100644 --- a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Conflicts.cs +++ b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Conflicts.cs @@ -66,7 +66,7 @@ public void Verify() var key = Ledger.UT_MemoryPool.CreateStorageKey(NativeContract.Ledger.Id, Prefix_Transaction, _u.ToArray()); // Conflicting transaction is in the Conflicts attribute of some other on-chain transaction. - var conflict = new TransactionState { Trimmed = true }; + var conflict = new TransactionState(); snapshot.Add(key, new StorageItem(conflict)); Assert.IsTrue(test.Verify(snapshot, new Transaction())); @@ -74,7 +74,6 @@ public void Verify() snapshot.Delete(key); conflict = new TransactionState { - Trimmed = false, BlockIndex = 123, Transaction = new Transaction(), State = VMState.NONE