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

Mining Analysis #429

Merged
merged 68 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
e9a9f11
base setup for miner opentel
temaniarpit27 Jun 13, 2022
f612c39
version change
temaniarpit27 Jun 13, 2022
6ca8301
modify ctx passing
temaniarpit27 Jun 13, 2022
767ee89
add attributes
temaniarpit27 Jun 13, 2022
bab7da1
update fill txs span attributes
manav2401 Jun 13, 2022
3a078e6
fix: use common attributes for remote and local txs
manav2401 Jun 13, 2022
c2602ca
pass context in seal
manav2401 Jun 13, 2022
1d9e1de
fix
manav2401 Jun 13, 2022
f252298
fix
manav2401 Jun 13, 2022
11bbda0
add traces to finalize and assemble
manav2401 Jun 14, 2022
4fb9770
fix
manav2401 Jun 14, 2022
75677c2
use task ctx in result loop
manav2401 Jun 14, 2022
59050f3
only start parent span if no error
manav2401 Jun 15, 2022
58d3540
send nil tracer from Finalize
manav2401 Jun 15, 2022
cb3a45e
Merge branch 'v0.3.0-dev' into arpit/pos-505
temaniarpit27 Jun 15, 2022
20a5a9d
clean up
manav2401 Jun 15, 2022
afef3b5
Merge branch 'arpit/pos-505' of https://github.com/maticnetwork/bor i…
manav2401 Jun 15, 2022
bcb11f2
add sub function timings in span attribute
manav2401 Jun 20, 2022
950bfe4
modify span attributes
manav2401 Jun 20, 2022
d848647
set time attribute to milliseconds
manav2401 Jun 21, 2022
cc1a18d
linters fix
temaniarpit27 Jun 22, 2022
6022e86
fix linters for consensus
manav2401 Jun 22, 2022
c9f3d25
add nolint to worker
manav2401 Jun 22, 2022
9d7ac22
fix testcase
temaniarpit27 Jun 22, 2022
99984dc
Added fillTransactions subTraces
0xsharma Jul 6, 2022
064c125
Merge branch 'develop' into arpit/pos-505
temaniarpit27 Jul 7, 2022
9588aa9
add traces in intermediate root hash function
manav2401 Jul 12, 2022
acd192a
add traces in WriteBlockAndSetHead function, fix linters
manav2401 Jul 12, 2022
5ec3450
merge develop
temaniarpit27 Jul 14, 2022
30b559b
merge arpit/pos-505
temaniarpit27 Jul 14, 2022
1552167
fix: linting errors
temaniarpit27 Jul 15, 2022
b5dfe09
fix: test cases
temaniarpit27 Jul 15, 2022
9506630
fix: go.mod
temaniarpit27 Jul 15, 2022
5d356da
Merge branch 'develop' of github.com:maticnetwork/bor into arpit/pos-505
temaniarpit27 Jul 15, 2022
6f78f62
fix: testcase
temaniarpit27 Jul 15, 2022
b29e300
extract tracing package
JekaMas Jul 19, 2022
e36bc2b
merge
JekaMas Jul 19, 2022
aa8b1cc
Merge branch 'develop' into arpit/pos-505
manav2401 Aug 5, 2022
799fce1
Merge branch 'develop' into arpit/pos-505
manav2401 Aug 8, 2022
5d1189b
Merge branch 'develop' into arpit/pos-505
manav2401 Aug 9, 2022
10109b4
merge develop
temaniarpit27 Aug 16, 2022
82a21ac
merge base branch
temaniarpit27 Aug 16, 2022
478a7e0
linters fix
temaniarpit27 Aug 16, 2022
2d68b7c
debug
JekaMas Aug 17, 2022
441a765
Revert "debug"
temaniarpit27 Aug 17, 2022
ae94046
fix: panic in NewTransactionsByPriceAndNonce iteration
manav2401 Aug 22, 2022
e1edcdf
Merge branch 'develop' of github.com:maticnetwork/bor into arpit/pos-505
temaniarpit27 Aug 24, 2022
9b995bc
Merge branch 'develop' into arpit/pos-505
manav2401 Aug 29, 2022
2ad0d64
merge develop
temaniarpit27 Sep 8, 2022
b9dc665
Merge branch 'arpit/pos-505' of github.com:maticnetwork/bor into arpi…
temaniarpit27 Sep 8, 2022
97e2840
merge develop
manav2401 Sep 23, 2022
aa532ee
change heimdall version to develop
temaniarpit27 Sep 27, 2022
9bdbc1e
miner/worker: fix duplicate call to tx ordering
manav2401 Sep 27, 2022
b8bc0b6
Merge branch 'arpit/pos-505' of https://github.com/maticnetwork/bor i…
manav2401 Sep 27, 2022
b726e53
Merge branch 'develop' of https://github.com/maticnetwork/bor into ar…
manav2401 Sep 27, 2022
9b4f796
miner/worker: refactor tracing
manav2401 Sep 27, 2022
b626090
consensus/bor: use tracing package
manav2401 Sep 27, 2022
4324423
tracing: add more abstraction for spans
manav2401 Sep 27, 2022
a810072
tracing: set all attributes at once
manav2401 Sep 28, 2022
0ab5821
Merge branch 'develop' into arpit/pos-505
temaniarpit27 Sep 28, 2022
54fef9c
remove nested tracing from blockchain.WriteBlockAndSetHead function
manav2401 Sep 29, 2022
c99de20
remove nested tracing from statedb.IntermediateRoot function
manav2401 Sep 29, 2022
b6ff133
handle end span in bor.Seal function
manav2401 Sep 29, 2022
91388bf
fix: typo
manav2401 Sep 29, 2022
f4a8a86
minor fixes
manav2401 Sep 29, 2022
c07ed79
fix: linters
manav2401 Sep 29, 2022
6e6268b
fix: remove nolint
manav2401 Sep 29, 2022
1d32559
go mod tidy
manav2401 Sep 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/evm/internal/t8ntool/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package t8ntool

