Skip to content

Commit

Permalink
RegistryDatastore tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Aug 13, 2024
1 parent e25cde4 commit 995bb62
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
- run: bun install --frozen-lockfile

- name: Run tests
run: bun --filter contracts test:hardhat
run: bun --filter contracts test
2 changes: 1 addition & 1 deletion contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"scripts": {
"compile:hardhat": "NODE_OPTIONS='--experimental-loader ts-node/esm/transpile-only' hardhat compile",
"test:hardhat": "NODE_OPTIONS='--experimental-loader ts-node/esm/transpile-only' hardhat test",
"test:forge": "echo \"Skipping forge test for now since there are no tests...\"",
"test:forge": "forge test",
"test": "bun run test:forge && bun run test:hardhat"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/registry/RegistryDatastore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract RegistryDatastore is IRegistryDatastore {
}

function getResolver(address registry, uint256 labelHash) public view returns(address resolver, uint96 flags) {
uint256 data = subregistries[registry][labelHash & LABEL_HASH_MASK];
uint256 data = resolvers[registry][labelHash & LABEL_HASH_MASK];
resolver = address(uint160(data));
flags = uint96(data >> 160);
}
Expand Down
144 changes: 144 additions & 0 deletions contracts/test/RegistryDatastore.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.13;

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

import "src/registry/RegistryDatastore.sol";

contract TestETHRegistry is Test {
uint256 LABEL_HASH_MASK = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000;
uint256 labelHash = uint256(keccak256("test"));
RegistryDatastore datastore;

function setUp() public {
datastore = new RegistryDatastore();
}

function test_GetSetSubregistry_MsgSender() public {
// set subregistry with empty flags
datastore.setSubregistry(labelHash, address(this), 0);

address subregistry;
uint96 flags;

(subregistry, flags) = datastore.getSubregistry(labelHash);
vm.assertEq(subregistry, address(this));
vm.assertEq(flags, 0);

(subregistry, flags) = datastore.getSubregistry(address(this), labelHash);
vm.assertEq(subregistry, address(this));
vm.assertEq(flags, 0);
}

function test_GetSetSubregistry_OtherRegistry() public {
DummyRegistry r = new DummyRegistry(datastore);
r.setSubregistry(labelHash, address(this), 0);

address subregistry;
uint96 flags;

(subregistry, flags) = datastore.getSubregistry(labelHash);
vm.assertEq(subregistry, address(0));
vm.assertEq(flags, 0);

(subregistry, flags) = datastore.getSubregistry(address(r), labelHash);
vm.assertEq(subregistry, address(this));
vm.assertEq(flags, 0);
}

function test_GetSetSubregistry_32BitCustomFlags() public {
datastore.setSubregistry(labelHash, address(this), 0x80000001);

address subregistry;
uint96 flags;

(subregistry, flags) = datastore.getSubregistry(labelHash);
vm.assertEq(subregistry, address(this));
vm.assertEq(flags, 0x80000001);

// get with flags on labelhash
(subregistry, flags) = datastore.getSubregistry((labelHash & LABEL_HASH_MASK) | 0x80000001);
vm.assertEq(subregistry, address(this));
vm.assertEq(flags, 0x80000001);
}

function test_GetSetSubregistry_96BitCustomFlags() public {
datastore.setSubregistry(labelHash, address(this), 0x800000000000000000000001);

(address subregistry, uint96 flags) = datastore.getSubregistry(labelHash);
vm.assertEq(subregistry, address(this));
vm.assertEq(flags, 0x800000000000000000000001);
}

function test_GetSetResolver_MsgSender() public {
datastore.setResolver(labelHash, address(this), 0);

address resolver;
uint96 flags;

(resolver, flags) = datastore.getResolver(labelHash);
vm.assertEq(resolver, address(this));
vm.assertEq(flags, 0);

(resolver, flags) = datastore.getResolver(address(this), labelHash);
vm.assertEq(resolver, address(this));
vm.assertEq(flags, 0);
}

function test_GetSetResolver_OtherRegistry() public {
DummyRegistry r = new DummyRegistry(datastore);
r.setResolver(labelHash, address(this), 0);

address resolver;
uint96 flags;

(resolver, flags) = datastore.getResolver(labelHash);
vm.assertEq(resolver, address(0));
vm.assertEq(flags, 0);

(resolver, flags) = datastore.getResolver(address(r), labelHash);
vm.assertEq(resolver, address(this));
vm.assertEq(flags, 0);
}

function test_GetSetResolver_32BitCustomFlags() public {
datastore.setResolver(labelHash, address(this), 0x80000001);

address resolver;
uint96 flags;

(resolver, flags) = datastore.getResolver(labelHash);
vm.assertEq(resolver, address(this));
vm.assertEq(flags, 0x80000001);

// get with flags on labelhash
(resolver, flags) = datastore.getResolver((labelHash & LABEL_HASH_MASK) | 0x80000001);
vm.assertEq(resolver, address(this));
vm.assertEq(flags, 0x80000001);
}

function test_GetSetResolver_96BitCustomFlags() public {
datastore.setResolver(labelHash, address(this), 0x800000000000000000000001);

(address resolver, uint96 flags) = datastore.getResolver(labelHash);
vm.assertEq(resolver, address(this));
vm.assertEq(flags, 0x800000000000000000000001);
}
}

contract DummyRegistry {
RegistryDatastore datastore;

constructor(RegistryDatastore _datastore) {
datastore = _datastore;
}

function setSubregistry(uint256 labelHash, address subregistry, uint96 flags) public {
datastore.setSubregistry(labelHash, subregistry, flags);
}

function setResolver(uint256 labelHash, address resolver, uint96 flags) public {
datastore.setResolver(labelHash, resolver, flags);
}
}

0 comments on commit 995bb62

Please sign in to comment.