From 737f9df1bb433c2c108c9799358a0babb9fead05 Mon Sep 17 00:00:00 2001 From: aerkol Date: Wed, 20 Nov 2019 10:43:10 +0800 Subject: [PATCH] Patch contracts at build time --- .gitignore | 3 +++ AElf.Contract.Tools.targets | 13 +++++++++++-- .../AElf.Contracts.Deployer.csproj | 10 ++++++++++ .../ContractsDeployer.cs | 3 +-- src/AElf.Contracts.Deployer/Program.cs | 18 ++++++++++++++++++ .../ContractAuditorTests.cs | 2 +- 6 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/AElf.Contracts.Deployer/Program.cs diff --git a/.gitignore b/.gitignore index ae61b75d94..66940f27a3 100644 --- a/.gitignore +++ b/.gitignore @@ -298,3 +298,6 @@ coverage*.xml *.gz *.zip *.zlo + +# Contract patcher dlls +scripts/patcher/* diff --git a/AElf.Contract.Tools.targets b/AElf.Contract.Tools.targets index d23e5d5306..72191b11e0 100644 --- a/AElf.Contract.Tools.targets +++ b/AElf.Contract.Tools.targets @@ -2,6 +2,7 @@ ..\..\protobuf + ..\..\scripts\patcher @@ -55,7 +56,15 @@ - - + + + + + diff --git a/src/AElf.Contracts.Deployer/AElf.Contracts.Deployer.csproj b/src/AElf.Contracts.Deployer/AElf.Contracts.Deployer.csproj index c3bb813fe8..91ec075a5e 100644 --- a/src/AElf.Contracts.Deployer/AElf.Contracts.Deployer.csproj +++ b/src/AElf.Contracts.Deployer/AElf.Contracts.Deployer.csproj @@ -4,6 +4,7 @@ AElf.Contracts.Deployer true netcoreapp3.0 + Exe TRACE;UNIT_TEST @@ -16,4 +17,13 @@ + + + + + + + + + diff --git a/src/AElf.Contracts.Deployer/ContractsDeployer.cs b/src/AElf.Contracts.Deployer/ContractsDeployer.cs index 79c663b96a..c7e8965021 100644 --- a/src/AElf.Contracts.Deployer/ContractsDeployer.cs +++ b/src/AElf.Contracts.Deployer/ContractsDeployer.cs @@ -26,8 +26,7 @@ private static byte[] GetCode(string dllName, string contractDir) ? Path.Combine(contractDir, $"{dllName}.dll") : Assembly.Load(dllName).Location; - return ContractPatcher.Patch(File.ReadAllBytes(dllPath)); - //File.ReadAllBytes(dllPath); + return File.ReadAllBytes(dllPath); } private static IEnumerable GetContractNames(Assembly assembly) diff --git a/src/AElf.Contracts.Deployer/Program.cs b/src/AElf.Contracts.Deployer/Program.cs new file mode 100644 index 0000000000..acc2aaf543 --- /dev/null +++ b/src/AElf.Contracts.Deployer/Program.cs @@ -0,0 +1,18 @@ +using System; +using System.IO; +using AElf.CSharp.CodeOps; + +namespace AElf.Contracts.Deployer +{ + class Program + { + static void Main(string[] args) + { + var sourceDllPath = args[0]; + var code = File.ReadAllBytes(sourceDllPath); + + // Overwrite + File.WriteAllBytes(sourceDllPath, ContractPatcher.Patch(code)); + } + } +} diff --git a/test/AElf.Runtime.CSharp.Tests/ContractAuditorTests.cs b/test/AElf.Runtime.CSharp.Tests/ContractAuditorTests.cs index 5cc0eec567..166338fa5a 100644 --- a/test/AElf.Runtime.CSharp.Tests/ContractAuditorTests.cs +++ b/test/AElf.Runtime.CSharp.Tests/ContractAuditorTests.cs @@ -74,7 +74,7 @@ public void CheckSystemContracts_AllShouldPass() // Load the DLL's from contracts folder to prevent codecov injection foreach (var contractPath in _contracts.Select(c => _contractDllDir + c.Module.ToString())) { - Should.NotThrow(()=>_auditorFixture.Audit(ContractPatcher.Patch(ReadCode(contractPath)))); + Should.NotThrow(()=>_auditorFixture.Audit(ReadCode(contractPath))); } }