Skip to content

Fixing Config endpoint and adding preferred peers#196

Merged
davidtavarez merged 34 commits intomasterfrom
config/preferred-peers
Apr 19, 2022
Merged

Fixing Config endpoint and adding preferred peers#196
davidtavarez merged 34 commits intomasterfrom
config/preferred-peers

Conversation

@davidtavarez
Copy link
Copy Markdown
Member

@davidtavarez davidtavarez commented Feb 9, 2022

@davidtavarez davidtavarez self-assigned this Feb 9, 2022
config_json["min_confirmations"] = config.GetMinimumConfirmations();

return request.BuildResult(config_json);
result["Ok"] = config_json;
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.

Why this? The "Ok" response is completely redundant, and goes against the jsonrpc 2.0 spec. I'm aware that the rust repo does that, but it's completely wrong to do so.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

true, you're correct 👍🏽

{
pConfig = Config::Load(config_path, environment);
if(config_path.has_value()){
pConfig->SetConfigPath(config_path.value());
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.

It's technically breaking the pattern by including config path in the config itself, since it's not actually part of the config. If you're going to though, you should at least move the calls to these setters into Config::Load, so other consumers get it for free. Also, this allows you to avoid duplicating the path calculation logic on lines 72-73

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

could src\Core\Global.cpp be a better place? let's not break the pattern.

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.

Yep, 2 options come to mind:

  1. Store it as a new field in global with a getter/setter
  2. Wrap ConfigPtr with a new class (ConfigFile? LoadedConfig?) that has 2 fields: path & values(the ConfigPtr). Then store that in global instead of the ConfigPtr.

@davidtavarez davidtavarez marked this pull request as ready for review February 11, 2022 00:37
uint8_t Config::GetMinSyncPeers() const noexcept { return m_pImpl->m_nodeConfig.GetP2P().GetMinSyncPeers(); }

const std::vector<std::string>& Config::GetPreferredPeers() const noexcept { return m_pImpl->m_nodeConfig.GetP2P().GetPreferredPeers(); }
const std::vector<std::string>& Config::GetAlloweddPeers() const noexcept { return m_pImpl->m_nodeConfig.GetP2P().GetAllowedPeers(); }
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.

I'm a fan of double D's, but not at the end of the word 'allowed'

Json::Value peers = p2pJSON.get(ConfigProps::P2P::PREFERRED_PEERS, Json::Value(Json::nullValue));
for (auto& peer : peers) {
m_peferredPeers.push_back(peer.asString());
}
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.

Formatting

@davidtavarez
Copy link
Copy Markdown
Member Author

While testing, I'm constantly getting this error from the peers:

Error: The I/O operation has been aborted because of either a thread exit or an application request.

I would appreciate suggestions on how to properly debug that.

));

if(Global::GetConfig().GetBlockedPeers().size() > 0 &&
Global::GetConfig().IsPeerBlocked(pSocket->GetIPAddress().GetAddress().to_string()))
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.

No need to check size first

return;
}

if(Global::GetConfig().GetAlloweddPeers().size() > 0 &&
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.

IsPeerAllowed should do the empty check

Copy link
Copy Markdown
Member

@DavidBurkett DavidBurkett left a comment

Choose a reason for hiding this comment

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

Couple small comments, but other than that, looks good

@DavidBurkett
Copy link
Copy Markdown
Member

While testing, I'm constantly getting this error from the peers:

Error: The I/O operation has been aborted because of either a thread exit or an application request.

I would appreciate suggestions on how to properly debug that.

I'll message you on TG tmrw

return false;
}

bool Config::IsPeerPreferred (const std::string peer) {
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.

std::string => std::string&

nit: Unnecessary space between IsPeerPreferred and (

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.

Better yet, take in a const IPAddress& instead, and store IPAddresss when parsing the config.

}

bool Config::IsPeerPreferred (const std::string peer) {
std::vector<std::string> vec = m_pImpl->m_nodeConfig.GetP2P().GetPreferredPeers();
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.

You need to store this variable as a const std::vector<std::string>& , or else this will be doing an expensive copy on every single usage.

Also, since you're always treating preferred peers as a set, you should be storing it as one (actually, a std::unordered_set, which tends to have an even faster lookup time)


const std::string& ipAddressStr = ipAddress.GetAddress().to_string();

if(Global::GetConfig().IsPeerBlocked(ipAddressStr)) {
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.

nit: C++ devs nearly always include a space between if and (. I would recommend sticking with that formatting standard.


if(Global::GetConfig().IsPeerBlocked(pSocket->GetIPAddress().GetAddress().to_string())) {
LOG_TRACE_F("peer is blocked: {}", pPeer);
const std::string& ipAddressStr = pSocket->GetIPAddress().GetAddress().to_string();
Copy link
Copy Markdown
Member

@DavidBurkett DavidBurkett Feb 11, 2022

Choose a reason for hiding this comment

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

to_string() returns a std::string not a const std::string&, so you shouldn't store it as a reference (I'm surrprised there's no compiler warning). If you follow my suggestion to change the IsPeerXXX functions to take in a const IPAddress& instead, then you can just store pSocket->GetIPAddress() instead, which does return a const reference.

@davidtavarez
Copy link
Copy Markdown
Member Author

File used to test this PR:

{
	"DATA_PATH" : "D:\\Downloads\\Grin",
	"P2P" : 
	{
		"MAX_PEERS" : 6,
		"MIN_PEERS" : 5,
		"PREFERRED_PEERS" : 
		[
			"217.160.242.228",
			"217.160.66.118",
			"212.227.210.43",
			"93.90.194.26",
			"82.165.107.169",
			"107.174.63.210",
			"grinpp01.grinnode.live",
			"grinpp02.grinnode.live",
			"grinpp03.grinnode.live"
		]
	},
	"WALLET" : 
	{
		"DATABASE" : "SQLITE",
		"MIN_CONFIRMATIONS" : 10
	}
}

@davidtavarez davidtavarez force-pushed the config/preferred-peers branch from 478b25d to 9f0f930 Compare April 5, 2022 19:05
@davidtavarez davidtavarez force-pushed the config/preferred-peers branch from 0862e86 to de66615 Compare April 5, 2022 20:00
@davidtavarez davidtavarez force-pushed the config/preferred-peers branch from de66615 to 9f0f930 Compare April 10, 2022 15:00
@davidtavarez
Copy link
Copy Markdown
Member Author

@DavidBurkett would you mind to do a final review to this PR. I don't I will add nothing else to this PR. I've been testing on Windows, Linux and MacOS.

128kb as buffet size for downloading the TxHashSet file (

static const int BUFFER_SIZE = 256 * 1024;
) seems to be reasonably to me due the amount of slow peers, but I'm not fully convinced yet.

@davidtavarez davidtavarez merged commit 60fe568 into master Apr 19, 2022
@davidtavarez davidtavarez deleted the config/preferred-peers branch April 19, 2022 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants