| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| """Dataset of Slither audited Solidity Smart Contracts.""" |
|
|
| import json |
|
|
| import datasets |
| import pandas as pd |
|
|
| _LABELS = { |
| 'all': [ |
| 'uninitialized-state','constant-function-asm', 'locked-ether', |
| 'incorrect-shift', 'divide-before-multiply', 'unused-return', |
| 'write-after-write', 'reentrancy-no-eth', 'unchecked-lowlevel', |
| 'incorrect-equality', 'weak-prng', 'arbitrary-send', |
| 'uninitialized-local', 'reentrancy-eth', 'shadowing-abstract', |
| 'controlled-delegatecall', 'unchecked-transfer', 'erc20-interface', |
| 'controlled-array-length', 'tautology', 'shadowing-state', |
| 'tx-origin', 'unprotected-upgrade', 'suicidal', |
| 'boolean-cst', 'unchecked-send', 'msg-value-loop', |
| 'erc721-interface', 'constant-function-state', 'delegatecall-loop', |
| 'mapping-deletion', 'reused-constructor', 'uninitialized-storage', |
| 'public-mappings-nested', 'array-by-reference','backdoor', |
| 'rtlo', 'name-reused','safe'], |
| 'big': ['access-control', 'arithmetic', 'other', 'reentrancy', 'safe', 'unchecked-calls'], |
| 'small': ['access-control', 'arithmetic', 'other', 'reentrancy', 'safe', 'unchecked-calls', 'locked-ether', 'bad-randomness', 'double-spending'] |
| } |
|
|
|
|
| _CITATION = """\ |
| @misc{rossini2022slitherauditedcontracts, |
| title = {Slither Audited Smart Contracts Dataset}, |
| author={Martina Rossini}, |
| year={2022} |
| } |
| """ |
|
|
|
|
| _DESCRIPTION = """\ |
| This dataset contains source code and deployed bytecode for Solidity Smart Contracts \ |
| that have been verified on Etherscan.io, along with a classification of their vulnerabilities \ |
| according to the Slither static analysis framework. |
| """ |
|
|
| _HOMEPAGE = "https://github.com/mwritescode/slither-audited-smart-contracts" |
|
|
| _LICENSE = "MIT" |
|
|
|
|
| _URLS = { |
| "raw": [f"data/raw/contracts{i}.parquet" for i in range(9)], |
| "label_mappings": "data/label_mappings.json", |
| "big-splits": "data/big-splits.csv", |
| "small-splits": "data/small-splits.csv" |
| } |
|
|
|
|
| class SlitherAuditedSmartContracts(datasets.GeneratorBasedBuilder): |
| """Slither Audited Smart Contracts dataset, including source code and deployed bytecode""" |
|
|
| VERSION = datasets.Version("1.1.0") |
|
|
| |
| |
| BUILDER_CONFIGS = [ |
| datasets.BuilderConfig(name="all-plain-text", version=VERSION, description="Complete dataset with plain-text slither results"), |
| datasets.BuilderConfig(name="all-multilabel", version=VERSION, description="Complete dataset with slither results as sequence of labels"), |
| datasets.BuilderConfig(name="big-plain-text", version=VERSION, description="Dataset containing only labels having numerous examples with plain-text slither results"), |
| datasets.BuilderConfig(name="big-multilabel", version=VERSION, description="Dataset containing only labels having numerous examples with slither results as a sequence of labels"), |
| datasets.BuilderConfig(name="small-plain-text", version=VERSION, description="Dataset containing only labels having few examples with plain-text slither results"), |
| datasets.BuilderConfig(name="small-multilabel", version=VERSION, description="Dataset containing only labels having few examples with slither results as a sequence of labels") |
| ] |
|
|
| def _info(self): |
| if "plain-text" in self.config.name: |
| features = datasets.Features( |
| { |
| "address": datasets.Value("string"), |
| "source_code": datasets.Value("string"), |
| "bytecode": datasets.Value("string"), |
| "slither": datasets.Value("string"), |
| } |
| ) |
| else: |
| features = datasets.Features( |
| { |
| "address": datasets.Value("string"), |
| "source_code": datasets.Value("string"), |
| "bytecode": datasets.Value("string"), |
| "slither": datasets.Sequence( |
| datasets.features.ClassLabel( |
| names=_LABELS[self.config.name.split('-')[0]] |
| ) |
| ) |
| |
| } |
| ) |
| return datasets.DatasetInfo( |
| description=_DESCRIPTION, |
| features=features, |
| homepage=_HOMEPAGE, |
| license=_LICENSE, |
| citation=_CITATION, |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| data_dir = dl_manager.download_and_extract(_URLS) |
|
|
| generators = [ |
| datasets.SplitGenerator( |
| name=datasets.Split.TRAIN, |
| gen_kwargs={ |
| "filepath": data_dir, |
| "split": "train" |
| }, |
| )] |
| if self.config.name.split('-')[0] != 'all': |
| generators += [ |
| datasets.SplitGenerator( |
| name=datasets.Split.TEST, |
| gen_kwargs={ |
| "filepath": data_dir, |
| "split": "test" |
| }, |
| ), |
| datasets.SplitGenerator( |
| name=datasets.Split.VALIDATION, |
| gen_kwargs={ |
| "filepath": data_dir, |
| "split": "val" |
| }, |
| )] |
| return generators |
|
|
| def __elaborate_results(self, slither_res, mappings): |
| if not slither_res["results"]: |
| contract_class = ["safe"] |
| else: |
| contract_class = [elem["check"] for elem in slither_res["results"]["detectors"]] |
| if self.config.name.split('-')[0] != 'all': |
| with open(mappings, 'r') as mappings_file: |
| class_mappings = json.load(mappings_file) |
| contract_class = list(set([class_mappings[cls] for cls in contract_class]) - {'ignore'}) |
| if len(contract_class) == 0: |
| contract_class = ['safe'] |
| return contract_class |
|
|
|
|
| def _generate_examples(self, filepath, split): |
| prefix = self.config.name.split('-')[0] |
| split_file = filepath[f"{prefix}-splits"] if prefix != 'all' else None |
|
|
| for chunk in filepath['raw']: |
| data = pd.read_parquet(chunk) |
| if split_file: |
| split_addrs = pd.read_csv(split_file).query('split == @split')['contracts'] |
| data = data[data['contracts'].isin(split_addrs)] |
|
|
| for idx, row in data.iterrows(): |
| if 'plain-text' in self.config.name: |
| yield idx, { |
| "address": '0x' + row['contracts'], |
| "source_code": row['source_code'], |
| "bytecode": row['bytecode'], |
| "slither": row['results'], |
| } |
| else: |
| slither = json.loads(row['results']) |
| contract_classes = self.__elaborate_results(slither, filepath['label_mappings']) |
| yield idx, { |
| "address": '0x' + row['contracts'], |
| "source_code": row['source_code'], |
| "bytecode": row['bytecode'], |
| "slither": contract_classes, |
| } |