Checkpoints.test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const { expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { VALUE_SIZES } = require('../../../scripts/generate/templates/Checkpoints.opts.js');
  4. const $Checkpoints = artifacts.require('$Checkpoints');
  5. // The library name may be 'Checkpoints' or 'CheckpointsUpgradeable'
  6. const libraryName = $Checkpoints._json.contractName.replace(/^\$/, '');
  7. const first = array => (array.length ? array[0] : undefined);
  8. const last = array => (array.length ? array[array.length - 1] : undefined);
  9. contract('Checkpoints', function () {
  10. beforeEach(async function () {
  11. this.mock = await $Checkpoints.new();
  12. });
  13. for (const length of VALUE_SIZES) {
  14. describe(`Trace${length}`, function () {
  15. beforeEach(async function () {
  16. this.methods = {
  17. latest: (...args) => this.mock.methods[`$latest_${libraryName}_Trace${length}(uint256)`](0, ...args),
  18. latestCheckpoint: (...args) =>
  19. this.mock.methods[`$latestCheckpoint_${libraryName}_Trace${length}(uint256)`](0, ...args),
  20. length: (...args) => this.mock.methods[`$length_${libraryName}_Trace${length}(uint256)`](0, ...args),
  21. push: (...args) => this.mock.methods[`$push(uint256,uint${256 - length},uint${length})`](0, ...args),
  22. lowerLookup: (...args) => this.mock.methods[`$lowerLookup(uint256,uint${256 - length})`](0, ...args),
  23. upperLookup: (...args) => this.mock.methods[`$upperLookup(uint256,uint${256 - length})`](0, ...args),
  24. upperLookupRecent: (...args) =>
  25. this.mock.methods[`$upperLookupRecent(uint256,uint${256 - length})`](0, ...args),
  26. };
  27. });
  28. describe('without checkpoints', function () {
  29. it('returns zero as latest value', async function () {
  30. expect(await this.methods.latest()).to.be.bignumber.equal('0');
  31. const ckpt = await this.methods.latestCheckpoint();
  32. expect(ckpt[0]).to.be.equal(false);
  33. expect(ckpt[1]).to.be.bignumber.equal('0');
  34. expect(ckpt[2]).to.be.bignumber.equal('0');
  35. });
  36. it('lookup returns 0', async function () {
  37. expect(await this.methods.lowerLookup(0)).to.be.bignumber.equal('0');
  38. expect(await this.methods.upperLookup(0)).to.be.bignumber.equal('0');
  39. expect(await this.methods.upperLookupRecent(0)).to.be.bignumber.equal('0');
  40. });
  41. });
  42. describe('with checkpoints', function () {
  43. beforeEach('pushing checkpoints', async function () {
  44. this.checkpoints = [
  45. { key: '2', value: '17' },
  46. { key: '3', value: '42' },
  47. { key: '5', value: '101' },
  48. { key: '7', value: '23' },
  49. { key: '11', value: '99' },
  50. ];
  51. for (const { key, value } of this.checkpoints) {
  52. await this.methods.push(key, value);
  53. }
  54. });
  55. it('length', async function () {
  56. expect(await this.methods.length()).to.be.bignumber.equal(this.checkpoints.length.toString());
  57. });
  58. it('returns latest value', async function () {
  59. expect(await this.methods.latest()).to.be.bignumber.equal(last(this.checkpoints).value);
  60. const ckpt = await this.methods.latestCheckpoint();
  61. expect(ckpt[0]).to.be.equal(true);
  62. expect(ckpt[1]).to.be.bignumber.equal(last(this.checkpoints).key);
  63. expect(ckpt[2]).to.be.bignumber.equal(last(this.checkpoints).value);
  64. });
  65. it('cannot push values in the past', async function () {
  66. await expectRevert(this.methods.push(last(this.checkpoints).key - 1, '0'), 'Checkpoint: decreasing keys');
  67. });
  68. it('can update last value', async function () {
  69. const newValue = '42';
  70. // check length before the update
  71. expect(await this.methods.length()).to.be.bignumber.equal(this.checkpoints.length.toString());
  72. // update last key
  73. await this.methods.push(last(this.checkpoints).key, newValue);
  74. expect(await this.methods.latest()).to.be.bignumber.equal(newValue);
  75. // check that length did not change
  76. expect(await this.methods.length()).to.be.bignumber.equal(this.checkpoints.length.toString());
  77. });
  78. it('lower lookup', async function () {
  79. for (let i = 0; i < 14; ++i) {
  80. const value = first(this.checkpoints.filter(x => i <= x.key))?.value || '0';
  81. expect(await this.methods.lowerLookup(i)).to.be.bignumber.equal(value);
  82. }
  83. });
  84. it('upper lookup & upperLookupRecent', async function () {
  85. for (let i = 0; i < 14; ++i) {
  86. const value = last(this.checkpoints.filter(x => i >= x.key))?.value || '0';
  87. expect(await this.methods.upperLookup(i)).to.be.bignumber.equal(value);
  88. expect(await this.methods.upperLookupRecent(i)).to.be.bignumber.equal(value);
  89. }
  90. });
  91. it('upperLookupRecent with more than 5 checkpoints', async function () {
  92. const moreCheckpoints = [
  93. { key: '12', value: '22' },
  94. { key: '13', value: '131' },
  95. { key: '17', value: '45' },
  96. { key: '19', value: '31452' },
  97. { key: '21', value: '0' },
  98. ];
  99. const allCheckpoints = [].concat(this.checkpoints, moreCheckpoints);
  100. for (const { key, value } of moreCheckpoints) {
  101. await this.methods.push(key, value);
  102. }
  103. for (let i = 0; i < 25; ++i) {
  104. const value = last(allCheckpoints.filter(x => i >= x.key))?.value || '0';
  105. expect(await this.methods.upperLookup(i)).to.be.bignumber.equal(value);
  106. expect(await this.methods.upperLookupRecent(i)).to.be.bignumber.equal(value);
  107. }
  108. });
  109. });
  110. });
  111. }
  112. });