Skip to content

Commit

Permalink
The validator set add an enrollment before the cycle
Browse files Browse the repository at this point in the history
The validator set should allow a node to re-enroll before the validator cycle
ends. It's not for sure that an enrollment will be contained in the next
new block, so the validator set must add the enrollment earlier before the
cycle ends.

I added the constant about the round that a node can re-enroll because
unnecessary re-enrollments should be avoid. The value can be configurable.

Relates bosagora#824
  • Loading branch information
linked0 committed Jun 23, 2020
1 parent 323b0e7 commit a8fa054
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
46 changes: 41 additions & 5 deletions source/agora/consensus/EnrollmentManager.d
Expand Up @@ -184,11 +184,47 @@ public class EnrollmentManager
***************************************************************************/

public Height getEnrolledHeight (const ref Hash enroll_hash) @trusted
public Height getEnrolledHeight (const ref Hash enroll_hash)
@trusted nothrow
{
return this.validator_set.getEnrolledHeight(enroll_hash);
}

/***************************************************************************
Get the unregistered enrollments in the block
And this is arranged in ascending order with the utxo_key
Params:
enrolls = will contain the unregistered enrollments data if found
height = current block height
Returns:
The unregistered enrollments data
***************************************************************************/

public Enrollment[] getEnrollments (ref Enrollment[] enrolls, Height height)
@trusted nothrow
{
enrolls.length = 0;
assumeSafeAppend(enrolls);

Enrollment[] pool_enrolls;
this.enroll_pool.getEnrollments(pool_enrolls);
import std.stdio;
foreach (enroll; pool_enrolls)
{
const enroll_height = this.getEnrolledHeight(enroll.utxo_key);
if (enroll_height == ulong.max ||
height == enroll_height + this.params.ValidatorCycle - 1)
{
enrolls ~= enroll;
}
}
return enrolls;
}

/***************************************************************************
Add a validator to the validator set or update the enrolled height.
Expand Down Expand Up @@ -750,7 +786,7 @@ unittest
assert(man.pool.count() == 3);

Enrollment[] enrolls;
man.pool.getEnrollments(enrolls);
man.getEnrollments(enrolls, Height(1));
assert(enrolls.length == 3);
assert(enrolls.isStrictlyMonotonic!("a.utxo_key < b.utxo_key"));

Expand All @@ -772,7 +808,7 @@ unittest
assert(man.getEnrolledHeight(enroll.utxo_key) == 9);
assert(man.addValidator(enroll, Height(9), &storage.findUTXO) !is null);
assert(man.getEnrolledHeight(enroll2.utxo_key) == ulong.max);
man.pool.getEnrollments(enrolls);
man.getEnrollments(enrolls, Height(9));
assert(enrolls.length == 1);
// One Enrollment was moved to validator set
assert(man.validator_set.count() == 1);
Expand All @@ -781,7 +817,7 @@ unittest
man.pool.remove(utxo_hash);
man.pool.remove(utxo_hash2);
man.pool.remove(utxo_hash3);
assert(man.pool.getEnrollments(enrolls).length == 0);
assert(man.getEnrollments(enrolls, Height(9)).length == 0);

Enrollment[] ordered_enrollments;
ordered_enrollments ~= enroll;
Expand All @@ -791,7 +827,7 @@ unittest
ordered_enrollments.sort!("a.utxo_key > b.utxo_key");
foreach (ordered_enroll; ordered_enrollments)
assert(man.pool.add(ordered_enroll, &storage.findUTXO));
man.pool.getEnrollments(enrolls);
man.getEnrollments(enrolls, Height(man.params.ValidatorCycle + 8));
assert(enrolls.length == 3);
assert(enrolls.isStrictlyMonotonic!("a.utxo_key < b.utxo_key"));

Expand Down
8 changes: 5 additions & 3 deletions source/agora/node/Ledger.d
Expand Up @@ -336,7 +336,8 @@ public class Ledger
const next_height = Height(this.getBlockHeight() + 1);
auto utxo_finder = this.utxo_set.getUTXOFinder();

this.enroll_man.pool.getEnrollments(data.enrolls);
this.enroll_man.getEnrollments(data.enrolls,
Height(this.getBlockHeight()));
foreach (hash, tx; this.pool)
{
if (auto reason = tx.isInvalidReason(utxo_finder, next_height))
Expand Down Expand Up @@ -1025,7 +1026,7 @@ unittest
enroll_3.cycle_length = validator_cycle;
enroll_3.enroll_sig = sign(node_key_pair_3, signature_noise, enroll_3);

Enrollment[] enrollments ;
Enrollment[] enrollments;
enrollments ~= enroll_1;
enrollments ~= enroll_2;
enrollments ~= enroll_3;
Expand All @@ -1046,7 +1047,8 @@ unittest

// Check if there are any unregistered enrollments
Enrollment[] unreg_enrollments;
assert(ledger.enroll_man.pool.getEnrollments(unreg_enrollments) is null);
assert(ledger.enroll_man.getEnrollments(unreg_enrollments,
ledger.getBlockHeight()) is null);
auto block_4 = ledger.getBlocksFrom(Height(4));
enrollments.sort!("a.utxo_key < b.utxo_key");
assert(block_4[0].header.enrollments == enrollments);
Expand Down

0 comments on commit a8fa054

Please sign in to comment.