Skip to content

Commit

Permalink
merge from master
Browse files Browse the repository at this point in the history
  • Loading branch information
cschuchardt88 committed Jul 19, 2024
2 parents e644bd3 + f307a31 commit 79888f0
Show file tree
Hide file tree
Showing 95 changed files with 1,634 additions and 675 deletions.
18 changes: 6 additions & 12 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Check Format (*.cs)
run: dotnet format --verify-no-changes --verbosity diagnostic

Build-Test-Neo-Cli:
Test-Everything:
needs: [Format]
timeout-minutes: 15
runs-on: ubuntu-latest
Expand All @@ -37,16 +37,16 @@ jobs:
with:
dotnet-version: ${{ env.DOTNET_VERSION }}

- name: Build (Neo.CLI)
run: dotnet build ./src/Neo.CLI --output ./out/Neo.CLI
- name: Build (Everything)
run: dotnet build

- name: Install dependencies
run: |
sudo apt-get install libleveldb-dev expect
find ./out -name 'config.json' | xargs perl -pi -e 's|LevelDBStore|MemoryStore|g'
find ./bin -name 'config.json' | xargs perl -pi -e 's|LevelDBStore|MemoryStore|g'
- name: Run tests with expect
run: expect ./scripts/Neo.CLI/test-neo-cli.expect
run: expect ./scripts/Neo.CLI/test-neo-cli.exp

Test:
needs: [Format]
Expand Down Expand Up @@ -126,19 +126,13 @@ jobs:
- name: Set Version
run: git rev-list --count HEAD | xargs printf 'CI%05d' | xargs -I{} echo 'VERSION_SUFFIX={}' >> $GITHUB_ENV

- name : Pack (Neo)
- name : Pack (Everything)
run: |
dotnet pack \
--configuration Release \
--output ./out \
--version-suffix ${{ env.VERSION_SUFFIX }}
- name: Remove Unwanted Files
working-directory: ./out
run: |
rm -v Neo.CLI*
rm -v Neo.GUI*
- name: Publish to Github Packages
working-directory: ./out
run: |
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/Neo.Benchmarks/Benchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private static void Run(string name, string poc)
Script = Convert.FromBase64String(poc),
Witnesses = Array.Empty<Witness>()
};
using var snapshot = system.GetSnapshot();
using var snapshot = system.GetSnapshotCache();
using var engine = ApplicationEngine.Create(TriggerType.Application, tx, snapshot, system.GenesisBlock, protocol, tx.SystemFee);
engine.LoadScript(tx.Script);
Stopwatch stopwatch = Stopwatch.StartNew();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/expect -f
#!/usr/bin/expect -d -f
#
# This script uses expect to test neo-cli
#
set timeout 10

exp_internal true

# Start neo-cli
spawn dotnet out/Neo.CLI/neo-cli.dll
spawn dotnet ./bin/Neo.CLI/net8.0/neo-cli.dll

