From 9a56de6d84c7a28bb4c9e308622070058045901b Mon Sep 17 00:00:00 2001 From: atvanguard <93arpit@gmail.com> Date: Sun, 17 May 2020 17:33:59 +0530 Subject: [PATCH] dev: cleanup isValidatorAction logic --- consensus/bor/bor.go | 20 ---------- consensus/bor/bor_test/bor_test.go | 60 +----------------------------- consensus/consensus.go | 1 - core/tx_pool.go | 1 - 4 files changed, 2 insertions(+), 80 deletions(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index 03fddbf5d..43ebe93fd 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -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") diff --git a/consensus/bor/bor_test/bor_test.go b/consensus/bor/bor_test/bor_test.go index 72b40d956..56b2e38f8 100644 --- a/consensus/bor/bor_test/bor_test.go +++ b/consensus/bor/bor_test/bor_test.go @@ -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() diff --git a/consensus/consensus.go b/consensus/consensus.go index 0620cff05..689c840da 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -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() } diff --git a/core/tx_pool.go b/core/tx_pool.go index 1462c2324..a1822ec54 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -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 } -- GitLab