OSDN Git Service

opt code add_get_blocker_api
authorshenao78 <shenao.78@163.com>
Thu, 13 Jun 2019 07:00:17 +0000 (15:00 +0800)
committershenao78 <shenao.78@163.com>
Thu, 13 Jun 2019 07:00:17 +0000 (15:00 +0800)
api/api.go
api/bbft.go
api/block_retrieve.go
protocol/consensus_node_manager.go

index 9a6f641..88f43e5 100644 (file)
@@ -302,7 +302,6 @@ func (a *API) buildHandler() {
        m.Handle("/get-merkle-proof", jsonHandler(a.getMerkleProof))
 
        m.Handle("/get-vote-result", jsonHandler(a.getVoteResult))
-       m.Handle("/get-blocker", jsonHandler(a.getBlocker))
 
        m.HandleFunc("/websocket-subscribe", a.websocketHandler)
 
index 30b4197..46f2239 100644 (file)
@@ -4,7 +4,6 @@ import (
        "sort"
 
        chainjson "github.com/vapor/encoding/json"
-       "github.com/vapor/protocol/bc/types"
 )
 
 type voteInfo struct {
@@ -47,31 +46,3 @@ func (a *API) getVoteResult(req struct {
        sort.Sort(voteInfoSlice(voteInfos))
        return NewSuccessResponse(voteInfos)
 }
-
-type getBlockerResp struct {
-       PubKey string `json:"pub_key"`
-}
-
-func (a *API) getBlocker(req struct {
-       BlockHash   chainjson.HexBytes `json:"block_hash"`
-       BlockHeight uint64             `json:"block_height"`
-}) Response {
-       var blockHeader *types.BlockHeader
-       var err error
-       if len(req.BlockHash) == 32 {
-               blockHash := hexBytesToHash(req.BlockHash)
-               blockHeader, err = a.chain.GetHeaderByHash(&blockHash)
-       } else {
-               blockHeader, err = a.chain.GetHeaderByHeight(req.BlockHeight)
-       }
-
-       if err != nil {
-               return NewErrorResponse(err)
-       }
-
-       pubKey, err := a.chain.GetBlocker(&blockHeader.PreviousBlockHash, blockHeader.Timestamp)
-       if err != nil {
-               return NewErrorResponse(err)
-       }
-       return NewSuccessResponse(&getBlockerResp{PubKey: pubKey})
-}
index 1b8684e..56fdbdc 100644 (file)
@@ -48,6 +48,7 @@ type GetBlockResp struct {
        PreviousBlockHash      *bc.Hash             `json:"previous_block_hash"`
        Timestamp              uint64               `json:"timestamp"`
        Witness                []chainjson.HexBytes `json:"witness"`
+       Blocker                string               `json:"blocker"`
        TransactionsMerkleRoot *bc.Hash             `json:"transaction_merkle_root"`
        TransactionStatusHash  *bc.Hash             `json:"transaction_status_hash"`
        Transactions           []*BlockTx           `json:"transactions"`
@@ -72,6 +73,11 @@ func (a *API) getBlock(ins BlockReq) Response {
                witness[i] = w
        }
 
+       blocker, err := a.chain.GetBlocker(&block.PreviousBlockHash, block.Timestamp)
+       if err != nil {
+               return NewErrorResponse(err)
+       }
+
        resp := &GetBlockResp{
                Hash:                   &blockHash,
                Size:                   uint64(len(rawBlock)),
@@ -80,6 +86,7 @@ func (a *API) getBlock(ins BlockReq) Response {
                PreviousBlockHash:      &block.PreviousBlockHash,
                Timestamp:              block.Timestamp,
                Witness:                witness,
+               Blocker:                blocker,
                TransactionsMerkleRoot: &block.TransactionsMerkleRoot,
                TransactionStatusHash:  &block.TransactionStatusHash,
                Transactions:           []*BlockTx{},
index a4b59e6..4fe6638 100644 (file)
@@ -49,25 +49,24 @@ func (c *consensusNodeManager) getBlocker(prevBlockHash *bc.Hash, timeStamp uint
        }
 
        startTimestamp := prevVoteRoundLastBlock.Timestamp + consensus.BlockTimeInterval
-
+       order := getBlockerOrder(startTimestamp, timeStamp, uint64(len(consensusNodeMap)))
        for xPub, consensusNode := range consensusNodeMap {
-               begin := getLastBlockTimeInTimeRange(startTimestamp, timeStamp, consensusNode.Order, uint64(len(consensusNodeMap)))
-               end := begin + consensus.BlockNumEachNode*consensus.BlockTimeInterval
-               if timeStamp >= begin && timeStamp < end {
+               if consensusNode.Order == order {
                        return xPub, nil
                }
        }
+
        // impossible occur
        return "", errors.New("can not find blocker by given timestamp")
 }
 
-func getLastBlockTimeInTimeRange(startTimestamp, endTimestamp, order, numOfConsensusNode uint64) uint64 {
+func getBlockerOrder(startTimestamp, blockTimestamp, numOfConsensusNode uint64) uint64 {
        // One round of product block time for all consensus nodes
        roundBlockTime := consensus.BlockNumEachNode * numOfConsensusNode * consensus.BlockTimeInterval
        // The start time of the last round of product block
-       lastRoundStartTime := startTimestamp + (endTimestamp-startTimestamp)/roundBlockTime*roundBlockTime
-       // The time of product block of the consensus in last round
-       return lastRoundStartTime + order*(consensus.BlockNumEachNode*consensus.BlockTimeInterval)
+       lastRoundStartTime := startTimestamp + (blockTimestamp-startTimestamp)/roundBlockTime*roundBlockTime
+       // Order of blocker
+       return (blockTimestamp - lastRoundStartTime)/(consensus.BlockNumEachNode*consensus.BlockTimeInterval)
 }
 
 func (c *consensusNodeManager) getPrevRoundLastBlock(prevBlockHash *bc.Hash) (*state.BlockNode, error) {