Skip to content

Commit

Permalink
Added a SuggestFinalityDepth to API
Browse files Browse the repository at this point in the history
  • Loading branch information
kiltsonfire committed Aug 29, 2024
1 parent 06f56bb commit 82c067d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
12 changes: 6 additions & 6 deletions consensus/misc/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ import (

func CalculateReward(header *types.WorkObjectHeader) *big.Int {
if header.Coinbase().IsInQiLedgerScope() {
return calculateQiReward(header)
return CalculateQiReward(header)
} else {
return calculateQuaiReward(header)
return CalculateQuaiReward(header)
}
}

// 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(currentHeader *types.WorkObjectHeader, qiAmt *big.Int) *big.Int {
quaiPerQi := new(big.Int).Div(calculateQuaiReward(currentHeader), calculateQiReward(currentHeader))
quaiPerQi := new(big.Int).Div(CalculateQuaiReward(currentHeader), CalculateQiReward(currentHeader))
return new(big.Int).Mul(qiAmt, quaiPerQi)
}

// 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"
func QuaiToQi(currentHeader *types.WorkObjectHeader, quaiAmt *big.Int) *big.Int {
qiPerQuai := new(big.Int).Div(calculateQiReward(currentHeader), calculateQuaiReward(currentHeader))
qiPerQuai := new(big.Int).Div(CalculateQiReward(currentHeader), CalculateQuaiReward(currentHeader))
return new(big.Int).Mul(quaiAmt, qiPerQuai)
}

// CalculateQuaiReward calculates the quai that can be recieved for mining a block and returns value in its
func calculateQuaiReward(header *types.WorkObjectHeader) *big.Int {
func CalculateQuaiReward(header *types.WorkObjectHeader) *big.Int {
return big.NewInt(1000000000000000000)
}

// CalculateQiReward caculates the qi that can be received for mining a block and returns value in qits
func calculateQiReward(header *types.WorkObjectHeader) *big.Int {
func CalculateQiReward(header *types.WorkObjectHeader) *big.Int {
return big.NewInt(1000)
}

Expand Down
9 changes: 9 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/common/math"
"github.com/dominant-strategies/go-quai/consensus"
"github.com/dominant-strategies/go-quai/consensus/misc"
"github.com/dominant-strategies/go-quai/core/rawdb"
"github.com/dominant-strategies/go-quai/core/state"
"github.com/dominant-strategies/go-quai/core/state/snapshot"
Expand Down Expand Up @@ -1240,3 +1241,11 @@ func (c *Core) ContentFrom(addr common.Address) (types.Transactions, types.Trans
}
return c.sl.txPool.ContentFrom(internal)
}

func (c *Core) SuggestFinalityDepth(qiValue *big.Int, correlatedRisk *big.Int) *big.Int {
qiRewardPerBlock := misc.CalculateQiReward(c.CurrentHeader().WorkObjectHeader())

// Finality qiValue * correlatedRisk / qiRewardPerBlock
finalityDepth := new(big.Int).Div(new(big.Int).Mul(qiValue, correlatedRisk), qiRewardPerBlock)
return finalityDepth
}
1 change: 1 addition & 0 deletions internal/quaiapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ type Backend interface {
BroadcastWorkShare(workShare *types.WorkObjectShareView, location common.Location) error
GetMaxTxInWorkShare() uint64
GetExpansionNumber() uint8
SuggestFinalityDepth(ctx context.Context, qiValue *big.Int, correlatedRisk *big.Int) (*big.Int, error)

BadHashExistsInChain() bool
IsBlockHashABadHash(hash common.Hash) bool
Expand Down
8 changes: 8 additions & 0 deletions internal/quaiapi/quai_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,3 +914,11 @@ func (s *PublicBlockChainQuaiAPI) CalcOrder(ctx context.Context, raw hexutil.Byt
}
return hexutil.Uint(order), nil
}
func (s *PublicBlockChainQuaiAPI) SuggestFinalityDepth(ctx context.Context, qiValue hexutil.Uint64, correlatedRisk hexutil.Uint64) (hexutil.Uint64, error) {

depth, err := s.b.SuggestFinalityDepth(ctx, big.NewInt(int64(qiValue)), big.NewInt(int64(correlatedRisk)))
if err != nil {
return 0, err
}
return hexutil.Uint64(depth.Uint64()), nil
}
8 changes: 8 additions & 0 deletions quai/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,14 @@ func (b *QuaiAPIBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error)
return b.gpo.SuggestTipCap(ctx)
}

func (b *QuaiAPIBackend) SuggestFinalityDepth(ctx context.Context, qiValue *big.Int, correlatedRisk *big.Int) (*big.Int, error) {
nodeCtx := b.quai.core.NodeCtx()
if nodeCtx != common.ZONE_CTX {
return common.Big0, errors.New("suggestFinalityDepth can only be called in zone chain")
}
return b.quai.core.SuggestFinalityDepth(qiValue, correlatedRisk), nil
}

func (b *QuaiAPIBackend) FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (firstBlock *big.Int, reward [][]*big.Int, baseFee []*big.Int, gasUsedRatio []float64, err error) {
return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles)
}
Expand Down

0 comments on commit 82c067d

Please sign in to comment.