# Expect the main input prompt
expect {
Expand All @@ -18,7 +18,7 @@ expect {
#
# Test 'create wallet'
#
send "create wallet test-wallet1.json\n"
send "create wallet ./bin/Neo.CLI/test-wallet1.json\n"

expect {
"password:" { send "asd\n" }
Expand All @@ -42,7 +42,7 @@ expect {
#
# Test 'create wallet'
#
send "create wallet test-wallet2.json L2ArHTuiDL4FHu4nfyhamrG8XVYB4QyRbmhj7vD6hFMB5iAMSTf6\n"
send "create wallet ./bin/Neo.CLI/test-wallet2.json L2ArHTuiDL4FHu4nfyhamrG8XVYB4QyRbmhj7vD6hFMB5iAMSTf6\n"

expect {
"password:" { send "abcd\n" }
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.GUI/GUI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private void timer1_Tick(object sender, EventArgs e)
check_nep5_balance = false;
UInt160[] addresses = Service.CurrentWallet.GetAccounts().Select(p => p.ScriptHash).ToArray();
if (addresses.Length == 0) return;
using var snapshot = Service.NeoSystem.GetSnapshot();
using var snapshot = Service.NeoSystem.GetSnapshotCache();
foreach (UInt160 assetId in NEP5Watched)
{
byte[] script;
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.Json/Neo.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.3" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>

</Project>
72 changes: 5 additions & 67 deletions src/Neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,18 @@
using Akka.Actor;
using Akka.Configuration;
using Akka.IO;
using Akka.Util.Internal;
using Neo.IO.Actors;
using Neo.Network.P2P;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.Plugins;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace Neo.Ledger
{
Expand Down Expand Up @@ -421,7 +417,7 @@ private void OnTransaction(Transaction tx)

private void Persist(Block block)
{
using (SnapshotCache snapshot = system.GetSnapshot())
using (SnapshotCache snapshot = system.GetSnapshotCache())
{
List<ApplicationExecuted> all_application_executed = new();
TransactionState[] transactionStates;
Expand All @@ -439,7 +435,7 @@ private void Persist(Block block)
all_application_executed.Add(application_executed);
transactionStates = engine.GetState<TransactionState[]>();
}
DataCache clonedSnapshot = snapshot.CreateSnapshot();
DataCache clonedSnapshot = snapshot.CloneCache();
// Warning: Do not write into variable snapshot directly. Write into variable clonedSnapshot and commit instead.
foreach (TransactionState transactionState in transactionStates)
{
Expand All @@ -453,7 +449,7 @@ private void Persist(Block block)
}
else
{
clonedSnapshot = snapshot.CreateSnapshot();
clonedSnapshot = snapshot.CloneCache();
}
ApplicationExecuted application_executed = new(engine);
Context.System.EventStream.Publish(application_executed);
Expand All @@ -472,10 +468,10 @@ private void Persist(Block block)
Context.System.EventStream.Publish(application_executed);
all_application_executed.Add(application_executed);
}
_ = InvokeCommittingAsync(system, block, snapshot, all_application_executed);
Committing?.Invoke(system, block, snapshot, all_application_executed);
snapshot.Commit();
}
_ = InvokeCommittedAsync(system, block);
Committed?.Invoke(system, block);
system.MemPool.UpdatePoolForBlockPersisted(block, system.StoreView);
extensibleWitnessWhiteList = null;
block_cache.Remove(block.PrevHash);
Expand All @@ -484,64 +480,6 @@ private void Persist(Block block)
Debug.Assert(header.Index == block.Index);
}

internal static async Task InvokeCommittingAsync(NeoSystem system, Block block, DataCache snapshot, IReadOnlyList<ApplicationExecuted> applicationExecutedList)
{
await InvokeHandlersAsync(Committing?.GetInvocationList(), h => ((CommittingHandler)h)(system, block, snapshot, applicationExecutedList));
}

internal static async Task InvokeCommittedAsync(NeoSystem system, Block block)
{
await InvokeHandlersAsync(Committed?.GetInvocationList(), h => ((CommittedHandler)h)(system, block));
}

private static async Task InvokeHandlersAsync(Delegate[] handlers, Action<Delegate> handlerAction)
{
if (handlers == null) return;

var exceptions = new ConcurrentBag<Exception>();
var tasks = handlers.Select(handler => Task.Run(() =>
{
try
{
// skip stopped plugin.
if (handler.Target is Plugin { IsStopped: true })
{
return;
}

handlerAction(handler);
}
catch (Exception ex) when (handler.Target is Plugin plugin)
{
switch (plugin.ExceptionPolicy)
{
case UnhandledExceptionPolicy.StopNode:
exceptions.Add(ex);
throw;
case UnhandledExceptionPolicy.StopPlugin:
//Stop plugin on exception
plugin.IsStopped = true;
break;
case UnhandledExceptionPolicy.Ignore:
// Log the exception and continue with the next handler
break;
default:
throw new InvalidCastException($"The exception policy {plugin.ExceptionPolicy} is not valid.");
}

Utility.Log(nameof(plugin), LogLevel.Error, ex);
}
catch (Exception ex)
{
exceptions.Add(ex);
}
})).ToList();

await Task.WhenAll(tasks);

exceptions.ForEach(e => throw e);
}

/// <summary>
/// Gets a <see cref="Akka.Actor.Props"/> object used for creating the <see cref="Blockchain"/> actor.
/// </summary>
Expand Down
19 changes: 19 additions & 0 deletions src/Neo/Ledger/MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -659,5 +659,24 @@ internal bool ReVerifyTopUnverifiedTransactionsIfNeeded(int maxToVerify, DataCac

return _unverifiedTransactions.Count > 0;
}

// This method is only for test purpose
// Do not use this method outside of unit tests
internal void Clear()
{
_txRwLock.EnterReadLock();
try
{
_unsortedTransactions.Clear();
_conflicts.Clear();
_sortedTransactions.Clear();
_unverifiedTransactions.Clear();
_unverifiedSortedTransactions.Clear();
}
finally
{
_txRwLock.ExitReadLock();
}
}
}
}
14 changes: 13 additions & 1 deletion src/Neo/NeoSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,24 @@ public void SuspendNodeStartup()
/// <summary>
/// Gets a snapshot of the blockchain storage.
/// </summary>
/// <returns></returns>
/// <returns>An instance of <see cref="SnapshotCache"/></returns>
[Obsolete("This method is obsolete, use GetSnapshotCache instead.")]
public SnapshotCache GetSnapshot()
{
return new SnapshotCache(store.GetSnapshot());
}

/// <summary>
/// Gets a snapshot of the blockchain storage with an execution cache.
/// With the snapshot, we have the latest state of the blockchain, with the cache,
/// we can run transactions in a sandboxed environment.
/// </summary>
/// <returns>An instance of <see cref="SnapshotCache"/></returns>
public SnapshotCache GetSnapshotCache()
{
return new SnapshotCache(store.GetSnapshot());
}

/// <summary>
/// Determines whether the specified transaction exists in the memory pool or storage.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Neo/Persistence/DataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,21 @@ public virtual void Commit()
/// Creates a snapshot, which uses this instance as the underlying storage.
/// </summary>
/// <returns>The snapshot of this instance.</returns>
[Obsolete("CreateSnapshot is deprecated, please use CloneCache instead.")]
public DataCache CreateSnapshot()
{
return new ClonedCache(this);
}

/// <summary>
/// Creates a clone of the snapshot cache, which uses this instance as the underlying storage.
/// </summary>
/// <returns>The <see cref="DataCache"/> of this <see cref="SnapshotCache"/> instance.</returns>
public DataCache CloneCache()
{
return new ClonedCache(this);
}

/// <summary>
/// Deletes an entry from the cache.
/// </summary>
Expand Down
Loading

0 comments on commit 79888f0

Please sign in to comment.