Skip to content

Allow for programmatic configuration of protocol settings#845

Merged
erikzhang merged 4 commits intoneo-project:masterfrom
devhawk:devhawk/protocol-settings
Jun 20, 2019
Merged

Allow for programmatic configuration of protocol settings#845
erikzhang merged 4 commits intoneo-project:masterfrom
devhawk:devhawk/protocol-settings

Conversation

@devhawk
Copy link
Copy Markdown
Contributor

@devhawk devhawk commented Jun 18, 2019

This PR adds a mechanism to configure the protocol settings programmatically. This enhancement is needed to enable automatic configuration of the privatenet blockchain,

This change adds an Initialize static function to ProtocolSettings, enabling the host process to pass custom protocol configuration information without needing to write a protocol.json file to disk. ASP.NET Configuration already supports multiple configuration providers. The caller of Initialize is free to use whatever configuration provider they wish, including in-memory .NET objects.

If Initialize method is not called, the first time the ProtocolSettings.Default get function is called, protocol configuration is loaded exactly as it loaded today - the protocol.json file is used as a configuration provider if available with mainnet defaults used for any configuration settings not provided. Again, as today these settings are cached for later use.

ProtocolSettings.Initialize must be called before the ProtocolSettings.Default property is ever accessed. Initialize will not overwrite existing loaded protocol settings - regardless if those settings were loaded by an earlier call to Initialize or by the first time the Default property is accessed. CompareExchange is used to ensure protocol settings are only initialized once, even in the face of multiple threads possibly accessing ProtocolSettings.

PR also includes unit tests for ProtocolSettings initialization. All unit tests pass locally.

Copy link
Copy Markdown
Member

@vncoelho vncoelho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such a flexible and well designed PR, @devhawk.
Great job with specific use of C# functions...aehuahuea

As soon as we have NEO 3.0 first preview we can validate this in practice.
PS: I restarted the TRAVIS job because UT looks pass locally as well.

@steven1227
Copy link
Copy Markdown
Member

This is a great Pull Request @devhawk

@erikzhang
Copy link
Copy Markdown
Member

I can't imagine how to switch networks without downtime after a node is running.

IConfigurationSection section = new ConfigurationBuilder().AddJsonFile("protocol.json", true).Build().GetSection("ProtocolConfiguration");
Default = new ProtocolSettings(section);
var settings = new ProtocolSettings(configuration.GetSection("ProtocolConfiguration"));
return null == Interlocked.CompareExchange(ref _default, settings, null);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When it return true? only when change the first time?

Copy link
Copy Markdown
Contributor Author

@devhawk devhawk Jun 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, UpdateDefault will only return true the first time it was called. CompareExchange atomically compares _default to null and stores settings in _default if and only if the comparison succeeds.

I agree with @erikzhang - a node can't switch networks once it is up and running. This code ensures _default's value can only be written once - even if there are multiple threads in the process accessing ProtocolSettings.Default.

@codecov-io
Copy link
Copy Markdown

codecov-io commented Jun 19, 2019

Codecov Report

Merging #845 into master will increase coverage by 0.05%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #845      +/-   ##
==========================================
+ Coverage   38.39%   38.45%   +0.05%     
==========================================
  Files         176      176              
  Lines       12465    12475      +10     
==========================================
+ Hits         4786     4797      +11     
+ Misses       7679     7678       -1
Impacted Files Coverage Δ
neo/ProtocolSettings.cs 94% <100%> (+4%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 70bf2f5...36af0d6. Read the comment docs.

return null == Interlocked.CompareExchange(ref _default, settings, null);
}

public static bool Initialize(IConfiguration configuration)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this Initialize()? UpdateDefault() will be called automatically when accessing Default.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialize is a public method intended to be called by host code. UpdateDefault is a private helper method that does the actual CompareExchange so that I didn't Duplicate code in the Initialize and Default getter code paths.

Copy link
Copy Markdown
Contributor

@igormcoelho igormcoelho Jun 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very useful for testing. This way we can manage to set everything up (using Initialize() before actually starting the other components. We could apply a similar solution to neo-plugins Settings: https://github.com/neo-project/neo-plugins/blob/a72c44813271efcafe0a4fcee2505fae3a3b4716/ApplicationLogs/Settings.cs#L9

Copy link
Copy Markdown
Contributor

@igormcoelho igormcoelho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine for me. As long as it's used once, and before network initialization.
Tests pass, but in the future we need to test this in a more integrated way (perhaps the DI approach @rodoufu).

@erikzhang erikzhang merged commit e844108 into neo-project:master Jun 20, 2019
@devhawk devhawk deleted the devhawk/protocol-settings branch July 21, 2019 20:48
Thacryba pushed a commit to simplitech/neo that referenced this pull request Feb 17, 2020
* 修复该页面在 GitHub 上显示的格式错误

* Update consensus.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants