Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kaden/audit #2

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/forge-std
49 changes: 33 additions & 16 deletions src/Repository.huff
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,31 @@
#define function owner(uint256) view returns (address)
#define function updater(uint256) view returns(address)

#define function addDpd(uint256, address, address, bytes32) nonpayable returns(uint256)
#define function updateDpdData(uint256, bytes32) nonpayable returns()
#define function updateDpdOwner(uint256, address) nonpayable returns()
#define function updateDpdUpdater(uint256, address) nonpayable returns()

#define event DPDAdded(uint256 indexed, address, address, bytes32)
#define event DPDUpdated(uint256,bytes32)
/// @notice Given a DPD id, CID, owner address, and updater address, initialize a new DPD.
#define function addDpd(uint256,bytes32,address,address) nonpayable returns ()

/// @notice Given a DPD id, return its CID.
#define function dpds(uint256) view returns (bytes32)
/// @notice Given a DPD id, return its owner address.
#define function owners(uint256) view returns (address)
/// @notice Given a DPD id, return its updater address.
#define function updaters(uint256) view returns (address)

/// @notice Update a DPD CID.
/// @notice Can only be called by the DPD's Updater address.
#define function updateDpdData(uint256, bytes32) nonpayable returns ()
/// @notice Set a new DPD owner.
#define function updateDpdOwner(uint256, address) nonpayable returns ()
/// @notice Set a new DPD updater.
#define function updateDpdUpdater(uint256, address) nonpayable returns ()

/// @notice Event emitted when a new DPD is added to the repository.
#define event DPDAdded(uint256, address, address,bytes32)
/// @notice Event emitted when a DPD is updated.
#define event DPDUpdated(uint256,uint256,bytes32)
/// @notice Event emitted when a DPD's owner is changed.
#define event DPDOwnerChanged(uint256,address)
/// @notice Event emitted when a DPD's upgrader is changed.
#define event DPDUpdaterChanged(uint256,address)

/* Event Signatures */
Expand Down Expand Up @@ -156,13 +173,13 @@
[DPD_UPDATER_MAP_SLOT] // [salt, dpd_id, dpd_id, data]
LOAD_ELEMENT_FROM_KEYS(0x00) // [updater, dpd_id, data]
caller // [msg.sender, updater, dpd_id, data]
eq iszero // [msg.sender!=updater, dpd_id, data]
eq // [msg.sender==updater, dpd_id, data]
dup2 // [dpd_id, msg.sender!=updater, dpd_id, data]
[DPD_OWNER_MAP_SLOT] // [salt, dpd_id, msg.sender!=updater, dpd_id, data]
LOAD_ELEMENT_FROM_KEYS(0x00) // [owner, msg.sender!=updater, dpd_id, data]
caller // [msg.sender, owner, msg.sender!=updater, dpd_id, data]
eq iszero // [msg.sender!=owner, msg.sender!=updater, dpd_id, data]
or // [msg.sender!=owner || msg.sender!=updater, dpd_id, data]
eq // [msg.sender==owner, msg.sender==updater, dpd_id, data]
or iszero // [!(msg.sender==owner || msg.sender==updater), dpd_id, data]
error jumpi // [dpd_id, data]

// Store new data.
Expand Down Expand Up @@ -193,12 +210,12 @@


// Ensure that the sender is the DPD owner.
dup1 // [dpd_id, dpd_id, updater]
[DPD_OWNER_MAP_SLOT] // [salt, dpd_id, dpd_id, updater]
LOAD_ELEMENT_FROM_KEYS(0x00) // [owner, dpd_id, updater]
caller // [msg.sender, owner, dpd_id, updater]
eq iszero // [msg.sender != owner, dpd_id, updater]
error jumpi // [dpd_id, updater]
dup1 // [dpd_id, dpd_id, updater]
[DPD_OWNER_MAP_SLOT] // [salt, dpd_id, dpd_id, updater]
LOAD_ELEMENT_FROM_KEYS(0x00) // [owner, dpd_id, updater]
caller // [msg.sender, owner, dpd_id, updater]
eq iszero // [msg.sender != owner, dpd_id, updater]
error jumpi // [dpd_id, updater]

