Thread Context for HTR Methods (#8770)

Co-authored-by: Radosław Kapka <rkapka@wp.pl>
This commit is contained in:
Nishant Das
2021-04-15 20:17:55 +08:00
committed by GitHub
parent d77c298ec6
commit 405e2a1a03
2 changed files with 17 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
package stateV0
import (
"context"
"encoding/binary"
"sync"
@@ -13,6 +14,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/htrutils"
"github.com/prysmaticlabs/prysm/shared/params"
"go.opencensus.io/trace"
)
var (
@@ -47,14 +49,17 @@ type stateRootHasher struct {
// computeFieldRoots returns the hash tree root computations of every field in
// the beacon state as a list of 32 byte roots.
func computeFieldRoots(state *pb.BeaconState) ([][]byte, error) {
func computeFieldRoots(ctx context.Context, state *pb.BeaconState) ([][]byte, error) {
if featureconfig.Get().EnableSSZCache {
return cachedHasher.computeFieldRootsWithHasher(state)
return cachedHasher.computeFieldRootsWithHasher(ctx, state)
}
return nocachedHasher.computeFieldRootsWithHasher(state)
return nocachedHasher.computeFieldRootsWithHasher(ctx, state)
}
func (h *stateRootHasher) computeFieldRootsWithHasher(state *pb.BeaconState) ([][]byte, error) {
func (h *stateRootHasher) computeFieldRootsWithHasher(ctx context.Context, state *pb.BeaconState) ([][]byte, error) {
ctx, span := trace.StartSpan(ctx, "beaconState.computeFieldRootsWithHasher")
defer span.End()
if state == nil {
return nil, errors.New("nil state")
}

View File

@@ -197,14 +197,14 @@ func (b *BeaconState) Copy() iface.BeaconState {
// HashTreeRoot of the beacon state retrieves the Merkle root of the trie
// representation of the beacon state based on the eth2 Simple Serialize specification.
func (b *BeaconState) HashTreeRoot(ctx context.Context) ([32]byte, error) {
_, span := trace.StartSpan(ctx, "beaconState.HashTreeRoot")
ctx, span := trace.StartSpan(ctx, "beaconState.HashTreeRoot")
defer span.End()
b.lock.Lock()
defer b.lock.Unlock()
if b.merkleLayers == nil || len(b.merkleLayers) == 0 {
fieldRoots, err := computeFieldRoots(b.state)
fieldRoots, err := computeFieldRoots(ctx, b.state)
if err != nil {
return [32]byte{}, err
}
@@ -214,7 +214,7 @@ func (b *BeaconState) HashTreeRoot(ctx context.Context) ([32]byte, error) {
}
for field := range b.dirtyFields {
root, err := b.rootSelector(field)
root, err := b.rootSelector(ctx, field)
if err != nil {
return [32]byte{}, err
}
@@ -379,7 +379,11 @@ func (b *BeaconState) FieldReferencesCount() map[string]uint64 {
return refMap
}
func (b *BeaconState) rootSelector(field fieldIndex) ([32]byte, error) {
func (b *BeaconState) rootSelector(ctx context.Context, field fieldIndex) ([32]byte, error) {
ctx, span := trace.StartSpan(ctx, "beaconState.rootSelector")
defer span.End()
span.AddAttributes(trace.StringAttribute("field", field.String()))
hasher := hashutil.CustomSHA256Hasher()
switch field {
case genesisTime: