Commit be7805e0 authored by aniket-engg's avatar aniket-engg Committed by Aniket

common unit tests updated

parent 0569590c
import { getStateVariableDeclarationsFormContractNode, import { getStateVariableDeclarationsFromContractNode,
getInheritsFromName, getContractName, getInheritsFromName, getContractName,
getFunctionOrModifierDefinitionParameterPart, getType, getDeclaredVariableName, getFunctionOrModifierDefinitionParameterPart, getType, getDeclaredVariableName,
getFunctionDefinitionReturnParameterPart } from './staticAnalysisCommon' getFunctionDefinitionReturnParameterPart } from './staticAnalysisCommon'
...@@ -57,7 +57,7 @@ export default class abstractAstView { ...@@ -57,7 +57,7 @@ export default class abstractAstView {
relevantNodes: [], relevantNodes: [],
modifiers: [], modifiers: [],
inheritsFrom: [], inheritsFrom: [],
stateVariables: getStateVariableDeclarationsFormContractNode(node) stateVariables: getStateVariableDeclarationsFromContractNode(node)
}) })
} else if (node.nodeType === "InheritanceSpecifier") { } else if (node.nodeType === "InheritanceSpecifier") {
const currentContract = that.getCurrentContract(that) const currentContract = that.getCurrentContract(that)
......
...@@ -47,7 +47,7 @@ const basicRegex = { ...@@ -47,7 +47,7 @@ const basicRegex = {
FUNCTIONTYPE: '^function \\(', FUNCTIONTYPE: '^function \\(',
EXTERNALFUNCTIONTYPE: '^function \\(.*\\).* external', EXTERNALFUNCTIONTYPE: '^function \\(.*\\).* external',
CONSTANTFUNCTIONTYPE: '^function \\(.*\\).* (view|pure)', CONSTANTFUNCTIONTYPE: '^function \\(.*\\).* (view|pure)',
REFTYPE: '( storage )|(mapping\\()|(\\[\\])', REFTYPE: '(storage)|(mapping\\()|(\\[\\])',
FUNCTIONSIGNATURE: '^function \\(([^\\(]*)\\)', FUNCTIONSIGNATURE: '^function \\(([^\\(]*)\\)',
LIBRARYTYPE: '^type\\(library (.*)\\)' LIBRARYTYPE: '^type\\(library (.*)\\)'
} }
...@@ -169,9 +169,9 @@ function getLocalCallName (localCallNode: FunctionCallAstNode): string { ...@@ -169,9 +169,9 @@ function getLocalCallName (localCallNode: FunctionCallAstNode): string {
* @localCallNode {ASTNode} Function call node * @localCallNode {ASTNode} Function call node
* @return {string} name of the function called * @return {string} name of the function called
*/ */
function getThisLocalCallName (localCallNode: FunctionCallAstNode): string { function getThisLocalCallName (thisLocalCallNode: FunctionCallAstNode): string {
if (!isThisLocalCall(localCallNode.expression)) throw new Error('staticAnalysisCommon.js: not an this local call Node') if (!isThisLocalCall(thisLocalCallNode.expression)) throw new Error('staticAnalysisCommon.js: not an this local call Node')
return localCallNode.expression.memberName return thisLocalCallNode.expression.memberName
} }
/** /**
...@@ -180,9 +180,9 @@ function getThisLocalCallName (localCallNode: FunctionCallAstNode): string { ...@@ -180,9 +180,9 @@ function getThisLocalCallName (localCallNode: FunctionCallAstNode): string {
* @localCallNode {ASTNode} Function call node * @localCallNode {ASTNode} Function call node
* @return {string} name of the function called * @return {string} name of the function called
*/ */
function getSuperLocalCallName (localCallNode: FunctionCallAstNode): string { function getSuperLocalCallName (superLocalCallNode: FunctionCallAstNode): string {
if (!isSuperLocalCall(localCallNode.expression)) throw new Error('staticAnalysisCommon.js: not an super local call Node') if (!isSuperLocalCall(superLocalCallNode.expression)) throw new Error('staticAnalysisCommon.js: not an super local call Node')
return localCallNode.expression.memberName return superLocalCallNode.expression.memberName
} }
/** /**
...@@ -256,8 +256,8 @@ function getFunctionDefinitionName (funcDef: FunctionDefinitionAstNode): string ...@@ -256,8 +256,8 @@ function getFunctionDefinitionName (funcDef: FunctionDefinitionAstNode): string
* @func {ASTNode} Inheritance specifier * @func {ASTNode} Inheritance specifier
* @return {string} name of contract inherited from * @return {string} name of contract inherited from
*/ */
function getInheritsFromName (inheritsNode: InheritanceSpecifierAstNode): UserDefinedTypeNameAstNode { function getInheritsFromName (inheritsNode: InheritanceSpecifierAstNode): string {
return inheritsNode.baseName return inheritsNode.baseName.name
} }
/** /**
...@@ -292,7 +292,7 @@ function getDeclaredVariableType (varDeclNode: VariableDeclarationAstNode): stri ...@@ -292,7 +292,7 @@ function getDeclaredVariableType (varDeclNode: VariableDeclarationAstNode): stri
* @contractNode {ASTNode} Contract Definition node * @contractNode {ASTNode} Contract Definition node
* @return {list variable declaration} state variable node list * @return {list variable declaration} state variable node list
*/ */
function getStateVariableDeclarationsFormContractNode (contractNode: ContractDefinitionAstNode): VariableDeclarationAstNode[] { function getStateVariableDeclarationsFromContractNode (contractNode: ContractDefinitionAstNode): VariableDeclarationAstNode[] {
return contractNode.nodes.filter(el => el.nodeType === "VariableDeclaration") return contractNode.nodes.filter(el => el.nodeType === "VariableDeclaration")
} }
...@@ -355,9 +355,9 @@ function getFunctionCallTypeParameterType (func: FunctionCallAstNode): string | ...@@ -355,9 +355,9 @@ function getFunctionCallTypeParameterType (func: FunctionCallAstNode): string |
* @funcCall {ASTNode} function call node * @funcCall {ASTNode} function call node
* @return {string} name of the lib defined * @return {string} name of the lib defined
*/ */
function getLibraryCallContractName (node: MemberAccessAstNode): string | undefined { function getLibraryCallContractName (node: FunctionCallAstNode): string | undefined {
if (!isLibraryCall(node)) throw new Error('staticAnalysisCommon.js: not an this library call Node') if (!isLibraryCall(node.expression)) throw new Error('staticAnalysisCommon.js: not an this library call Node')
const types: RegExpExecArray | null = new RegExp(basicRegex.LIBRARYTYPE).exec(node.expression.typeDescriptions.typeString) const types: RegExpExecArray | null = new RegExp(basicRegex.LIBRARYTYPE).exec(node.expression.expression.typeDescriptions.typeString)
if(types) if(types)
return types[1] return types[1]
} }
...@@ -522,6 +522,9 @@ function isLocalCallGraphRelevantNode (node: FunctionCallAstNode): boolean { ...@@ -522,6 +522,9 @@ function isLocalCallGraphRelevantNode (node: FunctionCallAstNode): boolean {
* @return {bool} * @return {bool}
*/ */
function isBuiltinFunctionCall (node: FunctionCallAstNode): boolean { function isBuiltinFunctionCall (node: FunctionCallAstNode): boolean {
// console.log('isBuiltinFunctionCall isLocalCall', isLocalCall(node))
// console.log('isBuiltinFunctionCall getLocalCallName', getLocalCallName(node))
// console.log('isBuiltinFunctionCall getFunctionCallTypeParameterType', getFunctionCallTypeParameterType(node))
return (isLocalCall(node) && builtinFunctions[getLocalCallName(node) + '(' + getFunctionCallTypeParameterType(node) + ')'] === true) || isAbiNamespaceCall(node) return (isLocalCall(node) && builtinFunctions[getLocalCallName(node) + '(' + getFunctionCallTypeParameterType(node) + ')'] === true) || isAbiNamespaceCall(node)
} }
...@@ -567,7 +570,7 @@ function isRequireCall (node: FunctionCallAstNode): boolean { ...@@ -567,7 +570,7 @@ function isRequireCall (node: FunctionCallAstNode): boolean {
* @return {bool} * @return {bool}
*/ */
function isStorageVariableDeclaration (node: VariableDeclarationAstNode): boolean { function isStorageVariableDeclaration (node: VariableDeclarationAstNode): boolean {
return expressionType(node, basicRegex.REFTYPE) return node.storageLocation === 'storage' && new RegExp(basicRegex.REFTYPE).test(node.typeDescriptions.typeIdentifier)
} }
/** /**
...@@ -731,7 +734,7 @@ function isLibrary (node: ContractDefinitionAstNode): boolean { ...@@ -731,7 +734,7 @@ function isLibrary (node: ContractDefinitionAstNode): boolean {
* @return {bool} * @return {bool}
*/ */
function isCallToNonConstLocalFunction (node: FunctionCallAstNode): boolean { function isCallToNonConstLocalFunction (node: FunctionCallAstNode): boolean {
return isLocalCall(node) && !expressionType(node, basicRegex.CONSTANTFUNCTIONTYPE) return isLocalCall(node) && !expressionTypeDescription(node, basicRegex.CONSTANTFUNCTIONTYPE)
} }
/** /**
...@@ -862,8 +865,8 @@ function isLLSend (node: MemberAccessAstNode): boolean { ...@@ -862,8 +865,8 @@ function isLLSend (node: MemberAccessAstNode): boolean {
*/ */
function isLLCall (node: MemberAccessAstNode): boolean { function isLLCall (node: MemberAccessAstNode): boolean {
return isMemberAccess(node, return isMemberAccess(node,
exactMatch(util.escapeRegExp(lowLevelCallTypes.CALL.type)), exactMatch(util.escapeRegExp(lowLevelCallTypes['CALL-v0.5'].type)),
undefined, exactMatch(basicTypes.ADDRESS), exactMatch(lowLevelCallTypes.CALL.ident)) undefined, exactMatch(basicTypes.ADDRESS), exactMatch(lowLevelCallTypes['CALL-v0.5'].ident))
} }
/** /**
...@@ -895,8 +898,8 @@ function isLLCallcode (node: MemberAccessAstNode): boolean { ...@@ -895,8 +898,8 @@ function isLLCallcode (node: MemberAccessAstNode): boolean {
*/ */
function isLLDelegatecall (node: MemberAccessAstNode): boolean { function isLLDelegatecall (node: MemberAccessAstNode): boolean {
return isMemberAccess(node, return isMemberAccess(node,
exactMatch(util.escapeRegExp(lowLevelCallTypes.DELEGATECALL.type)), exactMatch(util.escapeRegExp(lowLevelCallTypes['DELEGATECALL-v0.5'].type)),
undefined, exactMatch(basicTypes.ADDRESS), exactMatch(lowLevelCallTypes.DELEGATECALL.ident)) undefined, exactMatch(basicTypes.ADDRESS), exactMatch(lowLevelCallTypes['DELEGATECALL-v0.5'].ident))
} }
/** /**
...@@ -959,10 +962,15 @@ function isBytesLengthCheck (node: MemberAccessAstNode): boolean { ...@@ -959,10 +962,15 @@ function isBytesLengthCheck (node: MemberAccessAstNode): boolean {
// #################### Complex Node Identification - Private // #################### Complex Node Identification - Private
function isMemberAccess (node: MemberAccessAstNode, retType: string, accessor: string| undefined, accessorType: string, memberName: string | undefined): boolean { function isMemberAccess (node: MemberAccessAstNode, retType: string, accessor: string| undefined, accessorType: string, memberName: string | undefined): boolean {
return typeDescription(node, retType) && const nodeTypeDef: boolean = typeDescription(node, retType)
memName(node, memberName) && console.log('MemberAccess typeDef ->',nodeTypeDef)
memName(node.expression, accessor) && const nodeMemName: boolean = memName(node, memberName)
expressionTypeDescription(node.expression, accessorType) console.log('MemberAccess nodeMemName ->',nodeMemName)
const nodeExpMemName: boolean = memName(node.expression, accessor)
console.log('MemberAccess nodeExpMemName ->',nodeExpMemName)
const nodeExpTypeDef: boolean = expressionTypeDescription(node, accessorType)
console.log('MemberAccess nodeExpTypeDef ->',nodeExpTypeDef)
return nodeTypeDef && nodeMemName && nodeExpTypeDef && nodeExpMemName
} }
function isSpecialVariableAccess (node: MemberAccessAstNode, varType: any): boolean { function isSpecialVariableAccess (node: MemberAccessAstNode, varType: any): boolean {
...@@ -979,9 +987,9 @@ function isSpecialVariableAccess (node: MemberAccessAstNode, varType: any): bool ...@@ -979,9 +987,9 @@ function isSpecialVariableAccess (node: MemberAccessAstNode, varType: any): bool
// return (node && (nr === undefined || nr === null)) || (node && nr === 0 && !node.children) || (node && node.children && node.children.length >= nr) // return (node && (nr === undefined || nr === null)) || (node && nr === 0 && !node.children) || (node && node.children && node.children.length >= nr)
// } // }
function expressionType (node, typeRegex) { // function expressionType (node, typeRegex) {
return new RegExp(typeRegex).test(node.expression.typeDescriptions.typeString) // return new RegExp(typeRegex).test(node.expression.typeDescriptions.typeString)
} // }
function expressionTypeDescription (node, typeRegex) { function expressionTypeDescription (node, typeRegex) {
return new RegExp(typeRegex).test(node.expression.typeDescriptions.typeString) return new RegExp(typeRegex).test(node.expression.typeDescriptions.typeString)
...@@ -996,8 +1004,8 @@ function nodeType (node, typeRegex) { ...@@ -996,8 +1004,8 @@ function nodeType (node, typeRegex) {
} }
function memName (node, memNameRegex) { function memName (node, memNameRegex) {
const regex = new RegExp(memNameRegex) // const regex = new RegExp(memNameRegex)
return regex.test(node.name) || regex.test(node.memberName) return (node && !memNameRegex) || new RegExp(memNameRegex).test(node.name) || new RegExp(memNameRegex).test(node.memberName)
} }
function operator (node, opRegex) { function operator (node, opRegex) {
...@@ -1056,7 +1064,7 @@ function buildAbiSignature (funName: string, paramTypes: any[]): string { ...@@ -1056,7 +1064,7 @@ function buildAbiSignature (funName: string, paramTypes: any[]): string {
const helpers = { const helpers = {
// nrOfChildren, // nrOfChildren,
// minNrOfChildren, // minNrOfChildren,
expressionType, expressionTypeDescription,
nodeType, nodeType,
memName, memName,
operator, operator,
...@@ -1086,7 +1094,7 @@ export { ...@@ -1086,7 +1094,7 @@ export {
getLibraryCallMemberName, getLibraryCallMemberName,
getFullQualifiedFunctionCallIdent, getFullQualifiedFunctionCallIdent,
getFullQuallyfiedFuncDefinitionIdent, getFullQuallyfiedFuncDefinitionIdent,
getStateVariableDeclarationsFormContractNode, getStateVariableDeclarationsFromContractNode,
getFunctionOrModifierDefinitionParameterPart, getFunctionOrModifierDefinitionParameterPart,
getFunctionDefinitionReturnParameterPart, getFunctionDefinitionReturnParameterPart,
getUnAssignedTopLevelBinOps, getUnAssignedTopLevelBinOps,
......
{
"abstract": false,
"baseContracts": [],
"contractDependencies": [],
"contractKind": "contract",
"documentation": null,
"fullyImplemented": true,
"id": 8,
"linearizedBaseContracts":
[
8
],
"name": "C",
"nodeType": "ContractDefinition",
"nodes":
[
{
"body":
{
"id": 6,
"nodeType": "Block",
"src": "42:23:0",
"statements":
[
{
"assignments":
[
4
],
"declarations":
[
{
"constant": false,
"id": 4,
"name": "a",
"nodeType": "VariableDeclaration",
"overrides": null,
"scope": 6,
"src": "52:6:0",
"stateVariable": false,
"storageLocation": "default",
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
},
"typeName":
{
"id": 3,
"name": "uint",
"nodeType": "ElementaryTypeName",
"src": "52:4:0",
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
}
},
"value": null,
"visibility": "internal"
}
],
"id": 5,
"initialValue": null,
"nodeType": "VariableDeclarationStatement",
"src": "52:6:0"
}
]
},
"documentation": null,
"functionSelector": "26121ff0",
"id": 7,
"implemented": true,
"kind": "function",
"modifiers": [],
"name": "f",
"nodeType": "FunctionDefinition",
"overrides": null,
"parameters":
{
"id": 1,
"nodeType": "ParameterList",
"parameters": [],
"src": "27:2:0"
},
"returnParameters":
{
"id": 2,
"nodeType": "ParameterList",
"parameters": [],
"src": "42:0:0"
},
"scope": 8,
"src": "17:48:0",
"stateMutability": "pure",
"virtual": false,
"visibility": "public"
}
],
"scope": 9,
"src": "0:67:0"
}
\ No newline at end of file
{ {
"attributes": { "argumentTypes": null,
"member_name": "info", "arguments": [],
"type": "function () payable external returns (uint256)" "expression":
}, {
"children": [ "argumentTypes": [],
"expression":
{
"argumentTypes": null,
"arguments":
[
{ {
"attributes": { "argumentTypes": null,
"type": "contract InfoFeed", "hexValue": "30",
"value": "f" "id": 9,
"isConstant": false,
"isLValue": false,
"isPure": true,
"kind": "number",
"lValueRequested": false,
"nodeType": "Literal",
"src": "83:1:0",
"subdenomination": null,
"typeDescriptions":
{
"typeIdentifier": "t_rational_0_by_1",
"typeString": "int_const 0"
}, },
"id": 30, "value": "0"
"name": "Identifier", }
"src": "405:1:0" ],
"expression":
{
"argumentTypes":
[
{
"typeIdentifier": "t_rational_0_by_1",
"typeString": "int_const 0"
} }
], ],
"id": 32, "id": 8,
"name": "MemberAccess", "name": "c",
"src": "405:6:0" "nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 5,
"src": "81:1:0",
"typeDescriptions":
{
"typeIdentifier": "t_type$_t_contract$_c_$5_$",
"typeString": "type(contract c)"
}
},
"id": 10,
"isConstant": false,
"isLValue": false,
"isPure": true,
"kind": "typeConversion",
"lValueRequested": false,
"names": [],
"nodeType": "FunctionCall",
"src": "81:4:0",
"tryCall": false,
"typeDescriptions":
{
"typeIdentifier": "t_contract$_c_$5",
"typeString": "contract c"
}
},
"id": 11,
"isConstant": false,
"isLValue": false,
"isPure": false,
"lValueRequested": false,
"memberName": "f",
"nodeType": "MemberAccess",
"referencedDeclaration": 4,
"src": "81:6:0",
"typeDescriptions":
{
"typeIdentifier": "t_function_external_nonpayable$__$returns$__$",
"typeString": "function () external"
}
},
"id": 12,
"isConstant": false,
"isLValue": false,
"isPure": false,
"kind": "functionCall",
"lValueRequested": false,
"names": [],
"nodeType": "FunctionCall",
"src": "81:8:0",
"tryCall": false,
"typeDescriptions":
{
"typeIdentifier": "t_tuple$__$",
"typeString": "tuple()"
}
} }
\ No newline at end of file
module.exports = { module.exports = {
localCall: require('./localCall.json'), localCall: require('./localCall.json'),
contractDefinition: require('./contractDefinition.json'),
unaryOperation: require('./unaryOperation.json'),
nowAst: require('./nowAst.json'),
thisLocalCall: require('./thisLocalCall.json'), thisLocalCall: require('./thisLocalCall.json'),
libCall: require('./libCall.json'), libCall: require('./libCall.json'),
externalDirect: require('./externalDirect.json'), externalDirect: require('./externalDirect.json'),
......
{ { "argumentTypes": null,
"argumentTypes": null, "arguments":
[ { "argumentTypes": null,
"id": 95,
"name": "knownValues",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 79,
"src": "1392:11:0",
"typeDescriptions": { "typeIdentifier": "t_struct$_Data_$6_storage",
"typeString": "struct Set.Data storage ref" } },
{ "argumentTypes": null,
"id": 96,
"name": "value",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 81,
"src": "1405:5:0",
"typeDescriptions": { "typeIdentifier": "t_uint256", "typeString": "uint256" } } ],
"expression":
{ "argumentTypes": [ { "typeIdentifier": "t_struct$_Data_$6_storage",
"typeString": "struct Set.Data storage ref" },
{ "typeIdentifier": "t_uint256', typeString: 'uint256" } ],
"expression": "expression":
{ { "argumentTypes": null,
"argumentTypes": null, "id": 93,
"id": 33, "name": "Set",
"name": "L",
"nodeType": "Identifier", "nodeType": "Identifier",
"overloadedDeclarations": [], "overloadedDeclarations": [],
"referencedDeclaration": 21, "referencedDeclaration": 77,
"src": "244:1:0", "src": "1381:3:0",
"typeDescriptions": "typeDescriptions": { "typeIdentifier": "t_type$_t_contract$_Set_$77_$",
{ "typeString": "type(library Set)" } },
"typeIdentifier": "t_type$_t_contract$_L_$21_$", "id": 94,
"typeString": "type(library L)"
}
},
"id": 34,
"isConstant": false, "isConstant": false,
"isLValue": false, "isLValue": false,
"isPure": false, "isPure": false,
"lValueRequested": false, "lValueRequested": false,
"memberName": "f", "memberName": "insert",
"nodeType": "MemberAccess", "nodeType": "MemberAccess",
"referencedDeclaration": 6, "referencedDeclaration": 33,
"src": "244:3:0", "src": "1381:10:0",
"typeDescriptions": "typeDescriptions":
{ { "typeIdentifier":
"typeIdentifier": "t_function_delegatecall_nonpayable$_t_uint256_$returns$__$", "t_function_delegatecall_nonpayable$_t_struct$_Data_$6_storage_ptr_$_t_uint256_$returns$_t_bool_$",
"typeString": "function (uint256)" "typeString":
} "function (struct Set.Data storage pointer,uint256) returns (bool)" } },
"id": 97,
"isConstant": false,
"isLValue": false,
"isPure": false,
"kind": "functionCall",
"lValueRequested": false,
"names": [],
"nodeType": "FunctionCall",
"src": "1381:30:0",
"tryCall": false,
"typeDescriptions": { "typeIdentifier": "t_bool", "typeString": "bool" }
} }
\ No newline at end of file
{ {
"argumentTypes": null, "argumentTypes": null,
"arguments": [], "arguments":
[
{
"argumentTypes": null,
"hexValue": "616263",
"id": 15,
"isConstant": false,
"isLValue": false,
"isPure": true,
"kind": "string",
"lValueRequested": false,
"nodeType": "Literal",
"src": "115:5:0",
"subdenomination": null,
"typeDescriptions":
{
"typeIdentifier": "t_stringliteral_4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45",
"typeString": "literal_string \"abc\""
},
"value": "abc"
},
{
"argumentTypes": null,
"hexValue": "38",
"id": 16,
"isConstant": false,
"isLValue": false,
"isPure": true,
"kind": "number",
"lValueRequested": false,
"nodeType": "Literal",
"src": "125:1:0",
"subdenomination": null,
"typeDescriptions":
{
"typeIdentifier": "t_rational_8_by_1",
"typeString": "int_const 8"
},
"value": "8"
}
],
"expression": "expression":
{ {
"argumentTypes": [], "argumentTypes":
"id": 13, [
"name": "sha3", {
"typeIdentifier": "t_stringliteral_4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45",
"typeString": "literal_string \"abc\""
},
{
"typeIdentifier": "t_rational_8_by_1",
"typeString": "int_const 8"
}
],
"id": 14,
"name": "e",
"nodeType": "Identifier", "nodeType": "Identifier",
"overloadedDeclarations": [], "overloadedDeclarations": [],
"referencedDeclaration": 8, "referencedDeclaration": 6,
"src": "129:4:0", "src": "109:1:0",
"typeDescriptions": "typeDescriptions":
{ {
"typeIdentifier": "t_function_internal_pure$__$returns$_t_bool_$", "typeIdentifier": "t_function_event_nonpayable$_t_uint256_$_t_string_memory_ptr_$returns$__$",
"typeString": "function () pure returns (bool)" "typeString": "function (uint256,string memory)"
} }
}, },
"id": 14, "id": 17,
"isConstant": false, "isConstant": false,
"isLValue": false, "isLValue": false,
"isPure": false, "isPure": false,
"kind": "functionCall", "kind": "functionCall",
"lValueRequested": false, "lValueRequested": false,
"names": [], "names":
[
"b",
"a"
],
"nodeType": "FunctionCall", "nodeType": "FunctionCall",
"src": "129:6:0", "src": "109:19:0",
"tryCall": false, "tryCall": false,
"typeDescriptions": "typeDescriptions":
{ {
"typeIdentifier": "t_bool", "typeIdentifier": "t_tuple$__$",
"typeString": "bool" "typeString": "tuple()"
} }
} }
\ No newline at end of file
{
"argumentTypes": null,
"id": 11,
"name": "now",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": -17,
"src": "110:3:0",
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
}
}
\ No newline at end of file
{ {
"node1": { "node1": {
"attributes": { "constant": false,
"name": "x", "id": 20,
"type": "struct Ballot.Voter storage pointer" "name": "c",
}, "nodeType": "VariableDeclaration",
"children": [ "overrides": null,
"scope": 33,
"src": "174:11:0",
"stateVariable": false,
"storageLocation": "storage",
"typeDescriptions":
{ {
"attributes": { "typeIdentifier": "t_struct$_S_$3_storage_ptr",
"name": "Voter" "typeString": "struct C.S"
}, },
"id": 43, "typeName":
"name": "UserDefinedTypeName", {
"src": "604:5:0" "contractScope": null,
"id": 19,
"name": "S",
"nodeType": "UserDefinedTypeName",
"referencedDeclaration": 3,
"src": "174:1:0",
"typeDescriptions":
{
"typeIdentifier": "t_struct$_S_$3_storage_ptr",
"typeString": "struct C.S"
} }
], },
"id": 44, "value": null,
"name": "VariableDeclaration", "visibility": "internal"
"src": "604:15:0"
}, },
"node2": { "node2": {
"attributes": { "constant": false,
"name": "voters", "id": 11,
"type": "mapping(address => struct Ballot.Voter storage ref)" "name": "",
"nodeType": "VariableDeclaration",
"overrides": null,
"scope": 16,
"src": "82:29:0",
"stateVariable": false,
"storageLocation": "storage",
"typeDescriptions":
{
"typeIdentifier": "t_array$_t_mapping$_t_uint256_$_t_uint256_$_$dyn_storage_ptr",
"typeString": "mapping(uint256 => uint256)[]"
}, },
"children": [ "typeName":
{
"baseType":
{
"id": 9,
"keyType":
{ {
"children": [ "id": 7,
"name": "uint",
"nodeType": "ElementaryTypeName",
"src": "90:4:0",
"typeDescriptions":
{ {
"attributes": { "typeIdentifier": "t_uint256",
"name": "address" "typeString": "uint256"
}
}, },
"id": 16, "nodeType": "Mapping",
"name": "ElementaryTypeName", "src": "82:19:0",
"src": "235:7:0" "typeDescriptions":
{
"typeIdentifier": "t_mapping$_t_uint256_$_t_uint256_$",
"typeString": "mapping(uint256 => uint256)"
}, },
"valueType":
{ {
"attributes": { "id": 8,
"name": "Voter" "name": "uint",
"nodeType": "ElementaryTypeName",
"src": "96:4:0",
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
}
}
}, },
"id": 17, "id": 10,
"name": "UserDefinedTypeName", "length": null,
"src": "246:5:0" "nodeType": "ArrayTypeName",
"src": "82:21:0",
"typeDescriptions":
{
"typeIdentifier": "t_array$_t_mapping$_t_uint256_$_t_uint256_$_$dyn_storage_ptr",
"typeString": "mapping(uint256 => uint256)[]"
} }
],
"id": 18,
"name": "Mapping",
"src": "227:25:0"
} }
],
"id": 19,
"name": "VariableDeclaration",
"src": "227:32:0"
}, },
"node3": { "node3": {
"attributes": { "constant": false,
"name": "voters", "id": 125,
"type": "bytes32" "name": "f",
}, "nodeType": "VariableDeclaration",
"children": [ "overrides": null,
"scope": 160,
"src": "1005:14:0",
"stateVariable": false,
"storageLocation": "memory",
"typeDescriptions":
{ {
"attributes": { "typeIdentifier": "t_bytes_memory_ptr",
"name": "bytes" "typeString": "bytes"
}, },
"id": 16, "typeName":
"name": "ElementaryTypeName", {
"src": "235:7:0" "id": 124,
"name": "bytes",
"nodeType": "ElementaryTypeName",
"src": "1005:5:0",
"typeDescriptions":
{
"typeIdentifier": "t_bytes_storage_ptr",
"typeString": "bytes"
} }
], },
"id": 19, "value": null,
"name": "VariableDeclaration", "visibility": "internal"
"src": "227:32:0"
} }
} }
\ No newline at end of file
{ {
"argumentTypes": null,
"arguments": [],
"expression":
{
"argumentTypes": [], "argumentTypes": [],
"expression": "expression":
{ {
...@@ -29,4 +33,20 @@ ...@@ -29,4 +33,20 @@
"typeIdentifier": "t_function_internal_pure$__$returns$__$", "typeIdentifier": "t_function_internal_pure$__$returns$__$",
"typeString": "function () pure" "typeString": "function () pure"
} }
},
"id": 13,
"isConstant": false,
"isLValue": false,
"isPure": false,
"kind": "functionCall",
"lValueRequested": false,
"names": [],
"nodeType": "FunctionCall",
"src": "162:9:0",
"tryCall": false,
"typeDescriptions":
{
"typeIdentifier": "t_tuple$__$",
"typeString": "tuple()"
}
} }
\ No newline at end of file
{ {
"argumentTypes": null, "argumentTypes": null,
"arguments": [],
"expression":
{
"argumentTypes": [],
"expression": "expression":
{ {
"argumentTypes": null, "argumentTypes": null,
"id": 13, "id": 10,
"name": "this", "name": "this",
"nodeType": "Identifier", "nodeType": "Identifier",
"overloadedDeclarations": [], "overloadedDeclarations": [],
"referencedDeclaration": -28, "referencedDeclaration": -28,
"src": "138:4:0", "src": "99:4:0",
"typeDescriptions": "typeDescriptions":
{ {
"typeIdentifier": "t_contract$_C_$21", "typeIdentifier": "t_contract$_C_$26",
"typeString": "contract C" "typeString": "contract C"
} }
}, },
"id": 14, "id": 11,
"isConstant": false, "isConstant": false,
"isLValue": false, "isLValue": false,
"isPure": false, "isPure": false,
"lValueRequested": false, "lValueRequested": false,
"memberName": "h", "memberName": "f",
"nodeType": "MemberAccess", "nodeType": "MemberAccess",
"referencedDeclaration": 4, "referencedDeclaration": 25,
"src": "138:6:0", "src": "99:6:0",
"typeDescriptions":
{
"typeIdentifier": "t_function_external_nonpayable$__$returns$_t_uint256_$_t_uint256_$",
"typeString": "function () external returns (uint256,uint256)"
}
},
"id": 12,
"isConstant": false,
"isLValue": false,
"isPure": false,
"kind": "functionCall",
"lValueRequested": false,
"names": [],
"nodeType": "FunctionCall",
"src": "99:8:0",
"tryCall": true,
"typeDescriptions": "typeDescriptions":
{ {
"typeIdentifier": "t_function_external_payable$__$returns$__$", "typeIdentifier": "t_tuple$_t_uint256_$_t_uint256_$",
"typeString": "function () payable external" "typeString": "tuple(uint256,uint256)"
} }
} }
\ No newline at end of file
{
"argumentTypes": null,
"id": 13,
"isConstant": false,
"isLValue": false,
"isPure": false,
"lValueRequested": false,
"nodeType": "UnaryOperation",
"operator": "++",
"prefix": false,
"src": "95:3:0",
"subExpression":
{
"argumentTypes": null,
"id": 12,
"name": "x",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 4,
"src": "95:1:0",
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
}
},
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
}
}
\ No newline at end of file
pragma solidity >=0.4.9 <0.7.0; pragma solidity >=0.4.9 <0.7.0;
contract Set { library Set {
// We define a new struct datatype that will be used to // We define a new struct datatype that will be used to
// hold its data in the calling contract. // hold its data in the calling contract.
// struct Data { uint flags; } struct Data { mapping(uint => bool) flags; }
// Note that the first parameter is of type "storage // Note that the first parameter is of type "storage
// reference" and thus only its storage address and not // reference" and thus only its storage address and not
...@@ -11,35 +11,35 @@ contract Set { ...@@ -11,35 +11,35 @@ contract Set {
// special feature of library functions. It is idiomatic // special feature of library functions. It is idiomatic
// to call the first parameter 'self', if the function can // to call the first parameter 'self', if the function can
// be seen as a method of that object. // be seen as a method of that object.
//function insert(Data memory self, uint value) public function insert(Data storage self, uint value) public
// returns (bool) returns (bool)
//{ {
// if (self.flags[value]) if (self.flags[value])
// return false; // already there return false; // already there
// self.flags[value] = true; self.flags[value] = true;
// return true; return true;
// } }
// function remove(Data memory self, uint value) public function remove(Data storage self, uint value) public
// returns (bool) returns (bool)
// { {
// if (!self.flags[value]) if (!self.flags[value])
// return false; // not there return false; // not there
// self.flags[value] = false; self.flags[value] = false;
// return true; return true;
// } }
function contains(uint value) public pure function contains(Data storage self, uint value) public
returns (uint) returns (bool)
{ {
return value; return self.flags[value];
} }
} }
contract C { contract C {
Set x; Set.Data knownValues;
function register(uint value) public { function register(uint value) public {
// The library functions can be called without a // The library functions can be called without a
...@@ -47,18 +47,8 @@ contract C { ...@@ -47,18 +47,8 @@ contract C {
// "instance" will be the current contract. // "instance" will be the current contract.
address payable a; address payable a;
a.send(10 wei); a.send(10 wei);
//if (!Set.insert(knownValues, value)) if (!Set.insert(knownValues, value))
// revert(); revert();
}
function tests2() public {
x = Set(0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c);
uint y = x.contains(103);
if(y == 103){
y++;
} else {
y--;
}
} }
// In this contract, we can also directly access knownValues.flags, if we want. // In this contract, we can also directly access knownValues.flags, if we want.
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment