|
| 1 | +const { ethers } = require('hardhat'); |
| 2 | +const { expect } = require('chai'); |
| 3 | + |
| 4 | +function shouldBehaveLikeNonces() { |
| 5 | + describe('should behave like Nonces', function () { |
| 6 | + const sender = ethers.Wallet.createRandom(); |
| 7 | + const other = ethers.Wallet.createRandom(); |
| 8 | + |
| 9 | + it('gets a nonce', async function () { |
| 10 | + expect(this.mock.nonces(sender)).to.eventually.equal(0n); |
| 11 | + }); |
| 12 | + |
| 13 | + describe('_useNonce', function () { |
| 14 | + it('increments a nonce', async function () { |
| 15 | + expect(this.mock.nonces(sender)).to.eventually.equal(0n); |
| 16 | + |
| 17 | + const eventName = ['return$_useNonce', 'return$_useNonce_address'].find(name => |
| 18 | + this.mock.interface.getEvent(name), |
| 19 | + ); |
| 20 | + |
| 21 | + await expect(this.mock.$_useNonce(sender)).to.emit(this.mock, eventName).withArgs(0n); |
| 22 | + |
| 23 | + expect(this.mock.nonces(sender)).to.eventually.equal(1n); |
| 24 | + }); |
| 25 | + |
| 26 | + it("increments only sender's nonce", async function () { |
| 27 | + expect(this.mock.nonces(sender)).to.eventually.equal(0n); |
| 28 | + expect(this.mock.nonces(other)).to.eventually.equal(0n); |
| 29 | + |
| 30 | + await this.mock.$_useNonce(sender); |
| 31 | + |
| 32 | + expect(this.mock.nonces(sender)).to.eventually.equal(1n); |
| 33 | + expect(this.mock.nonces(other)).to.eventually.equal(0n); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('_useCheckedNonce', function () { |
| 38 | + it('increments a nonce', async function () { |
| 39 | + // current nonce is 0n |
| 40 | + expect(this.mock.nonces(sender)).to.eventually.equal(0n); |
| 41 | + |
| 42 | + await this.mock.$_useCheckedNonce(sender, 0n); |
| 43 | + |
| 44 | + expect(this.mock.nonces(sender)).to.eventually.equal(1n); |
| 45 | + }); |
| 46 | + |
| 47 | + it("increments only sender's nonce", async function () { |
| 48 | + // current nonce is 0n |
| 49 | + expect(this.mock.nonces(sender)).to.eventually.equal(0n); |
| 50 | + expect(this.mock.nonces(other)).to.eventually.equal(0n); |
| 51 | + |
| 52 | + await this.mock.$_useCheckedNonce(sender, 0n); |
| 53 | + |
| 54 | + expect(this.mock.nonces(sender)).to.eventually.equal(1n); |
| 55 | + expect(this.mock.nonces(other)).to.eventually.equal(0n); |
| 56 | + }); |
| 57 | + |
| 58 | + it('reverts when nonce is not the expected', async function () { |
| 59 | + const currentNonce = await this.mock.nonces(sender); |
| 60 | + |
| 61 | + await expect(this.mock.$_useCheckedNonce(sender, currentNonce + 1n)) |
| 62 | + .to.be.revertedWithCustomError(this.mock, 'InvalidAccountNonce') |
| 63 | + .withArgs(sender, currentNonce); |
| 64 | + }); |
| 65 | + }); |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +function shouldBehaveLikeNoncesKeyed() { |
| 70 | + describe('should support nonces with keys', function () { |
| 71 | + const sender = ethers.Wallet.createRandom(); |
| 72 | + |
| 73 | + const keyOffset = key => key << 64n; |
| 74 | + |
| 75 | + it('gets a nonce', async function () { |
| 76 | + expect(this.mock.nonces(sender, ethers.Typed.uint192(0n))).to.eventually.equal(keyOffset(0n) + 0n); |
| 77 | + expect(this.mock.nonces(sender, ethers.Typed.uint192(17n))).to.eventually.equal(keyOffset(17n) + 0n); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('_useNonce', function () { |
| 81 | + it('default variant uses key 0', async function () { |
| 82 | + expect(this.mock.nonces(sender, ethers.Typed.uint192(0n))).to.eventually.equal(keyOffset(0n) + 0n); |
| 83 | + expect(this.mock.nonces(sender, ethers.Typed.uint192(17n))).to.eventually.equal(keyOffset(17n) + 0n); |
| 84 | + |
| 85 | + await expect(this.mock.$_useNonce(sender)).to.emit(this.mock, 'return$_useNonce_address').withArgs(0n); |
| 86 | + |
| 87 | + await expect(this.mock.$_useNonce(sender, ethers.Typed.uint192(0n))) |
| 88 | + .to.emit(this.mock, 'return$_useNonce_address_uint192') |
| 89 | + .withArgs(1n); |
| 90 | + |
| 91 | + expect(this.mock.nonces(sender, ethers.Typed.uint192(0n))).to.eventually.equal(keyOffset(0n) + 2n); |
| 92 | + expect(this.mock.nonces(sender, ethers.Typed.uint192(17n))).to.eventually.equal(keyOffset(17n) + 0n); |
| 93 | + }); |
| 94 | + |
| 95 | + it('use nonce at another key', async function () { |
| 96 | + expect(this.mock.nonces(sender, ethers.Typed.uint192(0n))).to.eventually.equal(keyOffset(0n) + 0n); |
| 97 | + expect(this.mock.nonces(sender, ethers.Typed.uint192(17n))).to.eventually.equal(keyOffset(17n) + 0n); |
| 98 | + |
| 99 | + await expect(this.mock.$_useNonce(sender, ethers.Typed.uint192(17n))) |
| 100 | + .to.emit(this.mock, 'return$_useNonce_address_uint192') |
| 101 | + .withArgs(0n); |
| 102 | + |
| 103 | + await expect(this.mock.$_useNonce(sender, ethers.Typed.uint192(17n))) |
| 104 | + .to.emit(this.mock, 'return$_useNonce_address_uint192') |
| 105 | + .withArgs(1n); |
| 106 | + |
| 107 | + expect(this.mock.nonces(sender, ethers.Typed.uint192(0n))).to.eventually.equal(keyOffset(0n) + 0n); |
| 108 | + expect(this.mock.nonces(sender, ethers.Typed.uint192(17n))).to.eventually.equal(keyOffset(17n) + 2n); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('_useCheckedNonce', function () { |
| 113 | + it('default variant uses key 0', async function () { |
| 114 | + const currentNonce = await this.mock.nonces(sender, ethers.Typed.uint192(0n)); |
| 115 | + |
| 116 | + await this.mock.$_useCheckedNonce(sender, currentNonce); |
| 117 | + |
| 118 | + expect(this.mock.nonces(sender, ethers.Typed.uint192(0n))).to.eventually.equal(currentNonce + 1n); |
| 119 | + }); |
| 120 | + |
| 121 | + it('use nonce at another key', async function () { |
| 122 | + const currentNonce = await this.mock.nonces(sender, ethers.Typed.uint192(17n)); |
| 123 | + |
| 124 | + await this.mock.$_useCheckedNonce(sender, currentNonce); |
| 125 | + |
| 126 | + expect(this.mock.nonces(sender, ethers.Typed.uint192(17n))).to.eventually.equal(currentNonce + 1n); |
| 127 | + }); |
| 128 | + |
| 129 | + it('reverts when nonce is not the expected', async function () { |
| 130 | + const currentNonce = await this.mock.nonces(sender, ethers.Typed.uint192(42n)); |
| 131 | + |
| 132 | + // use and increment |
| 133 | + await this.mock.$_useCheckedNonce(sender, currentNonce); |
| 134 | + |
| 135 | + // reuse same nonce |
| 136 | + await expect(this.mock.$_useCheckedNonce(sender, currentNonce)) |
| 137 | + .to.be.revertedWithCustomError(this.mock, 'InvalidAccountNonce') |
| 138 | + .withArgs(sender, 1); |
| 139 | + |
| 140 | + // use "future" nonce too early |
| 141 | + await expect(this.mock.$_useCheckedNonce(sender, currentNonce + 10n)) |
| 142 | + .to.be.revertedWithCustomError(this.mock, 'InvalidAccountNonce') |
| 143 | + .withArgs(sender, 1); |
| 144 | + }); |
| 145 | + }); |
| 146 | + }); |
| 147 | +} |
| 148 | + |
| 149 | +module.exports = { |
| 150 | + shouldBehaveLikeNonces, |
| 151 | + shouldBehaveLikeNoncesKeyed, |
| 152 | +}; |
0 commit comments