Skip to content

Commit

Permalink
Merge pull request #136 from gildlab/15-10-2024-add-assume-to-avoid-k…
Browse files Browse the repository at this point in the history
…ey-collisions

Add missing vm.assume(alice != bob) to testMintSomeoneElse
  • Loading branch information
thedavidmeister authored Oct 15, 2024
2 parents d08a980 + 29e5256 commit d86e644
Show file tree
Hide file tree
Showing 21 changed files with 334 additions and 357 deletions.
20 changes: 20 additions & 0 deletions test/lib/LibUniqueAddressesGenerator.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: CAL
pragma solidity =0.8.25;

import {Vm} from "forge-std/Test.sol";

library LibUniqueAddressesGenerator {
// Generates two unique addresses from the provided fuzzed keys
function generateUniqueAddresses(Vm vm, uint256 SECP256K1_ORDER, uint256 fuzzedKeyAlice, uint256 fuzzedKeyBob)
internal
pure
returns (address, address)
{
// Ensure the fuzzed key is within the valid range for secp256k1
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);
vm.assume(alice != bob);

return (alice, bob);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "../../../../../src/concrete/vault/ERC20PriceOracleReceiptVault.sol";
import {ERC20PriceOracleReceiptVaultTest, Vm} from "test/abstract/ERC20PriceOracleReceiptVaultTest.sol";
import {TwoPriceOracle} from "../../../../../src/concrete/oracle/TwoPriceOracle.sol";
import {LibUniqueAddressesGenerator} from "../../../lib/LibUniqueAddressesGenerator.sol";

contract ERC20PriceOracleReceiptVaultConstructionTest is ERC20PriceOracleReceiptVaultTest {
/// Test ERC20PriceOracleReceiptVault is constracted
Expand Down Expand Up @@ -89,10 +90,9 @@ contract ERC20PriceOracleReceiptVaultConstructionTest is ERC20PriceOracleReceipt
uint8 usdDecimals,
uint80 answeredInRound
) external {
// Ensure the fuzzed key is within the valid range for secp256k1
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);
vm.assume(alice != bob);
// Generate unique addresses
(address alice, address bob) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, fuzzedKeyAlice, fuzzedKeyBob);

// Use common decimal bounds for price feeds
// Use 0-20 so we at least have some coverage higher than 18
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import {IERC20} from "forge-std/interfaces/IERC20.sol";
import {Receipt as ReceiptContract} from "../../../../../src/concrete/receipt/Receipt.sol";
import {IReceiptVaultV1} from "../../../../../src/interface/IReceiptVaultV1.sol";
import {LibUniqueAddressesGenerator} from "../../../lib/LibUniqueAddressesGenerator.sol";

contract ERC20PriceOracleReceiptVaultDepositTest is ERC20PriceOracleReceiptVaultTest {
using LibFixedPointDecimalArithmeticOpenZeppelin for uint256;
Expand Down Expand Up @@ -79,10 +80,9 @@ contract ERC20PriceOracleReceiptVaultDepositTest is ERC20PriceOracleReceiptVault
uint8 usdDecimals,
uint80 answeredInRound
) external {
// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);
vm.assume(alice != bob);
// Generate unique addresses
(address alice, address bob) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, fuzzedKeyAlice, fuzzedKeyBob);

// Use common decimal bounds for price feeds
// Use 0-20 so we at least have some coverage higher than 18
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Math
} from "rain.math.fixedpoint/lib/LibFixedPointDecimalArithmeticOpenZeppelin.sol";
import {IERC20} from "forge-std/interfaces/IERC20.sol";
import {LibUniqueAddressesGenerator} from "../../../lib/LibUniqueAddressesGenerator.sol";

contract ERC20PriceOracleReceiptVaultDepositTest is ERC20PriceOracleReceiptVaultTest {
using LibFixedPointDecimalArithmeticOpenZeppelin for uint256;
Expand Down Expand Up @@ -70,9 +71,10 @@ contract ERC20PriceOracleReceiptVaultDepositTest is ERC20PriceOracleReceiptVault
uint8 usdDecimals,
uint80 answeredInRound
) external {
// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);
// Generate unique addresses
(address alice, address bob) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, fuzzedKeyAlice, fuzzedKeyBob);

