Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3386 from trufflesuite/rework-shim-usage
Browse files Browse the repository at this point in the history
Rework shim usage
  • Loading branch information
fainashalts authored Sep 23, 2020
2 parents f150f0a + 03c8e2b commit 4233ce8
Show file tree
Hide file tree
Showing 15 changed files with 24,110 additions and 13,795 deletions.
3 changes: 2 additions & 1 deletion packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"@gnd/graphql-tools": "^4.0.5-fix.0",
"@gnd/jsonschema2graphql": "^1.0.15",
"@truffle/artifactor": "^4.0.74",
"@truffle/workflow-compile": "^2.1.48",
"@truffle/compile-common": "^0.3.10",
"@truffle/workflow-compile": "^3.0.0",
"apollo-server": "^2.3.1",
"fse": "^4.0.1",
"graphql": "^14.0.2",
Expand Down
10 changes: 7 additions & 3 deletions packages/db/src/artifacts/json/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fse from "fs-extra";
import { shimBytecode } from "@truffle/workflow-compile/shims";
import { Shims } from "@truffle/compile-common";

const TruffleResolver = require("@truffle/resolver");

Expand All @@ -23,8 +23,12 @@ export const resolvers = {

const artifact = truffleResolver.require(name)._json;

const linkedBytecodeCreate = shimBytecode(artifact.bytecode);
const linkedBytecodeCall = shimBytecode(artifact.deployedBytecode);
const linkedBytecodeCreate = Shims.LegacyToNew.forBytecode(
artifact.bytecode
);
const linkedBytecodeCall = Shims.LegacyToNew.forBytecode(
artifact.deployedBytecode
);

const result = {
...artifact,
Expand Down
16 changes: 11 additions & 5 deletions packages/db/src/artifacts/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "path";
import { TruffleDB } from "@truffle/db";
import { shimBytecode } from "@truffle/workflow-compile/shims";
import tmp from "tmp";
import { Shims } from "@truffle/compile-common";

const fixturesDirectory = path.join(
__dirname, // db/src/test
Expand Down Expand Up @@ -159,8 +159,12 @@ describe("Artifacts queries", () => {
callBytecode
} = contract;
expect(name).toEqual(Migrations.contractName);
expect(createBytecode).toEqual(shimBytecode(Migrations.bytecode));
expect(callBytecode).toEqual(shimBytecode(Migrations.deployedBytecode));
expect(createBytecode).toEqual(
Shims.LegacyToNew.forBytecode(Migrations.bytecode)
);
expect(callBytecode).toEqual(
Shims.LegacyToNew.forBytecode(Migrations.deployedBytecode)
);

expect(processedSource).toHaveProperty("source");
const { source } = processedSource;
Expand Down Expand Up @@ -236,9 +240,11 @@ describe("Artifacts queries", () => {

const { bytecode } = callBytecode;
const { bytes, linkReferences } = bytecode;
expect(bytes).toEqual(shimBytecode(Migrations.deployedBytecode).bytes);
expect(bytes).toEqual(
Shims.LegacyToNew.forBytecode(Migrations.deployedBytecode).bytes
);
expect(linkReferences).toEqual(
shimBytecode(Migrations.deployedBytecode).linkReferences
Shims.LegacyToNew.forBytecode(Migrations.deployedBytecode).linkReferences
);
});
});
8 changes: 3 additions & 5 deletions packages/db/src/db.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { GraphQLSchema, DocumentNode, parse, execute } from "graphql";

import { schema } from "@truffle/db/data";
import { generateCompileLoad } from "@truffle/db/loaders/commands";
import {
WorkflowCompileResult,
WorkspaceRequest
} from "@truffle/db/loaders/types";
import { WorkspaceRequest } from "@truffle/db/loaders/types";
import { WorkflowCompileResult } from "@truffle/compile-common";
import { Workspace } from "@truffle/db/workspace";

interface IConfig {
Expand Down Expand Up @@ -54,6 +51,7 @@ export class TruffleDB {
});

let cur = saga.next();

while (!cur.done) {
// HACK not sure why this is necessary; TS knows we're not done, so
// cur.value should only be WorkspaceRequest (first Generator param),
Expand Down
16 changes: 9 additions & 7 deletions packages/db/src/loaders/commands/compile.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import {
WorkflowCompileResult,
CompilationData,
CompiledContract,
toIdObject,
WorkspaceRequest,
WorkspaceResponse
} from "@truffle/db/loaders/types";
import {
WorkflowCompileResult,
Compilation,
CompiledContract
} from "@truffle/compile-common/src/types";

import { generateBytecodesLoad } from "@truffle/db/loaders/resources/bytecodes";
import { generateCompilationsLoad } from "@truffle/db/loaders/resources/compilations";
Expand Down Expand Up @@ -33,7 +36,7 @@ export function* generateCompileLoad(
// start by adding loading the project resource
const project = yield* generateProjectLoad(directory);

const getCurrent = function*(name, type) {
const getCurrent = function* (name, type) {
return yield* generateProjectNameResolve(toIdObject(project), name, type);
};

Expand Down Expand Up @@ -110,10 +113,9 @@ function processResultCompilations(
.map(processResultCompilation);
}

function processResultCompilation({
sourceIndexes,
contracts
}: WorkflowCompileResult["compilations"][string]): CompilationData {
function processResultCompilation(compilation: Compilation): CompilationData {
const { sourceIndexes, contracts } = compilation;

const contractsBySourcePath: {
[sourcePath: string]: CompiledContract[];
} = contracts
Expand Down
3 changes: 1 addition & 2 deletions packages/db/src/loaders/resources/bytecodes/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {
CompilationData,
CompiledContract,
toIdObject,
LoadedBytecodes,
WorkspaceRequest,
WorkspaceResponse
} from "@truffle/db/loaders/types";

import { CompiledContract } from "@truffle/compile-common";
import { AddBytecodes } from "./add.graphql";
export { AddBytecodes };

Expand Down
2 changes: 1 addition & 1 deletion packages/db/src/loaders/resources/contracts/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
CompiledContract,
LoadedBytecodes,
IdObject,
WorkspaceRequest,
WorkspaceResponse
} from "@truffle/db/loaders/types";
import { CompiledContract } from "@truffle/compile-common";

import { AddContracts } from "./add.graphql";
export { AddContracts };
Expand Down
28 changes: 17 additions & 11 deletions packages/db/src/loaders/schema/artifactsLoader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { TruffleDB } from "@truffle/db/db";
import * as Contracts from "@truffle/workflow-compile/new";
import { ContractObject } from "@truffle/contract-schema/spec";
import * as fse from "fs-extra";
import path from "path";
import Config from "@truffle/config";
Expand All @@ -15,6 +13,11 @@ import {
AssignProjectNames,
ResolveProjectName
} from "@truffle/db/loaders/resources/projects";
import {
WorkflowCompileResult,
CompiledContract
} from "@truffle/compile-common/src/types";
import WorkflowCompile from "@truffle/workflow-compile";

type NetworkLinkObject = {
[name: string]: string;
Expand Down Expand Up @@ -84,24 +87,26 @@ export class ArtifactsLoader {
}

async load(): Promise<void> {
const result = await Contracts.compile(this.config);
const result: WorkflowCompileResult = await WorkflowCompile.compile(
this.config
);

const { project, compilations } = await this.db.loadCompilations(result);

//map contracts and contract instances to compiler
await Promise.all(
compilations.map(async ({ id }) => {
compilations.map(async ({ id }, index) => {
const {
data: {
workspace: {
compilation: { compiler, processedSources }
compilation: { processedSources }
}
}
} = await this.db.query(GetCompilation, { id });

const networks = await this.loadNetworks(
project.id,
result.compilations[compiler.name].contracts,
result.compilations[index].contracts,
this.config["artifacts_directory"],
this.config["contracts_directory"]
);
Expand All @@ -110,10 +115,11 @@ export class ArtifactsLoader {
.map(processedSource => processedSource.contracts)
.flat();

const contracts = result.compilations[
compiler.name
].contracts.map(({ contractName }) =>
processedSourceContracts.find(({ name }) => name === contractName)
let contracts = [];
result.compilations[index].contracts.map(({ contractName }) =>
contracts.push(
processedSourceContracts.find(({ name }) => name === contractName)
)
);

if (networks[0].length) {
Expand Down Expand Up @@ -164,7 +170,7 @@ export class ArtifactsLoader {

async loadNetworks(
projectId: string,
contracts: Array<ContractObject>,
contracts: Array<CompiledContract>,
artifacts: string,
workingDirectory: string
) {
Expand Down
23 changes: 14 additions & 9 deletions packages/db/src/loaders/schema/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import {
ResolveProjectName
} from "@truffle/db/loaders/resources/projects";
import { generateId } from "@truffle/db/helpers";
import * as Contracts from "@truffle/workflow-compile/new";
import Migrate from "@truffle/migrate";
import { Environment } from "@truffle/environment";
import Config from "@truffle/config";
import Ganache from "ganache-core";
import Web3 from "web3";
import * as fse from "fs-extra";
import { shimBytecode } from "@truffle/workflow-compile/shims";
import * as tmp from "tmp";
import { Shims } from "@truffle/compile-common";

let server;
const port = 8545;
Expand All @@ -35,8 +34,8 @@ afterAll(async done => {

// mocking the truffle-workflow-compile to avoid jest timing issues
// and also to keep from adding more time to Travis testing
jest.mock("@truffle/workflow-compile/new", () => ({
compile: function(config, callback) {
jest.mock("@truffle/workflow-compile", () => ({
compile: function (config, callback) {
return require(path.join(
__dirname,
"workflowCompileOutputMock",
Expand Down Expand Up @@ -350,8 +349,10 @@ describe("Compilation", () => {
sourcePath: contract["sourcePath"]
});
sourceIds.push({ id: sourceId });
const shimBytecodeObject = shimBytecode(contract["bytecode"]);
const shimCallBytecodeObject = shimBytecode(
const shimBytecodeObject = Shims.LegacyToNew.forBytecode(
contract["bytecode"]
);
const shimCallBytecodeObject = Shims.LegacyToNew.forBytecode(
contract["deployedBytecode"]
);
let bytecodeId = generateId(shimBytecodeObject);
Expand Down Expand Up @@ -620,7 +621,9 @@ describe("Compilation", () => {
}
} = await db.query(GetWorkspaceBytecode, bytecodeIds[index]);

let shimmedBytecode = shimBytecode(artifacts[index].bytecode);
let shimmedBytecode = Shims.LegacyToNew.forBytecode(
artifacts[index].bytecode
);
expect(bytes).toEqual(shimmedBytecode.bytes);
}
});
Expand Down Expand Up @@ -684,10 +687,12 @@ describe("Compilation", () => {
}
} = await db.query(GetWorkspaceContract, contractIds[index]);

const artifactsCreateBytecode = shimBytecode(artifacts[index].bytecode);
const artifactsCreateBytecode = Shims.LegacyToNew.forBytecode(
artifacts[index].bytecode
);
expect(createBytecode.bytes).toEqual(artifactsCreateBytecode.bytes);

const artifactsCallBytecode = shimBytecode(
const artifactsCallBytecode = Shims.LegacyToNew.forBytecode(
artifacts[index].deployedBytecode
);
expect(callBytecode.bytes).toEqual(artifactsCallBytecode.bytes);
Expand Down
Loading

0 comments on commit 4233ce8

Please sign in to comment.