aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-cov/src/utils.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-06-12 01:54:59 +0800
committerFabio Berger <me@fabioberger.com>2018-06-12 01:54:59 +0800
commit60f5a52964d6965d35eb3c3cb15abc8471de6fd6 (patch)
tree3912c687aa7e3eb9891d1f4d5a46d516805a4503 /packages/sol-cov/src/utils.ts
parent89b7b56a2cb38d95f65067e5493e8135cb1e7e98 (diff)
parent7e5866ce3fffc67633d3ab84cc3d2a6abdd22ce5 (diff)
downloaddexon-sol-tools-60f5a52964d6965d35eb3c3cb15abc8471de6fd6.tar
dexon-sol-tools-60f5a52964d6965d35eb3c3cb15abc8471de6fd6.tar.gz
dexon-sol-tools-60f5a52964d6965d35eb3c3cb15abc8471de6fd6.tar.bz2
dexon-sol-tools-60f5a52964d6965d35eb3c3cb15abc8471de6fd6.tar.lz
dexon-sol-tools-60f5a52964d6965d35eb3c3cb15abc8471de6fd6.tar.xz
dexon-sol-tools-60f5a52964d6965d35eb3c3cb15abc8471de6fd6.tar.zst
dexon-sol-tools-60f5a52964d6965d35eb3c3cb15abc8471de6fd6.zip
Merge branch 'v2-prototype' into fix/contract-wrappers/exchangeTransferSimulator
* v2-prototype: Fix a bug in SolCompilerArtifacts adapter config overriding Increase timeout for contract migrations Remove some copy-paste code Await transactions in migrations Fix typos Await transactions in migrations Await fake transactions Fix a typo Implement SolidityProfiler & adapt sol-cov to work with Geth # Conflicts: # packages/migrations/CHANGELOG.json
Diffstat (limited to 'packages/sol-cov/src/utils.ts')
-rw-r--r--packages/sol-cov/src/utils.ts30
1 files changed, 29 insertions, 1 deletions
diff --git a/packages/sol-cov/src/utils.ts b/packages/sol-cov/src/utils.ts
index d970c42ee..0b32df02e 100644
--- a/packages/sol-cov/src/utils.ts
+++ b/packages/sol-cov/src/utils.ts
@@ -1,4 +1,6 @@
-import { LineColumn, SingleFileSourceRange } from './types';
+import * as _ from 'lodash';
+
+import { ContractData, LineColumn, SingleFileSourceRange } from './types';
export const utils = {
compareLineColumn(lhs: LineColumn, rhs: LineColumn): number {
@@ -14,4 +16,30 @@ export const utils = {
utils.compareLineColumn(childRange.end, parentRange.end) <= 0
);
},
+ bytecodeToBytecodeRegex(bytecode: string): string {
+ const bytecodeRegex = bytecode
+ // Library linking placeholder: __ConvertLib____________________________
+ .replace(/_.*_/, '.*')
+ // Last 86 characters is solidity compiler metadata that's different between compilations
+ .replace(/.{86}$/, '')
+ // Libraries contain their own address at the beginning of the code and it's impossible to know it in advance
+ .replace(/^0x730000000000000000000000000000000000000000/, '0x73........................................');
+ // HACK: Node regexes can't be longer that 32767 characters. Contracts bytecode can. We just truncate the regexes. It's safe in practice.
+ const MAX_REGEX_LENGTH = 32767;
+ const truncatedBytecodeRegex = bytecodeRegex.slice(0, MAX_REGEX_LENGTH);
+ return truncatedBytecodeRegex;
+ },
+ getContractDataIfExists(contractsData: ContractData[], bytecode: string): ContractData | undefined {
+ if (!bytecode.startsWith('0x')) {
+ throw new Error(`0x hex prefix missing: ${bytecode}`);
+ }
+ const contractData = _.find(contractsData, contractDataCandidate => {
+ const bytecodeRegex = utils.bytecodeToBytecodeRegex(contractDataCandidate.bytecode);
+ const runtimeBytecodeRegex = utils.bytecodeToBytecodeRegex(contractDataCandidate.runtimeBytecode);
+ // We use that function to find by bytecode or runtimeBytecode. Those are quasi-random strings so
+ // collisions are practically impossible and it allows us to reuse that code
+ return !_.isNull(bytecode.match(bytecodeRegex)) || !_.isNull(bytecode.match(runtimeBytecodeRegex));
+ });
+ return contractData;
+ },
};