Next Article in Journal
Long-Tailed Metrics and Object Detection in Camera Trap Datasets
Previous Article in Journal
Vertical Occlusal Dimension Changes of Complete Dentures between Autoclave and Traditional Processing Techniques
 
 
Font Type:
Arial Georgia Verdana
Font Size:
Aa Aa Aa
Line Spacing:
Column Width:
Background:
Article

Detection and Analysis of Ethereum Energy Smart Contracts

Electrical and Computer Engineering, University of Alberta, Edmonton, AB T6G 1H9, Canada
*
Author to whom correspondence should be addressed.
Appl. Sci. 2023, 13(10), 6027; https://doi.org/10.3390/app13106027
Submission received: 17 March 2023 / Revised: 28 April 2023 / Accepted: 10 May 2023 / Published: 14 May 2023

Abstract

:
As blockchain technology advances, so has the deployment of smart contracts on blockchain platforms, making it exceedingly challenging for users to explicitly identify application services. Unlike traditional contracts, smart contracts are not written in a natural language, making it difficult to determine their provenance. Automatic classification of smart contracts offers blockchain users keyword-based contract queries and a streamlined effective management of smart contracts. In addition, the advancement in smart contracts is accompanied by security challenges, which are generally caused by domain-specific security breaches in smart contract implementation. The development of secure and reliable smart contracts can be extremely challenging due to domain-specific vulnerabilities and constraints associated with various business logics. Accordingly, contract classification based on the application domain and the transaction context offers greater insight into the syntactic and semantic properties of that class. However, despite initial attempts at classifying Ethereum smart contracts, there has been no research on the identification of smart contracts deployed in transactive energy systems for energy exchange purposes. In this article, in response to the widely recognized prospects of blockchain-enabled smart contracts towards an economical and transparent energy sector, we propose a methodology for the detection and analysis of energy smart contracts. First, smart contracts are parsed by transforming code elements into vectors that encapsulate the semantic and syntactic characteristics of each term. This generates a corpus of annotated text as a balanced, representative collection of terms in energy contracts. The use of a domain corpus builder as an embedding layer to annotate energy smart contracts in conjunction with machine learning models results in a classification accuracy of 98.34%. Subsequently, a source code analysis scheme is applied to identified energy contracts to uncover patterns in code segment distribution, predominant adoption of certain functions, and recurring contracts across the Ethereum network.

1. Introduction

The emerging trend towards decentralization leads the pursuit for technologies that facilitate tamper-proof data exchange. Similarly, blockchain is based on a peer-to-peer architecture, which has empowered several core technologies including digital signatures, smart contracts, cryptographic hashing, and consensus mechanisms [1,2,3]. With the continued development of blockchain technology in a multitude of applications, the number of smart contracts deployed in distributed ledgers has increased tremendously. Smart contracts [4] are self-executing decentralized applications running on blockchain used for governance of financial assets that once deployed are autonomous and immutable. Smart contracts adhere to the underlying configuration of distributed ledgers and inherit automation, immutability, and decentralization qualities [5].
With the widespread adoption of blockchain platforms across various decentralized applications, smart contract interoperability is continuously evolving. Hence, Ethereum [6] has become a prominent smart-contract-based blockchain platform due to the increasing adoption of its decentralized applications. Ethereum’s surging popularity can be attributed to its high level of robustness and adaptability for a wide range of applications [7]. Ethereum currently hosts over four million smart contracts with approximately 670,000 smart contracts deployed each month and over 3000 DApps [8]. Therefore, a major concern for users is identifying the desired application service among dozens of smart contracts in a timely and efficient manner. The identification of contracts is one of Ethereum’s primary challenges. Some smart contract developers make their source code available, along with a description of its context and purpose. Unless the contract developer invests in publicizing it through designated fora, the vast majority of smart contracts remain anonymous and hardly traceable without a description. With the increasing number of smart contracts, assisting users to identify their required service among a massive number of contracts has become an ongoing challenge. As a result, it is imperative to outline a hierarchy that provides a comprehensive mapping of smart contracts that exceed the primary query services of blockchain platforms that are limited to contract address, block number, transaction hash, and timestamp. An important step towards performing such searches requires accurate labeling of the contracts, which was initially performed through an inefficient manual process. As a result, a comprehensive classification model capable of automatically classifying current or recently uploaded contracts is required.
Research has been carried out to help comprehend what smart contracts do and to enable contract searches based on their context and purpose. The proposed methods are centered on learning the characteristics and the structural code embedding of smart contracts. However, the existing classification and topic modeling schemes for smart contracts are limited to application domains, such as entertainment, management, the IoT, lottery, gambling, gaming, and so on [9,10,11]. This is why recent studies have underlined the significance of blockchain-enabled peer-to-peer energy trading systems [12] leveraging smart contracts [13].
The advent of blockchain technology offers the potential to securely automate P2P energy trading [14,15]. Smart contracts have proven to be effective for autonomous and secure execution of end-to-end energy transactions based on local consumer preferences. Ethereum’s smart contracts are regarded as strict protocols on the blockchain that allow energy transactions to be carried out once all prerequisites have been met. Energy exchanges are monitored as financial transactions, with the corresponding resource consumption quantified in gas units and remitted in Ether at the gas price [16,17]. However, despite the prevalent adoption of smart contracts in energy applications that streamline consumer and prosumer interactions towards a robust settlement process, no research has been conducted on the identification and analysis of energy smart contracts. Considering the multitude of deployed contracts, the lack of methodologies (such as classification models for the analysis of energy smart contracts) makes it challenging to gain insights into this ecological environment and to identify vulnerable smart contracts once deployed.
To the best of our knowledge, there has been no contribution explicitly addressing the detection and analysis of energy smart contracts. The key contributions of this article can be summarized as follows. This paper demonstrates the significance of domain-specific classification and analysis tools for smart contracts. This is accomplished using a method that leverages contextual terms for classification of smart contracts using code and comments. The proposed classification pipeline provides a method for the detection and labeling of energy smart contracts using their source code. It uses feature engineering methods to produce domain-specific corpora, which are subsequently embedded within machine learning classification models. A domain-specific analysis is a method of deriving technical and language-dependent features to enhance the structural and procedural understanding of smart contracts. This is achieved by employing contextual terms at a lexical level, searching for key terms and attribute tags to develop the energy corpus that will aid in deciphering the code’s context and retrieving energy contracts. By discerning the domains, we intend to reach higher levels of abstraction and handle the intricacies of DApps design specifications. The developed model captures the entire lexicon of transactions used for developing an energy smart contract. Finally, energy smart contracts are analyzed to identify patterns in the distribution of code segments, the predominance of specific functions, and recurring contracts across the Ethereum network.
The proposed approach can be used by contract developers to track similar contracts deployed on Ethereum. Contract level representation is used to include the highest level of granularity in the classification; thus, the proposed method is applicable to any application domain by using the key terms and attributes pertaining to that domain. It develops the corresponding corpus using contextual terms, and the collected attributes are then used to train the classifier. This methodology can be further extended to include measures for anomaly detection and malicious contract detection in the context of energy smart contract analysis. Security practitioners can use it to investigate the potential vulnerabilities of energy smart contracts. The search for vulnerabilities can be optimized by factoring intriguing elements such as the semantics of the defects while vulnerable contract’s syntactic representation is protected to the greatest extent possible. This an important step towards the design and development of auditing systems to address the identified vulnerabilities.
The article is organized as follows. Section 2 introduces energy smart contracts and provides background information on smart contracts and natural language processing. In Section 3, related works are discussed to further highlight the contributions of this research. Section 4 describes the research methodology and the proposed classification pipeline, followed by Section 5 which showcases the classification results obtained by the baseline models. Finally, Section 6 summarizes the conclusions and future research directions.

