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 #3430 from trufflesuite/db/load-on-compile
Browse files Browse the repository at this point in the history
Db/load on compile
  • Loading branch information
fainashalts authored and g. nicholas d'andrea committed Oct 27, 2020
2 parents 4db36c3 + d964049 commit 2919134
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 64 deletions.
1 change: 0 additions & 1 deletion packages/core/lib/commands/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ const command = {
{ encoding: "utf8" }
);
}

return WorkflowCompile.save(config, compilationOutput);
})
.then(() => done())
Expand Down
22 changes: 12 additions & 10 deletions packages/db/src/loaders/resources/bytecodes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ export function* generateBytecodesLoad(
.flat();

// now pluck all the create/call bytecodes and concat them (creates first)
const { createBytecodes, callBytecodes } = flattenedContracts.reduce(
(
{ createBytecodes, callBytecodes },
{ deployedBytecode: callBytecode, bytecode: createBytecode }
) => ({
createBytecodes: [...createBytecodes, createBytecode],
callBytecodes: [...callBytecodes, callBytecode]
}),
{ createBytecodes: [], callBytecodes: [] }
);
const { createBytecodes, callBytecodes } = flattenedContracts
.filter(({ bytecode }) => bytecode.bytes !== "")
.reduce(
(
{ createBytecodes, callBytecodes },
{ deployedBytecode: callBytecode, bytecode: createBytecode }
) => ({
createBytecodes: [...createBytecodes, createBytecode],
callBytecodes: [...callBytecodes, callBytecode]
}),
{ createBytecodes: [], callBytecodes: [] }
);
const bytecodes = [...createBytecodes, ...callBytecodes];

// submit
Expand Down
50 changes: 27 additions & 23 deletions packages/db/src/loaders/resources/contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,33 @@ export interface LoadableContract {
export function* generateContractsLoad(
loadableContracts: LoadableContract[]
): Load<DataModel.Contract[], "contractsAdd"> {
const contracts = loadableContracts.map(loadableContract => {
const {
contract: { contractName: name, abi: abiObject },
path: { sourceIndex, contractIndex },
bytecodes,
compilation
} = loadableContract;

const { createBytecode, callBytecode } = bytecodes.sources[
sourceIndex
].contracts[contractIndex];

return {
name,
abi: {
json: JSON.stringify(abiObject)
},
compilation,
processedSource: { index: sourceIndex },
createBytecode: createBytecode,
callBytecode: callBytecode
};
});
// we filter out contracts whose bytecode bytes are empty because these are
// either abstract contracts or interfaces, and do not belong in the db
const contracts = loadableContracts
.filter(({ contract }) => contract.bytecode.bytes !== "")
.map(loadableContract => {
const {
contract: { contractName: name, abi: abiObject },
path: { sourceIndex, contractIndex },
bytecodes,
compilation
} = loadableContract;

const { createBytecode, callBytecode } = bytecodes.sources[
sourceIndex
].contracts[contractIndex];

return {
name,
abi: {
json: JSON.stringify(abiObject)
},
compilation,
processedSource: { index: sourceIndex },
createBytecode: createBytecode,
callBytecode: callBytecode
};
});

const result = yield {
request: AddContracts,
Expand Down
15 changes: 3 additions & 12 deletions packages/db/src/loaders/schema/artifactsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ 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,19 +83,11 @@ export class ArtifactsLoader {
this.config = config;
}

async load(): Promise<void> {
const result: WorkflowCompileResult = await WorkflowCompile.compile(
this.config
);
async load(result: WorkflowCompileResult): Promise<void> {
const { compilations } = await this.db.loadCompilations(result, { names: true });

const project = await this.db.loadProject();

// third parameter in loadCompilation is for whether or not we need
// to update nameRecords (i.e. is this happening in test)
const { compilations } = await this.db.loadCompilations(result, {
names: true
});

//map contracts and contract instances to compiler
await Promise.all(
compilations.map(async ({ id }, index) => {
Expand All @@ -109,7 +100,7 @@ export class ArtifactsLoader {
const networks = await this.loadNetworks(
project.id,
result.compilations[index].contracts,
this.config["artifacts_directory"],
this.config["contracts_build_directory"],
this.config["contracts_directory"]
);

Expand Down
18 changes: 6 additions & 12 deletions packages/db/src/loaders/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,11 @@ afterAll(async done => {
setTimeout(() => server.close(done), 500);
});

// 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", () => ({
compile: function () {
return require(path.join(
__dirname,
"workflowCompileOutputMock",
"compilationOutput.json"
));
}
}));
const compilationResult = require(path.join(
__dirname,
"workflowCompileOutputMock",
"compilationOutput.json"
));

const fixturesDirectory = path.join(
__dirname,
Expand Down Expand Up @@ -497,7 +491,7 @@ describe("Compilation", () => {
});

const loader = new ArtifactsLoader(db, compilationConfig);
await loader.load();
await loader.load(compilationResult);
}, 10000);

afterAll(async () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/truffle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
"webpack-cli": "^3.3.11",
"yargs": "^8.0.2"
},
"optionalDependencies": {
"@truffle/db": "^0.1.0-3"
},
"publishConfig": {
"access": "public"
},
Expand Down
10 changes: 5 additions & 5 deletions packages/truffle/test/scenarios/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("truffle build [ @standalone ]", () => {
let config, project;

describe("when there is no build script in config", () => {
beforeEach("set up sandbox", function() {
beforeEach("set up sandbox", function () {
this.timeout(10000);
project = path.join(
__dirname,
Expand Down Expand Up @@ -37,7 +37,7 @@ describe("truffle build [ @standalone ]", () => {
});

describe("when there is a proper build config", () => {
beforeEach("set up sandbox", function() {
beforeEach("set up sandbox", function () {
this.timeout(10000);
project = path.join(
__dirname,
Expand All @@ -56,8 +56,8 @@ describe("truffle build [ @standalone ]", () => {
});

describe("when there is an object in the build config", () => {
beforeEach("set up sandbox", function() {
this.timeout(10000);
beforeEach("set up sandbox", function () {
this.timeout(12000);
project = path.join(
__dirname,
"../../sources/build/projectWithObjectInBuildScript"
Expand All @@ -79,6 +79,6 @@ describe("truffle build [ @standalone ]", () => {
)
);
}
});
}).timeout(20000);
});
});
1 change: 1 addition & 0 deletions packages/truffle/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ module.exports = {
// module that's a dependency of Truffle instead.
/^original-require$/,
/^mocha$/,
/^@truffle\/db/,
// this is the commands portion shared by cli.js and console-child.js
/^\.\/commands.bundled.js$/
],
Expand Down
10 changes: 9 additions & 1 deletion packages/workflow-compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const SUPPORTED_COMPILERS = {
external: require("@truffle/external-compile").Compile
};

const { TruffleDB } = require("@truffle/db");

async function compile(config) {
// determine compiler(s) to use
//
Expand Down Expand Up @@ -82,6 +84,7 @@ const WorkflowCompile = {
compilers
});
}

return {
contracts,
compilations
Expand All @@ -101,13 +104,18 @@ const WorkflowCompile = {
reportCompilationFinished,
reportNothingToCompile,

async save(options, { contracts, _compilations }) {
async save(options, { contracts, compilations }) {
const config = prepareConfig(options);

await fse.ensureDir(config.contracts_build_directory);

const artifacts = contracts.map(Shims.NewToLegacy.forContract);
await config.artifactor.saveAll(artifacts);

if (options.db && options.db.enabled === true && contracts.length > 0) {
const db = new TruffleDB(config);
await db.loadCompilations({ contracts, compilations }, { names: true });
}
}
};

Expand Down
1 change: 1 addition & 0 deletions packages/workflow-compile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@truffle/expect": "^0.0.15",
"@truffle/external-compile": "^2.0.3",
"@truffle/resolver": "^6.0.24",
"@truffle/db": "^0.1.0-3",
"fs-extra": "^8.1.0"
},
"devDependencies": {
Expand Down

0 comments on commit 2919134

Please sign in to comment.