Skip to content

Commit

Permalink
Merge pull request #4 from Javadyakuza/integrate-with-BDA
Browse files Browse the repository at this point in the history
Integrate with bda
  • Loading branch information
Cyace84 authored Aug 30, 2023
2 parents 7288a38 + 1368617 commit 6d1d0a7
Show file tree
Hide file tree
Showing 13 changed files with 1,508 additions and 4,128 deletions.
5,096 changes: 1,123 additions & 3,973 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"test": "ava"
},
"dependencies": {
"asciidoctor": "^3.0.2",
"handlebars": "^4.7.7",
"solidity-ast": "^0.4.38"
},
Expand Down
74 changes: 37 additions & 37 deletions src/ast-builder/ast-updater.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { SourceUnit } from 'solidity-ast';
import { SolcOutput } from 'solidity-ast/solc';
import { findAll, isNodeType } from 'solidity-ast/utils';
import { Config } from '../config';
import { SourceUnit } from "solidity-ast";
import { SolcOutput } from "solidity-ast/solc";
import { findAll, isNodeType } from "solidity-ast/utils";
import { Config } from "../config";
import {
getDependenciesCount,
getParentAstFromContractId,
getParentAstFromName,
} from './getters';
import { FullSources } from './types';
} from "./getters";
import { FullSources } from "./types";

