Skip to content

Commit

Permalink
Manager: Remove non-enrolled validators from peer list
Browse files Browse the repository at this point in the history
Will work both when a validator is no longer enrolled or changes
its stake.
  • Loading branch information
omerfirmak authored and Geod24 committed Feb 24, 2022
1 parent 8560b14 commit 5947214
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
24 changes: 20 additions & 4 deletions source/agora/network/Manager.d
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ public class NetworkManager

protected agora.api.FullNode.API owner_node;

/// Most recent validator set
protected UTXO[Hash] last_known_validator_utxos;

/// Ctor
public this (in Config config, ManagedDatabase cache, ITaskManager taskman, Clock clock, agora.api.FullNode.API owner_node)
{
Expand Down Expand Up @@ -525,11 +528,24 @@ public class NetworkManager
&this.onRegisterName, Periodic.Yes);
}

/// Called when validator set is changed
public void onValidatorSetChanged (UTXO[Hash] validator_utxos) @trusted
{
this.last_known_validator_utxos = validator_utxos;

foreach (val; this.validators())
if (val.identity.utxo !in this.last_known_validator_utxos)
if (this.peer_list.linearRemoveElement(val))
{
this.peer_list_version++;
val.shutdown();
}
}

/// Discover the network, connect to all required peers
/// Some nodes may want to connect to specific peers before
/// discovery() is considered complete
public void discover (UTXO[Hash] last_known_validator_utxos,
UTXO[Hash] required_peer_utxos = null) nothrow
public void discover (UTXO[Hash] required_peer_utxos = null) nothrow
{
this.quorum_set_keys.from(Set!Hash.init);
this.required_peers.from(Set!Hash.init);
Expand All @@ -543,9 +559,9 @@ public class NetworkManager

log.trace(
"Doing periodic network discovery: {} required peers requested, {} missing, known {}",
required_peer_utxos.length, this.required_peers.length, last_known_validator_utxos.length);
required_peer_utxos.length, this.required_peers.length, this.last_known_validator_utxos.length);

foreach (utxo; last_known_validator_utxos.byKeyValue)
foreach (utxo; this.last_known_validator_utxos.byKeyValue)
{
if (!this.peers.map!(ni => ni.identity.utxo).canFind(utxo.key))
{
Expand Down
9 changes: 8 additions & 1 deletion source/agora/node/FullNode.d
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ public class FullNode : API
this.ledger = this.makeLedger();

this.network = this.makeNetworkManager();
auto validator_set = this.ledger.getEnrolledUTXOs();
this.network.onValidatorSetChanged(validator_set);
this.transaction_relayer = this.makeTransactionRelayer();

Utils.getCollectorRegistry().addCollector(&this.collectAppStats);
Expand Down Expand Up @@ -525,7 +527,7 @@ public class FullNode : API

protected void discoveryTask () nothrow
{
this.network.discover(this.ledger.getEnrolledUTXOs());
this.network.discover();
this.startTaskTimer(TimersIdx.Discovery, this.config.node.network_discovery_interval);
}

Expand Down Expand Up @@ -1189,6 +1191,11 @@ public class FullNode : API
log.dbg("{}: height {}", __FUNCTION__, block.header.height);
this.pushBlock(block);
this.registry.onAcceptedBlock(block, validators_changed);
if (validators_changed)
{
auto validator_set = this.ledger.getEnrolledUTXOs();
this.network.onValidatorSetChanged(validator_set);
}
}

/***************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion source/agora/node/Validator.d
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public class Validator : FullNode, API
/// Ditto
protected override void discoveryTask ()
{
this.network.discover(this.ledger.getEnrolledUTXOs(), this.required_peer_utxos);
this.network.discover(this.required_peer_utxos);
this.timers[TimersIdx.Discovery].rearm(this.config.node.network_discovery_interval, false);
}

Expand Down
2 changes: 1 addition & 1 deletion source/agora/test/Quorum.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ unittest
TestConf conf = { outsider_validators : 3,
recurring_enrollment : false,
};
conf.node.network_discovery_interval = 2.seconds;
conf.node.network_discovery_interval = 1.seconds;
conf.node.retry_delay = 250.msecs;
auto network = makeTestNetwork!TestAPIManager(conf);
network.start();
Expand Down
3 changes: 2 additions & 1 deletion source/agora/test/RestoreSlashingInfo.d
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ unittest
recurring_enrollment : false
};
conf.node.timeout = 10.seconds;
conf.node.network_discovery_interval = 2.seconds;
conf.node.network_discovery_interval = 1.seconds;
conf.node.retry_delay = 250.msecs;
conf.node.min_listeners = 4;
auto network = makeTestNetwork!TestAPIManager(conf);
network.start();
scope(exit) network.shutdown();
Expand Down
1 change: 1 addition & 0 deletions source/agora/test/ValidatorRecurringEnrollment.d
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ unittest

TestConf conf;
conf.consensus.quorum_threshold = 100;
conf.node.network_discovery_interval = 1.seconds;

auto network = makeTestNetwork!(TestNetwork!SocialDistancingValidator)(conf);
network.start();
Expand Down

0 comments on commit 5947214

Please sign in to comment.