Blockhash.test.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const { ethers, predeploy } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture, mineUpTo, setCode } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { impersonate } = require('../helpers/account');
  5. const SYSTEM_ADDRESS = '0xfffffffffffffffffffffffffffffffffffffffe';
  6. const HISTORY_SERVE_WINDOW = 8191;
  7. const BLOCKHASH_SERVE_WINDOW = 256;
  8. async function fixture() {
  9. return {
  10. mock: await ethers.deployContract('$Blockhash'),
  11. systemSigner: await impersonate(SYSTEM_ADDRESS),
  12. latestBlock: await ethers.provider.getBlock('latest'),
  13. };
  14. }
  15. describe('Blockhash', function () {
  16. beforeEach(async function () {
  17. Object.assign(this, await loadFixture(fixture));
  18. });
  19. for (const supported of [true, false]) {
  20. describe(`${supported ? 'supported' : 'unsupported'} chain`, function () {
  21. beforeEach(async function () {
  22. if (supported) {
  23. await this.systemSigner.sendTransaction({ to: predeploy.eip2935, data: this.latestBlock.hash });
  24. } else {
  25. await setCode(predeploy.eip2935.target, '0x');
  26. }
  27. });
  28. it('recent block', async function () {
  29. // fast forward (less than blockhash serve window)
  30. await mineUpTo(this.latestBlock.number + BLOCKHASH_SERVE_WINDOW);
  31. await expect(this.mock.$blockHash(this.latestBlock.number)).to.eventually.equal(this.latestBlock.hash);
  32. });
  33. it('old block', async function () {
  34. // fast forward (more than blockhash serve window)
  35. await mineUpTo(this.latestBlock.number + BLOCKHASH_SERVE_WINDOW + 1);
  36. await expect(this.mock.$blockHash(this.latestBlock.number)).to.eventually.equal(
  37. supported ? this.latestBlock.hash : ethers.ZeroHash,
  38. );
  39. });
  40. it('very old block', async function () {
  41. // fast forward (more than history serve window)
  42. await mineUpTo(this.latestBlock.number + HISTORY_SERVE_WINDOW + 10);
  43. await expect(this.mock.$blockHash(this.latestBlock.number)).to.eventually.equal(ethers.ZeroHash);
  44. });
  45. it('future block', async function () {
  46. // check history access in the future
  47. await expect(this.mock.$blockHash(this.latestBlock.number + 10)).to.eventually.equal(ethers.ZeroHash);
  48. });
  49. });
  50. }
  51. });