Skip to content

Commit

Permalink
fix: logic for circuit imports due to Internal Function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
lydiagarms committed May 22, 2024
1 parent 6e94ae2 commit 5c5db4d
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 84 deletions.
5 changes: 4 additions & 1 deletion src/codeGenerators/orchestration/nodejs/toOrchestration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ export default function codeGenerator(node: any, options: any = {}): any {
return `${getAccessedValue(node.declarations[0].name)}`;
}

console.log("kangeroo");
console.log(node);
if (
node.initialValue.operator &&
!node.initialValue.operator.includes('=')
Expand Down Expand Up @@ -117,8 +119,9 @@ export default function codeGenerator(node: any, options: any = {}): any {
}

case 'ExpressionStatement':
if (!node.incrementsSecretState && node.interactsWithSecret)
if (!node.incrementsSecretState && (node.interactsWithSecret || node.expression?.internalFunctionInteractsWithSecret)){
return `\n${codeGenerator(node.expression)};`;
}
if (!node.interactsWithSecret)
return `\n// non-secret line would go here but has been filtered out`;
return `\n// increment would go here but has been filtered out`;
Expand Down
155 changes: 146 additions & 9 deletions src/transformers/visitors/circuitInternalFunctionCallVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ const internalCallVisitor = {
state.internalFncName?.forEach( (name,index) => {
node._newASTPointer.forEach(file => {
if(file.fileName === name) {
if(state.circuitImport[index]==='true') {
file.nodes.forEach(childNode => {
if(childNode.nodeType === 'FunctionDefinition'){
state.newParameterList = cloneDeep(childNode.parameters.parameters);
state.newReturnParameterList = cloneDeep(childNode.returnParameters.parameters);
state.newPreStatementList = cloneDeep(childNode.body.preStatements);
state.newPostStatementList = cloneDeep(childNode.body.postStatements);

state.newParameterList.forEach((node, nodeIndex) => {
if(node.nodeType === 'Boilerplate') {
Expand Down Expand Up @@ -110,7 +111,7 @@ const internalCallVisitor = {
})
}
})

if(state.circuitImport[index].isImported ==='true') {
// Collect the internal call ParameterList
let internalFncParameters: string[] = [];
state.newParameterList.forEach(node => {
Expand Down Expand Up @@ -166,20 +167,21 @@ const internalCallVisitor = {
state.circuitArguments.push(param);
}
});
}

node._newASTPointer.forEach(file => {
if(file.fileName === state.callingFncName[index].name){
file.nodes.forEach(childNode => {
if(childNode.nodeType === 'StructDefinition' && !state.isAddStructDefinition)
if(childNode.nodeType === 'StructDefinition' && !state.isAddStructDefinition && state.circuitImport[index].isImported === 'true')
file.nodes.splice(file.nodes.indexOf(childNode),1);
})
file.nodes.forEach(childNode => {
if(childNode.nodeType === 'FunctionDefinition'){
childNode.parameters.parameters = [...new Set([...childNode.parameters.parameters, ...state.newParameterList])];
reorderParameters(childNode.parameters.parameters);
childNode.returnParameters.parameters = [...new Set([...childNode.returnParameters.parameters, ...state.newReturnParameterList])];
if(childNode.nodeType === 'FunctionDefinition' && state.callingFncName[index].parent === 'FunctionDefinition'){
childNode.body.statements.forEach(node => {
if(childNode.nodeType === 'FunctionDefinition' && state.callingFncName[index].parent === 'FunctionDefinition' && state.circuitImport[index].isImported === 'true'){
childNode.body.statements.forEach(node => {
if(node.nodeType === 'ExpressionStatement') {
if(node.expression.nodeType === 'InternalFunctionCall' && node.expression.name === name){
node.expression.CircuitArguments = node.expression.CircuitArguments.concat(state.circuitArguments);
Expand All @@ -188,7 +190,7 @@ const internalCallVisitor = {
}
}
})
} else {
} else if (state.circuitImport[index].isImported === 'true') {
childNode.body.statements.forEach(node => {
if(node.nodeType === state.callingFncName[index].parent){
node.body.statements.forEach(kidNode => {
Expand All @@ -208,9 +210,8 @@ const internalCallVisitor = {
}

})
}

else if(state.circuitImport[index] === 'false'){
if(state.circuitImport[index].isImported === 'false'){
let newExpressionList = [];
let isPartitioned = false
let internalFncbpType: string;
Expand Down Expand Up @@ -289,7 +290,75 @@ const internalCallVisitor = {
else
node.newCommitmentValue = node.newCommitmentValue+' - ('+commitmentValue+')';
}
})
});
childNode.body.preStatements.forEach(node => {
switch(node.bpType) {
case 'PoKoSK' : {
state.newPreStatementList.forEach(statenode => {
if(statenode.bpType === 'PoKoSK' && statenode.name === node.name){
statenode.isNullified = statenode.isNullified || node.isNullified;
node = Object.assign(node,statenode);
}
});
break;
}
case 'nullification' : {
state.newPreStatementList.forEach(statenode => {
if(statenode.bpType === 'nullification' && statenode.name === node.name){
statenode.isNullified = statenode.isNullified || node.isNullified;
node = Object.assign(node,statenode);
}
});
break;
}
case 'oldCommitmentPreimage' : {
state.newPreStatementList.forEach(statenode => {
if(statenode.bpType === 'oldCommitmentPreimage' && statenode.name === node.name){
statenode.isNullified = statenode.isNullified || node.isNullified;
node = Object.assign(node,statenode);
}
});
break;
}
case 'oldCommitmentExistence' : {
state.newPreStatementList.forEach(statenode => {
if(statenode.bpType === 'oldCommitmentExistence' && statenode.name === node.name){
statenode.isNullified = statenode.isNullified || node.isNullified;
node = Object.assign(node,statenode);
}
});
break;
}
case 'newCommitment' : {
state.newPreStatementList.forEach(statenode => {
if(statenode.bpType === 'newCommitment' && statenode.name === node.name){
statenode.isNullified = statenode.isNullified || node.isNullified;
node = Object.assign(node,statenode);
}
});
break;
}
default :
break;
}
});
let nonDuplicatePreStatements = [];
state.newPreStatementList.forEach(stateNode => {
let isDuplicate = false;
let dupIndex =0;
childNode.body.preStatements.forEach((existingNode, exIndex) => {
if(existingNode.bpType === stateNode.bpType && existingNode.name === stateNode.name){
isDuplicate = true;
}
if (existingNode.name === stateNode.name) {
dupIndex = exIndex;
}
});
if (!isDuplicate) nonDuplicatePreStatements.push({node: stateNode, index: dupIndex });
});
nonDuplicatePreStatements.forEach(dupNode => {
childNode.body.preStatements.splice(dupNode.index +1, 0, dupNode.node);
});
childNode.body.postStatements.forEach( node => {
if(isPartitioned){
if(internalFncbpType === callingFncbpType)
Expand All @@ -298,6 +367,74 @@ const internalCallVisitor = {
node.newCommitmentValue = node.newCommitmentValue+' - ('+commitmentValue+')';
}
})
childNode.body.postStatements.forEach(node => {
switch(node.bpType) {
case 'PoKoSK' : {
state.newPostStatementList.forEach(statenode => {
if(statenode.bpType === 'PoKoSK' && statenode.name === node.name){
statenode.isNullified = statenode.isNullified || node.isNullified;
node = Object.assign(node,statenode);
}
});
break;
}
case 'nullification' : {
state.newPostStatementList.forEach(statenode => {
if(statenode.bpType === 'nullification' && statenode.name === node.name){
statenode.isNullified = statenode.isNullified || node.isNullified;
node = Object.assign(node,statenode);
}
});
break;
}
case 'oldCommitmentPreimage' : {
state.newPostStatementList.forEach(statenode => {
if(statenode.bpType === 'oldCommitmentPreimage' && statenode.name === node.name){
statenode.isNullified = statenode.isNullified || node.isNullified;
node = Object.assign(node,statenode);
}
});
break;
}
case 'oldCommitmentExistence' : {
state.newPostStatementList.forEach(statenode => {
if(statenode.bpType === 'oldCommitmentExistence' && statenode.name === node.name){
statenode.isNullified = statenode.isNullified || node.isNullified;
node = Object.assign(node,statenode);
}
});
break;
}
case 'newCommitment' : {
state.newPostStatementList.forEach(statenode => {
if(statenode.bpType === 'newCommitment' && statenode.name === node.name){
statenode.isNullified = statenode.isNullified || node.isNullified;
node = Object.assign(node,statenode);
}
});
break;
}
default :
break;
}
});
let nonDuplicatePostStatements = [];
state.newPostStatementList.forEach(stateNode => {
let isDuplicate = false;
let dupIndex =0;
childNode.body.postStatements.forEach((existingNode, exIndex) => {
if(existingNode.bpType === stateNode.bpType && existingNode.name === stateNode.name){
isDuplicate = true;
}
if (existingNode.name === stateNode.name) {
dupIndex = exIndex;
}
});
if (!isDuplicate) nonDuplicatePostStatements.push({node: stateNode, index: dupIndex });
});
nonDuplicatePostStatements.forEach(dupNode => {
childNode.body.postStatements.splice(dupNode.index +1, 0, dupNode.node);
});
}

})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const internalCallVisitor = {
node._newASTPointer.forEach(file => {
state.internalFncName?.forEach( (name, index)=> {
if(file.fileName === name && file.nodeType === 'File') {
if(state.circuitImport[index]==='true') {
file.nodes.forEach(childNode => {
if(childNode.nodeType === 'FunctionDefinition'){
state.newParametersList = cloneDeep(childNode.parameters.modifiedStateVariables);
Expand Down Expand Up @@ -290,7 +289,7 @@ const internalCallVisitor = {
let isDecDeleted = false;
if(node1.nodeType === 'VariableDeclarationStatement'){
childNode.body.statements.forEach((node2, index2)=> {
if(!isDecDeleted && index2 < index1 && node2.nodeType === 'VariableDeclarationStatement'){
if(!isDecDeleted && index2 < index1 && node2 && node2.nodeType === 'VariableDeclarationStatement'){
if ((node1.declarations[0].name === node2.declarations[0].name)){
childNode.body.statements.splice(index1, 1, node1.initialValue);
isDecDeleted = true;
Expand Down Expand Up @@ -384,54 +383,6 @@ const internalCallVisitor = {
})
}
})
}
if(state.circuitImport[index] === 'false') {
file.nodes.forEach(childNode => {
if(childNode.nodeType === 'FunctionDefinition'){
state.newStatementList = cloneDeep(childNode.body.statements);
state.newStatementList.forEach(node => {
if(node.nodeType === 'VariableDeclarationStatement') {
for(const [index, oldStateName] of state.oldStateArray[name].entries()) {
node.initialValue.leftHandSide.name = node.initialValue.leftHandSide.name?.replace('_'+oldStateName, '_'+ state.newStateArray[name][index]);
node.initialValue.rightHandSide.name = node.initialValue.rightHandSide.name?.replace(oldStateName, state.newStateArray[name][index]);
}
}
})
}
})

node._newASTPointer.forEach(file => {
if(file.fileName === state.callingFncName[index].name) {
file.nodes.forEach(childNode => {
if(childNode.nodeType === 'FunctionDefinition') {
if(state.callingFncName[index].parent === 'FunctionDefinition'){
state.statementnode = childNode;
} else{
childNode.body.statements.forEach(kidNode => {
if(kidNode.nodeType === state.callingFncName[index].parent)
state.statementnode = kidNode;
})
}
state.statementnode.body.statements.forEach(node => {
if(node.nodeType === 'ExpressionStatement'&& node.expression.name === state.internalFncName[index]) {
state.newStatementList.forEach(list => {
if(list.nodeType === 'VariableDeclarationStatement')
node.expression = Object.assign(node.expression,list.initialValue);
if(list.nodeType === 'Assignment')
state.statementnode.body.statements?.splice(state.statementnode.body.statements.indexOf(node)+1, 0, list);
})
}
if(node.nodeType === 'VariableDeclarationStatement' && state.statementnode.body.statements.indexOf(node) != 0){
state.statementnode.body.statements.splice(0, 0, state.statementnode.body.statements.splice(state.statementnode.body.statements.indexOf(node)+1, 1)[0]);
state.statementnode.body.statements.splice(0, 0, state.statementnode.body.statements.splice(state.statementnode.body.statements.indexOf(node), 1)[0]);
}
});
}
})

}
})
}
}
})
})
Expand Down Expand Up @@ -464,7 +415,7 @@ FunctionCall: {
const callingfnDefIndicators = callingfnDefPath?.scope.indicators;
const functionReferncedNode = scope.getReferencedPath(node.expression);
const internalfnDefIndicators = functionReferncedNode?.scope.indicators;
const startNodePath = path.getAncestorOfType('ContractDefinition')
const startNodePath = path.getAncestorOfType('ContractDefinition');
startNodePath?.node.nodes.forEach(node => {
if(node.nodeType === 'VariableDeclaration' && !node.typeDescriptions.typeIdentifier.includes('_struct')){
if(internalfnDefIndicators[node.id] && internalfnDefIndicators[node.id].isModified){
Expand Down
Loading

0 comments on commit 5c5db4d

Please sign in to comment.