Skip to content

Commit

Permalink
Added address addition and remove to LinkedList data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
asselstine committed Jul 13, 2020
1 parent f6b3f7b commit e179d9f
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
16 changes: 16 additions & 0 deletions contracts/prize-pool/MappedSinglyLinkedList.sol
Expand Up @@ -23,6 +23,22 @@ library MappedSinglyLinkedList {
return array;
}

function addAddress(Mapping storage self, address newAddress) internal {
require(newAddress != SENTINAL_TOKEN && newAddress != address(0), "Invalid address");
require(self.addressMap[newAddress] == address(0), "Already added");
self.addressMap[newAddress] = self.addressMap[SENTINAL_TOKEN];
self.addressMap[SENTINAL_TOKEN] = newAddress;
self.count = self.count + 1;
}

function removeAddress(Mapping storage self, address prevAddress, address addr) internal {
require(addr != SENTINAL_TOKEN && addr != address(0), "Invalid address");
require(self.addressMap[prevAddress] == addr, "Invalid prevAddress");
self.addressMap[prevAddress] = self.addressMap[addr];
self.addressMap[addr] = address(0);
self.count = self.count - 1;
}

function initialize(Mapping storage self, address[] memory addresses) internal {
uint256 count = 0;
self.addressMap[SENTINAL_TOKEN] = SENTINAL_TOKEN;
Expand Down
30 changes: 30 additions & 0 deletions contracts/test/MappedSinglyLinkedListExposed.sol
@@ -0,0 +1,30 @@
pragma solidity ^0.6.4;

import "../prize-pool/MappedSinglyLinkedList.sol";

contract MappedSinglyLinkedListExposed {
using MappedSinglyLinkedList for MappedSinglyLinkedList.Mapping;

MappedSinglyLinkedList.Mapping list;

constructor (address[] memory addresses) public {
list.initialize(addresses);
}

function addressArray() external view returns (address[] memory) {
return list.addressArray();
}

function addAddress(address newAddress) external {
list.addAddress(newAddress);
}

function removeAddress(address prevAddress, address addr) external {
list.removeAddress(prevAddress, addr);
}

function contains(address addr) external view returns (bool) {
return list.contains(addr);
}

}
77 changes: 77 additions & 0 deletions test/MappedSinglyLinkedListExposed.test.js
@@ -0,0 +1,77 @@
const { deployContract, deployMockContract } = require('ethereum-waffle')
const MappedSinglyLinkedListExposed = require('../build/MappedSinglyLinkedListExposed.json')

const { ethers } = require('./helpers/ethers')
const { expect } = require('chai')
const buidler = require('./helpers/buidler')
const { AddressZero } = require('ethers/constants')

const toWei = ethers.utils.parseEther

const debug = require('debug')('ptv3:MappedSinglyLinkedListExposed.test')

let overrides = { gasLimit: 20000000 }

const SENTINAL = '0x0000000000000000000000000000000000000001'

describe('PrizePool contract', function() {

let list

beforeEach(async () => {
[wallet, wallet2, wallet3, wallet4] = await buidler.ethers.getSigners()

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

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

describe('addressArray', () =>{
it('should create an array from addresses', async () => {
expect(await list.addressArray()).to.deep.equal([wallet2._address])
})
})

describe('addAddress', () => {
it('should not allow adding the SENTINAL address', async () => {
await expect(list.addAddress(SENTINAL)).to.be.revertedWith("Invalid address")
})

it('should not allow adding a zero address', async () => {
await expect(list.addAddress(AddressZero)).to.be.revertedWith("Invalid address")
})

it('should allow the user to add an address', async () => {
await list.addAddress(wallet._address)

expect(await list.addressArray()).to.deep.equal([wallet._address, wallet2._address])
})
})

describe('removeAddress', () => {
it('should not allow removing the SENTINAL address', async () => {
await expect(list.removeAddress(SENTINAL, SENTINAL)).to.be.revertedWith("Invalid address")
})

it('should not allow removing an address that does not exist', async () => {
await expect(list.removeAddress(wallet._address, wallet2._address)).to.be.revertedWith("Invalid prevAddress")
})

it('should not allow removing a zero address', async () => {
await expect(list.removeAddress(wallet._address, AddressZero)).to.be.revertedWith("Invalid address")
})

it('should allow the user to add an address', async () => {
await list.addAddress(wallet._address)

await list.removeAddress(wallet._address, wallet2._address)

expect(await list.addressArray()).to.deep.equal([wallet._address])
expect(await list.contains(wallet2._address)).to.be.false
})
})
});

0 comments on commit e179d9f

Please sign in to comment.