Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improve]: use IReadOnlyStoreView instead. #3683

Merged
merged 17 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ protected override void PostExecuteInstruction(Instruction instruction)
Diagnostic?.PostExecuteInstruction(instruction);
}

private static Block CreateDummyBlock(DataCache snapshot, ProtocolSettings settings)
private static Block CreateDummyBlock(IReadOnlyStoreView snapshot, ProtocolSettings settings)
{
UInt256 hash = NativeContract.Ledger.CurrentHash(snapshot);
Block currentBlock = NativeContract.Ledger.GetBlock(snapshot, hash);
Expand Down
16 changes: 8 additions & 8 deletions src/Neo/SmartContract/Native/ContractManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ private void SetMinimumDeploymentFee(ApplicationEngine engine, BigInteger value/
/// <param name="hash">The hash of the deployed contract.</param>
/// <returns>The deployed contract.</returns>
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
public ContractState GetContract(DataCache snapshot, UInt160 hash)
public ContractState GetContract(IReadOnlyStoreView snapshot, UInt160 hash)
{
return snapshot.TryGet(CreateStorageKey(Prefix_Contract).Add(hash))?.GetInteroperable<ContractState>(false);
var key = CreateStorageKey(Prefix_Contract).Add(hash);
return snapshot.TryGet(key, out var item) ? item.GetInteroperable<ContractState>(false) : null;
}

/// <summary>
Expand All @@ -149,12 +150,11 @@ public ContractState GetContract(DataCache snapshot, UInt160 hash)
/// <param name="id">Contract ID.</param>
/// <returns>The deployed contract.</returns>
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
public ContractState GetContractById(DataCache snapshot, int id)
public ContractState GetContractById(IReadOnlyStoreView snapshot, int id)
{
StorageItem item = snapshot.TryGet(CreateStorageKey(Prefix_ContractHash).AddBigEndian(id));
if (item is null) return null;
var hash = new UInt160(item.Value.Span);
return GetContract(snapshot, hash);
if (snapshot.TryGet(CreateStorageKey(Prefix_ContractHash).AddBigEndian(id), out var item))
shargon marked this conversation as resolved.
Show resolved Hide resolved
return GetContract(snapshot, new UInt160(item.Value.Span));
return null;
}

/// <summary>
Expand Down Expand Up @@ -184,7 +184,7 @@ private IIterator GetContractHashes(DataCache snapshot)
/// <param name="pcount">The number of parameters</param>
/// <returns>True if the method exists.</returns>
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
public bool HasMethod(DataCache snapshot, UInt160 hash, string method, int pcount)
public bool HasMethod(IReadOnlyStoreView snapshot, UInt160 hash, string method, int pcount)
{
var contract = GetContract(snapshot, hash);
if (contract is null) return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/SmartContract/Native/ContractMethodMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ContractMethodMetadata(MemberInfo member, ContractMethodAttribute attribu
if (parameterInfos.Length > 0)
{
NeedApplicationEngine = parameterInfos[0].ParameterType.IsAssignableFrom(typeof(ApplicationEngine));
NeedSnapshot = parameterInfos[0].ParameterType.IsAssignableFrom(typeof(DataCache));
NeedSnapshot = parameterInfos[0].ParameterType.IsAssignableFrom(typeof(IReadOnlyStoreView));
}
if (NeedApplicationEngine || NeedSnapshot)
Parameters = parameterInfos.Skip(1).Select(p => new InteropParameterDescriptor(p)).ToArray();
Expand Down
68 changes: 40 additions & 28 deletions src/Neo/SmartContract/Native/LedgerContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ internal override ContractTask OnPersistAsync(ApplicationEngine engine)
}
}
}

engine.SetState(transactions);
return ContractTask.CompletedTask;
}
Expand All @@ -87,7 +88,7 @@ internal bool Initialized(DataCache snapshot)
return snapshot.Find(CreateStorageKey(Prefix_Block).ToArray()).Any();
}

