Skip to content
This repository was archived by the owner on Jun 29, 2020. It is now read-only.

Refactor unregister address method #154

Merged
merged 6 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,536 changes: 624 additions & 912 deletions blockchain/build/contracts/ProofOfPhysicalAddress.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions blockchain/contracts/ProofOfPhysicalAddress.sol
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,9 @@ contract ProofOfPhysicalAddress {
// Remove physical address from list
uint256 length = users[msg.sender].physicalAddresses.length;

for (uint256 i = index; i < length - 1; i++) {
users[msg.sender].physicalAddresses[i] = users[msg.sender].physicalAddresses[i+1];
if (length > 1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be if (index != length -1) { ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's much better. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

users[msg.sender].physicalAddresses[index] = users[msg.sender].physicalAddresses[length - 1];
}

delete users[msg.sender].physicalAddresses[length - 1];
users[msg.sender].physicalAddresses.length--;

if (users[msg.sender].physicalAddresses.length == 0) {
Expand Down
6 changes: 3 additions & 3 deletions blockchain/migrations/1522104575_popa.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ module.exports = function(deployer, network) {
await deployer.deploy(TestERC20);
}

await deployer.deploy(POPA, ethereumClaimsRegistryAddress, {
gas: '6000000',
});
const gas = network === 'coverage' ? '0xfffffffffff' : '6000000';

await deployer.deploy(POPA, ethereumClaimsRegistryAddress, { gas });
});
};
83 changes: 83 additions & 0 deletions blockchain/test/proof_of_physical_address.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,89 @@ contract('address removal', function(accounts) {
});
});

contract('', () => {
it('should allow to unregister an address (1 of 3)', async () => {
const popa = await ProofOfPhysicalAddress.deployed();
const args1 = buildRegisterAddressArgs(accounts[0], { address: '742 evergreen terrace' });
const args2 = buildRegisterAddressArgs(accounts[0], { address: '743 evergreen terrace' });
const args3 = buildRegisterAddressArgs(accounts[0], { address: '744 evergreen terrace' });

await registerAddress(popa, args1, accounts[0]);
await registerAddress(popa, args2, accounts[0]);
await registerAddress(popa, args3, accounts[0]);

let addressesCount = await popa.userSubmittedAddressesCount(accounts[0]);
assert.equal(+addressesCount, 3);

await unregisterAddress(popa, args1, accounts[0]);

addressesCount = await popa.userSubmittedAddressesCount(accounts[0]);
assert.equal(+addressesCount, 2);

const [, , , location1] = await popa.userAddress(accounts[0], 0);
const [, , , location2] = await popa.userAddress(accounts[0], 1);
assert.equal(location1, '744 evergreen terrace');
assert.equal(location2, '743 evergreen terrace');
});
});

contract('', () => {
it('should allow to unregister an address (2 of 3)', async () => {
const popa = await ProofOfPhysicalAddress.deployed();
const args1 = buildRegisterAddressArgs(accounts[0], { address: '742 evergreen terrace' });
const args2 = buildRegisterAddressArgs(accounts[0], { address: '743 evergreen terrace' });
const args3 = buildRegisterAddressArgs(accounts[0], { address: '744 evergreen terrace' });

await registerAddress(popa, args1, accounts[0]);
await registerAddress(popa, args2, accounts[0]);
await registerAddress(popa, args3, accounts[0]);

let addressesCount = await popa.userSubmittedAddressesCount(accounts[0]);
assert.equal(+addressesCount, 3);

await unregisterAddress(popa, args2, accounts[0]);

addressesCount = await popa.userSubmittedAddressesCount(accounts[0]);
assert.equal(+addressesCount, 2);

const [, , , location1] = await popa.userAddress(accounts[0], 0);
const [, , , location2] = await popa.userAddress(accounts[0], 1);
assert.equal(location1, '742 evergreen terrace');
assert.equal(location2, '744 evergreen terrace');
});
});

contract('', () => {
it('should allow to unregister an address (3 of 3)', async () => {
const popa = await ProofOfPhysicalAddress.deployed();
const args1 = buildRegisterAddressArgs(accounts[0], { address: '742 evergreen terrace' });
const args2 = buildRegisterAddressArgs(accounts[0], { address: '743 evergreen terrace' });
const args3 = buildRegisterAddressArgs(accounts[0], { address: '744 evergreen terrace' });

await registerAddress(popa, args1, accounts[0]);
await registerAddress(popa, args2, accounts[0]);
await registerAddress(popa, args3, accounts[0]);

let addressesCount = await popa.userSubmittedAddressesCount(accounts[0]);
assert.equal(+addressesCount, 3);

await unregisterAddress(popa, args3, accounts[0]);

addressesCount = await popa.userSubmittedAddressesCount(accounts[0]);
assert.equal(+addressesCount, 2);

const [, , , location1] = await popa.userAddress(accounts[0], 0);
const [, , , location2] = await popa.userAddress(accounts[0], 1);
assert.equal(location1, '742 evergreen terrace');
assert.equal(location2, '743 evergreen terrace');
});
});






contract('', () => {
it('should not delete the user if the unregistered address was not their last one', async () => {
const popa = await ProofOfPhysicalAddress.deployed();
Expand Down