-
Notifications
You must be signed in to change notification settings - Fork 220
node: Mode system #853
base: master
Are you sure you want to change the base?
node: Mode system #853
Changes from 38 commits
d1bc7d1
35269f6
51b5514
2ad44ef
ab5447d
8cac90f
c9c3392
029ac42
d4bb9e4
11dabc8
e86ab46
c73623b
2564839
cbd62b3
fd1ec20
7d5ed7f
6427f54
01daf77
8536ff9
4f62c33
f41de1a
de3ccd8
6dd88ef
dd130bb
27ea359
5e0e7f8
d0c0f30
a89a910
04c2d42
fedde51
7aac009
817bae6
7745c8d
f921835
e6e225e
f3fd7aa
91eb460
fd9203f
87feb0f
092d546
d30b8d1
5894885
7e6919c
f83ab4f
9af9e0c
3d44018
13697d7
d38e39e
6a1b2ba
dd69999
6a4df34
2a98ccb
f5f6b8a
e73a42c
ef18644
ced9cc3
1056ec2
e33c868
5dad265
009f802
e79bde9
bcad2a0
86a7174
0bd73ce
842fc07
3d06822
2a3e53e
f1977f0
e12ad2d
6cf1de2
17e009c
cb96042
858a922
9cdedc8
305ab79
c0cb778
456afc9
fc27b27
374acf6
2e07919
9445883
892f2cd
5faefae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| // Copyright (C) 2016-2022 The Neo Project. | ||
| // | ||
| // The neo-cli is free software distributed under the MIT software | ||
| // license, see the accompanying file LICENSE in the main directory of | ||
| // the project or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using Neo.ConsoleService; | ||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Akka.Util.Internal; | ||
| namespace Neo.CLI; | ||
|
|
||
| partial class MainService | ||
| { | ||
| /// <summary> | ||
| /// Process "mode list" command. | ||
| /// </summary> | ||
| [ConsoleCommand("mode list", Category = "Mode Commands")] | ||
| private void OnListModes() | ||
| { | ||
| try | ||
| { | ||
| Directory.GetDirectories("./Modes/").ForEach(p => ConsoleHelper.Info(p)); | ||
| } | ||
| catch (IOException) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Process "mode save" command | ||
| /// <param name="modeName">Mode name</param> | ||
| /// </summary> | ||
| [ConsoleCommand("mode save", Category = "Mode Commands")] | ||
| private void OnSaveMode(string modeName) | ||
| { | ||
| // if no mode name assigned | ||
| if (modeName is null) | ||
| { | ||
| ConsoleHelper.Error("No mode name assigned."); | ||
| return; | ||
| } | ||
| modeName = modeName.ToLower(); | ||
| try | ||
| { | ||
| var targetMode = new DirectoryInfo($"Modes/{modeName}"); | ||
| // Create the mode if it does not exist | ||
| if (!targetMode.Exists) Directory.CreateDirectory(targetMode.FullName); | ||
| // Get the config files of the node | ||
| MoveModeConfig(modeName.ToLower()); | ||
| var plugins = new DirectoryInfo("./Plugins"); | ||
| // Cache directories before we start copying | ||
| var dirs = plugins.GetDirectories(); | ||
| // Save the Plugin files | ||
| foreach (var plugin in dirs) | ||
| { | ||
| foreach (var file in plugin.GetFiles().Where(p => p.Extension == ".json")) | ||
| { | ||
| file.CopyTo($"Modes/{modeName}/{plugin.Name}.json", true); | ||
| } | ||
| } | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Console.WriteLine(e); | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Process "mode delete" command | ||
| /// <param name="modeName">Mode name</param> | ||
| /// </summary> | ||
| [ConsoleCommand("mode delete", Category = "Mode Commands")] | ||
| private void OnDeleteMode(string modeName) | ||
| { | ||
| try | ||
| { | ||
| var dir = new DirectoryInfo($"Modes/{modeName.ToLower()}"); | ||
| if (!dir.Exists) | ||
| return; | ||
| Directory.Delete(dir.FullName, true); | ||
| ConsoleHelper.Info("Mode ", modeName, " was deleted."); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Console.WriteLine(e); | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Load the target mode, plugin should be according to the mode, | ||
| /// if the mode contains the plugin, install the plugin, otherwise delete the plugin | ||
| /// </summary> | ||
| /// <param name="mode"> name of the mode</param> | ||
| /// <exception cref="DirectoryNotFoundException"> if the mode is not found</exception> | ||
| private Task LoadMode(string mode) | ||
| { | ||
| try | ||
| { | ||
| var dir = new DirectoryInfo($"./Modes/{mode.ToLower()}"); | ||
| if (!dir.Exists) | ||
| throw new DirectoryNotFoundException($"Mode not found: {dir.FullName}"); | ||
|
|
||
| // Process the plugin | ||
| var modePlugins = dir.GetFiles(); | ||
|
|
||
| modePlugins.ForEach(async p => | ||
| { | ||
| var pluginName = Path.GetFileNameWithoutExtension(p.Name); | ||
| // if the plugin does not exist, maybe consider install it | ||
| if (p.Name.Contains("config")) return; | ||
| if (!Directory.Exists($"Plugins/{pluginName}/")) | ||
| { | ||
| await InstallPluginAsync(pluginName); | ||
| } | ||
| File.Copy($"Modes/{mode.ToLower()}/{p.Name}", | ||
| $"Plugins/{pluginName}/config.json", true); | ||
| }); | ||
| MoveModeConfig(mode.ToLower(), false); | ||
|
|
||
| // Get existing plugins and delete them if they are not in the mode | ||
| new DirectoryInfo("Plugins/").GetDirectories().ForEach(p => | ||
| { | ||
| if (modePlugins.Any(k => string.Compare(Path.GetFileNameWithoutExtension(k.Name), p.Name, StringComparison.OrdinalIgnoreCase) == 0)) return; | ||
| if (!File.Exists($"Plugins/{p.Name}/config.json")) return; | ||
| try | ||
| { | ||
| ConsoleHelper.Info("Removing plugin ", p.Name); | ||
| Directory.Delete($"Plugins/{p.Name}", true); | ||
| } | ||
| catch | ||
| { | ||
| // ignored | ||
| } | ||
| }); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Console.WriteLine(e); | ||
| throw; | ||
| } | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| /// save config.json and config.fs.json to the mode directory | ||
| /// <param name="mode"> name of the mode</param> | ||
| /// <param name="toMode"></param> | ||
| /// <exception cref="DirectoryNotFoundException"> if the mode is not found</exception> | ||
| private static void MoveModeConfig(string mode, bool toMode = true) | ||
| { | ||
| var modeDir = new DirectoryInfo($"./Modes/{mode.ToLower()}"); | ||
| var configDir = new DirectoryInfo("./"); | ||
| if (!modeDir.Exists) | ||
| throw new DirectoryNotFoundException($"Mode not found: {modeDir.FullName}"); | ||
| try | ||
| { | ||
| if (toMode) | ||
| { | ||
| File.Copy(configDir.FullName + "/config.json", | ||
| modeDir.FullName + "/config.json", true); | ||
| File.Copy(configDir.FullName + "/config.fs.json", | ||
| modeDir.FullName + "/config.fs.json", true); | ||
| } | ||
| else | ||
| { | ||
| File.Copy(modeDir.FullName + "/config.json", | ||
| configDir.FullName + "/config.json", true); | ||
| File.Copy(modeDir.FullName + "/config.fs.json", | ||
| configDir.FullName + "/config.fs.json", true); | ||
| } | ||
Jim8y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| catch (Exception e) | ||
| { | ||
| Console.WriteLine(e); | ||
| throw; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| // Copyright (C) 2016-2022 The Neo Project. | ||
| // The neo-cli is free software distributed under the MIT software | ||
| // The neo-cli is free software distributed under the MIT software | ||
| // license, see the accompanying file LICENSE in the main directory of | ||
| // the project or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
|
|
@@ -20,6 +20,7 @@ | |
| using System.Net.Http; | ||
| using System.Security.Cryptography; | ||
| using System.Threading.Tasks; | ||
| using Akka.Util.Internal; | ||
|
|
||
| namespace Neo.CLI | ||
| { | ||
|
|
@@ -141,6 +142,18 @@ private async Task InstallPluginAsync(string pluginName, HashSet<string> install | |
| await InstallDependenciesAsync(es, installed); | ||
| } | ||
| zip.ExtractToDirectory("./", true); | ||
| Console.WriteLine(); | ||
| // Save the config.json to current mode | ||
| try | ||
| { | ||
| // what if the file already exists in the mode? OK, lets overwrite it then. | ||
| File.Copy($"Plugins/{pluginName}/config.json", $"Modes/{_currentMode}/{pluginName}.json", true); | ||
|
||
| } | ||
| catch (Exception e) | ||
| { | ||
| // ignored | ||
| Console.WriteLine(e.Message); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -173,7 +186,8 @@ private async Task InstallDependenciesAsync(Stream config, HashSet<string> insta | |
| /// <returns></returns> | ||
| private static bool PluginExists(string pluginName) | ||
| { | ||
| return Plugin.Plugins.Any(p => p.Name.Equals(pluginName, StringComparison.InvariantCultureIgnoreCase)); | ||
| return Plugin.Plugins.Any(p => p.Name.Equals(pluginName, StringComparison.InvariantCultureIgnoreCase)) || | ||
| new DirectoryInfo("Plugins").GetDirectories().Any(p => p.Name.Equals(pluginName, StringComparison.InvariantCultureIgnoreCase)); | ||
Jim8y marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// <summary> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"extra":{},"scrypt":{"r":8,"p":8,"n":16384,"size":64},"name":"NbVj8GhwToNv4WF2gVaoco6hbkMQ8hrHWP","accounts":[{"address":"NbVj8GhwToNv4WF2gVaoco6hbkMQ8hrHWP","extra":{},"lock":false,"contract":{"script":"DCED7s02MiDW5N55Or\/rrgl3l2hGjFmjtuATKp6jsiAQ99hBVuezJw==","deployed":false,"parameters":[{"name":"signature","type":"signature"}]},"label":"NbVj8GhwToNv4WF2gVaoco6hbkMQ8hrHWP","key":"6PYTrLoLMT1Ms1H2msFTGUDRhHKXsZWA2Lj5rcF2GdPJju1Nuqy4npbef4","isDefault":true}],"version":"1.0"} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| { | ||
| "ApplicationConfiguration": { | ||
| "Logger": { | ||
| "Path": "Logs", | ||
| "ConsoleOutput": false, | ||
| "Active": false | ||
| }, | ||
| "Storage": { | ||
| "Engine": "LevelDBStore", | ||
| "Path": "Data_LevelDB_{0}" | ||
| }, | ||
| "P2P": { | ||
| "Port": 40333, | ||
| "WsPort": 40334, | ||
| "MinDesiredConnections": 10, | ||
| "MaxConnections": 40, | ||
| "MaxConnectionsPerAddress": 3 | ||
| }, | ||
| "UnlockWallet": { | ||
| "Path": "", | ||
| "Password": "", | ||
| "IsActive": false | ||
| } | ||
| }, | ||
| "ProtocolConfiguration": { | ||
| "Network": 91414437, | ||
| "AddressVersion": 53, | ||
| "MillisecondsPerBlock": 15000, | ||
| "MaxTransactionsPerBlock": 512, | ||
| "MemoryPoolMaxTransactions": 50000, | ||
| "MaxTraceableBlocks": 2102400, | ||
| "InitialGasDistribution": 5200000000000000, | ||
| "ValidatorsCount": 7, | ||
| "StandbyCommittee": [ | ||
| "026fa34ec057d74c2fdf1a18e336d0bd597ea401a0b2ad57340d5c220d09f44086", | ||
| "039a9db2a30942b1843db673aeb0d4fd6433f74cec1d879de6343cb9fcf7628fa4", | ||
| "0366d255e7ce23ea6f7f1e4bedf5cbafe598705b47e6ec213ef13b2f0819e8ab33", | ||
| "023f9cb7bbe154d529d5c719fdc39feaa831a43ae03d2a4280575b60f52fa7bc52", | ||
| "039ba959e0ab6dc616df8b803692f1c30ba9071b76b05535eb994bf5bbc402ad5f", | ||
| "035a2a18cddafa25ad353dea5e6730a1b9fcb4b918c4a0303c4387bb9c3b816adf", | ||
| "031f4d9c66f2ec348832c48fd3a16dfaeb59e85f557ae1e07f6696d0375c64f97b" | ||
| ], | ||
| "SeedList": [ | ||
| "morph1.fs.neo.org:40333", | ||
| "morph2.fs.neo.org:40333", | ||
| "morph3.fs.neo.org:40333", | ||
| "morph4.fs.neo.org:40333", | ||
| "morph5.fs.neo.org:40333", | ||
| "morph6.fs.neo.org:40333", | ||
| "morph7.fs.neo.org:40333" | ||
| ] | ||
| } | ||
| } |

Uh oh!
There was an error while loading. Please reload this page.