mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-28 07:36:44 +00:00
dev: Change wording for UnauthorizedSignerError, remove errRecentlySigned
This commit is contained in:
parent
767fe584b5
commit
c7ea09a019
5 changed files with 51 additions and 43 deletions
|
|
@ -121,10 +121,6 @@ var (
|
|||
// errOutOfRangeChain is returned if an authorization list is attempted to
|
||||
// be modified via out-of-range or non-contiguous headers.
|
||||
errOutOfRangeChain = errors.New("out of range or non-contiguous chain")
|
||||
|
||||
// errRecentlySigned is returned if a header is signed by an authorized entity
|
||||
// that already signed a header recently, thus is temporarily not allowed to.
|
||||
errRecentlySigned = errors.New("recently signed")
|
||||
)
|
||||
|
||||
// SignerFn is a signer callback function to request a header to be signed by a
|
||||
|
|
|
|||
|
|
@ -160,3 +160,30 @@ func TestOutOfTurnSigning(t *testing.T) {
|
|||
_, err = chain.InsertChain([]*types.Block{block})
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestSignerNotFound(t *testing.T) {
|
||||
init := buildEthereumInstance(t, rawdb.NewMemoryDatabase())
|
||||
chain := init.ethereum.BlockChain()
|
||||
engine := init.ethereum.Engine()
|
||||
_bor := engine.(*bor.Bor)
|
||||
|
||||
res, _ := loadSpanFromFile(t)
|
||||
h := &mocks.IHeimdallClient{}
|
||||
h.On("FetchWithRetry", "bor", "span", "1").Return(res, nil)
|
||||
_bor.SetHeimdallClient(h)
|
||||
|
||||
db := init.ethereum.ChainDb()
|
||||
block := init.genesis.ToBlock(db)
|
||||
|
||||
// random signer account that is not a part of the validator set
|
||||
signer := "3714d99058cd64541433d59c6b391555b2fd9b54629c2b717a6c9c00d1127b6b"
|
||||
signerKey, _ := hex.DecodeString(signer)
|
||||
key, _ = crypto.HexToECDSA(signer)
|
||||
addr = crypto.PubkeyToAddress(key.PublicKey)
|
||||
|
||||
block = buildNextBlock(t, _bor, chain, block, signerKey, init.genesis.Config.Bor)
|
||||
_, err := chain.InsertChain([]*types.Block{block})
|
||||
assert.Equal(t,
|
||||
*err.(*bor.UnauthorizedSignerError),
|
||||
bor.UnauthorizedSignerError{Number: 1, Signer: addr.Bytes()})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -84,9 +84,9 @@ func TestGetSignerSuccessionNumber_ProposerNotFound(t *testing.T) {
|
|||
signer := snap.ValidatorSet.Validators[3].Address
|
||||
_, err := snap.GetSignerSuccessionNumber(signer)
|
||||
assert.NotNil(t, err)
|
||||
e, ok := err.(*bor.ProposerNotFoundError)
|
||||
e, ok := err.(*bor.UnauthorizedProposerError)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, dummyProposerAddress, e.Address)
|
||||
assert.Equal(t, dummyProposerAddress.Bytes(), e.Proposer)
|
||||
}
|
||||
|
||||
func TestGetSignerSuccessionNumber_SignerNotFound(t *testing.T) {
|
||||
|
|
@ -97,9 +97,9 @@ func TestGetSignerSuccessionNumber_SignerNotFound(t *testing.T) {
|
|||
dummySignerAddress := randomAddress()
|
||||
_, err := snap.GetSignerSuccessionNumber(dummySignerAddress)
|
||||
assert.NotNil(t, err)
|
||||
e, ok := err.(*bor.SignerNotFoundError)
|
||||
e, ok := err.(*bor.UnauthorizedSignerError)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, dummySignerAddress, e.Address)
|
||||
assert.Equal(t, dummySignerAddress.Bytes(), e.Signer)
|
||||
}
|
||||
|
||||
func buildRandomValidatorSet(numVals int) []*bor.Validator {
|
||||
|
|
|
|||
|
|
@ -2,30 +2,8 @@ package bor
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/maticnetwork/bor/common"
|
||||
)
|
||||
|
||||
// Will include any new bor consensus errors here in an attempt to make error messages more descriptive
|
||||
|
||||
// ProposerNotFoundError is returned if the given proposer address is not present in the validator set
|
||||
type ProposerNotFoundError struct {
|
||||
Address common.Address
|
||||
}
|
||||
|
||||
func (e *ProposerNotFoundError) Error() string {
|
||||
return fmt.Sprintf("Proposer: %s not found", e.Address.Hex())
|
||||
}
|
||||
|
||||
// SignerNotFoundError is returned when the signer address is not present in the validator set
|
||||
type SignerNotFoundError struct {
|
||||
Address common.Address
|
||||
}
|
||||
|
||||
func (e *SignerNotFoundError) Error() string {
|
||||
return fmt.Sprintf("Signer: %s not found", e.Address.Hex())
|
||||
}
|
||||
|
||||
// TotalVotingPowerExceededError is returned when the maximum allowed total voting power is exceeded
|
||||
type TotalVotingPowerExceededError struct {
|
||||
Sum int64
|
||||
|
|
@ -99,7 +77,21 @@ func (e *BlockTooSoonError) Error() string {
|
|||
)
|
||||
}
|
||||
|
||||
// UnauthorizedSignerError is returned if a header is signed by a non-authorized entity.
|
||||
// UnauthorizedProposerError is returned if a header is [being] signed by an unauthorized entity.
|
||||
type UnauthorizedProposerError struct {
|
||||
Number uint64
|
||||
Proposer []byte
|
||||
}
|
||||
|
||||
func (e *UnauthorizedProposerError) Error() string {
|
||||
return fmt.Sprintf(
|
||||
"Proposer 0x%x is not a part of the producer set at block %d",
|
||||
e.Proposer,
|
||||
e.Number,
|
||||
)
|
||||
}
|
||||
|
||||
// UnauthorizedSignerError is returned if a header is [being] signed by an unauthorized entity.
|
||||
type UnauthorizedSignerError struct {
|
||||
Number uint64
|
||||
Signer []byte
|
||||
|
|
@ -107,9 +99,9 @@ type UnauthorizedSignerError struct {
|
|||
|
||||
func (e *UnauthorizedSignerError) Error() string {
|
||||
return fmt.Sprintf(
|
||||
"Validator set for block %d doesn't contain the signer 0x%x\n",
|
||||
e.Number,
|
||||
"Signer 0x%x is not a part of the producer set at block %d",
|
||||
e.Signer,
|
||||
e.Number,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -26,7 +26,6 @@ import (
|
|||
"github.com/maticnetwork/bor/core/types"
|
||||
"github.com/maticnetwork/bor/ethdb"
|
||||
"github.com/maticnetwork/bor/internal/ethapi"
|
||||
"github.com/maticnetwork/bor/log"
|
||||
"github.com/maticnetwork/bor/params"
|
||||
)
|
||||
|
||||
|
|
@ -191,24 +190,18 @@ func (s *Snapshot) GetSignerSuccessionNumber(signer common.Address) (int, error)
|
|||
proposer := s.ValidatorSet.GetProposer().Address
|
||||
proposerIndex, _ := s.ValidatorSet.GetByAddress(proposer)
|
||||
if proposerIndex == -1 {
|
||||
return -1, &ProposerNotFoundError{proposer}
|
||||
return -1, &UnauthorizedProposerError{s.Number, proposer.Bytes()}
|
||||
}
|
||||
signerIndex, _ := s.ValidatorSet.GetByAddress(signer)
|
||||
if signerIndex == -1 {
|
||||
return -1, &SignerNotFoundError{signer}
|
||||
return -1, &UnauthorizedSignerError{s.Number, signer.Bytes()}
|
||||
}
|
||||
limit := len(validators)/2 + 1
|
||||
|
||||
tempIndex := signerIndex
|
||||
if proposerIndex != tempIndex && limit > 0 {
|
||||
if proposerIndex != tempIndex {
|
||||
if tempIndex < proposerIndex {
|
||||
tempIndex = tempIndex + len(validators)
|
||||
}
|
||||
|
||||
if tempIndex-proposerIndex > limit {
|
||||
log.Info("errRecentlySigned", "proposerIndex", validators[proposerIndex].Address.Hex(), "signerIndex", validators[signerIndex].Address.Hex())
|
||||
return -1, errRecentlySigned
|
||||
}
|
||||
}
|
||||
return tempIndex - proposerIndex, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue