Skip to content

Commit

Permalink
Remove Trimmed value
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon committed Jul 5, 2023
1 parent 6c9b0f5 commit 8f53e90
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 29 deletions.
7 changes: 3 additions & 4 deletions src/Neo/SmartContract/Native/LedgerContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Conflicts>())
{
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);
Expand Down Expand Up @@ -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<TransactionState>();
if (state is not null && state.Trimmed) return true;
if (state is not null && state.Transaction is null) return true;
return false;
}

Expand Down Expand Up @@ -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<TransactionState>();
if (state is not null && state.Trimmed) return null;
if (state?.Transaction is null) return null;
return state;
}

Expand Down
22 changes: 7 additions & 15 deletions src/Neo/SmartContract/Native/TransactionState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@ namespace Neo.SmartContract.Native
/// </summary>
public class TransactionState : IInteroperable
{
/// <summary>
/// The transaction is trimmed.
/// To indicate this state is a placeholder for a conflict transaction.
/// </summary>
public bool Trimmed;
/// <summary>
/// The block containing this transaction.
/// </summary>
public uint BlockIndex;

/// <summary>
/// The transaction.
/// The transaction, if the transaction is trimmed this value will be null
/// </summary>
public Transaction Transaction;

Expand All @@ -47,7 +42,6 @@ IInteroperable IInteroperable.Clone()
{
return new TransactionState
{
Trimmed = Trimmed,
BlockIndex = BlockIndex,
Transaction = Transaction,
State = State,
Expand All @@ -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;
Expand All @@ -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<Transaction>();
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 };
}
}
}
13 changes: 5 additions & 8 deletions tests/Neo.UnitTests/Ledger/UT_TransactionState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ public void Initialize()
} }
}
};
originTrimmed = new TransactionState()
{
Trimmed = true,
};
originTrimmed = new TransactionState();
}

[TestMethod]
Expand All @@ -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]
Expand All @@ -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();
}
}
}
3 changes: 1 addition & 2 deletions tests/Neo.UnitTests/Network/P2P/Payloads/UT_Conflicts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ 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()));

// Conflicting transaction is on-chain.
snapshot.Delete(key);
conflict = new TransactionState
{
Trimmed = false,
BlockIndex = 123,
Transaction = new Transaction(),
State = VMState.NONE
Expand Down

0 comments on commit 8f53e90

Please sign in to comment.