2. Background

To ensure effective identification of energy smart contracts, this study is based on natural language processing (NLP). It is important to explore the core concepts of text classification in line with fundamental principles pertaining to the definition of smart contracting for the energy domain, as well as what constitutes a generic energy smart contract. Section 2.1 provides an introduction to energy smart contracts, followed by an overview of textual data classification in Section 2.2.

2.1. Energy Smart Contract

In recent years, much attention has been given to blockchain platforms supporting smart contracts for the development of decentralized applications. Smart contracts have been designed and deployed in a broad array of applications, from fund management to the power grid. Given that smart contracts are deployed on a blockchain that was primarily intended to store financial transactions, the most instinctive applications of smart contracts involve trading assets between contracting parties [18]. Coupled with increasing progression toward a more decentralized and dynamic energy system, the viability of blockchain-enabled smart contracts in transactive energy systems has become prominent. Energy smart contracts are employed for automatic execution and monitoring of energy trading events [19,20]. An energy smart contract is implemented with instructions to retrieve the capacity and price offered by generators. The offer is then consigned to the prospective buyers for the bidding process to begin. A double auction is a commonly used mechanism to settle the price. Once the price has been cleared, the grid power flow is examined to ensure the feasibility of the allocations.
According to Desen et al. [19], the information is transmitted through six different layers in the energy smart contract workflow, as illustrated in Figure 1. The first layer involves the transmission of input data in the form of bids, offers, voltage levels, or availability signals from any agent involved in the peer-to-peer transaction for the demand side to be automatically triggered.
The second layer implements innovative optimization and energy management algorithms to address potential inconsistencies within contracted and delivered energy. Any computationally intensive control algorithms should be placed off smart contracts to minimize extra computational expenses imposed by implementing them in this layer. The smart contract is coded in the third layer using solidity. This allows the preceding layers to incorporate different programming languages. This layer handles all financial transactions, including gas expenditure and agent registration. Layer 4 controls the verification and encryption requirements for the smart contract’s placement into the block. Layer 5 is where implementation and execution occur, which require interaction with virtual machines, namely the Etheruem Virtual Machine (EVM). Subsequently, the data are transmitted across communication protocols using wired or wireless communication means.

2.2. Text Classification

Text classification is a fundamental aspect of NLP used for topic labeling, sentiment analysis, and spam detection [21,22]. It can be performed either through automatic labeling or manual explanation. However, dealing with an overwhelming volume of text data embodies the significance of automatic labeling approaches. Text classification can be performed automatically using rule-based or machine learning methods. The rule-based method follows a pre-defined rule set for classification, thus necessitating a coherent understanding of the domain. Machine learning approaches, on the other hand, perform classification by analyzing the text [23]. They have proven to be effective for unstructured data. The machine-learning-based text classification approach derives feature representations from text in conjunction with domain knowledge. The extracted features are then applied as inputs to train the classifier.
It should be noted that smart contracts are profoundly different from the standard text. Smart contracts include source code and comments, both of which convey semantic information. However, they may also carry non-descriptive or non-existent comments, resulting in semantic sparsity. This prompts the adoption of domain knowledge to reduce the semantic sparsity of smart contracts.

3. Related Work

