Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consensus/clique: implement getSignerForBlock #22987

Merged
merged 7 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions consensus/clique/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +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 @@ -175,3 +179,54 @@ func (api *API) Status() (*status, error) {
NumBlocks: numBlocks,
}, nil
}

type SignerBlock struct {
MariusVanDerWijden marked this conversation as resolved.
Show resolved Hide resolved
*rpc.BlockNumberOrHash
RLP string `json:"rlp,omitempty"`
MariusVanDerWijden marked this conversation as resolved.
Show resolved Hide resolved
}

// 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)
MariusVanDerWijden marked this conversation as resolved.
Show resolved Hide resolved
block := new(types.Block)
if err := rlp.Decode(bytes.NewReader(blob), block); err == nil {
MariusVanDerWijden marked this conversation as resolved.
Show resolved Hide resolved
return getSigner(block.Header())
}
header := new(types.Header)
if err := rlp.Decode(bytes.NewReader(blob), header); err == nil {
MariusVanDerWijden marked this conversation as resolved.
Show resolved Hide resolved
return getSigner(header)
MariusVanDerWijden marked this conversation as resolved.
Show resolved Hide resolved
}
return common.Address{}, fmt.Errorf("could not decode rlp")
MariusVanDerWijden marked this conversation as resolved.
Show resolved Hide resolved
}

func getSigner(header *types.Header) (common.Address, error) {
MariusVanDerWijden marked this conversation as resolved.
Show resolved Hide resolved
if header == nil {
return common.Address{}, errors.New("could not find header")
}
MariusVanDerWijden marked this conversation as resolved.
Show resolved Hide resolved
if len(header.Extra)-extraSeal < 0 {
return common.Address{}, errors.New("invalid extra data in header")
}
signature := header.Extra[len(header.Extra)-extraSeal:]
// Recover the public key and the Ethereum address
pubkey, err := crypto.Ecrecover(SealHash(header).Bytes(), signature)
if err != nil {
return common.Address{}, err
}
var signer common.Address
copy(signer[:], crypto.Keccak256(pubkey[1:])[12:])
return signer, nil
}
6 changes: 6 additions & 0 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ web3._extend({
call: 'clique_status',
params: 0
}),
new web3._extend.Method({
name: 'getSigner',
call: 'clique_getSigner',
params: 1,
inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter]
MariusVanDerWijden marked this conversation as resolved.
Show resolved Hide resolved
}),
],
properties: [
new web3._extend.Property({
Expand Down