-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/scala/com/fluency03/blockchain/core/Difficulty.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.fluency03.blockchain.core | ||
|
||
object Difficulty { | ||
|
||
lazy val difficultyOneTarget: BigInt = targetOfBits("1d00ffff".hex) | ||
|
||
/** | ||
* https://github.com/bitcoin/bitcoin/blob/master/src/arith_uint256.cpp#L206 | ||
*/ | ||
def decodeCompact(nCompact: Long): (BigInt, Boolean, Boolean) = { | ||
val nSize = (nCompact >> 24).toInt | ||
val (nWord, result) = if (nSize <= 3) { | ||
val nWord1 = (nCompact & 0x007fffff) >> 8 * (3 - nSize) | ||
(nWord1, BigInt(nWord1)) | ||
} else { | ||
val nWord1 = nCompact & 0x007fffff | ||
(nWord1, BigInt(nWord1) << (8 * (nSize - 3))) | ||
} | ||
val fNegative = nWord != 0 && (nCompact & 0x00800000) != 0 | ||
val fOverflow = nWord != 0 && ((nSize > 34) || (nWord > 0xff && nSize > 33) || (nWord > 0xffff && nSize > 32)) | ||
(result, fNegative, fOverflow) | ||
} | ||
|
||
// def encodeCompact(target: BigInt, fNegative: Boolean): Long = { | ||
// val bitLength = target.bitLength | ||
// var nSize = ((if (bitLength == 0) 0 else bitLength + 1) + 7) / 8 | ||
// var nCompact: Long = if (nSize <= 3) { | ||
// getLowBits(target, 64) << 8 * (3 - nSize) | ||
// } else { | ||
// val bn = target >> 8 * (nSize - 3) | ||
// getLowBits(bn, 64) | ||
// } | ||
// // The 0x00800000 bit denotes the sign. | ||
// // Thus, if it is already set, divide the mantissa by 256 and increase the exponent. | ||
// if ((nCompact & 0x00800000) != 0) (nCompact >>= 8, nSize += 1) | ||
// | ||
// assert((nCompact & ~0x007fffff) == 0) | ||
// assert(nSize < 256) | ||
// nCompact |= nSize << 24 | ||
// nCompact |= (if (fNegative && ((nCompact & 0x007fffff) != 0)) 0x00800000 else 0) | ||
// nCompact | ||
// } | ||
|
||
def getLowBits(x: BigInt, N: Int): Long = (x & ((1 << N) - 1)).toLong | ||
|
||
def targetOfBits(bitsInt: Long): BigInt = { | ||
val (result, fNegative, _) = decodeCompact(bitsInt) | ||
if (fNegative) -result else result | ||
} | ||
|
||
// def bitsOfTarget(target: BigInt, fNegative: Boolean): Long = encodeCompact(target, fNegative) | ||
|
||
def padHexTarget(hex: String): String = hex.length match { | ||
case len => "0" * (64 - len) + hex | ||
} | ||
|
||
def hashLessThanTarget(hash: String, target: String): Boolean = ??? | ||
|
||
def difficultyOf(target: BigInt, negative: Boolean, overflow: Boolean): Double = { | ||
if (target == 0 || negative || overflow) 0.0 | ||
else (BigDecimal(difficultyOneTarget) / BigDecimal(target)).toDouble | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
http { | ||
host = "0.0.0.0" | ||
port = 9090 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/test/scala/com/fluency03/blockchain/core/DifficultyTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.fluency03.blockchain.core | ||
|
||
import com.fluency03.blockchain.core.Difficulty._ | ||
import org.scalatest.{FlatSpec, Matchers} | ||
|
||
class DifficultyTest extends FlatSpec with Matchers { | ||
|
||
val (t1, n1, o1) = decodeCompact("1d00ffff".hex) | ||
val (t2, n2, o2) = decodeCompact("453062093".toLong) | ||
val (t3, n3, o3) = decodeCompact("-453062093".toLong) | ||
|
||
"Bits in different formats" should "have its corresponding targets." in { | ||
difficultyOneTarget shouldEqual t1 | ||
t1.toString(16) shouldEqual "ffff0000000000000000000000000000000000000000000000000000" | ||
t2.toString(16) shouldEqual "12dcd000000000000000000000000000000000000000000000000" | ||
targetOfBits("01003456".hex) shouldEqual "00".hex | ||
targetOfBits("01123456".hex) shouldEqual "12".hex | ||
targetOfBits("02008000".hex) shouldEqual "80".hex | ||
targetOfBits("05009234".hex) shouldEqual "92340000".hex | ||
targetOfBits("04923456".hex) shouldEqual - "12345600".hex | ||
targetOfBits("04123456".hex) shouldEqual "12345600".hex | ||
// println("1d00ffff".hex) | ||
// println(bitsOfTarget(t1, n1)) | ||
// println(n1, o1) | ||
// println(bitsOfTarget(t2, n2)) | ||
// println(n2, o2) | ||
// println(bitsOfTarget(t3, n3)) | ||
// println(n3, o3) | ||
} | ||
|
||
"padHexTarget" should "add enough zeros (0) in front of a hex fot making it 64 bytes." in { | ||
padHexTarget(t1.toString(16)) shouldEqual "00000000ffff0000000000000000000000000000000000000000000000000000" | ||
padHexTarget(t2.toString(16)) shouldEqual "0000000000012dcd000000000000000000000000000000000000000000000000" | ||
difficultyOf(t1, n1, o1) shouldEqual 1.0 | ||
difficultyOf(t2, n2, o2) shouldEqual 55589.518126868665 | ||
// targetOfBits(bitsOfTarget(t1, n1)) shouldEqual t1 | ||
// targetOfBits(bitsOfTarget(t2, n2)) shouldEqual t2 | ||
} | ||
|
||
|
||
|
||
} |