Skip to content

Commit

Permalink
core/vm: fix benchmark overflow + prep for precompile repricings (eth…
Browse files Browse the repository at this point in the history
…ereum#21530)

* core/vm/testdata: add gascost expectations to testcases

* core/vm: verify expected gas in tests for precompiles

* core/vm: fix overflow flaw in gas/s calculation
  • Loading branch information
holiman authored and enriquefynn committed Feb 15, 2021
1 parent 4b434d8 commit 65d9977
Show file tree
Hide file tree
Showing 16 changed files with 1,007 additions and 4 deletions.
12 changes: 9 additions & 3 deletions core/vm/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
// precompiledTest defines the input/output pairs for precompiled contract tests.
type precompiledTest struct {
Input, Expected string
Gas uint64
Name string
NoBenchmark bool // Benchmark primarily the worst-cases
}
Expand Down Expand Up @@ -78,6 +79,9 @@ func testPrecompiled(addr string, test precompiledTest, t *testing.T) {
} else if common.Bytes2Hex(res) != test.Expected {
t.Errorf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res))
}
if expGas := test.Gas; expGas != gas {
t.Errorf("%v: gas wrong, expected %d, got %d", test.Name, expGas, gas)
}
// Verify that the precompile did not touch the input buffer
exp := common.Hex2Bytes(test.Input)
if !bytes.Equal(in, exp) {
Expand Down Expand Up @@ -137,20 +141,22 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) {

bench.Run(fmt.Sprintf("%s-Gas=%d", test.Name, reqGas), func(bench *testing.B) {
bench.ReportAllocs()
start := time.Now().Nanosecond()
start := time.Now()
bench.ResetTimer()
for i := 0; i < bench.N; i++ {
copy(data, in)
res, _, err = RunPrecompiledContract(p, data, reqGas)
}
bench.StopTimer()
elapsed := float64(time.Now().Nanosecond() - start)
elapsed := uint64(time.Since(start))
if elapsed < 1 {
elapsed = 1
}
gasUsed := reqGas * uint64(bench.N)
bench.ReportMetric(float64(reqGas), "gas/op")
bench.ReportMetric(float64(gasUsed*1000)/elapsed, "mgas/s")
// Keep it as uint64, multiply 100 to get two digit float later
mgasps := (100 * 1000 * gasUsed) / elapsed
bench.ReportMetric(float64(mgasps)/100, "mgas/s")
//Check if it is correct
if err != nil {
bench.Error(err)
Expand Down
5 changes: 5 additions & 0 deletions core/vm/testdata/precompiles/blake2F.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,35 @@
"Input": "0000000048c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001",
"Expected": "08c9bcf367e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d282e6ad7f520e511f6c3e2b8c68059b9442be0454267ce079217e1319cde05b",
"Name": "vector 4",
"Gas": 0,
"NoBenchmark": false
},
{
"Input": "0000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001",
"Expected": "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923",
"Name": "vector 5",
"Gas": 12,
"NoBenchmark": false
},
{
"Input": "0000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000",
"Expected": "75ab69d3190a562c51aef8d88f1c2775876944407270c42c9844252c26d2875298743e7f6d5ea2f2d3e8d226039cd31b4e426ac4f2d3d666a610c2116fde4735",
"Name": "vector 6",
"Gas": 12,
"NoBenchmark": false
},
{
"Input": "0000000148c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001",
"Expected": "b63a380cb2897d521994a85234ee2c181b5f844d2c624c002677e9703449d2fba551b3a8333bcdf5f2f7e08993d53923de3d64fcc68c034e717b9293fed7a421",
"Name": "vector 7",
"Gas": 1,
"NoBenchmark": false
},
{
"Input": "007A120048c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001",
"Expected": "6d2ce9e534d50e18ff866ae92d70cceba79bbcd14c63819fe48752c8aca87a4bb7dcc230d22a4047f0486cfcfb50a17b24b2899eb8fca370f22240adb5170189",
"Name": "vector 8",
"Gas": 8000000,
"NoBenchmark": false
}
]
Loading

0 comments on commit 65d9977

Please sign in to comment.