public TransactionConfirmationWatcherTests() { this.callbackRepository = Substitute.For <ICallbackRepository>(); this.ruleRepository = Substitute.ForPartsOf <FakeRuleRepository>(); this.blockStorage = Substitute.For <IBlocksStorage>(); this.blockStorage .GetAsync(Arg.Any <uint256>(), Arg.Any <CancellationToken>()) .Returns(info => mockedBlocks[info.ArgAt <uint256>(0)].ToValueTuple()); this.callbackExecuter = Substitute.For <ICallbackExecuter>(); this.logger = Substitute.For <ILogger <TransactionConfirmationWatcher> >(); this.watchRepository = new FakeWatchRepository(); this.handler = this.subject = new TransactionConfirmationWatcher ( this.callbackRepository, this.ruleRepository, this.blockStorage, this.callbackExecuter, this.watchRepository, this.logger ); this.blockListener = this.subject; this.defaultUrl = new Uri("http://zcoin.io"); MockCallbackRepository(); }
public BlockController(ExperimentTask task, int blockId, TargetBehaviour[] targets, IBlockListener listener, CursorBehaviour theCursor) { this.task = task; this.blockId = blockId; this.targets = targets; this.listener = listener; this.cursor = theCursor; }
public TarCompressCrypt(CancellationTokenSource cancellationTokenSource, IBlockListener blockListener, ILogger <TarCompressCrypt> logger, EncryptionCommands encryptionCommands, CompressionCommands compressionCommands) { _cancellationTokenSource = cancellationTokenSource; _blockListener = blockListener; _logger = logger; _encryptionCommands = encryptionCommands; _compressionCommands = compressionCommands; }
public Task <Task> StartAsync(IBlockListener listener, CancellationToken cancellationToken = default) { if (this.disposed) { throw new ObjectDisposedException(this.GetType().FullName); } if (this.retriever != null) { throw new InvalidOperationException("The retriever is already started."); } // Subscribe to ZeroMQ. this.subscriber = new SubscriberSocket(); try { this.poller = new NetMQPoller(); try { this.subscriber.ReceiveReady += (sender, e) => this.NotifyNewBlock(); this.subscriber.Connect(this.publisherAddress); this.subscriber.Subscribe(SubscriptionTopic); this.poller.Add(this.subscriber); this.poller.RunAsync(); // Start background tasks to retrieve blocks. this.canceler = new CancellationTokenSource(); try { this.retriever = this.RetrieveBlocksAsync(listener, this.canceler.Token); } catch { this.canceler.Dispose(); this.canceler = null; throw; } } catch { this.poller.Dispose(); this.poller = null; throw; } } catch { this.subscriber.Dispose(); this.subscriber = null; throw; } return(Task.FromResult(this.retriever)); }
public void AbortBlock() { if (currentTrial != null) { currentTrial.AbortTrial(); } currentTrial = null; listener = null; }
private void OnTouch(BlockTouchEventArgs e) { foreach (EventListener el in Plugins) { if (el.Event == Event.BlockTouch) { IBlockListener l = el.Listener as IBlockListener; l.OnTouch(e); } } }
private void OnPlace(BlockPlaceEventArgs e) { foreach (EventListener el in Plugins) { if (el.Event == Event.BlockPlace) { IBlockListener l = el.Listener as IBlockListener; l.OnPlace(e); } } }
private void OnDestroy(BlockDestroyEventArgs e) { foreach (EventListener el in Plugins) { if (el.Event == Event.BlockDestroy) { IBlockListener l = el.Listener as IBlockListener; l.OnDestroy(e); } } }
public BlocksSynchronizerTests() { this.exceptionHandler = new Mock <IBackgroundServiceExceptionHandler>(); this.logger = Substitute.For <ILogger <BlocksSynchronizer> >(); this.retriever = Substitute.For <IBlocksRetriever>(); this.storage = Substitute.For <IBlocksStorage>(); this.listener1 = Substitute.For <IBlockListener>(); this.listener2 = Substitute.For <IBlockListener>(); this.subject = new BlocksSynchronizer( this.exceptionHandler.Object, ZcoinNetworks.Instance.Regtest, this.logger, this.retriever, this.storage, new[] { this.listener1, this.listener2 } ); }
async Task RetrieveBlocksAsync(IBlockListener listener, CancellationToken cancellationToken = default) { await Task.Yield(); var height = await listener.GetStartBlockAsync(cancellationToken); while (true) { Block?block; await using (var client = await this.rpc.CreateChainInformationClientAsync(cancellationToken)) { block = await client.GetBlockAsync(height, cancellationToken); if (block == null) { var info = await client.GetChainInfoAsync(cancellationToken); var available = Convert.ToInt32(info.Blocks); if (available >= height) { // A new block just comming. continue; } if (available == height - 1) { // The target block is not available right now. await this.WaitForNewBlockAsync(cancellationToken); continue; } // There is a re-org happened. await listener.DiscardBlocksAsync(available, cancellationToken); height = available; continue; } } height = await listener.ProcessBlockAsync(block, height, cancellationToken); } }
public async Task ReadBlockAsync(long blockNumber, IBlockListener listener) { var blockId = Guid.NewGuid().ToString("N"); var previousBlockId = Guid.NewGuid().ToString("N"); listener.HandleRawBlock("raw-block".ToBase64(), blockId); var transactionsListener = listener.StartBlockTransactionsHandling ( new BlockHeaderReadEvent ( blockNumber, blockId, DateTime.UtcNow, 100, 1, previousBlockId ) ); var transactionId = new TransactionId(Guid.NewGuid().ToString("N")); await transactionsListener.HandleRawTransactionAsync("raw-transaction".ToBase64(), transactionId); transactionsListener.HandleExecutedTransaction ( new TransferAmountExecutedTransaction ( transactionNumber: 1, transactionId: transactionId, balanceChanges: new[] { new BalanceChange("1", new Asset("STEEM"), Money.Create(123, 4), "abc") }, fees: new[] { new Fee(new Asset("STEEM"), UMoney.Create(0.0001m, 4)), }, isIrreversible: true ) ); }
public Task ReadBlockAsync(long blockNumber, IBlockListener listener) { // TODO: Process block with specified number, emit header and transaction(s) events // // For example: // // var block = ...; // // var handleRawBlockTask = listener.HandleRawBlockAsync // ( // block.Raw.ToBase58(), // block.Hash // ); // for (int i = 0; i < block.Transactions.Length; i++) // { // var tx = block.Transactions[i]; // // if (tx.State == "SUCCESS") // { // // // If blockchain uses amount transfer scheme (is JSON-based) then emit TransferAmountTransactionExecutedEvent // // await listener.HandleExecutedTransactionAsync( // tx.Raw.ToBase58(), // new TransferAmountTransactionExecutedEvent( // block.Hash, // i, // tx.Hash, // tx.Actions.SelectMany(act => new [] // { // new BalanceChange(act.ActionId, new Asset(act.Token.Id), Money.Negate(Money.Create(act.Amount, act.Token.Accuracy)), act.From); // new BalanceChange(act.ActionId, new Asset(act.Token.Id), Money.Create(act.Amount, act.Token.Accuracy), act.To); // }), // fee: null, // isIrreversible: true // ) // ); // // // If blockchain uses coins transfer scheme (is UTXO-based) then emit TransferCoinsTransactionExecutedEvent // // await listener.HandleExecutedTransactionAsync( // tx.Raw.ToBase58(), // new TransferCoinsTransactionExecutedEvent( // block.Hash, // i, // tx.Hash, // tx.Outputs.Select(vout => new ReceivedCoin(vout.Number, "BTC", Money.Create(vout.Amount, 8), vout.Address)), // tx.Inputs.Select(vin => new CoinReference(vin.Hash, vin.Number)), // new [] { new Fee(new Asset("BTC"), Money.Create(tx.Fee, 8)) }, // isIrreversible: null // ) // ); // } // // if (tx.State == "FAILURE") // { // await listener.HandleFailedTransactionAsync( // tx.Raw.ToBase58(), // new TransactionFailedEvent( // block.Hash, // i, // tx.Hash, // TransactionBroadcastingError.RebuildRequired, // tx.Error, // fee: null // ) // ); // } // } // Better to send block header in the end: // // await listener.HandleHeaderAsync(new BlockHeaderReadEvent( // block.Number, // block.Hash, // block.Time, // block.Raw.Length, // block.Transactions.Count, // block.Previous.Hash // )); // // await handleRawBlockTask; throw new System.NotImplementedException(); }
/// <summary> /// Creates a new block walker. /// </summary> /// <param name="listener">The object that responds to traversal events.</param> public BlockWalker(IBlockListener <TInstruction> listener) { _listener = listener ?? throw new ArgumentNullException(nameof(listener)); }