// Use common decimal bounds for price feeds
// Use 0-20 so we at least have some coverage higher than 18
usdDecimals = uint8(bound(usdDecimals, 0, 20));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "rain.math.fixedpoint/lib/LibFixedPointDecimalArithmeticOpenZeppelin.sol";
import {IERC20} from "forge-std/interfaces/IERC20.sol";
import {IReceiptVaultV1} from "../../../../../src/interface/IReceiptVaultV1.sol";
import {LibUniqueAddressesGenerator} from "../../../lib/LibUniqueAddressesGenerator.sol";

contract ERC20PriceOracleReceiptVaultreceiptVaultTest is ERC20PriceOracleReceiptVaultTest {
using LibFixedPointDecimalArithmeticOpenZeppelin for uint256;
Expand Down Expand Up @@ -122,9 +123,10 @@ contract ERC20PriceOracleReceiptVaultreceiptVaultTest is ERC20PriceOracleReceipt
uint8 usdDecimals,
uint80 answeredInRound
) external {
// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);
// Generate unique addresses
(address alice, address bob) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, fuzzedKeyAlice, fuzzedKeyBob);

// Use common decimal bounds for price feeds
// Use 0-20 so we at least have some coverage higher than 18
usdDecimals = uint8(bound(usdDecimals, 0, 20));
Expand Down Expand Up @@ -196,9 +198,10 @@ contract ERC20PriceOracleReceiptVaultreceiptVaultTest is ERC20PriceOracleReceipt
uint8 usdDecimals,
uint80 answeredInRound
) external {
// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);
// Generate unique addresses
(address alice, address bob) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, fuzzedKeyAlice, fuzzedKeyBob);

// Use common decimal bounds for price feeds
// Use 0-20 so we at least have some coverage higher than 18
usdDecimals = uint8(bound(usdDecimals, 0, 20));
Expand Down
7 changes: 4 additions & 3 deletions test/src/concrete/oracle/OwnableOracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity =0.8.25;

import {Test} from "forge-std/Test.sol";
import {OwnableOracle} from "src/concrete/oracle/OwnableOracle.sol";
import {LibUniqueAddressesGenerator} from "../../../lib/LibUniqueAddressesGenerator.sol";

contract OwnableOracleTest is Test {
event Price(uint256 oldPrice, uint256 newPrice);
Expand Down Expand Up @@ -31,9 +32,9 @@ contract OwnableOracleTest is Test {
/// The owner can set the price.
/// Anon can't set the price.
function testSetPrice(uint256 ownerSeed, uint256 anonSeed, uint256 newPrice0, uint256 newPrice1) external {
address owner = vm.addr((ownerSeed % (SECP256K1_ORDER - 1)) + 1);
address anon = vm.addr((anonSeed % (SECP256K1_ORDER - 1)) + 1);
vm.assume(owner != anon);
// Generate unique addresses
(address owner, address anon) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, ownerSeed, anonSeed);

vm.prank(owner);
OwnableOracle oracle = new OwnableOracle();
Expand Down
23 changes: 11 additions & 12 deletions test/src/concrete/receipt/Receipt.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {Receipt} from "../../../../../src/concrete/receipt/Receipt.sol";
import {IReceiptOwnerV1} from "../../../../../src/interface/IReceiptOwnerV1.sol";
import {TestReceipt} from "test/concrete/TestReceipt.sol";
import {TestReceiptOwner} from "test/concrete/TestReceiptOwner.sol";
import {LibUniqueAddressesGenerator} from "../../../lib/LibUniqueAddressesGenerator.sol";

contract ReceiptTest is Test {
event ReceiptInformation(address sender, uint256 id, bytes information);
Expand Down Expand Up @@ -137,10 +138,10 @@ contract ReceiptTest is Test {
bytes memory fuzzedReceiptInformation,
uint256 transferAmount
) external {
// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);
vm.assume(alice != bob);
// Generate unique addresses
(address alice, address bob) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, fuzzedKeyAlice, fuzzedKeyBob);

// Bound with uint256 max - 1 so dowsnot get overflow while bounding transferAmount
amount = bound(amount, 1, type(uint256).max - 1);
id = bound(id, 0, type(uint256).max);
Expand Down Expand Up @@ -175,10 +176,9 @@ contract ReceiptTest is Test {
uint256 amount,
bytes memory fuzzedReceiptInformation
) external {
// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);
vm.assume(alice != bob);
// Generate unique addresses
(address alice, address bob) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, fuzzedKeyAlice, fuzzedKeyBob);

amount = bound(amount, 1, type(uint256).max);
id = bound(id, 0, type(uint256).max);
Expand Down Expand Up @@ -212,10 +212,9 @@ contract ReceiptTest is Test {
uint256 amount,
bytes memory fuzzedReceiptInformation
) external {
// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);
vm.assume(alice != bob);
// Generate unique addresses
(address alice, address bob) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, fuzzedKeyAlice, fuzzedKeyBob);

amount = bound(amount, 1, type(uint256).max);
id = bound(id, 0, type(uint256).max);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity =0.8.25;

import {OffchainAssetReceiptVaultTest, Vm} from "test/abstract/OffchainAssetReceiptVaultTest.sol";
import {OffchainAssetReceiptVault, CertificationExpired} from "src/concrete/vault/OffchainAssetReceiptVault.sol";
import {LibUniqueAddressesGenerator} from "../../../lib/LibUniqueAddressesGenerator.sol";

contract OffchainAssetReceipetVaultAuthorizedReceiptTransferTest is OffchainAssetReceiptVaultTest {
event Certify(address sender, uint256 certifyUntil, uint256 referenceBlockNumber, bool forceUntil, bytes data);
Expand All @@ -15,11 +16,9 @@ contract OffchainAssetReceipetVaultAuthorizedReceiptTransferTest is OffchainAsse
string memory assetName,
string memory assetSymbol
) external {
// Ensure the fuzzed key is within the valid range for secp256k1
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);

vm.assume(alice != bob);
// Generate unique addresses
(address alice, address bob) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, fuzzedKeyAlice, fuzzedKeyBob);

// Bound warpTimestamp from 1 to avoid potential issues with timestamp 0.
warpTimestamp = bound(warpTimestamp, 1, type(uint32).max);
Expand Down Expand Up @@ -52,11 +51,9 @@ contract OffchainAssetReceipetVaultAuthorizedReceiptTransferTest is OffchainAsse
uint256 blockNumber,
bytes memory data
) external {
// Ensure the fuzzed key is within the valid range for secp256k1
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);

vm.assume(alice != bob);
// Generate unique addresses
(address alice, address bob) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, fuzzedKeyAlice, fuzzedKeyBob);

