Skip to content

Commit

Permalink
Merge pull request #306 from EYBlockchain/lydia/revert
Browse files Browse the repository at this point in the history
Lydia/revert
  • Loading branch information
SwatiEY authored Jul 12, 2024
2 parents 1a8ef54 + c77a9e0 commit f7f6259
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 11 deletions.
24 changes: 19 additions & 5 deletions src/boilerplate/orchestration/javascript/raw/toOrchestration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -889,13 +889,14 @@ export const OrchestrationCodeBoilerPlate: any = (node: any) => {
lines.push(`${input.name}.all.integer`);
} else if(input.isBool) {
lines.push(`parseInt(${input.name}.integer, 10)`);
} else if(input.isAddress) {
lines.push(`${input.name}.hex(20)`);
}
else {
lines.push(`${input}.integer`);
}
});
}
returnInputs = returnInputs.concat(lines);

if(node.returnInputs[0]) {
node.returnInputs.forEach((input: any) => {
Expand Down Expand Up @@ -975,13 +976,26 @@ export const OrchestrationCodeBoilerPlate: any = (node: any) => {


case 'SendPublicTransaction':
node.publicInputsString = node.publicInputs.map(input =>
(typeof input === 'object' && !Array.isArray(input)) ? JSON.stringify(input) : input
).join(',');
if (node.publicInputs[0]) {
node.publicInputs.forEach((input: any) => {
if (input.properties) {
lines.push(`[${input.properties.map(p => p.type === 'bool' ? `(${input.name}${input.isConstantArray ? '.all' : ''}.${p.name}.integer === "1")` : `${input.name}${input.isConstantArray ? '.all' : ''}.${p.name}.integer`).join(',')}]`);
} else if (input.isConstantArray) {
lines.push(`${input.name}.all.integer`);
} else if(input.isBool) {
lines.push(`parseInt(${input.name}.integer, 10)`);
} else if(input.isAddress) {
lines.push(`${input.name}.hex(20)`);
}
else {
lines.push(`${input}.integer`);
}
});
}
return {
statements: [
`\n\n// Send transaction to the blockchain:
\nconst txData = await instance.methods.${node.functionName}(${node.publicInputsString}).encodeABI();
\nconst txData = await instance.methods.${node.functionName}(${lines}).encodeABI();
\nlet txParams = {
from: config.web3.options.defaultAccount,
to: contractAddr,
Expand Down
2 changes: 1 addition & 1 deletion src/codeGenerators/circuit/zokrates/toCircuit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ function codeGenerator(node: any) {
if(node.condition.leftExpression.nodeType == 'Identifier')
node.condition.leftExpression.name = node.condition.leftExpression.name.replace('_temp','');
initialStatements+= `
assert(${codeGenerator(node.condition)})`;
assert(!(${codeGenerator(node.condition)}))`;
return initialStatements;
}
// we use our list of condition vars to init temp variables.
Expand Down
10 changes: 10 additions & 0 deletions src/codeGenerators/orchestration/nodejs/toOrchestration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,16 @@ export default function codeGenerator(node: any, options: any = {}): any {
"Require statement not satisfied: ${node.message[0].value}"
);}\n`;

case 'RevertStatement':
if (!node.message){
return `throw new Error(
"Revert statement."
);\n`;
}
return `throw new Error(
"Reverted due to the following: ${node.message}"
);\n`;

case 'Folder':
case 'File':
case 'EditableCommitmentCommonFilesBoilerplate':
Expand Down
18 changes: 16 additions & 2 deletions src/transformers/visitors/toOrchestrationVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ const visitor = {
properties?: { name: string; type: string }[];
isConstantArray?: boolean;
isBool?: boolean;
isAddress?: boolean;
}

const sendPublicTransactionNode = buildNode('SendPublicTransaction', {
Expand All @@ -522,7 +523,7 @@ const visitor = {

node.parameters.parameters.forEach((para: { isSecret: any; typeName: { name: string; }; name: any; _newASTPointer: { typeName: { properties: any[]; }; }; }) => {
if (!para.isSecret) {
if (path.isStructDeclaration(para) || path.isConstantArray(para) || (para.typeName && para.typeName.name === 'bool')) {
if (path.isStructDeclaration(para) || path.isConstantArray(para) || (para.typeName && para.typeName.name === 'bool') || (para.typeName && para.typeName.name === 'address')) {
let newParam: PublicParam = { name: para.name };
if (path.isStructDeclaration(para)) {
newParam.properties = para._newASTPointer.typeName.properties.map(p => ({ "name": p.name, "type": p.type }));
Expand All @@ -532,6 +533,9 @@ const visitor = {
}
if (para.typeName?.name === 'bool') {
newParam.isBool = true;
}
if (para.typeName?.name === 'address') {
newParam.isAddress = true;
}
sendPublicTransactionNode.publicInputs.push(newParam);
} else {
Expand Down Expand Up @@ -866,12 +870,13 @@ const visitor = {
// this adds other values we need in the tx
for (const param of node.parameters.parameters) {
if (!param.isSecret) {
if (path.isStructDeclaration(param) || path.isConstantArray(param) ||( param.typeName && param.typeName.name === 'bool')) {
if (path.isStructDeclaration(param) || path.isConstantArray(param) ||( param.typeName && param.typeName.name === 'bool') || ( param.typeName && param.typeName.name === 'address')) {
let newParam: any = {};
newParam.name = param.name;
if (path.isStructDeclaration(param)) newParam.properties = param._newASTPointer.typeName.properties.map(p => ({"name" : p.name, "type" : p.type }));
if (path.isConstantArray(param)) newParam.isConstantArray = true;
if (param.typeName?.name === 'bool') newParam.isBool = true;
if (param.typeName?.name === 'address') newParam.isAddress = true;
newNodes.sendTransactionNode.publicInputs.push(newParam);
} else newNodes.sendTransactionNode.publicInputs.push(param.name);
}
Expand Down Expand Up @@ -1801,6 +1806,15 @@ const visitor = {
state.skipSubNodes = true;
return;
}
if (node.expression?.name === 'revert') {
const newNode = buildNode('RevertStatement', {
});
parent._newASTPointer[path.containerName] = newNode;
if (node.arguments[0]) newNode.message = node.arguments[0].value;
parent._newASTPointer.interactsWithSecret = path.getAncestorOfType('IfStatement').node._newASTPointer.interactsWithSecret;
state.skipSubNodes = true;
return;
}
if (node.kind !== 'typeConversion') {
state.skipSubNodes = true;
return;
Expand Down
6 changes: 6 additions & 0 deletions src/types/orchestration-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ export default function buildNode(nodeType: string, fields: any = {}): any {
message: [],
};
}
case 'RevertStatement': {
const {} = fields;
return {
nodeType,
};
}
case 'InitialisePreimage':
case 'InitialiseKeys':
case 'ReadPreimage':
Expand Down
39 changes: 36 additions & 3 deletions test/contracts/revert.zol
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,50 @@ contract MyContract {

secret uint256 private a;
secret address private admin;
address private pubAdmin;


constructor() {
admin = msg.sender;
pubAdmin = msg.sender;
}

function setAdmin(secret address newAdmin) public {
if(msg.sender == admin) {
revert(" revert ");
function setSecretAdmin(secret address newAdmin, uint256 value) public {
if(msg.sender != admin) {
revert("the msg sender is not admin ");
}
admin = newAdmin;
a+= value;
}

function setSecretAdmin1(secret address newAdmin) public {
if(msg.sender != admin) {
revert();
}
admin = newAdmin;
a+= 1;
}

function setPublicAdmin(address newAdmin) public {
if(msg.sender != pubAdmin) {
revert("the msg sender is not admin ");
}
pubAdmin = newAdmin;
a+= 1;
}

function setPublicAdmin1(address newAdmin) public {
if(msg.sender != pubAdmin) {
revert();
}
pubAdmin = newAdmin;
a+= 1;
}

function setPublicAdmin2(address newAdmin) public {
if(msg.sender != pubAdmin) {
revert();
}
pubAdmin = newAdmin;
}
}

0 comments on commit f7f6259

Please sign in to comment.