コード例 #1
0
        public void Setup()
        {
            _logManager          = Substitute.For <ILogManager>();
            _blockTree           = Substitute.For <IBlockTree>();
            _txPool              = Substitute.For <ITxPool>();
            _receiptStorage      = Substitute.For <IReceiptStorage>();
            _filterStore         = new FilterStore();
            _jsonRpcDuplexClient = Substitute.For <IJsonRpcDuplexClient>();
            _jsonSerializer      = new EthereumJsonSerializer();

            SubscriptionFactory subscriptionFactory = new SubscriptionFactory(
                _logManager,
                _blockTree,
                _txPool,
                _receiptStorage,
                _filterStore);

            _subscriptionManager = new SubscriptionManager(
                subscriptionFactory,
                _logManager);

            _subscribeRpcModule         = new SubscribeRpcModule(_subscriptionManager);
            _subscribeRpcModule.Context = new JsonRpcContext(RpcEndpoint.WebSocket, _jsonRpcDuplexClient);

            // block numbers matching filters in LogsSubscriptions with null arguments will be 33333-77777
            BlockHeader fromBlock = Build.A.BlockHeader.WithNumber(33333).TestObject;
            BlockHeader toBlock   = Build.A.BlockHeader.WithNumber(77777).TestObject;

            _blockTree.FindHeader(Arg.Any <BlockParameter>()).Returns(fromBlock);
            _blockTree.FindHeader(Arg.Any <BlockParameter>(), true).Returns(toBlock);
        }
コード例 #2
0
        public SyncingSubscription(
            IJsonRpcDuplexClient jsonRpcDuplexClient,
            IBlockTree?blockTree,
            IEthSyncingInfo ethSyncingInfo,
            ILogManager?logManager)
            : base(jsonRpcDuplexClient)
        {
            _blockTree      = blockTree ?? throw new ArgumentNullException(nameof(blockTree));
            _ethSyncingInfo = ethSyncingInfo ?? throw new ArgumentNullException(nameof(ethSyncingInfo));
            _logger         = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));

            _lastIsSyncing = _ethSyncingInfo.IsSyncing();
            if (_logger.IsTrace)
            {
                _logger.Trace($"Syncing subscription {Id}: Syncing status on start is {_lastIsSyncing}");
            }

            _blockTree.NewBestSuggestedBlock += OnConditionsChange;
            if (_logger.IsTrace)
            {
                _logger.Trace($"Syncing subscription {Id} will track NewBestSuggestedBlocks");
            }

            _blockTree.NewHeadBlock += OnConditionsChange;
            if (_logger.IsTrace)
            {
                _logger.Trace($"Syncing subscription {Id} will track NewHeadBlocks");
            }
        }
        public DroppedPendingTransactionsSubscription(IJsonRpcDuplexClient jsonRpcDuplexClient, ITxPool?txPool, ILogManager?logManager) : base(jsonRpcDuplexClient)
        {
            _txPool = txPool ?? throw new ArgumentNullException(nameof(txPool));
            _logger = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));

            _txPool.EvictedPending += OnEvicted;
            if (_logger.IsTrace)
            {
                _logger.Trace($"DroppedPendingTransactions subscription {Id} will track DroppedPendingTransactions");
            }
        }
コード例 #4
0
        public NewHeadSubscription(IJsonRpcDuplexClient jsonRpcDuplexClient, IBlockTree?blockTree, ILogManager?logManager)
            : base(jsonRpcDuplexClient)
        {
            _blockTree = blockTree ?? throw new ArgumentNullException(nameof(blockTree));
            _logger    = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));

            _blockTree.NewHeadBlock += OnNewHeadBlock;
            if (_logger.IsTrace)
            {
                _logger.Trace($"NewHeads subscription {Id} will track NewHeadBlocks");
            }
        }
コード例 #5
0
        public NewPendingTransactionsSubscription(IJsonRpcDuplexClient jsonRpcDuplexClient, ITxPool?txPool, ILogManager?logManager, Filter?filter = null)
            : base(jsonRpcDuplexClient)
        {
            _txPool = txPool ?? throw new ArgumentNullException(nameof(txPool));
            _logger = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));
            _includeTransactions = filter?.IncludeTransactions ?? false;

            _txPool.NewPending += OnNewPending;
            if (_logger.IsTrace)
            {
                _logger.Trace($"NewPendingTransactions subscription {Id} will track NewPendingTransactions");
            }
        }
コード例 #6
0
        public NewHeadSubscription(IJsonRpcDuplexClient jsonRpcDuplexClient, IBlockTree?blockTree, ILogManager?logManager, ISpecProvider specProvider, Filter?filter = null)
            : base(jsonRpcDuplexClient)
        {
            _blockTree           = blockTree ?? throw new ArgumentNullException(nameof(blockTree));
            _logger              = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));
            _includeTransactions = filter?.IncludeTransactions ?? false;
            _specProvider        = specProvider;

            _blockTree.BlockAddedToMain += OnBlockAddedToMain;
            if (_logger.IsTrace)
            {
                _logger.Trace($"NewHeads subscription {Id} will track BlockAddedToMain");
            }
        }
