diff --git a/core/blockchain/api.go b/core/blockchain/api.go index 8f21a15e..4ab26a0e 100644 --- a/core/blockchain/api.go +++ b/core/blockchain/api.go @@ -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 { @@ -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 + } +} diff --git a/meerdag/blockblue.go b/meerdag/blockblue.go index 12a353f0..9ea0cd0f 100644 --- a/meerdag/blockblue.go +++ b/meerdag/blockblue.go @@ -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 { diff --git a/node/api.go b/node/api.go index 59adcc7c..b0fd93c6 100644 --- a/node/api.go +++ b/node/api.go @@ -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 diff --git a/script/cli.sh b/script/cli.sh index 155a441a..32f14a3e 100755 --- a/script/cli.sh +++ b/script/cli.sh @@ -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" @@ -921,6 +934,7 @@ function usage(){ echo " loglevel [trace, debug, info, warn, error, critical]" echo " timeinfo" echo " subsidy" + echo " estimateBlocksMined " echo " meerinfo" echo " amanainfo" echo " amanapeerinfo" @@ -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