Skip to content
Merged
Changes from all commits
Commits
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
40 changes: 40 additions & 0 deletions api/web3server_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"fmt"
"math/big"
"strconv"
"sync"
"time"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -102,7 +103,7 @@
return "0x" + fmt.Sprintf("%x", amount), nil
}

func (svr *web3Handler) getBlockWithTransactions(blk *block.Block, receipts []*action.Receipt, isDetailed bool) (*getBlockResult, error) {

Check failure on line 106 in api/web3server_utils.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=iotexproject_iotex-core&issues=AaCzvPF9J1TUpn6z_uu0&open=AaCzvPF9J1TUpn6z_uu0&pullRequest=4973
if blk == nil || receipts == nil {
return nil, errInvalidBlock
}
Expand Down Expand Up @@ -772,6 +773,41 @@
return cfg
}

// serializeStopAndResult returns t with its Stop and GetResult serialized
// against each other.
//
// The timeout watchdog armed by parseTracer calls Stop from its own goroutine
// while the goroutine running the trace calls GetResult. geth's StructLogger
// stores the interruption reason on a plain field -- Stop writes l.reason and
// only then flips the atomic l.interrupt, and GetResult reads l.reason without
// consulting that atomic -- so the two overlap without a happens-before edge.
// Holding a mutex across both calls supplies it. Hooks are passed through
// untouched: they run on the tracing goroutine and coordinate through
// l.interrupt, which is already atomic.
func serializeStopAndResult(t *tracers.Tracer) *tracers.Tracer {
var (
mu sync.Mutex
getResult = t.GetResult
stop = t.Stop
)
guarded := &tracers.Tracer{Hooks: t.Hooks}
if getResult != nil {
guarded.GetResult = func() (json.RawMessage, error) {
mu.Lock()
defer mu.Unlock()
return getResult()
}
}
if stop != nil {
guarded.Stop = func(err error) {
mu.Lock()
defer mu.Unlock()
stop(err)
}
}
return guarded
}

// parseTracer builds the tracer described by config and arms a timeout watchdog
// that stops the tracer once the deadline expires. The watchdog covers every
// tracer type (default struct logger, configured struct logger and named
Expand Down Expand Up @@ -812,6 +848,10 @@
}
}

// The watchdog below calls Stop concurrently with the caller's GetResult,
// so hand back a tracer that serializes the two.
tracer = serializeStopAndResult(tracer)

// Define a meaningful timeout for a single transaction trace. This applies
// to all tracer types.
timeout := defaultTraceTimeout
Expand Down
Loading