From e70698370fb1791cad9db286cc6140de31d874ae Mon Sep 17 00:00:00 2001 From: drmathias Date: Wed, 27 Jul 2022 09:31:27 +0100 Subject: [PATCH 1/2] Support two levels of smart contract inheritance --- .../Loader/ContractAssemblyTests.cs | 29 +++++++++++++++++++ .../Loader/ContractAssembly.cs | 10 +++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/Stratis.SmartContracts.CLR.Tests/Loader/ContractAssemblyTests.cs b/src/Stratis.SmartContracts.CLR.Tests/Loader/ContractAssemblyTests.cs index 500aac96d6..b417e4558d 100644 --- a/src/Stratis.SmartContracts.CLR.Tests/Loader/ContractAssemblyTests.cs +++ b/src/Stratis.SmartContracts.CLR.Tests/Loader/ContractAssemblyTests.cs @@ -128,5 +128,34 @@ public Test(ISmartContractState state) Assert.NotNull(type); Assert.Equal("Test", type.Name); } + + [Fact] + public void GetDeployedType_TwoLevelsOfInheritance_CorrectType() + { + var code = @" +using Stratis.SmartContracts; + +public class TypeOne : SmartContract +{ + public TypeOne(ISmartContractState state) : base(state) {} +} + +[Deploy] +public class TypeTwo : TypeOne +{ + 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("TypeTwo", type.Name); + } } } diff --git a/src/Stratis.SmartContracts.CLR/Loader/ContractAssembly.cs b/src/Stratis.SmartContracts.CLR/Loader/ContractAssembly.cs index 4e9103776f..0c24c80fa1 100644 --- a/src/Stratis.SmartContracts.CLR/Loader/ContractAssembly.cs +++ b/src/Stratis.SmartContracts.CLR/Loader/ContractAssembly.cs @@ -49,12 +49,16 @@ private Type GetDeployedType() x.CustomAttributes.Any(y => y.AttributeType == typeof(DeployAttribute))); } - public static bool IsContractType(Type typeDefinition) + private static bool IsContractType(Type typeDefinition) { return typeDefinition.IsClass && !typeDefinition.IsAbstract && - typeDefinition.BaseType != null && - typeDefinition.BaseType == typeof(SmartContract); + InheritsFromType(typeDefinition, typeof(SmartContract)); + } + + private static bool InheritsFromType(Type subject, Type predicate) + { + return subject != null && (subject.BaseType == predicate || InheritsFromType(subject.BaseType, predicate)); } private Type GetObserverType() From ff40c32fc82e463c7fd4819bec7931faba14cbb3 Mon Sep 17 00:00:00 2001 From: drmathias Date: Thu, 28 Jul 2022 15:08:18 +0100 Subject: [PATCH 2/2] Use `IsSubclassOf` and amend test naming conventions --- .../Loader/ContractAssemblyTests.cs | 10 +++++----- .../Loader/ContractAssembly.cs | 7 +------ 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/Stratis.SmartContracts.CLR.Tests/Loader/ContractAssemblyTests.cs b/src/Stratis.SmartContracts.CLR.Tests/Loader/ContractAssemblyTests.cs index b417e4558d..5082ed2092 100644 --- a/src/Stratis.SmartContracts.CLR.Tests/Loader/ContractAssemblyTests.cs +++ b/src/Stratis.SmartContracts.CLR.Tests/Loader/ContractAssemblyTests.cs @@ -135,15 +135,15 @@ public void GetDeployedType_TwoLevelsOfInheritance_CorrectType() var code = @" using Stratis.SmartContracts; -public class TypeOne : SmartContract +public class BaseType : SmartContract { - public TypeOne(ISmartContractState state) : base(state) {} + public BaseType(ISmartContractState state) : base(state) {} } [Deploy] -public class TypeTwo : TypeOne +public class InheritedType : BaseType { - public TypeTwo(ISmartContractState state) : base(state) {} + public InheritedType(ISmartContractState state) : base(state) {} } "; var compilation = ContractCompiler.Compile(code); @@ -155,7 +155,7 @@ public TypeTwo(ISmartContractState state) : base(state) {} var type = contractAssembly.DeployedType; Assert.NotNull(type); - Assert.Equal("TypeTwo", type.Name); + Assert.Equal("InheritedType", type.Name); } } } diff --git a/src/Stratis.SmartContracts.CLR/Loader/ContractAssembly.cs b/src/Stratis.SmartContracts.CLR/Loader/ContractAssembly.cs index 0c24c80fa1..908261d5e9 100644 --- a/src/Stratis.SmartContracts.CLR/Loader/ContractAssembly.cs +++ b/src/Stratis.SmartContracts.CLR/Loader/ContractAssembly.cs @@ -53,12 +53,7 @@ private static bool IsContractType(Type typeDefinition) { return typeDefinition.IsClass && !typeDefinition.IsAbstract && - InheritsFromType(typeDefinition, typeof(SmartContract)); - } - - private static bool InheritsFromType(Type subject, Type predicate) - { - return subject != null && (subject.BaseType == predicate || InheritsFromType(subject.BaseType, predicate)); + typeDefinition.IsSubclassOf(typeof(SmartContract)); } private Type GetObserverType()