Skip to content

Commit

Permalink
Patch contracts at build time
Browse files Browse the repository at this point in the history
  • Loading branch information
aerkol committed Nov 20, 2019
1 parent effabf9 commit 737f9df
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,6 @@ coverage*.xml
*.gz
*.zip
*.zlo

# Contract patcher dlls
scripts/patcher/*
13 changes: 11 additions & 2 deletions AElf.Contract.Tools.targets
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProtoBaseDir>..\..\protobuf</ProtoBaseDir>
<PatcherDir>..\..\scripts\patcher</PatcherDir>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -55,7 +56,15 @@
<Compile Include="Protobuf/Generated/*.cs"/>
</ItemGroup>
</Target>
<Target Name="CopyNonInjectedContractDll" AfterTargets="AfterBuild">
<Copy Condition="'$(IsContract)' != '' AND '$(IsContract)'" SourceFiles="$(TargetDir)$(TargetName).dll" DestinationFolder="../../test/AElf.Runtime.CSharp.Tests/contracts" />
<Target Name="PatchContractCode" AfterTargets="AfterBuild">
<Exec Condition="'$(IsContract)' != '' AND '$(IsContract)'"
WorkingDirectory = "$(PatcherDir)"
Command="dotnet AElf.Contracts.Deployer.dll $(TargetDir)$(TargetName).dll" />
</Target>
<Target Name="CopyNonCodeCovContractDll" AfterTargets="PatchContractCode">
<Copy Condition="'$(IsContract)' != '' AND '$(IsContract)'"
SourceFiles="$(TargetDir)$(TargetName).dll"
DestinationFolder="../../test/AElf.Runtime.CSharp.Tests/contracts"
SkipUnchangedFiles="true" />
</Target>
</Project>
10 changes: 10 additions & 0 deletions src/AElf.Contracts.Deployer/AElf.Contracts.Deployer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PackageId>AElf.Contracts.Deployer</PackageId>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<TargetFramework>netcoreapp3.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DefineConstants>TRACE;UNIT_TEST</DefineConstants>
Expand All @@ -16,4 +17,13 @@
<ProjectReference Include="..\AElf.Runtime.CSharp.Core\AElf.Runtime.CSharp.Core.csproj" />
<ProjectReference Include="..\AElf.Types\AElf.Types.csproj" />
</ItemGroup>

<ItemGroup>
<PatcherDlls Include="$(TargetDir)\*"/>
</ItemGroup>

<Target Name="CopyDlls" AfterTargets="AfterBuild">
<MakeDir Directories="$(PatcherDir)" Condition="!Exists('$(PatcherDir)')" />
<Copy SourceFiles="@(PatcherDlls)" DestinationFolder="$(PatcherDir)" SkipUnchangedFiles="true" />
</Target>
</Project>
3 changes: 1 addition & 2 deletions src/AElf.Contracts.Deployer/ContractsDeployer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> GetContractNames(Assembly assembly)
Expand Down
18 changes: 18 additions & 0 deletions src/AElf.Contracts.Deployer/Program.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
2 changes: 1 addition & 1 deletion test/AElf.Runtime.CSharp.Tests/ContractAuditorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}

Expand Down

0 comments on commit 737f9df

Please sign in to comment.