File size: 20,259 Bytes
63b9d0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Smart Contracts Audit dataset."""


import os
import re
import pandas as pd
import datasets
from pyparsing import col


_CITATION = """\
@misc{storhaug2022smartcontractsaudit,
    title = {Smart Contracts Audit Dataset},
    author={André Storhaug},
    year={2022}
}
"""

_DESCRIPTION = """\
Smart Contracts Audit Dataset.
This is a dataset of audited verified (Etherscan.io) Smart Contracts \
that are deployed to the Ethereum blockchain.
"""

# TODO: Add a link to an official homepage for the dataset here
_HOMEPAGE = "https://andstor.github.io/verified-smart-contracts-audit"

# TODO: Add the license for the dataset here if you can find it
_LICENSE = ""

# Add link to the official dataset URLs here
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
_URLS = {
    "flattened": {
        "dev": [f"data/flattened/validation/part.{part}.parquet" for part in range(2)],
        "test": [f"data/flattened/test/part.{part}.parquet" for part in range(2)],
        "train": [f"data/flattened/train/part.{part}.parquet" for part in range(11)]
    },
    "inflated": {
        "dev": [f"data/inflated/validation/part.{part}.parquet" for part in range(1)],
        "test": [f"data/inflated/test/part.{part}.parquet" for part in range(1)],
        "train": [f"data/inflated/train/part.{part}.parquet" for part in range(5)]
    },
    "metadata": "data/metadata.parquet"
}

# Supported tools and columns config
_TOOLS = {
    'flattened': {
        'all': ["solidetector", "slither", "oyente", "smartcheck"],
        'solidetector': ["solidetector"],
        'slither': ['slither'],
        'oyente': ['oyente'],
        'smartcheck': ['smartcheck'],
    },
    'inflated': {
        'all': ["solidetector"],
        'solidetector': ["solidetector"],
    }
}

_TOOLS_AUDIT_DESC = {
    'solidetector': {
        'level_col': 'severity',
        'levels': {'High': 3, 'Medium': 2, 'Low': 1 },
    },
    'slither': {
        'level_col': 'impact',
        'levels': {'High': 3, 'Medium': 2, 'Low': 1, 'Informational': -1, 'Optimization': -2},
    },
    'oyente': {
        'level_col': 'level',
        'levels': {'Warning': 3},
    },
    'smartcheck': {
        'level_col': 'severity',
        'levels': {3: 3, 2: 2, 1: 1},
    }
}


_LEVELS = {
    'High': 3,
    'Warning': 3,
    3: 3,
    'Medium': 2,
    2: 2,
    'Low': 1,
    1: 1,
    'Informational': -1,
    'Optimization': -2, 
}

_EMBEDDED_LEVEL = "High"

def _check_strings(search_list, input_string):
    return [s in input_string for s in search_list]

# Name of the dataset usually match the script name with CamelCase instead of snake_case
class SmartContractsAudit(datasets.GeneratorBasedBuilder):
    """Smart Contracts Audit Dataset."""

    VERSION = datasets.Version("1.0.0")

    # You will be able to load one or the other configurations in the following list with
    # data = datasets.load_dataset('my_dataset', 'all')
    # data = datasets.load_dataset('my_dataset', 'plain_text')
    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="flattened_all", version=VERSION, description="Flattened data labeled with all tools"),
        datasets.BuilderConfig(name="flattened_all_extended", version=VERSION, description="Flattened data with metadata, labeled with all tools"),
        datasets.BuilderConfig(name="flattened_all_embedded", version=VERSION, description="Flattened data with embedded labeled with all tools"),
        #*(lambda VERSION=VERSION: [ datasets.BuilderConfig(name="flattened_all_embedded_" + lvl, version=VERSION) for lvl in ["high", "medium", "low", "informational", "optimization"]])(),
        
        datasets.BuilderConfig(name="flattened_slither", version=VERSION, description="Flattened data with metadata, labeled with SoliDetector"),
        datasets.BuilderConfig(name="flattened_slither_extended", version=VERSION, description="Flattened data labeled with SoliDetector"),
        datasets.BuilderConfig(name="flattened_slither_embedded", version=VERSION, description="Flattened data with embedded labeled with SoliDetector"),
        #*(lambda VERSION=VERSION: [ datasets.BuilderConfig(name="flattened_slither_embedded_" + lvl, version=VERSION) for lvl in ["high", "medium", "low", "informational", "optimization"]])(),
        
        datasets.BuilderConfig(name="flattened_solidetector", version=VERSION, description="Flattened data with metadata, labeled with SoliDetector"),
        datasets.BuilderConfig(name="flattened_solidetector_extended", version=VERSION, description="Flattened data labeled with SoliDetector"),
        datasets.BuilderConfig(name="flattened_solidetector_embedded", version=VERSION, description="Flattened data with embedded labeled with SoliDetector"),
        #*(lambda VERSION=VERSION: [ datasets.BuilderConfig(name="flattened_solidetector_embedded_" + lvl, version=VERSION) for lvl in ["high", "medium", "low"]])(),
        
        datasets.BuilderConfig(name="flattened_oyente", version=VERSION, description="Flattened data with metadata, labeled with Oyente"),
        datasets.BuilderConfig(name="flattened_oyente_extended", version=VERSION, description="Flattened data labeled with Oyente"),
        datasets.BuilderConfig(name="flattened_oyente_embedded", version=VERSION, description="Flattened data with embedded labeled with Oyente"),
        #*(lambda VERSION=VERSION: [ datasets.BuilderConfig(name="flattened_oyente_embedded_" + lvl, version=VERSION) for lvl in ["high", "medium", "low"]])(),
        
        datasets.BuilderConfig(name="flattened_smartcheck", version=VERSION, description="Flattened data with metadata, labeled with SmartCheck"),
        datasets.BuilderConfig(name="flattened_smartcheck_extended", version=VERSION, description="Flattened data labeled with SmartCheck"),
        datasets.BuilderConfig(name="flattened_smartcheck_embedded", version=VERSION, description="Flattened data with embedded labeled with SmartCheck"),
        #*(lambda VERSION=VERSION: [ datasets.BuilderConfig(name="flattened_smartcheck_embedded_" + lvl, version=VERSION) for lvl in ["high", "medium", "low"]])(),
        
        datasets.BuilderConfig(name="inflated_all", version=VERSION, description="Inflated data labeled with all tools"),
        datasets.BuilderConfig(name="inflated_all_embedded", version=VERSION, description="Inflated data with embedded labeled with all tools"),
        #*(lambda VERSION=VERSION: [ datasets.BuilderConfig(name="inflated_all_embedded_" + lvl, version=VERSION) for lvl in ["high", "medium", "low"]])(),
        
        datasets.BuilderConfig(name="inflated_solidetector", version=VERSION, description="Inflated data labeled with SoliDetector"),
        datasets.BuilderConfig(name="inflated_solidetector_embedded", version=VERSION, description="Inflated data with embedded labeled with SoliDetector"),
        #*(lambda VERSION=VERSION: [ datasets.BuilderConfig(name="inflated_solidetector_embedded_" + lvl, version=VERSION) for lvl in ["high", "medium", "low"]])(),
        
        #datasets.BuilderConfig(name="solidetector", version=VERSION, description="Labeling with SoliDetector"),
        #datasets.BuilderConfig(name="solidetector_plain_text", version=VERSION, description="Labeling with SoliDetector plain text version"),
    ]

    DEFAULT_CONFIG_NAME = "inflated_all"  # It's not mandatory to have a default configuration. Just use one if it make sense.

    def _info(self):
        # This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
        data_split = self.config.name.split("_")[0]
        tool = self.config.name.split("_")[1]

        if "embedded" in self.config.name:  # This is an example to show how to have different features for "first_domain" and "second_domain"
            features = datasets.Features(
                {
                    "text": datasets.Value("string"),
                    "language": datasets.Value("string")
                }
            )
        elif "flattened" in self.config.name:  # This is the name of the configuration selected in BUILDER_CONFIGS above
            features = datasets.Features(
                {
                    'contract_name': datasets.Value("string"),
                    'contract_address': datasets.Value("string"),
                    'language': datasets.Value("string"),
                    'source_code': datasets.Value("string"),
                    **{ t: datasets.Value("string") for t in _TOOLS[data_split][tool] },
                    'abi': datasets.Value("string"), # JSON string
                    'compiler_version': datasets.Value("string"),
                    'optimization_used': datasets.Value("bool"),
                    'runs': datasets.Value("int64"),
                    'constructor_arguments': datasets.Value("string"),
                    'evm_version': datasets.Value("string"),
                    'library': datasets.Value("string"),
                    'license_type': datasets.Value("string"),
                    'proxy': datasets.Value("bool"),
                    'implementation': datasets.Value("string"),
                    'swarm_source': datasets.Value("string")
                }
            )
        elif "inflated" in self.config.name:  # This is an example to show how to have different features for "first_domain" and "second_domain"
            features = datasets.Features(
                {
                    'contract_name': datasets.Value("string"),
                    'file_path': datasets.Value("string"),
                    'contract_address': datasets.Value("string"),
                    'language': datasets.Value("string"),
                    'source_code': datasets.Value("string"),
                    **{ t: datasets.Value("string") for t in _TOOLS[data_split][tool] },
                    'compiler_version': datasets.Value("string"),
                    'license_type': datasets.Value("string"),
                    'swarm_source': datasets.Value("string")
                }
            )

        if "extended" in self.config.name:
            features["tx_count"] = datasets.Value("int64")
            features["balance"] = datasets.Value("string")

        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            features=features,  # Here we define them above because they are different between the two configurations
            # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
            # specify them. They'll be used if as_supervised=True in builder.as_dataset.
            # supervised_keys=("sentence", "label"),
            # Homepage of the dataset for documentation
            homepage=_HOMEPAGE,
            # License for the dataset if available
            license=_LICENSE,
            # Citation for the dataset
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        # This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
        # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name

        # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
        # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
        # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
        urls = _URLS[self.config.name.split("_")[0]]
        downloaded_files = dl_manager.download_and_extract(urls)
        
        metadata = None
        if "extended" in self.config.name:
            metadata = dl_manager.download_and_extract(_URLS["metadata"])

        if "flattened" in self.config.name or "inflated" in self.config.name:
            return [
                datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"files": downloaded_files["train"], "metadata": metadata}),
                datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"files": downloaded_files["dev"], "metadata": metadata}),
                datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"files": downloaded_files["test"], "metadata": metadata}),
            ]
        else:
            return [
                datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"files": downloaded_files["train"], "metadata": metadata}),
            ]

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, files, metadata):
        """Yields examples."""
        # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
        # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
        #data = pd.read_parquet(filepath)
        data_split = self.config.name.split("_")[0]
        tool = self.config.name.split("_")[1]
        
        lvl = _EMBEDDED_LEVEL
        #lvl = self.config.name.split("_")[-1].capitalize()
        #if lvl not in _LEVELS:
        #    lvl = min(_LEVELS, key=_LEVELS.get)

        # Load metadata
        if metadata is not None:
            meta = pd.read_parquet(metadata)

        for path in files:
            if "embedded" in self.config.name:
                columns = ['contract_address', 'source_code', 'language']
                columns.extend(["file_path"] if "inflated" in self.config.name else [])
                columns.extend(_TOOLS[data_split][tool])
                data = pd.read_parquet(path, columns=columns)
            elif "flattened" in self.config.name:
                data = pd.read_parquet(path)
                data['runs'].fillna(0, inplace=True)
            else:
                data = pd.read_parquet(path)
            
            # Add metadata
            if metadata is not None:
                data = pd.merge(data, meta, how="left", on="contract_address")

            for index, row in data.iterrows():

                if "flattened" in self.config.name:
                    # Yields examples as (key, example) tuples
                    key = row['contract_address']
                    if "embedded" in self.config.name:
                        is_vulnerable = False
                        is_secure = False
                        for t in _TOOLS[data_split][tool]:
                            if pd.isnull(row[t]):
                                continue
                            if row[t] == "[]":
                                is_secure = True
                                continue
                            
                            vuln_levels = [_TOOLS_AUDIT_DESC[t]["level_col"] + '": "' + k for k,v in _LEVELS.items() if v >= _LEVELS[lvl]]
                            if any(_check_strings(vuln_levels, row[t])):
                                is_vulnerable = True
                                break
                            else:
                                is_secure = True
                                continue
                        
                        label = ""
                        if is_vulnerable:
                            label = "// VULNERABLE\n"
                        elif is_secure:
                            label = "// SECURE\n"
                        else:
                            label = "// UNKNOWN\n"

                        yield key, {
                            'text': label + row['source_code'],
                            'language': row['language'],
                        }
                    else:
                        yield key, {
                            'contract_name': row['contract_name'],
                            'contract_address': row['contract_address'],
                            'language': row['language'],
                            'source_code': row['source_code'],
                            **{ t: row[t] for t in _TOOLS[data_split][tool] },
                            'abi': row['abi'],
                            'compiler_version': row['compiler_version'],
                            'optimization_used': row['optimization_used'],
                            'runs': row['runs'],
                            'constructor_arguments': row['constructor_arguments'],
                            'evm_version': row['evm_version'],
                            'library': row['library'],
                            'license_type': row['license_type'],
                            'proxy': row['proxy'],
                            'implementation': row['implementation'],
                            'swarm_source': row['swarm_source'],
                            **({'tx_count': row["tx_count"]} if metadata is not None else {}),
                            **({'balance': row["balance"]} if metadata is not None else {})
                        }

                elif "inflated" in self.config.name:
                    # Yields examples as (key, example) tuples
                    key = row['contract_address'] + ":" + row['file_path'] + ":" + str(hash(row['source_code']))
                    if "embedded" in self.config.name:
                        is_vulnerable = False
                        is_secure = False
                        for t in _TOOLS[data_split][tool]:
                            if pd.isnull(row[t]):
                                continue
                            if row[t] == "[]":
                                is_secure = True
                                continue
                            
                            vuln_levels = [_TOOLS_AUDIT_DESC[t]["level_col"] + '": "' + k for k,v in _LEVELS.items() if v >= _LEVELS[lvl]]
                            if any(_check_strings(vuln_levels, row[t])):
                                is_vulnerable = True
                                break
                            else:
                                is_secure = True
                                continue

                        label = ""
                        if is_vulnerable:
                            label = "// VULNERABLE\n"
                        elif is_secure:
                            label = "// SECURE\n"
                        else:
                            label = "// UNKNOWN\n"

                        yield key, {
                            'text': label + row['source_code'],
                            'language': row['language'],
                        }
                    else:
                        yield key, {
                            'contract_name': row['contract_name'],
                            'file_path': row['file_path'],
                            'contract_address': row['contract_address'],
                            'language': row['language'],
                            'source_code': row['source_code'],
                            **{ t: row[t] for t in _TOOLS[data_split][tool] },
                            'compiler_version': row['compiler_version'],
                            'license_type': row['license_type'],
                            'swarm_source': row['swarm_source']
                        }