diff --git a/packages/core/lib/commands/compile.js b/packages/core/lib/commands/compile.js index 333962179e0..fab3ae2ca16 100644 --- a/packages/core/lib/commands/compile.js +++ b/packages/core/lib/commands/compile.js @@ -101,7 +101,6 @@ const command = { { encoding: "utf8" } ); } - return WorkflowCompile.save(config, compilationOutput); }) .then(() => done()) diff --git a/packages/db/src/loaders/resources/bytecodes/index.ts b/packages/db/src/loaders/resources/bytecodes/index.ts index 9d5205bcb18..478c9d0e0d8 100644 --- a/packages/db/src/loaders/resources/bytecodes/index.ts +++ b/packages/db/src/loaders/resources/bytecodes/index.ts @@ -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 diff --git a/packages/db/src/loaders/resources/contracts/index.ts b/packages/db/src/loaders/resources/contracts/index.ts index ae63277b897..4cdfae3b0dc 100644 --- a/packages/db/src/loaders/resources/contracts/index.ts +++ b/packages/db/src/loaders/resources/contracts/index.ts @@ -18,29 +18,33 @@ export interface LoadableContract { export function* generateContractsLoad( loadableContracts: LoadableContract[] ): Load { - 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, diff --git a/packages/db/src/loaders/schema/artifactsLoader.ts b/packages/db/src/loaders/schema/artifactsLoader.ts index f0adf285ecf..be149315252 100644 --- a/packages/db/src/loaders/schema/artifactsLoader.ts +++ b/packages/db/src/loaders/schema/artifactsLoader.ts @@ -20,7 +20,6 @@ import { WorkflowCompileResult, CompiledContract } from "@truffle/compile-common/src/types"; -import WorkflowCompile from "@truffle/workflow-compile"; type NetworkLinkObject = { [name: string]: string; @@ -84,19 +83,11 @@ export class ArtifactsLoader { this.config = config; } - async load(): Promise { - const result: WorkflowCompileResult = await WorkflowCompile.compile( - this.config - ); + async load(result: WorkflowCompileResult): Promise { + 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) => { @@ -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"] ); diff --git a/packages/db/src/loaders/test/index.ts b/packages/db/src/loaders/test/index.ts index 2396971940c..25d6c4754ad 100644 --- a/packages/db/src/loaders/test/index.ts +++ b/packages/db/src/loaders/test/index.ts @@ -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, @@ -497,7 +491,7 @@ describe("Compilation", () => { }); const loader = new ArtifactsLoader(db, compilationConfig); - await loader.load(); + await loader.load(compilationResult); }, 10000); afterAll(async () => { diff --git a/packages/truffle/package.json b/packages/truffle/package.json index 8afe6ff6ca3..7430f7c31d8 100644 --- a/packages/truffle/package.json +++ b/packages/truffle/package.json @@ -57,6 +57,9 @@ "webpack-cli": "^3.3.11", "yargs": "^8.0.2" }, + "optionalDependencies": { + "@truffle/db": "^0.1.0-3" + }, "publishConfig": { "access": "public" }, diff --git a/packages/truffle/test/scenarios/commands/build.js b/packages/truffle/test/scenarios/commands/build.js index cbbc3be8a8d..1edd72de812 100644 --- a/packages/truffle/test/scenarios/commands/build.js +++ b/packages/truffle/test/scenarios/commands/build.js @@ -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, @@ -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, @@ -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" @@ -79,6 +79,6 @@ describe("truffle build [ @standalone ]", () => { ) ); } - }); + }).timeout(20000); }); }); diff --git a/packages/truffle/webpack.config.js b/packages/truffle/webpack.config.js index cfd3f10dffc..2e429ff0068 100644 --- a/packages/truffle/webpack.config.js +++ b/packages/truffle/webpack.config.js @@ -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$/ ], diff --git a/packages/workflow-compile/index.js b/packages/workflow-compile/index.js index 6a8f0d5a2b3..62814b50c36 100644 --- a/packages/workflow-compile/index.js +++ b/packages/workflow-compile/index.js @@ -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 // @@ -82,6 +84,7 @@ const WorkflowCompile = { compilers }); } + return { contracts, compilations @@ -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 }); + } } }; diff --git a/packages/workflow-compile/package.json b/packages/workflow-compile/package.json index b45aea21d6c..58d09dadbe5 100644 --- a/packages/workflow-compile/package.json +++ b/packages/workflow-compile/package.json @@ -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": {