Skip to content

Commit 766b345

Browse files
authored
[P1-M04] Refactor registry to prevent multiple address registra… (#1194)
* implementation Signed-off-by: Christopher Maree <christopher.maree@gmail.com> * removed redundant require Signed-off-by: Christopher Maree <christopher.maree@gmail.com>
1 parent b64028e commit 766b345

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

core/contracts/oracle/implementation/Registry.sol

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ contract Registry is RegistryInterface, MultiRole {
9494
// For all parties in the array add them to the contract's parties.
9595
financialContract.valid = Validity.Valid;
9696
for (uint i = 0; i < parties.length; i = i.add(1)) {
97-
partyMap[parties[i]].contracts.push(contractAddress);
98-
uint newLength = partyMap[parties[i]].contracts.length;
99-
partyMap[parties[i]].contractIndex[contractAddress] = newLength - 1;
97+
_addPartyToContract(parties[i], contractAddress);
10098
}
10199

102100
emit NewContractRegistered(contractAddress, msg.sender, parties);
@@ -113,14 +111,8 @@ contract Registry is RegistryInterface, MultiRole {
113111
address contractAddress = msg.sender;
114112

115113
require(contractMap[contractAddress].valid == Validity.Valid, "Can only add to valid contract");
116-
require(!isPartyMemberOfContract(party, contractAddress), "Can only register a party once");
117-
118-
// Push the contract address and store the index.
119-
uint contractIndex = partyMap[party].contracts.length;
120-
partyMap[party].contracts.push(contractAddress);
121-
partyMap[party].contractIndex[contractAddress] = contractIndex;
122114

123-
emit PartyAdded(contractAddress, party);
115+
_addPartyToContract(party, contractAddress);
124116
}
125117

126118
/**
@@ -207,4 +199,17 @@ contract Registry is RegistryInterface, MultiRole {
207199
uint index = partyMap[party].contractIndex[contractAddress];
208200
return partyMap[party].contracts.length > index && partyMap[party].contracts[index] == contractAddress;
209201
}
202+
203+
/****************************************
204+
* INTERNAL FUNCTIONS *
205+
****************************************/
206+
207+
function _addPartyToContract(address party, address contractAddress) internal {
208+
require(!isPartyMemberOfContract(party, contractAddress), "Can only register a party once");
209+
uint contractIndex = partyMap[party].contracts.length;
210+
partyMap[party].contracts.push(contractAddress);
211+
partyMap[party].contractIndex[contractAddress] = contractIndex;
212+
213+
emit PartyAdded(contractAddress, party);
214+
}
210215
}

core/test/oracle/Registry.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ contract("Registry", function(accounts) {
5454
result = await registry.registerContract(parties, arbitraryContract, { from: creator1 });
5555
assert.isTrue(await registry.isContractRegistered(arbitraryContract));
5656

57+
// Make sure a PartyAdded event is emitted on initial contract registration.
58+
truffleAssert.eventEmitted(result, "PartyAdded", ev => {
59+
return (
60+
web3.utils.toChecksumAddress(ev.contractAddress) === web3.utils.toChecksumAddress(arbitraryContract) &&
61+
// Check that the party is a member of the parties array used in registration above
62+
parties.map(party => web3.utils.toChecksumAddress(party).indexOf(web3.utils.toChecksumAddress(ev.party)))
63+
);
64+
});
65+
5766
// Make sure a NewContractRegistered event is emitted.
5867
truffleAssert.eventEmitted(result, "NewContractRegistered", ev => {
5968
return (
@@ -77,7 +86,6 @@ contract("Registry", function(accounts) {
7786
await registry.resetMember(RegistryRolesEnum.OWNER, rando1, { from: owner });
7887

7988
// The owner can no longer add or remove contract creators.
80-
assert(await didContractThrow(registry.addMember(RegistryRolesEnum.CONTRACT_CREATOR, creator1, { from: owner })));
8189
assert(
8290
await didContractThrow(registry.removeMember(RegistryRolesEnum.CONTRACT_CREATOR, creator1, { from: owner }))
8391
);
@@ -140,6 +148,15 @@ contract("Registry", function(accounts) {
140148

141149
// Cannot register a contract that is already registered.
142150
assert(await didContractThrow(registry.registerContract([], fc1, { from: creator1 })));
151+
152+
// Cannot register the same address to a contract multiple times. In other words one
153+
// address cant be multiple party members of one contract at registration.
154+
const party = web3.utils.randomHex(20);
155+
const party2 = web3.utils.randomHex(20);
156+
157+
assert(
158+
await didContractThrow(registry.registerContract([party, party, party, party2], contract1, { from: creator1 }))
159+
);
143160
});
144161

145162
it("Adding parties to contracts", async function() {

0 commit comments

Comments
 (0)