Merge branch 'master' into next-v5.0

This commit is contained in:
Francisco Giordano
2023-05-16 00:07:07 -03:00
308 changed files with 21085 additions and 11515 deletions

View File

@ -6,8 +6,9 @@ const { fromRpcSig } = require('ethereumjs-util');
const ethSigUtil = require('eth-sig-util');
const Wallet = require('ethereumjs-wallet').default;
const { EIP712Domain, domainSeparator } = require('../../helpers/eip712');
const { web3 } = require('hardhat');
const { shouldBehaveLikeEIP6372 } = require('./EIP6372.behavior');
const { getDomain, domainType, domainSeparator } = require('../../helpers/eip712');
const { clockFromReceipt } = require('../../helpers/time');
const Delegation = [
{ name: 'delegatee', type: 'address' },
@ -15,9 +16,19 @@ const Delegation = [
{ name: 'expiry', type: 'uint256' },
];
const version = '1';
const buildAndSignDelegation = (contract, message, pk) =>
getDomain(contract)
.then(domain => ({
primaryType: 'Delegation',
types: { EIP712Domain: domainType(domain), Delegation },
domain,
message,
}))
.then(data => fromRpcSig(ethSigUtil.signTypedMessage(pk, { data })));
function shouldBehaveLikeVotes(accounts, tokens, { mode = 'blocknumber', fungible = true }) {
shouldBehaveLikeEIP6372(mode);
function shouldBehaveLikeVotes(accounts, tokens, fungible = true) {
const getWeight = token => web3.utils.toBN(fungible ? token : 1);
describe('run votes workflow', function () {
@ -26,17 +37,10 @@ function shouldBehaveLikeVotes(accounts, tokens, fungible = true) {
});
it('domain separator', async function () {
expect(await this.votes.DOMAIN_SEPARATOR()).to.equal(
await domainSeparator({
name: this.name,
version,
chainId: this.chainId,
verifyingContract: this.votes.address,
}),
);
expect(await this.votes.DOMAIN_SEPARATOR()).to.equal(domainSeparator(await getDomain(this.votes)));
});
describe('delegation', function () {
describe('delegation with signature', function () {
const token = tokens[0];
it('delegation without tokens', async function () {
@ -60,6 +64,8 @@ function shouldBehaveLikeVotes(accounts, tokens, fungible = true) {
expect(await this.votes.delegates(accounts[1])).to.be.equal(ZERO_ADDRESS);
const { receipt } = await this.votes.delegate(accounts[1], { from: accounts[1] });
const timepoint = await clockFromReceipt[mode](receipt);
expectEvent(receipt, 'DelegateChanged', {
delegator: accounts[1],
fromDelegate: ZERO_ADDRESS,
@ -73,9 +79,9 @@ function shouldBehaveLikeVotes(accounts, tokens, fungible = true) {
expect(await this.votes.delegates(accounts[1])).to.be.equal(accounts[1]);
expect(await this.votes.getVotes(accounts[1])).to.be.bignumber.equal(weight);
expect(await this.votes.getPastVotes(accounts[1], receipt.blockNumber - 1)).to.be.bignumber.equal('0');
expect(await this.votes.getPastVotes(accounts[1], timepoint - 1)).to.be.bignumber.equal('0');
await time.advanceBlock();
expect(await this.votes.getPastVotes(accounts[1], receipt.blockNumber)).to.be.bignumber.equal(weight);
expect(await this.votes.getPastVotes(accounts[1], timepoint)).to.be.bignumber.equal(weight);
});
it('delegation update', async function () {
@ -88,6 +94,8 @@ function shouldBehaveLikeVotes(accounts, tokens, fungible = true) {
expect(await this.votes.getVotes(accounts[2])).to.be.bignumber.equal('0');
const { receipt } = await this.votes.delegate(accounts[2], { from: accounts[1] });
const timepoint = await clockFromReceipt[mode](receipt);
expectEvent(receipt, 'DelegateChanged', {
delegator: accounts[1],
fromDelegate: accounts[1],
@ -108,11 +116,11 @@ function shouldBehaveLikeVotes(accounts, tokens, fungible = true) {
expect(await this.votes.getVotes(accounts[1])).to.be.bignumber.equal('0');
expect(await this.votes.getVotes(accounts[2])).to.be.bignumber.equal(weight);
expect(await this.votes.getPastVotes(accounts[1], receipt.blockNumber - 1)).to.be.bignumber.equal(weight);
expect(await this.votes.getPastVotes(accounts[2], receipt.blockNumber - 1)).to.be.bignumber.equal('0');
expect(await this.votes.getPastVotes(accounts[1], timepoint - 1)).to.be.bignumber.equal(weight);
expect(await this.votes.getPastVotes(accounts[2], timepoint - 1)).to.be.bignumber.equal('0');
await time.advanceBlock();
expect(await this.votes.getPastVotes(accounts[1], receipt.blockNumber)).to.be.bignumber.equal('0');
expect(await this.votes.getPastVotes(accounts[2], receipt.blockNumber)).to.be.bignumber.equal(weight);
expect(await this.votes.getPastVotes(accounts[1], timepoint)).to.be.bignumber.equal('0');
expect(await this.votes.getPastVotes(accounts[2], timepoint)).to.be.bignumber.equal(weight);
});
describe('with signature', function () {
@ -121,33 +129,25 @@ function shouldBehaveLikeVotes(accounts, tokens, fungible = true) {
const nonce = 0;
delegator.address = web3.utils.toChecksumAddress(delegator.getAddressString());
const buildData = (chainId, verifyingContract, name, message) => ({
data: {
primaryType: 'Delegation',
types: { EIP712Domain, Delegation },
domain: { name, version, chainId, verifyingContract },
message,
},
});
it('accept signed delegation', async function () {
await this.votes.$_mint(delegator.address, token);
const weight = getWeight(token);
const { v, r, s } = fromRpcSig(
ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.votes.address, this.name, {
delegatee,
nonce,
expiry: MAX_UINT256,
}),
),
const { v, r, s } = await buildAndSignDelegation(
this.votes,
{
delegatee,
nonce,
expiry: MAX_UINT256,
},
delegator.getPrivateKey(),
);
expect(await this.votes.delegates(delegator.address)).to.be.equal(ZERO_ADDRESS);
const { receipt } = await this.votes.delegateBySig(delegatee, nonce, MAX_UINT256, v, r, s);
const timepoint = await clockFromReceipt[mode](receipt);
expectEvent(receipt, 'DelegateChanged', {
delegator: delegator.address,
fromDelegate: ZERO_ADDRESS,
@ -162,21 +162,20 @@ function shouldBehaveLikeVotes(accounts, tokens, fungible = true) {
expect(await this.votes.delegates(delegator.address)).to.be.equal(delegatee);
expect(await this.votes.getVotes(delegator.address)).to.be.bignumber.equal('0');
expect(await this.votes.getVotes(delegatee)).to.be.bignumber.equal(weight);
expect(await this.votes.getPastVotes(delegatee, receipt.blockNumber - 1)).to.be.bignumber.equal('0');
expect(await this.votes.getPastVotes(delegatee, timepoint - 1)).to.be.bignumber.equal('0');
await time.advanceBlock();
expect(await this.votes.getPastVotes(delegatee, receipt.blockNumber)).to.be.bignumber.equal(weight);
expect(await this.votes.getPastVotes(delegatee, timepoint)).to.be.bignumber.equal(weight);
});
it('rejects reused signature', async function () {
const { v, r, s } = fromRpcSig(
ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.votes.address, this.name, {
delegatee,
nonce,
expiry: MAX_UINT256,
}),
),
const { v, r, s } = await buildAndSignDelegation(
this.votes,
{
delegatee,
nonce,
expiry: MAX_UINT256,
},
delegator.getPrivateKey(),
);
await this.votes.delegateBySig(delegatee, nonce, MAX_UINT256, v, r, s);
@ -185,15 +184,14 @@ function shouldBehaveLikeVotes(accounts, tokens, fungible = true) {
});
it('rejects bad delegatee', async function () {
const { v, r, s } = fromRpcSig(
ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.votes.address, this.name, {
delegatee,
nonce,
expiry: MAX_UINT256,
}),
),
const { v, r, s } = await buildAndSignDelegation(
this.votes,
{
delegatee,
nonce,
expiry: MAX_UINT256,
},
delegator.getPrivateKey(),
);
const receipt = await this.votes.delegateBySig(other, nonce, MAX_UINT256, v, r, s);
@ -204,16 +202,16 @@ function shouldBehaveLikeVotes(accounts, tokens, fungible = true) {
});
it('rejects bad nonce', async function () {
const { v, r, s } = fromRpcSig(
ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.votes.address, this.name, {
delegatee,
nonce: nonce + 1,
expiry: MAX_UINT256,
}),
),
const { v, r, s } = await buildAndSignDelegation(
this.votes,
{
delegatee,
nonce: nonce + 1,
expiry: MAX_UINT256,
},
delegator.getPrivateKey(),
);
await expectRevert(
this.votes.delegateBySig(delegatee, nonce + 1, MAX_UINT256, v, r, s),
'Votes: invalid nonce',
@ -222,15 +220,14 @@ function shouldBehaveLikeVotes(accounts, tokens, fungible = true) {
it('rejects expired permit', async function () {
const expiry = (await time.latest()) - time.duration.weeks(1);
const { v, r, s } = fromRpcSig(
ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.votes.address, this.name, {
delegatee,
nonce,
expiry,
}),
),
const { v, r, s } = await buildAndSignDelegation(
this.votes,
{
delegatee,
nonce,
expiry,
},
delegator.getPrivateKey(),
);
await expectRevert(this.votes.delegateBySig(delegatee, nonce, expiry, v, r, s), 'Votes: signature expired');
@ -244,7 +241,7 @@ function shouldBehaveLikeVotes(accounts, tokens, fungible = true) {
});
it('reverts if block number >= current block', async function () {
await expectRevert(this.votes.getPastTotalSupply(5e10), 'block not yet mined');
await expectRevert(this.votes.getPastTotalSupply(5e10), 'future lookup');
});
it('returns 0 if there are no checkpoints', async function () {
@ -252,36 +249,47 @@ function shouldBehaveLikeVotes(accounts, tokens, fungible = true) {
});
it('returns the correct checkpointed total supply', async function () {
const blockNumber = Number(await time.latestBlock());
const weight = tokens.map(token => getWeight(token));
await this.votes.$_mint(accounts[1], tokens[0]); // mint 0
// t0 = mint #0
const t0 = await this.votes.$_mint(accounts[1], tokens[0]);
await time.advanceBlock();
await this.votes.$_mint(accounts[1], tokens[1]); // mint 1
// t1 = mint #1
const t1 = await this.votes.$_mint(accounts[1], tokens[1]);
await time.advanceBlock();
await this.votes.$_burn(...(fungible ? [accounts[1]] : []), tokens[1]); // burn 1
// t2 = burn #1
const t2 = await this.votes.$_burn(...(fungible ? [accounts[1]] : []), tokens[1]);
await time.advanceBlock();
await this.votes.$_mint(accounts[1], tokens[2]); // mint 2
// t3 = mint #2
const t3 = await this.votes.$_mint(accounts[1], tokens[2]);
await time.advanceBlock();
await this.votes.$_burn(...(fungible ? [accounts[1]] : []), tokens[0]); // burn 0
// t4 = burn #0
const t4 = await this.votes.$_burn(...(fungible ? [accounts[1]] : []), tokens[0]);
await time.advanceBlock();
await this.votes.$_burn(...(fungible ? [accounts[1]] : []), tokens[2]); // burn 2
// t5 = burn #2
const t5 = await this.votes.$_burn(...(fungible ? [accounts[1]] : []), tokens[2]);
await time.advanceBlock();
const weight = tokens.map(getWeight);
t0.timepoint = await clockFromReceipt[mode](t0.receipt);
t1.timepoint = await clockFromReceipt[mode](t1.receipt);
t2.timepoint = await clockFromReceipt[mode](t2.receipt);
t3.timepoint = await clockFromReceipt[mode](t3.receipt);
t4.timepoint = await clockFromReceipt[mode](t4.receipt);
t5.timepoint = await clockFromReceipt[mode](t5.receipt);
expect(await this.votes.getPastTotalSupply(blockNumber)).to.be.bignumber.equal('0');
expect(await this.votes.getPastTotalSupply(blockNumber + 1)).to.be.bignumber.equal(weight[0]);
expect(await this.votes.getPastTotalSupply(blockNumber + 2)).to.be.bignumber.equal(weight[0]);
expect(await this.votes.getPastTotalSupply(blockNumber + 3)).to.be.bignumber.equal(weight[0].add(weight[1]));
expect(await this.votes.getPastTotalSupply(blockNumber + 4)).to.be.bignumber.equal(weight[0].add(weight[1]));
expect(await this.votes.getPastTotalSupply(blockNumber + 5)).to.be.bignumber.equal(weight[0]);
expect(await this.votes.getPastTotalSupply(blockNumber + 6)).to.be.bignumber.equal(weight[0]);
expect(await this.votes.getPastTotalSupply(blockNumber + 7)).to.be.bignumber.equal(weight[0].add(weight[2]));
expect(await this.votes.getPastTotalSupply(blockNumber + 8)).to.be.bignumber.equal(weight[0].add(weight[2]));
expect(await this.votes.getPastTotalSupply(blockNumber + 9)).to.be.bignumber.equal(weight[2]);
expect(await this.votes.getPastTotalSupply(blockNumber + 10)).to.be.bignumber.equal(weight[2]);
expect(await this.votes.getPastTotalSupply(blockNumber + 11)).to.be.bignumber.equal('0');
await expectRevert(this.votes.getPastTotalSupply(blockNumber + 12), 'Checkpoints: block not yet mined');
expect(await this.votes.getPastTotalSupply(t0.timepoint - 1)).to.be.bignumber.equal('0');
expect(await this.votes.getPastTotalSupply(t0.timepoint)).to.be.bignumber.equal(weight[0]);
expect(await this.votes.getPastTotalSupply(t0.timepoint + 1)).to.be.bignumber.equal(weight[0]);
expect(await this.votes.getPastTotalSupply(t1.timepoint)).to.be.bignumber.equal(weight[0].add(weight[1]));
expect(await this.votes.getPastTotalSupply(t1.timepoint + 1)).to.be.bignumber.equal(weight[0].add(weight[1]));
expect(await this.votes.getPastTotalSupply(t2.timepoint)).to.be.bignumber.equal(weight[0]);
expect(await this.votes.getPastTotalSupply(t2.timepoint + 1)).to.be.bignumber.equal(weight[0]);
expect(await this.votes.getPastTotalSupply(t3.timepoint)).to.be.bignumber.equal(weight[0].add(weight[2]));
expect(await this.votes.getPastTotalSupply(t3.timepoint + 1)).to.be.bignumber.equal(weight[0].add(weight[2]));
expect(await this.votes.getPastTotalSupply(t4.timepoint)).to.be.bignumber.equal(weight[2]);
expect(await this.votes.getPastTotalSupply(t4.timepoint + 1)).to.be.bignumber.equal(weight[2]);
expect(await this.votes.getPastTotalSupply(t5.timepoint)).to.be.bignumber.equal('0');
await expectRevert(this.votes.getPastTotalSupply(t5.timepoint + 1), 'Votes: future lookup');
});
});
@ -296,7 +304,7 @@ function shouldBehaveLikeVotes(accounts, tokens, fungible = true) {
describe('getPastVotes', function () {
it('reverts if block number >= current block', async function () {
await expectRevert(this.votes.getPastVotes(accounts[2], 5e10), 'block not yet mined');
await expectRevert(this.votes.getPastVotes(accounts[2], 5e10), 'future lookup');
});
it('returns 0 if there are no checkpoints', async function () {
@ -304,21 +312,24 @@ function shouldBehaveLikeVotes(accounts, tokens, fungible = true) {
});
it('returns the latest block if >= last checkpoint block', async function () {
const tx = await this.votes.delegate(accounts[2], { from: accounts[1] });
const { receipt } = await this.votes.delegate(accounts[2], { from: accounts[1] });
const timepoint = await clockFromReceipt[mode](receipt);
await time.advanceBlock();
await time.advanceBlock();
const latest = await this.votes.getVotes(accounts[2]);
expect(await this.votes.getPastVotes(accounts[2], tx.receipt.blockNumber)).to.be.bignumber.equal(latest);
expect(await this.votes.getPastVotes(accounts[2], tx.receipt.blockNumber + 1)).to.be.bignumber.equal(latest);
expect(await this.votes.getPastVotes(accounts[2], timepoint)).to.be.bignumber.equal(latest);
expect(await this.votes.getPastVotes(accounts[2], timepoint + 1)).to.be.bignumber.equal(latest);
});
it('returns zero if < first checkpoint block', async function () {
await time.advanceBlock();
const tx = await this.votes.delegate(accounts[2], { from: accounts[1] });
const { receipt } = await this.votes.delegate(accounts[2], { from: accounts[1] });
const timepoint = await clockFromReceipt[mode](receipt);
await time.advanceBlock();
await time.advanceBlock();
expect(await this.votes.getPastVotes(accounts[2], tx.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
expect(await this.votes.getPastVotes(accounts[2], timepoint - 1)).to.be.bignumber.equal('0');
});
});
});