Checkpoints.test.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const { expectRevert, time } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { batchInBlock } = require('../helpers/txpool');
  4. const CheckpointsImpl = artifacts.require('CheckpointsImpl');
  5. contract('Checkpoints', function (accounts) {
  6. beforeEach(async function () {
  7. this.checkpoint = await CheckpointsImpl.new();
  8. });
  9. describe('without checkpoints', function () {
  10. it('returns zero as latest value', async function () {
  11. expect(await this.checkpoint.latest()).to.be.bignumber.equal('0');
  12. });
  13. it('returns zero as past value', async function () {
  14. await time.advanceBlock();
  15. expect(await this.checkpoint.getAtBlock(await web3.eth.getBlockNumber() - 1)).to.be.bignumber.equal('0');
  16. });
  17. });
  18. describe('with checkpoints', function () {
  19. beforeEach('pushing checkpoints', async function () {
  20. this.tx1 = await this.checkpoint.push(1);
  21. this.tx2 = await this.checkpoint.push(2);
  22. await time.advanceBlock();
  23. this.tx3 = await this.checkpoint.push(3);
  24. await time.advanceBlock();
  25. await time.advanceBlock();
  26. });
  27. it('returns latest value', async function () {
  28. expect(await this.checkpoint.latest()).to.be.bignumber.equal('3');
  29. });
  30. it('returns past values', async function () {
  31. expect(await this.checkpoint.getAtBlock(this.tx1.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  32. expect(await this.checkpoint.getAtBlock(this.tx1.receipt.blockNumber)).to.be.bignumber.equal('1');
  33. expect(await this.checkpoint.getAtBlock(this.tx2.receipt.blockNumber)).to.be.bignumber.equal('2');
  34. // Block with no new checkpoints
  35. expect(await this.checkpoint.getAtBlock(this.tx2.receipt.blockNumber + 1)).to.be.bignumber.equal('2');
  36. expect(await this.checkpoint.getAtBlock(this.tx3.receipt.blockNumber)).to.be.bignumber.equal('3');
  37. expect(await this.checkpoint.getAtBlock(this.tx3.receipt.blockNumber + 1)).to.be.bignumber.equal('3');
  38. });
  39. it('reverts if block number >= current block', async function () {
  40. await expectRevert(
  41. this.checkpoint.getAtBlock(await web3.eth.getBlockNumber()),
  42. 'Checkpoints: block not yet mined',
  43. );
  44. await expectRevert(
  45. this.checkpoint.getAtBlock(await web3.eth.getBlockNumber() + 1),
  46. 'Checkpoints: block not yet mined',
  47. );
  48. });
  49. it('multiple checkpoints in the same block', async function () {
  50. const lengthBefore = await this.checkpoint.length();
  51. await batchInBlock([
  52. () => this.checkpoint.push(8, { gas: 100000 }),
  53. () => this.checkpoint.push(9, { gas: 100000 }),
  54. () => this.checkpoint.push(10, { gas: 100000 }),
  55. ]);
  56. const lengthAfter = await this.checkpoint.length();
  57. expect(lengthAfter.toNumber()).to.be.equal(lengthBefore.toNumber() + 1);
  58. expect(await this.checkpoint.latest()).to.be.bignumber.equal('10');
  59. });
  60. });
  61. });