/// <exception cref="ProtocolException"/> protected override void Parse() { // An inv is vector<CInv> where CInv is int+hash. The int is either 1 or 2 for tx or block. var arrayLen = ReadVarInt(); if (arrayLen > _maxInventoryItems) throw new ProtocolException("Too many items in INV message: " + arrayLen); _items = new List<InventoryItem>((int) arrayLen); for (var i = 0UL; i < arrayLen; i++) { if (Cursor + 4 + 32 > Bytes.Length) { throw new ProtocolException("Ran off the end of the INV"); } var typeCode = ReadUint32(); InventoryItem.ItemType type; // See ppszTypeName in net.h switch (typeCode) { case 0: type = InventoryItem.ItemType.Error; break; case 1: type = InventoryItem.ItemType.Transaction; break; case 2: type = InventoryItem.ItemType.Block; break; default: throw new ProtocolException("Unknown CInv type: " + typeCode); } var item = new InventoryItem(type, ReadHash()); _items.Add(item); } Bytes = null; }
/// <summary> /// Asks the connected peer for the block of the given hash, and returns a Future representing the answer. /// If you want the block right away and don't mind waiting for it, just call .get() on the result. Your thread /// will block until the peer answers. You can also use the Future object to wait with a timeout, or just check /// whether it's done later. /// </summary> /// <param name="blockHash">Hash of the block you were requesting.</param> /// <exception cref="IOException"/> public IAsyncResult BeginGetBlock(Sha256Hash blockHash, AsyncCallback callback, object state) { var getdata = new GetDataMessage(_params); var inventoryItem = new InventoryItem(InventoryItem.ItemType.Block, blockHash); getdata.AddItem(inventoryItem); var future = new GetDataFuture<Block>(inventoryItem, callback, state); // Add to the list of things we're waiting for. It's important this come before the network send to avoid // race conditions. lock (_pendingGetBlockFutures) { _pendingGetBlockFutures.Add(future); } _conn.WriteMessage(getdata); return future; }
public void AddItem(InventoryItem item) { _items.Add(item); }