From 3dfb7e44f90a3d61573bd1e3226b63a2a3ee4d65 Mon Sep 17 00:00:00 2001 From: nicksavers Date: Thu, 30 Oct 2014 16:57:17 +0100 Subject: [PATCH] Exception severity uniform --- .../main/java/org/ethereum/vm/Program.java | 18 ++---------- .../src/main/java/org/ethereum/vm/VM.java | 16 +++++------ .../src/test/java/org/ethereum/vm/VMTest.java | 28 +++++++++---------- 3 files changed, 24 insertions(+), 38 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/Program.java b/ethereumj-core/src/main/java/org/ethereum/vm/Program.java index ea40232c..9cc040dd 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/Program.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/Program.java @@ -114,13 +114,8 @@ public class Program { public void setPC(int pc) { this.pc = pc; - if (this.pc == ops.length) + if (this.pc >= ops.length) stop(); - - if (this.pc > ops.length) { - stop(); - throw new PcOverflowException("pc overflow pc=" + pc); - } } public boolean isStopped() { @@ -142,10 +137,8 @@ public class Program { public byte[] sweep(int n) { - if (pc + n > ops.length) { + if (pc + n > ops.length) stop(); - throw new PcOverflowException("pc overflow sweep n: " + n + " pc: " + pc); - } byte[] data = Arrays.copyOfRange(ops, pc, pc + n); pc += n; @@ -825,11 +818,4 @@ public class Program { super(message); } } - - @SuppressWarnings("serial") - public class PcOverflowException extends RuntimeException { - public PcOverflowException(String message) { - super(message); - } - } } \ No newline at end of file diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/VM.java b/ethereumj-core/src/main/java/org/ethereum/vm/VM.java index baf8d634..f58df050 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/VM.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/VM.java @@ -2,7 +2,6 @@ package org.ethereum.vm; import org.ethereum.crypto.HashUtil; import org.ethereum.db.ContractDetails; -import org.ethereum.vm.Program.OutOfGasException; import static org.ethereum.config.SystemProperties.CONFIG; @@ -739,11 +738,11 @@ public class VM { DataWord val = program.storageLoad(key); if (logger.isInfoEnabled()) - hint = "key: " + key + " value: " + val; + hint = "key: " + key + " value: " + val; - if (val == null) { + if (val == null) val = key.and(DataWord.ZERO); - } + program.stackPush(val); program.step(); } break; @@ -825,7 +824,9 @@ public class VM { int nPush = op.val() - PUSH1.val() + 1; byte[] data = program.sweep(nPush); - hint = "" + Hex.toHexString(data); + + if (logger.isInfoEnabled()) + hint = "" + Hex.toHexString(data); program.stackPush(data); } break; @@ -913,9 +914,8 @@ public class VM { vmCounter++; } catch (RuntimeException e) { - if(e instanceof OutOfGasException) - program.spendAllGas(); - logger.warn("VM halted", e.getMessage()); + logger.warn("VM halted", e.getMessage()); + program.spendAllGas(); program.stop(); throw e; } finally { diff --git a/ethereumj-core/src/test/java/org/ethereum/vm/VMTest.java b/ethereumj-core/src/test/java/org/ethereum/vm/VMTest.java index b28a5e1e..597e0d6c 100644 --- a/ethereumj-core/src/test/java/org/ethereum/vm/VMTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/vm/VMTest.java @@ -452,32 +452,32 @@ public class VMTest { assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase() ); } - @Test(expected=RuntimeException.class) // PUSHN OP mal data + @Test // PUSHN OP not enough data public void testPUSHN_1() { VM vm = new VM(); program = new Program(Hex.decode("61AA"), invoke); + String expected = "000000000000000000000000000000000000000000000000000000000000AA00"; - try { - program.fullTrace(); - vm.step(program); - } finally { - assertTrue(program.isStopped()); - } + program.fullTrace(); + vm.step(program); + + assertTrue(program.isStopped()); + assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase()); } - @Test(expected=RuntimeException.class) // PUSHN OP mal data + @Test // PUSHN OP not enough data public void testPUSHN_2() { VM vm = new VM(); program = new Program(Hex.decode("7fAABB"), invoke); + String expected = "AABB000000000000000000000000000000000000000000000000000000000000"; - try { - program.fullTrace(); - vm.step(program); - } finally { - assertTrue(program.isStopped()); - } + program.fullTrace(); + vm.step(program); + + assertTrue(program.isStopped()); + assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase()); } @Test // AND OP