Add bytes32 to bytes32 enumerable map (#3192)

* feat(enumerablemap): add bytes32 to bytes32 map

* chore(changelog): edit CHANGELOG

* feat(enumerable map): edit struct visibility
This commit is contained in:
Wias Liaw
2022-03-22 23:36:29 +08:00
committed by GitHub
parent c028c56965
commit b13bdb0249
8 changed files with 334 additions and 378 deletions

View File

@ -0,0 +1,181 @@
const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const zip = require('lodash.zip');
function shouldBehaveLikeMap (keys, values, zeroValue) {
const [keyA, keyB, keyC] = keys;
const [valueA, valueB, valueC] = values;
async function expectMembersMatch (map, keys, values) {
expect(keys.length).to.equal(values.length);
await Promise.all(keys.map(async key =>
expect(await map.contains(key)).to.equal(true),
));
expect(await map.length()).to.bignumber.equal(keys.length.toString());
expect(
(await Promise.all(keys.map(key => map.get(key)))).map(k => k.toString()),
).to.have.same.members(
values.map(value => value.toString()),
);
// To compare key-value pairs, we zip keys and values, and convert BNs to
// strings to workaround Chai limitations when dealing with nested arrays
expect(await Promise.all([...Array(keys.length).keys()].map(async (index) => {
const entry = await map.at(index);
return [entry.key.toString(), entry.value.toString()];
}))).to.have.same.deep.members(
zip(keys.map(k => k.toString()), values.map(v => v.toString())),
);
}
it('starts empty', async function () {
expect(await this.map.contains(keyA)).to.equal(false);
await expectMembersMatch(this.map, [], []);
});
describe('set', function () {
it('adds a key', async function () {
const receipt = await this.map.set(keyA, valueA);
expectEvent(receipt, 'OperationResult', { result: true });
await expectMembersMatch(this.map, [keyA], [valueA]);
});
it('adds several keys', async function () {
await this.map.set(keyA, valueA);
await this.map.set(keyB, valueB);
await expectMembersMatch(this.map, [keyA, keyB], [valueA, valueB]);
expect(await this.map.contains(keyC)).to.equal(false);
});
it('returns false when adding keys already in the set', async function () {
await this.map.set(keyA, valueA);
const receipt = (await this.map.set(keyA, valueA));
expectEvent(receipt, 'OperationResult', { result: false });
await expectMembersMatch(this.map, [keyA], [valueA]);
});
it('updates values for keys already in the set', async function () {
await this.map.set(keyA, valueA);
await this.map.set(keyA, valueB);
await expectMembersMatch(this.map, [keyA], [valueB]);
});
});
describe('remove', function () {
it('removes added keys', async function () {
await this.map.set(keyA, valueA);
const receipt = await this.map.remove(keyA);
expectEvent(receipt, 'OperationResult', { result: true });
expect(await this.map.contains(keyA)).to.equal(false);
await expectMembersMatch(this.map, [], []);
});
it('returns false when removing keys not in the set', async function () {
const receipt = await this.map.remove(keyA);
expectEvent(receipt, 'OperationResult', { result: false });
expect(await this.map.contains(keyA)).to.equal(false);
});
it('adds and removes multiple keys', async function () {
// []
await this.map.set(keyA, valueA);
await this.map.set(keyC, valueC);
// [A, C]
await this.map.remove(keyA);
await this.map.remove(keyB);
// [C]
await this.map.set(keyB, valueB);
// [C, B]
await this.map.set(keyA, valueA);
await this.map.remove(keyC);
// [A, B]
await this.map.set(keyA, valueA);
await this.map.set(keyB, valueB);
// [A, B]
await this.map.set(keyC, valueC);
await this.map.remove(keyA);
// [B, C]
await this.map.set(keyA, valueA);
await this.map.remove(keyB);
// [A, C]
await expectMembersMatch(this.map, [keyA, keyC], [valueA, valueC]);
expect(await this.map.contains(keyB)).to.equal(false);
});
});
describe('read', function () {
beforeEach(async function () {
await this.map.set(keyA, valueA);
});
describe('get', function () {
it('existing value', async function () {
expect(
(await this.map.get(keyA)).toString(),
).to.be.equal(valueA.toString());
});
it('missing value', async function () {
await expectRevert(this.map.get(keyB), 'EnumerableMap: nonexistent key');
});
});
describe('get with message', function () {
it('existing value', async function () {
expect(
(await this.map.getWithMessage(keyA, 'custom error string'))
.toString(),
).to.be.equal(valueA.toString());
});
it('missing value', async function () {
await expectRevert(this.map.getWithMessage(keyB, 'custom error string'), 'custom error string');
});
});
describe('tryGet', function () {
it('existing value', async function () {
const result = await this.map.tryGet(keyA);
expect(result['0']).to.be.equal(true);
expect(result['1'].toString()).to.be.equal(valueA.toString());
});
it('missing value', async function () {
const result = await this.map.tryGet(keyB);
expect(result['0']).to.be.equal(false);
expect(result['1'].toString()).to.be.equal(zeroValue.toString());
});
});
});
}
module.exports = {
shouldBehaveLikeMap,
};

View File

@ -0,0 +1,58 @@
const { BN, constants } = require('@openzeppelin/test-helpers');
const AddressToUintMapMock = artifacts.require('AddressToUintMapMock');
const UintToAddressMapMock = artifacts.require('UintToAddressMapMock');
const Bytes32ToBytes32MapMock = artifacts.require('Bytes32ToBytes32MapMock');
const { shouldBehaveLikeMap } = require('./EnumerableMap.behavior');
contract('EnumerableMap', function (accounts) {
const [ accountA, accountB, accountC ] = accounts;
const keyA = new BN('7891');
const keyB = new BN('451');
const keyC = new BN('9592328');
const bytesA = '0xdeadbeef'.padEnd(66, '0');
const bytesB = '0x0123456789'.padEnd(66, '0');
const bytesC = '0x42424242'.padEnd(66, '0');
// AddressToUintMap
describe('AddressToUintMap', function () {
beforeEach(async function () {
this.map = await AddressToUintMapMock.new();
});
shouldBehaveLikeMap(
[accountA, accountB, accountC],
[keyA, keyB, keyC],
new BN('0'),
);
});
// UintToAddressMap
describe('UintToAddressMap', function () {
beforeEach(async function () {
this.map = await UintToAddressMapMock.new();
});
shouldBehaveLikeMap(
[keyA, keyB, keyC],
[accountA, accountB, accountC],
constants.ZERO_ADDRESS,
);
});
// Bytes32ToBytes32Map
describe('Bytes32ToBytes32Map', function () {
beforeEach(async function () {
this.map = await Bytes32ToBytes32MapMock.new();
});
shouldBehaveLikeMap(
[keyA, keyB, keyC].map(k => ('0x' + k.toString(16)).padEnd(66, '0')),
[bytesA, bytesB, bytesC],
constants.ZERO_BYTES32,
);
});
});

View File

@ -1,157 +0,0 @@
const { BN, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const { expectMembersMatch } = require('./helpers');
const AddressToUintMapMock = artifacts.require('AddressToUintMapMock');
contract('AddressToUintMap', function (accounts) {
const [accountA, accountB, accountC] = accounts;
const valueA = new BN('7891');
const valueB = new BN('451');
const valueC = new BN('9592328');
beforeEach(async function () {
this.map = await AddressToUintMapMock.new();
});
it('starts empty', async function () {
expect(await this.map.contains(accountA)).to.equal(false);
await expectMembersMatch(this.map, [], []);
});
describe('set', function () {
it('adds a key', async function () {
const receipt = await this.map.set(accountA, valueA);
expectEvent(receipt, 'OperationResult', { result: true });
await expectMembersMatch(this.map, [accountA], [valueA]);
});
it('adds several keys', async function () {
await this.map.set(accountA, valueA);
await this.map.set(accountB, valueB);
await expectMembersMatch(this.map, [accountA, accountB], [valueA, valueB]);
expect(await this.map.contains(accountC)).to.equal(false);
});
it('returns false when adding keys already in the set', async function () {
await this.map.set(accountA, valueA);
const receipt = await this.map.set(accountA, valueA);
expectEvent(receipt, 'OperationResult', { result: false });
await expectMembersMatch(this.map, [accountA], [valueA]);
});
it('updates values for keys already in the set', async function () {
await this.map.set(accountA, valueA);
await this.map.set(accountA, valueB);
await expectMembersMatch(this.map, [accountA], [valueB]);
});
});
describe('remove', function () {
it('removes added keys', async function () {
await this.map.set(accountA, valueA);
const receipt = await this.map.remove(accountA);
expectEvent(receipt, 'OperationResult', { result: true });
expect(await this.map.contains(accountA)).to.equal(false);
await expectMembersMatch(this.map, [], []);
});
it('returns false when removing keys not in the set', async function () {
const receipt = await this.map.remove(accountA);
expectEvent(receipt, 'OperationResult', { result: false });
expect(await this.map.contains(accountA)).to.equal(false);
});
it('adds and removes multiple keys', async function () {
// []
await this.map.set(accountA, valueA);
await this.map.set(accountC, valueC);
// [A, C]
await this.map.remove(accountA);
await this.map.remove(accountB);
// [C]
await this.map.set(accountB, valueB);
// [C, B]
await this.map.set(accountA, valueA);
await this.map.remove(accountC);
// [A, B]
await this.map.set(accountA, valueA);
await this.map.set(accountB, valueB);
// [A, B]
await this.map.set(accountC, valueC);
await this.map.remove(accountA);
// [B, C]
await this.map.set(accountA, valueA);
await this.map.remove(accountB);
// [A, C]
await expectMembersMatch(this.map, [accountA, accountC], [valueA, valueC]);
expect(await this.map.contains(accountB)).to.equal(false);
});
});
describe('read', function () {
beforeEach(async function () {
await this.map.set(accountA, valueA);
});
describe('get', function () {
it('existing value', async function () {
expect(await this.map.get(accountA)).to.bignumber.equal(valueA);
});
it('missing value', async function () {
await expectRevert(this.map.get(accountB), 'EnumerableMap: nonexistent key');
});
});
describe('tryGet', function () {
const stringifyTryGetValue = ({ 0: result, 1: value }) => ({ 0: result, 1: value.toString() });
it('existing value', async function () {
const actual = stringifyTryGetValue(await this.map.tryGet(accountA));
const expected = stringifyTryGetValue({ 0: true, 1: valueA });
expect(actual).to.deep.equal(expected);
});
it('missing value', async function () {
const actual = stringifyTryGetValue(await this.map.tryGet(accountB));
const expected = stringifyTryGetValue({ 0: false, 1: new BN('0') });
expect(actual).to.deep.equal(expected);
});
});
});
});

View File

@ -1,157 +0,0 @@
const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const { expectMembersMatch } = require('./helpers');
const UintToAddressMapMock = artifacts.require('UintToAddressMapMock');
contract('UintToAddressMap', function (accounts) {
const [accountA, accountB, accountC] = accounts;
const keyA = new BN('7891');
const keyB = new BN('451');
const keyC = new BN('9592328');
beforeEach(async function () {
this.map = await UintToAddressMapMock.new();
});
it('starts empty', async function () {
expect(await this.map.contains(keyA)).to.equal(false);
await expectMembersMatch(this.map, [], []);
});
describe('set', function () {
it('adds a key', async function () {
const receipt = await this.map.set(keyA, accountA);
expectEvent(receipt, 'OperationResult', { result: true });
await expectMembersMatch(this.map, [keyA], [accountA]);
});
it('adds several keys', async function () {
await this.map.set(keyA, accountA);
await this.map.set(keyB, accountB);
await expectMembersMatch(this.map, [keyA, keyB], [accountA, accountB]);
expect(await this.map.contains(keyC)).to.equal(false);
});
it('returns false when adding keys already in the set', async function () {
await this.map.set(keyA, accountA);
const receipt = await this.map.set(keyA, accountA);
expectEvent(receipt, 'OperationResult', { result: false });
await expectMembersMatch(this.map, [keyA], [accountA]);
});
it('updates values for keys already in the set', async function () {
await this.map.set(keyA, accountA);
await this.map.set(keyA, accountB);
await expectMembersMatch(this.map, [keyA], [accountB]);
});
});
describe('remove', function () {
it('removes added keys', async function () {
await this.map.set(keyA, accountA);
const receipt = await this.map.remove(keyA);
expectEvent(receipt, 'OperationResult', { result: true });
expect(await this.map.contains(keyA)).to.equal(false);
await expectMembersMatch(this.map, [], []);
});
it('returns false when removing keys not in the set', async function () {
const receipt = await this.map.remove(keyA);
expectEvent(receipt, 'OperationResult', { result: false });
expect(await this.map.contains(keyA)).to.equal(false);
});
it('adds and removes multiple keys', async function () {
// []
await this.map.set(keyA, accountA);
await this.map.set(keyC, accountC);
// [A, C]
await this.map.remove(keyA);
await this.map.remove(keyB);
// [C]
await this.map.set(keyB, accountB);
// [C, B]
await this.map.set(keyA, accountA);
await this.map.remove(keyC);
// [A, B]
await this.map.set(keyA, accountA);
await this.map.set(keyB, accountB);
// [A, B]
await this.map.set(keyC, accountC);
await this.map.remove(keyA);
// [B, C]
await this.map.set(keyA, accountA);
await this.map.remove(keyB);
// [A, C]
await expectMembersMatch(this.map, [keyA, keyC], [accountA, accountC]);
expect(await this.map.contains(keyB)).to.equal(false);
});
});
describe('read', function () {
beforeEach(async function () {
await this.map.set(keyA, accountA);
});
describe('get', function () {
it('existing value', async function () {
expect(await this.map.get(keyA)).to.be.equal(accountA);
});
it('missing value', async function () {
await expectRevert(this.map.get(keyB), 'EnumerableMap: nonexistent key');
});
});
describe('get with message', function () {
it('existing value', async function () {
expect(await this.map.getWithMessage(keyA, 'custom error string')).to.be.equal(accountA);
});
it('missing value', async function () {
await expectRevert(this.map.getWithMessage(keyB, 'custom error string'), 'custom error string');
});
});
describe('tryGet', function () {
it('existing value', async function () {
expect(await this.map.tryGet(keyA)).to.be.deep.equal({
0: true,
1: accountA,
});
});
it('missing value', async function () {
expect(await this.map.tryGet(keyB)).to.be.deep.equal({
0: false,
1: constants.ZERO_ADDRESS,
});
});
});
});
});

View File

@ -1,31 +0,0 @@
const zip = require('lodash.zip');
const toStringArray = (array) => array.map((i) => i.toString());
async function expectMembersMatch (map, keys, values) {
const stringKeys = toStringArray(keys);
const stringValues = toStringArray(values);
expect(keys.length).to.equal(values.length);
await Promise.all(keys.map(async (key) => expect(await map.contains(key)).to.equal(true)));
expect(await map.length()).to.bignumber.equal(keys.length.toString());
expect(toStringArray(await Promise.all(keys.map((key) => map.get(key))))).to.have.same.members(stringValues);
// to compare key-value pairs, we zip keys and values
// we also stringify pairs because this helper is used for multiple types of maps
expect(
await Promise.all(
[...Array(keys.length).keys()].map(async (index) => {
const { key, value } = await map.at(index);
return [key.toString(), value.toString()];
}),
),
).to.have.same.deep.members(zip(stringKeys, stringValues));
}
module.exports = {
expectMembersMatch,
};