From 82c067d8dde93759203bdbfe8d97eeca361caa6c Mon Sep 17 00:00:00 2001 From: drk Date: Thu, 29 Aug 2024 09:35:24 -0500 Subject: [PATCH] Added a SuggestFinalityDepth to API --- consensus/misc/rewards.go | 12 ++++++------ core/core.go | 9 +++++++++ internal/quaiapi/backend.go | 1 + internal/quaiapi/quai_api.go | 8 ++++++++ quai/api_backend.go | 8 ++++++++ 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/consensus/misc/rewards.go b/consensus/misc/rewards.go index 673d2a6e63..3ce30bb637 100644 --- a/consensus/misc/rewards.go +++ b/consensus/misc/rewards.go @@ -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) } diff --git a/core/core.go b/core/core.go index b4ca5b9b4c..498bca2223 100644 --- a/core/core.go +++ b/core/core.go @@ -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" @@ -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 +} diff --git a/internal/quaiapi/backend.go b/internal/quaiapi/backend.go index 06ba4cdf9a..92d97529c1 100644 --- a/internal/quaiapi/backend.go +++ b/internal/quaiapi/backend.go @@ -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 diff --git a/internal/quaiapi/quai_api.go b/internal/quaiapi/quai_api.go index ea316d8322..8e5be29b36 100644 --- a/internal/quaiapi/quai_api.go +++ b/internal/quaiapi/quai_api.go @@ -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 +} diff --git a/quai/api_backend.go b/quai/api_backend.go index 842f04df80..faae199886 100644 --- a/quai/api_backend.go +++ b/quai/api_backend.go @@ -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) }