Skip to content

Commit 91536f9

Browse files
committedJul 7, 2022
update storage namings
1 parent 597a1e7 commit 91536f9

File tree

7 files changed

+169
-184
lines changed

7 files changed

+169
-184
lines changed
 

‎packages/registry/contracts/CannonRegistry.sol

+52-46
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,28 @@ contract CannonRegistry is Storage, Ownable, UUPSImplementation {
99
error Unauthorized();
1010
error InvalidUrl(string url);
1111
error InvalidName(bytes32 name);
12+
error TooManyTags();
1213

1314
event PackagePublish(bytes32 indexed name, bytes32 indexed version, bytes32[] indexed tags, string url, address owner);
1415

1516
uint public constant MIN_PACKAGE_NAME_LENGTH = 3;
1617

17-
function upgradeTo(address newImplementation) public override onlyOwner {
18-
_upgradeTo(newImplementation);
18+
function upgradeTo(address _newImplementation) public override onlyOwner {
19+
_upgradeTo(_newImplementation);
1920
}
2021

21-
function validatePackageName(bytes32 name) public pure returns (bool) {
22+
function validatePackageName(bytes32 _name) public pure returns (bool) {
2223
// each character must be in the supported charset
2324

2425
for (uint i = 0; i < 32; i++) {
25-
if (name[i] == bytes1(0)) {
26+
if (_name[i] == bytes1(0)) {
2627
// must be long enough
2728
if (i < MIN_PACKAGE_NAME_LENGTH) {
2829
return false;
2930
}
3031

3132
// last character cannot be `-`
32-
if (name[i - 1] == "-") {
33+
if (_name[i - 1] == "-") {
3334
return false;
3435
}
3536

@@ -38,10 +39,10 @@ contract CannonRegistry is Storage, Ownable, UUPSImplementation {
3839

3940
// must be in valid character set
4041
if (
41-
(name[i] < "0" || name[i] > "9") &&
42-
(name[i] < "a" || name[i] > "z") &&
42+
(_name[i] < "0" || _name[i] > "9") &&
43+
(_name[i] < "a" || _name[i] > "z") &&
4344
// first character cannot be `-`
44-
(i == 0 || name[i] != "-")
45+
(i == 0 || _name[i] != "-")
4546
) {
4647
return false;
4748
}
@@ -51,79 +52,84 @@ contract CannonRegistry is Storage, Ownable, UUPSImplementation {
5152
}
5253

5354
function publish(
54-
bytes32 _name,
55-
bytes32 _version,
56-
bytes32[] memory _tags,
57-
string memory _url
55+
bytes32 _packageName,
56+
bytes32 _packageVersionName,
57+
bytes32[] memory _packageTags,
58+
string memory _packageVersionUrl
5859
) external {
59-
Store storage s = _store();
60+
if (_packageTags.length > 5) {
61+
revert TooManyTags();
62+
}
6063

61-
if (bytes(_url).length == 0) {
62-
revert InvalidUrl(_url);
64+
if (bytes(_packageVersionUrl).length == 0) {
65+
revert InvalidUrl(_packageVersionUrl);
6366
}
6467

65-
if (s.owners[_name] != address(0) && s.owners[_name] != msg.sender) {
68+
Package storage _p = _store().packages[_packageName];
69+
70+
if (_p.owner != address(0) && _p.owner != msg.sender) {
6671
revert Unauthorized();
6772
}
6873

69-
if (s.owners[_name] == address(0)) {
70-
if (!validatePackageName(_name)) {
71-
revert InvalidName(_name);
74+
if (_p.owner == address(0)) {
75+
if (!validatePackageName(_packageName)) {
76+
revert InvalidName(_packageName);
7277
}
7378

74-
s.owners[_name] = msg.sender;
75-
s.packages.push(_name);
79+
_p.owner = msg.sender;
7680
}
7781

78-
if (bytes(s.urls[_name][_version]).length == 0) {
79-
s.versions[_name].push(_version);
82+
if (bytes(_p.versionUrls[_packageVersionName]).length == 0) {
83+
_p.versions.push(_packageVersionName);
8084
}
8185

82-
s.urls[_name][_version] = _url;
86+
_p.versionUrls[_packageVersionName] = _packageVersionUrl;
8387

84-
for (uint i = 0; i < _tags.length; i++) {
85-
s.urls[_name][_tags[i]] = _url;
88+
for (uint i = 0; i < _packageTags.length; i++) {
89+
bytes32 _tag = _packageTags[i];
90+
91+
if (bytes(_p.versionUrls[_tag]).length == 0) {
92+
_p.versions.push(_tag);
93+
}
94+
95+
_p.versionUrls[_tag] = _packageVersionUrl;
8696
}
8797

88-
emit PackagePublish(_name, _version, _tags, _url, msg.sender);
98+
emit PackagePublish(_packageName, _packageVersionName, _packageTags, _packageVersionUrl, msg.sender);
8999
}
90100

91-
function nominatePackageOwner(bytes32 _name, address _newOwner) external {
92-
Store storage s = _store();
101+
function nominatePackageOwner(bytes32 _packageName, address _newPackageOwner) external {
102+
Package storage _p = _store().packages[_packageName];
93103

94-
if (s.owners[_name] != msg.sender) {
104+
if (_p.owner != msg.sender) {
95105
revert Unauthorized();
96106
}
97107

98-
s.nominatedOwner[_name] = _newOwner;
108+
_p.nominatedOwner = _newPackageOwner;
99109
}
100110

101-
function acceptPackageOwnership(bytes32 _name) external {
102-
Store storage s = _store();
111+
function acceptPackageOwnership(bytes32 _packageName) external {
112+
Package storage _p = _store().packages[_packageName];
103113

104-
address newOwner = s.nominatedOwner[_name];
114+
address newOwner = _p.nominatedOwner;
105115

106116
if (msg.sender != newOwner) {
107117
revert Unauthorized();
108118
}
109119

110-
s.owners[_name] = newOwner;
111-
s.nominatedOwner[_name] = address(0);
112-
}
113-
114-
function getPackageNominatedOwner(bytes32 _protocolName) external view returns (address) {
115-
return _store().nominatedOwner[_protocolName];
120+
_p.owner = newOwner;
121+
_p.nominatedOwner = address(0);
116122
}
117123

118-
function getPackages() external view returns (bytes32[] memory) {
119-
return _store().packages;
124+
function getPackageNominatedOwner(bytes32 _packageName) external view returns (address) {
125+
return _store().packages[_packageName].nominatedOwner;
120126
}
121127

122-
function getPackageVersions(bytes32 _protocolName) external view returns (bytes32[] memory) {
123-
return _store().versions[_protocolName];
128+
function getPackageVersions(bytes32 _packageName) external view returns (bytes32[] memory) {
129+
return _store().packages[_packageName].versions;
124130
}
125131

126-
function getPackageUrl(bytes32 _protocolName, bytes32 _protocolVersion) external view returns (string memory) {
127-
return _store().urls[_protocolName][_protocolVersion];
132+
function getPackageUrl(bytes32 _packageName, bytes32 _packageVersionName) external view returns (string memory) {
133+
return _store().packages[_packageName].versionUrls[_packageVersionName];
128134
}
129135
}

‎packages/registry/contracts/Proxy.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ pragma solidity ^0.8.11;
44
import {UUPSProxy} from "@synthetixio/core-contracts/contracts/proxy/UUPSProxy.sol";
55

66
contract Proxy is UUPSProxy {
7-
// solhint-disable-next-line no-empty-blocks
8-
constructor(address firstImplementation) UUPSProxy(firstImplementation) {}
7+
// solhint-disable-next-line no-empty-blocks
8+
constructor(address firstImplementation) UUPSProxy(firstImplementation) {}
99
}

‎packages/registry/contracts/Storage.sol

+15-12
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22
pragma solidity ^0.8.11;
33

44
contract Storage {
5-
struct Store {
6-
bytes32[] packages;
7-
mapping(bytes32 => mapping(bytes32 => string)) urls;
8-
mapping(bytes32 => address) owners;
9-
mapping(bytes32 => bytes32[]) versions;
10-
mapping(bytes32 => address) nominatedOwner;
11-
}
5+
struct Store {
6+
mapping(bytes32 => Package) packages;
7+
}
8+
9+
struct Package {
10+
address owner;
11+
address nominatedOwner;
12+
bytes32[] versions;
13+
mapping(bytes32 => string) versionUrls;
14+
}
1215

13-
function _store() internal pure returns (Store storage store) {
14-
assembly {
15-
// bytes32(uint(keccak256("usecannon.cannon-registry")) - 1)
16-
store.slot := 0xd386b53009e5ad6d6853d9184c05c992a989289c1761a6d9dd1cdfd204098522
17-
}
16+
function _store() internal pure returns (Store storage store) {
17+
assembly {
18+
// bytes32(uint(keccak256("usecannon.cannon.registry")) - 1)
19+
store.slot := 0xc1d43f226fc86c5ffbb0bdc2447f5a3d1ab534c239d7dc14b283a49f5d5dbd2a
1820
}
21+
}
1922
}

‎packages/registry/test/contracts/CannonRegistry.test.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ describe('CannonRegistry', function () {
128128
}, 'InvalidName("0x736f6d652d6d6f64756c652d0000000000000000000000000000000000000000")');
129129
});
130130

131+
it('should not allow more than 5 tags', async function () {
132+
await assertRevert(async () => {
133+
await CannonRegistry.publish(
134+
toBytes32('some-module'),
135+
toBytes32('0.0.1'),
136+
['one', 'two', 'three', 'four', 'five', 'six'].map(toBytes32),
137+
'ipfs://some-module-hash@0.0.1'
138+
);
139+
}, 'TooManyTags()');
140+
});
141+
131142
it('should create the first package and assign the owner', async function () {
132143
const tx = await CannonRegistry.connect(user1).publish(
133144
toBytes32('some-module'),
@@ -181,7 +192,7 @@ describe('CannonRegistry', function () {
181192
const tx = await CannonRegistry.connect(user1).publish(
182193
toBytes32('some-module'),
183194
toBytes32('0.0.3'),
184-
['latest', 'stable'].map((s) => toBytes32(s)),
195+
['latest', 'stable'].map(toBytes32),
185196
'ipfs://updated-module-hash@0.0.3'
186197
);
187198

@@ -235,9 +246,7 @@ describe('CannonRegistry', function () {
235246
);
236247

237248
equal(
238-
await CannonRegistry.getPackageNominatedOwner(
239-
toBytes32('some-module')
240-
),
249+
await CannonRegistry.getPackageNominatedOwner(toBytes32('some-module')),
241250
await user2.getAddress()
242251
);
243252
});
@@ -266,13 +275,6 @@ describe('CannonRegistry', function () {
266275
});
267276
});
268277

269-
describe('getPackages()', function () {
270-
it('returns created packages', async function () {
271-
const result = await CannonRegistry.connect(user2).getPackages();
272-
ok(Array.isArray(result));
273-
});
274-
});
275-
276278
describe('getPackageVersions()', function () {
277279
it('returns package versions', async function () {
278280
const result = await CannonRegistry.connect(user2).getPackageVersions(
@@ -283,6 +285,8 @@ describe('CannonRegistry', function () {
283285
toBytes32('0.0.1'),
284286
toBytes32('0.0.2'),
285287
toBytes32('0.0.3'),
288+
toBytes32('latest'),
289+
toBytes32('stable'),
286290
]);
287291
});
288292
});

0 commit comments

Comments
 (0)