Checkpoints.test.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const { expectRevert, time } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { batchInBlock } = require('../helpers/txpool');
  4. const CheckpointsMock = artifacts.require('CheckpointsMock');
  5. const first = (array) => array.length ? array[0] : undefined;
  6. const last = (array) => array.length ? array[array.length - 1] : undefined;
  7. contract('Checkpoints', function (accounts) {
  8. describe('History checkpoints', function () {
  9. beforeEach(async function () {
  10. this.checkpoint = await CheckpointsMock.new();
  11. });
  12. describe('without checkpoints', function () {
  13. it('returns zero as latest value', async function () {
  14. expect(await this.checkpoint.latest()).to.be.bignumber.equal('0');
  15. });
  16. it('returns zero as past value', async function () {
  17. await time.advanceBlock();
  18. expect(await this.checkpoint.getAtBlock(await web3.eth.getBlockNumber() - 1)).to.be.bignumber.equal('0');
  19. expect(await this.checkpoint.getAtRecentBlock(await web3.eth.getBlockNumber() - 1)).to.be.bignumber.equal('0');
  20. });
  21. });
  22. describe('with checkpoints', function () {
  23. beforeEach('pushing checkpoints', async function () {
  24. this.tx1 = await this.checkpoint.push(1);
  25. this.tx2 = await this.checkpoint.push(2);
  26. await time.advanceBlock();
  27. this.tx3 = await this.checkpoint.push(3);
  28. await time.advanceBlock();
  29. await time.advanceBlock();
  30. });
  31. it('returns latest value', async function () {
  32. expect(await this.checkpoint.latest()).to.be.bignumber.equal('3');
  33. });
  34. for (const fn of [ 'getAtBlock(uint256)', 'getAtRecentBlock(uint256)' ]) {
  35. describe(`lookup: ${fn}`, function () {
  36. it('returns past values', async function () {
  37. expect(await this.checkpoint.methods[fn](this.tx1.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  38. expect(await this.checkpoint.methods[fn](this.tx1.receipt.blockNumber)).to.be.bignumber.equal('1');
  39. expect(await this.checkpoint.methods[fn](this.tx2.receipt.blockNumber)).to.be.bignumber.equal('2');
  40. // Block with no new checkpoints
  41. expect(await this.checkpoint.methods[fn](this.tx2.receipt.blockNumber + 1)).to.be.bignumber.equal('2');
  42. expect(await this.checkpoint.methods[fn](this.tx3.receipt.blockNumber)).to.be.bignumber.equal('3');
  43. expect(await this.checkpoint.methods[fn](this.tx3.receipt.blockNumber + 1)).to.be.bignumber.equal('3');
  44. });
  45. it('reverts if block number >= current block', async function () {
  46. await expectRevert(
  47. this.checkpoint.methods[fn](await web3.eth.getBlockNumber()),
  48. 'Checkpoints: block not yet mined',
  49. );
  50. await expectRevert(
  51. this.checkpoint.methods[fn](await web3.eth.getBlockNumber() + 1),
  52. 'Checkpoints: block not yet mined',
  53. );
  54. });
  55. });
  56. }
  57. it('multiple checkpoints in the same block', async function () {
  58. const lengthBefore = await this.checkpoint.length();
  59. await batchInBlock([
  60. () => this.checkpoint.push(8, { gas: 100000 }),
  61. () => this.checkpoint.push(9, { gas: 100000 }),
  62. () => this.checkpoint.push(10, { gas: 100000 }),
  63. ]);
  64. expect(await this.checkpoint.length()).to.be.bignumber.equal(lengthBefore.addn(1));
  65. expect(await this.checkpoint.latest()).to.be.bignumber.equal('10');
  66. });
  67. });
  68. });
  69. for (const length of [160, 224]) {
  70. describe(`Trace${length}`, function () {
  71. beforeEach(async function () {
  72. this.contract = await artifacts.require(`Checkpoints${length}Mock`).new();
  73. });
  74. describe('without checkpoints', function () {
  75. it('returns zero as latest value', async function () {
  76. expect(await this.contract.latest()).to.be.bignumber.equal('0');
  77. });
  78. it('lookup returns 0', async function () {
  79. expect(await this.contract.lowerLookup(0)).to.be.bignumber.equal('0');
  80. expect(await this.contract.upperLookup(0)).to.be.bignumber.equal('0');
  81. expect(await this.contract.upperLookupRecent(0)).to.be.bignumber.equal('0');
  82. });
  83. });
  84. describe('with checkpoints', function () {
  85. beforeEach('pushing checkpoints', async function () {
  86. this.checkpoints = [
  87. { key: 2, value: '17' },
  88. { key: 3, value: '42' },
  89. { key: 5, value: '101' },
  90. { key: 7, value: '23' },
  91. { key: 11, value: '99' },
  92. ];
  93. for (const { key, value } of this.checkpoints) {
  94. await this.contract.push(key, value);
  95. }
  96. });
  97. it('returns latest value', async function () {
  98. expect(await this.contract.latest())
  99. .to.be.bignumber.equal(last(this.checkpoints).value);
  100. });
  101. it('cannot push values in the past', async function () {
  102. await expectRevert(this.contract.push(last(this.checkpoints).key - 1, '0'), 'Checkpoint: invalid key');
  103. });
  104. it('can update last value', async function () {
  105. const newValue = '42';
  106. // check length before the update
  107. expect(await this.contract.length()).to.be.bignumber.equal(this.checkpoints.length.toString());
  108. // update last key
  109. await this.contract.push(last(this.checkpoints).key, newValue);
  110. expect(await this.contract.latest()).to.be.bignumber.equal(newValue);
  111. // check that length did not change
  112. expect(await this.contract.length()).to.be.bignumber.equal(this.checkpoints.length.toString());
  113. });
  114. it('lower lookup', async function () {
  115. for (let i = 0; i < 14; ++i) {
  116. const value = first(this.checkpoints.filter(x => i <= x.key))?.value || '0';
  117. expect(await this.contract.lowerLookup(i)).to.be.bignumber.equal(value);
  118. }
  119. });
  120. it('upper lookup', async function () {
  121. for (let i = 0; i < 14; ++i) {
  122. const value = last(this.checkpoints.filter(x => i >= x.key))?.value || '0';
  123. expect(await this.contract.upperLookup(i)).to.be.bignumber.equal(value);
  124. expect(await this.contract.upperLookupRecent(i)).to.be.bignumber.equal(value);
  125. }
  126. });
  127. });
  128. });
  129. }
  130. });