From c54bd33f47682b9c1d854f48554af849b383bb80 Mon Sep 17 00:00:00 2001 From: Kevin Ho Date: Thu, 2 Apr 2020 15:26:01 -0400 Subject: [PATCH 1/2] transpiler null-checking bytecode --- packages/solc-transpiler/src/compiler.ts | 82 ++++++++++++------------ 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/packages/solc-transpiler/src/compiler.ts b/packages/solc-transpiler/src/compiler.ts index 101cec3afd9f..d6042920c959 100644 --- a/packages/solc-transpiler/src/compiler.ts +++ b/packages/solc-transpiler/src/compiler.ts @@ -79,50 +79,52 @@ export const compile = (configJsonString: string, callbacks?: any): string => { for (const [filename, fileJson] of Object.entries(res.contracts)) { log.debug(`Transpiling file: ${filename}`) for (const [contractName, contractJson] of Object.entries(fileJson)) { - // Library links in bytecode strings have invalid hex: they are of the form __$asdfasdf$__. - // Because __$ is not a valid hex string, we replace with a valid hex string during transpilation, - // storing the links re-substituting the __$* strings afterwards - const originalRefStrings = getOriginalLinkRefStringsAndSubstituteValidHex( - contractJson - ) - log.debug( - `Transpiling contract: ${contractName} with valid hex strings for link placeholders.` - ) - const output = transpileContract( - transpiler, - contractJson, - filename, - contractName - ) - log.debug(`Transpiled contract ${contractName}.`) + if (!!contractJson && !!contractJson.evm && !!contractJson.evm.bytecode && !!contractJson.evm.deployedBytecode) { + // Library links in bytecode strings have invalid hex: they are of the form __$asdfasdf$__. + // Because __$ is not a valid hex string, we replace with a valid hex string during transpilation, + // storing the links re-substituting the __$* strings afterwards + const originalRefStrings = getOriginalLinkRefStringsAndSubstituteValidHex( + contractJson + ) + log.debug( + `Transpiling contract: ${contractName} with valid hex strings for link placeholders.` + ) + const output = transpileContract( + transpiler, + contractJson, + filename, + contractName + ) + log.debug(`Transpiled contract ${contractName}.`) - res.contracts[filename][contractName].evm.bytecode.object = remove0x( - output.bytecode || '' - ) - res.contracts[filename][ - contractName - ].evm.deployedBytecode.object = remove0x(output.deployedBytecode || '') - res.contracts[filename][contractName].evm.bytecode.object = remove0x( - output.bytecode || '' - ) - res.contracts[filename][ - contractName - ].evm.deployedBytecode.object = remove0x(output.deployedBytecode || '') + res.contracts[filename][contractName].evm.bytecode.object = remove0x( + output.bytecode || '' + ) + res.contracts[filename][ + contractName + ].evm.deployedBytecode.object = remove0x(output.deployedBytecode || '') + res.contracts[filename][contractName].evm.bytecode.object = remove0x( + output.bytecode || '' + ) + res.contracts[filename][ + contractName + ].evm.deployedBytecode.object = remove0x(output.deployedBytecode || '') - log.debug( - `Updating links for all libraries by putting back original invalid hex (__$...$__) strings and updating link .start's.` - ) - updateLinkRefsAndSubstituteOriginalStrings( - res.contracts[filename][contractName], - originalRefStrings - ) + log.debug( + `Updating links for all libraries by putting back original invalid hex (__$...$__) strings and updating link .start's.` + ) + updateLinkRefsAndSubstituteOriginalStrings( + res.contracts[filename][contractName], + originalRefStrings + ) - if (!!output.errors) { - if (!res.errors) { - res.errors = [] - } + if (!!output.errors) { + if (!res.errors) { + res.errors = [] + } - res.errors.push(...output.errors) + res.errors.push(...output.errors) + } } } } From 014573c364d3ca684a81749ed8f2a34d68fafa5c Mon Sep 17 00:00:00 2001 From: Kevin Ho Date: Thu, 2 Apr 2020 15:47:22 -0400 Subject: [PATCH 2/2] lint, use continue in transpiler --- packages/solc-transpiler/src/compiler.ts | 90 +++++++++++++----------- 1 file changed, 48 insertions(+), 42 deletions(-) diff --git a/packages/solc-transpiler/src/compiler.ts b/packages/solc-transpiler/src/compiler.ts index d6042920c959..6232f0ba687a 100644 --- a/packages/solc-transpiler/src/compiler.ts +++ b/packages/solc-transpiler/src/compiler.ts @@ -79,52 +79,58 @@ export const compile = (configJsonString: string, callbacks?: any): string => { for (const [filename, fileJson] of Object.entries(res.contracts)) { log.debug(`Transpiling file: ${filename}`) for (const [contractName, contractJson] of Object.entries(fileJson)) { - if (!!contractJson && !!contractJson.evm && !!contractJson.evm.bytecode && !!contractJson.evm.deployedBytecode) { - // Library links in bytecode strings have invalid hex: they are of the form __$asdfasdf$__. - // Because __$ is not a valid hex string, we replace with a valid hex string during transpilation, - // storing the links re-substituting the __$* strings afterwards - const originalRefStrings = getOriginalLinkRefStringsAndSubstituteValidHex( - contractJson - ) - log.debug( - `Transpiling contract: ${contractName} with valid hex strings for link placeholders.` - ) - const output = transpileContract( - transpiler, - contractJson, - filename, - contractName - ) - log.debug(`Transpiled contract ${contractName}.`) - - res.contracts[filename][contractName].evm.bytecode.object = remove0x( - output.bytecode || '' - ) - res.contracts[filename][ - contractName - ].evm.deployedBytecode.object = remove0x(output.deployedBytecode || '') - res.contracts[filename][contractName].evm.bytecode.object = remove0x( - output.bytecode || '' - ) - res.contracts[filename][ - contractName - ].evm.deployedBytecode.object = remove0x(output.deployedBytecode || '') + if ( + !contractJson || + !contractJson.evm || + !contractJson.evm.bytecode || + !contractJson.evm.deployedBytecode + ) { + continue + } + // Library links in bytecode strings have invalid hex: they are of the form __$asdfasdf$__. + // Because __$ is not a valid hex string, we replace with a valid hex string during transpilation, + // storing the links re-substituting the __$* strings afterwards + const originalRefStrings = getOriginalLinkRefStringsAndSubstituteValidHex( + contractJson + ) + log.debug( + `Transpiling contract: ${contractName} with valid hex strings for link placeholders.` + ) + const output = transpileContract( + transpiler, + contractJson, + filename, + contractName + ) + log.debug(`Transpiled contract ${contractName}.`) - log.debug( - `Updating links for all libraries by putting back original invalid hex (__$...$__) strings and updating link .start's.` - ) - updateLinkRefsAndSubstituteOriginalStrings( - res.contracts[filename][contractName], - originalRefStrings - ) + res.contracts[filename][contractName].evm.bytecode.object = remove0x( + output.bytecode || '' + ) + res.contracts[filename][ + contractName + ].evm.deployedBytecode.object = remove0x(output.deployedBytecode || '') + res.contracts[filename][contractName].evm.bytecode.object = remove0x( + output.bytecode || '' + ) + res.contracts[filename][ + contractName + ].evm.deployedBytecode.object = remove0x(output.deployedBytecode || '') - if (!!output.errors) { - if (!res.errors) { - res.errors = [] - } + log.debug( + `Updating links for all libraries by putting back original invalid hex (__$...$__) strings and updating link .start's.` + ) + updateLinkRefsAndSubstituteOriginalStrings( + res.contracts[filename][contractName], + originalRefStrings + ) - res.errors.push(...output.errors) + if (!!output.errors) { + if (!res.errors) { + res.errors = [] } + + res.errors.push(...output.errors) } } }