Smart contracts, unlike conventional contracts, are not written in natural language, making it difficult to determine their context. When compared to conventional programming languages, such as C and Java, uniform understanding of Ethereum smart contracts is relatively limited. Nonetheless, there have been a few attempts at classifying Ethereum smart contracts prior to this research. However, smart contract classification described in the existing works is not necessarily consistent with Buterin’s initial classification of three tiers of financial, semi-financial, and non-financial applications [24]. For instance, Shi et al. [25] applied NLP on bytecode to classify contracts as governance, finance, gaming, wallet, and social, although wallets are a subset of financial applications, according to Buterin’s early classification. Using LSTM, Hu et al. [11] classified Ethereum smart contracts by identifying six behavior patterns analysing transactions including game, gambling, exchange, finance, high-risk, and social transactions. Later, Tian et al. [9] developed a smart contract classification strategy based on Bi-LSTM and Gaussian latent Dirichlet allocation (LDA) to classify contracts as entertainment, management, lottery and tools, finance, IoT, and others.
According to our experience, there is no consistent set of smart contract specifications in published research work, and smart contracts associated with comparable practices are often classified differently. The proposed study extracts domain models with the intended goal of deriving business logic from current Ethereum-based Dapps aiming at transactive energy systems. Complete formal characterizations of a program’s intended behavior are rarely available; thus, it is imperative to describe smart contracts from a domain-specific standpoint. Furthermore, addressing prevalent smart contract vulnerabilities mandates a semantic and synthetic understanding of the compromised contract.
Although the transparent execution of smart contracts has enhanced the readability of blockchain-enabled systems, the characteristics of distributed ledgers make it extremely challenging to revoke vulnerable smart contracts once they are deployed. As a result, massive financial losses caused by smart contract security breaches in former intrusions have compromised the ecological stability of the contract layer in widely adopted blockchain platforms such as Ethereum. Hence, a growing concern in blockchain security is the detection of smart contract vulnerabilities [26,27]. Scalability and new security vulnerabilities will emerge as the scale of the Ethereum projects advances over time. Novel vulnerability detection algorithms should detect and assess novel security threats and determine how to mitigate them. Accordingly, the search for vulnerabilities can be optimized by factoring intriguing elements such as the semantics of the defects. In fact, contract classification based on the application domain and transaction context offers greater insight into the syntactic and semantic properties of a given class of contracts. This can be further used for the design and implementation of customized vulnerability and fault detection mechanisms for a specific domain, including transactive energy systems [28]. Consequently, it is important to design and employ smart contract analysis tools to gain a broader knowledge of contracts concerning their underlying domains [29]. This will help to establish the groundwork for the development of domain-specific vulnerability detection algorithms for the detection and mitigation of unknown vulnerabilities and facilitates protecting vulnerable contracts’ syntactic representation to the greatest extent possible.

4. Methodology

To evaluate the grammatical, symbolic, and arithmetic characteristics of smart contracts, this study analyzes their source codes using machine learning algorithms. They are deployed as predictive models for detecting Ethereum energy smart contracts. As depicted in Figure 2, the proposed classification pipeline can be deconstructed into tree main stages of pre-training, training, and testing. Pre-training encompasses data collection and feature engineering. This adheres to the embedding layer and baseline models under training, after which the models are tested and evaluated. Logistic regression (LR), naive Bayes (NB), and Support Vector Machine (SVM) are employed as classifiers and a comparative performance analysis is carried out on the obtained results. As shown in Figure 2, pre-training embeddings and domain-specific embeddings form the corpus. Its components are further illustrated in the form of the ontology shown in Figure 3 and the embeddings listed in Table 1, which can be interpreted in terms of their relative frequency (see Section 5 for an example).

4.1. Data Collection and Pre-Processing

This study is performed on smart contracts solidity source codes retrieved from etherscan.io [8]. Since the code is explicitly used as the input for the classifier, it must be parsed to acquire an appropriate code representation form [37,38]. Code representation is performed using a customized solidity parser at the contract level, function level, comment level, and token level to identify semantic information within the focal points. As a result, contract level representation is employed to include the highest level of granularity in the classification. Moreover, pre-processing of raw unstructured text is required for content analysis. The pre-processing of the source code includes cleaning, normalizing, and stemming to remove stop words and semantic-irrelevant terms and to break down the words to their roots for detection of semantic similarities in feature extraction [37].

4.2. Building a Domain Corpus

In NLP, the frequency of terms is important to analyze the context. As a result, most feature extraction methods need a corpus of annotated terms paired with an ensemble of algorithms to learn the significance of a term in a document [38]. The process of corpus development and energy token extraction is discussed in this section.
Machine learning has been widely adopted for the semantic and syntactic analysis of text. As a result, most machine learning approaches require a corpus of annotated text for the underlying algorithm to learn the significance of each term in the document [39]. The corpus must be a balanced, representative collection of terms about a specific topic [40]. The vocabulary of size 376 and energy corpus was created using domain knowledge and by extracting domain features and terms from both the comment and code segments of the contracts. Text analysis and feature extraction methods include different levels of granularity including document level, sentence level, and word level analyses. In the course of developing the energy corpus and processing the smart contracts, we analyzed source codes at the lexical level. From a lexical standpoint, featuring a domain-specific corpus facilitates capturing the terms and statements that correspond to a specific application domain with prototypical measures at syntactic and semantic levels. Moreover, a domain-specific term distinguishes itself by the relative degree of closure of its lexicon, which signifies that, unlike a general corpus, a domain-specific vocabulary is nearly finite. Since certain syntactic structures and classes are more prevalent in a given application domain than in general corpus, we aim to create key energy terms, tokens, and attribute tags that will facilitate interpreting the context of the code and retrieving energy contracts. Moreover, semantics serve a limited role in smart contract development and using keywords allows a surface-level interpretation of source codes. Using term frequency and relevance, the corpus is intended to connect a knowledge base as a dictionary to the source code text. The corpus will be updated progressively as new energy contracts are identified during the process.

Extracting Energy Tokens

Tokens are the most prominent parts of Ethereum smart contracts. On the blockchain, cryptocurrencies serve a comparable function to cash, whereas cryptographic tokens serve as universal tools for managing digital assets. ERC-20 is one of the most widely used Ethereum tokens, adopted by a multitude of digital currencies [41]. The advent of the standardized ERC-20 contributed to the emergence of Ethereum-based utility tokens and a tokenized economy [39]. Accordingly, energy tokens are widely adopted across smart contracts representing tokenizable values for peer-to-peer energy transactions and keeping the values fungible across energy application networks [42,43]. These tokens are minted and distributed amongst stakeholders with the physical capacity to contribute services to the energy network. For instance, Power Ledger (a blockchain-enabled energy trading platform) employs POWR tokens to transact energy and trade environmental commodities [44]. As a result, incorporating energy tokens in energy corpus will facilitate the identification of energy contracts. Following the initial filtering using energy tokens, feature extraction is performed to identify additional properties of energy contracts through text analysis methods discussed in the following section.

