Exemplo n.º 1
0
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed && disposing)
            {
                // shutdown
                LocalClient.Dispose();
                CoreDaemon.Dispose();
                Kernel.Dispose();

                disposed = true;
            }
        }
Exemplo n.º 2
0
        public MainWindow()
        {
            try
            {
                //TODO
                //MainnetRules.BypassValidation = true;
                //MainnetRules.BypassExecuteScript = true;
                ScriptEngine.BypassVerifySignature = true;

#if SQLITE
                var storageContext = new SQLiteStorageContext();
                var knownAddressStorage = new BitSharp.Storage.SQLite.KnownAddressStorage(storageContext);
                this.storageContext = storageContext;
#elif FIREBIRD
                var storageContext = new FirebirdStorageContext();
                var knownAddressStorage = new BitSharp.Storage.Firebird.KnownAddressStorage(storageContext);
                this.storageContext = storageContext;
#elif SQL_SERVER
                var storageContext = new SqlServerStorageContext();
                var knownAddressStorage = new BitSharp.Storage.SqlServer.KnownAddressStorage(storageContext);
                this.storageContext = storageContext;
#endif

                this.cacheContext = new CacheContext(this.storageContext);
                this.rules = new MainnetRules(this.cacheContext);
                this.blockchainDaemon = new BlockchainDaemon(this.rules, this.cacheContext);
                this.localClient = new LocalClient(LocalClientType.MainNet, this.blockchainDaemon, knownAddressStorage);

                // setup view model
                this.viewModel = new MainWindowViewModel(this.blockchainDaemon);

                InitializeComponent();

                // start the blockchain daemon
                this.blockchainDaemon.Start();

                this.viewModel.ViewBlockchainLast();

                // start p2p client
                this.localClient.Start();

                this.DataContext = this.viewModel;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                throw;
            }
        }
