Skip to content

Commit eeade94

Browse files
shargonvncoelhocschuchardt88
authored
Nullable for HashIndexState and TransactionState (#3801)
* Fix interop serialization * Clean * Ensure is valid * Clean * Fix new class * Clean HashIndexState * Update src/Neo/SmartContract/Native/HashIndexState.cs * TransactionState nullable * Clean * fix * Revert storageItem changes * optimize * Revert some changes --------- Co-authored-by: Vitor Nazário Coelho <vncoelho@gmail.com> Co-authored-by: Christopher Schuchardt <cschuchardt88@gmail.com>
1 parent dd62411 commit eeade94

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

src/Neo.CLI/CLI/MainService.Blockchain.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ public void OnShowTransactionCommand(UInt256 hash)
119119
{
120120
var tx = NativeContract.Ledger.GetTransactionState(NeoSystem.StoreView, hash);
121121

122-
if (tx is null)
122+
if (tx?.Transaction is null)
123123
{
124124
ConsoleHelper.Error($"Transaction {hash} doesn't exist.");
125125
return;
126126
}
127127

128128
var block = NativeContract.Ledger.GetHeader(NeoSystem.StoreView, tx.BlockIndex);
129129

130-
DateTime transactionDatetime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
130+
var transactionDatetime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
131131
transactionDatetime = transactionDatetime.AddMilliseconds(block.Timestamp).ToLocalTime();
132132

133133
ConsoleHelper.Info("", "-------------", "Transaction", "-------------");

src/Neo/SmartContract/Native/HashIndexState.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// Redistribution and use in source and binary forms with or without
1010
// modifications are permitted.
1111

12+
#nullable enable
13+
1214
using Neo.Extensions;
1315
using Neo.VM;
1416
using Neo.VM.Types;
@@ -17,12 +19,12 @@ namespace Neo.SmartContract.Native
1719
{
1820
class HashIndexState : IInteroperable
1921
{
20-
public UInt256 Hash;
21-
public uint Index;
22+
public UInt256 Hash { get; set; } = UInt256.Zero;
23+
public uint Index { get; set; }
2224

2325
void IInteroperable.FromStackItem(StackItem stackItem)
2426
{
25-
Struct @struct = (Struct)stackItem;
27+
var @struct = (Struct)stackItem;
2628
Hash = new UInt256(@struct[0].GetSpan());
2729
Index = (uint)@struct[1].GetInteger();
2830
}
@@ -33,3 +35,5 @@ StackItem IInteroperable.ToStackItem(IReferenceCounter referenceCounter)
3335
}
3436
}
3537
}
38+
39+
#nullable disable

src/Neo/SmartContract/Native/LedgerContract.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,19 @@ internal override ContractTask OnPersistAsync(ApplicationEngine engine)
5353
{
5454
// It's possible that there are previously saved malicious conflict records for this transaction.
5555
// If so, then remove it and store the relevant transaction itself.
56-
engine.SnapshotCache.GetAndChange(CreateStorageKey(Prefix_Transaction, tx.Transaction.Hash), () => new StorageItem(new TransactionState())).FromReplica(new StorageItem(tx));
56+
engine.SnapshotCache.GetAndChange(CreateStorageKey(Prefix_Transaction, tx.Transaction.Hash), () => new StorageItem(new TransactionState()))
57+
.FromReplica(new StorageItem(tx));
5758

5859
// Store transaction's conflicits.
5960
var conflictingSigners = tx.Transaction.Signers.Select(s => s.Account);
6061
foreach (var attr in tx.Transaction.GetAttributes<Conflicts>())
6162
{
62-
engine.SnapshotCache.GetAndChange(CreateStorageKey(Prefix_Transaction, attr.Hash), () => new StorageItem(new TransactionState())).FromReplica(new StorageItem(new TransactionState() { BlockIndex = engine.PersistingBlock.Index }));
63+
engine.SnapshotCache.GetAndChange(CreateStorageKey(Prefix_Transaction, attr.Hash), () => new StorageItem(new TransactionState()))
64+
.FromReplica(new StorageItem(new TransactionState() { BlockIndex = engine.PersistingBlock.Index }));
6365
foreach (var signer in conflictingSigners)
6466
{
65-
engine.SnapshotCache.GetAndChange(CreateStorageKey(Prefix_Transaction, attr.Hash, signer), () => new StorageItem(new TransactionState())).FromReplica(new StorageItem(new TransactionState() { BlockIndex = engine.PersistingBlock.Index }));
67+
engine.SnapshotCache.GetAndChange(CreateStorageKey(Prefix_Transaction, attr.Hash, signer), () => new StorageItem(new TransactionState()))
68+
.FromReplica(new StorageItem(new TransactionState() { BlockIndex = engine.PersistingBlock.Index }));
6669
}
6770
}
6871
}
@@ -73,7 +76,9 @@ internal override ContractTask OnPersistAsync(ApplicationEngine engine)
7376

7477
internal override ContractTask PostPersistAsync(ApplicationEngine engine)
7578
{
76-
HashIndexState state = engine.SnapshotCache.GetAndChange(_currentBlock, () => new StorageItem(new HashIndexState())).GetInteroperable<HashIndexState>();
79+
var state = engine.SnapshotCache.GetAndChange(_currentBlock, () => new StorageItem(new HashIndexState()))
80+
// Don't need to seal because the size is fixed and it can't grow
81+
.GetInteroperable<HashIndexState>();
7782
state.Hash = engine.PersistingBlock.Hash;
7883
state.Index = engine.PersistingBlock.Index;
7984
return ContractTask.CompletedTask;

src/Neo/SmartContract/Native/TransactionState.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// Redistribution and use in source and binary forms with or without
1010
// modifications are permitted.
1111

12+
#nullable enable
13+
1214
using Neo.Extensions;
1315
using Neo.Network.P2P.Payloads;
1416
using Neo.VM;
@@ -25,17 +27,17 @@ public class TransactionState : IInteroperable
2527
/// <summary>
2628
/// The block containing this transaction.
2729
/// </summary>
28-
public uint BlockIndex;
30+
public uint BlockIndex { get; set; }
2931

3032
/// <summary>
3133
/// The transaction, if the transaction is trimmed this value will be null
3234
/// </summary>
33-
public Transaction Transaction;
35+
public Transaction? Transaction { get; set; }
3436

3537
/// <summary>
3638
/// The execution state
3739
/// </summary>
38-
public VMState State;
40+
public VMState State { get; set; }
3941

4042
private ReadOnlyMemory<byte> _rawTransaction;
4143

@@ -52,7 +54,7 @@ IInteroperable IInteroperable.Clone()
5254

5355
void IInteroperable.FromReplica(IInteroperable replica)
5456
{
55-
TransactionState from = (TransactionState)replica;
57+
var from = (TransactionState)replica;
5658
BlockIndex = from.BlockIndex;
5759
Transaction = from.Transaction;
5860
State = from.State;
@@ -62,7 +64,7 @@ void IInteroperable.FromReplica(IInteroperable replica)
6264

6365
void IInteroperable.FromStackItem(StackItem stackItem)
6466
{
65-
Struct @struct = (Struct)stackItem;
67+
var @struct = (Struct)stackItem;
6668
BlockIndex = (uint)@struct[0].GetInteger();
6769

6870
// Conflict record.
@@ -84,3 +86,5 @@ StackItem IInteroperable.ToStackItem(IReferenceCounter referenceCounter)
8486
}
8587
}
8688
}
89+
90+
#nullable disable

0 commit comments

Comments
 (0)