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 committed Feb 21, 2022
1 parent be463db commit 814a317
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
21 changes: 17 additions & 4 deletions source/agora/network/Manager.d
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,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 @@ -535,11 +538,21 @@ public class NetworkManager
&this.onRegisterName, Periodic.Yes);
}

/// Called when validator set is changed
public void onValidatorSetChanged (ref 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++;
}

/// 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 @@ -553,9 +566,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 @@ -1168,6 +1170,11 @@ public class FullNode : API
log.dbg("{}: height {}", __PRETTY_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 @@ -259,7 +259,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
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 814a317

Please sign in to comment.