Ejemplo n.º 1
0
 /// <inheritdoc cref="IStore"/>
 public void DeleteChainId(Guid chainId)
 {
     _store.DeleteChainId(chainId);
 }
        public LibplanetNodeService(
            LibplanetNodeServiceProperties <T> properties,
            IBlockPolicy <T> blockPolicy,
            IEnumerable <IRenderer <T> > renderers,
            Func <BlockChain <T>, Swarm <T>, PrivateKey, CancellationToken, Task> minerLoopAction,
            Progress <PreloadState> preloadProgress,
            Action <RPCException, string> exceptionHandlerAction,
            Action <bool> preloadStatusHandlerAction,
            bool ignoreBootstrapFailure = false
            )
        {
            if (blockPolicy is null)
            {
                throw new ArgumentNullException(nameof(blockPolicy));
            }

            Properties = properties;

            var genesisBlock = LoadGenesisBlock(properties);

            var iceServers = Properties.IceServers;

            (Store, StateStore) = LoadStore(
                Properties.StorePath,
                Properties.StoreType,
                Properties.StoreStatesCacheSize);

            var pendingTxs = Store.IterateStagedTransactionIds()
                             .ToImmutableHashSet();

            Store.UnstageTransactionIds(pendingTxs);
            Log.Debug("Pending txs unstaged. [{PendingCount}]", pendingTxs.Count);

            var chainIds = Store.ListChainIds().ToList();

            Log.Debug($"Number of chain ids: {chainIds.Count()}");

            foreach (var chainId in chainIds)
            {
                Log.Debug($"chainId: {chainId}");
            }

            if (Properties.Confirmations > 0)
            {
                renderers = renderers.Select(r => r is IActionRenderer <T> ar
                    ? new DelayedActionRenderer <T>(ar, Store, Properties.Confirmations)
                    : new DelayedRenderer <T>(r, Store, Properties.Confirmations)
                                             );

                // Log the outmost (before delayed) events as well as
                // the innermost (after delayed) events:
                ILogger logger = Log.ForContext("SubLevel", " RAW-RENDER-EVENT");
                renderers = renderers.Select(r => r is IActionRenderer <T> ar
                    ? new LoggedActionRenderer <T>(ar, logger, LogEventLevel.Debug)
                    : new LoggedRenderer <T>(r, logger, LogEventLevel.Debug)
                                             );
            }

            BlockChain = new BlockChain <T>(
                policy: blockPolicy,
                store: Store,
                stateStore: StateStore,
                genesisBlock: genesisBlock,
                renderers: renderers
                );

            foreach (Guid chainId in chainIds.Where(chainId => chainId != BlockChain.Id))
            {
                Store.DeleteChainId(chainId);
            }

            _minerLoopAction            = minerLoopAction;
            _exceptionHandlerAction     = exceptionHandlerAction;
            _preloadStatusHandlerAction = preloadStatusHandlerAction;
            IEnumerable <IceServer> shuffledIceServers = null;

            if (!(iceServers is null))
            {
                var rand = new Random();
                shuffledIceServers = iceServers.OrderBy(x => rand.Next());
            }

            Swarm = new Swarm <T>(
                BlockChain,
                Properties.PrivateKey,
                Properties.AppProtocolVersion,
                trustedAppProtocolVersionSigners: Properties.TrustedAppProtocolVersionSigners,
                host: Properties.Host,
                listenPort: Properties.Port,
                iceServers: shuffledIceServers,
                workers: Properties.Workers,
                differentAppProtocolVersionEncountered: Properties.DifferentAppProtocolVersionEncountered,
                options: new SwarmOptions
            {
                MaxTimeout           = TimeSpan.FromSeconds(10),
                BlockHashRecvTimeout = TimeSpan.FromSeconds(10),
            }
                );

            PreloadEnded   = new AsyncManualResetEvent();
            BootstrapEnded = new AsyncManualResetEvent();

            PreloadProgress        = preloadProgress;
            IgnoreBootstrapFailure = ignoreBootstrapFailure;
        }