Merge pull request #169 from meshplus/feat/add-tps-statistics
feat(*): add api to get TPS
This commit is contained in:
commit
bda3e60bb7
|
@ -2,6 +2,7 @@ package grpc
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
|
||||
"github.com/meshplus/bitxhub-model/pb"
|
||||
|
@ -19,3 +20,15 @@ func (cbs *ChainBrokerService) GetInfo(ctx context.Context, req *pb.Request) (*p
|
|||
return nil, fmt.Errorf("wrong query type")
|
||||
}
|
||||
}
|
||||
|
||||
func (cbs *ChainBrokerService) GetTPS(ctx context.Context, req *pb.GetTPSRequest) (*pb.Response, error) {
|
||||
tps, err := cbs.api.Chain().TPS(req.Begin, req.End)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(data, tps)
|
||||
|
||||
return &pb.Response{Data: data}, nil
|
||||
}
|
||||
|
|
|
@ -86,7 +86,7 @@ func (cbs *ChainBrokerService) sendTransaction(req *pb.SendTransactionRequest) (
|
|||
To: req.To,
|
||||
Timestamp: req.Timestamp,
|
||||
Data: req.Data,
|
||||
Nonce: req.Nonce,
|
||||
Nonce: uint64(req.Nonce),
|
||||
Signature: req.Signature,
|
||||
Extra: req.Extra,
|
||||
}
|
||||
|
@ -107,7 +107,7 @@ func (cbs *ChainBrokerService) sendView(req *pb.SendTransactionRequest) (*pb.Rec
|
|||
To: req.To,
|
||||
Timestamp: req.Timestamp,
|
||||
Data: req.Data,
|
||||
Nonce: req.Nonce,
|
||||
Nonce: uint64(req.Nonce),
|
||||
Signature: req.Signature,
|
||||
Extra: req.Extra,
|
||||
}
|
||||
|
|
|
@ -113,7 +113,7 @@ func sendTransaction(ctx *cli.Context) error {
|
|||
From: from,
|
||||
To: to,
|
||||
Timestamp: req.Timestamp,
|
||||
Nonce: req.Nonce,
|
||||
Nonce: uint64(req.Nonce),
|
||||
Data: req.Data,
|
||||
}
|
||||
|
||||
|
|
2
go.mod
2
go.mod
|
@ -18,7 +18,7 @@ require (
|
|||
github.com/magiconair/properties v1.8.1
|
||||
github.com/meshplus/bitxhub-core v0.1.0-rc1.0.20200903124405-b2c62dd8db89
|
||||
github.com/meshplus/bitxhub-kit v1.0.1-0.20200914065214-5161497a783c
|
||||
github.com/meshplus/bitxhub-model v1.0.0-rc4.0.20200826032743-bb0d60e38d74
|
||||
github.com/meshplus/bitxhub-model v1.0.0-rc4.0.20200922032630-a7786639f22f
|
||||
github.com/meshplus/go-lightp2p v0.0.0-20200817105923-6b3aee40fa54
|
||||
github.com/mitchellh/go-homedir v1.1.0
|
||||
github.com/multiformats/go-multiaddr v0.2.2
|
||||
|
|
2
go.sum
2
go.sum
|
@ -689,6 +689,8 @@ github.com/meshplus/bitxhub-model v1.0.0-rc4.0.20200514093243-7e8ae60d1c19 h1:D0
|
|||
github.com/meshplus/bitxhub-model v1.0.0-rc4.0.20200514093243-7e8ae60d1c19/go.mod h1:QK8aACbxtZEA3Hk1BOCirW0uxMWLsMrLDpWz9FweIKM=
|
||||
github.com/meshplus/bitxhub-model v1.0.0-rc4.0.20200826032743-bb0d60e38d74 h1:l8OpKXWu9IH5FeAs3alBmOEbrRLoRIyMCTGnacASaTg=
|
||||
github.com/meshplus/bitxhub-model v1.0.0-rc4.0.20200826032743-bb0d60e38d74/go.mod h1:QK8aACbxtZEA3Hk1BOCirW0uxMWLsMrLDpWz9FweIKM=
|
||||
github.com/meshplus/bitxhub-model v1.0.0-rc4.0.20200922032630-a7786639f22f h1:YywrLHhlBvg42Q1NAvmMiSUzj9nV6P9Wjg3XYc7/UHQ=
|
||||
github.com/meshplus/bitxhub-model v1.0.0-rc4.0.20200922032630-a7786639f22f/go.mod h1:QK8aACbxtZEA3Hk1BOCirW0uxMWLsMrLDpWz9FweIKM=
|
||||
github.com/meshplus/go-bitxhub-client v1.0.0-rc3/go.mod h1:FpiCyf6KhydcqthrHdvvPhbPIcD92b+Ju8T7WvQtSyM=
|
||||
github.com/meshplus/go-lightp2p v0.0.0-20200817105923-6b3aee40fa54 h1:5Ip5AB7SxxQHg5SRtf2cCOI2wy1p75MQB12soPtPyf8=
|
||||
github.com/meshplus/go-lightp2p v0.0.0-20200817105923-6b3aee40fa54/go.mod h1:G89UJaeqCQFxFdp8wzy1AdKfMtDEhpySau0pjDNeeaw=
|
||||
|
|
|
@ -50,6 +50,7 @@ type NetworkAPI interface {
|
|||
type ChainAPI interface {
|
||||
Status() string
|
||||
Meta() (*pb.ChainMeta, error)
|
||||
TPS(begin, end uint64) (uint64, error)
|
||||
}
|
||||
|
||||
type FeedAPI interface {
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package coreapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/meshplus/bitxhub-model/pb"
|
||||
"github.com/meshplus/bitxhub/internal/coreapi/api"
|
||||
)
|
||||
|
@ -20,3 +22,36 @@ func (api *ChainAPI) Status() string {
|
|||
func (api *ChainAPI) Meta() (*pb.ChainMeta, error) {
|
||||
return api.bxh.Ledger.GetChainMeta(), nil
|
||||
}
|
||||
|
||||
func (api *ChainAPI) TPS(begin, end uint64) (uint64, error) {
|
||||
total := 0
|
||||
startTime := int64(0)
|
||||
endTime := int64(0)
|
||||
|
||||
if begin >= end {
|
||||
return 0, fmt.Errorf("begin number should be smaller than end number")
|
||||
}
|
||||
|
||||
for i := begin; i <= end; i++ {
|
||||
block, err := api.bxh.Ledger.GetBlock(i)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
total += len(block.Transactions)
|
||||
if i == begin {
|
||||
startTime = block.BlockHeader.Timestamp
|
||||
}
|
||||
if i == end {
|
||||
endTime = block.BlockHeader.Timestamp
|
||||
}
|
||||
}
|
||||
|
||||
elapsed := endTime - startTime
|
||||
|
||||
if elapsed <= 0 {
|
||||
return 0, fmt.Errorf("incorrect block timestamp")
|
||||
}
|
||||
|
||||
return uint64(total) * 1e9 / uint64(elapsed), nil
|
||||
}
|
||||
|
|
|
@ -151,7 +151,7 @@ func (x *InterchainManager) HandleIBTPs(data []byte) *boltvm.Response {
|
|||
interchain := &Interchain{}
|
||||
x.GetObject(AppchainKey(x.Caller()), &interchain)
|
||||
|
||||
for _, ibtp := range ibtps.Iptp {
|
||||
for _, ibtp := range ibtps.Ibtps {
|
||||
if err := x.checkIBTP(ibtp, interchain); err != nil {
|
||||
return boltvm.Error(err.Error())
|
||||
}
|
||||
|
@ -161,7 +161,7 @@ func (x *InterchainManager) HandleIBTPs(data []byte) *boltvm.Response {
|
|||
return res
|
||||
}
|
||||
|
||||
for _, ibtp := range ibtps.Iptp {
|
||||
for _, ibtp := range ibtps.Ibtps {
|
||||
x.ProcessIBTP(ibtp, interchain)
|
||||
}
|
||||
|
||||
|
@ -234,7 +234,7 @@ func (x *InterchainManager) beginMultiTargetsTransaction(ibtps *pb.IBTPs) *boltv
|
|||
globalId := fmt.Sprintf("%s-%s", x.Caller(), x.GetTxHash())
|
||||
args = append(args, pb.String(globalId))
|
||||
|
||||
for _, ibtp := range ibtps.Iptp {
|
||||
for _, ibtp := range ibtps.Ibtps {
|
||||
if ibtp.Type != pb.IBTP_INTERCHAIN {
|
||||
return boltvm.Error("ibtp type != IBTP_INTERCHAIN")
|
||||
}
|
||||
|
|
|
@ -160,7 +160,7 @@ func mockBlock(blockNumber uint64, txs []*pb.Transaction) *pb.Block {
|
|||
func mockTx(data *pb.TransactionData) *pb.Transaction {
|
||||
return &pb.Transaction{
|
||||
Data: data,
|
||||
Nonce: rand.Int63(),
|
||||
Nonce: uint64(rand.Int63()),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -233,7 +233,7 @@ func mockTransferTx(t *testing.T) *pb.Transaction {
|
|||
Type: pb.TransactionData_NORMAL,
|
||||
Amount: 1,
|
||||
},
|
||||
Nonce: rand.Int63(),
|
||||
Nonce: uint64(rand.Int63()),
|
||||
}
|
||||
|
||||
err := tx.Sign(privKey)
|
||||
|
|
|
@ -5,10 +5,9 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/meshplus/bitxhub/internal/repo"
|
||||
|
||||
"github.com/meshplus/bitxhub-kit/types"
|
||||
"github.com/meshplus/bitxhub-model/pb"
|
||||
"github.com/meshplus/bitxhub/internal/repo"
|
||||
"github.com/meshplus/bitxhub/pkg/storage"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
|
|
@ -107,7 +107,7 @@ func mockBlock(blockNumber uint64, txs []*pb.Transaction) *pb.Block {
|
|||
func mockTx(data *pb.TransactionData) *pb.Transaction {
|
||||
return &pb.Transaction{
|
||||
Data: data,
|
||||
Nonce: rand.Int63(),
|
||||
Nonce: uint64(rand.Int63()),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -86,7 +86,7 @@ func generateTx() *pb.Transaction {
|
|||
Amount: 10,
|
||||
},
|
||||
Timestamp: time.Now().UnixNano(),
|
||||
Nonce: rand.Int63(),
|
||||
Nonce: uint64(rand.Int63()),
|
||||
}
|
||||
_ = tx.Sign(privKey)
|
||||
tx.TransactionHash = tx.Hash()
|
||||
|
|
|
@ -48,7 +48,7 @@ func TestNode_Start(t *testing.T) {
|
|||
Amount: 10,
|
||||
},
|
||||
Timestamp: time.Now().UnixNano(),
|
||||
Nonce: rand.Int63(),
|
||||
Nonce: uint64(rand.Int63()),
|
||||
}
|
||||
err = tx.Sign(privKey)
|
||||
require.Nil(t, err)
|
||||
|
|
|
@ -28,7 +28,7 @@ func NewContext(tx *pb.Transaction, txIndex uint64, data *pb.TransactionData, le
|
|||
TransactionIndex: txIndex,
|
||||
TransactionHash: tx.TransactionHash,
|
||||
TransactionData: data,
|
||||
Nonce: tx.Nonce,
|
||||
Nonce: int64(tx.Nonce),
|
||||
Logger: logger,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ func NewContext(tx *pb.Transaction, data *pb.TransactionData, ledger ledger.Ledg
|
|||
callee: tx.To,
|
||||
ledger: ledger,
|
||||
transactionData: data,
|
||||
nonce: tx.Nonce,
|
||||
nonce: int64(tx.Nonce),
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -88,7 +88,7 @@ func genContractTransaction(vmType pb.TransactionData_VMType, privateKey crypto.
|
|||
To: address,
|
||||
Data: td,
|
||||
Timestamp: time.Now().UnixNano(),
|
||||
Nonce: rand.Int63(),
|
||||
Nonce: uint64(rand.Int63()),
|
||||
}
|
||||
|
||||
if err := tx.Sign(privateKey); err != nil {
|
||||
|
@ -116,7 +116,7 @@ func deployContract(api api.CoreAPI, privateKey crypto.PrivateKey, contract []by
|
|||
From: from,
|
||||
Data: td,
|
||||
Timestamp: time.Now().UnixNano(),
|
||||
Nonce: rand.Int63(),
|
||||
Nonce: uint64(rand.Int63()),
|
||||
}
|
||||
|
||||
tx.TransactionHash = tx.Hash()
|
||||
|
|
Loading…
Reference in New Issue