Checkpoints.test.js 2.3 KB

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