Skip to content

Commit

Permalink
Added conversions into the tokenChoice data
Browse files Browse the repository at this point in the history
  • Loading branch information
kiltsonfire committed Oct 12, 2024
1 parent 62e4f15 commit 606639c
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions core/exchange_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package core

import (
"errors"
"fmt"
"math/big"

"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/consensus/misc"
"github.com/dominant-strategies/go-quai/core/rawdb"
"github.com/dominant-strategies/go-quai/core/types"
"github.com/dominant-strategies/go-quai/log"
Expand Down Expand Up @@ -86,7 +88,34 @@ func CalculateTokenChoicesSet(hc *HeaderChain, block *types.WorkObject) (types.T
Qi: qi,
}
}
} else if types.IsConversionTx(tx) {
_, _, diff, err := tx.DecodeEtxData()
if err != nil {
return types.TokenChoiceSet{}, err
}
// Convert diff (big.Int) to a string key
diffKey := diff.String()

if entry, exists := tokenChoices[diffKey]; exists {
if tx.ETXSender().IsInQiLedgerScope() {
entry.Quai += NormalizeConversionValueToBlock(block, tx.Value(), false)
} else if tx.ETXSender().IsInQuaiLedgerScope() {
entry.Qi += NormalizeConversionValueToBlock(block, tx.Value(), true)
}
tokenChoices[diffKey] = entry
} else {
var quai, qi int
if tx.ETXSender().IsInQiLedgerScope() {
quai += NormalizeConversionValueToBlock(block, tx.Value(), false)
} else if tx.ETXSender().IsInQuaiLedgerScope() {
qi += NormalizeConversionValueToBlock(block, tx.Value(), true)
}

tokenChoices[diffKey] = struct{ Quai, Qi int }{
Quai: quai,
Qi: qi,
}
}
}
}

Expand Down Expand Up @@ -135,6 +164,19 @@ func CalculateTokenChoicesSet(hc *HeaderChain, block *types.WorkObject) (types.T
return types.TokenChoiceSet{}, errors.New("Failed to calculate token choices set")
}

func NormalizeConversionValueToBlock(block *types.WorkObject, value *big.Int, chooseQi bool) int {
var reward *big.Int
if chooseQi {
reward = misc.CalculateQiReward(block.WorkObjectHeader())
} else {
reward = misc.CalculateQuaiReward(block, block.WorkObjectHeader())
}

numBlocks := int(new(big.Int).Quo(value, reward).Int64())
fmt.Printf("numBlocks: %v\n", numBlocks)
return numBlocks
}

// serialize tokenChoiceSet
func SerializeTokenChoiceSet(tokenChoiceSet types.TokenChoiceSet) ([]*big.Int, []*big.Int) {
var diff []*big.Int
Expand Down

0 comments on commit 606639c

Please sign in to comment.