// Store new data.
[DPD_UPDATER_MAP_SLOT] // [salt, dpd_id, updater]
Expand Down
6 changes: 3 additions & 3 deletions test/Benchmark.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "forge-std/Test.sol";
import "forge-std/console.sol";
import "./benchmarks/DPDRepository.sol";

contract BenchmarkedTest is Test {
contract BenchmarkTest is Test {
/// @dev Address of the Huff DPDRepository contract.
Repository public repository;

Expand Down Expand Up @@ -136,8 +136,8 @@ interface Repository {
/// @notice Event emitted when a new DPD is added to the repository.
event DPDAdded(uint256 indexed id, address owner, address updater, bytes32 cid);

/// @notice Event emitted when a new DPD is added to the repository.
event DPDUpdated(uint256 indexed id, bytes32 cid);
/// @notice Event emitted when a DPD is updated.
event DPDUpdated(uint256 indexed dpdId, bytes32 cid);

/// @notice Event emitted when a DPD's owner is changed.
event DPDOwnerUpdated(uint256 indexed id, address newOwner);
Expand Down
35 changes: 23 additions & 12 deletions test/Repository.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "foundry-huff/HuffDeployer.sol";
import "forge-std/Test.sol";

contract RepositoryTest is Test {
/// @dev Address of the SimpleStore contract.
/// @dev Address of the Repository contract.
Repository public repository;

/// @notice Event emitted when a new DPD is added to the repository.
Expand Down Expand Up @@ -90,14 +90,25 @@ contract RepositoryTest is Test {
repository.addDpd(0, address(this), address(this), bytes32(uint256(69)));
}

/// @dev Ensure that you can update DPD data.
function testUpdateData() public {
vm.expectEmit(true, true, true, true);
emit DPDAdded(0, address(this), address(this), bytes32(uint256(69)));
repository.addDpd(0, address(this), address(this), bytes32(uint256(69)));
/// @dev Ensure that you can update DPD data as owner.
function testUpdateDataAsOwner() public {
repository.addDpd(0, address(this), address(0xBEEF), bytes32(uint256(69)));
repository.updateDpdData(0, bytes32(uint256(1000)));

vm.expectEmit(true, true, true, true);
emit DPDUpdated(0, bytes32(uint256(1000)));
assertEq(uint256(repository.dpds(0)), 1000);
}

/// @dev Ensure that you can update DPD data as updater.
function testUpdateDataAsUpdater() public {
repository.addDpd(0, address(0xBEEF), address(this), bytes32(uint256(69)));
repository.updateDpdData(0, bytes32(uint256(1000)));

assertEq(uint256(repository.dpds(0)), 1000);
}

/// @dev Ensure that you cannot update DPD data as non-owner/updater.
function testFailUpdateDataAsNonUpdaterNonOwner() public {
repository.addDpd(0, address(0xBEEF), address(0xBEEF), bytes32(uint256(69)));
repository.updateDpdData(0, bytes32(uint256(1000)));

assertEq(uint256(repository.dpds(0)), 1000);
Expand Down Expand Up @@ -152,12 +163,12 @@ interface Repository {
/// @notice Event emitted when a new DPD is added to the repository.
event DPDAdded(uint256 indexed id, address owner, address updater, bytes32 cid);

/// @notice Event emitted when a new DPD is added to the repository.
event DPDUpdated(uint256 indexed id, bytes32 cid);
/// @notice Event emitted when a DPD is updated.
event DPDUpdated(uint256 indexed dpdId, bytes32 cid);

/// @notice Event emitted when a DPD's owner is changed.
event DPDOwnerUpdated(uint256 indexed id, address newOwner);
event DPDOwnerChanged(uint256 indexed dpdId, address newOwner);

/// @notice Event emitted when a DPD's upgrader is changed.
event DPDUpdaterUpdated(uint256 indexed id, address newUpdater);
event DPDUpdaterChanged(uint256 indexed dpdId, address newUpdater);
}