Skip to content

Commit

Permalink
Create mockPrecompileContext fore contracts_test
Browse files Browse the repository at this point in the history
instead of repeating `NewContext(common.HexToAddress("1337"), mockEVM)`.
  • Loading branch information
karlb committed Sep 15, 2023
1 parent 5e03be5 commit 741fc0e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 2 additions & 0 deletions core/vm/celo_contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ var mockEVM = &EVM{
Context: vmBlockCtx,
TxContext: vmTxCtx,
}

var mockPrecompileContext = NewContext(common.HexToAddress("1337"), mockEVM)
8 changes: 4 additions & 4 deletions core/vm/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func testPrecompiled(addr string, test precompiledTest, t *testing.T) {
in := common.Hex2Bytes(test.Input)
gas := p.RequiredGas(in)
t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) {
if res, _, err := RunPrecompiledContract(p, in, gas, NewContext(common.HexToAddress("1337"), mockEVM)); err != nil {
if res, _, err := RunPrecompiledContract(p, in, gas, mockPrecompileContext); err != nil {
t.Error(err)
} else if common.Bytes2Hex(res) != test.Expected {
t.Errorf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res))
Expand All @@ -118,7 +118,7 @@ func testPrecompiledOOG(addr string, test precompiledTest, t *testing.T) {
gas := p.RequiredGas(in) - 1

t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) {
_, _, err := RunPrecompiledContract(p, in, gas, NewContext(common.HexToAddress("1337"), mockEVM))
_, _, err := RunPrecompiledContract(p, in, gas, mockPrecompileContext)
if err.Error() != "out of gas" {
t.Errorf("Expected error [out of gas], got [%v]", err)
}
Expand All @@ -135,7 +135,7 @@ func testPrecompiledFailure(addr string, test precompiledFailureTest, t *testing
in := common.Hex2Bytes(test.Input)
gas := p.RequiredGas(in)
t.Run(test.Name, func(t *testing.T) {
_, _, err := RunPrecompiledContract(p, in, gas, NewContext(common.HexToAddress("1337"), mockEVM))
_, _, err := RunPrecompiledContract(p, in, gas, mockPrecompileContext)
if err.Error() != test.ExpectedError {
t.Errorf("Expected error [%v], got [%v]", test.ExpectedError, err)
}
Expand Down Expand Up @@ -167,7 +167,7 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) {
bench.ResetTimer()
for i := 0; i < bench.N; i++ {
copy(data, in)
res, _, err = RunPrecompiledContract(p, data, reqGas, NewContext(common.HexToAddress("1337"), mockEVM))
res, _, err = RunPrecompiledContract(p, data, reqGas, mockPrecompileContext)
}
bench.StopTimer()
elapsed := uint64(time.Since(start))
Expand Down

0 comments on commit 741fc0e

Please sign in to comment.