public void GetDeployedType_MultipleTypes_DeployAttribute_Returns_Correct_Type()
        {
            var code        = @"
using Stratis.SmartContracts;

[Deploy]
public class TypeOne : SmartContract
{
    public TypeOne(ISmartContractState state) : base(state) {}
}

public class TypeTwo : SmartContract
{
    public TypeTwo(ISmartContractState state) : base(state) {}
}
";
            var compilation = ContractCompiler.Compile(code);

            var assemblyLoadResult = this.loader.Load((ContractByteCode)compilation.Compilation);

            var contractAssembly = assemblyLoadResult.Value;

            var type = contractAssembly.DeployedType;

            Assert.NotNull(type);
            Assert.Equal("TypeOne", type.Name);
        }
        public void SmartContracts_GasInjector_SingleParamConstructorGasInjectedSuccess()
        {
            ContractCompilationResult compilationResult =
                ContractCompiler.Compile(TestSingleConstructorSource);

            Assert.True(compilationResult.Success);
            byte[] originalAssemblyBytes = compilationResult.Compilation;

            IContractModuleDefinition module = this.moduleReader.Read(originalAssemblyBytes).Value;

            module.Rewrite(this.rewriter);

            CSharpFunctionalExtensions.Result <IContractAssembly> assembly = this.assemblyLoader.Load(module.ToByteCode());

            IContract contract = Contract.CreateUninitialized(assembly.Value.GetType(module.ContractType.Name), this.state, null);

            IContractInvocationResult result = contract.InvokeConstructor(null);

            // TODO: Un-hard-code this.
            // Constructor: 15
            // Property setter: 12
            // Storage: 150
            // "string newString = this.Owner + 1;": 36
            Assert.Equal((Gas)213, this.gasMeter.GasConsumed);
        }
Exemplo n.º 3
0
        public void SmartContractValidator_ModuleReference_Tests()
        {
            var adjustedSource = @"
using System;
using Stratis.SmartContracts;

[Deploy]
public class Test : SmartContract
{
    public Test(ISmartContractState state): base(state) 
    {
    }
}
";
            ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource);

            Assert.True(compilationResult.Success);

            byte[] assemblyBytes = compilationResult.Compilation;
            IContractModuleDefinition decompilation = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;

            // Add a module reference
            decompilation.ModuleDefinition.ModuleReferences.Add(new ModuleReference("Test.dll"));

            var moduleDefinition = decompilation.ModuleDefinition;

            SmartContractValidationResult result = new SmartContractValidator().Validate(moduleDefinition);

            Assert.False(result.IsValid);
            Assert.NotEmpty(result.Errors);
            Assert.True(result.Errors.All(e => e is ModuleDefinitionValidationResult));
        }
Exemplo n.º 4
0
        public void SmartContract_ValidateFormat_NewShortArray_Succeeds()
        {
            var adjustedSource = @"
using System;
using Stratis.SmartContracts;

public class Test : SmartContract
{
    public Test(ISmartContractState state) : base(state) {}

    public void CreateNewStruct() {
        var item = new [] { 1 };
    }
}
";
            ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource);

            Assert.True(compilationResult.Success);

            var validator = new SmartContractFormatValidator();

            byte[] assemblyBytes             = compilationResult.Compilation;
            IContractModuleDefinition decomp = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;

            var result = validator.Validate(decomp.ModuleDefinition);

            Assert.True(result.IsValid);
        }
Exemplo n.º 5
0
        public void SmartContract_ValidateFormat_One_CustomStruct()
        {
            var adjustedSource = @"
                using System;
                using Stratis.SmartContracts;

                public class StructTest : SmartContract
                {
                    public struct Item
                    {
                        public int Number;
                        public string Name;
                    }

                    public StructTest(ISmartContractState state) : base(state)
                    {
                    }
                }
            ";

            ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource);

            Assert.True(compilationResult.Success);

            var validator = new SmartContractFormatValidator();

            byte[] assemblyBytes             = compilationResult.Compilation;
            IContractModuleDefinition decomp = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;
            var result = validator.Validate(decomp.ModuleDefinition);

            Assert.True(result.IsValid);
        }
