Skip to content

Commit 9a1c548

Browse files
authored
add benchmark to template (#705)
* add benchmark to template * fix comment
1 parent 3a6c74a commit 9a1c548

File tree

1 file changed

+117
-95
lines changed

1 file changed

+117
-95
lines changed

accounts/abi/bind/precompilebind/precompile_contract_test_template.go

Lines changed: 117 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -23,109 +23,112 @@ import (
2323
"github.com/stretchr/testify/require"
2424
)
2525
26-
// TestRun tests the Run function of the precompile contract.
27-
// These tests are run against the precompile contract directly with
28-
// the given input and expected output. They're just a guide to
29-
// help you write your own tests. These tests are for general cases like
30-
// allowlist, readOnly behaviour, and gas cost. You should write your own
31-
// tests for specific cases.
32-
func TestRun(t *testing.T) {
33-
tests := map[string]testutils.PrecompileTest{
34-
{{- $contract := .Contract}}
35-
{{- $structs := .Structs}}
36-
{{- range .Contract.Funcs}}
37-
{{- $func := .}}
38-
{{- if $contract.AllowList}}
39-
{{- $roles := mkList "NoRole" "Enabled" "Admin"}}
40-
{{- range $role := $roles}}
41-
{{- $fail := and (not $func.Original.IsConstant) (eq $role "NoRole")}}
42-
"calling {{decapitalise $func.Normalized.Name}} from {{$role}} should {{- if $fail}} fail {{- else}} succeed{{- end}}": {
43-
Caller: allowlist.Test{{$role}}Addr,
44-
BeforeHook: allowlist.SetDefaultRoles(Module.Address),
45-
InputFn: func(t testing.TB) []byte {
46-
{{- if len $func.Normalized.Inputs | lt 1}}
26+
// These tests are run against the precompile contract directly with
27+
// the given input and expected output. They're just a guide to
28+
// help you write your own tests. These tests are for general cases like
29+
// allowlist, readOnly behaviour, and gas cost. You should write your own
30+
// tests for specific cases.
31+
var(
32+
tests = map[string]testutils.PrecompileTest{
33+
{{- $contract := .Contract}}
34+
{{- $structs := .Structs}}
35+
{{- range .Contract.Funcs}}
36+
{{- $func := .}}
37+
{{- if $contract.AllowList}}
38+
{{- $roles := mkList "NoRole" "Enabled" "Admin"}}
39+
{{- range $role := $roles}}
40+
{{- $fail := and (not $func.Original.IsConstant) (eq $role "NoRole")}}
41+
"calling {{decapitalise $func.Normalized.Name}} from {{$role}} should {{- if $fail}} fail {{- else}} succeed{{- end}}": {
42+
Caller: allowlist.Test{{$role}}Addr,
43+
BeforeHook: allowlist.SetDefaultRoles(Module.Address),
44+
InputFn: func(t testing.TB) []byte {
45+
{{- if len $func.Normalized.Inputs | lt 1}}
46+
// CUSTOM CODE STARTS HERE
47+
// populate test input here
48+
testInput := {{capitalise $func.Normalized.Name}}Input{}
49+
input, err := Pack{{$func.Normalized.Name}}(testInput)
50+
{{- else if len $func.Normalized.Inputs | eq 1 }}
51+
{{- $input := index $func.Normalized.Inputs 0}}
52+
// CUSTOM CODE STARTS HERE
53+
// set test input to a value here
54+
var testInput {{bindtype $input.Type $structs}}
55+
input, err := Pack{{$func.Normalized.Name}}(testInput)
56+
{{- else}}
57+
input, err := Pack{{$func.Normalized.Name}}()
58+
{{- end}}
59+
require.NoError(t, err)
60+
return input
61+
},
62+
{{- if not $fail}}
63+
// This test is for a successful call. You can set the expected output here.
4764
// CUSTOM CODE STARTS HERE
48-
// populate test input here
49-
testInput := {{capitalise $func.Normalized.Name}}Input{}
50-
input, err := Pack{{$func.Normalized.Name}}(testInput)
51-
{{- else if len $func.Normalized.Inputs | eq 1 }}
52-
{{- $input := index $func.Normalized.Inputs 0}}
53-
// CUSTOM CODE STARTS HERE
54-
// set test input to a value here
55-
var testInput {{bindtype $input.Type $structs}}
56-
input, err := Pack{{$func.Normalized.Name}}(testInput)
57-
{{- else}}
58-
input, err := Pack{{$func.Normalized.Name}}()
65+
ExpectedRes: []byte{},
5966
{{- end}}
60-
require.NoError(t, err)
61-
return input
67+
SuppliedGas: {{$func.Normalized.Name}}GasCost,
68+
ReadOnly: false,
69+
ExpectedErr: {{if $fail}} ErrCannot{{$func.Normalized.Name}}.Error() {{- else}} "" {{- end}},
6270
},
63-
{{- if not $fail}}
64-
// This test is for a successful call. You can set the expected output here.
65-
// CUSTOM CODE STARTS HERE
66-
ExpectedRes: []byte{},
6771
{{- end}}
68-
SuppliedGas: {{$func.Normalized.Name}}GasCost,
69-
ReadOnly: false,
70-
ExpectedErr: {{if $fail}} ErrCannot{{$func.Normalized.Name}}.Error() {{- else}} "" {{- end}},
71-
},
72-
{{- end}}
73-
{{- end}}
74-
{{- if not $func.Original.IsConstant}}
75-
"readOnly {{decapitalise $func.Normalized.Name}} should fail": {
76-
Caller: common.Address{1},
77-
InputFn: func(t testing.TB) []byte {
78-
{{- if len $func.Normalized.Inputs | lt 1}}
79-
// CUSTOM CODE STARTS HERE
80-
// populate test input here
81-
testInput := {{capitalise $func.Normalized.Name}}Input{}
82-
input, err := Pack{{$func.Normalized.Name}}(testInput)
83-
{{- else if len $func.Normalized.Inputs | eq 1 }}
84-
{{- $input := index $func.Normalized.Inputs 0}}
85-
// CUSTOM CODE STARTS HERE
86-
// set test input to a value here
87-
var testInput {{bindtype $input.Type $structs}}
88-
input, err := Pack{{$func.Normalized.Name}}(testInput)
89-
{{- else}}
90-
input, err := Pack{{$func.Normalized.Name}}()
91-
{{- end}}
92-
require.NoError(t, err)
93-
return input
72+
{{- end}}
73+
{{- if not $func.Original.IsConstant}}
74+
"readOnly {{decapitalise $func.Normalized.Name}} should fail": {
75+
Caller: common.Address{1},
76+
InputFn: func(t testing.TB) []byte {
77+
{{- if len $func.Normalized.Inputs | lt 1}}
78+
// CUSTOM CODE STARTS HERE
79+
// populate test input here
80+
testInput := {{capitalise $func.Normalized.Name}}Input{}
81+
input, err := Pack{{$func.Normalized.Name}}(testInput)
82+
{{- else if len $func.Normalized.Inputs | eq 1 }}
83+
{{- $input := index $func.Normalized.Inputs 0}}
84+
// CUSTOM CODE STARTS HERE
85+
// set test input to a value here
86+
var testInput {{bindtype $input.Type $structs}}
87+
input, err := Pack{{$func.Normalized.Name}}(testInput)
88+
{{- else}}
89+
input, err := Pack{{$func.Normalized.Name}}()
90+
{{- end}}
91+
require.NoError(t, err)
92+
return input
93+
},
94+
SuppliedGas: {{$func.Normalized.Name}}GasCost,
95+
ReadOnly: true,
96+
ExpectedErr: vmerrs.ErrWriteProtection.Error(),
9497
},
95-
SuppliedGas: {{$func.Normalized.Name}}GasCost,
96-
ReadOnly: true,
97-
ExpectedErr: vmerrs.ErrWriteProtection.Error(),
98-
},
99-
{{- end}}
100-
"insufficient gas for {{decapitalise $func.Normalized.Name}} should fail": {
101-
Caller: common.Address{1},
102-
InputFn: func(t testing.TB) []byte {
103-
{{- if len $func.Normalized.Inputs | lt 1}}
104-
// CUSTOM CODE STARTS HERE
105-
// populate test input here
106-
testInput := {{capitalise $func.Normalized.Name}}Input{}
107-
input, err := Pack{{$func.Normalized.Name}}(testInput)
108-
{{- else if len $func.Normalized.Inputs | eq 1 }}
109-
{{- $input := index $func.Normalized.Inputs 0}}
110-
// CUSTOM CODE STARTS HERE
111-
// set test input to a value here
112-
var testInput {{bindtype $input.Type $structs}}
113-
input, err := Pack{{$func.Normalized.Name}}(testInput)
114-
{{- else}}
115-
input, err := Pack{{$func.Normalized.Name}}()
116-
{{- end}}
117-
require.NoError(t, err)
118-
return input
98+
{{- end}}
99+
"insufficient gas for {{decapitalise $func.Normalized.Name}} should fail": {
100+
Caller: common.Address{1},
101+
InputFn: func(t testing.TB) []byte {
102+
{{- if len $func.Normalized.Inputs | lt 1}}
103+
// CUSTOM CODE STARTS HERE
104+
// populate test input here
105+
testInput := {{capitalise $func.Normalized.Name}}Input{}
106+
input, err := Pack{{$func.Normalized.Name}}(testInput)
107+
{{- else if len $func.Normalized.Inputs | eq 1 }}
108+
{{- $input := index $func.Normalized.Inputs 0}}
109+
// CUSTOM CODE STARTS HERE
110+
// set test input to a value here
111+
var testInput {{bindtype $input.Type $structs}}
112+
input, err := Pack{{$func.Normalized.Name}}(testInput)
113+
{{- else}}
114+
input, err := Pack{{$func.Normalized.Name}}()
115+
{{- end}}
116+
require.NoError(t, err)
117+
return input
118+
},
119+
SuppliedGas: {{$func.Normalized.Name}}GasCost - 1,
120+
ReadOnly: false,
121+
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
119122
},
120-
SuppliedGas: {{$func.Normalized.Name}}GasCost - 1,
121-
ReadOnly: false,
122-
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
123-
},
124-
{{- end}}
125-
}
123+
{{- end}}
124+
}
125+
)
126+
127+
// Test{{.Contract.Type}}Run tests the Run function of the precompile contract.
128+
func Test{{.Contract.Type}}Run(t *testing.T) {
126129
{{- if .Contract.AllowList}}
127130
// Run tests with allowlist tests.
128-
// This adds allowlist run tests to your custom tests
131+
// This adds allowlist tests to your custom tests
129132
// and runs them all together.
130133
// Even if you don't add any custom tests, keep this. This will still
131134
// run the default allowlist tests.
@@ -139,4 +142,23 @@ func TestRun(t *testing.T) {
139142
}
140143
{{- end}}
141144
}
145+
146+
func Benchmark{{.Contract.Type}}(b *testing.B) {
147+
{{- if .Contract.AllowList}}
148+
// Benchmark tests with allowlist tests.
149+
// This adds allowlist tests to your custom tests
150+
// and benchmarks them all together.
151+
// Even if you don't add any custom tests, keep this. This will still
152+
// run the default allowlist tests.
153+
allowlist.BenchPrecompileWithAllowList(b, Module, state.NewTestStateDB, tests)
154+
{{- else}}
155+
// Benchmark tests.
156+
for name, test := range tests {
157+
b.Run(name, func(b *testing.B) {
158+
test.Bench(b, module, newStateDB(b))
159+
})
160+
}
161+
{{- end}}
162+
}
163+
142164
`

0 commit comments

Comments
 (0)