Skip to content

Commit

Permalink
Write the Genesis block in the BlockStorage
Browse files Browse the repository at this point in the history
Move the loading code into a separate load() method,
and get rid of the empty() method.

Loading should ideally never be done in the constructor.

Fixes #286
  • Loading branch information
AndrejMitrovic committed Sep 6, 2019
1 parent 62b75b3 commit ab3d1eb
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
40 changes: 23 additions & 17 deletions source/agora/node/BlockStorage.d
Expand Up @@ -46,12 +46,15 @@ public interface IBlockStorage

/***************************************************************************
Returns:
true if the storage is empty (height index was not loaded)
Load the block storage. If there was nothing to load,
a Genesis block will be added to the ledger. In this case the calling
code should treat the block as new and update the set of UTXOs, etc.
If the blockchain data is corrupt the application will be halted.
***************************************************************************/

public bool isEmpty ();
public void load ();

/***************************************************************************
Expand Down Expand Up @@ -185,24 +188,28 @@ public class BlockStorage : IBlockStorage

this.height_idx = new IndexHeight();
this.hash_idx = new IndexHash();

if (!this.path.exists)
mkdirRecurse(this.path);

if (!this.loadAllIndexes())
assert(0);
}

/***************************************************************************
Returns:
true if the storage is empty (height index was not loaded)
Load the blockchain from the storage
***************************************************************************/

public bool isEmpty ()
public void load ()
{
return this.height_idx.length == 0;
if (!this.path.exists)
mkdirRecurse(this.path);

if (!this.loadAllIndexes())
assert(0);

// Add Genesis if the storage is empty
if (this.height_idx.length == 0)
{
if (!this.saveBlock(GenesisBlock))
assert(0);
}
}

/***************************************************************************
Expand Down Expand Up @@ -962,14 +969,13 @@ public class MemBlockStorage : IBlockStorage

/***************************************************************************
Returns:
true if the storage is empty (height index was not loaded)
Adds the genesis block to the in-memory blockchain storage.
***************************************************************************/

public bool isEmpty ()
public void load ()
{
return this.height_idx.length == 0;
this.saveBlock(GenesisBlock);
}

/***************************************************************************
Expand Down
8 changes: 1 addition & 7 deletions source/agora/node/Ledger.d
Expand Up @@ -47,13 +47,7 @@ public class Ledger
{
this.pool = pool;
this.storage = storage;

// add the genesis block
if (this.storage.isEmpty())
{
this.storage.saveBlock(GenesisBlock);
assert(!this.storage.isEmpty()); // sanity check
}
this.storage.load();

// ensure latest checksum can be read
if (!this.storage.readLastBlock(this.last_block))
Expand Down
1 change: 1 addition & 0 deletions tests/unit/BlockStorage.d
Expand Up @@ -108,6 +108,7 @@ private void main ()

// Verify index data that is already stored.
BlockStorage other = new BlockStorage(path);
other.load();

foreach (height; iota(count).randomCover(rnd))
{
Expand Down
1 change: 0 additions & 1 deletion tests/unit/BlockStorageChecksum.d
Expand Up @@ -51,7 +51,6 @@ private void writeBlocks (string path)

const(Block)[] blocks;
blocks ~= GenesisBlock;
storage.saveBlock(blocks[$ - 1]);

foreach (block_idx; 0 .. BlockCount)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/BlockStorageMultiTx.d
Expand Up @@ -44,10 +44,10 @@ private void main ()

BlockStorage.removeIndexFile(path);
BlockStorage storage = new BlockStorage(path);
storage.load();
const(Block)[] blocks;

blocks ~= GenesisBlock;
storage.saveBlock(blocks[$ - 1]);

// We can use a random keypair because blocks are not validated
auto gen_key_pair = KeyPair.random();
Expand Down

0 comments on commit ab3d1eb

Please sign in to comment.