Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a BitMap struct #2710

Merged
merged 10 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 23 additions & 0 deletions contracts/mocks/BitmapMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "../utils/structs/BitMap.sol";

contract BitMapMock {
using BitMap for BitMap.UintBitMap;

BitMap.UintBitMap private _bitmap;

function get(uint256 index) public view returns (bool) {
return _bitmap.get(index);
}

function set(uint256 index) public {
_bitmap.set(index);
}

function unset(uint256 index) public {
_bitmap.unset(index);
}
}
32 changes: 32 additions & 0 deletions contracts/utils/structs/BitMap.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

/**
* @dev Library for managing uint256 to bool mapping in a compact and efficient way, providing the keys are sequential.
* Largelly inspired by Uniswap's https://github.com/Uniswap/merkle-distributor/blob/master/contracts/MerkleDistributor.sol[merkle-distributor].
*/
library BitMap {
struct UintBitMap {
Amxx marked this conversation as resolved.
Show resolved Hide resolved
mapping(uint256 => uint256) _data;
}

function get(UintBitMap storage bitmap, uint256 index) internal view returns (bool) {
uint256 bucket = index / 256;
uint256 pos = index % 256;
uint256 word = bitmap._data[bucket];
uint256 mask = (1 << pos);
return word & mask == mask;
Amxx marked this conversation as resolved.
Show resolved Hide resolved
}

function set(UintBitMap storage bitmap, uint256 index) internal {
uint256 bucket = index / 256;
uint256 pos = index % 256;
bitmap._data[bucket] = bitmap._data[bucket] | (1 << pos);
Amxx marked this conversation as resolved.
Show resolved Hide resolved
}

function unset(UintBitMap storage bitmap, uint256 index) internal {
uint256 bucket = index / 256;
uint256 pos = index % 256;
bitmap._data[bucket] = bitmap._data[bucket] & ~(1 << pos);
Amxx marked this conversation as resolved.
Show resolved Hide resolved
}
}
113 changes: 113 additions & 0 deletions test/utils/structs/BitMap.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const { BN } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');

const BitMap = artifacts.require('BitMapMock');

contract('BitMap', function (accounts) {
const keyA = new BN('7891');
const keyB = new BN('451');
const keyC = new BN('9592328');

beforeEach(async function () {
this.bitmap = await BitMap.new();
});

it('starts empty', async function () {
expect(await this.bitmap.get(keyA)).to.equal(false);
expect(await this.bitmap.get(keyB)).to.equal(false);
expect(await this.bitmap.get(keyC)).to.equal(false);
});

describe('set', function () {
it('adds a key', async function () {
await this.bitmap.set(keyA);
expect(await this.bitmap.get(keyA)).to.equal(true);
expect(await this.bitmap.get(keyB)).to.equal(false);
expect(await this.bitmap.get(keyC)).to.equal(false);
});

it('adds several keys', async function () {
await this.bitmap.set(keyA);
await this.bitmap.set(keyB);
expect(await this.bitmap.get(keyA)).to.equal(true);
expect(await this.bitmap.get(keyB)).to.equal(true);
expect(await this.bitmap.get(keyC)).to.equal(false);
});

it('adds several consecutive keys', async function () {
await this.bitmap.set(keyA.addn(0));
await this.bitmap.set(keyA.addn(1));
await this.bitmap.set(keyA.addn(3));
expect(await this.bitmap.get(keyA.addn(0))).to.equal(true);
expect(await this.bitmap.get(keyA.addn(1))).to.equal(true);
expect(await this.bitmap.get(keyA.addn(2))).to.equal(false);
expect(await this.bitmap.get(keyA.addn(3))).to.equal(true);
expect(await this.bitmap.get(keyA.addn(4))).to.equal(false);
});
});

describe('unset', function () {
it('removes added keys', async function () {
await this.bitmap.set(keyA);
await this.bitmap.set(keyB);
await this.bitmap.unset(keyA);
expect(await this.bitmap.get(keyA)).to.equal(false);
expect(await this.bitmap.get(keyB)).to.equal(true);
expect(await this.bitmap.get(keyC)).to.equal(false);
});

it('removes consecutive added keys', async function () {
await this.bitmap.set(keyA.addn(0));
await this.bitmap.set(keyA.addn(1));
await this.bitmap.set(keyA.addn(3));
await this.bitmap.unset(keyA.addn(1));
expect(await this.bitmap.get(keyA.addn(0))).to.equal(true);
expect(await this.bitmap.get(keyA.addn(1))).to.equal(false);
expect(await this.bitmap.get(keyA.addn(2))).to.equal(false);
expect(await this.bitmap.get(keyA.addn(3))).to.equal(true);
expect(await this.bitmap.get(keyA.addn(4))).to.equal(false);
});

it('adds and removes multiple keys', async function () {
// []

await this.bitmap.set(keyA);
await this.bitmap.set(keyC);

// [A, C]

await this.bitmap.unset(keyA);
await this.bitmap.unset(keyB);

// [C]

await this.bitmap.set(keyB);

// [C, B]

await this.bitmap.set(keyA);
await this.bitmap.unset(keyC);

// [A, B]

await this.bitmap.set(keyA);
await this.bitmap.set(keyB);

// [A, B]

await this.bitmap.set(keyC);
await this.bitmap.unset(keyA);

// [B, C]

await this.bitmap.set(keyA);
await this.bitmap.unset(keyB);

// [A, C]

expect(await this.bitmap.get(keyA)).to.equal(true);
expect(await this.bitmap.get(keyB)).to.equal(false);
expect(await this.bitmap.get(keyC)).to.equal(true);
});
});
});