Exemple #1
0
    def _initProcessingStateTable(self,
                                  dbsession: session,
                                  submitInteral: int = 100):
        if dbsession.query(ProcessingState).first():
            logger.warning("error")
            return

        for root, dirs, files in os.walk(self.allow_paths):

            interval: int = 0
            templist = []
            for file in files:
                if file.endswith("txt") or file.endswith("sol"):
                    sol_version_set: set = self._getSolidityVersions(file)
                    templist.append(
                        ProcessingState(
                            contractAddr=os.path.splitext(file)[0],
                            fullyextracted=0 if len(sol_version_set) else -1,
                            solc_versions=";".join(sol_version_set)))
                    interval = interval + 1
                    if interval % submitInteral == 0:  #
                        dbsession.add_all(templist)
                        templist = []  #
                        dbsession.flush()
                        dbsession.commit()  #
                        print("sumbit: %d times" %
                              ((interval - 1) // submitInteral + 1))

            if templist:
                dbsession.add_all(templist)
                dbsession.flush()
                dbsession.commit()
                print("sumbit: %d times" %
                      ((interval - 1) // submitInteral + 1))
Exemple #2
0
    def _extractCountMetric(self, dbsession: session, contractAddress: str,
                            rootNode: solcast.nodes.NodeBase) -> bool:

        try:

            cmResult: CountMetric = CountMetricCalculator(
                contract_addr=contractAddress,
                contract_path=os.path.join(
                    self.contract_root_path, contractAddress + "." +
                    self.contract_src_path_skeleton.split(".")[1]),
                rootNode=rootNode).getCountMetrics()
        except Exception as e:
            logger.error(
                "|_____Extracting CountMetric for Contract:{contract} ERROR: {emsg}"
                .format(contract=contractAddress, emsg=e))
            return False
        else:
            #
            dbsession.add(cmResult)
            dbsession.commit()
            dbsession.flush()
            return True
Exemple #3
0
    def extractBatchContractFeature(self,
                                    dbsession: session,
                                    min_id: int = 0,
                                    max_id: int = 60000,
                                    batchSize: int = 500):

        processinglist:List[ProcessingState] = dbsession.query(ProcessingState)\
                                            .filter(ProcessingState.id.between(min_id,max_id))\
                                            .filter(ProcessingState.fullyextracted == 0)\
                                            .limit(batchSize)
        if not processinglist:
            logger.warning(
                "All Smart Contracts Have Been Successfully Feature-Extracted")

        #
        SCid: int = 1
        for processing in processinglist:
            # time.sleep(0.2)
            print("当前抽取特征的批次完成进度: %d / %d" % (SCid, batchSize))
            contractAddr = processing.contractAddr
            logger.info("Extracting Features for Contract:{contract}".format(
                contract=contractAddr))

            #
            cpmState = processing.complexityMetricExtracted
            ctmState = processing.countMetricExtracted
            oomState = processing.objectOrientedMetricExtracted
            lrmState = processing.languageRelatedMetricExtracted

            allState = all([cpmState, ctmState, oomState, lrmState])
            rootNode: solcast.nodes.NodeBase = self._getRootNode(
                contractAddr=contractAddr,
                versionString=processing.solc_versions)
            print(processing)
            try:

                if not processing.complexityMetricExtracted:
                    cpmState: bool = self._extractComplexityMetric(
                        dbsession, contractAddr, rootNode=rootNode)

                    if cpmState:
                        logger.info(
                            "|___Extracting ComplexityMetric for Contract:{contract} Successfully."
                            .format(contract=contractAddr))
                    else:
                        logger.error(
                            "|___Extracting ComplexityMetric for Contract:{contract} Failed!."
                            .format(contract=contractAddr))
                    assert cpmState == True

                if not processing.countMetricExtracted:
                    ctmState: bool = self._extractCountMetric(
                        dbsession,
                        contractAddress=contractAddr,
                        rootNode=rootNode)
                    if ctmState:
                        logger.info(
                            "|___Extracting CountMetric Features for Contract:{contract} Successfully."
                            .format(contract=contractAddr))
                    else:
                        logger.error(
                            "|___Extracting CountMetric Features for Contract:{contract} Failed!."
                            .format(contract=contractAddr))
                    assert ctmState == True

                if not processing.objectOrientedMetricExtracted:
                    oomState: bool = self._extractObjectOrientedMetric(
                        dbsession,
                        contractAddress=contractAddr,
                        rootNode=rootNode)
                    if oomState:
                        logger.info(
                            "|___Extracting ObjectOrientedMetric Features for Contract:{contract} Successfully."
                            .format(contract=contractAddr))
                    else:
                        logger.error(
                            "|___Extracting ObjectOrientedMetric Features for Contract:{contract} Failed!."
                            .format(contract=contractAddr))
                    assert oomState == True

                if not processing.languageRelatedMetricExtracted:
                    lrmState: bool = self._extractLanguageRelatedMetric(
                        dbsession,
                        contractAddress=contractAddr,
                        rootNode=rootNode)
                    if lrmState:
                        logger.info(
                            "|___Extracting LanguageRelatedMetric Features for Contract:{contract} Successfully."
                            .format(contract=contractAddr))
                    else:
                        logger.error(
                            "|___Extracting LanguageRelatedMetric Features for Contract:{contract} Failed!."
                            .format(contract=contractAddr))
                    assert lrmState == True

            except Exception as e:
                logger.error(
                    "|___Extracting Features for Contract:{contract} ERROR: {emsg}"
                    .format(contract=contractAddr, emsg=e))

                break
            else:
                allState = all([cpmState, ctmState, oomState, lrmState])
                if allState:
                    logger.success(
                        "Extracting Features for Contract:{contract} Full SUCCESSFULLY!"
                        .format(contract=contractAddr))
                else:
                    logger.warning(
                        "Extracting Features for Contract:{contract} PARTIAL SUCCESSFULLY!"
                        .format(contract=contractAddr))
            finally:
                processing.fullyextracted = allState
                processing.complexityMetricExtracted = cpmState
                processing.countMetricExtracted = ctmState
                processing.objectOrientedMetricExtracted = oomState
                processing.languageRelatedMetricExtracted = lrmState
                dbsession.commit()
                dbsession.flush()
                logger.success(
                    "Update Information on ProcessingState Table for  Contract:{contract} successfully!"
                    .format(contract=contractAddr))
                SCid = SCid + 1