Skip to content

Commit

Permalink
Ledger: Rename 'getBlockHeight' to just 'height'
Browse files Browse the repository at this point in the history
Because 'getBlockHeight' is overly verbose and doesn't do justice to the
fact the the last block height is a quasi-property of the Ledger.
  • Loading branch information
Geod24 authored and omerfirmak committed Jan 19, 2022
1 parent 845774e commit c789fd2
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 74 deletions.
44 changes: 22 additions & 22 deletions source/agora/consensus/Ledger.d
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public class NodeLedger : Ledger
in ubyte double_spent_threshold_pct = 0,
in ushort min_fee_pct = 0) @safe
{
const Height expected_height = this.getBlockHeight() + 1;
const Height expected_height = this.height() + 1;
auto tx_hash = hashFull(tx);

// If we were looking for this TX, stop
Expand Down Expand Up @@ -306,7 +306,7 @@ public class NodeLedger : Ledger
scope UTXOFinder utxo_finder)
@safe nothrow
{
const expect_height = this.getBlockHeight() + 1;
const expect_height = this.height() + 1;
bool[Hash] local_unknown_txs;

Amount tot_fee, tot_data_fee;
Expand Down Expand Up @@ -378,7 +378,7 @@ public class NodeLedger : Ledger
public string validateConsensusData (in ConsensusData data,
in uint[] initial_missing_validators) @trusted nothrow
{
const validating = this.getBlockHeight() + 1;
const validating = this.height() + 1;
auto utxo_finder = this.utxo_set.getUTXOFinder();

Transaction[] tx_set;
Expand Down Expand Up @@ -732,7 +732,7 @@ public class ValidatingLedger : NodeLedger
public void prepareNominatingSet (out ConsensusData data)
@safe
{
const next_height = this.getBlockHeight() + 1;
const next_height = this.height() + 1;

auto utxo_finder = this.utxo_set.getUTXOFinder();
data.enrolls = this.getCandidateEnrollments(next_height, utxo_finder);
Expand Down Expand Up @@ -870,7 +870,7 @@ public class ValidatingLedger : NodeLedger
/// simulate block creation as if a nomination and externalize round completed
public void forceCreateBlock (bool sign_all = true)
{
const next_block = this.getBlockHeight() + 1;
const next_block = this.height() + 1;
this.simulatePreimages(next_block);
ConsensusData data;
this.prepareNominatingSet(data);
Expand All @@ -880,7 +880,7 @@ public class ValidatingLedger : NodeLedger
// In which case, we simply re-enroll the validators already enrolled
if (data.enrolls.length == 0 && this.validatorCount(next_block + 1) == 0)
{
auto validators = this.getValidators(this.getBlockHeight());
auto validators = this.getValidators(this.height());
foreach (v; validators)
{
auto kp = WK.Keys[v.address];
Expand All @@ -893,7 +893,7 @@ public class ValidatingLedger : NodeLedger
if (auto reason = this.externalize(data, sign_all))
{
assert(0, format!"Failure in unit test. Block %s should have been externalized: %s"(
this.getBlockHeight() + 1, reason));
this.height() + 1, reason));
}
}

Expand All @@ -903,7 +903,7 @@ public class ValidatingLedger : NodeLedger
// Special case for genesis
if (!last_txs.length)
{
assert(this.getBlockHeight() == 0);
assert(this.height() == 0);

last_txs = genesisSpendable().enumerate()
.map!(en => en.value.refund(WK.Keys.A.address).sign())
Expand Down Expand Up @@ -1010,7 +1010,7 @@ version (unittest)
unittest
{
scope ledger = new TestLedger(WK.Keys.NODE3);
assert(ledger.getBlockHeight() == 0);
assert(ledger.height() == 0);

auto blocks = ledger.getBlocksFrom(Height(0)).take(10);
assert(blocks[$ - 1] == ledger.params.Genesis);
Expand All @@ -1029,7 +1029,7 @@ unittest

/// now generate 98 more blocks to make it 100 + genesis block (101 total)
genBlockTransactions(98);
assert(ledger.getBlockHeight() == 100);
assert(ledger.height() == 100);

blocks = ledger.getBlocksFrom(Height(0)).takeExactly(10);
assert(blocks[0] == ledger.params.Genesis);
Expand Down Expand Up @@ -1315,7 +1315,7 @@ unittest
.array;
txs.each!(tx => assert(ledger.acceptTransaction(tx) is null));
ledger.forceCreateBlock();
assert(ledger.getBlockHeight() == 1);
assert(ledger.height() == 1);

// Create data with nomal size
ubyte[] data;
Expand All @@ -1335,7 +1335,7 @@ unittest
.array;
txs.each!(tx => assert(ledger.acceptTransaction(tx) is null));
ledger.forceCreateBlock();
assert(ledger.getBlockHeight() == 2);
assert(ledger.height() == 2);
auto blocks = ledger.getBlocksFrom(Height(0)).take(10).array;
assert(blocks.length == 3);
assert(blocks[2].header.height == 2);
Expand All @@ -1356,7 +1356,7 @@ unittest
.array;
txs.each!(tx => assert(ledger.acceptTransaction(tx) is null));
ledger.forceCreateBlock();
assert(ledger.getBlockHeight() == 3);
assert(ledger.height() == 3);
blocks = ledger.getBlocksFrom(Height(0)).take(10).array;
assert(blocks.length == 4);
assert(blocks[3].header.height == 3);
Expand Down Expand Up @@ -1396,7 +1396,7 @@ unittest
.map!(en => en.value.refund(WK.Keys[en.index].address).sign()).array;
txs.each!(tx => assert(ledger.acceptTransaction(tx) is null));
ledger.forceCreateBlock();
assert(ledger.getBlockHeight() == 1);
assert(ledger.height() == 1);

// generate a block with only freezing transactions
auto new_txs = txs[0 .. 4].enumerate()
Expand All @@ -1408,7 +1408,7 @@ unittest
new_txs ~= TxBuilder(txs[$ - 1]).split(WK.Keys[0].address.repeat(8)).sign();
new_txs.each!(tx => assert(ledger.acceptTransaction(tx) is null));
ledger.forceCreateBlock();
assert(ledger.getBlockHeight() == 2);
assert(ledger.height() == 2);

// UTXOs for enrollments
Hash[] utxos = [
Expand All @@ -1424,12 +1424,12 @@ unittest
.array;
new_txs.each!(tx => assert(ledger.acceptTransaction(tx) is null));
ledger.forceCreateBlock();
assert(ledger.getBlockHeight() == 3);
assert(ledger.height() == 3);

foreach (height; 4 .. params.ValidatorCycle)
{
new_txs = genGeneralBlock(new_txs);
assert(ledger.getBlockHeight() == Height(height));
assert(ledger.height() == Height(height));
}

// add four new enrollments
Expand All @@ -1453,13 +1453,13 @@ unittest

// create the last block of the cycle to make the `Enrollment`s enrolled
new_txs = genGeneralBlock(new_txs);
assert(ledger.getBlockHeight() == Height(20));
assert(ledger.height() == Height(20));
auto b20 = ledger.getBlocksFrom(Height(20))[0];
assert(b20.header.enrollments.length == 4);

// block 21
new_txs = genGeneralBlock(new_txs);
assert(ledger.getBlockHeight() == Height(21));
assert(ledger.height() == Height(21));

// check missing validators not revealing pre-images.
auto temp_txs = genTransactions(new_txs);
Expand Down Expand Up @@ -1600,7 +1600,7 @@ unittest
}

assert(ledger.externalize(data) is null);
assert(ledger.getBlockHeight() == blocks.length);
assert(ledger.height() == blocks.length);
blocks ~= ledger.getBlocksFrom(Height(blocks.length))[0];

auto cb_txs = blocks[$-1].txs.filter!(tx => tx.isCoinbase).array;
Expand Down Expand Up @@ -1799,7 +1799,7 @@ unittest

// should be able to create a new block after previous height receives majority signatures
ledger.forceCreateBlock();
assert(ledger.getBlockHeight() == 2);
assert(ledger.height() == 2);
}

// test enrollment sigs and request for a future height
Expand All @@ -1824,7 +1824,7 @@ unittest
assert(ledger.enrollment_manager.addEnrollment(enrollment, kp.address,
Height(5), &ledger.peekUTXO, &ledger.getPenaltyDeposit));

while (ledger.getBlockHeight() != Height(5))
while (ledger.height() != Height(5))
ledger.forceCreateBlock();
assert(ledger.lastBlock.header.height == Height(5));
assert(ledger.lastBlock.header.enrollments.canFind(enrollment));
Expand Down
16 changes: 8 additions & 8 deletions source/agora/consensus/protocol/Nominator.d
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ extern(D):
// we stop the nomination and set the new quorum set.
if(this.is_nominating)
{
const height = this.ledger.getBlockHeight();
const height = this.ledger.height();
try
{
() @trusted {
Expand Down Expand Up @@ -431,7 +431,7 @@ extern(D):
protected ulong getExpectedBlockTime () @safe @nogc nothrow pure
{
return this.params.GenesisTimestamp +
(ledger.getBlockHeight() + 1) * this.params.BlockInterval.total!"seconds";
(ledger.height() + 1) * this.params.BlockInterval.total!"seconds";
}

/***************************************************************************
Expand All @@ -449,7 +449,7 @@ extern(D):

protected void checkNominate () @safe
{
const slot_idx = this.ledger.getBlockHeight() + 1;
const slot_idx = this.ledger.height() + 1;
// are we done nominating this round
if (this.last_confirmed_height >= slot_idx)
{
Expand Down Expand Up @@ -479,11 +479,11 @@ extern(D):
return;
}

if (!this.ledger.hasMajoritySignature(this.ledger.getBlockHeight()))
if (!this.ledger.hasMajoritySignature(this.ledger.height()))
{
this.log.trace(
"checkNominate(): Last block ({}) doesn't have majority signatures, signed={}",
this.ledger.getBlockHeight(), this.ledger.lastBlock().header.validators);
this.ledger.height(), this.ledger.lastBlock().header.validators);
return;
}

Expand Down Expand Up @@ -709,7 +709,7 @@ extern(D):

public const(BlockHeader) receiveBlockSignature (in ValidatorBlockSig block_sig) @safe
{
const cur_height = this.ledger.getBlockHeight();
const cur_height = this.ledger.height();
log.trace("Received BLOCK SIG {} from node {} for block {}",
block_sig.signature, block_sig.utxo, block_sig.height);
if (block_sig.height > cur_height)
Expand Down Expand Up @@ -903,7 +903,7 @@ extern(D):
}
}

const last_height = this.ledger.getBlockHeight();
const last_height = this.ledger.height();
if (last_height + 1 == slot_idx) // Let's check last block is still one before this one
{
if (!this.ledger.hasMajoritySignature(last_height))
Expand Down Expand Up @@ -934,7 +934,7 @@ extern(D):
ref const(Value) value) nothrow
{
Height height = Height(slot_idx);
const Height last_height = this.ledger.getBlockHeight();
const Height last_height = this.ledger.height();
if (height != last_height + 1)
{
log.trace("valueExternalized: Will not externalize envelope with slot id {} as ledger is at height {}",
Expand Down
26 changes: 13 additions & 13 deletions source/agora/consensus/state/Ledger.d
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public class Ledger
***************************************************************************/

public Height getBlockHeight () const scope @safe @nogc nothrow pure
public Height height () const scope @safe @nogc nothrow pure
{
return this.last_block.header.height;
}
Expand All @@ -228,7 +228,7 @@ public class Ledger
Params:
height = Height at which to query the validator set.
Accurate results are only guaranteed for
`height <= this.getBlockHeight() + 1`.
`height <= this.height() + 1`.
empty = Whether to allow an empty return value.
By default, this function will throw if there is no validators
at `height`. If `true` is passed, it will not.
Expand Down Expand Up @@ -268,7 +268,7 @@ public class Ledger
Params:
height = Height at which to query the validator set.
Accurate results are only guaranteed for
`height <= this.getBlockHeight() + 1`.
`height <= this.height() + 1`.
Returns:
Number of active validators at `height`.
Expand Down Expand Up @@ -351,7 +351,7 @@ public class Ledger
? null
: "Trying to externalize a different block for the latest height";

if (!this.hasMajoritySignature(this.getBlockHeight()))
if (!this.hasMajoritySignature(this.height()))
return "Previous height does not have majority signature";

if (auto fail_reason = this.validateBlock(block))
Expand Down Expand Up @@ -916,7 +916,7 @@ public class Ledger
/// Ditto
public bool hasMajoritySignature (Height height) @safe nothrow
{
if (height > this.getBlockHeight())
if (height > this.height())
return false;
else if (height == this.last_block.header.height) // most common case
return this.hasMajoritySignature(this.last_block.header);
Expand All @@ -941,11 +941,11 @@ public class Ledger

public auto getBlocksFrom (Height start_height) @safe nothrow
{
start_height = min(start_height, this.getBlockHeight() + 1);
start_height = min(start_height, this.height() + 1);

// Call to `Height.value` to work around
// https://issues.dlang.org/show_bug.cgi?id=21583
return iota(start_height.value, this.getBlockHeight() + 1)
return iota(start_height.value, this.height() + 1)
.map!(idx => this.storage.readBlock(Height(idx)));
}

Expand All @@ -963,15 +963,15 @@ public class Ledger
Returns:
A newly created block based on the current block
(See `Ledger.lastBlock()` and `Ledger.getBlockHeight()`)
(See `Ledger.lastBlock()` and `Ledger.height()`)
***************************************************************************/

public Block buildBlock (Transactions) (Transactions txs,
Enrollment[] enrollments, uint[] missing_validators)
@safe
{
const height = this.getBlockHeight() + 1;
const height = this.height() + 1;
const validators = this.getValidators(height);

Hash[] preimages = validators.enumerate.map!(
Expand All @@ -998,7 +998,7 @@ public class Ledger
/// return the last paid out block before the current block
public Height getLastPaidHeight () const scope @safe @nogc nothrow pure
{
return lastPaidHeight(this.getBlockHeight, this.params.PayoutPeriod);
return lastPaidHeight(this.height, this.params.PayoutPeriod);
}

/***************************************************************************
Expand Down Expand Up @@ -1037,7 +1037,7 @@ public class Ledger
/// Ditto
public UTXO[Hash] getEnrolledUTXOs () @safe nothrow
{
return this.getEnrolledUTXOs(this.getBlockHeight() + 1);
return this.getEnrolledUTXOs(this.height() + 1);
}

/***************************************************************************
Expand Down Expand Up @@ -1182,9 +1182,9 @@ public abstract class UTXOTracker
unittest
{
scope ledger = new Ledger();
assert(ledger.getBlockHeight() == 0);
assert(ledger.height() == 0);
// This used to throw because `validateBlock` would pass and the Ledger
// was attempting to re-add the same block.
assert(ledger.acceptBlock(ledger.lastBlock()) is null);
assert(ledger.getBlockHeight() == 0);
assert(ledger.height() == 0);
}
Loading

0 comments on commit c789fd2

Please sign in to comment.