Skip to content

Commit

Permalink
More tests for RootRegistry and ETHRegistry.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arachnid committed Aug 28, 2024
1 parent 73eb5ab commit 84508d7
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 11 deletions.
8 changes: 5 additions & 3 deletions contracts/src/registry/ETHRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,20 @@ contract ETHRegistry is LockableRegistry, AccessControl {
function getSubregistry(string calldata label) external view virtual override returns (IRegistry) {
(address subregistry, uint96 flags) = datastore.getSubregistry(uint256(keccak256(bytes(label))));
uint64 expires = uint64(flags);
if (expires >= block.timestamp) {
if (expires <= block.timestamp) {
return IRegistry(address(0));
}
return IRegistry(subregistry);
}

function getResolver(string calldata label) external view virtual override returns (address) {
(address resolver, uint96 flags) = datastore.getResolver(uint256(keccak256(bytes(label))));
(address subregistry, uint96 flags) = datastore.getSubregistry(uint256(keccak256(bytes(label))));
uint64 expires = uint64(flags);
if (expires >= block.timestamp) {
if (expires <= block.timestamp) {
return address(0);
}

(address resolver, ) = datastore.getResolver(uint256(keccak256(bytes(label))));
return resolver;
}
}
17 changes: 11 additions & 6 deletions contracts/src/registry/LockableRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ abstract contract LockableRegistry is BaseRegistry {
constructor(IRegistryDatastore _datastore) BaseRegistry(_datastore) {
}

function _lock(uint256 tokenId, uint96 flags)
function _lock(uint256 tokenId, uint96 _flags)
internal
withSubregistryFlags(tokenId, FLAG_FLAGS_LOCKED, 0)
returns(uint96 newFlags)
{
(address subregistry, uint96 oldFlags) = datastore.getSubregistry(tokenId);
newFlags = oldFlags | (flags & FLAGS_MASK);
newFlags = oldFlags | (_flags & FLAGS_MASK);
if (newFlags != oldFlags) {
datastore.setSubregistry(tokenId, subregistry, newFlags);
}
Expand All @@ -35,16 +35,21 @@ abstract contract LockableRegistry is BaseRegistry {
onlyTokenOwner(tokenId)
withSubregistryFlags(tokenId, FLAG_SUBREGISTRY_LOCKED, 0)
{
(, uint96 flags) = datastore.getSubregistry(tokenId);
datastore.setSubregistry(tokenId, address(registry), flags);
(, uint96 _flags) = datastore.getSubregistry(tokenId);
datastore.setSubregistry(tokenId, address(registry), _flags);
}

function setResolver(uint256 tokenId, address resolver)
external
onlyTokenOwner(tokenId)
withSubregistryFlags(tokenId, FLAG_RESOLVER_LOCKED, 0)
{
(, uint96 flags) = datastore.getResolver(tokenId);
datastore.setResolver(tokenId, resolver, flags);
(, uint96 _flags) = datastore.getResolver(tokenId);
datastore.setResolver(tokenId, resolver, _flags);
}

function flags(uint256 tokenId) external view returns(uint96) {
(, uint96 _flags) = datastore.getSubregistry(tokenId);
return _flags;
}
}
13 changes: 11 additions & 2 deletions contracts/src/registry/RootRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ contract RootRegistry is LockableRegistry, AccessControl {
* @param registry The address of the registry to use.
* @param flags Flags to set.
*/
function mint(string calldata label, address owner, IRegistry registry, uint32 flags)
function mint(string calldata label, address owner, IRegistry registry, uint96 flags)
external
onlyRole(TLD_ISSUER_ROLE)
returns(uint256 tokenId)
{
uint256 tokenId = uint256(keccak256(bytes(label)));
tokenId = uint256(keccak256(bytes(label)));
_mint(owner, tokenId, 1, "");
datastore.setSubregistry(tokenId, address(registry), flags);
emit NewSubname(label);
Expand All @@ -53,6 +54,14 @@ contract RootRegistry is LockableRegistry, AccessControl {
datastore.setSubregistry(tokenId, address(0), 0);
}

function lock(uint256 tokenId, uint96 flags)
external
onlyTokenOwner(tokenId)
returns(uint96)
{
return _lock(tokenId, flags);
}

function supportsInterface(bytes4 interfaceId) public view override(BaseRegistry, AccessControl) returns (bool) {
return super.supportsInterface(interfaceId);
}
Expand Down
30 changes: 30 additions & 0 deletions contracts/test/ETHRegistry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,34 @@ contract TestETHRegistry is Test, ERC1155Holder {
registry.register("test2", address(this), registry, flags, uint64(block.timestamp) + 86400);
registry.register("test2", address(this), registry, 0, uint64(block.timestamp) + 86400);
}

function test_set_subregistry() public {
uint256 tokenId = registry.register("test2", address(this), registry, 0, uint64(block.timestamp) + 86400);
registry.setSubregistry(tokenId, IRegistry(address(this)));
vm.assertEq(address(registry.getSubregistry("test2")), address(this));
}

function testFail_cannot_set_locked_subregistry() public {
uint96 flags = registry.FLAG_SUBREGISTRY_LOCKED();
uint256 tokenId = registry.register("test2", address(this), registry, flags, uint64(block.timestamp) + 86400);
registry.setSubregistry(tokenId, IRegistry(address(this)));
}

function test_set_resolver() public {
uint256 tokenId = registry.register("test2", address(this), registry, 0, uint64(block.timestamp) + 86400);
registry.setResolver(tokenId, address(this));
vm.assertEq(address(registry.getResolver("test2")), address(this));
}

function testFail_cannot_set_locked_resolver() public {
uint96 flags = registry.FLAG_RESOLVER_LOCKED();
uint256 tokenId = registry.register("test2", address(this), registry, flags, uint64(block.timestamp) + 86400);
registry.setResolver(tokenId, address(this));
}

function testFail_cannot_set_locked_flags() public {
uint96 flags = registry.FLAG_FLAGS_LOCKED();
uint256 tokenId = registry.register("test2", address(this), registry, flags, uint64(block.timestamp) + 86400);
registry.lock(tokenId, registry.FLAG_RESOLVER_LOCKED());
}
}
95 changes: 95 additions & 0 deletions contracts/test/RootRegistry.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.13;

import "forge-std/Test.sol";
import "forge-std/console.sol";

import {ERC1155Holder} from "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";

import "src/registry/RootRegistry.sol";
import "src/registry/RegistryDatastore.sol";

contract TestRootRegistry is Test, ERC1155Holder {
event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value);

RegistryDatastore datastore;
RootRegistry registry;

function setUp() public {
datastore = new RegistryDatastore();
registry = new RootRegistry(datastore);
registry.grantRole(registry.TLD_ISSUER_ROLE(), address(this));
}

function test_register_unlocked() public {
uint256 expectedId = uint256(keccak256("test2"));
vm.expectEmit(true, true, true, true);
emit TransferSingle(address(this), address(0), address(this), expectedId, 1);

uint256 tokenId = registry.mint("test2", address(this), registry, 0);
vm.assertEq(tokenId, expectedId);
uint96 flags = registry.flags(tokenId);
vm.assertEq(flags, 0);
}

function test_register_locked() public {
uint96 flags = registry.FLAG_SUBREGISTRY_LOCKED() | registry.FLAG_RESOLVER_LOCKED();
uint256 expectedId = uint256(keccak256("test2"));
vm.expectEmit(true, true, true, true);
emit TransferSingle(address(this), address(0), address(this), expectedId, 1);

uint256 tokenId = registry.mint("test2", address(this), registry, flags);
vm.assertEq(tokenId, expectedId);
uint96 actualFlags = registry.flags(tokenId);
vm.assertEq(flags, actualFlags);
}

function test_lock_name() public {
uint96 flags = registry.FLAG_SUBREGISTRY_LOCKED() | registry.FLAG_RESOLVER_LOCKED();
uint256 tokenId = registry.mint("test2", address(this), registry, 0);
uint96 actualFlags = registry.lock(tokenId, flags);
vm.assertEq(flags, actualFlags);
uint96 actualFlags2 = registry.flags(tokenId);
vm.assertEq(flags, actualFlags2);
}

function test_cannot_unlock_name() public {
uint96 flags = registry.FLAG_SUBREGISTRY_LOCKED() | registry.FLAG_RESOLVER_LOCKED();

uint256 tokenId = registry.mint("test2", address(this), registry, flags);
uint96 newFlags = registry.lock(tokenId, 0);
vm.assertEq(flags, newFlags);
uint96 newFlags2 = registry.flags(tokenId);
vm.assertEq(flags, newFlags2);
}

function test_set_subregistry() public {
uint256 tokenId = registry.mint("test", address(this), registry, 0);
registry.setSubregistry(tokenId, IRegistry(address(this)));
vm.assertEq(address(registry.getSubregistry("test")), address(this));
}

function testFail_cannot_set_locked_subregistry() public {
uint96 flags = registry.FLAG_SUBREGISTRY_LOCKED();
uint256 tokenId = registry.mint("test", address(this), registry, flags);
registry.setSubregistry(tokenId, IRegistry(address(this)));
}

function test_set_resolver() public {
uint256 tokenId = registry.mint("test", address(this), registry, 0);
registry.setResolver(tokenId, address(this));
vm.assertEq(address(registry.getResolver("test")), address(this));
}

function testFail_cannot_set_locked_resolver() public {
uint96 flags = registry.FLAG_RESOLVER_LOCKED();
uint256 tokenId = registry.mint("test", address(this), registry, flags);
registry.setResolver(tokenId, address(this));
}

function testFail_cannot_set_locked_flags() public {
uint96 flags = registry.FLAG_FLAGS_LOCKED();
uint256 tokenId = registry.mint("test", address(this), registry, flags);
registry.lock(tokenId, registry.FLAG_RESOLVER_LOCKED());
}
}

0 comments on commit 84508d7

Please sign in to comment.