Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase line width in Prettier config #26

Merged
merged 1 commit into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
increase line width in prettier config
  • Loading branch information
frangio committed Jul 17, 2020
commit 42392bf4b85033ee77810f807779be6b948530a5
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ module.exports = {
singleQuote: true,
arrowParens: 'avoid',
trailingComma: 'all',
printWidth: 120,
};
34 changes: 6 additions & 28 deletions packages/core/src/eip-1967.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,21 @@ import BN from 'bn.js';

import { EthereumProvider, getStorageAt } from './provider';

export async function getAdminAddress(
provider: EthereumProvider,
address: string,
): Promise<string> {
export async function getAdminAddress(provider: EthereumProvider, address: string): Promise<string> {
const ADMIN_LABEL = 'eip1967.proxy.admin';
const FALLBACK_ADMIN_LABEL = 'org.zeppelinos.proxy.admin';
const storage = await getEip1967Storage(
provider,
address,
ADMIN_LABEL,
FALLBACK_ADMIN_LABEL,
);
const storage = await getEip1967Storage(provider, address, ADMIN_LABEL, FALLBACK_ADMIN_LABEL);

return toChecksumAddress(storage);
}

export async function getImplementationAddress(
provider: EthereumProvider,
address: string,
): Promise<string> {
export async function getImplementationAddress(provider: EthereumProvider, address: string): Promise<string> {
const IMPLEMENTATION_LABEL = 'eip1967.proxy.implementation';
const FALLBACK_IMPLEMENTATION_LABEL = 'org.zeppelinos.proxy.implementation';
const storage = await getEip1967Storage(
provider,
address,
IMPLEMENTATION_LABEL,
FALLBACK_IMPLEMENTATION_LABEL,
);
const storage = await getEip1967Storage(provider, address, IMPLEMENTATION_LABEL, FALLBACK_IMPLEMENTATION_LABEL);

if (isEmptySlot(storage)) {
throw new Error(
`Contract at ${address} doesn't look like an EIP 1967 proxy`,
);
throw new Error(`Contract at ${address} doesn't look like an EIP 1967 proxy`);
}

return toChecksumAddress(storage);
Expand All @@ -50,11 +32,7 @@ async function getEip1967Storage(
let storage = await getStorageAt(provider, address, toEip1967Hash(slot));

if (isEmptySlot(storage)) {
storage = await getStorageAt(
provider,
address,
toFallbackEip1967Hash(fallbackSlot),
);
storage = await getStorageAt(provider, address, toFallbackEip1967Hash(fallbackSlot));
}

return storage;
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export abstract class UpgradesError extends Error {
}

[util.inspect.custom](): string {
return (
chalk.red.bold('Error:') + ' ' + this.message + '\n\n' + this.details()
);
return chalk.red.bold('Error:') + ' ' + this.message + '\n\n' + this.details();
}
}
7 changes: 2 additions & 5 deletions packages/core/src/impl-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import { fetchOrDeploy } from './impl-store';
const rimraf = util.promisify(rimrafAsync);

test.before(async () => {
process.chdir(
await fs.mkdtemp(path.join(os.tmpdir(), 'upgrades-core-test-')),
);
process.chdir(await fs.mkdtemp(path.join(os.tmpdir(), 'upgrades-core-test-')));
});

test.after(async () => {
Expand All @@ -30,8 +28,7 @@ test('deploys on cache miss', async t => {

test('reuses on cache hit', async t => {
const provider = stubProvider();
const cachedDeploy = () =>
fetchOrDeploy('version1', provider, provider.deploy);
const cachedDeploy = () => fetchOrDeploy('version1', provider, provider.deploy);
const address1 = await cachedDeploy();
const address2 = await cachedDeploy();
t.is(provider.deployCount, 1);
Expand Down
19 changes: 4 additions & 15 deletions packages/core/src/levenshtein.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
export function levenshtein<T, A>(
a: T[],
b: T[],
match: Match<T, A>,
): Operation<T, A>[] {
export function levenshtein<T, A>(a: T[], b: T[], match: Match<T, A>): Operation<T, A>[] {
const matrix = buildMatrix(a, b, (a, b) => match(a, b) === 'equal');
return walkMatrix(matrix, a, b, match);
}
Expand All @@ -18,10 +14,8 @@ function buildMatrix<T>(a: T[], b: T[], eq: Equal<T>): number[][] {
const matrix: number[][] = new Array(a.length + 1);

type CostFunction = (i: number, j: number) => number;
const insertionCost: CostFunction = (i, j) =>
j > a.length ? 0 : INSERTION_COST;
const substitutionCost: CostFunction = (i, j) =>
eq(a[i - 1], b[j - 1]) ? 0 : SUBSTITUTION_COST;
const insertionCost: CostFunction = (i, j) => (j > a.length ? 0 : INSERTION_COST);
const substitutionCost: CostFunction = (i, j) => (eq(a[i - 1], b[j - 1]) ? 0 : SUBSTITUTION_COST);
const deletionCost: CostFunction = () => DELETION_COST;

// increment along the first column of each row
Expand Down Expand Up @@ -58,12 +52,7 @@ export interface Operation<T, A> {
}

// Walks an edit distance matrix, returning the sequence of operations performed
function walkMatrix<T, A>(
matrix: number[][],
a: T[],
b: T[],
match: Match<T, A>,
): Operation<T, A>[] {
function walkMatrix<T, A>(matrix: number[][], a: T[], b: T[], match: Match<T, A>): Operation<T, A>[] {
let i = matrix.length - 1;
let j = matrix[0].length - 1;

Expand Down
9 changes: 2 additions & 7 deletions packages/core/src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,13 @@ export class Manifest {
return (await this.read()).impls[version];
}

async storeDeployment(
version: string,
deployment: Deployment,
): Promise<void> {
async storeDeployment(version: string, deployment: Deployment): Promise<void> {
await this.update(data => (data.impls[version] = deployment));
}

async getDeploymentFromAddress(address: string): Promise<Deployment> {
const data = await this.read();
const deployment = Object.values(data.impls).find(
d => d.address === address,
);
const deployment = Object.values(data.impls).find(d => d.address === address);
if (deployment === undefined) {
throw new Error(`Deployment at address ${address} is not registered`);
}
Expand Down
11 changes: 2 additions & 9 deletions packages/core/src/provider.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
export interface EthereumProvider {
send(method: 'eth_chainId', params?: []): Promise<string>;
send(method: 'eth_getCode', params: [string, string?]): Promise<string>;
send(
method: 'eth_getStorageAt',
params: [string, string, string?],
): Promise<string>;
send(method: 'eth_getStorageAt', params: [string, string, string?]): Promise<string>;
send(method: string, params?: unknown[]): Promise<unknown>;
}

Expand All @@ -21,10 +18,6 @@ export async function getStorageAt(
return provider.send('eth_getStorageAt', [address, position, block]);
}

export async function getCode(
provider: EthereumProvider,
address: string,
block?: string,
): Promise<string> {
export async function getCode(provider: EthereumProvider, address: string, block?: string): Promise<string> {
return provider.send('eth_getCode', [address, block]);
}
9 changes: 2 additions & 7 deletions packages/core/src/src-decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,14 @@ interface Source {
content: string;
}

export function solcInputOutputDecoder(
solcInput: SolcInput,
solcOutput: SolcOutput,
): SrcDecoder {
export function solcInputOutputDecoder(solcInput: SolcInput, solcOutput: SolcOutput): SrcDecoder {
const sources: Record<number, Source> = {};

function getSource(sourceId: number): Source {
if (sourceId in sources) {
return sources[sourceId];
} else {
const name = Object.entries(solcOutput.sources).find(
([, { id }]) => sourceId === id,
)?.[0];
const name = Object.entries(solcOutput.sources).find(([, { id }]) => sourceId === id)?.[0];
if (name === undefined) {
throw new Error(`Source file not available`);
}
Expand Down
39 changes: 8 additions & 31 deletions packages/core/src/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,9 @@ interface Context {
const test = _test as TestInterface<Context>;

test.before(async t => {
const solcOutput: SolcOutput = JSON.parse(
await fs.readFile('cache/solc-output.json', 'utf8'),
);
const solcOutput: SolcOutput = JSON.parse(await fs.readFile('cache/solc-output.json', 'utf8'));
t.context.contracts = {};
for (const def of findAll(
'ContractDefinition',
solcOutput.sources['contracts/test/Storage.sol'].ast,
)) {
for (const def of findAll('ContractDefinition', solcOutput.sources['contracts/test/Storage.sol'].ast)) {
t.context.contracts[def.name] = def;
}
});
Expand All @@ -42,40 +37,22 @@ test('Storage2', t => {
});

test('storage upgrade equal', t => {
const v1 = extractStorageLayout(
t.context.contracts['StorageUpgrade_Equal_V1'],
dummyDecodeSrc,
);
const v2 = extractStorageLayout(
t.context.contracts['StorageUpgrade_Equal_V2'],
dummyDecodeSrc,
);
const v1 = extractStorageLayout(t.context.contracts['StorageUpgrade_Equal_V1'], dummyDecodeSrc);
const v2 = extractStorageLayout(t.context.contracts['StorageUpgrade_Equal_V2'], dummyDecodeSrc);
const comparison = getStorageUpgradeErrors(v1, v2);
t.deepEqual(comparison, []);
});

test('storage upgrade append', t => {
const v1 = extractStorageLayout(
t.context.contracts['StorageUpgrade_Append_V1'],
dummyDecodeSrc,
);
const v2 = extractStorageLayout(
t.context.contracts['StorageUpgrade_Append_V2'],
dummyDecodeSrc,
);
const v1 = extractStorageLayout(t.context.contracts['StorageUpgrade_Append_V1'], dummyDecodeSrc);
const v2 = extractStorageLayout(t.context.contracts['StorageUpgrade_Append_V2'], dummyDecodeSrc);
const comparison = getStorageUpgradeErrors(v1, v2);
t.deepEqual(comparison, []);
});

test('storage upgrade delete', t => {
const v1 = extractStorageLayout(
t.context.contracts['StorageUpgrade_Delete_V1'],
dummyDecodeSrc,
);
const v2 = extractStorageLayout(
t.context.contracts['StorageUpgrade_Delete_V2'],
dummyDecodeSrc,
);
const v1 = extractStorageLayout(t.context.contracts['StorageUpgrade_Delete_V1'], dummyDecodeSrc);
const v2 = extractStorageLayout(t.context.contracts['StorageUpgrade_Delete_V2'], dummyDecodeSrc);
const comparison = getStorageUpgradeErrors(v1, v2);
t.deepEqual(comparison, [
{
Expand Down
48 changes: 16 additions & 32 deletions packages/core/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ export interface StorageLayout {
types: Record<string, TypeItem>;
}

export function extractStorageLayout(
contractDef: ContractDefinition,
decodeSrc: SrcDecoder,
): StorageLayout {
export function extractStorageLayout(contractDef: ContractDefinition, decodeSrc: SrcDecoder): StorageLayout {
const layout: StorageLayout = { storage: [], types: {} };

for (const varDecl of contractDef.nodes) {
Expand All @@ -52,10 +49,7 @@ export function extractStorageLayout(
return layout;
}

export function assertStorageUpgradeSafe(
original: StorageLayout,
updated: StorageLayout,
): void {
export function assertStorageUpgradeSafe(original: StorageLayout, updated: StorageLayout): void {
const errors = getStorageUpgradeErrors(original, updated);

if (errors.length > 0) {
Expand All @@ -71,13 +65,7 @@ class StorageUpgradeErrors extends UpgradesError {
details() {
return this.errors
.map(e => {
return (
chalk.bold(e.updated?.src ?? 'unknown') +
': ' +
e.action +
' of variable ' +
e.updated?.label
);
return chalk.bold(e.updated?.src ?? 'unknown') + ': ' + e.action + ' of variable ' + e.updated?.label;
})
.join('\n\n');
}
Expand All @@ -91,8 +79,7 @@ export function getStorageUpgradeErrors(
const nameMatches = o.label === u.label;

// TODO: type matching should compare struct members, etc.
const typeMatches =
original.types[o.type].label === updated.types[u.type].label;
const typeMatches = original.types[o.type].label === updated.types[u.type].label;

if (typeMatches && nameMatches) {
return 'equal';
Expand All @@ -118,19 +105,16 @@ export function getStorageUpgradeErrors(
// Thus, the following regex has to perform a lookahead to make sure it gets
// the substitution right.
function decodeTypeIdentifier(typeIdentifier: string): string {
return typeIdentifier.replace(
/(\$_|_\$_|_\$)(?=(\$_|_\$_|_\$)*([^_$]|$))/g,
m => {
switch (m) {
case '$_':
return '(';
case '_$':
return ')';
case '_$_':
return ',';
default:
throw new Error('Unreachable');
}
},
);
return typeIdentifier.replace(/(\$_|_\$_|_\$)(?=(\$_|_\$_|_\$)*([^_$]|$))/g, m => {
switch (m) {
case '$_':
return '(';
case '_$':
return ')';
case '_$_':
return ',';
default:
throw new Error('Unreachable');
}
});
}
21 changes: 4 additions & 17 deletions packages/core/src/validate.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import _test, { TestInterface } from 'ava';
import { promises as fs } from 'fs';

import {
validate,
isUpgradeSafe,
getStorageLayout,
getContractVersion,
Validation,
} from './validate';
import { validate, isUpgradeSafe, getStorageLayout, getContractVersion, Validation } from './validate';
import { solcInputOutputDecoder } from './src-decoder';

interface Context {
Expand All @@ -17,12 +11,8 @@ interface Context {
const test = _test as TestInterface<Context>;

test.before(async t => {
const solcInput = JSON.parse(
await fs.readFile('cache/solc-input.json', 'utf8'),
);
const solcOutput = JSON.parse(
await fs.readFile('cache/solc-output.json', 'utf8'),
);
const solcInput = JSON.parse(await fs.readFile('cache/solc-input.json', 'utf8'));
const solcOutput = JSON.parse(await fs.readFile('cache/solc-output.json', 'utf8'));
const decodeSrc = solcInputOutputDecoder(solcInput, solcOutput);
t.context.validation = validate(solcOutput, decodeSrc);
});
Expand All @@ -48,10 +38,7 @@ testValid('HasDelegateCall', false);
testValid('ImportedParentHasStateVariableAssignment', false);

test('inherited storage', t => {
const version = getContractVersion(
t.context.validation,
'StorageInheritChild',
);
const version = getContractVersion(t.context.validation, 'StorageInheritChild');
const layout = getStorageLayout(t.context.validation, version);
t.is(layout.storage.length, 8);
for (let i = 0; i < layout.storage.length; i++) {
Expand Down
Loading