protected override bool ReadProperties(BinaryReader reader) { BlockType blockType = (BlockType)((BitConverter.ToUInt16(Header.Extensions, 0) & Block.BlockTypeMask) >> 8); switch (blockType) { case BlockType.Receive: Block = new ReceiveBlock(); break; case BlockType.Send: Block = new SendBlock(); break; case BlockType.Open: Block = new OpenBlock(); break; case BlockType.Change: Block = new ChangeBlock(); break; } Block.ReadFromStream(reader); return(true); }
/// <summary> /// Send buffer /// </summary> /// <param name="buffer"></param> /// <param name="endpoint"></param> /// <param name="ct"></param> /// <returns></returns> public async override Task <int> SendAsync(ArraySegment <byte> buffer, SocketAddress endpoint, CancellationToken ct) { await SendBlock.SendAsync(Message.Create(null, null, null, DataMessage.Create(buffer, endpoint)), ct).ConfigureAwait(false); return(buffer.Count); }
/// <summary> /// Detach the connected socket /// </summary> public virtual void Detach() { _socketReceive.Dispose(); _socketSend.Dispose(); ReceiveBlock.Complete(); SendBlock.Complete(); }
public static Task<bool> SendAsync<TInput> ( this ITargetBlock<TInput> target, TInput item, CancellationToken cancellationToken) { if (target == null) throw new ArgumentNullException ("target"); cancellationToken.ThrowIfCancellationRequested (); var status = target.OfferMessage ( new DataflowMessageHeader (1), item, null, false); if (status == DataflowMessageStatus.Accepted) return Task.FromResult (true); if (status != DataflowMessageStatus.Declined && status != DataflowMessageStatus.Postponed) return Task.FromResult (false); var block = new SendBlock<TInput> (target, item, cancellationToken); return block.Send (); }
public void Visit(SendBlock block) { FillValue(block); }
public void Visit(SendBlock block) { UInt256 hash = block.Hash(); if (_sendBlockStore.FindOneById(hash) == null) { return; } UInt256 previous = block.Hashables.Previous; if (_sendBlockStore.FindOneById(previous) == null) // have we seen the previous block before { return; } FrontiersDb account = _frontierStore.FindOneById(block.Hashables.Previous); if (account == null) { return; } //BinaryReader reader = new BinaryReader(new MemoryStream(account.ValueBytes)); //UInt256 blockHash = new UInt256(reader.ReadBytes(32)); //UInt256 representative = new UInt256(reader.ReadBytes(32)); //BigInteger balance = new BigInteger(reader.ReadBytes(17)); //long timeStamp = reader.ReadInt64(); if (!Utility.ValidateMessage(account.Account, hash, block.Signature)) { throw new BadSignatureException(); } var accountInfo = _latestAccountStore.FindOneById(account.Account); ThrowHelper.Sync.ThrowIfGapDetected(accountInfo.Head, block.Hashables.Previous); ThrowHelper.BadActor.ThrowIfOverspendDetected(accountInfo.Balance, block.Hashables.Balance); UInt128 amount = accountInfo.Balance - block.Hashables.Balance; /* * if (result.code == rai::process_result::progress) * { * rai::account_info info; * auto latest_error (ledger.store.account_get (transaction, account, info)); * assert (!latest_error); * assert (info.head == block_a.hashables.previous); * result.code = info.balance.number () >= block_a.hashables.balance.number () ? rai::process_result::progress : rai::process_result::overspend; // Is this trying to spend more than they have (Malicious) * if (result.code == rai::process_result::progress) * { * auto amount (info.balance.number () - block_a.hashables.balance.number ()); * ledger.store.representation_add (transaction, info.rep_block, 0 - amount); * ledger.store.block_put (transaction, hash, block_a); * ledger.change_latest (transaction, account, hash, info.rep_block, block_a.hashables.balance, info.block_count + 1); * ledger.store.pending_put (transaction, rai::pending_key (block_a.hashables.destination, hash), {account, amount}); * ledger.store.frontier_del (transaction, block_a.hashables.previous); * ledger.store.frontier_put (transaction, hash, account); * result.account = account; * result.amount = amount; * } */ }
public void PutBlock(UInt256 blockHash, SendBlock block, UInt256 successorHash = null) { ThrowHelper.Sanity.ThrowIfTrue(block == null || GetBlock(successorHash) != null); }