4.3. Embedding Layer

The main challenge in active learning is selecting the most insightful data instances to label and use to begin training. The choice of embeddings requires special consideration since we are parsing source code. As mentioned earlier, source code deviates from a genuine text in that it features distinct granularity levels and lacks high-quality contextual information at the document level. NLP and word embeddings have recently seen considerable advances. However, source code processing requires semantics knowledge at the concept level rather than unique occurrences in text. At the same time, instance-specific embeddings (as introduced in BERT and similar approaches) are best suited for language translation and search engine queries [45]. Hence, feature selection techniques are useful to identify and eliminate unnecessary and irrelevant subsets of features [46]. Word2Vec [47] and Term Frequency–Inverse Document Frequency (TF-IDF) [48] were used in this work as feature extraction methods. They analyzed over 10,000 smart contracts to facilitate preliminary filtering. Word2vec is a pre-trained word embedding neural network, effective in text classification with small corpus, as in our case. The Word2Vec embedding layer keeps the semantic and syntactic information of codes and comments and it predicts the context of the terms. Using Continuous Bag of Words (CBOW) as the underlying architecture, the semantic correlation of the existing terms in the corpus was evaluated to find the closest match [49]. CBOW quantifies the frequency of the terms in the document by assigning each term a value representing the occurrence of that feature. The corpus is updated by terms with the highest semantic similarity scores, as illustrated in Table 1. Using a lexicon of energy-relevant terms, the occurrence of each term and their corresponding semantic correlation score are factored as measures of the energy-ness of the contract. Subsequently, the pre-processed contracts are passed through an embedding layer stacked in front of the classification model. TF-IDF is used as a sparse embedding layer to extract features from the labeled data. It works by penalizing frequently occurring terms in the source code to identify prominent yet infrequently used terms that prevail over the context. Using TF-IDF, each term in the source code is assigned a weight that determines the significance of the term in the source code based on its frequency and inverse document frequency used for training the classifier.

4.4. Baseline Models

Determining the best classifier is an imperative yet challenging decision in any text classification workflow. It needs to take into account many aspects, including the data composition, scalability of training, and run-time efficiency. In this study, NB, LR, and SVM are used as baseline models for classification of the Ethereum smart contracts [50].

4.4.1. Logistic Regression

LR is a discriminative, probabilistic classifier that is commonly employed in NLP as a supervised machine learning model. Based on its core assumption that dependent and independent variables do not have a linear relationship, LR examines the relationship between categorical variables using a logistic function. It requires a training corpus to detect discriminating features between the desired classes. The input corpus is used by LR to learn the domain’s verbal intuition and syntactic literature, as well as to retrieve document features and biased terms. Each input feature is assigned a weight that represents its importance in the classification decision. LR assigns higher weights to the primitive terms, although it is not capable of generating an instance of these terms on its own. The bias term, commonly referred to as the intercept, is also added to the weighted inputs. This implies that energy terms are negatively associated with the non-energy decision, as illustrated in Figure 4. Instead of determining similarity, LR takes into account the distance between the energy and the non-energy contracts. Following that, gradient descent is used to iteratively update the weights in order to minimize the cross-entropy loss which is a convex optimization problem. Hence, the algorithm’s resistance to correlated characteristics contributes to a higher classification precision.

4.4.2. Naive Bayes

Another probabilistic, supervised classifier employed in this study is NB. It determines the likelihood of a label based on previously observed characteristics and their conditional independence. Using the Bayes theorem, this model identifies the correlation between conditional probabilities and statistical quantities. As an incremental approach, NB is predicated on a theory that all attributes are independent and any context disregarding this conditional independence principle deteriorates its performance. The source code is transformed into a feature vector as an input for naive Bayes to be trained on the training set, estimating the likelihood of energy-ness given each feature. Generally, features can be developed by analyzing the training set while keeping linguistic intuitions and the domain-specific linguistic literature in mind. Developing complex features that are variations of a number of primitive features is especially useful, as illustrated in Figure 3. A thorough assessment of errors on the training set often yields perspectives on these features. NB generates the probability of each feature for each class, such that the probability of each feature can be optimized to project energy-ness or non-energy-ness of the smart contracts.

4.4.3. Support Vector Machine

The last supervised learning approach used in this research is SVM. It has proven to be an effective method for pattern recognition and text classification. As a discriminant classifier with a statistical learning paradigm, SVM attempts to capture the optimum trade-off between complexity and learning to ensure maximum generalization and minimum structural risk. The source code is perceived as a bag of words and each term is associated with a feature where the significance of the feature is determined by the frequency by which it appears inside the contract using TF-IDF. Once feature vectors are obtained, SVM transforms the training set into a multidimensional space to create a hyper-plane. The optimal position of the hyper-plane is directly affected by the data points closest to the decision boundary as they are the most challenging to identify. In addition, a subset of the training set is used as support vectors in the decision function, making it memory efficient. Using a higher dimension, SVM distinguishes the classes with the highest marginal distance, establishing a decision boundary to optimize the classification accuracy.

5. Evaluation Results

