Skip to content

Commit

Permalink
added new tests that are not passing to ArraySimple.js
Browse files Browse the repository at this point in the history
  • Loading branch information
theblockstalk committed Apr 5, 2018
1 parent fc775dc commit f2205fc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ The three main contracts that are used in the upgradeable proxy mechanism are:
3. [Upgradeable.sol](https://github.com/jackandtheblockstalk/upgradeable-proxy/blob/master/contracts/Upgradeable.sol)

Please see in-code contract and function descriptions for how these contracts allow you to make an upgradeable smart contract.
**TODO:** write descriptions of contracts above into code...

### 3.2 How to make a simple Uint getter/setter smart contract upgradeable

Expand Down Expand Up @@ -144,6 +143,8 @@ You cannot do the following changes on an upgraded contract and expect that it w

1. Change the order of previously defined state variables in the next version upgraded smart contracts
- See contract _DoubleUintV2_
- Adding new slots to a fixed size array is not possible. See contract _ArraySimple_
- Adding extra fields to a struct is not possible. See _StructSimple_ (TODO)
2. Declare any variables with initialized values `uint variable1 = 8`. This includes declare any constant state variables `uint constant variable1 = 8`.
- **Note:** state variables must be initialized using the intialize() function.
- See contract _UintInitialize_
Expand Down
51 changes: 50 additions & 1 deletion test/ArraySimple.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
const Proxy = artifacts.require('Proxy')
const ArraySimpleV1a = artifacts.require('ArraySimpleV1a')
const ArraySimpleV1b = artifacts.require('ArraySimpleV1b')
const ArraySimpleV2a = artifacts.require('ArraySimpleV2a')
const ArraySimpleV2a_ExtraValue = artifacts.require('ArraySimpleV2a_ExtraValue')
const ArraySimpleV2b = artifacts.require('ArraySimpleV2b')

const INDENT = ' ';

contract('ArraySimple', function (accounts) {

let proxy,
arraySimpleV1a,
arraySimpleV1b,
arraySimpleV2a,
arraySimpleV2a_ExtraValue,
arraySimpleV2b,
arraySimplebyProxy,
arraySimpleV2a_ExtraValuebyProxy;

const inputValues = [11, 22, 33], inputValues2 = [12, 23, 34];

beforeEach(async function() {
arraySimpleV1a = await ArraySimpleV1a.new();
arraySimpleV1b = await ArraySimpleV2a.new();
arraySimpleV2a = await ArraySimpleV2a.new();
arraySimpleV2a_ExtraValue = await ArraySimpleV2a_ExtraValue.new();
arraySimpleV2b = await ArraySimpleV2b.new();
proxy = await Proxy.new(arraySimpleV1a.address);
arraySimplebyProxy = ArraySimpleV1a.at(proxy.address);
arraySimpleV2a_ExtraValuebyProxy = ArraySimpleV2a_ExtraValue.at(proxy.address);
arraySimpleV1bbyProxy = ArraySimpleV1b.at(proxy.address);
})

function parseBigNumberArray(bnArray) {
Expand All @@ -31,7 +38,7 @@ contract('ArraySimple', function (accounts) {
}
}

it('should be able to upgrade to new fixed size array function', async function () {
it('should be able to upgrade fixed size array function', async function () {
await arraySimplebyProxy.setValues(inputValues)
let values = await arraySimplebyProxy.getValues.call()
parseBigNumberArray(values)
Expand All @@ -49,6 +56,7 @@ contract('ArraySimple', function (accounts) {
})

it('should not be able to use ABI for function that accesses storage that does not exist', async function () {
console.log('Note that smart contract array change arraySimpleV2a_ExtraValue fails!!!')
try {
await arraySimpleV2a_ExtraValuebyProxy.setValues([1, 2, 3, 4]);
throw new Error("This error should not happen")
Expand All @@ -58,11 +66,52 @@ contract('ArraySimple', function (accounts) {
})

it('should not be able to upgrade to function that increases fixed array size', async function () {
console.log('Note that smart contract array change arraySimpleV2a_ExtraValue fails!!!')
await arraySimplebyProxy.upgradeTo(arraySimpleV2a_ExtraValue.address)
await arraySimpleV2a_ExtraValuebyProxy.setValues([1, 2, 3, 4]);

let values = await arraySimplebyProxy.getValues.call()
parseBigNumberArray(values)
assert.deepEqual(values, [1, 2, 3], "Should only equal the first 3 values of the input")
})

it('should not be able to upgrade a fixed size array to a dynamic array', async function () {
console.log('Note that smart contract array change arraySimpleV2b fails!!!')
await arraySimplebyProxy.setValues(inputValues)
let values = await arraySimplebyProxy.getValues.call()
parseBigNumberArray(values)
assert.deepEqual(values, inputValues, "Not equal to inputValues")

await arraySimplebyProxy.upgradeTo(arraySimpleV2b.address)
values = await arraySimplebyProxy.getValues.call()
parseBigNumberArray(values)
console.log('1', inputValues, values)
assert.notDeepEqual(values, inputValues, "Equal to inputValues") // Note that values are not correct here

// await arraySimplebyProxy.setValues(inputValues2)
// values = await arraySimplebyProxy.getValues.call()
// parseBigNumberArray(values)
// console.log('2', '[1, 2, 3]', values)
// assert.deepEqual(values, [1, 2, 3], "Not equal to constant defined in function")
// throw new Error("This error should not happen");
})

it('should be able to upgrade a dynamic size array function', async function () {
proxy = await Proxy.new(arraySimpleV1b.address);
arraySimpleV1bbyProxy = ArraySimpleV1b.at(proxy.address);
await arraySimpleV1bbyProxy.setValues(inputValues)
let values = await arraySimpleV1bbyProxy.getValues.call()
parseBigNumberArray(values)
assert.deepEqual(values, inputValues, "Not equal to inputValues")

// await arraySimpleV1bbyProxy.upgradeTo(arraySimpleV2b.address)
// values = await arraySimplebyProxy.getValues.call()
// parseBigNumberArray(values)
// assert.deepEqual(values, inputValues, "Not equal to inputValues")
//
// await arraySimplebyProxy.setValues(inputValues2)
// values = await arraySimplebyProxy.getValues.call()
// parseBigNumberArray(values)
// assert.deepEqual(values, [1, 2, 3], "Not equal to constant defined in function")
})
})

0 comments on commit f2205fc

Please sign in to comment.