Exemplo n.º 6
0
        public void SmartContract_ValidateFormat_InitializedReadonlyField_Fails()
        {
            var adjustedSource = @"
                using System;
                using Stratis.SmartContracts;

                public class FieldTest : SmartContract
                {
                    private readonly int field1 = 1234567;

                    public FieldTest(ISmartContractState state) : base(state)
                    {
                    }
                }
            ";

            ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource);

            Assert.True(compilationResult.Success);

            var validator = new FieldDefinitionValidator();

            byte[] assemblyBytes             = compilationResult.Compilation;
            IContractModuleDefinition decomp = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;

            IEnumerable <ValidationResult> result = validator.Validate(decomp.ContractType);

            Assert.NotEmpty(result);
        }
Exemplo n.º 7
0
        public void Validate_Determinism_Async()
        {
            string adjustedSource = @"using System;
                                            using Stratis.SmartContracts;
                                            using System.Threading.Tasks;

                                            public class Test : SmartContract
                                            {
                                                public Test(ISmartContractState state)
                                                    : base(state) {}

                                            public async void Bid()
                                            {
                                                await Task.Run(job);
                                            }

                                            public async Task job()
                                            {
                                                int w = 9;
                                            }
                                            }";

            ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource);

            Assert.True(compilationResult.Success);

            byte[] assemblyBytes = compilationResult.Compilation;
            IContractModuleDefinition     moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;
            SmartContractValidationResult result           = this.validator.Validate(moduleDefinition.ModuleDefinition);

            Assert.False(result.IsValid);
        }
Exemplo n.º 8
0
        public void SmartContract_ValidateFormat_TwoTypes()
        {
            var adjustedSource = @"
using System;
using Stratis.SmartContracts;

public class Test : SmartContract
{
    public Test(ISmartContractState state) : base(state) {}
}


public class Test2 {
}
";
            ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource);

            Assert.True(compilationResult.Success);

            var validator = new SmartContractFormatValidator();

            byte[] assemblyBytes             = compilationResult.Compilation;
            IContractModuleDefinition decomp = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;

            var result = validator.Validate(decomp.ModuleDefinition);

            Assert.False(result.IsValid);
            Assert.Equal(2, result.Errors.Count());
        }
Exemplo n.º 9
0
        public void Validate_Determinism_DisallowedMethodParams()
        {
            string adjustedSource = @"using System;
                                            using Stratis.SmartContracts;

                                            public class Test : SmartContract
                                            {
                                                public Test(ISmartContractState state)
                                                    : base(state) {}         

                                                public void DateTime1(DateTime param)
                                                {
                                                }

                                                public void F(float param)
                                                {
                                                }
                                            }";

            ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource);

            Assert.True(compilationResult.Success);

            byte[] assemblyBytes = compilationResult.Compilation;
            IContractModuleDefinition     moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;
            SmartContractValidationResult result           = this.validator.Validate(moduleDefinition.ModuleDefinition);

            Assert.False(result.IsValid);
            Assert.Equal(2, result.Errors.Count());
            Assert.True(result.Errors.All(e => e is MethodParamValidator.MethodParamValidationResult));
            Assert.Contains(result.Errors, e => e.Message.Contains("System.DateTime"));
            Assert.Contains(result.Errors, e => e.Message.Contains("System.Single"));
        }
Exemplo n.º 10
0
        public void Validate_Determinism_ErrorMessages_TwoMethods()
        {
            string source = @"
                    using Stratis.SmartContracts;

                    public class MessageTest : SmartContract
                    {
                        public MessageTest(ISmartContractState smartContractState)
                            : base(smartContractState)
                        {
                        }

                        public void MessageTestFloat1()
                        {
                            float test = (float) 3.5;
                        }

                        public void MessageTestFloat2()
                        {
                            float test = (float) 3.5;
                        }
                    }";

            ContractCompilationResult compilationResult = ContractCompiler.Compile(source, OptimizationLevel.Debug);

            Assert.True(compilationResult.Success);

            IContractModuleDefinition     moduleDefinition = ContractDecompiler.GetModuleDefinition(compilationResult.Compilation).Value;
            SmartContractValidationResult result           = this.validator.Validate(moduleDefinition.ModuleDefinition);

            Assert.False(result.IsValid);
            Assert.Equal(2, result.Errors.Count());
            Assert.Equal(FloatValidator.FloatValidationType, result.Errors.First().ValidationType);
            Assert.Equal(FloatValidator.FloatValidationType, result.Errors.Skip(1).Take(1).First().ValidationType);
        }
        public void Validate_Determinism_GenericClass()
        {
            string adjustedSource = @"using System;
                                            using Stratis.SmartContracts;

                                            public class Test<T> : SmartContract
                                            {
                                                public Test(ISmartContractState state)
                                                    : base(state) {}         

                                                public T TestMethod(int param)
                                                {
                                                    return default(T);
                                                }
                                            }";

            ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource);

            Assert.True(compilationResult.Success);

            byte[] assemblyBytes = compilationResult.Compilation;
            IContractModuleDefinition     moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;
            SmartContractValidationResult result           = this.validator.Validate(moduleDefinition.ModuleDefinition);

            Assert.False(result.IsValid);
            Assert.Single(result.Errors);
            Assert.True(result.Errors.First() is GenericTypeValidator.GenericTypeValidationResult);
        }
Exemplo n.º 12
0
        public void Validate_Determinism_ErrorMessages_Simple_DateTime()
        {
            string source = @"
                    using Stratis.SmartContracts;
                    using System;

                    public class MessageTest : SmartContract
                    {
                        public MessageTest(ISmartContractState smartContractState)
                            : base(smartContractState)
                        {
                        }

                        public void MessageTestDateTime()
                        {
                            var test = DateTime.Now;
                        }
                    }";

            ContractCompilationResult compilationResult = ContractCompiler.Compile(source);

            Assert.True(compilationResult.Success);

            IContractModuleDefinition     moduleDefinition = ContractDecompiler.GetModuleDefinition(compilationResult.Compilation).Value;
            SmartContractValidationResult result           = this.validator.Validate(moduleDefinition.ModuleDefinition);

            Assert.False(result.IsValid);
            Assert.True(result.Errors.All(e => e is WhitelistValidator.WhitelistValidationResult));
        }
Exemplo n.º 13
0
        public void Validate_Determinism_ErrorMessages_Recursion_OneLevel()
        {
            string source = @"
                    using Stratis.SmartContracts;

                    public class MessageTest : SmartContract
                    {
                        public MessageTest(ISmartContractState smartContractState)
                            : base(smartContractState)
                        {
                        }

                        public void MessageTestValid()
                        {
                            MessageTestValid1();
                        }

                        public void MessageTestValid1()
                        {
                            float test = (float)3.5;
                            MessageTestValid();
                        }
                    }";

            ContractCompilationResult compilationResult = ContractCompiler.Compile(source, OptimizationLevel.Debug);

            Assert.True(compilationResult.Success);

            IContractModuleDefinition     moduleDefinition = ContractDecompiler.GetModuleDefinition(compilationResult.Compilation).Value;
            SmartContractValidationResult result           = this.validator.Validate(moduleDefinition.ModuleDefinition);

            Assert.False(result.IsValid);
            Assert.Single(result.Errors);
            Assert.Equal("Float usage", result.Errors.First().ValidationType);
        }
Exemplo n.º 14
0
        public void Validate_Determinism_Recursion()
        {
            string source = @"
                    using Stratis.SmartContracts;

                    public class RecursionTest : SmartContract
                    {
                        public RecursionTest(ISmartContractState smartContractState)
                            : base(smartContractState)
                        {
                        }

                        public void Bid()
                        {
                            Job(5);
                        }

                        public void Job(int index)
                        {
                            if (index > 0)
                                Job(index - 1);
                        }
                    }";

            ContractCompilationResult compilationResult = ContractCompiler.Compile(source);

            Assert.True(compilationResult.Success);

            IContractModuleDefinition     moduleDefinition = ContractDecompiler.GetModuleDefinition(compilationResult.Compilation).Value;
            SmartContractValidationResult result           = this.validator.Validate(moduleDefinition.ModuleDefinition);

            Assert.True(result.IsValid);
        }
Exemplo n.º 15
0
        public void SmartContract_ValidateFormat_NewObj_Fails()
        {
            var adjustedSource = @"
using System;
using Stratis.SmartContracts;

public class Test : SmartContract
{
    public Test(ISmartContractState state) : base(state) {}

    public void CreateNewObject() {
        var obj = new object();
    }
}
";
            ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource);

            Assert.True(compilationResult.Success);

            var validator = new SmartContractFormatValidator();

            byte[] assemblyBytes             = compilationResult.Compilation;
            IContractModuleDefinition decomp = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;

            var result = validator.Validate(decomp.ModuleDefinition);

            Assert.False(result.IsValid);
            Assert.Single(result.Errors);
            Assert.IsType <NewObjValidator.NewObjValidationResult>(result.Errors.First());
        }
Exemplo n.º 16
0
        public void SmartContract_Compiler_ReturnsTrue()
        {
            ContractCompilationResult compilationResult = ContractCompiler.Compile("class C{static void M(){}}");

            Assert.True(compilationResult.Success);
            Assert.Empty(compilationResult.Diagnostics);
            Assert.NotNull(compilationResult.Compilation);
        }
Exemplo n.º 17
0
        public void SmartContract_Compiler_ReturnsFalse()
        {
            ContractCompilationResult compilationResult = ContractCompiler.Compile("Uncompilable");

            Assert.False(compilationResult.Success);
            Assert.NotEmpty(compilationResult.Diagnostics);
            Assert.Null(compilationResult.Compilation);
        }
Exemplo n.º 18
0
        public void Validate_Determinism_Fails_MultiDimensional_Arrays()
        {
            string adjustedSource = TestString.Replace(ReplaceCodeString, @"var test = new int[50,50];").Replace(ReplaceReferencesString, "");

            byte[] assemblyBytes = ContractCompiler.Compile(adjustedSource).Compilation;
            IContractModuleDefinition     moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;
            SmartContractValidationResult result           = this.validator.Validate(moduleDefinition.ModuleDefinition);

            Assert.False(result.IsValid);
        }
Exemplo n.º 19
0
        public void Validate_Determinism_Environment()
        {
            // Environment should not be available
            // We do not compile contracts with a reference to System.Runtime.Extensions
            string adjustedSource = TestString.Replace(ReplaceCodeString, "int test = Environment.TickCount;").Replace(ReplaceReferencesString, "");

            ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource);

            Assert.False(compilationResult.Success);
        }
Exemplo n.º 20
0
        public void Validate_Determinism_Passes_ArrayConstruction_LessThan2()
        {
            string adjustedSource = TestString.Replace(ReplaceCodeString, @"var test = new int[]{10167};").Replace(ReplaceReferencesString, "");

            byte[] assemblyBytes = ContractCompiler.Compile(adjustedSource).Compilation;
            IContractModuleDefinition     moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;
            SmartContractValidationResult result           = this.validator.Validate(moduleDefinition.ModuleDefinition);

            Assert.True(result.IsValid);
        }
Exemplo n.º 21
0
        public void Validate_Determinism_AppDomain()
        {
            // AppDomain should not be available
            // We do not compile contracts with a reference to System.Runtime.Extensions
            string adjustedSource = TestString.Replace(ReplaceCodeString, @"var test = AppDomain.CurrentDomain; var test2 = test.Id;").Replace(ReplaceReferencesString, "");

            ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource);

            Assert.False(compilationResult.Success);
        }
        public IContractModuleDefinition CompileToModuleDef(string source)
        {
            ContractCompilationResult compilationResult = ContractCompiler.Compile(source, OptimizationLevel.Debug);

            Assert.True(compilationResult.Success);

            byte[] assemblyBytes             = compilationResult.Compilation;
            IContractModuleDefinition decomp = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;

            return(decomp);
        }
