Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions src/Neo.CLI/CLI/MainService.Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ private void OnExportBlocksStartCountCommand(uint start, uint count = uint.MaxVa
/// Reads blocks from a stream and yields blocks that are not yet in the blockchain.
/// </summary>
/// <param name="stream">The stream to read blocks from.</param>
/// <param name="read_start">If true, reads the start block index from the stream.</param>
/// <param name="readStart">If true, reads the start block index from the stream.</param>
/// <returns>An enumerable of blocks that are not yet in the blockchain.</returns>
private IEnumerable<Block> GetBlocks(Stream stream, bool read_start = false)
private IEnumerable<Block> GetBlocks(Stream stream, bool readStart = false)
{
using BinaryReader r = new BinaryReader(stream);
uint start = read_start ? r.ReadUInt32() : 0;
uint start = readStart ? r.ReadUInt32() : 0;
uint count = r.ReadUInt32();
uint end = start + count - 1;
uint currentHeight = NativeContract.Ledger.CurrentIndex(NeoSystem.StoreView);
Expand Down
14 changes: 7 additions & 7 deletions src/Neo.VM/JumpTable/JumpTable.Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,21 +534,21 @@ public virtual void EndFinally(ExecutionEngine engine, Instruction instruction)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void Ret(ExecutionEngine engine, Instruction instruction)
{
var context_pop = engine.InvocationStack.Pop();
var stack_eval = engine.InvocationStack.Count == 0 ? engine.ResultStack : engine.InvocationStack.Peek().EvaluationStack;
if (context_pop.EvaluationStack != stack_eval)
var contextPop = engine.InvocationStack.Pop();
var stackEval = engine.InvocationStack.Count == 0 ? engine.ResultStack : engine.InvocationStack.Peek().EvaluationStack;
if (contextPop.EvaluationStack != stackEval)
{
if (context_pop.RVCount >= 0 && context_pop.EvaluationStack.Count != context_pop.RVCount)
if (contextPop.RVCount >= 0 && contextPop.EvaluationStack.Count != contextPop.RVCount)
// This exception indicates a mismatch between the expected and actual number of stack items.
// It typically occurs due to compilation errors caused by potential issues in the compiler, resulting in either too many or too few
// items left on the stack compared to what was anticipated by the return value count.
// When you run into this problem, try to reach core-devs at https://github.com/neo-project/neo for help.
throw new InvalidOperationException($"Return value count mismatch: expected {context_pop.RVCount}, but got {context_pop.EvaluationStack.Count} items on the evaluation stack");
context_pop.EvaluationStack.CopyTo(stack_eval);
throw new InvalidOperationException($"Return value count mismatch: expected {contextPop.RVCount}, but got {contextPop.EvaluationStack.Count} items on the evaluation stack");
contextPop.EvaluationStack.CopyTo(stackEval);
}
if (engine.InvocationStack.Count == 0)
engine.State = VMState.HALT;
engine.UnloadContext(context_pop);
engine.UnloadContext(contextPop);
engine.isJumping = true;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Neo.VM/ReferenceCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,21 @@ public int CheckZeroReferred()
for (var node = _cachedComponents.First; node != null;)
{
var component = node.Value;
bool on_stack = false;
bool onStack = false;

// Check if any item in the SCC is still on the stack.
foreach (StackItem item in component)
{
// An item is considered 'on stack' if it has stack references or if its parent items are still on stack.
if (item.StackReferences > 0 || item.ObjectReferences?.Values.Any(p => p.References > 0 && p.Item.OnStack) == true)
{
on_stack = true;
onStack = true;
break;
}
}

// If any item in the component is on stack, mark all items in the component as on stack.
if (on_stack)
if (onStack)
{
foreach (StackItem item in component)
item.OnStack = true;
Expand Down
4 changes: 2 additions & 2 deletions src/Neo.VM/Types/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public StackItem this[PrimitiveType key]
if (IsReadOnly) throw new InvalidOperationException("The map is readonly, can not set value.");
if (ReferenceCounter != null)
{
if (_dict.TryGetValue(key, out StackItem? old_value))
ReferenceCounter.RemoveReference(old_value, this);
if (_dict.TryGetValue(key, out StackItem? oldValue))
ReferenceCounter.RemoveReference(oldValue, this);
else
ReferenceCounter.AddReference(key, this);
if (value is CompoundType { ReferenceCounter: null })
Expand Down
4 changes: 2 additions & 2 deletions src/Neo/BigDecimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ public static bool TryParse(string s, byte decimals, out BigDecimal result)
var index = s.IndexOfAny(['e', 'E']);
if (index >= 0)
{
if (!sbyte.TryParse(s[(index + 1)..], out var e_temp))
if (!sbyte.TryParse(s[(index + 1)..], out var eTemp))
{
result = default;
return false;
}
e = e_temp;
e = eTemp;
s = s[..index];
}
index = s.IndexOf('.');
Expand Down
40 changes: 24 additions & 16 deletions src/Neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,15 @@ private VerifyResult OnNewBlock(Block block)
_blockCache.TryAdd(blockHash, block);
if (block.Index == currentHeight + 1)
{
var block_persist = block;
var blockPersist = block;
var blocksToPersistList = new List<Block>();
while (true)
{
blocksToPersistList.Add(block_persist);
if (block_persist.Index + 1 > headerHeight) break;
var header = _system.HeaderCache[block_persist.Index + 1];
blocksToPersistList.Add(blockPersist);
if (blockPersist.Index + 1 > headerHeight) break;
var header = _system.HeaderCache[blockPersist.Index + 1];
if (header == null) break;
if (!_blockCache.TryGetValue(header.Hash, out block_persist)) break;
if (!_blockCache.TryGetValue(header.Hash, out blockPersist)) break;
}

var blocksPersisted = 0;
Expand Down Expand Up @@ -443,7 +443,7 @@ private void Persist(Block block)
{
using (var snapshot = _system.GetSnapshotCache())
{
var all_application_executed = new List<ApplicationExecuted>();
var allApplicationExecuted = new List<ApplicationExecuted>();
TransactionState[] transactionStates;
using (var engine = ApplicationEngine.Create(TriggerType.OnPersist, null, snapshot, block, _system.Settings, 0))
{
Expand All @@ -454,11 +454,14 @@ private void Persist(Block block)
throw engine.FaultException;
throw new InvalidOperationException();
}
ApplicationExecuted application_executed = new(engine);
Context.System.EventStream.Publish(application_executed);
all_application_executed.Add(application_executed);

var applicationExecuted = new ApplicationExecuted(engine);
Context.System.EventStream.Publish(applicationExecuted);

allApplicationExecuted.Add(applicationExecuted);
transactionStates = engine.GetState<TransactionState[]>();
}

var clonedSnapshot = snapshot.CloneCache();
// Warning: Do not write into variable snapshot directly. Write into variable clonedSnapshot and commit instead.
foreach (var transactionState in transactionStates)
Expand All @@ -475,10 +478,12 @@ private void Persist(Block block)
{
clonedSnapshot = snapshot.CloneCache();
}
ApplicationExecuted application_executed = new(engine);
Context.System.EventStream.Publish(application_executed);
all_application_executed.Add(application_executed);

var applicationExecuted = new ApplicationExecuted(engine);
Context.System.EventStream.Publish(applicationExecuted);
allApplicationExecuted.Add(applicationExecuted);
}

using (var engine = ApplicationEngine.Create(TriggerType.PostPersist, null, snapshot, block, _system.Settings, 0))
{
engine.LoadScript(s_postPersistScript);
Expand All @@ -488,13 +493,16 @@ private void Persist(Block block)
throw engine.FaultException;
throw new InvalidOperationException();
}
ApplicationExecuted application_executed = new(engine);
Context.System.EventStream.Publish(application_executed);
all_application_executed.Add(application_executed);

var applicationExecuted = new ApplicationExecuted(engine);
Context.System.EventStream.Publish(applicationExecuted);
allApplicationExecuted.Add(applicationExecuted);
}
InvokeCommitting(_system, block, snapshot, all_application_executed);

InvokeCommitting(_system, block, snapshot, allApplicationExecuted);
snapshot.Commit();
}

InvokeCommitted(_system, block);
_system.MemPool.UpdatePoolForBlockPersisted(block, _system.StoreView);
_extensibleWitnessWhiteList = null;
Expand Down
46 changes: 25 additions & 21 deletions src/Neo/Persistence/DataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,55 +256,57 @@ public void Delete(StorageKey key)
}

/// <inheritdoc/>
public IEnumerable<(StorageKey Key, StorageItem Value)> Find(StorageKey? key_prefix = null, SeekDirection direction = SeekDirection.Forward)
public IEnumerable<(StorageKey Key, StorageItem Value)> Find(StorageKey? keyPrefix = null, SeekDirection direction = SeekDirection.Forward)
{
var key = key_prefix?.ToArray();
var key = keyPrefix?.ToArray();
return Find(key, direction);
}

/// <summary>
/// Finds the entries starting with the specified prefix.
/// </summary>
/// <param name="key_prefix">The prefix of the key.</param>
/// <param name="keyPrefix">The prefix of the key.</param>
/// <param name="direction">The search direction.</param>
/// <returns>The entries found with the desired prefix.</returns>
public IEnumerable<(StorageKey Key, StorageItem Value)> Find(byte[]? key_prefix = null, SeekDirection direction = SeekDirection.Forward)
public IEnumerable<(StorageKey Key, StorageItem Value)> Find(byte[]? keyPrefix = null, SeekDirection direction = SeekDirection.Forward)
{
var seek_prefix = key_prefix;
var seekPrefix = keyPrefix;
if (direction == SeekDirection.Backward)
{
ArgumentNullException.ThrowIfNull(key_prefix);
if (key_prefix.Length == 0)
ArgumentNullException.ThrowIfNull(keyPrefix);
if (keyPrefix.Length == 0)
{
// Backwards seek for zero prefix is not supported for now.
throw new ArgumentOutOfRangeException(nameof(key_prefix));
throw new ArgumentOutOfRangeException(nameof(keyPrefix));
}
seek_prefix = null;
for (var i = key_prefix.Length - 1; i >= 0; i--)
seekPrefix = null;
for (var i = keyPrefix.Length - 1; i >= 0; i--)
{
if (key_prefix[i] < 0xff)
if (keyPrefix[i] < 0xff)
{
seek_prefix = key_prefix.Take(i + 1).ToArray();
// The next key after the key_prefix.
seek_prefix[i]++;
seekPrefix = keyPrefix.Take(i + 1).ToArray();
// The next key after the keyPrefix.
seekPrefix[i]++;
break;
}
}
if (seek_prefix == null)
if (seekPrefix == null)
{
throw new ArgumentException($"{nameof(key_prefix)} with all bytes being 0xff is not supported now");
throw new ArgumentException($"{nameof(keyPrefix)} with all bytes being 0xff is not supported now");
}
}
return FindInternal(key_prefix, seek_prefix, direction);
return FindInternal(keyPrefix, seekPrefix, direction);
}

private IEnumerable<(StorageKey Key, StorageItem Value)> FindInternal(byte[]? key_prefix, byte[]? seek_prefix, SeekDirection direction)
private IEnumerable<(StorageKey Key, StorageItem Value)> FindInternal(byte[]? keyPrefix, byte[]? seekPrefix, SeekDirection direction)
{
foreach (var (key, value) in Seek(seek_prefix, direction))
if (key_prefix == null || key.ToArray().AsSpan().StartsWith(key_prefix))
foreach (var (key, value) in Seek(seekPrefix, direction))
{
if (keyPrefix == null || key.ToArray().AsSpan().StartsWith(keyPrefix))
yield return (key, value);
else if (direction == SeekDirection.Forward || (seek_prefix == null || !key.ToArray().SequenceEqual(seek_prefix)))
else if (direction == SeekDirection.Forward || (seekPrefix == null || !key.ToArray().SequenceEqual(seekPrefix)))
yield break;
}
}

/// <summary>
Expand All @@ -320,10 +322,12 @@ public void Delete(StorageKey key)
? ByteArrayComparer.Default
: ByteArrayComparer.Reverse;
foreach (var (key, value) in Seek(start, direction))
{
if (comparer.Compare(key.ToArray(), end) < 0)
yield return (key, value);
else
yield break;
}
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Neo/Persistence/IReadOnlyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ public TValue this[TKey key]
/// <summary>
/// Finds the entries starting with the specified prefix.
/// </summary>
/// <param name="key_prefix">The prefix of the key.</param>
/// <param name="keyPrefix">The prefix of the key.</param>
/// <param name="direction">The search direction.</param>
/// <returns>The entries found with the desired prefix.</returns>
public IEnumerable<(TKey Key, TValue Value)> Find(TKey? key_prefix = null, SeekDirection direction = SeekDirection.Forward);
public IEnumerable<(TKey Key, TValue Value)> Find(TKey? keyPrefix = null, SeekDirection direction = SeekDirection.Forward);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Neo/SmartContract/ApplicationEngine.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ partial class ApplicationEngine
/// </summary>
public const int MaxNotificationCount = 512;

private uint random_times = 0;
private uint randomTimes = 0;

/// <summary>
/// The <see cref="InteropDescriptor"/> of System.Runtime.Platform.
Expand Down Expand Up @@ -314,7 +314,7 @@ protected internal BigInteger GetRandom()
long price;
if (IsHardforkEnabled(Hardfork.HF_Aspidochelone))
{
buffer = Cryptography.Helper.Murmur128(nonceData, ProtocolSettings.Network + random_times++);
buffer = Cryptography.Helper.Murmur128(nonceData, ProtocolSettings.Network + randomTimes++);
price = 1 << 13;
}
else
Expand Down
37 changes: 19 additions & 18 deletions src/Neo/SmartContract/Native/FungibleToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,37 +136,38 @@ private protected async ContractTask<bool> Transfer(ApplicationEngine engine, UI
if (amount.Sign < 0) throw new ArgumentOutOfRangeException(nameof(amount), "cannot be negative");
if (!from.Equals(engine.CallingScriptHash) && !engine.CheckWitnessInternal(from))
return false;
StorageKey key_from = CreateStorageKey(Prefix_Account, from);
StorageItem storage_from = engine.SnapshotCache.GetAndChange(key_from);

StorageKey keyFrom = CreateStorageKey(Prefix_Account, from);
StorageItem storageFrom = engine.SnapshotCache.GetAndChange(keyFrom);
if (amount.IsZero)
{
if (storage_from != null)
if (storageFrom != null)
{
TState state_from = storage_from.GetInteroperable<TState>();
OnBalanceChanging(engine, from, state_from, amount);
TState stateFrom = storageFrom.GetInteroperable<TState>();
OnBalanceChanging(engine, from, stateFrom, amount);
}
}
else
{
if (storage_from is null) return false;
TState state_from = storage_from.GetInteroperable<TState>();
if (state_from.Balance < amount) return false;
if (storageFrom is null) return false;
TState stateFrom = storageFrom.GetInteroperable<TState>();
if (stateFrom.Balance < amount) return false;
if (from.Equals(to))
{
OnBalanceChanging(engine, from, state_from, BigInteger.Zero);
OnBalanceChanging(engine, from, stateFrom, BigInteger.Zero);
}
else
{
OnBalanceChanging(engine, from, state_from, -amount);
if (state_from.Balance == amount)
engine.SnapshotCache.Delete(key_from);
OnBalanceChanging(engine, from, stateFrom, -amount);
if (stateFrom.Balance == amount)
engine.SnapshotCache.Delete(keyFrom);
else
state_from.Balance -= amount;
StorageKey key_to = CreateStorageKey(Prefix_Account, to);
StorageItem storage_to = engine.SnapshotCache.GetAndChange(key_to, () => new StorageItem(new TState()));
TState state_to = storage_to.GetInteroperable<TState>();
OnBalanceChanging(engine, to, state_to, amount);
state_to.Balance += amount;
stateFrom.Balance -= amount;
StorageKey keyTo = CreateStorageKey(Prefix_Account, to);
StorageItem storageTo = engine.SnapshotCache.GetAndChange(keyTo, () => new StorageItem(new TState()));
TState stateTo = storageTo.GetInteroperable<TState>();
OnBalanceChanging(engine, to, stateTo, amount);
stateTo.Balance += amount;
}
}
await PostTransferAsync(engine, from, to, amount, data, true);
Expand Down
Loading