protected EmptyBlockRepositoryFixture(string subDirectory, IHashProvider hashProvider, IBlockObjectTypeProvider typeProvider) { var factory = new LoggerFactory(); var genesisBlock = new Block { Index = 0L, PreviousHash = new byte[] { 0 }, MerkleRootHash = new byte[] { 0 }, Timestamp = 1465154705U, Nonce = 0L, Objects = new BlockObject[] { }, }; var baseDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); Value = new SqliteBlockStore( baseDirectory, subDirectory, "blockchain", genesisBlock, typeProvider, factory.CreateLogger <SqliteBlockStore>()); genesisBlock.Index = 1; genesisBlock.Hash = genesisBlock.ToHashBytes(hashProvider); Value.AddAsync(genesisBlock).ConfigureAwait(false).GetAwaiter().GetResult(); GenesisBlock = genesisBlock; TypeProvider = typeProvider; }
private static bool Bootstrap() { IConfiguration config = new ConfigurationBuilder() .AddEnvironmentVariables() .AddJsonFile("chain.json") .Build(); var loggerFactory = new LoggerFactory(); loggerFactory.AddConsole(); var chain = new ChainSettings(); config.Bind(chain); System.Console.Title = chain.Name; var logger = loggerFactory.CreateLogger(chain.Name); TaskScheduler.UnobservedTaskException += (s, e) => { UnhandledException(logger, e.Exception); }; AppDomain.CurrentDomain.UnhandledException += (s, e) => { UnhandledException(logger, e.ExceptionObject as Exception); }; try { var hashProvider = new ObjectHashProvider(); var typeProvider = new BlockObjectTypeProvider(); if (chain.GenesisBlock == null) { logger.LogCritical("Chain genesis block is missing"); goto failfast; } if (chain.GenesisBlock.Hash == null) { logger.LogCritical("Chain genesis block hash is missing"); goto failfast; } var genesisHash = chain.GenesisBlock.ToHashBytes(hashProvider); #if DEBUG var genesisHashDebug = "["; foreach (var c in genesisHash) { genesisHashDebug += c + ", "; } genesisHashDebug += "]"; logger.LogDebug($"Expected genesisHash = {genesisHashDebug}"); #endif if (!chain.GenesisBlock.Hash.ConstantTimeEquals(genesisHash)) { logger.LogCritical($"Chain genesis block hash '{genesisHash.ToHex()}' is invalid"); goto failfast; } var chainHash = hashProvider.ComputeHashBytes(chain); #if DEBUG var chainHashDebug = "["; foreach (var c in chainHash) { chainHashDebug += c + ", "; } chainHashDebug += "]"; logger.LogDebug($"Expected chainHash = {chainHashDebug}"); #endif if (!chain.Hash.ConstantTimeEquals(chainHash)) { logger.LogCritical("Chain configuration hash is missing or invalid"); goto failfast; } // // Storage: // IBlockStore blocks = null; if (!string.IsNullOrWhiteSpace(chain.StorageEngine)) { switch (chain.StorageEngine?.ToUpperInvariant()) { case "SQLITE": { string baseDirectory = chain.StorageDirectory ?? Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); blocks = new SqliteBlockStore(baseDirectory, chain.Name, "blocks", chain.GenesisBlock, typeProvider, loggerFactory.CreateLogger <SqliteBlockStore>()); break; } } } // // Blockchain: // if (blocks == null) { logger.LogCritical("Could not find a supported storage engine for the chain."); goto failfast; } _blockchain = new Blockchain(blocks, new NoProofOfWork(), hashProvider, loggerFactory.CreateLogger <Blockchain>()); } catch (Exception ex) { UnhandledException(logger, ex); } logger.LogInformation($"{chain.Name} is running."); return(true); failfast: logger.LogInformation("Press any key to shut down."); return(false); }