Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfixes and deps bump #776

Merged
merged 11 commits into from
Mar 14, 2023
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
2 changes: 1 addition & 1 deletion builder/files/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ syncmode = "full"
# vhosts = ["*"]
# corsdomain = ["*"]
# [jsonrpc.timeouts]
# read = "30s"
# read = "10s"
# write = "30s"
# idle = "2m0s"

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(),
)
}
8 changes: 4 additions & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@ func (bc *BlockChain) writeBlockAndSetHead(ctx context.Context, block *types.Blo

var stateSyncLogs []*types.Log

tracing.Exec(writeBlockAndSetHeadCtx, "blockchain.writeBlockWithState", func(_ context.Context, span trace.Span) {
tracing.Exec(writeBlockAndSetHeadCtx, "", "blockchain.writeBlockWithState", func(_ context.Context, span trace.Span) {
stateSyncLogs, err = bc.writeBlockWithState(block, receipts, logs, state)
tracing.SetAttributes(
span,
Expand All @@ -1392,7 +1392,7 @@ func (bc *BlockChain) writeBlockAndSetHead(ctx context.Context, block *types.Blo

var reorg bool

tracing.Exec(writeBlockAndSetHeadCtx, "blockchain.ReorgNeeded", func(_ context.Context, span trace.Span) {
tracing.Exec(writeBlockAndSetHeadCtx, "", "blockchain.ReorgNeeded", func(_ context.Context, span trace.Span) {
reorg, err = bc.forker.ReorgNeeded(currentBlock.Header(), block.Header())
tracing.SetAttributes(
span,
Expand All @@ -1406,7 +1406,7 @@ func (bc *BlockChain) writeBlockAndSetHead(ctx context.Context, block *types.Blo
return NonStatTy, err
}

tracing.Exec(writeBlockAndSetHeadCtx, "blockchain.reorg", func(_ context.Context, span trace.Span) {
tracing.Exec(writeBlockAndSetHeadCtx, "", "blockchain.reorg", func(_ context.Context, span trace.Span) {
if reorg {
// Reorganise the chain if the parent is not the head block
if block.ParentHash() != currentBlock.Hash() {
Expand Down Expand Up @@ -1435,7 +1435,7 @@ func (bc *BlockChain) writeBlockAndSetHead(ctx context.Context, block *types.Blo

// Set new head.
if status == CanonStatTy {
tracing.Exec(writeBlockAndSetHeadCtx, "blockchain.writeHeadBlock", func(_ context.Context, _ trace.Span) {
tracing.Exec(writeBlockAndSetHeadCtx, "", "blockchain.writeHeadBlock", func(_ context.Context, _ trace.Span) {
bc.writeHeadBlock(block)
})
}
Expand Down
Loading