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)
"sort"
chainjson "github.com/vapor/encoding/json"
- "github.com/vapor/protocol/bc/types"
)
type voteInfo 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})
-}
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"`
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)),
PreviousBlockHash: &block.PreviousBlockHash,
Timestamp: block.Timestamp,
Witness: witness,
+ Blocker: blocker,
TransactionsMerkleRoot: &block.TransactionsMerkleRoot,
TransactionStatusHash: &block.TransactionStatusHash,
Transactions: []*BlockTx{},
}
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) {