Skip to content

Commit

Permalink
cherry-pick txpool optimisation changes
Browse files Browse the repository at this point in the history
  • Loading branch information
0xsharma authored and manav2401 committed Mar 14, 2023
1 parent cf4ebfa commit 741debf
Show file tree
Hide file tree
Showing 32 changed files with 3,394 additions and 494 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ ios:
@echo "Import \"$(GOBIN)/Geth.framework\" to use the library."

test:
$(GOTEST) --timeout 5m -shuffle=on -cover -coverprofile=cover.out $(TESTALL)
$(GOTEST) --timeout 5m -shuffle=on -cover -short -coverprofile=cover.out -covermode=atomic $(TESTALL)

test-txpool-race:
$(GOTEST) -run=TestPoolMiningDataRaces --timeout 600m -race -v ./core/

test-race:
$(GOTEST) --timeout 15m -race -shuffle=on $(TESTALL)
Expand All @@ -75,7 +78,7 @@ lint:

lintci-deps:
rm -f ./build/bin/golangci-lint
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ./build/bin v1.48.0
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ./build/bin v1.50.1

goimports:
goimports -local "$(PACKAGE)" -w .
Expand Down
3 changes: 2 additions & 1 deletion cmd/evm/internal/t8ntool/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"os"
"strings"

"gopkg.in/urfave/cli.v1"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core"
Expand All @@ -32,7 +34,6 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/tests"
"gopkg.in/urfave/cli.v1"
)

