Skip to content

Commit

Permalink
Added better error handling to Native.Ledger (#3593)
Browse files Browse the repository at this point in the history
* Added better error handling to `Native.Ledger`

* revert some logic

---------

Co-authored-by: Shargon <[email protected]>
Co-authored-by: NGD Admin <[email protected]>
  • Loading branch information
3 people authored Nov 28, 2024
1 parent 1703774 commit d862ce8
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Neo/SmartContract/Native/LedgerContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ internal override ContractTask PostPersistAsync(ApplicationEngine engine)

internal bool Initialized(DataCache snapshot)
{
if (snapshot is null)
throw new ArgumentNullException(nameof(snapshot));

return snapshot.Find(CreateStorageKey(Prefix_Block).ToArray()).Any();
}

Expand All @@ -94,6 +97,9 @@ private bool IsTraceableBlock(DataCache snapshot, uint index, uint maxTraceableB
/// <returns>The hash of the block.</returns>
public UInt256 GetBlockHash(DataCache snapshot, uint index)
{
if (snapshot is null)
throw new ArgumentNullException(nameof(snapshot));

StorageItem item = snapshot.TryGet(CreateStorageKey(Prefix_BlockHash).AddBigEndian(index));
if (item is null) return null;
return new UInt256(item.Value.Span);
Expand All @@ -107,6 +113,9 @@ public UInt256 GetBlockHash(DataCache snapshot, uint index)
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
public UInt256 CurrentHash(DataCache snapshot)
{
if (snapshot is null)
throw new ArgumentNullException(nameof(snapshot));

return snapshot[CreateStorageKey(Prefix_CurrentBlock)].GetInteroperable<HashIndexState>().Hash;
}

Expand All @@ -118,6 +127,9 @@ public UInt256 CurrentHash(DataCache snapshot)
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
public uint CurrentIndex(DataCache snapshot)
{
if (snapshot is null)
throw new ArgumentNullException(nameof(snapshot));

return snapshot[CreateStorageKey(Prefix_CurrentBlock)].GetInteroperable<HashIndexState>().Index;
}

Expand All @@ -129,6 +141,9 @@ public uint CurrentIndex(DataCache snapshot)
/// <returns><see langword="true"/> if the blockchain contains the block; otherwise, <see langword="false"/>.</returns>
public bool ContainsBlock(DataCache snapshot, UInt256 hash)
{
if (snapshot is null)
throw new ArgumentNullException(nameof(snapshot));

return snapshot.Contains(CreateStorageKey(Prefix_Block).Add(hash));
}

Expand All @@ -155,6 +170,12 @@ public bool ContainsTransaction(DataCache snapshot, UInt256 hash)
/// <returns><see langword="true"/> if the blockchain contains the hash of the conflicting transaction; otherwise, <see langword="false"/>.</returns>
public bool ContainsConflictHash(DataCache snapshot, UInt256 hash, IEnumerable<UInt160> signers, uint maxTraceableBlocks)
{
if (snapshot is null)
throw new ArgumentNullException(nameof(snapshot));

if (signers is null)
throw new ArgumentNullException(nameof(signers));

// Check the dummy stub firstly to define whether there's exist at least one conflict record.
var stub = snapshot.TryGet(CreateStorageKey(Prefix_Transaction).Add(hash))?.GetInteroperable<TransactionState>();
if (stub is null || stub.Transaction is not null || !IsTraceableBlock(snapshot, stub.BlockIndex, maxTraceableBlocks))
Expand All @@ -179,6 +200,9 @@ public bool ContainsConflictHash(DataCache snapshot, UInt256 hash, IEnumerable<U
/// <returns>The trimmed block.</returns>
public TrimmedBlock GetTrimmedBlock(DataCache snapshot, UInt256 hash)
{
if (snapshot is null)
throw new ArgumentNullException(nameof(snapshot));

StorageItem item = snapshot.TryGet(CreateStorageKey(Prefix_Block).Add(hash));
if (item is null) return null;
return item.Value.AsSerializable<TrimmedBlock>();
Expand Down Expand Up @@ -262,6 +286,9 @@ public Header GetHeader(DataCache snapshot, uint index)
/// <returns>The <see cref="TransactionState"/> with the specified hash.</returns>
public TransactionState GetTransactionState(DataCache snapshot, UInt256 hash)
{
if (snapshot is null)
throw new ArgumentNullException(nameof(snapshot));

var state = snapshot.TryGet(CreateStorageKey(Prefix_Transaction).Add(hash))?.GetInteroperable<TransactionState>();
if (state?.Transaction is null) return null;
return state;
Expand Down

0 comments on commit d862ce8

Please sign in to comment.