Table 2 and Table 3 demonstrate how each model performs on energy and non-energy contracts independently, as well as the overall accuracy of each algorithm using precision, recall, and the F1-score [51]. The obtained results show that LR produced the maximum accuracy of 98.34%, 97.53% precision, 98.78% recall, and 98.12% F1-score. Figure 4 depicts the LRs effective energy assessment of the frequently encountered terms in the source codes. The model is fed the TF-IDF features and their corresponding coefficients, which translate to a weighted combination of input features. This is to determine the importance of the feature in the overall logloss calculation. Hence, the probability of the contract being a non-energy contract increases as the logloss increases, while the probability of the contract being an energy contract increases as the logloss decreases. As a result, the term “Energy” has been appointed a −8.0191 correlation score with non-energy terms and is regarded as the most energy-related term. Pylon [52], which has been further identified as one of the dominant energy tokens adopted in several energy transactions, has been classified as an energy-related term with a −2.361 correlation score with respect to non-energy terms. On the other hand, terms such as game, gamble, sport, betting, and lottery that embody dominant application areas for smart contracts are classified as non-energy terms [11].
LR achieved substantial results since the classification task is fundamentally a binary problem. In addition, the probabilistic structure of LR allows the use of the likelihood ratio for reducing the costs associated with misclassification. The primary difference between LR and NB as the best performing algorithms is that LR is a discriminative classifier and NB is a generative classifier [53]. As a result, the success of LR over NB can be traced back to LRs robustness to correlated features and NBs intense conditional independence theorem. Although NB is acknowledged for its fast convergence, it demonstrated relatively high errors compared to LR. Nevertheless, because of its comparable outcomes and effortless training, NB remains a viable technique for use on small datasets [54].
SVM produced satisfactory results in the contract classification, with 87.23% accuracy. SVM is able to generalize because, as a probabilistic method, it does not penalize instances in which the correct decision is made with a reasonable degree of certainty. However, SVM underperformed in comparison to the other algorithms since it aims to maximize the perpendicular space between the two edges of the hyperplane to reduce the risk of generalization errors. As a result of the high correlation between smart contracts, the marginal distance of the data points decreased, resulting in a higher generalization error and a lower accuracy.
Subsequently, a fraction of the identified energy contracts were analyzed to capture any patterns in code segment distribution, prevalent adoption of specific functions, and recurring contracts across the Ethereum network [29]. Figure 5 depicts the most common energy tokens used to facilitate transactions in energy smart contracts. POWR is a utility token that grants access to the Power Ledger platform and its peer-to-peer features and serves as the ecosystem’s fuel [44]. Similarly, WPP Energy is a publicly available blockchain-based renewable energy investment platform that offers peer-to-peer smart-contract-enabled transactions using WPP tokens in an effort to promote the use of cryptocurrency in the energy market [34]. WePower is another blockchain-based green energy trading platform that enables energy suppliers to gain capital for green energy efficiency through smart contracts and WPR tokens. These tokens are indicative of the energy produced by producers in the days to come, giving buyers the opportunity to invest in renewable energy [55].
Finally, potential discrepancies between the code segments of both categories were examined, selecting 20 energy smart contracts and 20 non-energy smart contracts. Figure 6 illustrates the comparative results obtained from the distribution of contract code segments. When compared to non-energy smart contracts, the results obtained on the distribution of energy code segments imply that the development of energy contracts tends to prioritize the adoption of contracts and libraries over interfaces. The number of logical code lines (excluding comments and empty lines (LLOC)), the number of source code lines (SLOC), the number of functions (NF), the deepest nesting level (NL), the number of parameters (PAR), and the number of comment lines (CLOC) results also validate the prevalent use of identical contracts with minor adjustments and demonstrate how the lines of comments are heterogeneous and not necessarily proportionate to the length of the code. The results further confirm the prominent adoption of StandardToken and ERC20 in both classes upon analyzing dominant contract names and function names across both energy and non-energy smart contracts. As illustrated in Figure 7, the primary differences between the two classes in terms of function names can be attributed to the Ownable, Safemath, Pausable, and Mintable contracts. This indicates that Mintable contracts are not commonly used in energy contract development, since Mintable tokens feature a non-fixed total supply, allowing the token issuer to mint additional tokens. On the other hand, Ownable, Safemath, and Pausable are not identified as dominant non-energy contracts. Owanble contracts may be utilized for lowering gas costs and binding configuration functions to specific external addresses and are widely adopted in the energy sector. The SafeMath library examines whether an arithmetic operation will result in an integer overflow/underflow. Energy contracts use Safemath to send an exception and rollback the transaction. Pausable contracts are another common practice among energy contracts, allowing the owner of a Pausable contract to halt and restart functions. The owner can pause the functionality at any time; thus, users may be hesitant to utilize the corresponding dApp, making this a drawback in design for energy contracts. Although from a vulnerability analysis standpoint, the lack of a pause mechanism requires vulnerable contracts to be aborted while an alternate instance becomes available on the blockchain. Finally, the same analysis among functions in both classes entails the adoption of comparable functions across each category, with more prevalent adoption among energy contracts, as illustrated in Figure 7.

6. Conclusions

Blockchain technology has brought innovation to a wide array of industries. The number of transactions on the Ethereum blockchain is approaching half a billion, turning Ethereum into the largest smart contract blockchain platform. Unlike traditional contracts, smart contracts are not written in a natural language, making it difficult to determine their content. As a result, smart contract classification based on the application domain and transaction context provides greater insight into the syntactic and semantic properties of that domain. With the progression towards a more decentralized and dynamic energy system, the impact of blockchain-enabled smart contracts in transactive energy systems has gained prominence. As a result, it is imperative to analyze the energy smart contract feature space to gain more insights into the characteristics of contracts deployed for energy transactions.
Analyzing over 10,000 smart contract solidity source codes, this study proposes an approach to discriminate energy smart contracts using the publicly accessible Ethereum source codes. NLP and machine learning classification algorithms are employed to detect and properly label energy smart contracts. To begin, a domain-specific embedding layer is generated to identify and analyze energy tokens and energy-related terms. Subsequently, both the energy corpus and categorical attributes are employed as baselines for the training of the classification algorithms. Logistic regression, naive Bayes, and Support Vector Machine are implemented as classifiers. The classification performance of each algorithm is then evaluated using accuracy, precision, recall, and F1-score metrics. Energy smart contracts are detected with up to 98.34% accuracy, with LR outperforming the other algorithms. Detected contracts are further examined to discern any discrepancies or patterns in the distribution of code segments, the predominant use of specific functions, and recurring contracts across the Ethereum network.
We anticipate that the proposed approach will help to establish the groundwork for innovative solutions for domain-specific classification and vulnerability detection of smart contracts. Looking exclusively into the grammatical, symbolic, and arithmetic characteristics of energy smart contracts may facilitate the identification of vulnerability features that may have gone undetected in previous studies. Subsequently, machine learning models can be employed for vulnerability assessments of the energy contracts at the function level. To improve the accuracy of the existing vulnerability detection models, the implications of integrating the conventional pattern extraction methods with machine learning models can also be investigated.

