Skip to content

Commit

Permalink
Merge pull request #616 from lochjin/dev1.2
Browse files Browse the repository at this point in the history
Support chaininfo rpc
  • Loading branch information
dindinw authored Feb 27, 2024
2 parents 226c349 + bded8aa commit cd8d0d5
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/json/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,15 @@ type MeerDAGInfoResult struct {
BlockCacheHeightSize uint64 `json:"bcacheheightsize"`
BlockCacheRate string `json:"bcacherate"`
BlockDataCacheSize string `json:"bdcachesize"`
AnticoneSize int `json:"anticonesize"`
}

type ChainInfoResult struct {
Count uint64 `json:"count"`
Start string `json:"start"`
End string `json:"end"`
BlocksPerSecond float64 `json:"blockspersecond"`
TxsPerSecond float64 `json:"txspersecond"`
SecondPerHeight string `json:"secondperheight"`
Concurrency float64 `json:"concurrency"`
}
4 changes: 4 additions & 0 deletions meerdag/phantom.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,10 @@ func (ph *Phantom) CheckBlockOrderDB(maxDepth uint64) error {
return nil
}

func (ph *Phantom) AnticoneSize() int {
return ph.anticoneSize
}

// The main chain of DAG is support incremental expansion
type MainChain struct {
bd *MeerDAG
Expand Down
72 changes: 72 additions & 0 deletions node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package node
import (
js "encoding/json"
"fmt"
"github.com/Qitmeer/qng/core/blockchain"
"math/big"
"strconv"
"time"
Expand Down Expand Up @@ -221,6 +222,7 @@ func (api *PublicBlockChainAPI) GetMeerDAGInfo() (interface{}, error) {
mdr.BlockCacheHeightSize = md.GetMinBlockCacheSize()
mdr.BlockCacheRate = fmt.Sprintf("%.2f%%", float64(md.GetBlockCacheSize())/float64(mdr.Total)*100)
mdr.BlockDataCacheSize = fmt.Sprintf("%d / %d", md.GetBlockDataCacheSize(), md.GetMinBlockDataCacheSize())
mdr.AnticoneSize = md.GetInstance().(*meerdag.Phantom).AnticoneSize()
return mdr, nil
}

Expand All @@ -235,6 +237,76 @@ func (api *PublicBlockChainAPI) GetDatabaseInfo() (interface{}, error) {
return ret, nil
}

func (api *PublicBlockChainAPI) GetChainInfo(lastCount int) (interface{}, error) {
if !api.node.GetPeerServer().IsCurrent() {
return nil, fmt.Errorf("Busy, try again later")
}
count := meerdag.MinBlockPruneSize
if lastCount > 1 {
count = lastCount
}
md := api.node.GetBlockChain().BlockDAG()
mainTip := md.GetMainChainTip()
var start meerdag.IBlock
var blockNode *blockchain.BlockNode
info := json.ChainInfoResult{Count: 0, End: fmt.Sprintf("%s (order:%d)", mainTip.GetHash().String(), mainTip.GetOrder())}
totalTxs := 0
err := md.Foreach(mainTip, uint(count), meerdag.All, func(block meerdag.IBlock) (bool, error) {
if block.GetID() <= 0 {
return true, nil
}
blockNode = api.node.GetBlockChain().GetBlockNode(block)
if blockNode == nil {
return true, nil
}
info.Count++
totalTxs += blockNode.GetPriority()
start = block
return true, nil
})
if err != nil {
return nil, err
}
if info.Count <= 0 {
return nil, fmt.Errorf("No blocks")
}
totalTxs -= blockNode.GetPriority()
if totalTxs < 0 {
totalTxs = 0
}
info.Start = fmt.Sprintf("%s (order:%d)", start.GetHash().String(), start.GetOrder())
startNode := api.node.GetBlockChain().GetBlockHeader(start)
if startNode == nil {
return nil, fmt.Errorf("No block:%s", start.GetHash().String())
}
endNode := api.node.GetBlockChain().GetBlockNode(mainTip)
if endNode == nil {
return nil, fmt.Errorf("No block:%s", mainTip.GetHash().String())
}
totalTxs += endNode.GetPriority()
totalTime := endNode.GetTimestamp() - startNode.Timestamp.Unix()
if totalTime < 0 {
totalTime = 0
}
if totalTime <= 0 {
return nil, fmt.Errorf("Time is too short")
}
info.BlocksPerSecond = float64(info.Count) / float64(totalTime)
info.TxsPerSecond = float64(totalTxs) / float64(totalTime)

totalHeight := int64(mainTip.GetHeight()) - int64(start.GetHeight())
if totalHeight < 0 {
totalHeight = 0
}
if totalHeight > 0 {
secondPerHeight := time.Duration(totalTime) * time.Second / time.Duration(totalHeight)
info.SecondPerHeight = secondPerHeight.String()
info.Concurrency = float64(info.Count) / float64(totalHeight)
}

return info, nil
}

type PrivateBlockChainAPI struct {
node *QitmeerFull
}
Expand Down
14 changes: 14 additions & 0 deletions script/cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,15 @@ function dag_info(){
get_result "$data"
}

function chain_info(){
local count=$1
if [ "$count" == "" ]; then
count=0
fi
local data='{"jsonrpc":"2.0","method":"getChainInfo","params":['$count'],"id":1}'
get_result "$data"
}

function database_info(){
local data='{"jsonrpc":"2.0","method":"getDatabaseInfo","params":[],"id":1}'
get_result "$data"
Expand Down Expand Up @@ -845,6 +854,7 @@ function usage(){
echo " getaddresses <private key>"
echo " modules"
echo " daginfo"
echo " chaininfo"
echo " dbinfo"
echo " config"
echo "block :"
Expand Down Expand Up @@ -1573,6 +1583,10 @@ elif [ "$1" == "daginfo" ]; then
shift
dag_info $@

elif [ "$1" == "chaininfo" ]; then
shift
chain_info $@

elif [ "$1" == "dbinfo" ]; then
shift
database_info $@
Expand Down

0 comments on commit cd8d0d5

Please sign in to comment.