Skip to content

Commit 27aa003

Browse files
committed
NameService: replace root with TLD
And move commonly used code to a separate methods. Originally introduced in nspcc-dev/neofs-contract@4b86891.
1 parent 0c57843 commit 27aa003

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

src/NameService/NameService.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,6 @@ public static void Update(ByteString nef, string manifest)
140140
ContractManagement.Update(nef, manifest);
141141
}
142142

143-
public static void AddRoot(string root)
144-
{
145-
CheckCommittee();
146-
if (!CheckFragment(root, true))
147-
throw new FormatException("The format of the root is incorrect.");
148-
StorageMap rootMap = new(Storage.CurrentContext, Prefix_Root);
149-
if (rootMap[root] is not null)
150-
throw new InvalidOperationException("The root already exists.");
151-
rootMap.Put(root, 0);
152-
}
153-
154143
[Safe]
155144
public static Iterator Roots()
156145
{
@@ -188,7 +177,10 @@ public static bool IsAvailable(string name)
188177
StorageMap nameMap = new(context, Prefix_Name);
189178
string[] fragments = SplitAndCheck(name, false);
190179
if (fragments is null) throw new FormatException("The format of the name is incorrect.");
191-
if (rootMap[fragments[^1]] is null) throw new Exception("The root does not exist.");
180+
if (rootMap[fragments[^1]] is null) {
181+
if (fragments.Length != 1) throw new InvalidOperationException("The TLD is not found");
182+
return true;
183+
}
192184
long price = GetPrice((byte)fragments[0].Length);
193185
if (price < 0) return false;
194186
return parentExpired(nameMap, 0, fragments);
@@ -203,11 +195,18 @@ public static bool Register(string name, UInt160 owner)
203195
StorageMap nameMap = new(context, Prefix_Name);
204196
string[] fragments = SplitAndCheck(name, false);
205197
if (fragments is null) throw new FormatException("The format of the name is incorrect.");
206-
if (rootMap[fragments[^1]] is null) throw new Exception("The root does not exist.");
207-
if (parentExpired(nameMap, 1, fragments)) throw new InvalidOperationException("One of the parent domains has expired.");
208-
ByteString parentKey = GetKey(fragments[1]);
209-
NameState parent = (NameState)StdLib.Deserialize(nameMap[parentKey]);
210-
parent.CheckAdmin();
198+
ByteString tld = rootMap[fragments[^1]];
199+
if (fragments.Length == 1) {
200+
CheckCommittee();
201+
if (tld is not null) throw new InvalidOperationException("TLD already exists.");
202+
rootMap.Put(fragments[^1], 0);
203+
} else {
204+
if (tld is null) throw new InvalidOperationException("TLD does not exist.");
205+
if (parentExpired(nameMap, 1, fragments)) throw new InvalidOperationException("One of the parent domains has expired.");
206+
ByteString parentKey = GetKey(fragments[1]);
207+
NameState parent = (NameState)StdLib.Deserialize(nameMap[parentKey]);
208+
parent.CheckAdmin();
209+
}
211210
if (!Runtime.CheckWitness(owner)) throw new InvalidOperationException("No authorization.");
212211
long price = GetPrice((byte)fragments[0].Length);
213212
if (price < 0)
@@ -505,7 +504,7 @@ private static string[] SplitAndCheck(string name, bool allowMultipleFragments)
505504
if (length < 3 || length > NameMaxLength) return null;
506505
string[] fragments = StdLib.StringSplit(name, ".");
507506
length = fragments.Length;
508-
if (length < 2 || length > 8) return null;
507+
if (length > 8) return null;
509508
if (length > 2 && !allowMultipleFragments) return null;
510509
for (int i = 0; i < length; i++)
511510
if (!CheckFragment(fragments[i], i == length - 1))

0 commit comments

Comments
 (0)