BlockChain has the potential of bringing the biggest IT revolution of this century or at least first half of this century. After Internet revolution, last century, BlockChain revolution is probably the biggest disruption so far which is challenging the weakest nerve of Internet: of being Trust less. BlockChain is the only technology known so far which can bring trust on a trust less internet. Blockchain brings this trust by making data practically immutable.
Ethereum currently is one of the most widely used BlockChain system used so far. Ethereum too make data immutable in order to create trust among nodes. Now when we say that Ethereum makes data immutable that means that not only the values which is stored becomes immutable but also the program or smart contracts becomes immutable. And that means that if one has deployed a smart contract on Ethereum, whatever may have happened he/she will not be able to update or modify the same.
So, immutability of data comes at a cost and the cost is any future update of smart contract will not be possible which is practically not workable for many use cases. But if we design smart contracts in certain fashion then partially this problem can be resolved.
The proposed architecture solution will work as a parent and child model. As a first step, we can deploy a parent smart contract and this parent contract will have the feature of storing any other contract address and calling that contract using the contract address. Now whenever we deploy a new child contract we can update the parent contract with the address of new child contract and then parent contract will be able to call the child contract. The only limitation here is that the structure of child contract should be known to the parent contract i.e. number of inputs going into child contract and number of output coming out of child contract should be known to the parent contract before deployment of parent contract.
Let’s take an example of calculator in order to explain this architecture better. Let’s say that we want to build a calculator with following functionality on day one –
- Add two numbers
- Multiply two numbers
- Store the result of all the operation and never lose the data
In order to build a calculator with above functionality we first need to deploy a parent contract and this parent contract will have following features –
- Call any child contract which can take two numbers and return one number
- Ability to store contract address
- Ability to store return result in state variable
Once parent contract with above functionality is deployed then one will need to deploy two child contracts, one with add function and one with multiply function. Both these functions will take in two numbers and will return the result and will not store anything in the state variable. Once child contract is deployed, address of these two contract will be updated to the parent contract. Now parent contract can call these two contract and can store the result. This way we have fulfilled the requirement of building a calculator.
Now after some time the user of the calculator decides that he/she do not want add functionality but a subtraction. Also, its already a requirement that no data should be lost. If we try to deploy a new parent and child contract, then we will lose all the data stored by the old parent contract which is undesirable. But since we have followed parent and child model, what we can do is we can deploy another child contract similar to add child contract which will do subtraction and once deployed update the parent contract with the address of this new subtract child contract. This way we will be able to update the functionality of the calculator smart contract and data stored is still immutable.
Below is the diagrammatic explanation of calculator example.
As a first step deploy Parent contract with specified functionality. Parent contract will contain the state variables and only business logic will be present in the child contract. During the design phase of the parent contract it is required to identify all the state variable which will be used in present and can be used in future.
Deploy another child contract with add functionality.
Deploy another child contract with subtract functionality.
Connect Parent contract with both the child contract.
Now in order to remove add function and add subtract function, first let’s deploy subtract contract.
Now remove the connection with Add contract and add the connection with subtract contract. As required, anytime connection to the add contract can be added back again. Also, if parent contract was designed keeping future in mind then may be subtract contract can be added as an added functionality along with Add and Multiply.