Skip to content

Commit

Permalink
WIP debugging unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kiltsonfire committed Oct 12, 2024
1 parent a579a88 commit 62e4f15
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 27 deletions.
57 changes: 51 additions & 6 deletions consensus/misc/rewards.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package misc

import (
"fmt"
"math/big"

"github.com/dominant-strategies/go-quai/common"
Expand All @@ -25,8 +26,12 @@ func CalculateReward(primeTerminus *types.WorkObject, header *types.WorkObjectHe

// Calculate the amount of Quai that Qi can be converted to. Expect the current Header and the Qi amount in "qits", returns the quai amount in "its"
func QiToQuai(primeTerminus *types.WorkObject, header *types.WorkObject, qiAmt *big.Int) *big.Int {
quaiByQi := new(big.Int).Mul(CalculateQuaiReward(primeTerminus, header.WorkObjectHeader()), qiAmt)
return new(big.Int).Div(quaiByQi, CalculateQiReward(header.WorkObjectHeader()))
quaiReward := CalculateQuaiReward(primeTerminus, header.WorkObjectHeader())
quaiByQi := new(big.Int).Mul(quaiReward, qiAmt)
fmt.Printf("quaiByQi: %v\n", quaiByQi)
qiReward := CalculateQiReward(header.WorkObjectHeader())
fmt.Printf("qiReward: %v\n", qiReward)
return new(big.Int).Div(quaiByQi, qiReward)
}

// Calculate the amount of Qi that Quai can be converted to. Expect the current Header and the Quai amount in "its", returns the Qi amount in "qits"
Expand All @@ -48,29 +53,68 @@ func QuaiToQi(primeTerminus *types.WorkObject, header *types.WorkObject, quaiAmt
// spaces = [{"K Qi": state["K Qi"], "K Quai": k_quai}, spaces[1]]
// return spaces
func CalculateKQuai(header *types.WorkObject, beta0 *big.Int, beta1 *big.Int) *big.Int {
// Set kQuai to the exchange rate from the header
kQuai := new(big.Int).Set(header.ExchangeRate()) // in Its
fmt.Printf("kQuai (Exchange Rate): %v\n", kQuai)

// Calculate log of the difficulty
d2 := LogBig(header.Difficulty())
fmt.Printf("d2 (Log of Difficulty): %v\n", d2)

// Multiply beta0 and d2
num := new(big.Int).Mul(beta0, d2)
fmt.Printf("num (beta0 * d2): %v\n", num)

// Negate num
negnum := new(big.Int).Neg(num)
fmt.Printf("negnum (-num): %v\n", negnum)

// Multiply beta1 and the difficulty
denom := new(big.Int).Mul(beta1, header.Difficulty())
fmt.Printf("denom (beta1 * Difficulty): %v\n", denom)

// Divide negnum by denom
frac := new(big.Int).Quo(negnum, denom)
fmt.Printf("frac (negnum / denom): %v\n", frac)

// Subtract 2^64 from frac
sub := new(big.Int).Sub(frac, common.Big2e64)
fmt.Printf("sub (frac - 2^64): %v\n", sub)

// Just checking we are close to zero
check := new(big.Int).Quo(sub, common.Big2e64)
fmt.Printf("check (sub / 2^64): %v\n", check)

// Multiply sub by kQuai
bykQuai := new(big.Int).Mul(sub, kQuai)
fmt.Printf("bykQuai (sub * kQuai): %v\n", bykQuai)

// Multiply params.OneOverAlpha by 2^64
divisor := new(big.Int).Mul(params.OneOverAlpha, common.Big2e64)
final := new(big.Int).Quo(bykQuai, divisor)
fmt.Printf("divisor (OneOverAlpha * 2^64): %v\n", divisor)

// Divide bykQuai by divisor to get the final result
delta := new(big.Int).Quo(bykQuai, divisor)
fmt.Printf("final (bykQuai / divisor): %v\n", delta)

final := new(big.Int).Add(kQuai, delta)
fmt.Printf("final (kQuai + delta): %v\n", final)

return final
}

func CalculateQuaiReward(primeTerminus *types.WorkObject, header *types.WorkObjectHeader) *big.Int {
//exchangeRate := primeTerminus.ExchangeRate()
numerator := new(big.Int).Mul(primeTerminus.ExchangeRate(), LogBig(header.Difficulty()))
return new(big.Int).Quo(numerator, common.Big2e64)
reward := new(big.Int).Quo(numerator, common.Big2e64)
return reward
}

// CalculateQiReward caculates the qi that can be received for mining a block and returns value in qits
func CalculateQiReward(header *types.WorkObjectHeader) *big.Int {
return new(big.Int).Quo(header.Difficulty(), params.OneOverKqi)
diff := header.Difficulty()
qiReward := new(big.Int).Quo(diff, params.OneOverKqi)
return qiReward
}

// CalculateExchangeRate based on the quai to qi and qi to quai exchange rates
Expand Down Expand Up @@ -111,7 +155,8 @@ func FindMinDenominations(reward *big.Int) map[uint8]uint64 {

// IntrinsicLogEntropy returns the logarithm of the intrinsic entropy reduction of a PoW hash
func LogBig(diff *big.Int) *big.Int {
c, m := mathutil.BinaryLog(diff, consensus.MantBits)
diffCopy := new(big.Int).Set(diff)
c, m := mathutil.BinaryLog(diffCopy, consensus.MantBits)
bigBits := new(big.Int).Mul(big.NewInt(int64(c)), new(big.Int).Exp(big.NewInt(2), big.NewInt(consensus.MantBits), nil))
bigBits = new(big.Int).Add(bigBits, m)
return bigBits
Expand Down
91 changes: 70 additions & 21 deletions consensus/misc/rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,48 +38,97 @@ func TestCalculateReward(t *testing.T) {

func TestQiToQuai(t *testing.T) {
primeTerminus := &types.WorkObject{}
header := &types.WorkObject{}
qiAmt := big.NewInt(1000)
wb := &types.WorkObjectBody{}
header := &types.Header{}
wb.SetHeader(header)
primeTerminus.SetBody(wb)
primeTerminus.Header().SetExchangeRate(big.NewInt(52821824897296313)) //10^18/log2(500000) numerator is the target reward in its and 5e5 is starting difficulty

currentWo := &types.WorkObject{}
currentWoHeader := &types.WorkObjectHeader{}
currentWoHeader.SetDifficulty(big.NewInt(500000))
currentWo.SetWorkObjectHeader(currentWoHeader)

qiAmt := big.NewInt(1000) // 1 Qi or 1000 Qits

// Expected result for conversion
expectedQuaiAmt := big.NewInt(500) // Replace with actual expected value
expectedQuaiAmt := big.NewInt(999999999999999999) // Replace with actual expected value

// Call the function
actualQuaiAmt := QiToQuai(primeTerminus, header, qiAmt)
actualQuaiAmt := QiToQuai(primeTerminus, currentWo, qiAmt)

// Assert the result
assert.Equal(t, expectedQuaiAmt, actualQuaiAmt, "QiToQuai should return the correct Quai amount")
}

func TestCalculateQuaiReward(t *testing.T) {
func TestQuaiToQi(t *testing.T) {
primeTerminus := &types.WorkObject{}
header := &types.WorkObjectHeader{}
wb := &types.WorkObjectBody{}
header := &types.Header{}
wb.SetHeader(header)
primeTerminus.SetBody(wb)
primeTerminus.Header().SetExchangeRate(big.NewInt(52821824897296313)) // 10^18/log2(500000), numerator is the target reward in Qits, 5e5 is starting difficulty

currentWo := &types.WorkObject{}
currentWoHeader := &types.WorkObjectHeader{}
currentWoHeader.SetDifficulty(big.NewInt(500000))
currentWo.SetWorkObjectHeader(currentWoHeader)

// Mock ExchangeRate and Difficulty
// primeTerminus.ExchangeRate = func() *big.Int {
// return big.NewInt(100000)
// }
// header.Difficulty = func() *big.Int {
// return big.NewInt(100)
// }
quaiAmt := big.NewInt(999999999999999999) // 1 Quai or 10^18 Qits

// Expected result
expectedReward := big.NewInt(50) // Replace with actual expected value
// Expected result for conversion
expectedQiAmt := big.NewInt(1000) // Replace with actual expected value

// Call the function
actualReward := CalculateQuaiReward(primeTerminus, header)
actualQiAmt := QuaiToQi(primeTerminus, currentWo, quaiAmt)

// Assert the result
assert.Equal(t, expectedReward, actualReward, "CalculateQuaiReward should return the correct reward")
assert.Equal(t, expectedQiAmt, actualQiAmt, "QuaiToQi should return the correct Qi amount")
}

func TestCalculateKQuai(t *testing.T) {
prime := &types.WorkObject{}
wb := &types.WorkObjectBody{}
header := &types.Header{}
wb.SetHeader(header)
prime.SetBody(wb)
prime.Header().SetExchangeRate(big.NewInt(52821824897296313)) // 10^18/log2(500000), numerator is the target reward in Qits, 5e5 is starting difficulty
primeWoHeader := &types.WorkObjectHeader{}
primeWoHeader.SetDifficulty(big.NewInt(500000))
prime.ExchangeRate().Set(big.NewInt(5000000000000000000)) // Example value for KQuai
prime.SetWorkObjectHeader(primeWoHeader)

// Set beta0 and beta1 values for testing
beta0 := big.NewFloat(-0.5) // Example value for beta0
beta1 := big.NewFloat(0.000189316) // Example value for beta1

bigBeta0 := BigBeta(beta0)
bigBeta1 := BigBeta(beta1)

// Expected result for the calculation (replace with actual expected value)
expectedKQuai := big.NewInt(5000000000000000000) // Placeholder expected value

// Call the CalculateKQuai function
actualKQuai := CalculateKQuai(prime, bigBeta0, bigBeta1)

// Assert the result
assert.Equal(t, expectedKQuai, actualKQuai, "CalculateKQuai should return the correct KQuai amount")
}

func BigBeta(beta *big.Float) *big.Int {
bigBeta := new(big.Float).Mul(beta, new(big.Float).SetInt(common.Big2e64))
bigBetaInt, _ := bigBeta.Int(nil)
return bigBetaInt
}

func TestLogBig(t *testing.T) {
// Test with a difficulty value
difficulty := big.NewInt(1000000000)
difficulty := big.NewInt(500000)

// Expected result (replace with actual expected logarithm result)
expectedLog := big.NewInt(123456) // Replace with actual expected value

expectedLog, pass := new(big.Int).SetString("349225800312206723030", 10)
if !pass {
t.Errorf("Failed to parse expected logarithmic result")
}
// Call the function
actualLog := LogBig(difficulty)

Expand Down

0 comments on commit 62e4f15

Please sign in to comment.