This is an old revision of the document!
A Constructor is a special group of Operators that are called when an Object is first created. In most Object-Oriented Programming (OOP) languages (i.e., C++, Java and C#, e.g.), the Constructor has the same name as the Object. It is the initial stage in the Lifecycle of Object Data (see 2.3.4.5 Data Lifecycle Taxonomy). For example, for an Object called Vehicle, the Constructor would also be called Vehicle. The constructor is responsible for the setup of the Object: the initialization of Field Data, the allocation of memory from the Heap. For example, if there are dynamic Field Data, then the memory needed for those Fields is usually allocated from the Heap. Although Constants are generally managed and allocated by the compiler and are from the Stack, Static Field Data can be allocated from the Heap and can be set during construction. For example, the values of Field Data initialized by using initialization parameters on the Constructor or read from initialization or setup files.
Generally, there are three kinds of constructors available in OO:
There is always a default Constructor that required no parameters, however, there can be other Constructors allowing for the passing of values to be used during initialization. For example, the minimum or maximum values used for the Field Data.
Regardless of the number of Constructors, there is always a Constructor that is called when an object is created. Often the calling of a Constructor is automatic and used the default Constructor, but the programmer can use any of the Constructors defined for the Object. See: https://www.tutorialspoint.com/solidity/solidity_constructors.htm
When Data Object is deployed in Ethereum, the following occurs:
The contract is initialized using the optional Constructor method named: constructor(). A Constructor is a special function declared using the constructor keyword. It is an optional function and is used to initialize state variables of a contract. Following are the key characteristics of a constructor1)
Example of a simple Constructor
contract Inventory
{ uint public quantityInStock;
constructor () public
{ quantityInStock = 0;
} // End Inventory constructor
function checkInventory() external view
{ if ( quantityInStock < 0 )
{ revert ( "quantityInStock must be greater than 0");
} // End if
} // End checkInventory
} // end Inventroy contract
Example of a Constructor with arguments that intialize the state variables
contract Inventory
{ uint public quantityInStock;
constructor ( uint _initialQuantity ) public
{ quantityInStock = _initialQuantity;
} // End Inventory constructor
function checkInventory() external view
{ if ( quantityInStock < 0 )
{ revert ( "quantityInStock must be greater than 0");
} // End if
} // End checkInventory
} // end Inventroy contract
Destructor is a special method called automatically during the destruction of an object. Actions executed in the destructor include the following:
Although the original intent of a DIDO is built around the concept of immutability of the data, why is there a need for destruction of the data (see 2.3.4.5 Data Lifecycle Taxonomy).
Since the software (i.e., Smart Contracts) are also stored on the DIDO and are self-executing, they too cannot be modified after they are deployed, not even by the creator of the contract. This is particularly true in Ethereum, which is a permission-less network of nodes meaning the software on the network (i.e., smart contracts) are executed by everyone who can access the network, which includes nefarious actors (i.e., attackers). In addition, the entire contents of the network including constants, state variables, transactions, the smart contract byte code are completely visible to the anyone having access to the DIDO making it an ideal target for “bad actors”.
Jiachi Chen, Xin Xia, David Lo, John Grundy, Why Do Smart Contracts Self-Destruct? Investigating the Selfdestruct Function on Ethereum, May 2020, Accessed: 5 December 2021, https://www.researchgate.net/publication/341478354_Why_Do_Smart_Contracts_Self-Destruct_Investigating_the_Selfdestruct_Function_on_Ethereum ))
The 2016 attack known as the reentrancy attack or DAO attack drew the attention of both academia and industry as various schemes were introduced to prevent such attacks in the future. Part of the solution is to specify requirements, develop and test Smart Contracts rigorously before they are deployed. Although this is always best, it is not always possible to predict all the possible ways a Smart Contract is vulnerable, especially in the future. Therefore, another part of the solution is to add some mechanisms to stop the contracts and/or transfer the tokens when emergency situations arise (e.g., a contract is under attacked). The only option left for the owners of the Smart Contract is to reduce the impact of financial loss. In response, Ethereum's Solidity provides a Selfdestruct function which allows the Smart Contract to transfer all remaining tokens to a different Smart Contract and to remove the errant Smart Contract from the Ethereum network.
When a Data Object is destroyed in Ethereum2), the following occurs:
selfdestruct() method or the deprecated suicide() method