public DevLibplanetNodeService(
            LibplanetNodeServiceProperties <T> properties,
            IBlockPolicy <T> easyPolicy,
            IBlockPolicy <T> hardPolicy,
            IStagePolicy <T> stagePolicy,
            IEnumerable <IRenderer <T> > renderers,
            Func <Swarm <T>, Swarm <T>, PrivateKey, CancellationToken, Task> minerLoopAction,
            Progress <PreloadState> preloadProgress,
            Action <RPCException, string> exceptionHandlerAction,
            Action <bool> preloadStatusHandlerAction,
            bool ignoreBootstrapFailure = false
            ) : base(properties, easyPolicy, stagePolicy, renderers, null, preloadProgress, exceptionHandlerAction, preloadStatusHandlerAction, ignoreBootstrapFailure)
        {
            if (easyPolicy is null)
            {
                throw new ArgumentNullException(nameof(easyPolicy));
            }

            if (hardPolicy is null)
            {
                throw new ArgumentNullException(nameof(hardPolicy));
            }

            Log.Debug("Initializing node service.");

            var genesisBlock = LoadGenesisBlock(properties);

            var iceServers = properties.IceServers;

            (SubStore, SubStateStore) = LoadStore(
                properties.StorePath is null ? null : Path.Combine(properties.StorePath, "sub"),
                properties.StoreType,
                properties.StoreStatesCacheSize);

            SubChain = new BlockChain <T>(
                policy: hardPolicy,
                stagePolicy: stagePolicy,
                store: SubStore,
                stateStore: SubStateStore,
                genesisBlock: genesisBlock
                );

            _minerLoopAction = minerLoopAction;
            var subSwarmPrivateKey = new PrivateKey();

            Log.Debug(
                "Address of sub-swarm is: {address}",
                subSwarmPrivateKey.ToAddress());
            SubSwarm = new Swarm <T>(
                SubChain,
                subSwarmPrivateKey,
                properties.AppProtocolVersion,
                trustedAppProtocolVersionSigners: properties.TrustedAppProtocolVersionSigners,
                host: "localhost",
                listenPort: properties.Port + 1,
                iceServers: iceServers,
                workers: properties.Workers
                );
        }
Exemple #2
0
 protected Block <T> LoadGenesisBlock(
     LibplanetNodeServiceProperties <T> properties,
     HashAlgorithmGetter hashAlgorithmGetter
     )
 {
     if (!(properties.GenesisBlock is null))
     {
         return(properties.GenesisBlock);
     }
        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;
        }
 protected Block <T> LoadGenesisBlock(LibplanetNodeServiceProperties <T> properties)
 {
     if (!(properties.GenesisBlock is null))
     {
         return(properties.GenesisBlock);
     }
Exemple #5
0
        public LibplanetNodeService(
            LibplanetNodeServiceProperties <T> properties,
            IBlockPolicy <T> blockPolicy,
            IStagePolicy <T> stagePolicy,
            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,
            bool ignorePreloadFailure   = false
            )
        {
            if (blockPolicy is null)
            {
                throw new ArgumentNullException(nameof(blockPolicy));
            }

            Properties = properties;

            var genesisBlock = LoadGenesisBlock(properties, blockPolicy.GetHashAlgorithm);

            var iceServers = Properties.IceServers;

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

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

            Log.Debug($"Number of chain ids: {chainIds.Count()}");
            Log.Debug($"Canonical chain id: {Store.GetCanonicalChainId().ToString()}");

            if (Properties.Confirmations > 0)
            {
                HashAlgorithmGetter       getHashAlgo = blockPolicy.GetHashAlgorithm;
                IComparer <IBlockExcerpt> comparer    = blockPolicy.CanonicalChainComparer;
                int confirms = Properties.Confirmations;
                renderers = renderers.Select(r => r is IActionRenderer <T> ar
                    ? new DelayedActionRenderer <T>(ar, comparer, Store, getHashAlgo, confirms, 50)
                    : new DelayedRenderer <T>(r, comparer, Store, getHashAlgo, confirms)
                                             );

                // 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)
                                             );
            }

            if (Properties.NonblockRenderer)
            {
                renderers = renderers.Select(r =>
                {
                    if (r is IActionRenderer <T> ar)
                    {
                        return(new NonblockActionRenderer <T>(
                                   ar,
                                   Properties.NonblockRendererQueue,
                                   NonblockActionRenderer <T> .FullMode.DropOldest
                                   ));
                    }
                    else
                    {
                        return(new NonblockRenderer <T>(
                                   r,
                                   Properties.NonblockRendererQueue,
                                   NonblockActionRenderer <T> .FullMode.DropOldest
                                   ));
                    }
                });
            }

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

            _obsoletedChainIds = chainIds.Where(chainId => chainId != BlockChain.Id).ToList();

            _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.SwarmPrivateKey,
                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(50),
                BlockHashRecvTimeout   = TimeSpan.FromSeconds(50),
                BlockRecvTimeout       = TimeSpan.FromSeconds(5),
                BranchpointThreshold   = 50,
                StaticPeers            = Properties.StaticPeers,
                MinimumBroadcastTarget = Properties.MinimumBroadcastTarget,
                BucketSize             = Properties.BucketSize,
                PollInterval           = Properties.PollInterval,
                MaximumPollPeers       = Properties.MaximumPollPeers
            }
                );

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

            PreloadProgress        = preloadProgress;
            IgnoreBootstrapFailure = ignoreBootstrapFailure;
            _ignorePreloadFailure  = ignorePreloadFailure;
        }