Skip to content

Instantly share code, notes, and snippets.

@pprados
Created February 23, 2017 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pprados/2041e29028a55f33250944dbc9ae8dc4 to your computer and use it in GitHub Desktop.
Save pprados/2041e29028a55f33250944dbc9ae8dc4 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.8;
/**
* Interface shared by Proxy, ContractV1 and ContractV2.
*/
contract Interface {
/** return uint, depend of the current version. */
function doSomething() constant returns(uint);
}
/**
* A contract with amendment.
* The attr is initialized to 1000.
* The method doSomething() return attr + version = 1001
*/
contract Contract is Interface {
uint constant private version=1;
uint public attr=1000;
/*
Impossible to use modifier ! You must insert this kind of code in each method.
modifier viaAmendment() {
if ((address(amendment) != 0) && (msg.sender != address(amendment))) {
return ???
}
else
_;
}
*/
function doSomething() constant returns(uint) {
if ((address(amendment) != 0) && (msg.sender != address(amendment))) {
return amendment.doSomething();
}
else
return attr+version;
}
// -- Technical functions and attributs
/** Amendment. */
Interface public amendment;
/** Set amendment. */
function setAmendment(Interface _amendment) {
amendment=_amendment;
}
}
/**
* An amendment.
*/
contract Amendment is Interface {
uint constant private version=2;
uint newAttr=100;
/**
* The new version of the method doSomething.
*/
function doSomething() constant returns(uint) {
return origin.attr()+newAttr+version;
}
/**
* This new method can't be called.
*/
function doOtherThing() constant returns(uint) {
return 42;
}
// -- Technical functions and attributs
/** Origin contract. */
Contract public origin;
/**
* New amendment linked with the original contract.
*/
function Amendment(Contract _origin) {
origin=_origin;
}
}
// -------------
/**
* Unit test.
*
* After created an instance,
* - call 'init()'
* - call 'doSomething()' return 1001
* - call 'changeToV2'
* - call 'doSomething()' return 1102
*/
contract A_UnitTest {
Contract private aContract;
function init() {
aContract=new Contract();
}
function doSomething() constant returns(uint) {
return aContract.doSomething();
}
function doOtherThing() constant returns(uint) {
throw; // Not implemented
}
/** Change to V2. */
function changeToV2() {
aContract.setAmendment(new Amendment(aContract));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment