Skip to content

Commit 6ee2b14

Browse files
Thomas Kerinafk11
authored andcommitted
ProofOfWork: rename check -> checkPow because we no longer want to throw when a hash is valid but over the target
1 parent b34b72c commit 6ee2b14

2 files changed

Lines changed: 6 additions & 10 deletions

File tree

src/Chain/ProofOfWork.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(Math $math, ParamsInterface $params)
3838
* @param int $bits
3939
* @return \GMP
4040
*/
41-
public function getTarget($bits): \GMP
41+
public function getTarget(int $bits): \GMP
4242
{
4343
$negative = false;
4444
$overflow = false;
@@ -85,7 +85,7 @@ public function getDifficulty(int $bits): string
8585
* @param int $nBits
8686
* @return bool
8787
*/
88-
public function check(BufferInterface $hash, int $nBits): bool
88+
public function checkPow(BufferInterface $hash, int $nBits): bool
8989
{
9090
$negative = false;
9191
$overflow = false;
@@ -96,7 +96,7 @@ public function check(BufferInterface $hash, int $nBits): bool
9696
}
9797

9898
if ($this->math->cmp($hash->getGmp(), $target) > 0) {
99-
throw new \RuntimeException("Hash doesn't match nBits");
99+
return false;
100100
}
101101

102102
return true;
@@ -109,7 +109,7 @@ public function check(BufferInterface $hash, int $nBits): bool
109109
*/
110110
public function checkHeader(BlockHeaderInterface $header): bool
111111
{
112-
return $this->check($header->getHash(), $header->getBits());
112+
return $this->checkPow($header->getHash(), $header->getBits());
113113
}
114114

115115
/**

tests/Chain/ProofOfWorkTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,16 @@ public function testWhereBitsBelowMinimum()
4242
$params = new Params($math);
4343
$pow = new ProofOfWork(new Math(), $params);
4444
$bits = 1;
45-
$pow->check(Buffer::hex('00000000a3bbe4fd1da16a29dbdaba01cc35d6fc74ee17f794cf3aab94f7aaa0'), $bits);
45+
$pow->checkPow(Buffer::hex('00000000a3bbe4fd1da16a29dbdaba01cc35d6fc74ee17f794cf3aab94f7aaa0'), $bits);
4646
}
4747

48-
/**
49-
* @expectedException \RuntimeException
50-
* @expectedExceptionMessage Hash doesn't match nBits
51-
*/
5248
public function testWhereHashTooLow()
5349
{
5450
$math = new Math();
5551
$params = new Params($math);
5652
$pow = new ProofOfWork(new Math(), $params);
5753
$bits = 0x181287ba;
58-
$pow->check(Buffer::hex('00000000a3bbe4fd1da16a29dbdaba01cc35d6fc74ee17f794cf3aab94f7aaa0'), $bits);
54+
$this->assertFalse($pow->checkPow(Buffer::hex('00000000a3bbe4fd1da16a29dbdaba01cc35d6fc74ee17f794cf3aab94f7aaa0'), $bits));
5955
}
6056

6157
/**

0 commit comments

Comments
 (0)