export const resetIds = (ast: any, idCounter: number = 0): number => {
// Recursively visit child nodes and update their ID's
for (const key in ast) {
if (typeof ast[key] === 'object' && ast[key] !== null) {
if (key === 'body') {
if (ast.hasOwnProperty('id') && ast.id === undefined) {
if (typeof ast[key] === "object" && ast[key] !== null) {
if (key === "body") {
if (ast.hasOwnProperty("id") && ast.id === undefined) {
idCounter = resetIds(ast[key], idCounter);
ast.id = idCounter;
idCounter++;
} else {
idCounter = resetIds(ast[key], idCounter);
}
} else {
if (ast.hasOwnProperty('id') && ast.id === undefined) {
if (ast.hasOwnProperty("id") && ast.id === undefined) {
idCounter = resetIds(ast[key], idCounter);
ast.id = idCounter;
idCounter++;
Expand All @@ -34,14 +34,14 @@ export const resetIds = (ast: any, idCounter: number = 0): number => {
}
// Update references to new ID's
for (const key in ast) {
if (typeof ast[key] === 'number' && key === 'id') {
if (typeof ast[key] === "number" && key === "id") {
if (ast[key] < idCounter) {
ast[key] = idCounter;
++idCounter;
}
}
}
if (ast.hasOwnProperty('exportedSymbols')) {
if (ast.hasOwnProperty("exportedSymbols")) {
const name = Object.keys(ast.exportedSymbols)[0]!;
ast.exportedSymbols[name] = [ast.id - 1];
}
Expand All @@ -58,12 +58,12 @@ const assignIds = (node: any, id: number) => {
};

export const replaceSrc = (obj: any, id: number) => {
if (!obj || typeof obj !== 'object') {
if (!obj || typeof obj !== "object") {
return;
}

if (obj.hasOwnProperty('src')) {
const splitedSrc = obj.src.split(':');
if (obj.hasOwnProperty("src")) {
const splitedSrc = obj.src.split(":");
obj.src = `${splitedSrc[0]}:${splitedSrc[1]}:${id}`;
}

Expand All @@ -76,15 +76,15 @@ export const replaceSrc = (obj: any, id: number) => {

export function updateDependices(
origSources: FullSources,
sources: SolcOutput['sources'],
config: Config,
sources: SolcOutput["sources"],
config: Config
) {
for (const contract of Object.keys(sources)) {
const ast = sources[contract]!.ast;
const origSource = origSources[contract]!;
updateImports(ast, sources, config.root!);
updateReferences(ast, sources, origSource.asts);
for (const contractDef of findAll('ContractDefinition', ast)) {
for (const contractDef of findAll("ContractDefinition", ast)) {
const dependencies = contractDef.contractDependencies;
ast.exportedSymbols[contractDef.name] = [contractDef.id];
if (dependencies.length === 0) {
Expand All @@ -96,21 +96,21 @@ export function updateDependices(
ast,
sources,
origSource.asts,
dependencies,
dependencies
);
const uniqParentIds = [...new Set(parentIds)];
contractDef.contractDependencies = uniqParentIds;
contractDef.linearizedBaseContracts = [contractDef.id].concat(
uniqParentIds,
uniqParentIds
);
}
}
}
const updateFunctions = (
ast: SourceUnit,
sources: SolcOutput['sources'],
sources: SolcOutput["sources"],
origAsts: SourceUnit[],
dependencies: number[],
dependencies: number[]
) => {
let parentIds: number[] = [];

Expand All @@ -120,15 +120,15 @@ const updateFunctions = (
if (origParentAst) {
const parentAst = sources[origParentAst.absolutePath]!.ast;

for (const func of findAll('FunctionDefinition', ast)) {
for (const func of findAll("FunctionDefinition", ast)) {
if (func.baseFunctions) {
const baseFuncId = func.baseFunctions[0]!;
let funcSrc: string | undefined;
let parentId: number | undefined;

for (const node of origParentAst.nodes) {
if (
isNodeType('FunctionDefinition', node) &&
isNodeType("FunctionDefinition", node) &&
node.id === baseFuncId
) {
funcSrc = node.src;
Expand All @@ -139,12 +139,12 @@ const updateFunctions = (
if (funcSrc) {
for (const node of parentAst.nodes) {
if (
isNodeType('FunctionDefinition', node) &&
isNodeType("FunctionDefinition", node) &&
node.src.slice(0, -1) == funcSrc!.slice(0, -1)
) {
func.baseFunctions = [node.id];
parentId = Object.values(parentAst.exportedSymbols).find(
id => id![0] === node.id,
(id) => id![0] === node.id
)![0];
break;
}
Expand All @@ -164,10 +164,10 @@ const updateFunctions = (

const updateImports = (
ast: SourceUnit,
sources: SolcOutput['sources'],
rootPath: string,
sources: SolcOutput["sources"],
rootPath: string
) => {
for (const imp of findAll('ImportDirective', ast)) {
for (const imp of findAll("ImportDirective", ast)) {
let absolutePath = imp.absolutePath;
if (imp.absolutePath.startsWith(rootPath)) {
absolutePath = imp.absolutePath.slice(rootPath.length + 1);
Expand All @@ -184,11 +184,11 @@ const updateImports = (

const updateReferences = (
ast: SourceUnit,
sources: SolcOutput['sources'],
origAsts: SourceUnit[],
sources: SolcOutput["sources"],
origAsts: SourceUnit[]
) => {
for (const imp of findAll('InheritanceSpecifier', ast)) {
if (!isNodeType('UserDefinedTypeName', imp.baseName)) {
for (const imp of findAll("InheritanceSpecifier", ast)) {
if (!isNodeType("UserDefinedTypeName", imp.baseName)) {
continue;
}
const baseName = imp.baseName.name!;
Expand All @@ -199,7 +199,7 @@ const updateReferences = (

imp.baseName.referencedDeclaration = realParentId;
const splitedtypeIdentifier =
imp.baseName.typeDescriptions.typeIdentifier!.split('$');
imp.baseName.typeDescriptions.typeIdentifier!.split("$");
imp.baseName.typeDescriptions.typeIdentifier = `${splitedtypeIdentifier[0]}${splitedtypeIdentifier[1]}$${realParentId}`;
}
};
Expand All @@ -219,15 +219,15 @@ export const renameAbsolutePaths = (root: string, asts: SourceUnit[]) => {

export function sortASTByDependency(
sources: SourceUnit[][],
solcOutput: SolcOutput,
solcOutput: SolcOutput
) {
// Count the number of times each contract is inherited
const dependencyCount: Map<string, number> = getDependenciesCount(sources);

const sortedDependencyCount = Array.from(dependencyCount).sort(
(a, b) => b[1] - a[1],
(a, b) => b[1] - a[1]
);
const sortedSources: SolcOutput['sources'] = {};
const sortedSources: SolcOutput["sources"] = {};
let id = 0;
let lastAstId = 0;
sortedDependencyCount.reverse().forEach(([path]) => {
Expand Down
35 changes: 26 additions & 9 deletions src/ast-builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
} from "./ast-updater";
import { Config } from "../config";
import { Build } from "../site";
import path from "path";

import path, { resolve } from "path";

import { compileAst } from "./compile-ast";

Expand All @@ -24,20 +25,35 @@ const createRawOutput = (sources: Sources) => {
return output;
};

const createInput = (solcOutput: SolcOutput) => {
const createInput = (solcOutput: SolcOutput, sourcesDir: string) => {
const sources = solcOutput.sources;
const SolcInput: SolcInput = { sources: {} };
for (const key of Object.keys(sources)) {
const fileContent = fs.readFileSync(key, "utf8").toString();

for (let key of Object.keys(sources)) {
const tempKey = key.startsWith("contracts")
? resolve(sourcesDir.replace("/contracts", "/"), key)
: resolve(sourcesDir.replace("/contracts", "/node_modules"), key);
const fileContent = fs.readFileSync(tempKey, "utf8").toString();

SolcInput.sources[key] = { content: fileContent };
}
return SolcInput;
};

export function isMainContract(absolutePath: string, astPath: string) {
export function isMainContract(
absolutePath: string,
astPath: string,
sourcesDir: string
) {
// checking if its in the project main contracts ot=r and third party contract
sourcesDir = absolutePath.startsWith("contracts")
? sourcesDir.replace("/contracts", "")
: sourcesDir.replace("/contracts", "/node_modules");
// Extract the contract name from absolute path
if (!fileExists(absolutePath) || !fileExists(astPath)) {
throw new Error("File does not exist");
if (!fileExists(resolve(sourcesDir, absolutePath)) || !fileExists(astPath)) {
throw new Error(`File does not exist :${resolve(sourcesDir, absolutePath)},
${absolutePath},
${astPath}`);
}
const contractName = getContractName(absolutePath);

Expand Down Expand Up @@ -67,7 +83,8 @@ export const makeBuild = async (
compileAst(config);
const { sources: astSources, fullSources } = getAstsFromSources(
config.astOutputDir!,
config.root!
config.root!,
config.sourcesDir!
)!;
const solcOutput = createRawOutput(astSources);
const sourcesList = Object.values(fullSources).map((source) => source.asts);
Expand All @@ -77,7 +94,7 @@ export const makeBuild = async (
const ph = path.join(config.root!, "build/astBuild.json");
fs.writeFileSync(ph, JSON.stringify(sortedSources, null, 2));
solcOutput.sources = sortedSources;
const solcInput = createInput(solcOutput);
const solcInput = createInput(solcOutput, config.sourcesDir!);
const build: Build = {
input: solcInput,
output: solcOutput,
Expand Down
Loading

0 comments on commit 6d1d0a7

Please sign in to comment.