Exemplo n.º 23
0
        public void VM_ExecuteContract_OutOfGas()
        {
            ContractCompilationResult compilationResult = ContractCompiler.Compile(
                @"
using System;
using Stratis.SmartContracts;

public class Contract : SmartContract
{
    public Contract(ISmartContractState state) : base(state) {}
}
");

            Assert.True(compilationResult.Success);

            byte[] contractExecutionCode = compilationResult.Compilation;
            byte[] codeHash = HashHelper.Keccak256(contractExecutionCode);

            // Set up the state with an empty gasmeter so that out of gas occurs
            var contractState = Mock.Of <ISmartContractState>(s =>
                                                              s.Block == Mock.Of <IBlock>(b => b.Number == 1 && b.Coinbase == this.TestAddress) &&
                                                              s.Message == new Message(this.TestAddress, this.TestAddress, 0) &&
                                                              s.PersistentState == new PersistentState(
                                                                  new TestPersistenceStrategy(this.state),
                                                                  this.context.Serializer, this.TestAddress.ToUint160()) &&
                                                              s.Serializer == this.context.Serializer &&
                                                              s.ContractLogger == new ContractLogHolder() &&
                                                              s.InternalTransactionExecutor == Mock.Of <IInternalTransactionExecutor>() &&
                                                              s.InternalHashHelper == new InternalHashHelper() &&
                                                              s.GetBalance == new Func <ulong>(() => 0));

            var emptyGasMeter    = new GasMeter((RuntimeObserver.Gas) 0);
            var executionContext = new ExecutionContext(new Observer(emptyGasMeter, new MemoryMeter(100_000)));

            VmExecutionResult result = this.vm.Create(
                this.state,
                contractState,
                executionContext,
                contractExecutionCode,
                null);

            CachedAssemblyPackage cachedAssembly = this.context.ContractCache.Retrieve(new uint256(codeHash));

            // Check that it's been cached, even though we ran out of gas.
            Assert.NotNull(cachedAssembly);

            // Check that the observer has been reset.
            Assert.Null(cachedAssembly.Assembly.GetObserver());

            Assert.False(result.IsSuccess);
            Assert.Equal(VmExecutionErrorKind.OutOfGas, result.Error.ErrorKind);
        }
Exemplo n.º 24
0
        public void Only_Map_Deployed_Type_Success()
        {
            var compilationResult = ContractCompiler.Compile(Code);

            var assembly = Assembly.Load(compilationResult.Compilation);

            var mapper = new ContractSchemaFactory();

            IDictionary <string, OpenApiSchema> mapped = mapper.Map(new ContractAssembly(assembly));

            Assert.Equal(11, mapped.Count);
            Assert.False(mapped.ContainsKey("SomeMethod"));
        }
Exemplo n.º 25
0
        public void ValidateAnonymousClassesDisallowed()
        {
            string adjustedSource = TestString.Replace(ReplaceCodeString, @"var test = new
            {
                Test = ""Stratis""
            };").Replace(ReplaceReferencesString, "");

            byte[] assemblyBytes = ContractCompiler.Compile(adjustedSource).Compilation;
            IContractModuleDefinition     moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;
            SmartContractValidationResult result           = this.validator.Validate(moduleDefinition.ModuleDefinition);

            Assert.False(result.IsValid);
        }
Exemplo n.º 26
0
        public void Validate_Determinism_DynamicTypeAllowed()
        {
            string adjustedSource = TestString.Replace(ReplaceCodeString, "dynamic test = 56; test = \"aString\"; ").Replace(ReplaceReferencesString, "");
            ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource);

            Assert.True(compilationResult.Success);

            byte[] assemblyBytes = compilationResult.Compilation;
            IContractModuleDefinition     moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;
            SmartContractValidationResult result           = this.validator.Validate(moduleDefinition.ModuleDefinition);

            Assert.True(result.IsValid);
        }
Exemplo n.º 27
0
        public void Validate_Determinism_Nullable_Fails()
        {
            string adjustedSource = TestString.Replace(ReplaceCodeString, "int? test = null;").Replace(ReplaceReferencesString, "");

            ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource, OptimizationLevel.Debug);

            Assert.True(compilationResult.Success);

            byte[] assemblyBytes = compilationResult.Compilation;
            IContractModuleDefinition     moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;
            SmartContractValidationResult result           = this.validator.Validate(moduleDefinition.ModuleDefinition);

            Assert.False(result.IsValid);
        }
Exemplo n.º 28
0
        public void Validate_Determinism_Fails_Array_Clone()
        {
            string code = @"
            var test = new int[25];
            var ret = test.Clone();";

            string adjustedSource = TestString.Replace(ReplaceCodeString, code).Replace(ReplaceReferencesString, "");

            byte[] assemblyBytes = ContractCompiler.Compile(adjustedSource).Compilation;
            IContractModuleDefinition     moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;
            SmartContractValidationResult result           = this.validator.Validate(moduleDefinition.ModuleDefinition);

            Assert.False(result.IsValid);
        }
Exemplo n.º 29
0
        public void Validate_Determinism_KnownBadMethodCall()
        {
            string adjustedSource = TestString.Replace(ReplaceCodeString, @"var floor = System.Math.Floor(12D);").Replace(ReplaceReferencesString, "");

            ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource);

            Assert.True(compilationResult.Success);

            byte[] assemblyBytes = compilationResult.Compilation;
            IContractModuleDefinition     moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;
            SmartContractValidationResult result           = this.validator.Validate(moduleDefinition.ModuleDefinition);

            Assert.False(result.IsValid);
        }
Exemplo n.º 30
0
        public void Validate_Determinism_GetHashCode()
        {
            string adjustedSource = TestString.Replace(ReplaceCodeString, "int hashCode = GetHashCode();").Replace(ReplaceReferencesString, "");

            ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource);

            Assert.True(compilationResult.Success);

            byte[] assemblyBytes = compilationResult.Compilation;
            IContractModuleDefinition     moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value;
            SmartContractValidationResult result           = this.validator.Validate(moduleDefinition.ModuleDefinition);

            Assert.False(result.IsValid);
        }