From c3358184de00cfa1a08b0a72aaec8202170385e8 Mon Sep 17 00:00:00 2001 From: Ricardo Guilherme Schmidt <3esmit@gmail.com> Date: Tue, 26 Sep 2023 02:41:52 -0300 Subject: [PATCH] add TestSnapshotReads --- .gas-report | 24 +++--- .gas-snapshot | 2 + test/MiniMeToken.t.sol | 163 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 12 deletions(-) diff --git a/.gas-report b/.gas-report index cc5dc93..b239e46 100644 --- a/.gas-report +++ b/.gas-report @@ -1,28 +1,28 @@ | contracts/MiniMeToken.sol:MiniMeToken contract | | | | | | |------------------------------------------------|-----------------|--------|--------|---------|---------| | Deployment Cost | Deployment Size | | | | | -| 1804309 | 10009 | | | | | +| 1711400 | 10009 | | | | | | Function Name | min | avg | median | max | # calls | | allowance | 0 | 62 | 0 | 808 | 13 | | approve | 0 | 15018 | 14715 | 31708 | 10 | | approveAndCall | 0 | 31201 | 0 | 93603 | 3 | -| balanceOf | 0 | 264 | 0 | 2753 | 55 | -| balanceOfAt | 0 | 182 | 0 | 2363 | 30 | +| balanceOf | 0 | 406 | 0 | 2753 | 59 | +| balanceOfAt | 0 | 1308 | 377 | 4027 | 54 | | changeController | 0 | 507 | 0 | 3558 | 10 | | claimTokens | 9537 | 41281 | 57154 | 57154 | 3 | -| controller | 0 | 0 | 0 | 0 | 8 | +| controller | 0 | 0 | 0 | 0 | 9 | | createCloneToken | 0 | 924533 | 924533 | 1849066 | 2 | -| decimals | 0 | 0 | 0 | 0 | 8 | +| decimals | 0 | 0 | 0 | 0 | 9 | | destroyTokens | 2308 | 5206 | 4310 | 9001 | 3 | | enableTransfers | 0 | 0 | 0 | 0 | 3 | -| generateTokens | 0 | 7009 | 0 | 95808 | 41 | -| name | 0 | 0 | 0 | 0 | 8 | -| parentSnapShotBlock | 0 | 0 | 0 | 0 | 9 | -| parentToken | 0 | 0 | 0 | 0 | 9 | +| generateTokens | 0 | 16254 | 0 | 95808 | 54 | +| name | 0 | 0 | 0 | 0 | 9 | +| parentSnapShotBlock | 0 | 0 | 0 | 0 | 10 | +| parentToken | 0 | 0 | 0 | 0 | 10 | | receive | 7960 | 7979 | 7979 | 7998 | 2 | -| symbol | 0 | 0 | 0 | 0 | 8 | -| totalSupply | 0 | 212 | 0 | 1911 | 9 | -| totalSupplyAt | 0 | 285 | 0 | 1995 | 7 | +| symbol | 0 | 0 | 0 | 0 | 9 | +| totalSupply | 0 | 201 | 0 | 1911 | 19 | +| totalSupplyAt | 0 | 1349 | 0 | 3659 | 11 | | transfer | 526 | 39282 | 50395 | 93084 | 20 | | transferFrom | 0 | 16837 | 3495 | 66596 | 7 | diff --git a/.gas-snapshot b/.gas-snapshot index 4da22be..a8b42a2 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -27,6 +27,8 @@ ReceiveTest:testAcceptingEther() (gas: 18628) ReceiveTest:testDeployment() (gas: 26595) ReceiveTest:testRejectingEther() (gas: 18691) ReentrancyTest:testAttack() (gas: 229394) +TestSnapshotReads:testDeployment() (gas: 26550) +TestSnapshotReads:testSnapshotReads() (gas: 747396) TransferTest:testDeployment() (gas: 26617) TransferTest:testDoubleTransfer() (gas: 92425) TransferTest:testDoubleTransfer2() (gas: 70578) diff --git a/test/MiniMeToken.t.sol b/test/MiniMeToken.t.sol index 4b2f162..02f35cc 100644 --- a/test/MiniMeToken.t.sol +++ b/test/MiniMeToken.t.sol @@ -54,6 +54,7 @@ contract MiniMeTokenTest is Test { assertEq(minimeToken.controller(), deployer, "controller should be correct"); assertEq(address(minimeToken.parentToken()), parentToken, "parent token should be correct"); assertEq(minimeToken.parentSnapShotBlock(), parentSnapShotBlock, "parent snapshot block should be correct"); + assertEq(minimeToken.totalSupply(), 0, "total supply should be correct"); vm.resumeGasMetering(); } @@ -914,3 +915,165 @@ contract ClaimTokensTest is MiniMeTokenTest { vm.resumeGasMetering(); } } + +contract TestSnapshotReads is MiniMeTokenTest { + function setUp() public virtual override { + MiniMeTokenTest.setUp(); + } + + function testSnapshotReads() public { + _generateTokens(accounts[0], 10); + _generateTokens(accounts[1], 5); + _generateTokens(accounts[2], 3); + _generateTokens(accounts[3], 1); + + uint256 currentBlock = block.number; + uint256 nextBlock = currentBlock + 1; + uint256 secondNextBlock = currentBlock + 2; + uint256 thirdNextBlock = currentBlock + 3; + + vm.roll(nextBlock); + _generateTokens(accounts[0], 2); + _generateTokens(accounts[1], 1); + _generateTokens(accounts[2], 1); + _generateTokens(accounts[3], 1); + + vm.roll(secondNextBlock); + _generateTokens(accounts[0], 1); + _generateTokens(accounts[1], 1); + _generateTokens(accounts[2], 1); + + vm.roll(thirdNextBlock); + _generateTokens(accounts[0], 1); + _generateTokens(accounts[1], 1); + + assertEq(minimeToken.totalSupply(), 29, "total supply should be correct"); + assertEq(minimeToken.balanceOf(accounts[0]), 14, "balance of account 0 should be correct"); + assertEq(minimeToken.balanceOf(accounts[1]), 8, "balance of account 1 should be correct"); + assertEq(minimeToken.balanceOf(accounts[2]), 5, "balance of account 2 should be correct"); + assertEq(minimeToken.balanceOf(accounts[3]), 2, "balance of account 3 should be correct"); + + assertEq(minimeToken.totalSupplyAt(currentBlock), 19, "total supply at current block should be correct"); + assertEq(minimeToken.totalSupplyAt(nextBlock), 24, "total supply at next block should be correct"); + assertEq(minimeToken.totalSupplyAt(secondNextBlock), 27, "total supply at second next block should be correct"); + assertEq(minimeToken.totalSupplyAt(thirdNextBlock), 29, "total supply at third next block should be correct"); + + assertEq( + minimeToken.balanceOfAt(accounts[0], currentBlock), + 10, + "balance of account 0 at current block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[1], currentBlock), + 5, + "balance of account 1 at current block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[2], currentBlock), + 3, + "balance of account 2 at current block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[3], currentBlock), + 1, + "balance of account 3 at current block should be correct" + ); + + assertEq( + minimeToken.balanceOfAt(accounts[0], nextBlock), 12, "balance of account 0 at next block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[1], nextBlock), 6, "balance of account 1 at next block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[2], nextBlock), 4, "balance of account 2 at next block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[3], nextBlock), 2, "balance of account 3 at next block should be correct" + ); + + assertEq( + minimeToken.balanceOfAt(accounts[0], secondNextBlock), + 13, + "balance of account 0 at second next block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[1], secondNextBlock), + 7, + "balance of account 1 at second next block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[2], secondNextBlock), + 5, + "balance of account 2 at second next block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[3], secondNextBlock), + 2, + "balance of account 3 at second next block should be correct" + ); + + assertEq( + minimeToken.balanceOfAt(accounts[0], thirdNextBlock), + 14, + "balance of account 0 at third next block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[1], thirdNextBlock), + 8, + "balance of account 1 at third next block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[2], thirdNextBlock), + 5, + "balance of account 2 at third next block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[3], thirdNextBlock), + 2, + "balance of account 3 at third next block should be correct" + ); + + assertEq( + minimeToken.balanceOfAt(accounts[0], currentBlock - 1), + 0, + "balance of account 0 at previous block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[1], currentBlock - 1), + 0, + "balance of account 1 at previous block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[2], currentBlock - 1), + 0, + "balance of account 2 at previous block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[3], currentBlock - 1), + 0, + "balance of account 3 at previous block should be correct" + ); + + assertEq( + minimeToken.balanceOfAt(accounts[0], thirdNextBlock + 1), + 14, + "balance of account 0 at next block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[1], thirdNextBlock + 1), + 8, + "balance of account 1 at next block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[2], thirdNextBlock + 1), + 5, + "balance of account 2 at next block should be correct" + ); + assertEq( + minimeToken.balanceOfAt(accounts[3], thirdNextBlock + 1), + 2, + "balance of account 3 at next block should be correct" + ); + } +}