// Bound timestamp from 1 to avoid potential issues with timestamp 0.
timestamp = bound(timestamp, 1, type(uint32).max - 1); // Need to subtract 1 for the next bound
Expand Down Expand Up @@ -102,11 +99,9 @@ contract OffchainAssetReceipetVaultAuthorizedReceiptTransferTest is OffchainAsse
uint256 blockNumber,
bool forceUntil
) external {
// Ensure the fuzzed key is within the valid range for secp256k1
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);

vm.assume(alice != bob);
// Generate unique addresses
(address alice, address bob) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, fuzzedKeyAlice, fuzzedKeyBob);

blockNumber = bound(blockNumber, 0, type(uint32).max);
vm.roll(blockNumber);
Expand Down Expand Up @@ -143,11 +138,9 @@ contract OffchainAssetReceipetVaultAuthorizedReceiptTransferTest is OffchainAsse
string memory assetName,
string memory assetSymbol
) external {
// Ensure the fuzzed key is within the valid range for secp256k1
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);

vm.assume(alice != bob);
// Generate unique addresses
(address alice, address bob) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, fuzzedKeyAlice, fuzzedKeyBob);

OffchainAssetReceiptVault vault = createVault(alice, assetName, assetSymbol);

Expand All @@ -168,11 +161,10 @@ contract OffchainAssetReceipetVaultAuthorizedReceiptTransferTest is OffchainAsse
string memory assetName,
string memory assetSymbol
) external {
// Ensure the fuzzed key is within the valid range for secp256k1
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);
// Generate unique addresses
(address alice, address bob) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, fuzzedKeyAlice, fuzzedKeyBob);

vm.assume(alice != bob);
OffchainAssetReceiptVault vault = createVault(alice, assetName, assetSymbol);

// Prank as Alice to set role
Expand All @@ -193,11 +185,9 @@ contract OffchainAssetReceipetVaultAuthorizedReceiptTransferTest is OffchainAsse
string memory assetName,
string memory assetSymbol
) external {
// Ensure the fuzzed key is within the valid range for secp256k1
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);

vm.assume(alice != bob);
// Generate unique addresses
(address alice, address bob) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, fuzzedKeyAlice, fuzzedKeyBob);

OffchainAssetReceiptVault vault = createVault(alice, assetName, assetSymbol);

Expand Down
Loading

0 comments on commit d86e644

Please sign in to comment.