/** * */ private function update() { $header = $this->index->getHeader(); // Check all active features if ($header->getTimestamp() >= $this->params->p2shActivateTime()) { $this->p2sh = true; } $hash = $this->index->getHash()->getBinary(); $this->bip30 = !($this->index->getHeight() == 91842 && $hash == pack("H*", "00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec") || $this->index->getHeight() == 91880 && $hash == pack("H*", "00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721")); $highest = $this->majorityVersion(); if ($highest >= 2) { $this->bip34 = true; } if ($highest >= 3) { $this->derSig = true; } if ($highest >= 4) { $this->cltv = true; } // Calculate flags $this->flags = $this->p2sh ? InterpreterInterface::VERIFY_NONE : InterpreterInterface::VERIFY_P2SH; if ($this->derSig) { $this->flags |= InterpreterInterface::VERIFY_DERSIG; } if ($this->cltv) { $this->flags |= InterpreterInterface::VERIFY_CHECKLOCKTIMEVERIFY; } }
/** * @param BlockIndexInterface $index * @return bool */ public function isNext(BlockIndexInterface $index) { if (false === $this->hash->equals($index->getHeader()->getPrevBlock())) { return false; } if (false === ($index->getHeight() == $this->height + 1)) { return false; } return true; }
/** * @param ChainAccessInterface $chain * @param BlockIndexInterface $index * @param BlockIndexInterface $prevIndex * @param Forks $forks * @return $this */ public function checkContextual(ChainAccessInterface $chain, BlockIndexInterface $index, BlockIndexInterface $prevIndex, Forks $forks) { $work = $this->getWorkRequired($chain, $prevIndex); $header = $index->getHeader(); if ($this->math->cmp(gmp_init($header->getBits(), 10), gmp_init($work, 10)) != 0) { throw new \RuntimeException('Headers::CheckContextual(): invalid proof of work : ' . $header->getBits() . '? ' . $work); } if ($header->getVersion() < $forks->getMajorityVersion()) { echo $index->getHash()->getHex() . PHP_EOL; echo "Heaader: " . $header->getVersion() . "\nMajority: " . $forks->getMajorityVersion() . PHP_EOL; throw new \RuntimeException('Rejected version'); } return $this; }
/** * @param BlockIndexInterface $index * @return array */ public function convertIndexToArray(BlockIndexInterface $index) { $header = $index->getHeader(); return ['height' => $index->getHeight(), 'hash' => $index->getHash()->getHex(), 'work' => $index->getWork(), 'version' => $header->getVersion(), 'prevBlock' => $header->getPrevBlock()->getHex(), 'merkleRoot' => $header->getMerkleRoot()->getHex(), 'nBits' => $header->getBits(), 'nTimestamp' => $header->getTimestamp(), 'nNonce' => $header->getNonce()]; }
/** * @param ChainStateInterface $chain * @param BlockIndexInterface $prevIndex * @return int|string */ public function getWorkRequired(ChainStateInterface $chain, BlockIndexInterface $prevIndex) { $math = $this->math; if ($math->cmp($math->mod($math->add($prevIndex->getHeight(), 1), $this->params->powRetargetInterval()), 0) !== 0) { // No change in difficulty return $prevIndex->getHeader()->getBits()->getInt(); } // Re-target $heightLastRetarget = $math->sub($prevIndex->getHeight(), $math->sub($this->params->powRetargetInterval(), 1)); $lastTime = $chain->fetchAncestor($heightLastRetarget)->getHeader()->getTimestamp(); return $this->calculateNextWorkRequired($prevIndex, $lastTime); }
/** * @param ChainSegment $segment * @param int $prevBits * @param BlockIndexInterface $index */ public function logRetarget(ChainSegment $segment, $prevBits, BlockIndexInterface $index) { $this->log('retarget', ['hash' => $index->getHash()->getHex(), 'height' => $index->getHeight(), 'prevBits' => $prevBits, 'newBits' => $index->getHeader()->getBits()]); }