public ValidationServiceResult Validate(string fileName, string source, IConsole console, string[] parameters)
        {
            var validationServiceResult = new ValidationServiceResult();

            CompileContract(source, console, validationServiceResult);
            ValidateContract(fileName, console, parameters, validationServiceResult);

            return(validationServiceResult);
        }
        private static void BuildModuleDefinition(IConsole console, ValidationServiceResult validationServiceResult, out byte[] compilation, out IContractModuleDefinition moduleDefinition)
        {
            console.WriteLine("Building ModuleDefinition...");

            compilation      = validationServiceResult.CompilationResult.Compilation;
            moduleDefinition = ContractDecompiler.GetModuleDefinition(compilation, new DotNetCoreAssemblyResolver()).Value;
            console.WriteLine("ModuleDefinition built successfully.");

            console.WriteLine();
        }
        private static void CompileContract(string source, IConsole console, ValidationServiceResult validationServiceResult)
        {
            console.WriteLine($"Compiling...");
            validationServiceResult.CompilationResult = ContractCompiler.Compile(source);
            if (!validationServiceResult.CompilationResult.Success)
            {
                console.WriteLine("Compilation failed!");
            }
            else
            {
                console.WriteLine($"Compilation OK");
            }

            console.WriteLine();
        }
        public static ValidationServiceResult Validate(string fileName, ContractCompilationResult compilationResult, IConsole console, string[] parameters)
        {
            var validationServiceResult = new ValidationServiceResult();

            byte[] compilation = compilationResult.Compilation;
            IContractModuleDefinition moduleDefinition = BuildModuleDefinition(console, compilation);

            console.WriteLine($"Validating file {fileName}...");

            Assembly smartContract = Assembly.Load(compilation);

            // Network does not matter here as we are only checking the deserialized Types of the params.
            var serializer = new MethodParameterStringSerializer(new SmartContractsRegTest());

            object[] methodParameters = null;
            if (parameters.Length != 0)
            {
                methodParameters = serializer.Deserialize(parameters);
            }

            validationServiceResult.ConstructorExists = Contract.ConstructorExists(smartContract.ExportedTypes.FirstOrDefault(), methodParameters);

            if (!validationServiceResult.ConstructorExists)
            {
                console.WriteLine("Smart contract construction failed.");
                console.WriteLine("No constructor exists with the provided parameters.");
            }

            validationServiceResult.DeterminismValidationResult = new SctDeterminismValidator().Validate(moduleDefinition);
            validationServiceResult.FormatValidationResult      = new SmartContractFormatValidator().Validate(moduleDefinition.ModuleDefinition);
            if (!validationServiceResult.DeterminismValidationResult.IsValid || !validationServiceResult.FormatValidationResult.IsValid)
            {
                console.WriteLine("Smart Contract failed validation. Run validate [FILE] for more info.");
            }

            console.WriteLine();

            return(validationServiceResult);
        }
        private static void ValidateContract(string fileName, IConsole console, string[] parameters, ValidationServiceResult validationServiceResult)
        {
            byte[] compilation;
            IContractModuleDefinition moduleDefinition;

            BuildModuleDefinition(console, validationServiceResult, out compilation, out moduleDefinition);

            console.WriteLine($"Validating file {fileName}...");

            Assembly smartContract = Assembly.Load(compilation);

            var serializer = new MethodParameterSerializer();

            object[] methodParameters = null;
            if (parameters.Length != 0)
            {
                var methodParametersRaw = new MethodParameterSerializer().ToRaw(parameters);
                methodParameters = serializer.ToObjects(methodParametersRaw);
            }

            validationServiceResult.ConstructorExists = Contract.ConstructorExists(smartContract.ExportedTypes.FirstOrDefault(), methodParameters);

            if (!validationServiceResult.ConstructorExists)
            {
                console.WriteLine("Smart contract construction failed.");
                console.WriteLine("No constructor exists with the provided parameters.");
            }

            validationServiceResult.DeterminismValidationResult = new SctDeterminismValidator().Validate(moduleDefinition);
            validationServiceResult.FormatValidationResult      = new SmartContractFormatValidator().Validate(moduleDefinition.ModuleDefinition);
            if (!validationServiceResult.DeterminismValidationResult.IsValid || !validationServiceResult.FormatValidationResult.IsValid)
            {
                console.WriteLine("Smart Contract failed validation. Run validate [FILE] for more info.");
            }

            console.WriteLine();
        }
Exemple #6
0
        private static void ValidateContract(string fileName, IConsole console, string[] parameters, ValidationServiceResult validationServiceResult)
        {
            byte[] compilation;
            SmartContractDecompilation decompilation;

            BuildModuleDefinition(console, validationServiceResult, out compilation, out decompilation);

            console.WriteLine($"Validating file {fileName}...");

            Assembly smartContract = Assembly.Load(compilation);

            var serializer = new MethodParameterSerializer();

            object[] methodParameters = null;
            if (parameters.Length != 0)
            {
                var methodParametersRaw = new MethodParameterSerializer().ToRaw(parameters);
                methodParameters = serializer.ToObjects(methodParametersRaw);
            }

            validationServiceResult.LifeCycleResult = SmartContractConstructor.Construct(smartContract.ExportedTypes.FirstOrDefault(), new ValidatorSmartContractState(), methodParameters);
            if (!validationServiceResult.LifeCycleResult.Success)
            {
                console.WriteLine("Smart contract construction failed.");
                console.WriteLine("If the smart contract is constructed with parameters, please ensure they are provided.");
            }

            validationServiceResult.DeterminismValidationResult = new SmartContractDeterminismValidator().Validate(decompilation);
            validationServiceResult.FormatValidationResult      = new SmartContractFormatValidator(ReferencedAssemblyResolver.AllowedAssemblies).Validate(decompilation);
            if (!validationServiceResult.DeterminismValidationResult.IsValid || !validationServiceResult.FormatValidationResult.IsValid)
            {
                console.WriteLine("Smart Contract failed validation. Run validate [FILE] for more info.");
            }

            console.WriteLine();
        }