public IEnumerable <IReportElement> CreateSection(ValidationReportData data)
        {
            // If compilation failed we have nothing to validate
            if (!data.CompilationSuccess)
            {
                yield break;
            }

            yield return(new ReportElement($"Determinism Validation Result"));

            yield return(new ReportElement($"Determinism Valid: {data.DeterminismValid}"));

            yield return(new NewLineElement());

            if (!data.DeterminismValid)
            {
                var grouped = data.DeterminismValidationErrors.GroupBy(x => x.SubjectName);
                foreach (var method in grouped)
                {
                    yield return(new ReportElement($"{method.Key}:"));

                    foreach (var error in method)
                    {
                        yield return(new ReportElement($"   {error.Message}"));
                    }

                    yield return(new NewLineElement());
                }
            }
        }
Esempio n. 2
0
        public IEnumerable <IReportElement> CreateSection(ValidationReportData data)
        {
            if (data.CompilationSuccess && data.FormatValid && data.DeterminismValid)
            {
                yield return(new ReportElement("ByteCode"));

                yield return(new ReportElement(data.CompilationBytes.ToHexString()));
            }
        }
Esempio n. 3
0
        public IEnumerable <IReportElement> CreateSection(ValidationReportData data)
        {
            yield return(new NewLineElement());

            foreach (Warning warning in data.Warnings)
            {
                yield return(new ReportElement($"Warning: {warning.Message}"));
            }

            yield return(new NewLineElement());
        }
Esempio n. 4
0
        public IEnumerable <IReportElement> CreateSection(ValidationReportData data)
        {
            yield return(new ReportElement($"Compilation Result"));

            yield return(new ReportElement($"Compilation OK: {data.CompilationSuccess}"));

            foreach (var compilationError in data.CompilationErrors)
            {
                yield return(new ReportElement($"Error: {compilationError.Message}"));
            }

            yield return(new NewLineElement());
        }
        public IEnumerable <IReportElement> CreateSection(ValidationReportData data)
        {
            if (data.CompilationSuccess && data.FormatValid && data.DeterminismValid)
            {
                yield return(new ReportElement("Hash"));

                byte[]  hash        = HashHelper.Keccak256(data.CompilationBytes);
                uint256 hashDisplay = new uint256(hash);
                yield return(new ReportElement(hashDisplay.ToString()));

                yield return(new NewLineElement());

                yield return(new ReportElement("ByteCode"));

                yield return(new ReportElement(data.CompilationBytes.ToHexString()));
            }
        }
        public IEnumerable<IReportElement> CreateSection(ValidationReportData data)
        {
            // If compilation failed we have nothing to validate
            if (!data.CompilationSuccess)
            {
                yield break;
            }

            yield return new ReportElement($"Format Validation Result");
            yield return new ReportElement($"Format Valid: {data.FormatValid}");

            foreach (var error in data.FormatValidationErrors)
            {
                yield return new ReportElement($"Error: {error.Message}");
            }

            yield return new NewLineElement();
        }
Esempio n. 7
0
        private int OnExecute(CommandLineApplication app)
        {
            if (!this.InputFiles.Any())
            {
                app.ShowHelp();
                return(1);
            }

            Console.WriteLine();
            Console.WriteLine("Smart Contract Validator");
            Console.WriteLine();

            var determinismValidator = new SctDeterminismValidator();
            var formatValidator      = new SmartContractFormatValidator();
            var warningValidator     = new SmartContractWarningValidator();

            var reportData = new List <ValidationReportData>();

            foreach (string file in this.InputFiles)
            {
                if (!File.Exists(file))
                {
                    Console.WriteLine($"{file} does not exist");
                    continue;
                }

                string source;

                Console.WriteLine($"Reading {file}");

                using (var sr = new StreamReader(File.OpenRead(file)))
                {
                    source = sr.ReadToEnd();
                }

                Console.WriteLine($"Read {file} OK");
                Console.WriteLine();

                if (string.IsNullOrWhiteSpace(source))
                {
                    Console.WriteLine($"Empty file at {file}");
                    Console.WriteLine();
                    continue;
                }

                var validationData = new ValidationReportData
                {
                    FileName                    = file,
                    CompilationErrors           = new List <CompilationError>(),
                    DeterminismValidationErrors = new List <ValidationResult>(),
                    FormatValidationErrors      = new List <ValidationError>(),
                    Warnings                    = new List <Warning>()
                };

                reportData.Add(validationData);

                Console.WriteLine($"Compiling...");
                ContractCompilationResult compilationResult = ContractCompiler.Compile(source);

                validationData.CompilationSuccess = compilationResult.Success;

                if (!compilationResult.Success)
                {
                    Console.WriteLine("Compilation failed!");
                    Console.WriteLine();

                    validationData.CompilationErrors
                    .AddRange(compilationResult
                              .Diagnostics
                              .Select(d => new CompilationError {
                        Message = d.ToString()
                    }));

                    continue;
                }

                validationData.CompilationBytes = compilationResult.Compilation;

                Console.WriteLine($"Compilation OK");
                Console.WriteLine();

                byte[] compilation = compilationResult.Compilation;

                Console.WriteLine("Building ModuleDefinition");

                IContractModuleDefinition moduleDefinition = ContractDecompiler.GetModuleDefinition(compilation, new DotNetCoreAssemblyResolver()).Value;

                Console.WriteLine("ModuleDefinition built successfully");
                Console.WriteLine();

                Console.WriteLine($"Validating file {file}...");
                Console.WriteLine();

                SmartContractValidationResult formatValidationResult = formatValidator.Validate(moduleDefinition.ModuleDefinition);

                validationData.FormatValid = formatValidationResult.IsValid;

                validationData
                .FormatValidationErrors
                .AddRange(formatValidationResult
                          .Errors
                          .Select(e => new ValidationError {
                    Message = e.Message
                }));

                SmartContractValidationResult determinismValidationResult = determinismValidator.Validate(moduleDefinition);

                validationData.DeterminismValid = determinismValidationResult.IsValid;

                validationData
                .DeterminismValidationErrors
                .AddRange(determinismValidationResult.Errors);

                SmartContractValidationResult warningResult = warningValidator.Validate(moduleDefinition.ModuleDefinition);

                validationData
                .Warnings
                .AddRange(warningResult
                          .Errors
                          .Select(e => new Warning {
                    Message = e.Message
                }));
            }

            List <IReportSection> reportStructure = new List <IReportSection>();

            reportStructure.Add(new HeaderSection());
            reportStructure.Add(new CompilationSection());

            reportStructure.Add(new FormatSection());
            reportStructure.Add(new DeterminismSection());

            reportStructure.Add(new WarningsSection());

            if (this.ShowBytes)
            {
                reportStructure.Add(new ByteCodeSection());
            }

            reportStructure.Add(new FooterSection());

            var renderer = new StreamTextRenderer(Console.Out);

            foreach (ValidationReportData data in reportData)
            {
                renderer.Render(reportStructure, data);
            }

            return(1);
        }
Esempio n. 8
0
 public IEnumerable <IReportElement> CreateSection(ValidationReportData data)
 {
     yield return(new SpacerElement());
 }
Esempio n. 9
0
 public IEnumerable <IReportElement> CreateSection(ValidationReportData data)
 {
     yield return(new HeaderElement($"Smart Contract Validation results for file {data.FileName}"));
 }