prefer to panic instead of returning an error

This commit is contained in:
Jared Wasinger 2024-11-21 20:49:26 +07:00
parent 39a39ce0da
commit 5ef597efc7

View file

@ -82,33 +82,24 @@ type execStats struct {
GasUsed uint64 `json:"gasUsed"` // the amount of gas used during execution GasUsed uint64 `json:"gasUsed"` // the amount of gas used during execution
} }
func timedExec(bench bool, execFunc func() ([]byte, uint64, error)) (output []byte, stats execStats, execErr error, benchErr error) { func timedExec(bench bool, execFunc func() ([]byte, uint64, error)) ([]byte, execStats, error) {
if bench { if bench {
// Do one warm-up run // Do one warm-up run
var gasUsed uint64 output, gasUsed, err := execFunc()
output, gasUsed, execErr = execFunc()
var benchErr error
result := testing.Benchmark(func(b *testing.B) { result := testing.Benchmark(func(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
haveOutput, haveGasUsed, haveErr := execFunc() haveOutput, haveGasUsed, haveErr := execFunc()
if !bytes.Equal(haveOutput, output) { if !bytes.Equal(haveOutput, output) {
benchErr = fmt.Errorf("output differs, have\n%x\nwant %x\n", haveOutput, output) panic(fmt.Sprintf("output differs, have\n%x\nwant %x\n", haveOutput, output))
b.FailNow()
} }
if haveGasUsed != gasUsed { if haveGasUsed != gasUsed {
benchErr = fmt.Errorf("gas differs, have %v want%v", haveGasUsed, gasUsed) panic(fmt.Sprintf("gas differs, have %v want%v", haveGasUsed, gasUsed))
b.FailNow()
} }
if haveErr != execErr { if haveErr != execErr {
benchErr = fmt.Errorf("err differs, have %v want %v", haveErr, execErr) panic(fmt.Sprintf("err differs, have %v want %v", haveErr, execErr))
b.FailNow()
} }
} }
}) })
if benchErr != nil {
return nil, execStats{}, nil, benchErr
}
// Get the average execution time from the benchmarking result. // Get the average execution time from the benchmarking result.
// There are other useful stats here that could be reported. // There are other useful stats here that could be reported.
stats := execStats{ stats := execStats{
@ -117,7 +108,7 @@ func timedExec(bench bool, execFunc func() ([]byte, uint64, error)) (output []by
BytesAllocated: result.AllocedBytesPerOp(), BytesAllocated: result.AllocedBytesPerOp(),
GasUsed: gasUsed, GasUsed: gasUsed,
} }
return output, stats, execErr, nil return output, stats, err
} }
var memStatsBefore, memStatsAfter goruntime.MemStats var memStatsBefore, memStatsAfter goruntime.MemStats
goruntime.ReadMemStats(&memStatsBefore) goruntime.ReadMemStats(&memStatsBefore)
@ -125,13 +116,13 @@ func timedExec(bench bool, execFunc func() ([]byte, uint64, error)) (output []by
output, gasUsed, err := execFunc() output, gasUsed, err := execFunc()
duration := time.Since(t0) duration := time.Since(t0)
goruntime.ReadMemStats(&memStatsAfter) goruntime.ReadMemStats(&memStatsAfter)
stats = execStats{ stats := execStats{
Time: duration, Time: duration,
Allocs: int64(memStatsAfter.Mallocs - memStatsBefore.Mallocs), Allocs: int64(memStatsAfter.Mallocs - memStatsBefore.Mallocs),
BytesAllocated: int64(memStatsAfter.TotalAlloc - memStatsBefore.TotalAlloc), BytesAllocated: int64(memStatsAfter.TotalAlloc - memStatsBefore.TotalAlloc),
GasUsed: gasUsed, GasUsed: gasUsed,
} }
return output, stats, err, nil return output, stats, err
} }
func runCmd(ctx *cli.Context) error { func runCmd(ctx *cli.Context) error {
@ -301,10 +292,7 @@ func runCmd(ctx *cli.Context) error {
} }
bench := ctx.Bool(BenchFlag.Name) bench := ctx.Bool(BenchFlag.Name)
output, stats, execErr, err := timedExec(bench, execFunc) output, stats, execErr := timedExec(bench, execFunc)
if err != nil {
return fmt.Errorf("benchmarking failed: %v\n", err)
}
if ctx.Bool(DumpFlag.Name) { if ctx.Bool(DumpFlag.Name) {
root, err := runtimeConfig.State.Commit(genesisConfig.Number, true) root, err := runtimeConfig.State.Commit(genesisConfig.Number, true)