# Pattern-Based Test Suite Reduction Method for Smart Contracts
## 4 Test Case Design and Implementation
### Test Package Implementation  
Preliminary results of smart contract performance tests showed the need to construct a dedicated package for their testing. The results published in the article [7] showed a high-performance potential and determined the development of a dedicated software package enabling a higher level of precision of the obtained measurements. Performance testing required the creation of eight smart contracts, each with one to eight verification rules. The performance tests used smart contracts to verify energy transfer transactions between nodes of the prosumer community network. A list of eight unique verification rules was used:  
* source community is the same as the target community,
* source differs from the target,
* quantity > 0,
* source energy surplus > 0,
* source energy surplus >= quantity to send,
* target need > 0,
* target need > quantity to send,
* target need > target battery energy surplus. Each of the created classes for smart contracts implemented the abstract smart contract class _AbstractSmartContract_ from the _AdapT_ pattern [8]. Class _AbstractSmartContract_ belongs to the abstract layer of the _AdapT_ pattern and enables the implementation of a smart contract from any subject domain. For the purposes of the current research, the _AbstractSmartContract_ class was modified and different configurations of verification rules within a single smart contract were abandoned. This was conducted to achieve the independence of tested smart contracts. The content of the abstract layer of the pattern applied in the current research is presented in the Unified Modeling Language (UML) Class diagram (Figure 7). The implementation of the abstract class for the smart contract has also been changed. The current implementation provides a single list of verification rules. That abstract class implements _checkSC()_ method, which is available and common for all concrete smart contracts inheriting from that class. The source code of the _AbstractSmartContract_ class was shown in (Listing 1). Figure 7: Smart contract abstract classes applied in the tests. ```
importjava.util.List; importjava.util.function.Predicate; publicabstractclassAbstractSmartContract{ //listofverificationrules protectedList<Predicate<AbstractTransaction>>rulesList; //checkingthesmartcontract publicbooleancheckSC(AbstractTransactiontr){ booleancorrect=false; for(Predicate<AbstractTransaction>vR:rulesList){ correct=vR.test(tr); if(!correct)break; } returncorrect; } }
```  
For testing purposes, eight classes of concrete smart contracts were created, which contained from one to eight verification rules. Each of these classes inherits from the abstract _AbstractSmartContract_ class. The class of a particular smart contract only needs to initialize the list of verification rules for that smart contract. For example, the source code of the _SmartContractEightRules_ class for eight verification rules is shown (Listing 2). ```
importjava.util.Arrays; publicfinalclassSmartContractEightRulesextendsAbstractSmartContract{ publicSmartContractEightRules(){ rulesList=Arrays.asList( t->((Transaction)t).getSourceCommunityID()==((Transaction)t). getTargetCommunityID(), t->((Transaction)t).getSourceID()!=((Transaction)t).getTargetID(), t->((Transaction)t).getQuantity()>0, t->((Transaction)t).getSourceSurplus()>0, t->((Transaction)t).getSourceSurplus()>=((Transaction)t).getQuantity(), t->((Transaction)t).getTargetNeed()>0, t->((Transaction)t).getTargetNeed()>((Transaction)t).getQuantity(), getTargetBatterEnergySurplus()); }
```  
The execution time measurement was made using additional Java classes. It should be emphasized that those classes are external to the smart contract. Test classes do not interfere with the structure of the smart contract class. The obtained measurements of the smart contract evaluation time are not burdened with the execution costs of additional activities. The _System.nanoTime()_ method was used to reach high precision time measurement. The method offers a measurement accuracy of 1 nanosecond.