go-ethereum/consensus/dpos/api.go
2018-08-09 12:47:24 +08:00

71 lines
2.2 KiB
Go

// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package dpos
import (
"github.com/meitu/go-ethereum/common"
"github.com/meitu/go-ethereum/consensus"
"github.com/meitu/go-ethereum/core/types"
"github.com/meitu/go-ethereum/rpc"
"math/big"
)
// API is a user facing RPC API to allow controlling the delegate and voting
// mechanisms of the delegated-proof-of-stake
type API struct {
chain consensus.ChainReader
dpos *Dpos
}
// GetValidators retrieves the list of the validators at specified block
func (api *API) GetValidators(number *rpc.BlockNumber) ([]common.Address, error) {
var header *types.Header
if number == nil || *number == rpc.LatestBlockNumber {
header = api.chain.CurrentHeader()
} else {
header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
}
if header == nil {
return nil, errUnknownBlock
}
epochTrie, err := types.NewEpochTrie(header.DposContext.EpochHash, api.dpos.db)
if err != nil {
return nil, err
}
dposContext := types.DposContext{}
dposContext.SetEpoch(epochTrie)
validators, err := dposContext.GetValidators()
if err != nil {
return nil, err
}
return validators, nil
}
// GetConfirmedBlockNumber retrieves the latest irreversible block
func (api *API) GetConfirmedBlockNumber() (*big.Int, error) {
var err error
header := api.dpos.confirmedBlockHeader
if header == nil {
header, err = api.dpos.loadConfirmedBlockHeader(api.chain)
if err != nil {
return nil, err
}
}
return header.Number, nil
}