Last active
February 23, 2017 10:54
-
-
Save pprados/3c9bd8182bfb5b9757f18de5fd364838 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.8; | |
/** | |
* The version 1 of the contract. | |
* The attr is initialized to 1000. | |
* The method doSomething() return attr + version = 1001 | |
*/ | |
contract ContractV1 { | |
uint constant private version=1; | |
uint public attr=1000; | |
/** return attr+newAttr+version (1001). */ | |
function doSomething() constant returns(uint) { | |
return attr+version; | |
} | |
// -- Technical functions and attributs | |
/** kill this version. */ | |
function replacedBy(address newVersion) { | |
selfdestruct(newVersion); | |
} | |
} | |
/** | |
* The version 2 of the contract. | |
* To preserve all the attributs from the v1 version, this version IS a ContractV1. | |
* All methods can be rewrite, new one can be added and some attributs can be added. | |
* | |
* The newAttr is initialized to 100. | |
* The method doSomething() return attr + newAtttr + version = 1102 | |
*/ | |
contract ContractV2 { | |
uint constant private version=2; | |
uint attr; | |
uint newAttr=100; | |
/** return attr+newAttr+version (1102). */ | |
function doSomething() constant returns(uint) { | |
return attr+newAttr+version; | |
} | |
/** return 42. Another method in version 2. */ | |
function doOtherThing() constant returns(uint) { | |
return 42; | |
} | |
// -- Technical functions and attributs | |
/** | |
* Propagate the states from the v1 to v2. | |
*/ | |
function ContractV2(ContractV1 origin) { | |
attr=origin.attr(); // Copy the current state of v1 | |
origin.replacedBy(this); | |
} | |
// -- Technical functions and attributs | |
/** kill this version. */ | |
function replacedBy(address newVersion) { | |
selfdestruct(newVersion); | |
} | |
} | |
// ------------- | |
/** | |
* Unit test. | |
* | |
* After created an instance, | |
* - call 'init()' | |
* - call 'doSomething()' return 1001 | |
* - call 'changeToV2' | |
* - call 'doSomething()' return 1102 | |
* - call 'doOtherthing()' return 42 | |
*/ | |
contract A_UnitTest { | |
ContractV1 aContractV1; | |
ContractV2 aContractV2; | |
/** | |
* Initialise useContract with a ContractV1. | |
*/ | |
function init() { | |
aContractV1=new ContractV1(); | |
} | |
/** | |
* Invoke the method 'doSomething()', valide with a contract v1 or v2. | |
* The caller must be known the reference of the current version. | |
*/ | |
function doSomething() constant returns(uint) { | |
return ((address(aContractV2) == 0) | |
? aContractV1.doSomething() | |
: aContractV2.doSomething()); | |
} | |
/** Invoke a method 'doOtherThing()', only valide with a contract v2. */ | |
function doOtherThing() constant returns(uint) { | |
if (address(aContractV2) == 0) throw; | |
return aContractV2.doOtherThing(); | |
} | |
/** Change to V2. */ | |
function changeToV2() { | |
aContractV2=new ContractV2(aContractV1); | |
aContractV1.replacedBy(aContractV2); | |
delete aContractV1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment