Skip to content

Commit fae4281

Browse files
committed
Fix read-only write protection check and enhance precompiled contract tests
1 parent 992189a commit fae4281

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

core/vm/contracts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func RunPrecompiledContract(p WriteCapablePrecompiledContract, evm *EVM, contrac
291291
//return RunPrecompiledContract(p, evm, input, contract, readOnly)
292292
}
293293
// immediately error out if readOnly
294-
if readOnly && !p.IsWrite() {
294+
if readOnly && p.IsWrite() {
295295
return nil, 0, errWriteProtection
296296
}
297297
gasCost, err := p.RequiredGas(evm, contract, input)

core/vm/contracts_write_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,12 @@ func TestStakingPrecompiles(t *testing.T) {
210210
}
211211

212212
func TestWriteCapablePrecompilesReadOnly(t *testing.T) {
213-
p := &stakingPrecompile{}
214-
expectedError := errWriteProtection
215-
res, _, err := RunPrecompiledContract(p, nil, nil, []byte{}, 0, true)
213+
var (
214+
p = &stakingPrecompile{}
215+
expectedError = errWriteProtection
216+
env = NewEVM(BlockContext{ShardID: 1}, TxContext{}, nil, params.TestChainConfig, Config{})
217+
res, _, err = RunPrecompiledContract(p, env, nil, []byte{}, 0, true)
218+
)
216219
if err != nil {
217220
if err.Error() != expectedError.Error() {
218221
t.Errorf("Expected error %v, got %v", expectedError, err)

core/vm/evm_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,12 @@ func TestEpochPrecompile(t *testing.T) {
2020
CodeAddr: &precompileAddr,
2121
Gas: GasQuickStep,
2222
}
23-
result, err := run(evm,
24-
&contract,
25-
input,
26-
true,
27-
)
23+
result, _, err := RunPrecompiledContract(&epoch{}, evm, &contract, input, GasQuickStep, true)
2824
if err != nil {
2925
t.Fatalf("Got error%v\n", err)
3026
}
3127
resultingEpoch := new(big.Int).SetBytes(result)
3228
if resultingEpoch.Cmp(targetEpoch) != 0 {
33-
t.Error("Epoch did not match")
29+
t.Errorf("Epoch did not match, expected %d, got %d", targetEpoch.Int64(), resultingEpoch.Int64())
3430
}
3531
}

0 commit comments

Comments
 (0)