mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-04 19:13:47 +00:00
dev: cleanup isValidatorAction logic
This commit is contained in:
parent
4be5477f64
commit
9a56de6d84
4 changed files with 2 additions and 80 deletions
|
|
@ -1204,26 +1204,6 @@ func (c *Bor) SetHeimdallClient(h IHeimdallClient) {
|
|||
c.HeimdallClient = h
|
||||
}
|
||||
|
||||
func (c *Bor) IsValidatorAction(chain consensus.ChainReader, from common.Address, tx *types.Transaction) bool {
|
||||
header := chain.CurrentHeader()
|
||||
validators, err := c.GetCurrentValidators(header.Number.Uint64(), header.Number.Uint64()+1)
|
||||
if err != nil {
|
||||
log.Error("Failed fetching snapshot", err)
|
||||
return false
|
||||
}
|
||||
|
||||
isValidator := false
|
||||
for _, validator := range validators {
|
||||
if bytes.Compare(validator.Address.Bytes(), from.Bytes()) == 0 {
|
||||
isValidator = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return isValidator && (isProposeSpanAction(tx, chain.Config().Bor.ValidatorContract) ||
|
||||
isProposeStateAction(tx, chain.Config().Bor.StateReceiverContract))
|
||||
}
|
||||
|
||||
func isProposeSpanAction(tx *types.Transaction, validatorContract string) bool {
|
||||
// keccak256('proposeSpan()').slice(0, 4)
|
||||
proposeSpanSig, _ := hex.DecodeString("4b0e4d17")
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import (
|
|||
"math/big"
|
||||
"testing"
|
||||
|
||||
"github.com/maticnetwork/bor/common"
|
||||
"github.com/maticnetwork/bor/consensus/bor"
|
||||
"github.com/maticnetwork/bor/core/rawdb"
|
||||
"github.com/maticnetwork/bor/crypto"
|
||||
|
|
@ -18,7 +17,7 @@ import (
|
|||
"github.com/maticnetwork/bor/mocks"
|
||||
)
|
||||
|
||||
func TestCommitSpan(t *testing.T) {
|
||||
func TestInsertingSpanSizeBlocks(t *testing.T) {
|
||||
init := buildEthereumInstance(t, rawdb.NewMemoryDatabase())
|
||||
chain := init.ethereum.BlockChain()
|
||||
engine := init.ethereum.Engine()
|
||||
|
|
@ -31,7 +30,7 @@ func TestCommitSpan(t *testing.T) {
|
|||
var to int64
|
||||
|
||||
// Insert sprintSize # of blocks so that span is fetched at the start of a new sprint
|
||||
for i := uint64(1); i <= sprintSize; i++ {
|
||||
for i := uint64(1); i <= spanSize; i++ {
|
||||
block = buildNextBlock(t, _bor, chain, block, nil, init.genesis.Config.Bor)
|
||||
insertNewBlock(t, chain, block)
|
||||
if i == sprintSize-1 {
|
||||
|
|
@ -55,61 +54,6 @@ func TestCommitSpan(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestIsValidatorAction(t *testing.T) {
|
||||
init := buildEthereumInstance(t, rawdb.NewMemoryDatabase())
|
||||
chain := init.ethereum.BlockChain()
|
||||
engine := init.ethereum.Engine()
|
||||
_bor := engine.(*bor.Bor)
|
||||
h, heimdallSpan := getMockedHeimdallClient(t)
|
||||
_bor.SetHeimdallClient(h)
|
||||
|
||||
proposeStateData, _ := hex.DecodeString("ede01f170000000000000000000000000000000000000000000000000000000000000000")
|
||||
proposeSpanData, _ := hex.DecodeString("4b0e4d17")
|
||||
var tx *types.Transaction
|
||||
tx = types.NewTransaction(
|
||||
0,
|
||||
common.HexToAddress(chain.Config().Bor.StateReceiverContract),
|
||||
big.NewInt(0), 0, big.NewInt(0),
|
||||
proposeStateData,
|
||||
)
|
||||
assert.True(t, _bor.IsValidatorAction(chain, addr, tx))
|
||||
|
||||
tx = types.NewTransaction(
|
||||
0,
|
||||
common.HexToAddress(chain.Config().Bor.ValidatorContract),
|
||||
big.NewInt(0), 0, big.NewInt(0),
|
||||
proposeSpanData,
|
||||
)
|
||||
assert.True(t, _bor.IsValidatorAction(chain, addr, tx))
|
||||
|
||||
db := init.ethereum.ChainDb()
|
||||
block := init.genesis.ToBlock(db)
|
||||
|
||||
for i := uint64(1); i <= spanSize; i++ {
|
||||
block = buildNextBlock(t, _bor, chain, block, nil, init.genesis.Config.Bor)
|
||||
insertNewBlock(t, chain, block)
|
||||
}
|
||||
|
||||
for _, validator := range heimdallSpan.SelectedProducers {
|
||||
_addr := validator.Address
|
||||
tx = types.NewTransaction(
|
||||
0,
|
||||
common.HexToAddress(chain.Config().Bor.StateReceiverContract),
|
||||
big.NewInt(0), 0, big.NewInt(0),
|
||||
proposeStateData,
|
||||
)
|
||||
assert.True(t, _bor.IsValidatorAction(chain, _addr, tx))
|
||||
|
||||
tx = types.NewTransaction(
|
||||
0,
|
||||
common.HexToAddress(chain.Config().Bor.ValidatorContract),
|
||||
big.NewInt(0), 0, big.NewInt(0),
|
||||
proposeSpanData,
|
||||
)
|
||||
assert.True(t, _bor.IsValidatorAction(chain, _addr, tx))
|
||||
}
|
||||
}
|
||||
|
||||
func TestOutOfTurnSigning(t *testing.T) {
|
||||
init := buildEthereumInstance(t, rawdb.NewMemoryDatabase())
|
||||
chain := init.ethereum.BlockChain()
|
||||
|
|
|
|||
|
|
@ -119,7 +119,6 @@ type Engine interface {
|
|||
// Bor is a consensus engine developed by folks at Matic Network
|
||||
type Bor interface {
|
||||
Engine
|
||||
IsValidatorAction(chain ChainReader, from common.Address, tx *types.Transaction) bool
|
||||
CancelActiveSealingOp()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -531,7 +531,6 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
|
|||
// Drop non-local transactions under our own minimal accepted gas price
|
||||
local = local || pool.locals.contains(from) // account may be local even if the transaction arrived from the network
|
||||
if !local &&
|
||||
!pool.chain.Engine().(consensus.Bor).IsValidatorAction(pool.chain.(consensus.ChainReader), from, tx) &&
|
||||
pool.gasPrice.Cmp(tx.GasPrice()) > 0 {
|
||||
return ErrUnderpriced
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue