Skip to content

Commit

Permalink
Merge pull request #828 from lochjin/dev2.0_subsidy
Browse files Browse the repository at this point in the history
feat:support estimateBlocksMined API
  • Loading branch information
dindinw authored Nov 15, 2024
2 parents 8590ecf + cadd71a commit 3ecff42
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
32 changes: 32 additions & 0 deletions core/blockchain/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
"github.com/Qitmeer/qng/core/types"
"github.com/Qitmeer/qng/engine/txscript"
"github.com/Qitmeer/qng/meerdag"
"github.com/Qitmeer/qng/params"
rapi "github.com/Qitmeer/qng/rpc/api"
"github.com/Qitmeer/qng/rpc/client/cmds"
"strconv"
"time"
)

func (b *BlockChain) APIs() []rapi.API {
Expand Down Expand Up @@ -490,3 +492,33 @@ func (api *PublicBlockAPI) makeBlock(h hash.Hash, verbose *bool, inclTx *bool, f
return fields, nil

}

func (api *PublicBlockAPI) EstimateBlocksMined(start int64, height int64) (interface{}, error) {
if height <= 0 {
height = int64(time.Hour * 24 / params.ActiveNetParams.TargetTimePerBlock) // mainnet=2880
}
if height <= 0 {
return 0, nil
}
bd := api.chain.BlockDAG()
if start <= 0 {
return bd.GetBluesByDepth(uint(height), nil), nil
} else {
var endBlock meerdag.IBlock
end := start + height
for i := uint(end); i < bd.GetBlockTotal(); i++ {
block := bd.GetBlockById(i)
if block == nil {
break
}
if block.GetHeight() == uint(end) && bd.IsOnMainChain(block.GetID()) {
endBlock = block
break
}
}
if endBlock == nil {
return 0, fmt.Errorf("Invalid input params")
}
return bd.GetBluesByDepth(uint(height), endBlock), nil
}
}
6 changes: 4 additions & 2 deletions meerdag/blockblue.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ func (bd *MeerDAG) getBlueInfo(ib IBlock) *BlueInfo {
return NewBlueInfo(pb.blueNum+1, mt/int64(blues), int64(mainIB.GetState().GetWeight()), int64(ib.GetHeight()))
}

func (bd *MeerDAG) GetBluesByDepth(depth uint) int {
func (bd *MeerDAG) GetBluesByDepth(depth uint, start IBlock) int {
if _, ok := bd.instance.(*Phantom); !ok {
return 0
}
curDepth := uint(0)
start := bd.GetMainChainTip()
if start == nil {
start = bd.GetMainChainTip()
}
cur := start
count := 0
for curDepth < depth && cur != nil {
Expand Down
2 changes: 1 addition & 1 deletion node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (api *PublicBlockChainAPI) GetSubsidy() (interface{}, error) {
}
info.NextSubsidy = sc.CalcBlockSubsidy(binfo)
dailyBlockCount := int64(time.Hour * 24 / params.ActiveNetParams.TargetTimePerBlock)
info.EstimateDailyBlocksMined = int64(api.node.GetBlockChain().BlockDAG().GetBluesByDepth(uint(dailyBlockCount)))
info.EstimateDailyBlocksMined = int64(api.node.GetBlockChain().BlockDAG().GetBluesByDepth(uint(dailyBlockCount), nil))
estimateDailySubsidy := info.EstimateDailyBlocksMined * info.NextSubsidy
info.EstimateDailySubsidy = fmt.Sprintf("%d (%d x %d)", estimateDailySubsidy, info.EstimateDailyBlocksMined, info.NextSubsidy)
info.EstimateDailyMainheightRange = dailyBlockCount
Expand Down
18 changes: 18 additions & 0 deletions script/cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,19 @@ function get_subsidy(){
get_result "$data"
}

function estimate_blocks_mined(){
local start=$1
local height=$2
if [ "$start" == "" ]; then
start=0
fi
if [ "$height" == "" ]; then
height=0
fi
local data='{"jsonrpc":"2.0","method":"estimateBlocksMined","params":['$start','$height'],"id":1}'
get_result "$data"
}

function dag_info(){
local data='{"jsonrpc":"2.0","method":"getMeerDAGInfo","params":[],"id":1}'
get_result "$data"
Expand Down Expand Up @@ -921,6 +934,7 @@ function usage(){
echo " loglevel [trace, debug, info, warn, error, critical]"
echo " timeinfo"
echo " subsidy"
echo " estimateBlocksMined <start> <height>"
echo " meerinfo"
echo " amanainfo"
echo " amanapeerinfo"
Expand Down Expand Up @@ -1721,6 +1735,10 @@ elif [ "$1" == "listaccount" ]; then
shift
listAccount "$@"

elif [ "$1" == "estimateBlocksMined" ]; then
shift
estimate_blocks_mined "$@"

elif [ "$1" == "list_command" ]; then
usage
else
Expand Down

0 comments on commit 3ecff42

Please sign in to comment.