public function testHashIsSerializedInReverseOrder() { $buffer = Buffer::hex('0001020300010203000102030001020300010203000102030001020300010203'); $inv = Inventory::block($buffer); $results = unpack("Vtype/H64hash", $inv->getBinary()); $parsedBuffer = Buffer::hex($results['hash']); $this->assertEquals($buffer->getHex(), Buffertools::flipBytes($parsedBuffer)->getHex()); }
/** * @param ChainViewInterface $headerChain * @param BufferInterface $startHash * @throws \RuntimeException * @throws \Exception * @return Inventory[] */ private function relativeNextInventory(ChainViewInterface $headerChain, BufferInterface $startHash) { if (!$headerChain->containsHash($startHash)) { throw new \RuntimeException('Hash not found in this chain'); } $startHeight = $headerChain->getHeightFromHash($startHash) + 1; $stopHeight = min($startHeight + self::DOWNLOAD_AMOUNT, $headerChain->getIndex()->getHeight()); $nInFlight = count($this->inFlight); $request = []; for ($i = $startHeight; $i < $stopHeight && $nInFlight < self::MAX_IN_FLIGHT; $i++) { $request[] = Inventory::block($headerChain->getHashFromHeight($i)); $nInFlight++; } return $request; }
/** * @param Inventory $inv * @return \BitWasp\Buffertools\Buffer */ public function serialize(Inventory $inv) { return $this->getTemplate()->write([$inv->getType(), $inv->getHash()]); }
/** * @param Peer $sender * @param TransactionInterface $current */ public function handleTransaction(Peer $sender, TransactionInterface $current) { $hash = $current->getTransactionId(); //echo "TX: $hash\n"; if (!$this->Have($hash)) { $wasMalleated = false; $transaction = $this->fixTransaction($sender, $current, $wasMalleated); $newHash = $transaction->getTransactionId(); if ($wasMalleated) { $this->haveTx[pack("H*", $newHash)] = $transaction; echo "Was malleated: {$hash} - sending to " . (count($this->peers) - 1) . "\n"; foreach ($this->peers as $peer) { if ($sender !== $peer) { $peer->inv([Inventory::tx(Buffer::hex($newHash))]); } } } } else { echo "Already processed? {$hash} \n"; } }
/** * @param Inventory $inventory * @return bool */ public function checkInventory(Inventory $inventory) { if ($inventory->isBlock()) { //return isset($this->blocks->hashIndex[$inventory->getHash()->getHex()]); } if ($inventory->isTx()) { return $this->db->transactions->fetch($inventory->getHash()->getHex()) !== null; } return true; }
/** * @param Inventory $inventory * @return bool */ public function check(Inventory $inventory) { return isset($this->storage[$inventory->getHash()->getBinary()]); }