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

[contract] Add statistics variables #68

Merged
merged 3 commits into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions blockchain/contracts/PoBA.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ contract PoBA {

mapping (address => User) public users;

// Stats:
uint64 public totalUsers;
uint64 public totalBankAccounts;

// Events:
event LogBankAccountRegistered(address indexed wallet, bytes32 keccakIdentifier);
event LogBankAccountUnregistered(address indexed wallet, bytes32 keccakIdentifier);
event LogSignerChanged(address newSigner);

// Modifiers:
modifier onlyOwner() {
Expand Down Expand Up @@ -60,6 +65,7 @@ contract PoBA {
// and on contract-side to verify them
function setSigner(address newSigner) public onlyOwner {
signer = newSigner;
emit LogSignerChanged(newSigner);
}

function register(
Expand All @@ -74,6 +80,7 @@ contract PoBA {
if (!userExists(msg.sender)) {
// new user
users[msg.sender].creationBlock = block.number;
totalUsers += 1;
}

BankAccount memory ba;
Expand All @@ -85,15 +92,17 @@ contract PoBA {
ba.creationBlock = block.number;

bytes32 hash = keccak256(
abi.encodePacked(
msg.sender,
abi.encodePacked(
msg.sender,
ba.accountNumber,
ba.bankName
));
)
);
require(signerIsValid(hash, v, r, s));
ba.keccakIdentifier = hash;

users[msg.sender].bankAccounts.push(ba);
totalBankAccounts += 1;

emit LogBankAccountRegistered(msg.sender, ba.keccakIdentifier);
}
Expand All @@ -116,9 +125,11 @@ contract PoBA {
users[msg.sender].bankAccounts[index] = users[msg.sender].bankAccounts[length - 1];
}
users[msg.sender].bankAccounts.length--;
totalBankAccounts -= 1;

if (users[msg.sender].bankAccounts.length == 0) {
delete users[msg.sender];
totalUsers -= 1;
}

emit LogBankAccountUnregistered(msg.sender, keccakIdentifier);
Expand Down
136 changes: 136 additions & 0 deletions blockchain/test/poba.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,42 @@ contract('bank account registration (success)', () => {
assert.equal(+bankAccounts2, 1)
})
})

contract('', () => {
it("totalUsers should be incremented if it's the first bank account for that user", async () => {
const poba = await PoBA.deployed()

let users = await poba.totalUsers()
assert.equal(+users, 0)

const bankAccountData = {
account: '1111222233330000',
institution: 'Chase'
}
const args = buildRegisterBankAccountArgs(ethAccount[0], bankAccountData)
await registerBankAccount(poba, args, ethAccount[0])

users = await poba.totalUsers()
assert.equal(+users, 1)
})
})

contract('', () => {
it('should increment totalBankAccounts value', async () => {
const poba = await PoBA.deployed()
let totalBankAccounts = await poba.totalBankAccounts()
assert.equal(+totalBankAccounts, 0)

const accountData = {
account: '1111222233330001',
institution: 'Chase'
}
const args = buildRegisterBankAccountArgs(ethAccount[0], accountData)
await registerBankAccount(poba, args, ethAccount[0])
totalBankAccounts = await poba.totalBankAccounts()
assert.equal(+totalBankAccounts, 1)
})
})
})

contract('bank account registration (fail)', () => {
Expand Down Expand Up @@ -240,6 +276,29 @@ contract('bank account registration (fail)', () => {
)
})
})

contract('', () => {
it('totalUsers should not be incremented if registerBankAccount fails', async () => {
const poba = await PoBA.deployed()

let totalUsers = await poba.totalUsers()
assert.equal(+totalUsers, 0)

// Make registerBankAccount fail with an emtpy institutuion
const bankAccountData = {
account: '1111222233330000',
institution: ''
}
const args = buildRegisterBankAccountArgs(ethAccount[0], bankAccountData)
await registerBankAccount(poba, args, ethAccount[0]).then(
() => assert.fail(), // should reject
async () => {
totalUsers = await poba.totalUsers()
assert.equal(+totalUsers, 0)
}
)
})
})
})

contract('bank account removal', accounts => {
Expand Down Expand Up @@ -366,6 +425,83 @@ contract('bank account removal', accounts => {
assert.equal(+bankAccountCount, 1)
})
})

contract('', () => {
it('should decrement totalUsers value if the unregistered bank account was the only one for that user', async () => {
const poba = await PoBA.deployed()

let totalUsers = await poba.totalUsers()
assert.equal(+totalUsers, 0)

const bankAccountData = {
account: '1111222233330000',
institution: 'Chase'
}
const args = buildRegisterBankAccountArgs(accounts[0], bankAccountData)
await registerBankAccount(poba, args, accounts[0])
const bankAccountCount = await poba.accountsLength(accounts[0])
assert.equal(+bankAccountCount, 1)

totalUsers = await poba.totalUsers()
assert.equal(+totalUsers, 1)

await unregisterBankAccount(poba, args, accounts[0])

totalUsers = await poba.totalUsers()
assert.equal(+totalUsers, 0)
})
})

contract('', () => {
it('should not decrement totalUsers value if the unregistered bank account was not the only one for that user', async () => {
const poba = await PoBA.deployed()

let totalUsers = await poba.totalUsers()
assert.equal(+totalUsers, 0)
let bankAccounts = await poba.accountsLength(ethAccount[0])
assert.equal(+bankAccounts, 0)

const bankOne = {
account: '1111222233330000',
institution: 'Chase'
}
const bankTwo = {
account: '1111222233330001',
institution: 'Sugar'
}
const args1 = buildRegisterBankAccountArgs(ethAccount[0], bankOne)
await registerBankAccount(poba, args1, ethAccount[0])
const args2 = buildRegisterBankAccountArgs(ethAccount[0], bankTwo)
await registerBankAccount(poba, args2, ethAccount[0])
bankAccounts = await poba.accountsLength(ethAccount[0])
assert.equal(+bankAccounts, 2)

await unregisterBankAccount(poba, args1, ethAccount[0])

totalUsers = await poba.totalUsers()
assert.equal(+totalUsers, 1)
})
})

contract('', () => {
it('should decrement totalBankAccounts value', async () => {
const poba = await PoBA.deployed()
const accountData = {
account: '1111222233330001',
institution: 'Chase'
}
const args = buildRegisterBankAccountArgs(ethAccount[0], accountData)
await registerBankAccount(poba, args, ethAccount[0])

let totalBankAccounts = await poba.totalBankAccounts()
assert.equal(+totalBankAccounts, 1)

await unregisterBankAccount(poba, args, ethAccount[0])

totalBankAccounts = await poba.totalBankAccounts()
assert.equal(+totalBankAccounts, 0)
})
})
})

contract('setSigner', accounts => {
Expand Down