foldtrace

package
v0.1.121 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2026 License: AGPL-3.0 Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FoldTraceCmd = &cobra.Command{
	Use:   "fold-trace",
	Short: "Trace an execution trace and fold it for visualization.",
	Long:  usage,
	Args:  cobra.ArbitraryArgs,
	PreRunE: func(cmd *cobra.Command, args []string) error {
		return validateInputMetric()
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		data, err := getInputData(cmd, args)
		if err != nil {
			return fmt.Errorf("unable to read input data: %w", err)
		}
		td := &TraceData{}
		err = json.Unmarshal(data, td)
		if err != nil {
			return fmt.Errorf("unable to unmarshal input json trace: %w", err)
		}

		folded := make(map[string]uint64)

		contexts := []string{inputRootContextName}
		currentDepth := uint64(1)
		lastLabel := ""

		metricType := inputMetric
		for idx, op := range td.StructLogs {
			if op.Depth > currentDepth {
				contexts = append(contexts, lastLabel)
			} else if op.Depth < currentDepth {
				contexts = contexts[:len(contexts)-1]
			}
			currentDepth = op.Depth

			currentLabel := op.OP

			if isCall(op.OP) {
				if len(op.Stack) < 6 {
					log.Warn().Int("stackLength", len(op.Stack)).Msg("Detected a call with a stack that's too short")
				} else {
					currentLabel = op.OP + " to " + op.Stack[len(op.Stack)-2]
				}
			}
			lastLabel = currentLabel

			var metricValue uint64
			switch metricType {
			case "gas":
				metricValue = op.GasCost
			case "actualgas":
				metricValue = getActualUsedGas(idx, td)
			default:
				metricValue = 1
			}
			currentMetricPath := strings.Join(contexts, ";") + ";" + currentLabel

			if !isCall(op.OP) {
				folded[currentMetricPath] += metricValue
			}
			if isCall(op.OP) && len(op.Stack[len(op.Stack)-2]) < 6 {
				folded[currentMetricPath] += metricValue
			}

			log.Trace().Strs("context", contexts).Uint64("depth", currentDepth).Str("currentLabel", currentLabel).Uint64("pc", op.PC).Msg("Trace operation")

		}
		for context, metric := range folded {
			fmt.Printf("%s %d\n", context, metric)
		}

		return nil
	},
}

Functions

This section is empty.

Types

type TraceData

type TraceData struct {
	StructLogs []TraceOperation `json:"structLogs"`
}

type TraceOperation

type TraceOperation struct {
	PC      uint64   `json:"pc"`
	OP      string   `json:"op"`
	Gas     uint64   `json:"gas"`
	GasCost uint64   `json:"gasCost"`
	Depth   uint64   `json:"depth"`
	Stack   []string `json:"stack"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL