Skip to content

Commit

Permalink
Tests: Add EmptyBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak committed Jul 14, 2021
1 parent 507e98a commit 1c862ee
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 20 deletions.
49 changes: 29 additions & 20 deletions source/agora/test/Base.d
Original file line number Diff line number Diff line change
Expand Up @@ -994,15 +994,15 @@ public class TestAPIManager
***************************************************************************/

public void generateBlocks (Height height,
public void generateBlocks (Height height, bool no_txs = false,
string file = __FILE__, int line = __LINE__)
{
generateBlocks(iota(GenesisValidators), height, file, line);
generateBlocks(iota(GenesisValidators), height, no_txs, file, line);
}

/// Ditto
public void generateBlocks (Idxs)(Idxs client_idxs, Height height,
string file = __FILE__, int line = __LINE__)
bool no_txs = false, string file = __FILE__, int line = __LINE__)
{
static assert (isInputRange!Idxs);

Expand All @@ -1012,7 +1012,7 @@ public class TestAPIManager

// Call addBlock for each block to be externalised for these clients
iota(height - last_block.header.height)
.each!(_ => this.addBlock(client_idxs, file, line));
.each!(_ => this.addBlock(client_idxs, no_txs, file, line));
}

/**************************************************************************
Expand All @@ -1027,35 +1027,44 @@ public class TestAPIManager
***************************************************************************/

void addBlock (string file = __FILE__, int line = __LINE__)
void addBlock (bool no_txs = false,
string file = __FILE__, int line = __LINE__)
{
addBlock(iota(0, GenesisValidators), file, line);
addBlock(iota(0, GenesisValidators), no_txs, file, line);
}

/// Ditto
void addBlock (Idxs)(Idxs client_idxs,
void addBlock (Idxs)(Idxs client_idxs, bool no_txs = false,
string file = __FILE__, int line = __LINE__)
{
static assert (isInputRange!Idxs);

auto first_client = this.clients[client_idxs.front];
const last_block = first_client.getBlock(first_client.getBlockHeight());

auto last_height = first_client.getBlockHeight();
// traget height will be one more than previous block
Height target_height = last_block.header.height + 1;
Height target_height = Height(last_height + 1);

// Get spendables from last block
auto spendables = last_block.spendable().array;
while (!no_txs)
{
const last_block = first_client.getBlock(last_height);

// Show the last block if not enough spendables
assert(spendables.length >= 1,
format!"[%s:%s] No spendables in block:\n%s"
(file, line, prettify(last_block)));
// Get spendables from a previous block
auto spendables = last_block.spendable().array;

// Send transaction to the first client
spendables.takeExactly(1)
.map!(txb => txb.sign())
.each!(tx => first_client.putTransaction(tx));
if (spendables.length == 0)
{
assert(last_height != 0, "Can't find spendables");
last_height--;
}
else
{
// Send transaction to the first client
spendables.takeExactly(1)
.map!(txb => txb.sign())
.each!(tx => first_client.putTransaction(tx));
break;
}
}

// Get preimage height from enrollment to this next block
auto enrolled_height = target_height <= GenesisValidatorCycle ? 0
Expand Down
38 changes: 38 additions & 0 deletions source/agora/test/EmptyBlocks.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*******************************************************************************
Contains the simplest empty block creating test
Copyright:
Copyright (c) 2019-2021 BOSAGORA Foundation
All rights reserved.
License:
MIT License. See LICENSE for details.
*******************************************************************************/

module agora.test.EmptyBlocks;

version (unittest):

import agora.api.Validator;
import agora.consensus.data.Transaction;
import agora.test.Base;

/// EmptyBlocks test
unittest
{
auto network = makeTestNetwork!TestAPIManager(TestConf.init);
network.start();
scope(exit) network.shutdown();
scope(failure) network.printLogs();
network.waitForDiscovery();

auto nodes = network.clients;
auto node_1 = nodes[0];

// Create blocks for 2 validator cycles and a couple more
auto target_height = Height(2 * GenesisValidatorCycle + 2);
network.generateBlocks(target_height, true);
network.assertSameBlocks(target_height);
}

0 comments on commit 1c862ee

Please sign in to comment.