From 27fc66ef249994b07aebfa7c8bc5a7b4d31a2eea Mon Sep 17 00:00:00 2001 From: atvanguard <93arpit@gmail.com> Date: Thu, 27 Feb 2020 16:54:01 +0530 Subject: [PATCH] Add bor.isValidatorAction and call it while validating underpriced tx --- consensus/bor/bor.go | 18 ++++++++++++++++++ core/tx_pool.go | 15 +++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index 0daf9353b0..1bdf00d811 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -1218,6 +1218,24 @@ func (c *Bor) CommitStates( return nil } +func (c *Bor) isValidatorAction(chain consensus.ChainReader, from common.Address, tx *types.Transaction) bool { + header := chain.CurrentHeader() + snap, err := c.snapshot(chain, header.Number.Uint64(), header.Hash(), nil) + if err != nil { + log.Error("Failed fetching snapshot", err) + } + + _isValidatorAction := false + for _, validator := range snap.ValidatorSet.Validators { + if bytes.Compare(validator.Address.Bytes(), from.Bytes()) == 0 { + _isValidatorAction = true + break + } + } + // @todo only either of proposeState and proposeSpan should pass this check + return _isValidatorAction +} + // // Private methods // diff --git a/core/tx_pool.go b/core/tx_pool.go index a089394a18..39d66b5286 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -27,6 +27,7 @@ import ( "github.com/maticnetwork/bor/common" "github.com/maticnetwork/bor/common/prque" + "github.com/maticnetwork/bor/consensus" "github.com/maticnetwork/bor/core/state" "github.com/maticnetwork/bor/core/types" "github.com/maticnetwork/bor/event" @@ -116,14 +117,22 @@ const ( TxStatusIncluded ) +// bor acts as a way to be able to type cast consensus.Engine; +// since importing "github.com/maticnetwork/bor/consensus/bor" results in a cyclic dependency +type bor interface { + isValidatorAction(chain consensus.ChainReader, from common.Address, tx *types.Transaction) bool +} + // blockChain provides the state of blockchain and current gas limit to do // some pre checks in tx pool and event subscribers. type blockChain interface { CurrentBlock() *types.Block GetBlock(hash common.Hash, number uint64) *types.Block StateAt(root common.Hash) (*state.StateDB, error) - SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription + + Engine() consensus.Engine + CurrentHeader() *types.Header } // TxPoolConfig are the configuration parameters of the transaction pool. @@ -527,7 +536,9 @@ 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.gasPrice.Cmp(tx.GasPrice()) > 0 { + if !local && + !pool.chain.Engine().(bor).isValidatorAction(pool.chain.(consensus.ChainReader), from, tx) && + pool.gasPrice.Cmp(tx.GasPrice()) > 0 { return ErrUnderpriced } // Ensure the transaction adheres to nonce ordering