public static void Main(string[] args) { DirectoryInfo debugDirectoryInfo = Directory.GetParent(Directory.GetCurrentDirectory()); string workingDirectory = debugDirectoryInfo.Parent.Parent.Parent.Parent.FullName; string filename = "Branch.sol"; string solcPath = workingDirectory + "\\Tool\\solc.exe"; string filePath = workingDirectory + "\\Test\\regression\\" + filename; SolidityCompiler compiler = new SolidityCompiler(); CompilerOutput compilerOutput = compiler.Compile(solcPath, filePath); SoliditySourceFile soliditySourceFile = compilerOutput.Sources[filename]; ASTNode root = soliditySourceFile.Ast; Debug.Assert(root.GetType() == typeof(SourceUnit)); foreach (ASTNode decl in ((SourceUnit)root).Nodes) { if (decl is ContractDefinition) { ContractDefinition contract = (ContractDefinition)decl; Console.WriteLine("contract " + contract.Name); foreach (ASTNode node in contract.Nodes) { if (node is FunctionDefinition) { FunctionDefinition func = (FunctionDefinition)node; Console.WriteLine("function " + func.Name); Test(func); } } } } Console.ReadLine(); }
public void WhenCompile() { string contract = "contract Multiply8 { " + "function multiply() returns(string) { " + "return \"hello world\"; " + "} " + "}"; var binaries = SolidityCompiler.Compile(contract); string s = ""; }
public BatchExeResult Execute(string filename, out string expected, out string current) { BatchExeResult result = BatchExeResult.SolcError; string filePath = Path.Combine(testDirectory, filename); // compile the program SolidityCompiler compiler = new SolidityCompiler(); CompilerOutput compilerOutput = compiler.Compile(solcPath, filePath); if (compilerOutput.ContainsError()) { compilerOutput.PrintErrorsToConsole(); throw new SystemException("Compilation Error"); } // build the Solidity AST from solc output AST solidityAST = new AST(compilerOutput, Path.GetDirectoryName(filePath)); // translate Solidity to Boogie try { BoogieTranslator translator = new BoogieTranslator(); var translatorFlags = new TranslatorFlags(); translatorFlags.GenerateInlineAttributes = false; BoogieAST boogieAST = translator.Translate(solidityAST, new HashSet <Tuple <string, string> >(), translatorFlags); // dump the Boogie program to a file using (var outWriter = new StreamWriter(outFile)) { outWriter.WriteLine(boogieAST.GetRoot()); } } catch (Exception e) { Console.WriteLine($"VeriSol translation error: {e.Message}"); result = BatchExeResult.SolToBoogieError; expected = current = null; return(result); } // read the corral configuration from Json string configJsonPath = Path.Combine(configDirectory, Path.GetFileNameWithoutExtension(filename) + ".json"); string jsonString = File.ReadAllText(configJsonPath); CorralConfiguration corralConfig = JsonConvert.DeserializeObject <CorralConfiguration>(jsonString); string corralOutput = RunCorral(corralConfig); expected = corralConfig.ExpectedResult; current = corralOutput; result = CompareCorralOutput(corralConfig.ExpectedResult, corralOutput); return(result); }
public void WhenGetAbi() { string contract = "contract Multiply8 { " + "function multiply() returns(string) { " + "return \"hello world\"; " + "} " + "}" + "contract Multiply6 { " + "function multiply() returns(string) { " + "return \"hello world\"; " + "} " + "}"; var arr = SolidityCompiler.Compile(contract); Assert.IsTrue(arr.Count() == 2); }
/** * Runs solidity conversion function */ private bool runSolidityToBoogieConversion() { Console.WriteLine("Starting Solidity Compiler."); // compile the program Console.WriteLine($"Running Compiler on {contractName}."); SolidityCompiler compiler = new SolidityCompiler(); CompilerOutput compilerOutput = compiler.Compile(solidityCompilerPath, solidityFilePath); if (compilerOutput.ContainsError()) { compilerOutput.PrintErrorsToConsole(); throw new SystemException("Compilation Error"); } // build the Solidity AST from solc output AST solidityAST = new AST(compilerOutput, Path.GetDirectoryName(solidityFilePath)); // translate Solidity to Boogie try { // if application reaches this stage, compilation of the program was successful // The application now attemps to convert the solidity code to Boogie through the use of collection and syntax trees. ConversionToBoogie_Main translator = new ConversionToBoogie_Main(); Console.WriteLine($"\nAttempting Conversion to Boogie."); BoogieAST boogieAST = translator.Translate(solidityAST, ignoreMethods, translatorFlags); // dump the Boogie program to a file var outFilePath = Path.Combine(solidityFileDir, boogieRepresentationOfSolContract); using (var outWriter = new StreamWriter(boogieRepresentationOfSolContract)) { outWriter.WriteLine(boogieAST.GetRoot());//get all AST components } } catch (Exception e) { Console.WriteLine($"OverSight translation error: {e.Message}"); return(false); } return(true); }
private bool ExecuteSolToBoogie() { // compile the program Console.WriteLine($"\n----- Running Solc on {SpecFilePath}...."); SolidityCompiler compiler = new SolidityCompiler(); CompilerOutput compilerOutput = compiler.Compile(SolcPath, SpecFilePath); if (compilerOutput.ContainsError()) { compilerOutput.PrintErrorsToConsole(); throw new SystemException("Compilation Error"); } // build the Solidity AST from solc output AST solidityAST = new AST(compilerOutput, Path.GetDirectoryName(SpecFilePath)); // translate Solidity to Boogie try { BoogieTranslator translator = new BoogieTranslator(); Console.WriteLine($"\n----- Running SolToBoogie...."); var translatorFlags = new TranslatorFlags(); translatorFlags.GenerateInlineAttributes = true; BoogieAST boogieAST = translator.Translate(solidityAST, ignoreMethods, translatorFlags); // dump the Boogie program to a file var outFilePath = Path.Combine(SpecFileDir, outFileName); using (var outWriter = new StreamWriter(outFileName)) { outWriter.WriteLine(boogieAST.GetRoot()); } } catch (Exception e) { Console.WriteLine($"VeriSol translation error: {e.Message}"); return(false); } return(true); }
public JObject CompileSolidity(IEnumerable <string> parameters, string id, JObject response) { if (parameters == null || !parameters.Any()) { return(CreateErrorResponse(id, (int)RpcErrorCodes.RPC_INVALID_PARAMS, "The source is not specified")); } var contract = parameters.First(); IEnumerable <SolidityCompilerResult> compilationResult; try { compilationResult = SolidityCompiler.Compile(contract); } catch { return(CreateErrorResponse(id, (int)RpcErrorCodes.RPC_PARSE_ERROR, "the contract cannot be parsed")); } var result = new JObject(); result.Add("language", "Solidity"); result.Add("languageVersion", "0"); result.Add("compilerVersion", "0.4.19"); result.Add("source", contract); var info = new JArray(); foreach (var compilationR in compilationResult) { var record = new JObject(); record.Add("abiDefinition", compilationR.AbiCode); record.Add("code", compilationR.Payload); info.Add(record); } result.Add("info", info); response["result"] = result; return(response); }
public static void Main(string[] args) { if (args.Length < 3) { Console.WriteLine("Usage: SolToBoogie <relative path to filename.sol> <workingdir> <relative path to outfile.bpl> [options]"); Console.WriteLine("\t Options:"); Console.WriteLine("\t\t /break: Opens the debugger"); Console.WriteLine("\t\t /ignoreMethod:<method>@<contract>: Ignores translation of the method within contract, and only generates a declaration"); Console.WriteLine("\t\t\t\t multiple such pairs can be specified, ignored set is the union"); Console.WriteLine("\t\t\t\t a wild card '*' can be used for method, would mean all the methods of the contract"); Console.WriteLine("\t\t /noInlineAttrs: do not generate any {:inline x} attributes, to speed Corral"); return; } string filePath = Path.Combine(Directory.GetCurrentDirectory(), args[0]); string workingDirectory = args[1]; string outFile = Path.Combine(Directory.GetCurrentDirectory(), args[2]); string solcName = GetSolcNameByOSPlatform(); string solcPath = Path.Combine(workingDirectory, "Tool", solcName); SolidityCompiler compiler = new SolidityCompiler(); CompilerOutput compilerOutput = compiler.Compile(solcPath, filePath); HashSet <Tuple <string, string> > ignoredMethods = new HashSet <Tuple <string, string> >(); // Issue with run_verisol_win.cmd it passes "/ignoreMethod:a@b /ignoreMethod:c@d .." as a single string var splitArgs = new List <string>(); args.Select(arg => arg.Split(" ")) .ToList() .ForEach(a => a.ToList().ForEach(b => splitArgs.Add(b))); if (splitArgs.Any(x => x.Equals("/break"))) { Debugger.Launch(); } foreach (var arg in splitArgs.Where(x => x.StartsWith("/ignoreMethod:"))) { Debug.Assert(arg.Contains("@"), $"Error: incorrect use of /ignoreMethod in {arg}"); Debug.Assert(arg.LastIndexOf("@") == arg.IndexOf("@"), $"Error: incorrect use of /ignoreMethod in {arg}"); var str = arg.Substring("/ignoreMethod:".Length); var method = str.Substring(0, str.IndexOf("@")); var contract = str.Substring(str.IndexOf("@") + 1); ignoredMethods.Add(Tuple.Create(method, contract)); } Console.WriteLine($"Ignored method/contract pairs ==> \n\t {string.Join(",", ignoredMethods.Select(x => x.Item1 + "@" + x.Item2))}"); bool genInlineAttributesInBpl = true; if (splitArgs.Any(x => x.Equals("/noInlineAttrs"))) { genInlineAttributesInBpl = false; } if (compilerOutput.ContainsError()) { PrintErrors(compilerOutput.Errors); throw new SystemException("Compilation Error"); } else { AST solidityAST = new AST(compilerOutput, Path.GetDirectoryName(filePath)); try { BoogieTranslator translator = new BoogieTranslator(); BoogieAST boogieAST = translator.Translate(solidityAST, ignoredMethods, genInlineAttributesInBpl); using (var outWriter = new StreamWriter(outFile)) { outWriter.WriteLine(boogieAST.GetRoot()); } } catch (Exception e) { Console.WriteLine($"Verisol translation exception: {e.Message}"); } } }