Add JUMPDEST

This commit is contained in:
nicksavers 2014-10-16 03:49:08 +02:00
parent 07245866bb
commit 7b914794e9
4 changed files with 70 additions and 13 deletions

View File

@ -55,6 +55,12 @@ public class Program {
}
}
public byte getOp(int pc) {
if(ops.length == 0)
return 0;
return ops[pc];
}
public byte getCurrentOp() {
if(ops.length == 0)
return 0;
@ -93,8 +99,11 @@ public class Program {
}
public void setPC(DataWord pc) {
this.setPC(pc.intValue());
}
this.pc = pc.intValue();
public void setPC(int pc) {
this.pc = pc;
if (this.pc == ops.length)
stop();
@ -105,10 +114,6 @@ public class Program {
}
}
public void setPC(int pc) {
this.pc = pc;
}
public boolean isStopped() {
return stopped;
}

View File

@ -800,23 +800,25 @@ public class VM {
case JUMP:{
program.stackRequire(1);
DataWord pos = program.stackPop();
// if (!pos.equals(DataWord.ZERO) && OpCode.code(program.getCurrentOp()) != OpCode.JUMPDEST)
int nextPC = pos.intValue();
// if (nextPC != 0 && program.getOp(nextPC-1) != OpCode.JUMPDEST.asInt())
// throw new BadJumpDestinationException();
if (logger.isInfoEnabled())
hint = "~> " + pos.value();
program.setPC(pos);
program.setPC(nextPC);
} break;
case JUMPI:{
program.stackRequire(2);
DataWord pos = program.stackPop();
DataWord cond = program.stackPop();
// if (!pos.isZero() && OpCode.code(program.getCurrentOp()) != OpCode.JUMPDEST)
int nextPC = pos.intValue();
// if (nextPC != 0 && program.getOp(nextPC-1) != OpCode.JUMPDEST.asInt())
// throw new BadJumpDestinationException();
if (!cond.isZero()) {
program.setPC(pos);
program.setPC(nextPC);
} else {
program.step();
}

View File

@ -12,8 +12,9 @@ import java.nio.file.Files;
public class JSONReader {
public static String loadJSON(String filename) {
String json = getFromUrl("https://raw.githubusercontent.com/ethereum/tests/master/vmtests/" + filename);
return json == "" ? json = getFromLocal(filename) : json;
return getFromLocal(filename);
// String json = getFromUrl("https://raw.githubusercontent.com/ethereum/tests/master/vmtests/" + filename);
// return json == "" ? json = getFromLocal(filename) : json;
}
public static String getFromLocal(String filename) {

View File

@ -1777,6 +1777,55 @@ public class VMTest {
}
}
@Test // JUMPDEST OP for JUMP
public void testJUMPDEST_1() {
VM vm = new VM();
ProgramInvokeMockImpl invoke = new ProgramInvokeMockImpl();
Program program = new Program(Hex.decode("602360085860015d600257"), invoke);
String s_expected_key = "0000000000000000000000000000000000000000000000000000000000000002";
String s_expected_val = "0000000000000000000000000000000000000000000000000000000000000023";
vm.step(program);
vm.step(program);
vm.step(program);
vm.step(program);
vm.step(program);
DataWord key = new DataWord(Hex.decode(s_expected_key));
DataWord val = program.result.getRepository().getStorageValue(invoke.getOwnerAddress().getNoLeadZeroesData(), key);
assertTrue(program.isStopped());
program.getResult().getRepository().close();
assertEquals(s_expected_val, Hex.toHexString(val.getData()).toUpperCase());
}
@Test // JUMPDEST OP for JUMPI
public void testJUMPDEST_2() {
VM vm = new VM();
ProgramInvokeMockImpl invoke = new ProgramInvokeMockImpl();
Program program = new Program(Hex.decode("60236001600a5960015d600257"), invoke);
String s_expected_key = "0000000000000000000000000000000000000000000000000000000000000002";
String s_expected_val = "0000000000000000000000000000000000000000000000000000000000000023";
vm.step(program);
vm.step(program);
vm.step(program);
vm.step(program);
vm.step(program);
vm.step(program);
DataWord key = new DataWord(Hex.decode(s_expected_key));
DataWord val = program.result.getRepository().getStorageValue(invoke.getOwnerAddress().getNoLeadZeroesData(), key);
assertTrue(program.isStopped());
program.getResult().getRepository().close();
assertEquals(s_expected_val, Hex.toHexString(val.getData()).toUpperCase());
}
@Test // ADD OP mal
public void testADD_1() {