github.com/bloxroute-labs/bor@v0.1.4/consensus/bor/api.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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-ethereum 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-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package bor
    18  
    19  import (
    20  	"github.com/maticnetwork/bor/common"
    21  	"github.com/maticnetwork/bor/consensus"
    22  	"github.com/maticnetwork/bor/core/types"
    23  	"github.com/maticnetwork/bor/rpc"
    24  )
    25  
    26  // API is a user facing RPC API to allow controlling the signer and voting
    27  // mechanisms of the proof-of-authority scheme.
    28  type API struct {
    29  	chain consensus.ChainReader
    30  	bor   *Bor
    31  }
    32  
    33  // GetSnapshot retrieves the state snapshot at a given block.
    34  func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error) {
    35  	// Retrieve the requested block number (or current if none requested)
    36  	var header *types.Header
    37  	if number == nil || *number == rpc.LatestBlockNumber {
    38  		header = api.chain.CurrentHeader()
    39  	} else {
    40  		header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
    41  	}
    42  	// Ensure we have an actually valid block and return its snapshot
    43  	if header == nil {
    44  		return nil, errUnknownBlock
    45  	}
    46  	return api.bor.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
    47  }
    48  
    49  // GetSnapshotAtHash retrieves the state snapshot at a given block.
    50  func (api *API) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error) {
    51  	header := api.chain.GetHeaderByHash(hash)
    52  	if header == nil {
    53  		return nil, errUnknownBlock
    54  	}
    55  	return api.bor.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
    56  }
    57  
    58  // GetSigners retrieves the list of authorized signers at the specified block.
    59  func (api *API) GetSigners(number *rpc.BlockNumber) ([]common.Address, error) {
    60  	// Retrieve the requested block number (or current if none requested)
    61  	var header *types.Header
    62  	if number == nil || *number == rpc.LatestBlockNumber {
    63  		header = api.chain.CurrentHeader()
    64  	} else {
    65  		header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
    66  	}
    67  	// Ensure we have an actually valid block and return the signers from its snapshot
    68  	if header == nil {
    69  		return nil, errUnknownBlock
    70  	}
    71  	snap, err := api.bor.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  	return snap.signers(), nil
    76  }
    77  
    78  // GetSignersAtHash retrieves the list of authorized signers at the specified block.
    79  func (api *API) GetSignersAtHash(hash common.Hash) ([]common.Address, error) {
    80  	header := api.chain.GetHeaderByHash(hash)
    81  	if header == nil {
    82  		return nil, errUnknownBlock
    83  	}
    84  	snap, err := api.bor.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	return snap.signers(), nil
    89  }
    90  
    91  // GetCurrentProposer gets the current proposer
    92  func (api *API) GetCurrentProposer() (common.Address, error) {
    93  	snap, err := api.GetSnapshot(nil)
    94  	if err != nil {
    95  		return common.Address{}, err
    96  	}
    97  	return snap.ValidatorSet.GetProposer().Address, nil
    98  }
    99  
   100  // GetCurrentValidators gets the current validators
   101  func (api *API) GetCurrentValidators() ([]*Validator, error) {
   102  	snap, err := api.GetSnapshot(nil)
   103  	if err != nil {
   104  		return make([]*Validator, 0), err
   105  	}
   106  	return snap.ValidatorSet.Validators, nil
   107  }