github.hscsec.cn/scroll-tech/go-ethereum@v1.9.7/consensus/clique/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 clique
    18  
    19  import (
    20  	"github.com/ethereum/go-ethereum/common"
    21  	"github.com/ethereum/go-ethereum/consensus"
    22  	"github.com/ethereum/go-ethereum/core/types"
    23  	"github.com/ethereum/go-ethereum/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  	clique *Clique
    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.clique.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.clique.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.clique.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.clique.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  // Proposals returns the current proposals the node tries to uphold and vote on.
    92  func (api *API) Proposals() map[common.Address]bool {
    93  	api.clique.lock.RLock()
    94  	defer api.clique.lock.RUnlock()
    95  
    96  	proposals := make(map[common.Address]bool)
    97  	for address, auth := range api.clique.proposals {
    98  		proposals[address] = auth
    99  	}
   100  	return proposals
   101  }
   102  
   103  // Propose injects a new authorization proposal that the signer will attempt to
   104  // push through.
   105  func (api *API) Propose(address common.Address, auth bool) {
   106  	api.clique.lock.Lock()
   107  	defer api.clique.lock.Unlock()
   108  
   109  	api.clique.proposals[address] = auth
   110  }
   111  
   112  // Discard drops a currently running proposal, stopping the signer from casting
   113  // further votes (either for or against).
   114  func (api *API) Discard(address common.Address) {
   115  	api.clique.lock.Lock()
   116  	defer api.clique.lock.Unlock()
   117  
   118  	delete(api.clique.proposals, address)
   119  }