Exemplo n.º 3
0
        public MainWindow()
        {
            try
            {
                //TODO
                //**************************************************************
                var useTestNet = false;
                var connectToPeers = true;

                var ignoreScripts = false;

                var pruningMode = PruningMode.TxIndex | PruningMode.BlockSpentIndex | PruningMode.BlockTxesDelete;
                var enableDummyWallet = true;

                var useLevelDb = false;
                var runInMemory = false;

                var cleanData = false;
                var cleanChainState = false;
                var cleanBlockTxes = false
                    // clean block txes if the chain state is being cleaned and block txes have been pruned, the new chain state will require intact blocks to validate
                    || (cleanChainState && (pruningMode.HasFlag(PruningMode.BlockTxesPreserveMerkle) || pruningMode.HasFlag(PruningMode.BlockTxesDestroyMerkle)));
                //NOTE: Running with a cleaned chained state against a pruned blockchain does not work.
                //      It will see the data is missing, but won't redownload the blocks.
                //**************************************************************

                long? cacheSizeMaxBytes = 512.MEBIBYTE();

                // directories
                var baseDirectory = Config.LocalStoragePath;
                //if (Debugger.IsAttached)
                //    baseDirectory = Path.Combine(baseDirectory, "Debugger");

                var chainType = useTestNet ? ChainType.TestNet3 : ChainType.MainNet;

                string[] blockTxesStorageLocations = null;

                // detect local dev machine - TODO proper configuration
                var isAzureVM = (Environment.MachineName == "BITSHARP");
                var isLocalDev = (Environment.MachineName == "SKIPPY");

                if (isAzureVM)
                {
                    cacheSizeMaxBytes = null;
                    BlockRequestWorker.SecondaryBlockFolder = @"E:\BitSharp.Blocks\RawBlocks";
                    PeerWorker.ConnectedMax = 15;

                    blockTxesStorageLocations = new[]
                    {
                        @"E:\Blocks1",
                        @"E:\Blocks2",
                        @"E:\Blocks3",
                        @"E:\Blocks4",
                    };
                }
                else if (isLocalDev)
                {
                    //Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.BelowNormal;

                    cacheSizeMaxBytes = (int)2.GIBIBYTE();

                    // location to store a copy of raw blocks to avoid redownload
                    BlockRequestWorker.SecondaryBlockFolder = Path.Combine(baseDirectory, "RawBlocks");

                    // split block txes storage across 2 dedicated SSDs, keep chain state on main SSD
                    //blockTxesStorageLocations = new[]
                    //{
                    //    @"Y:\BitSharp",
                    //    @"Z:\BitSharp",
                    //};
                }

                //TODO
                if (cleanData)
                {
                    try { Directory.Delete(Path.Combine(baseDirectory, "Data", chainType.ToString()), recursive: true); }
                    catch (IOException) { }
                }
                if (cleanChainState)
                {
                    try { Directory.Delete(Path.Combine(baseDirectory, "Data", chainType.ToString(), "ChainState"), recursive: true); }
                    catch (IOException) { }
                }
                if (cleanBlockTxes)
                {
                    try { Directory.Delete(Path.Combine(baseDirectory, "Data", chainType.ToString(), "BlockTxes"), recursive: true); }
                    catch (IOException) { }
                }

                // initialize kernel
                this.kernel = new StandardKernel();

                // add logging module
                this.kernel.Load(new LoggingModule(baseDirectory, LogLevel.Info));

                // log startup
                this.logger = LogManager.GetCurrentClassLogger();
                this.logger.Info($"Starting up: {DateTime.Now}");

                var modules = new List<INinjectModule>();

                // add storage module
                if (useLevelDb)
                {
                    ulong? blocksCacheSize = null;
                    ulong? blocksWriteCacheSize = null;

                    ulong? blockTxesCacheSize = null;
                    ulong? blockTxesWriteCacheSize = null;

                    ulong? chainStateCacheSize = null;
                    ulong? chainStateWriteCacheSize = null;

                    modules.Add(new LevelDbStorageModule(baseDirectory, chainType,
                        blocksCacheSize, blocksWriteCacheSize,
                        blockTxesCacheSize, blockTxesWriteCacheSize,
                        chainStateCacheSize, chainStateWriteCacheSize));

                    long? writeCacheSizeMaxBytes = 128.MEBIBYTE();

                }
                else if (runInMemory)
                {
                    modules.Add(new MemoryStorageModule());
                    modules.Add(new NodeMemoryStorageModule());
                }
                else
                {
                    modules.Add(new EsentStorageModule(baseDirectory, chainType, cacheSizeMaxBytes: cacheSizeMaxBytes, blockTxesStorageLocations: blockTxesStorageLocations));
                }

                // add rules module
                modules.Add(new RulesModule(chainType));

                // load modules
                this.kernel.Load(modules.ToArray());

                // initialize rules
                var rules = this.kernel.Get<ICoreRules>();
                rules.IgnoreScripts = ignoreScripts;

                // initialize the blockchain daemon
                this.coreDaemon = this.kernel.Get<CoreDaemon>();
                this.coreDaemon.PruningMode = pruningMode;
                this.kernel.Bind<CoreDaemon>().ToConstant(this.coreDaemon).InTransientScope();

                // initialize dummy wallet monitor
                this.dummyMonitor = new DummyMonitor(this.coreDaemon);
                if (enableDummyWallet)
                {
                    Task.Run(() => this.dummyMonitor.Start());
                }
                else
                {
                    // allow pruning to any height when not using the wallet
                    this.coreDaemon.PrunableHeight = int.MaxValue;
                }

                // initialize p2p client
                this.localClient = this.kernel.Get<LocalClient>();
                this.kernel.Bind<LocalClient>().ToConstant(this.localClient).InTransientScope();

                // setup view model
                this.viewModel = new MainWindowViewModel(this.kernel, this.dummyMonitor);
                InitializeComponent();

                // start the blockchain daemon
                Task.Run(() => this.coreDaemon.Start());

                // start p2p client
                Task.Run(() => this.localClient.Start(connectToPeers));

                this.DataContext = this.viewModel;
            }
            catch (Exception ex)
            {
                if (this.logger != null)
                {
                    this.logger.Fatal(ex, "Application failed");
                    LogManager.Flush();
                }
                else
                {
                    Console.WriteLine(ex);
                }

                Environment.Exit(-1);
            }
        }