Skip to content

Commit a2919b5

Browse files
committed
core, eth, miner: improved tx removal & fatal error on db sync err
* core: Added GasPriceChange event * eth: When one of the DB flush methods error a fatal error log message is given. Hopefully this will prevent corrupted databases from occuring. * miner: remove transactions with low gas price. Closes bnb-chain#906, bnb-chain#903
1 parent 6674ea8 commit a2919b5

6 files changed

Lines changed: 47 additions & 31 deletions

File tree

‎common/size.go‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ func CurrencyToString(num *big.Int) string {
4444
)
4545

4646
switch {
47-
case num.Cmp(Douglas) >= 0:
48-
fin = new(big.Int).Div(num, Douglas)
49-
denom = "Douglas"
50-
case num.Cmp(Einstein) >= 0:
51-
fin = new(big.Int).Div(num, Einstein)
52-
denom = "Einstein"
5347
case num.Cmp(Ether) >= 0:
5448
fin = new(big.Int).Div(num, Ether)
5549
denom = "Ether"

‎common/size_test.go‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ func (s *SizeSuite) TestStorageSizeString(c *checker.C) {
2525
}
2626

2727
func (s *CommonSuite) TestCommon(c *checker.C) {
28-
douglas := CurrencyToString(BigPow(10, 43))
29-
einstein := CurrencyToString(BigPow(10, 22))
3028
ether := CurrencyToString(BigPow(10, 19))
3129
finney := CurrencyToString(BigPow(10, 16))
3230
szabo := CurrencyToString(BigPow(10, 13))
@@ -35,8 +33,6 @@ func (s *CommonSuite) TestCommon(c *checker.C) {
3533
ada := CurrencyToString(BigPow(10, 4))
3634
wei := CurrencyToString(big.NewInt(10))
3735

38-
c.Assert(douglas, checker.Equals, "10 Douglas")
39-
c.Assert(einstein, checker.Equals, "10 Einstein")
4036
c.Assert(ether, checker.Equals, "10 Ether")
4137
c.Assert(finney, checker.Equals, "10 Finney")
4238
c.Assert(szabo, checker.Equals, "10 Szabo")
@@ -45,13 +41,3 @@ func (s *CommonSuite) TestCommon(c *checker.C) {
4541
c.Assert(ada, checker.Equals, "10 Ada")
4642
c.Assert(wei, checker.Equals, "10 Wei")
4743
}
48-
49-
func (s *CommonSuite) TestLarge(c *checker.C) {
50-
douglaslarge := CurrencyToString(BigPow(100000000, 43))
51-
adalarge := CurrencyToString(BigPow(100000000, 4))
52-
weilarge := CurrencyToString(big.NewInt(100000000))
53-
54-
c.Assert(douglaslarge, checker.Equals, "10000E298 Douglas")
55-
c.Assert(adalarge, checker.Equals, "10000E7 Einstein")
56-
c.Assert(weilarge, checker.Equals, "100 Babbage")
57-
}

‎core/events.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package core
22

33
import (
4-
"github.com/ethereum/go-ethereum/core/types"
4+
"math/big"
5+
56
"github.com/ethereum/go-ethereum/core/state"
7+
"github.com/ethereum/go-ethereum/core/types"
68
)
79

810
// TxPreEvent is posted when a transaction enters the transaction pool.
@@ -44,6 +46,8 @@ type ChainUncleEvent struct {
4446

4547
type ChainHeadEvent struct{ Block *types.Block }
4648

49+
type GasPriceChanged struct{ Price *big.Int }
50+
4751
// Mining operation events
4852
type StartMining struct{}
4953
type TopMining struct{}

‎core/manager.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package core
22

33
import (
4+
"github.com/ethereum/go-ethereum/accounts"
45
"github.com/ethereum/go-ethereum/common"
56
"github.com/ethereum/go-ethereum/event"
67
"github.com/ethereum/go-ethereum/p2p"
78
)
89

910
type Backend interface {
11+
AccountManager() *accounts.Manager
1012
BlockProcessor() *BlockProcessor
1113
ChainManager() *ChainManager
1214
TxPool() *TxPool

‎eth/backend.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ func (s *Ethereum) Start() error {
451451
return nil
452452
}
453453

454+
// sync databases every minute. If flushing fails we exit immediatly. The system
455+
// may not continue under any circumstances.
454456
func (s *Ethereum) syncDatabases() {
455457
ticker := time.NewTicker(1 * time.Minute)
456458
done:
@@ -459,13 +461,13 @@ done:
459461
case <-ticker.C:
460462
// don't change the order of database flushes
461463
if err := s.extraDb.Flush(); err != nil {
462-
glog.V(logger.Error).Infof("error: flush extraDb: %v\n", err)
464+
glog.Fatalf("fatal error: flush extraDb: %v\n", err)
463465
}
464466
if err := s.stateDb.Flush(); err != nil {
465-
glog.V(logger.Error).Infof("error: flush stateDb: %v\n", err)
467+
glog.Fatalf("fatal error: flush stateDb: %v\n", err)
466468
}
467469
if err := s.blockDb.Flush(); err != nil {
468-
glog.V(logger.Error).Infof("error: flush blockDb: %v\n", err)
470+
glog.Fatalf("fatal error: flush blockDb: %v\n", err)
469471
}
470472
case <-s.shutdownChan:
471473
break done

‎miner/worker.go‎

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sync"
88
"sync/atomic"
99

10+
"github.com/ethereum/go-ethereum/accounts"
1011
"github.com/ethereum/go-ethereum/common"
1112
"github.com/ethereum/go-ethereum/core"
1213
"github.com/ethereum/go-ethereum/core/state"
@@ -253,7 +254,12 @@ func (self *worker) makeCurrent() {
253254
func (w *worker) setGasPrice(p *big.Int) {
254255
w.mu.Lock()
255256
defer w.mu.Unlock()
256-
w.gasPrice = p
257+
258+
// calculate the minimal gas price the miner accepts when sorting out transactions.
259+
const pct = int64(90)
260+
w.gasPrice = gasprice(p, pct)
261+
262+
w.mux.Post(core.GasPriceChanged{w.gasPrice})
257263
}
258264

259265
func (self *worker) commitNewWork() {
@@ -269,27 +275,40 @@ func (self *worker) commitNewWork() {
269275
transactions := self.eth.TxPool().GetTransactions()
270276
sort.Sort(types.TxByNonce{transactions})
271277

278+
accounts, _ := self.eth.AccountManager().Accounts()
272279
// Keep track of transactions which return errors so they can be removed
273280
var (
274281
remove = set.New()
275282
tcount = 0
276283
ignoredTransactors = set.New()
284+
lowGasTransactors = set.New()
285+
ownedAccounts = accountAddressesSet(accounts)
286+
lowGasTxs types.Transactions
277287
)
278288

279-
const pct = int64(90)
280-
// calculate the minimal gas price the miner accepts when sorting out transactions.
281-
minprice := gasprice(self.gasPrice, pct)
282289
for _, tx := range transactions {
283290
// We can skip err. It has already been validated in the tx pool
284291
from, _ := tx.From()
285292

286293
// check if it falls within margin
287-
if tx.GasPrice().Cmp(minprice) < 0 {
294+
if tx.GasPrice().Cmp(self.gasPrice) < 0 {
288295
// ignore the transaction and transactor. We ignore the transactor
289296
// because nonce will fail after ignoring this transaction so there's
290297
// no point
291-
ignoredTransactors.Add(from)
292-
glog.V(logger.Info).Infof("transaction(%x) below gas price (<%d%% ask price). All sequential txs from this address(%x) will fail\n", tx.Hash().Bytes()[:4], pct, from[:4])
298+
lowGasTransactors.Add(from)
299+
300+
glog.V(logger.Info).Infof("transaction(%x) below gas price (tx=%v ask=%v). All sequential txs from this address(%x) will be ignored\n", tx.Hash().Bytes()[:4], common.CurrencyToString(tx.GasPrice()), common.CurrencyToString(self.gasPrice), from[:4])
301+
}
302+
303+
// Continue with the next transaction if the transaction sender is included in
304+
// the low gas tx set. This will also remove the tx and all sequential transaction
305+
// from this transactor
306+
if lowGasTransactors.Has(from) {
307+
// add tx to the low gas set. This will be removed at the end of the run
308+
// owned accounts are ignored
309+
if !ownedAccounts.Has(from) {
310+
lowGasTxs = append(lowGasTxs, tx)
311+
}
293312
continue
294313
}
295314

@@ -327,6 +346,7 @@ func (self *worker) commitNewWork() {
327346
tcount++
328347
}
329348
}
349+
self.eth.TxPool().RemoveTransactions(lowGasTxs)
330350

331351
var (
332352
uncles []*types.Header
@@ -423,3 +443,11 @@ func gasprice(price *big.Int, pct int64) *big.Int {
423443
p.Mul(p, big.NewInt(pct))
424444
return p
425445
}
446+
447+
func accountAddressesSet(accounts []accounts.Account) *set.Set {
448+
accountSet := set.New()
449+
for _, account := range accounts {
450+
accountSet.Add(common.BytesToAddress(account.Address))
451+
}
452+
return accountSet
453+
}

0 commit comments

Comments
 (0)