Skip to content

Commit

Permalink
consensus/clique: implement getSigner
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusVanDerWijden committed Jun 4, 2021
1 parent 890673a commit 168c82d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
44 changes: 35 additions & 9 deletions consensus/clique/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
package clique

import (
"bytes"
"errors"
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
)

Expand Down Expand Up @@ -178,16 +180,40 @@ func (api *API) Status() (*status, error) {
}, nil
}

// GetSignerForBlock returns the signer for a specific clique block.
func (api *API) GetSignerForBlock(blockNrOrHash *rpc.BlockNumberOrHash) (common.Address, error) {
var header *types.Header
if blockNrOrHash == nil {
header = api.chain.CurrentHeader()
} else if hash, ok := blockNrOrHash.Hash(); ok {
header = api.chain.GetHeaderByHash(hash)
} else if number, ok := blockNrOrHash.Number(); ok {
header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
type SignerBlock struct {
*rpc.BlockNumberOrHash
RLP string `json:"rlp,omitempty"`
}

// GetSigner returns the signer for a specific clique block.
// Can be called with either a blocknumber, blockhash or an rlp encoded blob.
// The RLP encoded blob can either be a block or a header.
func (api *API) GetSigner(rlpOrBlockNr *SignerBlock) (common.Address, error) {
if len(rlpOrBlockNr.RLP) == 0 {
blockNrOrHash := rlpOrBlockNr.BlockNumberOrHash
var header *types.Header
if blockNrOrHash == nil {
header = api.chain.CurrentHeader()
} else if hash, ok := blockNrOrHash.Hash(); ok {
header = api.chain.GetHeaderByHash(hash)
} else if number, ok := blockNrOrHash.Number(); ok {
header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
}
return getSigner(header)
}
blob := common.Hex2Bytes(rlpOrBlockNr.RLP)
block := new(types.Block)
if err := rlp.Decode(bytes.NewReader(blob), block); err == nil {
return getSigner(block.Header())
}
header := new(types.Header)
if err := rlp.Decode(bytes.NewReader(blob), header); err == nil {
return getSigner(header)
}
return common.Address{}, fmt.Errorf("could not decode rlp")
}

func getSigner(header *types.Header) (common.Address, error) {
if header == nil {
return common.Address{}, errors.New("could not find header")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ web3._extend({
params: 0
}),
new web3._extend.Method({
name: 'getSignerForBlock',
call: 'clique_getSignerForBlock',
name: 'getSigner',
call: 'clique_getSigner',
params: 1,
inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter]
}),
Expand Down

0 comments on commit 168c82d

Please sign in to comment.