private void Load(FileInfo file)
{
_log.InfoFormat("Reading block store from {0}", file);
using (var input = file.OpenRead())
{
// Read a version byte.
var version = input.Read();
if (version == -1)
{
// No such file or the file was empty.
throw new FileNotFoundException(file.Name + " does not exist or is empty");
}
if (version != 1)
{
throw new BlockStoreException("Bad version number: " + version);
}
// Chain head pointer is the first thing in the file.
var chainHeadHash = new byte[32];
if (input.Read(chainHeadHash) < chainHeadHash.Length)
throw new BlockStoreException("Truncated block store: cannot read chain head hash");
_chainHead = new Sha256Hash(chainHeadHash);
_log.InfoFormat("Read chain head from disk: {0}", _chainHead);
var now = Environment.TickCount;
// Rest of file is raw block headers.
var headerBytes = new byte[Block.HeaderSize];
try
{
while (true)
{
// Read a block from disk.
if (input.Read(headerBytes) < 80)
{
// End of file.
break;
}
// Parse it.
var b = new Block(_params, headerBytes);
// Look up the previous block it connects to.
var prev = Get(b.PrevBlockHash);
StoredBlock s;
if (prev == null)
{
// First block in the stored chain has to be treated specially.
if (b.Equals(_params.GenesisBlock))
{
s = new StoredBlock(_params.GenesisBlock.CloneAsHeader(), _params.GenesisBlock.GetWork(), 0);
}
else
{
throw new BlockStoreException("Could not connect " + b.Hash + " to " + b.PrevBlockHash);
}
}
else
{
// Don't try to verify the genesis block to avoid upsetting the unit tests.
b.VerifyHeader();
// Calculate its height and total chain work.
s = prev.Build(b);
}
// Save in memory.
_blockMap[b.Hash] = s;
}
}
catch (ProtocolException e)
{
// Corrupted file.
throw new BlockStoreException(e);
}
catch (VerificationException e)
{
// Should not be able to happen unless the file contains bad blocks.
throw new BlockStoreException(e);
}
var elapsed = Environment.TickCount - now;
_log.InfoFormat("Block chain read complete in {0}ms", elapsed);
}
}