type result struct {
Expand Down
24 changes: 24 additions & 0 deletions common/debug/debug.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package debug

import (
"fmt"
"runtime"
)

Expand All @@ -26,3 +27,26 @@ func Callers(show int) []string {

return callers
}

func CodeLine() (string, string, int) {
pc, filename, line, _ := runtime.Caller(1)
return runtime.FuncForPC(pc).Name(), filename, line
}

func CodeLineStr() string {
pc, filename, line, _ := runtime.Caller(1)
return fmt.Sprintf("%s:%d - %s", filename, line, runtime.FuncForPC(pc).Name())
}

func Stack(all bool) []byte {
buf := make([]byte, 4096)

for {
n := runtime.Stack(buf, all)
if n < len(buf) {
return buf[:n]
}

buf = make([]byte, 2*len(buf))
}
}
29 changes: 25 additions & 4 deletions common/math/big.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package math
import (
"fmt"
"math/big"

"github.com/holiman/uint256"
)

// Various big integer limit values.
Expand Down Expand Up @@ -132,6 +134,7 @@ func MustParseBig256(s string) *big.Int {
// BigPow returns a ** b as a big integer.
func BigPow(a, b int64) *big.Int {
r := big.NewInt(a)

return r.Exp(r, big.NewInt(b), nil)
}

Expand All @@ -140,6 +143,15 @@ func BigMax(x, y *big.Int) *big.Int {
if x.Cmp(y) < 0 {
return y
}

return x
}

func BigMaxUint(x, y *uint256.Int) *uint256.Int {
if x.Lt(y) {
return y
}

return x
}

Expand All @@ -148,6 +160,15 @@ func BigMin(x, y *big.Int) *big.Int {
if x.Cmp(y) > 0 {
return y
}

return x
}

func BigMinUint256(x, y *uint256.Int) *uint256.Int {
if x.Gt(y) {
return y
}

return x
}

Expand Down Expand Up @@ -227,10 +248,10 @@ func U256Bytes(n *big.Int) []byte {
// S256 interprets x as a two's complement number.
// x must not exceed 256 bits (the result is undefined if it does) and is not modified.
//
// S256(0) = 0
// S256(1) = 1
// S256(2**255) = -2**255
// S256(2**256-1) = -1
// S256(0) = 0
// S256(1) = 1
// S256(2**255) = -2**255
// S256(2**256-1) = -1
func S256(x *big.Int) *big.Int {
if x.Cmp(tt255) < 0 {
return x
Expand Down
23 changes: 23 additions & 0 deletions common/math/uint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package math

import (
"math/big"

"github.com/holiman/uint256"
)

var (
U0 = uint256.NewInt(0)
U1 = uint256.NewInt(1)
U100 = uint256.NewInt(100)
)

func U256LTE(a, b *uint256.Int) bool {
return a.Lt(b) || a.Eq(b)
}

func FromBig(v *big.Int) *uint256.Int {
u, _ := uint256.FromBig(v)

return u
}
9 changes: 9 additions & 0 deletions common/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package common

import "time"

const TimeMilliseconds = "15:04:05.000"

func NowMilliseconds() string {
return time.Now().Format(TimeMilliseconds)
}
10 changes: 8 additions & 2 deletions common/tracing/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
Expand Down Expand Up @@ -51,11 +52,16 @@ func Trace(ctx context.Context, spanName string) (context.Context, trace.Span) {
return tr.Start(ctx, spanName)
}

func Exec(ctx context.Context, spanName string, opts ...Option) {
func Exec(ctx context.Context, instrumentationName, spanName string, opts ...Option) {
var span trace.Span

tr := FromContext(ctx)

if tr == nil && len(instrumentationName) != 0 {
tr = otel.GetTracerProvider().Tracer(instrumentationName)
ctx = WithTracer(ctx, tr)
}

if tr != nil {
ctx, span = tr.Start(ctx, spanName)
}
Expand Down Expand Up @@ -85,7 +91,7 @@ func ElapsedTime(ctx context.Context, span trace.Span, msg string, fn func(conte
fn(ctx, span)

if span != nil {
span.SetAttributes(attribute.Int(msg, int(time.Since(now).Milliseconds())))
span.SetAttributes(attribute.Int(msg, int(time.Since(now).Microseconds())))
}
}

Expand Down
8 changes: 4 additions & 4 deletions consensus/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ func (c *Bor) FinalizeAndAssemble(ctx context.Context, chain consensus.ChainHead
if IsSprintStart(headerNumber, c.config.CalculateSprint(headerNumber)) {
cx := statefull.ChainContext{Chain: chain, Bor: c}

tracing.Exec(finalizeCtx, "bor.checkAndCommitSpan", func(ctx context.Context, span trace.Span) {
tracing.Exec(finalizeCtx, "", "bor.checkAndCommitSpan", func(ctx context.Context, span trace.Span) {
// check and commit span
err = c.checkAndCommitSpan(finalizeCtx, state, header, cx)
})
Expand All @@ -877,7 +877,7 @@ func (c *Bor) FinalizeAndAssemble(ctx context.Context, chain consensus.ChainHead
}

if c.HeimdallClient != nil {
tracing.Exec(finalizeCtx, "bor.checkAndCommitSpan", func(ctx context.Context, span trace.Span) {
tracing.Exec(finalizeCtx, "", "bor.checkAndCommitSpan", func(ctx context.Context, span trace.Span) {
// commit states
stateSyncData, err = c.CommitStates(finalizeCtx, state, header, cx)
})
Expand All @@ -889,7 +889,7 @@ func (c *Bor) FinalizeAndAssemble(ctx context.Context, chain consensus.ChainHead
}
}

tracing.Exec(finalizeCtx, "bor.changeContractCodeIfNeeded", func(ctx context.Context, span trace.Span) {
tracing.Exec(finalizeCtx, "", "bor.changeContractCodeIfNeeded", func(ctx context.Context, span trace.Span) {
err = c.changeContractCodeIfNeeded(headerNumber, state)
})

Expand All @@ -899,7 +899,7 @@ func (c *Bor) FinalizeAndAssemble(ctx context.Context, chain consensus.ChainHead
}

// No block rewards in PoA, so the state remains as it is
tracing.Exec(finalizeCtx, "bor.IntermediateRoot", func(ctx context.Context, span trace.Span) {
tracing.Exec(finalizeCtx, "", "bor.IntermediateRoot", func(ctx context.Context, span trace.Span) {
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
})

Expand Down
53 changes: 53 additions & 0 deletions consensus/misc/eip1559.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"fmt"
"math/big"

"github.com/holiman/uint256"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -92,3 +94,54 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
)
}
}

// CalcBaseFee calculates the basefee of the header.
func CalcBaseFeeUint(config *params.ChainConfig, parent *types.Header) *uint256.Int {
var (
initialBaseFeeUint = uint256.NewInt(params.InitialBaseFee)
baseFeeChangeDenominatorUint64 = params.BaseFeeChangeDenominator(config.Bor, parent.Number)
baseFeeChangeDenominatorUint = uint256.NewInt(baseFeeChangeDenominatorUint64)
)

// If the current block is the first EIP-1559 block, return the InitialBaseFee.
if !config.IsLondon(parent.Number) {
return initialBaseFeeUint.Clone()
}

var (
parentGasTarget = parent.GasLimit / params.ElasticityMultiplier
parentGasTargetBig = uint256.NewInt(parentGasTarget)
)

// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
if parent.GasUsed == parentGasTarget {
return math.FromBig(parent.BaseFee)
}

if parent.GasUsed > parentGasTarget {
// If the parent block used more gas than its target, the baseFee should increase.
gasUsedDelta := uint256.NewInt(parent.GasUsed - parentGasTarget)

parentBaseFee := math.FromBig(parent.BaseFee)
x := gasUsedDelta.Mul(parentBaseFee, gasUsedDelta)
y := x.Div(x, parentGasTargetBig)
baseFeeDelta := math.BigMaxUint(
x.Div(y, baseFeeChangeDenominatorUint),
math.U1,
)

return x.Add(parentBaseFee, baseFeeDelta)
}

// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
gasUsedDelta := uint256.NewInt(parentGasTarget - parent.GasUsed)
parentBaseFee := math.FromBig(parent.BaseFee)
x := gasUsedDelta.Mul(parentBaseFee, gasUsedDelta)
y := x.Div(x, parentGasTargetBig)
baseFeeDelta := x.Div(y, baseFeeChangeDenominatorUint)

return math.BigMaxUint(
x.Sub(parentBaseFee, baseFeeDelta),
math.U0.Clone(),
)
}
17 changes: 13 additions & 4 deletions core/tx_journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ func (journal *txJournal) load(add func([]*types.Transaction) []error) error {
if _, err := os.Stat(journal.path); os.IsNotExist(err) {
return nil
}

// Open the journal for loading any past transactions
input, err := os.Open(journal.path)
if err != nil {
return err
}

defer input.Close()

// Temporarily discard any journal additions (don't double add on load)
Expand All @@ -80,29 +82,35 @@ func (journal *txJournal) load(add func([]*types.Transaction) []error) error {
// appropriate progress counters. Then use this method to load all the
// journaled transactions in small-ish batches.
loadBatch := func(txs types.Transactions) {
errs := add(txs)

dropped = len(errs)

for _, err := range add(txs) {
if err != nil {
log.Debug("Failed to add journaled transaction", "err", err)
dropped++
}
log.Debug("Failed to add journaled transaction", "err", err)
}
}
var (
failure error
batch types.Transactions
)

for {
// Parse the next transaction and terminate on error
tx := new(types.Transaction)

if err = stream.Decode(tx); err != nil {
if err != io.EOF {
failure = err
}

if batch.Len() > 0 {
loadBatch(batch)
}

break
}

// New transaction parsed, queue up for later, import if threshold is reached
total++

Expand All @@ -111,6 +119,7 @@ func (journal *txJournal) load(add func([]*types.Transaction) []error) error {
batch = batch[:0]
}
}

log.Info("Loaded local transaction journal", "transactions", total, "dropped", dropped)

return failure
Expand Down
Loading

0 comments on commit 741debf

Please sign in to comment.