private bool IsTraceableBlock(DataCache snapshot, uint index, uint maxTraceableBlocks)
private bool IsTraceableBlock(IReadOnlyStoreView snapshot, uint index, uint maxTraceableBlocks)
{
uint currentIndex = CurrentIndex(snapshot);
if (index > currentIndex) return false;
Expand All @@ -100,14 +101,15 @@ private bool IsTraceableBlock(DataCache snapshot, uint index, uint maxTraceableB
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="index">The index of the block.</param>
/// <returns>The hash of the block.</returns>
public UInt256 GetBlockHash(DataCache snapshot, uint index)
public UInt256 GetBlockHash(IReadOnlyStoreView 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);
var key = CreateStorageKey(Prefix_BlockHash).AddBigEndian(index);
if (snapshot.TryGet(key, out var item))
return new UInt256(item.Value.Span);
return null;
}

/// <summary>
Expand All @@ -116,7 +118,7 @@ public UInt256 GetBlockHash(DataCache snapshot, uint index)
/// <param name="snapshot">The snapshot used to read data.</param>
/// <returns>The hash of the current block.</returns>
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
public UInt256 CurrentHash(DataCache snapshot)
public UInt256 CurrentHash(IReadOnlyStoreView snapshot)
{
if (snapshot is null)
throw new ArgumentNullException(nameof(snapshot));
Expand All @@ -130,7 +132,7 @@ public UInt256 CurrentHash(DataCache snapshot)
/// <param name="snapshot">The snapshot used to read data.</param>
/// <returns>The index of the current block.</returns>
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
public uint CurrentIndex(DataCache snapshot)
public uint CurrentIndex(IReadOnlyStoreView snapshot)
{
if (snapshot is null)
throw new ArgumentNullException(nameof(snapshot));
Expand All @@ -143,8 +145,10 @@ public uint CurrentIndex(DataCache snapshot)
/// </summary>
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="hash">The hash of the block.</param>
/// <returns><see langword="true"/> if the blockchain contains the block; otherwise, <see langword="false"/>.</returns>
public bool ContainsBlock(DataCache snapshot, UInt256 hash)
/// <returns>
/// <see langword="true"/> if the blockchain contains the block; otherwise, <see langword="false"/>.
/// </returns>
public bool ContainsBlock(IReadOnlyStoreView snapshot, UInt256 hash)
{
if (snapshot is null)
throw new ArgumentNullException(nameof(snapshot));
Expand All @@ -157,8 +161,10 @@ public bool ContainsBlock(DataCache snapshot, UInt256 hash)
/// </summary>
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="hash">The hash of the transaction.</param>
/// <returns><see langword="true"/> if the blockchain contains the transaction; otherwise, <see langword="false"/>.</returns>
public bool ContainsTransaction(DataCache snapshot, UInt256 hash)
/// <returns>
/// <see langword="true"/> if the blockchain contains the transaction; otherwise, <see langword="false"/>.
/// </returns>
public bool ContainsTransaction(IReadOnlyStoreView snapshot, UInt256 hash)
{
var txState = GetTransactionState(snapshot, hash);
return txState != null;
Expand All @@ -172,8 +178,11 @@ public bool ContainsTransaction(DataCache snapshot, UInt256 hash)
/// <param name="hash">The hash of the conflicting transaction.</param>
/// <param name="signers">The list of signer accounts of the conflicting transaction.</param>
/// <param name="maxTraceableBlocks">MaxTraceableBlocks protocol setting.</param>
/// <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)
/// <returns>
/// <see langword="true"/> if the blockchain contains the hash of the conflicting transaction;
/// otherwise, <see langword="false"/>.
/// </returns>
public bool ContainsConflictHash(IReadOnlyStoreView snapshot, UInt256 hash, IEnumerable<UInt160> signers, uint maxTraceableBlocks)
{
if (snapshot is null)
throw new ArgumentNullException(nameof(snapshot));
Expand All @@ -182,14 +191,16 @@ public bool ContainsConflictHash(DataCache snapshot, UInt256 hash, IEnumerable<U
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>();
var key = CreateStorageKey(Prefix_Transaction).Add(hash);
var stub = snapshot.TryGet(key, out var item) ? item.GetInteroperable<TransactionState>() : null;
if (stub is null || stub.Transaction is not null || !IsTraceableBlock(snapshot, stub.BlockIndex, maxTraceableBlocks))
return false;

// At least one conflict record is found, then need to check signers intersection.
foreach (var signer in signers)
{
var state = snapshot.TryGet(CreateStorageKey(Prefix_Transaction).Add(hash).Add(signer))?.GetInteroperable<TransactionState>();
key = CreateStorageKey(Prefix_Transaction).Add(hash).Add(signer);
var state = snapshot.TryGet(key, out var tx) ? tx.GetInteroperable<TransactionState>() : null;
if (state is not null && IsTraceableBlock(snapshot, state.BlockIndex, maxTraceableBlocks))
return true;
}
Expand All @@ -203,14 +214,15 @@ public bool ContainsConflictHash(DataCache snapshot, UInt256 hash, IEnumerable<U
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="hash">The hash of the block.</param>
/// <returns>The trimmed block.</returns>
public TrimmedBlock GetTrimmedBlock(DataCache snapshot, UInt256 hash)
public TrimmedBlock GetTrimmedBlock(IReadOnlyStoreView 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>();
var key = CreateStorageKey(Prefix_Block).Add(hash);
if (snapshot.TryGet(key, out var item))
return item.Value.AsSerializable<TrimmedBlock>();
return null;
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
Expand All @@ -235,7 +247,7 @@ private TrimmedBlock GetBlock(ApplicationEngine engine, byte[] indexOrHash)
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="hash">The hash of the block.</param>
/// <returns>The block with the specified hash.</returns>
public Block GetBlock(DataCache snapshot, UInt256 hash)
public Block GetBlock(IReadOnlyStoreView snapshot, UInt256 hash)
{
TrimmedBlock state = GetTrimmedBlock(snapshot, hash);
if (state is null) return null;
Expand All @@ -252,7 +264,7 @@ public Block GetBlock(DataCache snapshot, UInt256 hash)
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="index">The index of the block.</param>
/// <returns>The block with the specified index.</returns>
public Block GetBlock(DataCache snapshot, uint index)
public Block GetBlock(IReadOnlyStoreView snapshot, uint index)
{
UInt256 hash = GetBlockHash(snapshot, index);
if (hash is null) return null;
Expand All @@ -265,7 +277,7 @@ public Block GetBlock(DataCache snapshot, uint index)
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="hash">The hash of the block.</param>
/// <returns>The block header with the specified hash.</returns>
public Header GetHeader(DataCache snapshot, UInt256 hash)
public Header GetHeader(IReadOnlyStoreView snapshot, UInt256 hash)
{
return GetTrimmedBlock(snapshot, hash)?.Header;
}
Expand All @@ -276,7 +288,7 @@ public Header GetHeader(DataCache snapshot, UInt256 hash)
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="index">The index of the block.</param>
/// <returns>The block header with the specified index.</returns>
public Header GetHeader(DataCache snapshot, uint index)
public Header GetHeader(IReadOnlyStoreView snapshot, uint index)
{
UInt256 hash = GetBlockHash(snapshot, index);
if (hash is null) return null;
Expand All @@ -289,14 +301,14 @@ public Header GetHeader(DataCache snapshot, uint index)
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="hash">The hash of the transaction.</param>
/// <returns>The <see cref="TransactionState"/> with the specified hash.</returns>
public TransactionState GetTransactionState(DataCache snapshot, UInt256 hash)
public TransactionState GetTransactionState(IReadOnlyStoreView 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;
var key = CreateStorageKey(Prefix_Transaction).Add(hash);
var state = snapshot.TryGet(key, out var item) ? item.GetInteroperable<TransactionState>() : null;
return state?.Transaction is null ? null : state;
}

/// <summary>
Expand All @@ -305,7 +317,7 @@ public TransactionState GetTransactionState(DataCache snapshot, UInt256 hash)
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="hash">The hash of the transaction.</param>
/// <returns>The transaction with the specified hash.</returns>
public Transaction GetTransaction(DataCache snapshot, UInt256 hash)
public Transaction GetTransaction(IReadOnlyStoreView snapshot, UInt256 hash)
{
return GetTransactionState(snapshot, hash)?.Transaction;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Plugins/StorageDumper/StorageDumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ private void OnPersistStorage(uint network, DataCache snapshot)

void ICommittedHandler.Blockchain_Committed_Handler(NeoSystem system, Block block)
{
OnCommitStorage(system.Settings.Network, system.StoreView);
OnCommitStorage(system.Settings.Network);
}

void OnCommitStorage(uint network, DataCache snapshot)
void OnCommitStorage(uint network)
{
if (_currentBlock != null && _writer != null)
{
Expand All @@ -147,7 +147,7 @@ void OnCommitStorage(uint network, DataCache snapshot)
}
}

private void InitFileWriter(uint network, DataCache snapshot)
private void InitFileWriter(uint network, IReadOnlyStoreView snapshot)
{
uint blockIndex = NativeContract.Ledger.CurrentIndex(snapshot);
if (_writer == null
Expand Down
Loading