Static Analyzer for Solidity
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
slither/utils/similarity/test.py

55 lines
1.7 KiB

import argparse
import logging
import sys
import traceback
import operator
import numpy as np
from .model import load_model
from .encode import encode_contract, load_and_encode, parse_target
from .cache import save_cache
from .similarity import similarity
6 years ago
logger = logging.getLogger("Slither-simil")
def test(args):
try:
model = args.model
model = load_model(model)
filename = args.filename
contract, fname = parse_target(args.fname)
infile = args.input
6 years ago
ntop = args.ntop
6 years ago
if filename is None or contract is None or fname is None or infile is None:
logger.error('The test mode requires filename, contract, fname and input parameters.')
sys.exit(-1)
irs = encode_contract(filename, **vars(args))
6 years ago
if len(irs) == 0:
sys.exit(-1)
6 years ago
y = " ".join(irs[(filename,contract,fname)])
fvector = model.get_sentence_vector(y)
6 years ago
cache = load_and_encode(infile, model, **vars(args))
#save_cache("cache.npz", cache)
r = dict()
for x,y in cache.items():
r[x] = similarity(fvector, y)
r = sorted(r.items(), key=operator.itemgetter(1), reverse=True)
6 years ago
logger.info("Reviewed %d functions, listing the %d most similar ones:", len(r), ntop)
format_table = "{: <65} {: <20} {: <20} {: <10}"
logger.info(format_table.format(*["filename", "contract", "function", "score"]))
6 years ago
for x,score in r[:ntop]:
6 years ago
score = str(round(score, 3))
logger.info(format_table.format(*(list(x)+[score])))
except Exception:
logger.error('Error in %s' % args.filename)
logger.error(traceback.format_exc())
sys.exit(-1)