Skip to content

Commit

Permalink
Added initialize protection to MappedSinglyLinkedList
Browse files Browse the repository at this point in the history
  • Loading branch information
asselstine committed Aug 14, 2020
1 parent 30c0ada commit 790ee5d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion contracts/test/MappedSinglyLinkedListExposed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ contract MappedSinglyLinkedListExposed {

MappedSinglyLinkedList.Mapping list;

constructor () public {
function initialize() external {
list.initialize();
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/utils/MappedSinglyLinkedList.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ library MappedSinglyLinkedList {
/// @notice Initializes the list.
/// @dev It is important that this is called so that the SENTINAL is correctly setup.
function initialize(Mapping storage self) internal {
require(self.count == 0, "Already init");
self.addressMap[SENTINAL] = SENTINAL;
self.count = 0;
}

function addAddresses(Mapping storage self, address[] memory addresses) internal {
Expand Down
5 changes: 5 additions & 0 deletions test/MappedSinglyLinkedListExposed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ describe('MappedSinglyLinkedListExposed', function() {
[wallet, wallet2, wallet3, wallet4] = await buidler.ethers.getSigners()

list = await deployContract(wallet, MappedSinglyLinkedListExposed, [], overrides)
await list.initialize()
await list.addAddresses([wallet2._address])
})

describe('initialize()', () => {
it('should have initialized with a value', async () => {
expect(await list.contains(wallet2._address)).to.be.true
})

it('should not be initialized after it contains values', async () => {
await expect(list.initialize()).to.be.revertedWith('Already init')
})
})

describe('addressArray', () =>{
Expand Down

0 comments on commit 790ee5d

Please sign in to comment.