Skip to content

Commit

Permalink
update signle contract check
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim8y committed Feb 24, 2024
1 parent 33a377a commit cf94f21
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions src/Neo.Compiler.CSharp/CompilationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Akka.Util.Internal;
using BigInteger = System.Numerics.BigInteger;

namespace Neo.Compiler
Expand Down Expand Up @@ -54,7 +55,6 @@ public CompilationEngine(Options options)
public List<CompilationContext> Compile(IEnumerable<string> sourceFiles, IEnumerable<MetadataReference> references)
{
IEnumerable<SyntaxTree> syntaxTrees = sourceFiles.OrderBy(p => p).Select(p => CSharpSyntaxTree.ParseText(File.ReadAllText(p), options: Options.GetParseOptions(), path: p));
if (IsSingleAbstractClass(syntaxTrees)) throw new FormatException("The given class is abstract, no valid neo SmartContract found.");
CSharpCompilationOptions compilationOptions = new(OutputKind.DynamicallyLinkedLibrary, deterministic: true, nullableContextOptions: Options.Nullable);
Compilation = CSharpCompilation.Create(null, syntaxTrees, references, compilationOptions);
return CompileProjectContracts(Compilation);
Expand Down Expand Up @@ -104,6 +104,9 @@ private List<CompilationContext> CompileProjectContracts(Compilation compilation
}
}

// Verify if there is any valid smart contract class
if (classDependencies.Count == 0) throw new FormatException("No valid neo SmartContract found. Please make sure your contract is subclass of SmartContract and is not abstract.");
// Check contract dependencies, make sure there is no cycle in the dependency graph
var sortedClasses = TopologicalSort(classDependencies);
foreach (var classSymbol in sortedClasses)
{
Expand All @@ -125,13 +128,11 @@ void Visit(INamedTypeSymbol classSymbol)
{
return;
}
if (visiting.Contains(classSymbol))
if (!visiting.Add(classSymbol))
{
throw new InvalidOperationException("Cyclic dependency detected");
}

visiting.Add(classSymbol);

if (dependencies.TryGetValue(classSymbol, out var dependency))
{
foreach (var dep in dependency)
Expand Down Expand Up @@ -244,15 +245,5 @@ public Compilation GetCompilation(string csproj)
}
return reference;
}

private static bool IsSingleAbstractClass(IEnumerable<SyntaxTree> syntaxTrees)
{
if (syntaxTrees.Count() != 1) return false;

var tree = syntaxTrees.First();
var classDeclarations = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().ToList();

return classDeclarations.Count == 1 && classDeclarations[0].Modifiers.Any(SyntaxKind.AbstractKeyword);
}
}
}

0 comments on commit cf94f21

Please sign in to comment.