github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/consensus/dpos/api.go (about)

     1  // Copyright 2019 The go-vnt Authors
     2  // This file is part of the go-vnt library.
     3  //
     4  // The go-vnt library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-vnt library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-vnt library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package dpos
    18  
    19  import (
    20  	"github.com/vntchain/go-vnt/common"
    21  	"github.com/vntchain/go-vnt/consensus"
    22  	"github.com/vntchain/go-vnt/core/types"
    23  	"github.com/vntchain/go-vnt/rpc"
    24  	"math/big"
    25  )
    26  
    27  // API is a user facing RPC API to allow controlling the signer and voting
    28  // mechanisms of the proof-of-authority scheme.
    29  type API struct {
    30  	chain consensus.ChainReader
    31  	dpos  *Dpos
    32  }
    33  
    34  // GetSigners retrieves the list of authorized signers at the specified block.
    35  func (api *API) GetSigners(number *rpc.BlockNumber) ([]common.Address, error) {
    36  	// Retrieve the requested block number (or current if none requested)
    37  	var header *types.Header
    38  	if number == nil || *number == rpc.LatestBlockNumber {
    39  		header = api.chain.CurrentHeader()
    40  	} else {
    41  		header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
    42  	}
    43  	// Ensure we have an actually valid block and return the signers from its snapshot
    44  	if header == nil {
    45  		return nil, errUnknownBlock
    46  	}
    47  
    48  	return header.Witnesses, nil
    49  }
    50  
    51  // GetSignersAtHash retrieves the state snapshot at a given block.
    52  func (api *API) GetSignersAtHash(hash common.Hash) ([]common.Address, error) {
    53  	header := api.chain.GetHeaderByHash(hash)
    54  	if header == nil {
    55  		return nil, errUnknownBlock
    56  	}
    57  
    58  	return header.Witnesses, nil
    59  }
    60  
    61  func (api *API) GetAllMessage() []types.ConsensusMsg {
    62  	msgs := api.dpos.bft.roundMp.getAllMsgOf(api.dpos.bft.h, api.dpos.bft.r)
    63  	return msgs
    64  }
    65  
    66  func (api *API) GetPrePrepareMsg() *types.PreprepareMsg {
    67  	msg, err := api.dpos.bft.roundMp.getPrePrepareMsg(api.dpos.bft.h, api.dpos.bft.r)
    68  	if err != nil {
    69  		return nil
    70  	}
    71  	return msg
    72  }
    73  
    74  func (api *API) GetPrepareMsgs() []*types.PrepareMsg {
    75  	msgs := api.dpos.bft.roundMp.getAllMsgOf(api.dpos.bft.h, api.dpos.bft.r)
    76  	var result []*types.PrepareMsg
    77  	for _, msg := range msgs {
    78  		if prepare, ok := msg.(*types.PrepareMsg); ok {
    79  			result = append(result, prepare)
    80  		}
    81  	}
    82  	return result
    83  }
    84  
    85  func (api *API) GetCommitMsgs() []*types.CommitMsg {
    86  	msgs := api.dpos.bft.roundMp.getAllMsgOf(api.dpos.bft.h, api.dpos.bft.r)
    87  	var result []*types.CommitMsg
    88  	for _, msg := range msgs {
    89  		if commit, ok := msg.(*types.CommitMsg); ok {
    90  			result = append(result, commit)
    91  		}
    92  	}
    93  	return result
    94  }
    95  
    96  func (api *API) GetCurrentStep() uint32 {
    97  	return api.dpos.bft.step
    98  }
    99  
   100  func (api *API) GetCurrentHeight() *big.Int {
   101  	return api.dpos.bft.h
   102  }
   103  
   104  func (api *API) GetCurrentRound() uint32 {
   105  	return api.dpos.bft.r
   106  }