Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
some fixes
  • Loading branch information
RenanSouza2 committed Apr 17, 2023
commit 16ae23b7fd5013b47f15f717735c89138752b580
23 changes: 15 additions & 8 deletions contracts/governance/utils/Votes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ abstract contract Votes is IVotes, Context, EIP712, Nonces {
function _transferVotingUnits(address from, address to, uint256 amount) internal virtual {
if (from == address(0)) {
uint224 latest = _totalCheckpoints.latest();
_totalCheckpoints.push(SafeCast.toUint32(block.timestamp), latest + SafeCast.toUint32(amount));
_totalCheckpoints.push(SafeCast.toUint32(block.number), latest + SafeCast.toUint224(amount));
}
if (to == address(0)) {
uint224 latest = _totalCheckpoints.latest();
_totalCheckpoints.push(SafeCast.toUint32(block.timestamp), latest - SafeCast.toUint32(amount));
_totalCheckpoints.push(SafeCast.toUint32(block.number), latest - SafeCast.toUint224(amount));
}
_moveDelegateVotes(delegates(from), delegates(to), amount);
}
Expand All @@ -155,18 +155,18 @@ abstract contract Votes is IVotes, Context, EIP712, Nonces {
function _moveDelegateVotes(address from, address to, uint256 amount) private {
if (from != to && amount > 0) {
if (from != address(0)) {
uint224 latest = _totalCheckpoints.latest();
uint224 latest = _delegateCheckpoints[from].latest();
(uint256 oldValue, uint256 newValue) = _delegateCheckpoints[from].push(
SafeCast.toUint32(block.timestamp),
latest - SafeCast.toUint32(amount)
SafeCast.toUint32(block.number),
latest - SafeCast.toUint224(amount)
);
emit DelegateVotesChanged(from, oldValue, newValue);
}
if (to != address(0)) {
uint224 latest = _totalCheckpoints.latest();
uint224 latest = _delegateCheckpoints[to].latest();
(uint256 oldValue, uint256 newValue) = _delegateCheckpoints[to].push(
SafeCast.toUint32(block.timestamp),
latest + SafeCast.toUint32(amount)
SafeCast.toUint32(block.number),
latest + SafeCast.toUint224(amount)
);
emit DelegateVotesChanged(to, oldValue, newValue);
}
Expand All @@ -180,6 +180,13 @@ abstract contract Votes is IVotes, Context, EIP712, Nonces {
return SafeCast.toUint32(_delegateCheckpoints[account].length());
}

/**
* @dev Get the `pos`-th checkpoint for `account`.
*/
function _checkpoints(address account, uint32 pos) internal view virtual returns (Checkpoints.Checkpoint224 memory) {
return _delegateCheckpoints[account]._checkpoints[pos];
}

function _add(uint256 a, uint256 b) private pure returns (uint256) {
return a + b;
}
Expand Down
7 changes: 7 additions & 0 deletions contracts/token/ERC20/extensions/ERC20Votes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ abstract contract ERC20Votes is ERC20, Votes {
return _numCheckpoints(account);
}

/**
* @dev Get the `pos`-th checkpoint for `account`.
*/
function checkpoints(address account, uint32 pos) public view virtual returns (Checkpoints.Checkpoint224 memory) {
return _checkpoints(account, pos);
}

/**
* @dev Returns the balance of `account`.
*/
Expand Down
12 changes: 6 additions & 6 deletions test/token/ERC20/extensions/ERC20Votes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ contract('ERC20Votes', function (accounts) {
expect(await this.token.getPastVotes(holder, block - 6)).to.be.bignumber.equal('0');
});

describe('set delegation', function () {
describe.only('set delegation', function () {
describe('call', function () {
it('delegation with balance', async function () {
await this.token.$_mint(holder, supply);
Expand Down Expand Up @@ -231,7 +231,7 @@ contract('ERC20Votes', function (accounts) {
});
});

describe('change delegation', function () {
describe.only('change delegation', function () {
beforeEach(async function () {
await this.token.$_mint(holder, supply);
await this.token.delegate(holder, { from: holder });
Expand Down Expand Up @@ -269,7 +269,7 @@ contract('ERC20Votes', function (accounts) {
});
});

describe('transfers', function () {
describe.only('transfers', function () {
beforeEach(async function () {
await this.token.$_mint(holder, supply);
});
Expand Down Expand Up @@ -360,7 +360,7 @@ contract('ERC20Votes', function (accounts) {
});

// The following tests are a adaptation of https://github.com/compound-finance/compound-protocol/blob/master/tests/Governance/CompTest.js.
describe('Compound test suite', function () {
describe.only('Compound test suite', function () {
beforeEach(async function () {
await this.token.$_mint(holder, supply);
});
Expand Down Expand Up @@ -497,7 +497,7 @@ contract('ERC20Votes', function (accounts) {
});
});

describe('getPastTotalSupply', function () {
describe.only('getPastTotalSupply', function () {
beforeEach(async function () {
await this.token.delegate(holder, { from: holder });
});
Expand Down Expand Up @@ -574,7 +574,7 @@ contract('ERC20Votes', function (accounts) {
});
});

describe('Voting workflow', function () {
describe.only('Voting workflow', function () {
beforeEach(async function () {
this.name = name;
this.votes = this.token;
Expand Down
74 changes: 1 addition & 73 deletions test/utils/Checkpoints.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const { expectRevert, time } = require('@openzeppelin/test-helpers');
const { expectRevert } = require('@openzeppelin/test-helpers');

const { expect } = require('chai');

const { batchInBlock } = require('../helpers/txpool');

const $Checkpoints = artifacts.require('$Checkpoints');

const first = array => (array.length ? array[0] : undefined);
Expand All @@ -14,76 +12,6 @@ contract('Checkpoints', function () {
this.mock = await $Checkpoints.new();
});

describe('History checkpoints', function () {
const latest = (self, ...args) => self.methods['$latest_Checkpoints_Trace224(uint256)'](0, ...args);
const latestCheckpoint = (self, ...args) =>
self.methods['$latestCheckpoint_Checkpoints_Trace224(uint256)'](0, ...args);
const push = (self, ...args) => self.methods['$push(uint256,uint256)'](0, ...args);
const getAtBlock = (self, ...args) => self.methods['$upperLookup(uint256,uint32)'](0, ...args);
const getLength = (self, ...args) => self.methods['$length_Checkpoints_Trace224(uint256)'](0, ...args);

describe('without checkpoints', function () {
it('returns zero as latest value', async function () {
expect(await latest(this.mock)).to.be.bignumber.equal('0');

const ckpt = await latestCheckpoint(this.mock);
expect(ckpt[0]).to.be.equal(false);
expect(ckpt[1]).to.be.bignumber.equal('0');
expect(ckpt[2]).to.be.bignumber.equal('0');
});

it('returns zero as past value', async function () {
await time.advanceBlock();
expect(await getAtBlock(this.mock, (await web3.eth.getBlockNumber()) - 1)).to.be.bignumber.equal('0');
});
});

describe('with checkpoints', function () {
beforeEach('pushing checkpoints', async function () {
this.tx1 = await push(this.mock, 1);
this.tx2 = await push(this.mock, 2);
await time.advanceBlock();
this.tx3 = await push(this.mock, 3);
await time.advanceBlock();
await time.advanceBlock();
});

it('returns latest value', async function () {
expect(await latest(this.mock)).to.be.bignumber.equal('3');

const ckpt = await latestCheckpoint(this.mock);
expect(ckpt[0]).to.be.equal(true);
expect(ckpt[1]).to.be.bignumber.equal(web3.utils.toBN(this.tx3.receipt.blockNumber));
expect(ckpt[2]).to.be.bignumber.equal(web3.utils.toBN('3'));
});

describe(`lookup: getAtBlock`, function () {
it('returns past values', async function () {
expect(await getAtBlock(this.mock, this.tx1.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
expect(await getAtBlock(this.mock, this.tx1.receipt.blockNumber)).to.be.bignumber.equal('1');
expect(await getAtBlock(this.mock, this.tx2.receipt.blockNumber)).to.be.bignumber.equal('2');
// Block with no new checkpoints
expect(await getAtBlock(this.mock, this.tx2.receipt.blockNumber + 1)).to.be.bignumber.equal('2');
expect(await getAtBlock(this.mock, this.tx3.receipt.blockNumber)).to.be.bignumber.equal('3');
expect(await getAtBlock(this.mock, this.tx3.receipt.blockNumber + 1)).to.be.bignumber.equal('3');
});
});

it('multiple checkpoints in the same block', async function () {
const lengthBefore = await getLength(this.mock);

await batchInBlock([
() => push(this.mock, 8, { gas: 100000 }),
() => push(this.mock, 9, { gas: 100000 }),
() => push(this.mock, 10, { gas: 100000 }),
]);

expect(await getLength(this.mock)).to.be.bignumber.equal(lengthBefore.addn(1));
expect(await latest(this.mock)).to.be.bignumber.equal('10');
});
});
});

for (const length of [160, 224]) {
describe(`Trace${length}`, function () {
const latest = (self, ...args) => self.methods[`$latest_Checkpoints_Trace${length}(uint256)`](0, ...args);
Expand Down