Author Contributions

Conceptualization, B.L. and P.M.; methodology, B.L.; software, B.L.; validation, B.L.; formal analysis, B.L.; investigation, B.L.; resources, P.M.; writing—original draft preparation, B.L.; writing—review and editing, P.M.; supervision, P.M.; project administration, P.M.; funding acquisition, P.M. All authors have read and agreed to the published version of the manuscript.

Funding

This research was supported by the Government of Alberta under the Major Innovation Fund, project RCP-19-001-MIF and by the Natural Sciences and Engineering Research Council (NSERC) of Canada, project RGPIN-2017-05866.

Conflicts of Interest

The authors declare no conflict of interest.

References

  1. Monrat, A.A.; Schelén, O.; Andersson, K. A survey of blockchain from the perspectives of applications, challenges, and opportunities. IEEE Access 2019, 7, 117134–117151. [Google Scholar] [CrossRef]
  2. Benisi, N.Z.; Aminian, M.; Javadi, B. Blockchain-based decentralized storage networks: A survey. J. Netw. Comput. Appl. 2020, 162, 102656. [Google Scholar] [CrossRef]
  3. Jiang, P.; Guo, F.; Liang, K.; Lai, J.; Wen, Q. Searchain: Blockchain-based private keyword search in decentralized storage. Future Gener. Comput. Syst. 2020, 107, 781–792. [Google Scholar] [CrossRef]
  4. Szabo, N. Smart contracts: Building blocks for digital markets. EXTROPY J. Transhum. Thought 1996, 18, 50–53. [Google Scholar]
  5. Tolmach, P.; Li, Y.; Lin, S.W.; Liu, Y.; Li, Z. A survey of smart contract formal specification and verification. ACM Comput. Surv. 2021, 54, 1–38. [Google Scholar] [CrossRef]
  6. Etheruem. Available online: https://ethereum.org/en/dapps/ (accessed on 17 April 2023).
  7. Oliva, G.A.; Hassan, A.E.; Jiang, Z.M.J. An exploratory study of smart contracts in the Ethereum blockchain platform. Empir. Softw. Eng. 2020, 25, 1864–1904. [Google Scholar] [CrossRef]
  8. Ethescan. Available online: https://etherscan.io/ (accessed on 17 April 2023).
  9. Tian, G.; Wang, Q.; Zhao, Y.; Guo, L.; Sun, Z.; Lv, L. Smart contract classification with a bi-LSTM based approach. IEEE Access 2020, 8, 43806–43816. [Google Scholar] [CrossRef]
  10. Sun, X.; Lin, X.; Liao, Z. An ABI-based Classification Approach for Ethereum Smart Contracts. In Proceedings of the 2021 IEEE Intl Conf on Dependable, Autonomic and Secure Computing, Intl Conf on Pervasive Intelligence and Computing, Intl Conf on Cloud and Big Data Computing, Intl Conf on Cyber Science and Technology Congress (DASC/PiCom/CBDCom/CyberSciTech), Virtual, 25–28 October 2021; IEEE: Piscataway, NJ, USA, 2021; pp. 99–104. [Google Scholar]
  11. Hu, T.; Liu, X.; Chen, T.; Zhang, X.; Huang, X.; Niu, W.; Lu, J.; Zhou, K.; Liu, Y. Transaction-based classification and detection approach for Ethereum smart contract. Inf. Process. Manag. 2021, 58, 102462. [Google Scholar] [CrossRef]
  12. Zhang, S.; May, D.; Gül, M.; Musilek, P. Reinforcement learning-driven local transactive energy market for distributed energy resources. Energy AI 2022, 8, 100150. [Google Scholar] [CrossRef]
  13. Vieira, G.; Zhang, J. Peer-to-peer energy trading in a microgrid leveraged by smart contracts. Renew. Sustain. Energy Rev. 2021, 143, 110900. [Google Scholar] [CrossRef]
  14. Seven, S.; Yao, G.; Soran, A.; Onen, A.; Muyeen, S. Peer-to-peer energy trading in virtual power plant based on blockchain smart contracts. IEEE Access 2020, 8, 175713–175726. [Google Scholar] [CrossRef]
  15. Eisele, S.; Barreto, C.; Dubey, A.; Koutsoukos, X.; Eghtesad, T.; Laszka, A.; Mavridou, A. Blockchains for transactive energy systems: Opportunities, challenges, and approaches. Computer 2020, 53, 66–76. [Google Scholar] [CrossRef]
  16. Han, D.; Zhang, C.; Ping, J.; Yan, Z. Smart contract architecture for decentralized energy trading and management based on blockchains. Energy 2020, 199, 117417. [Google Scholar] [CrossRef]
  17. Marchesi, L.; Marchesi, M.; Destefanis, G.; Barabino, G.; Tigano, D. Design patterns for gas optimization in ethereum. In Proceedings of the 2020 IEEE International Workshop on Blockchain Oriented Software Engineering (IWBOSE), London, ON, Canada, 18 February 2020; IEEE: Piscataway, NJ, USA, 2020; pp. 9–15. [Google Scholar]
  18. Zheng, Z.; Xie, S.; Dai, H.N.; Chen, W.; Chen, X.; Weng, J.; Imran, M. An overview on smart contracts: Challenges, advances and platforms. Future Gener. Comput. Syst. 2020, 105, 475–491. [Google Scholar] [CrossRef]
  19. Kirli, D.; Couraud, B.; Robu, V.; Salgado-Bravo, M.; Norbu, S.; Andoni, M.; Antonopoulos, I.; Negrete-Pincetic, M.; Flynn, D.; Kiprakis, A. Smart contracts in energy systems: A systematic review of fundamental approaches and implementations. Renew. Sustain. Energy Rev. 2022, 158, 112013. [Google Scholar] [CrossRef]
  20. Huang, Q.; Amin, W.; Umer, K.; Gooi, H.B.; Eddy, F.Y.S.; Afzal, M.; Shahzadi, M.; Khan, A.A.; Ahmad, S.A. A review of transactive energy systems: Concept and implementation. Energy Rep. 2021, 7, 7804–7824. [Google Scholar] [CrossRef]
  21. Hartmann, J.; Huppertz, J.; Schamp, C.; Heitmann, M. Comparing automated text classification methods. Int. J. Res. Mark. 2019, 36, 20–38. [Google Scholar] [CrossRef]
  22. Minaee, S.; Kalchbrenner, N.; Cambria, E.; Nikzad, N.; Chenaghlu, M.; Gao, J. Deep learning–based text classification: A comprehensive review. ACM Comput. Surv. 2021, 54, 1–40. [Google Scholar] [CrossRef]
  23. Zablith, F.; Osman, I.H. ReviewModus: Text classification and sentiment prediction of unstructured reviews using a hybrid combination of machine learning and evaluation models. Appl. Math. Model. 2019, 71, 569–583. [Google Scholar] [CrossRef]
  24. Buterin, V. A next-generation smart contract and decentralized application platform. White Pap. 2014, 3, 1–36. [Google Scholar]
  25. Shi, C.; Xiang, Y.; Doss, R.R.M.; Yu, J.; Sood, K.; Gao, L. A Bytecode-based Approach for Smart Contract Classification. arXiv 2021, arXiv:2106.15497. [Google Scholar]
  26. Wang, W.; Song, J.; Xu, G.; Li, Y.; Wang, H.; Su, C. Contractward: Automated vulnerability detection models for ethereum smart contracts. IEEE Trans. Netw. Sci. Eng. 2020, 8, 1133–1144. [Google Scholar] [CrossRef]
  27. Wang, L.; Cheng, H.; Zheng, Z.; Yang, A.; Zhu, X. Ponzi scheme detection via oversampling-based Long Short-Term Memory for smart contracts. Knowl.-Based Syst. 2021, 228, 107312. [Google Scholar] [CrossRef]
  28. Gupta, R.; Patel, M.M.; Shukla, A.; Tanwar, S. Deep learning-based malicious smart contract detection scheme for internet of things environment. Comput. Electr. Eng. 2022, 97, 107583. [Google Scholar] [CrossRef]
  29. Pinna, A.; Ibba, S.; Baralla, G.; Tonelli, R.; Marchesi, M. A massive analysis of ethereum smart contracts empirical study and code metrics. IEEE Access 2019, 7, 78194–78213. [Google Scholar] [CrossRef]
  30. Sunchain. Available online: https://www.sunchain.fr/ (accessed on 17 April 2023).
  31. GNRG. Available online: https://gnrg.co/ (accessed on 17 April 2023).
  32. CES Energy Solutions. Available online: https://www.cesenergysolutions.com/ (accessed on 17 April 2023).
  33. Electrify Asia. Available online: https://electrify.asia/ (accessed on 17 April 2023).
  34. WPP Energy. Available online: https://wppenergy.com/ (accessed on 17 April 2023).
  35. Creative Energy. Available online: https://creative.energy/ (accessed on 17 April 2023).
  36. CoinBase. Available online: https://www.coinbase.com/price/solar-energy (accessed on 17 April 2023).
  37. Uysal, A.K.; Gunal, S. The impact of preprocessing on text classification. Inf. Process. Manag. 2014, 50, 104–112. [Google Scholar] [CrossRef]
  38. Mirończuk, M.M.; Protasiewicz, J. A recent overview of the state-of-the-art elements of text classification. Expert Syst. Appl. 2018, 106, 36–54. [Google Scholar] [CrossRef]
  39. García-Méndez, S.; Fernandez-Gavilanes, M.; Juncal-Martínez, J.; González-Castaño, F.J.; Seara, Ó.B. Identifying banking transaction descriptions via support vector machine short-text classification based on a specialized labelled corpus. IEEE Access 2020, 8, 61642–61655. [Google Scholar] [CrossRef]
  40. Altınel, B.; Ganiz, M.C.; Diri, B. A corpus-based semantic kernel for text classification by using meaning values of terms. Eng. Appl. Artif. Intell. 2015, 43, 54–66. [Google Scholar] [CrossRef]
  41. Victor, F.; Lüders, B.K. Measuring ethereum-based erc20 token networks. In Proceedings of the International Conference on Financial Cryptography and Data Security; Springer: Cham, Switzerland, 2019; pp. 113–129. [Google Scholar]
  42. Gai, K.; Wu, Y.; Zhu, L.; Qiu, M.; Shen, M. Privacy-preserving energy trading using consortium blockchain in smart grid. IEEE Trans. Ind. Inform. 2019, 15, 3548–3558. [Google Scholar] [CrossRef]
  43. Toderean, L.; Antal, C.; Antal, M.; Mitrea, D.; Cioara, T.; Anghel, I.; Salomie, I. A Lockable ERC20 Token for Peer to Peer Energy Trading. arXiv 2021, arXiv:2111.04467. [Google Scholar]
  44. PowerLedger. Available online: https://www.powerledger.io/ (accessed on 12 May 2023).
  45. Tenney, I.; Das, D.; Pavlick, E. BERT rediscovers the classical NLP pipeline. arXiv 2019, arXiv:1905.05950. [Google Scholar]
  46. Labani, M.; Moradi, P.; Ahmadizar, F.; Jalili, M. A novel multivariate filter method for feature selection in text classification problems. Eng. Appl. Artif. Intell. 2018, 70, 25–37. [Google Scholar] [CrossRef]
  47. Jatnika, D.; Bijaksana, M.A.; Suryani, A.A. Word2vec model analysis for semantic similarities in english words. Procedia Comput. Sci. 2019, 157, 160–167. [Google Scholar] [CrossRef]
  48. Qaiser, S.; Ali, R. Text mining: Use of TF-IDF to examine the relevance of words to documents. Int. J. Comput. Appl. 2018, 181, 25–29. [Google Scholar] [CrossRef]
  49. Zhang, H.; Zhong, G. Improving short text classification by learning vector representations of both words and hidden topics. Knowl.-Based Syst. 2016, 102, 76–86. [Google Scholar] [CrossRef]
  50. Hossain, M.R.; Sarkar, S.; Rahman, M. Different machine learning based approaches of baseline and deep learning models for bengali news categorization. Int. J. Comput. Appl. 2020, 975, 8887. [Google Scholar]
  51. Goutte, C.; Gaussier, E. A probabilistic interpretation of precision, recall and F-score, with implication for evaluation. In Proceedings of the Advances in Information Retrieval: 27th European Conference on IR Research, ECIR 2005, Santiago de Compostela, Spain, 21–23 March 2005; Springer: Berlin/Heidelberg, Germany, 2005; pp. 345–359. [Google Scholar]
  52. Adeyemi, A.; Yan, M.; Shahidehpour, M.; Botero, C.; Guerra, A.V.; Gurung, N.; Zhang, L.C.; Paaso, A. Blockchain technology applications in power distribution systems. Electr. J. 2020, 33, 106817. [Google Scholar] [CrossRef]
  53. Setyawan, M.Y.H.; Awangga, R.M.; Efendi, S.R. Comparison of multinomial naive Bayes algorithm and logistic regression for intent classification in chatbot. In Proceedings of the 2018 International Conference on Applied Engineering (ICAE), Batam, Indonesia, 3–4 October 2018; IEEE: Piscataway, NJ, USA, 2018; pp. 1–5. [Google Scholar]
  54. Shah, K.; Patel, H.; Sanghvi, D.; Shah, M. A comparative analysis of logistic regression, random forest and KNN models for the text classification. Augment. Hum. Res. 2020, 5, 1–16. [Google Scholar] [CrossRef]
  55. WePower network. Available online: https://www.blockdata.tech/profiles/wepower (accessed on 12 May 2023).