コード例 #7
0
        public NewReceivedUserOpsSubscription(
            IJsonRpcDuplexClient jsonRpcDuplexClient,
            IDictionary <Address, IUserOperationPool>?userOperationPools,
            ILogManager?logManager,
            UserOperationSubscriptionParam?userOperationSubscriptionParam = null)
            : base(jsonRpcDuplexClient)
        {
            if (userOperationPools is null)
            {
                throw new ArgumentNullException(nameof(userOperationPools));
            }
            if (userOperationSubscriptionParam is not null)
            {
                if (userOperationSubscriptionParam.EntryPoints.Length == 0)
                {
                    _userOperationPoolsToTrack = userOperationPools.Values.ToArray();
                }
                else
                {
                    _userOperationPoolsToTrack = userOperationPools
                                                 .Where(kv => userOperationSubscriptionParam.EntryPoints.Contains(kv.Key))
                                                 .Select(kv => kv.Value)
                                                 .ToArray();
                }

                _includeUserOperations = userOperationSubscriptionParam.IncludeUserOperations;
            }
            else
            {
                // use all pools
                _userOperationPoolsToTrack = userOperationPools.Values.ToArray();
            }

            _logger = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));

            foreach (var pool in _userOperationPoolsToTrack)
            {
                pool.NewReceived += OnNewReceived;
            }

            if (_logger.IsTrace)
            {
                _logger.Trace($"newReceivedUserOperations subscription {Id} will track newReceivedUserOperations");
            }
        }
コード例 #8
0
        public LogsSubscription(
            IJsonRpcDuplexClient jsonRpcDuplexClient,
            IReceiptStorage?receiptStorage,
            IFilterStore?store,
            IBlockTree?blockTree,
            ILogManager?logManager,
            Filter?filter = null)
            : base(jsonRpcDuplexClient)
        {
            _receiptStorage = receiptStorage ?? throw new ArgumentNullException(nameof(receiptStorage));
            _blockTree      = blockTree ?? throw new ArgumentNullException(nameof(blockTree));
            _logger         = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));
            IFilterStore filterStore = store ?? throw new ArgumentNullException(nameof(store));

            if (filter != null)
            {
                _filter = filterStore.CreateLogFilter(
                    filter.FromBlock,
                    filter.ToBlock,
                    filter.Address,
                    filter.Topics);
                if (_logger.IsTrace)
                {
                    _logger.Trace($"Logs Subscription {Id}: Created LogFilter with the same arguments like \"filter\"");
                }
            }
            else
            {
                _filter = filterStore.CreateLogFilter(
                    BlockParameter.Latest,
                    BlockParameter.Latest);
                if (_logger.IsTrace)
                {
                    _logger.Trace($"Logs Subscription {Id}: Argument \"filter\" was null and created LogFilter with arguments: FromBlock: BlockParameter.Latest, ToBlock: BlockParameter.Latest");
                }
            }

            _receiptStorage.ReceiptsInserted += OnReceiptsInserted;
            _blockTree.NewHeadBlock          += OnNewHeadBlock;
            if (_logger.IsTrace)
            {
                _logger.Trace($"Logs subscription {Id} will track ReceiptsInserted.");
            }
        }
コード例 #9
0
        public Subscription CreateSubscription(IJsonRpcDuplexClient jsonRpcDuplexClient, SubscriptionType subscriptionType, Filter?filter)
        {
            switch (subscriptionType)
            {
            case SubscriptionType.NewHeads:
                return(new NewHeadSubscription(jsonRpcDuplexClient, _blockTree, _logManager));

            case SubscriptionType.Logs:
                return(new LogsSubscription(jsonRpcDuplexClient, _receiptStorage, _filterStore, _blockTree, _logManager, filter));

            case SubscriptionType.NewPendingTransactions:
                return(new NewPendingTransactionsSubscription(jsonRpcDuplexClient, _txPool, _logManager));

            case SubscriptionType.Syncing:
                return(new SyncingSubscription(jsonRpcDuplexClient, _blockTree, _logManager));

            default: throw new Exception("Unexpected SubscriptionType.");
            }
        }
コード例 #10
0
        public Subscription CreateSubscription(IJsonRpcDuplexClient jsonRpcDuplexClient, string subscriptionType, string?args = null)
        {
            if (_subscriptionConstructors.TryGetValue(subscriptionType, out CustomSubscriptionType customSubscription))
            {
                Type?paramType = customSubscription.ParamType;

                IJsonRpcParam?param            = null;
                bool          thereIsParameter = paramType is not null;
                bool          thereAreArgs     = args is not null;
                if (thereIsParameter && (thereAreArgs || paramType.CannotBeAssignedNull()))
                {
                    param = (IJsonRpcParam)Activator.CreateInstance(paramType);
                    if (thereAreArgs)
                    {
                        param !.FromJson(_jsonSerializer, args);
                    }
                }

                return(customSubscription.Constructor(jsonRpcDuplexClient, param));
            }

            throw new KeyNotFoundException($"{subscriptionType} is an invalid or unregistered subscription type");
        }
コード例 #11
0
 protected Subscription(IJsonRpcDuplexClient jsonRpcDuplexClient)
 {
     Id = string.Concat("0x", Guid.NewGuid().ToString("N"));
     JsonRpcDuplexClient = jsonRpcDuplexClient;
     ProcessMessages();
 }