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.
Additionally, this ensures the block storage is in charge
of adding the hardcoded genesis block to the storage.

The load() method can be used by the Ledger to make sure
UTXOs are tracked when the block storage was empty on boot.

Fixes #286
  • Loading branch information
AndrejMitrovic committed Sep 6, 2019
1 parent 62b75b3 commit 04d8463
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
42 changes: 31 additions & 11 deletions source/agora/node/BlockStorage.d
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@ public interface IBlockStorage

/***************************************************************************
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.
Returns:
true if the storage is empty (height index was not loaded)
true if the blockchain was loaded from storage
***************************************************************************/

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

/***************************************************************************
Expand Down Expand Up @@ -188,21 +194,32 @@ public class BlockStorage : IBlockStorage

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

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

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

public bool isEmpty ()
public bool load ()
{
return this.height_idx.length == 0;
if (!this.loadAllIndexes())
assert(0);

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

return false;
}

return true;
}

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

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

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

/***************************************************************************
Expand Down
8 changes: 1 addition & 7 deletions source/agora/node/Ledger.d
Original file line number Diff line number Diff line change
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

0 comments on commit 04d8463

Please sign in to comment.