Figure 1. The smart contracting paradigm for energy applications.
Figure 1. The smart contracting paradigm for energy applications.
Applsci 13 06027 g001
Figure 2. Architecture of the classification pipeline.
Figure 2. Architecture of the classification pipeline.
Applsci 13 06027 g002
Figure 3. Ontology of domain-specific terms.
Figure 3. Ontology of domain-specific terms.
Applsci 13 06027 g003
Figure 4. Energy context assessment by logistic regression.
Figure 4. Energy context assessment by logistic regression.
Applsci 13 06027 g004
Figure 5. Dominant energy tokens.
Figure 5. Dominant energy tokens.
Applsci 13 06027 g005
Figure 6. Code segment analysis.
Figure 6. Code segment analysis.
Applsci 13 06027 g006
Figure 7. Code segment analysis.
Figure 7. Code segment analysis.
Applsci 13 06027 g007
Table 1. Word2Vec Embeddings.
Table 1. Word2Vec Embeddings.
TermMatchesSimilarityTermMatchesSimilarityTermMatchesSimilarity
SolarTidal0.8525BiomassCME0.9951Sunchain [30]GNRG [31]0.9526
Hydro0.8455 Geothermal0.9932 CES [32]0.9034
Wind0.8441 Biodiesel0.9901 ELEC [33]0.8951
Geothermal0.8300 Hydro0.9817 Electrify Asia [33]0.8840
Biomass0.8185 Tidal0.9701 WPP [34]0.8815
CME0.8181 Wind0.9295  Creative Energy [35]0.8789
Biodesiel0.7907 Energy0.8791 SEG [36]0.8620
Table 2. Class-specific metrics from the confusion matrix report.
Table 2. Class-specific metrics from the confusion matrix report.
ModelPrecisionRecallF1-Score ModelPrecisionRecallF1-Score
EnergyLR0.951.00.97Non-energyLR1.00.980.99
Naive Bayes0.860.920.89 Naive Bayes0.910.860.88
SVM0.970.770.86 SVM0.810.980.88
Table 3. Performance of the baseline algorithms on a full-feature model.
Table 3. Performance of the baseline algorithms on a full-feature model.
ModelAccuracyPrecisionRecallF1-Score
LR0.98340.97530.98780.9812
Naive Bayes0.89100.89230.89100.8909
SVM0.87230.89000.87230.8709
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content.

Share and Cite

MDPI and ACS Style

Lashkari, B.; Musilek, P. Detection and Analysis of Ethereum Energy Smart Contracts. Appl. Sci. 2023, 13, 6027. https://doi.org/10.3390/app13106027

AMA Style

Lashkari B, Musilek P. Detection and Analysis of Ethereum Energy Smart Contracts. Applied Sciences. 2023; 13(10):6027. https://doi.org/10.3390/app13106027

Chicago/Turabian Style

Lashkari, Bahareh, and Petr Musilek. 2023. "Detection and Analysis of Ethereum Energy Smart Contracts" Applied Sciences 13, no. 10: 6027. https://doi.org/10.3390/app13106027

Note that from the first issue of 2016, this journal uses article numbers instead of page numbers. See further details here.

Article Metrics

Back to TopTop