diff --git a/source/agora/node/BlockStorage.d b/source/agora/node/BlockStorage.d index 68984a85a23..bba69d39d8d 100644 --- a/source/agora/node/BlockStorage.d +++ b/source/agora/node/BlockStorage.d @@ -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 (); /*************************************************************************** @@ -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); + } } /*************************************************************************** @@ -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); } /*************************************************************************** diff --git a/source/agora/node/Ledger.d b/source/agora/node/Ledger.d index efdb0310235..445fdd35520 100644 --- a/source/agora/node/Ledger.d +++ b/source/agora/node/Ledger.d @@ -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)) diff --git a/tests/unit/BlockStorage.d b/tests/unit/BlockStorage.d index 07b24f05ae1..75dbad1f88b 100644 --- a/tests/unit/BlockStorage.d +++ b/tests/unit/BlockStorage.d @@ -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)) { diff --git a/tests/unit/BlockStorageChecksum.d b/tests/unit/BlockStorageChecksum.d index e2af28a73e5..ecefeb88574 100644 --- a/tests/unit/BlockStorageChecksum.d +++ b/tests/unit/BlockStorageChecksum.d @@ -51,7 +51,6 @@ private void writeBlocks (string path) const(Block)[] blocks; blocks ~= GenesisBlock; - storage.saveBlock(blocks[$ - 1]); foreach (block_idx; 0 .. BlockCount) { diff --git a/tests/unit/BlockStorageMultiTx.d b/tests/unit/BlockStorageMultiTx.d index e6617845537..d5238d32998 100644 --- a/tests/unit/BlockStorageMultiTx.d +++ b/tests/unit/BlockStorageMultiTx.d @@ -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();