/** * @param ChainState $state * @param ChainCache $chainView * @param Peer $peer * @param Inventory[] $items */ public function advertised(ChainState $state, ChainCache $chainView, Peer $peer, array $items) { $chain = $state->getChain(); $fetch = []; $lastUnknown = null; foreach ($items as $inv) { $hash = $inv->getHash(); if ($chain->containsHash($hash)) { if (!$chainView->containsHash($hash)) { $fetch[] = $inv; } } else { $lastUnknown = $hash; } } if (null !== $lastUnknown) { echo "send headers\n"; $peer->getheaders($state->getHeadersLocator($lastUnknown)); $this->peerState->fetch($peer)->updateBlockAvailability($state, $lastUnknown); } if (count($fetch) > 0) { echo 'SEND GETDATA:' . count($fetch) . '\\n'; $peer->getdata($fetch); } }
/** * @expectedException \RuntimeException * @expectedExceptionMessage ChainCache: New BlockIndex does not refer to last */ public function testFailsAddingToCache() { $hash = str_pad("", 32, ""); $another = str_pad("", 32, "€"); $cache = new ChainCache([$hash]); $next = new BlockIndex(new Buffer(str_pad("", 32, "A")), 1, '1', new BlockHeader(1, new Buffer($another), new Buffer('', 32), 0, new Buffer(), 0)); $cache->add($next); }
/** * @param BlockIndex $index */ public function updateTip(BlockIndex $index) { if ($this->index->getHash() !== $index->getHeader()->getPrevBlock()) { throw new \RuntimeException('Header: Header does not extend this chain'); } if ($index->getHeight() - 1 != $this->index->getHeight()) { throw new \RuntimeException('Header: Incorrect chain height'); } $this->chainCache->add($index); $this->index = $index; }