import (
"context"
"crypto/ecdsa"
"encoding/json"
"errors"
Expand Down Expand Up @@ -188,7 +189,7 @@ func (i *bbInput) sealEthash(block *types.Block) (*types.Block, error) {
// If the testmode is used, the sealer will return quickly, and complain
// "Sealing result is not read by miner" if it cannot write the result.
results := make(chan *types.Block, 1)
if err := engine.Seal(nil, block, results, nil); err != nil {
if err := engine.Seal(context.Background(), nil, block, results, nil); err != nil {
panic(fmt.Sprintf("failed to seal block: %v", err))
}
found := <-results
Expand Down
9 changes: 5 additions & 4 deletions consensus/beacon/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package beacon

import (
"context"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -278,11 +279,11 @@ func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.

// FinalizeAndAssemble implements consensus.Engine, setting the final state and
// assembling the block.
func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
func (beacon *Beacon) FinalizeAndAssemble(ctx context.Context, chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
// FinalizeAndAssemble is different with Prepare, it can be used in both block
// generation and verification. So determine the consensus rules by header type.
if !beacon.IsPoSHeader(header) {
return beacon.ethone.FinalizeAndAssemble(chain, header, state, txs, uncles, receipts)
return beacon.ethone.FinalizeAndAssemble(ctx, chain, header, state, txs, uncles, receipts)
}
// Finalize and assemble the block
beacon.Finalize(chain, header, state, txs, uncles)
Expand All @@ -294,9 +295,9 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
//
// Note, the method returns immediately and will send the result async. More
// than one result may also be returned depending on the consensus algorithm.
func (beacon *Beacon) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
func (beacon *Beacon) Seal(ctx context.Context, chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
if !beacon.IsPoSHeader(block.Header()) {
return beacon.ethone.Seal(chain, block, results, stop)
return beacon.ethone.Seal(ctx, chain, block, results, stop)
}
// The seal verification is done by the external consensus engine,
// return directly without pushing any block back. In another word
Expand Down
104 changes: 94 additions & 10 deletions consensus/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bor

import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"errors"
Expand All @@ -14,6 +15,9 @@ import (
"time"

lru "github.com/hashicorp/golang-lru"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"golang.org/x/crypto/sha3"

"github.com/ethereum/go-ethereum/accounts"
Expand Down Expand Up @@ -716,14 +720,14 @@ func (c *Bor) Finalize(chain consensus.ChainHeaderReader, header *types.Header,
if headerNumber%c.config.Sprint == 0 {
cx := statefull.ChainContext{Chain: chain, Bor: c}
// check and commit span
if err := c.checkAndCommitSpan(state, header, cx); err != nil {
if err := c.checkAndCommitSpan(context.Background(), nil, state, header, cx); err != nil {
log.Error("Error while committing span", "error", err)
return
}

if c.HeimdallClient != nil {
// commit statees
stateSyncData, err = c.CommitStates(state, header, cx)
stateSyncData, err = c.CommitStates(context.Background(), nil, state, header, cx)
if err != nil {
log.Error("Error while committing states", "error", err)
return
Expand Down Expand Up @@ -780,7 +784,18 @@ func (c *Bor) changeContractCodeIfNeeded(headerNumber uint64, state *state.State

// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
// nor block rewards given, and returns the final block.
func (c *Bor) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
func (c *Bor) FinalizeAndAssemble(ctx context.Context, chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {

tracer := otel.GetTracerProvider().Tracer("MinerWorker")
finalizeCtx, finalizeSpan := tracer.Start(ctx, "FinalizeAndAssemble")
defer finalizeSpan.End()

finalizeSpan.SetAttributes(
attribute.Int("number", int(header.Number.Int64())),
attribute.String("hash", header.Hash().String()),
attribute.Int("number of txs", len(txs)),
manav2401 marked this conversation as resolved.
Show resolved Hide resolved
)

stateSyncData := []*types.StateSyncData{}

headerNumber := header.Number.Uint64()
Expand All @@ -789,15 +804,15 @@ func (c *Bor) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *typ
cx := statefull.ChainContext{Chain: chain, Bor: c}

// check and commit span
err := c.checkAndCommitSpan(state, header, cx)
err := c.checkAndCommitSpan(finalizeCtx, tracer, state, header, cx)
if err != nil {
log.Error("Error while committing span", "error", err)
return nil, err
}

if c.HeimdallClient != nil {
// commit states
stateSyncData, err = c.CommitStates(state, header, cx)
stateSyncData, err = c.CommitStates(finalizeCtx, tracer, state, header, cx)
if err != nil {
log.Error("Error while committing states", "error", err)
return nil, err
Expand Down Expand Up @@ -837,7 +852,11 @@ func (c *Bor) Authorize(signer common.Address, signFn SignerFn) {

// Seal implements consensus.Engine, attempting to create a sealed block using
// the local signing credentials.
func (c *Bor) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
func (c *Bor) Seal(ctx context.Context, chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {

tracer := otel.GetTracerProvider().Tracer("MinerWorker")
_, sealSpan := tracer.Start(ctx, "Seal")

header := block.Header()
// Sealing the genesis block is not supported
number := header.Number.Uint64()
Expand Down Expand Up @@ -884,7 +903,7 @@ func (c *Bor) Seal(chain consensus.ChainHeaderReader, block *types.Block, result
// Wait until sealing is terminated or delay timeout.
log.Info("Waiting for slot to sign and propagate", "number", number, "hash", header.Hash, "delay-in-sec", uint(delay), "delay", common.PrettyDuration(delay))

go func() {
go func(sealSpan trace.Span) {
select {
case <-stop:
log.Debug("Discarding sealing operation for block", "number", number)
Expand All @@ -907,13 +926,20 @@ func (c *Bor) Seal(chain consensus.ChainHeaderReader, block *types.Block, result
"delay", delay,
"headerDifficulty", header.Difficulty,
)
sealSpan.SetAttributes(
attribute.Int("number", int(number)),
attribute.String("hash", header.Hash().String()),
attribute.String("delay", delay.String()),
attribute.Bool("out-of-turn", wiggle > 0),
)
sealSpan.End()
}
select {
case results <- block.WithSeal(header):
default:
log.Warn("Sealing result was not read by miner", "number", number, "sealhash", SealHash(header, c.config))
}
}()
}(sealSpan)

return nil
}
Expand Down Expand Up @@ -967,19 +993,37 @@ func (c *Bor) Close() error {
}

func (c *Bor) checkAndCommitSpan(
ctx context.Context,
tracer trace.Tracer,
state *state.StateDB,
header *types.Header,
chain core.ChainContext,
) error {

checkAndCommitSpanCtx := context.Background()
var checkAndCommitSpan trace.Span = nil
manav2401 marked this conversation as resolved.
Show resolved Hide resolved
if tracer != nil {
checkAndCommitSpanCtx, checkAndCommitSpan = tracer.Start(ctx, "checkAndCommitSpan")
defer checkAndCommitSpan.End()
}

headerNumber := header.Number.Uint64()

span, err := c.spanner.GetCurrentSpan(header.ParentHash)
if err != nil {
return err
}

if checkAndCommitSpan != nil {
checkAndCommitSpan.SetAttributes(
attribute.Int("number", int(headerNumber)),
attribute.String("hash", header.Hash().String()),
attribute.Int("current span id", int(span.ID)),
)
}

if c.needToCommitSpan(span, headerNumber) {
return c.FetchAndCommitSpan(span.ID+1, state, header, chain)
return c.FetchAndCommitSpan(checkAndCommitSpanCtx, tracer, span.ID+1, state, header, chain)
}

return nil
Expand All @@ -1005,11 +1049,20 @@ func (c *Bor) needToCommitSpan(span *span.Span, headerNumber uint64) bool {
}

func (c *Bor) FetchAndCommitSpan(
ctx context.Context,
tracer trace.Tracer,
newSpanID uint64,
state *state.StateDB,
header *types.Header,
chain core.ChainContext,
) error {

var fetchAndCommitSpan trace.Span = nil
if tracer != nil {
_, fetchAndCommitSpan = tracer.Start(ctx, "FetchAndCommitSpan")
defer fetchAndCommitSpan.End()
}

var heimdallSpan span.HeimdallSpan

if c.HeimdallClient == nil {
Expand Down Expand Up @@ -1038,15 +1091,33 @@ func (c *Bor) FetchAndCommitSpan(
)
}

if fetchAndCommitSpan != nil {
fetchAndCommitSpan.SetAttributes(
attribute.Int("number", int(header.Number.Int64())),
attribute.String("hash", header.Hash().String()),
attribute.Int("fetched span id", int(heimdallSpan.ID)),
)
}

return c.spanner.CommitSpan(heimdallSpan, state, header, chain)
}

// CommitStates commit states
func (c *Bor) CommitStates(
ctx context.Context,
tracer trace.Tracer,
state *state.StateDB,
header *types.Header,
chain statefull.ChainContext,
) ([]*types.StateSyncData, error) {

var commitStatesSpan trace.Span = nil
if tracer != nil {
_, commitStatesSpan = tracer.Start(ctx, "CommitStates")
defer commitStatesSpan.End()
}

fetchStart := time.Now()
stateSyncs := make([]*types.StateSyncData, 0)
number := header.Number.Uint64()

Expand Down Expand Up @@ -1074,8 +1145,9 @@ func (c *Bor) CommitStates(
}
}

fetchTime := time.Since(fetchStart)
processStart := time.Now()
totalGas := 0 /// limit on gas for state sync per block

chainID := c.chainConfig.ChainID.String()

for _, eventRecord := range eventRecords {
Expand Down Expand Up @@ -1107,6 +1179,18 @@ func (c *Bor) CommitStates(
lastStateID++
}

processTime := time.Since(processStart)

if commitStatesSpan != nil {
commitStatesSpan.SetAttributes(
attribute.Int("number", int(number)),
attribute.String("hash", header.Hash().String()),
attribute.String("fetch time", fetchTime.String()),
attribute.String("process time", processTime.String()),
attribute.Int("state sync count", len(stateSyncs)),
attribute.Int("total gas", totalGas),
)
}
log.Info("StateSyncData", "Gas", totalGas, "Block-number", number, "LastStateID", lastStateID, "TotalRecords", len(eventRecords))

return stateSyncs, nil
Expand Down
5 changes: 3 additions & 2 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package clique

import (
"bytes"
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -569,7 +570,7 @@ func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Heade

// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
// nor block rewards given, and returns the final block.
func (c *Clique) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
func (c *Clique) FinalizeAndAssemble(ctx context.Context, chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
// Finalize block
c.Finalize(chain, header, state, txs, uncles)

Expand All @@ -589,7 +590,7 @@ func (c *Clique) Authorize(signer common.Address, signFn SignerFn) {

// Seal implements consensus.Engine, attempting to create a sealed block using
// the local signing credentials.
func (c *Clique) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
func (c *Clique) Seal(ctx context.Context, chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
header := block.Header()

// Sealing the genesis block is not supported
Expand Down
5 changes: 3 additions & 2 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package consensus

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -97,15 +98,15 @@ type Engine interface {
//
// Note: The block header and state database might be updated to reflect any
// consensus rules that happen at finalization (e.g. block rewards).
FinalizeAndAssemble(chain ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
FinalizeAndAssemble(ctx context.Context, chain ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error)

// Seal generates a new sealing request for the given input block and pushes
// the result into the given channel.
//
// Note, the method returns immediately and will send the result async. More
// than one result may also be returned depending on the consensus algorithm.
Seal(chain ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error
Seal(ctx context.Context, chain ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error

// SealHash returns the hash of a block prior to it being sealed.
SealHash(header *types.Header) common.Hash
Expand Down
3 changes: 2 additions & 1 deletion consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package ethash

import (
"bytes"
"context"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -598,7 +599,7 @@ func (ethash *Ethash) Finalize(chain consensus.ChainHeaderReader, header *types.

// FinalizeAndAssemble implements consensus.Engine, accumulating the block and
// uncle rewards, setting the final state and assembling the block.
func (ethash *Ethash) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
func (ethash *Ethash) FinalizeAndAssemble(ctx context.Context, chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
// Finalize block
ethash.Finalize(chain, header, state, txs, uncles)

Expand Down
7 changes: 4 additions & 3 deletions consensus/ethash/ethash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package ethash

import (
"context"
"io/ioutil"
"math/big"
"math/rand"
Expand All @@ -38,7 +39,7 @@ func TestTestMode(t *testing.T) {
defer ethash.Close()

results := make(chan *types.Block)
err := ethash.Seal(nil, types.NewBlockWithHeader(header), results, nil)
err := ethash.Seal(context.Background(), nil, types.NewBlockWithHeader(header), results, nil)
if err != nil {
t.Fatalf("failed to seal block: %v", err)
}
Expand Down Expand Up @@ -111,7 +112,7 @@ func TestRemoteSealer(t *testing.T) {

// Push new work.
results := make(chan *types.Block)
ethash.Seal(nil, block, results, nil)
ethash.Seal(context.Background(), nil, block, results, nil)

var (
work [4]string
Expand All @@ -128,7 +129,7 @@ func TestRemoteSealer(t *testing.T) {
header = &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(1000)}
block = types.NewBlockWithHeader(header)
sealhash = ethash.SealHash(header)
ethash.Seal(nil, block, results, nil)
ethash.Seal(context.Background(), nil, block, results, nil)

if work, err = api.GetWork(); err != nil || work[0] != sealhash.Hex() {
t.Error("expect to return the latest pushed work")
Expand Down
Loading