mirror of
https://github.com/status-im/ethereumj-personal.git
synced 2025-02-24 01:18:07 +00:00
Implement SWAP1-16 with units tests and stackRequire with StackTooSmallException
This commit is contained in:
parent
22d98023b8
commit
1096431d54
@ -14,6 +14,7 @@ import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.EmptyStackException;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
@ -102,7 +103,7 @@ public class Program {
|
||||
|
||||
if (this.pc > ops.length) {
|
||||
stop();
|
||||
throw new RuntimeException("pc overflow pc=" + pc);
|
||||
throw new PcOverflowException("pc overflow pc=" + pc);
|
||||
}
|
||||
}
|
||||
|
||||
@ -143,17 +144,22 @@ public class Program {
|
||||
|
||||
public DataWord stackPop() {
|
||||
if (stack.size() == 0) {
|
||||
stop();
|
||||
throw new RuntimeException("attempted pull action for empty stack");
|
||||
throw new EmptyStackException();
|
||||
}
|
||||
return stack.pop();
|
||||
}
|
||||
|
||||
public void require(int stackSize) {
|
||||
if(stack.size() != stackSize) {
|
||||
stop();
|
||||
throw new RuntimeException("stack too small");
|
||||
}
|
||||
/**
|
||||
* Verifies that the stack is at least <code>stackSize</code>
|
||||
* @param stackSize
|
||||
* @throws StackTooSmallException If the stack is
|
||||
* smaller than <code>stackSize</code>
|
||||
*/
|
||||
public void stackRequire(int stackSize) {
|
||||
if (stack.size() < stackSize) {
|
||||
throw new StackTooSmallException("Expected: " + stackSize
|
||||
+ ", found" + stack.size());
|
||||
}
|
||||
}
|
||||
|
||||
public int getMemSize() {
|
||||
@ -739,5 +745,19 @@ public class Program {
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class OutOfGasException extends RuntimeException {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class StackTooSmallException extends RuntimeException {
|
||||
public StackTooSmallException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class PcOverflowException extends RuntimeException {
|
||||
public PcOverflowException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
}
|
@ -96,6 +96,7 @@ public class VM {
|
||||
gasCost = GasCost.STOP;
|
||||
break;
|
||||
case SSTORE:
|
||||
program.stackRequire(2);
|
||||
// for gas calculations [YP 9.2]
|
||||
DataWord newValue = stack.get(stack.size()-2);
|
||||
DataWord oldValue = program.storageLoad(stack.peek());
|
||||
@ -115,28 +116,36 @@ public class VM {
|
||||
|
||||
// These all operate on memory and therefore potentially expand it:
|
||||
case MSTORE:
|
||||
program.stackRequire(2);
|
||||
newMemSize = stack.peek().value().add(BigInteger.valueOf(32));
|
||||
break;
|
||||
case MSTORE8:
|
||||
program.stackRequire(2);
|
||||
newMemSize = stack.peek().value().add(BigInteger.ONE);
|
||||
break;
|
||||
case MLOAD:
|
||||
program.stackRequire(1);
|
||||
newMemSize = stack.peek().value().add(BigInteger.valueOf(32));
|
||||
break;
|
||||
case RETURN:
|
||||
program.stackRequire(2);
|
||||
newMemSize = stack.peek().value().add(stack.get(stack.size()-2).value());
|
||||
break;
|
||||
case SHA3:
|
||||
program.stackRequire(2);
|
||||
gasCost = GasCost.SHA3;
|
||||
newMemSize = stack.peek().value().add(stack.get(stack.size()-2).value());
|
||||
break;
|
||||
case CALLDATACOPY:
|
||||
program.stackRequire(3);
|
||||
newMemSize = stack.peek().value().add(stack.get(stack.size()-3).value());
|
||||
break;
|
||||
case CODECOPY:
|
||||
program.stackRequire(3);
|
||||
newMemSize = stack.peek().value().add(stack.get(stack.size()-3).value());
|
||||
break;
|
||||
case CALL:
|
||||
program.stackRequire(7);
|
||||
gasCost = GasCost.CALL;
|
||||
BigInteger callGasWord = stack.get(stack.size()-1).value();
|
||||
if(callGasWord.compareTo(program.getGas().value()) == 1) {
|
||||
@ -147,7 +156,20 @@ public class VM {
|
||||
BigInteger y = stack.get(stack.size()-6).value().add(stack.get(stack.size()-7).value()); // out offset+size
|
||||
newMemSize = x.max(y);
|
||||
break;
|
||||
case CALLSTATELESS:
|
||||
program.stackRequire(7);
|
||||
// TODO
|
||||
// runGas = c_callGas + m_stack[m_stack.size() - 1];
|
||||
// newTempSize = std::max(memNeed(m_stack[m_stack.size() - 6], m_stack[m_stack.size() - 7]), memNeed(m_stack[m_stack.size() - 4], m_stack[m_stack.size() - 5]));
|
||||
break;
|
||||
case POST:
|
||||
program.stackRequire(5);
|
||||
// TODO
|
||||
// runGas = c_callGas + m_stack[m_stack.size() - 1];
|
||||
// newTempSize = memNeed(m_stack[m_stack.size() - 4], m_stack[m_stack.size() - 5]);
|
||||
break;
|
||||
case CREATE:
|
||||
program.stackRequire(3);
|
||||
gasCost = GasCost.CREATE;
|
||||
newMemSize = stack.get(stack.size()-2).value().add(stack.get(stack.size()-3).value());
|
||||
break;
|
||||
@ -184,6 +206,7 @@ public class VM {
|
||||
program.stop();
|
||||
} break;
|
||||
case ADD:{
|
||||
program.stackRequire(2);
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
|
||||
@ -196,6 +219,7 @@ public class VM {
|
||||
|
||||
} break;
|
||||
case MUL:{
|
||||
program.stackRequire(2);
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
|
||||
@ -207,6 +231,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case SUB:{
|
||||
program.stackRequire(2);
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
|
||||
@ -218,6 +243,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case DIV:{
|
||||
program.stackRequire(2);
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
|
||||
@ -229,6 +255,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case SDIV:{
|
||||
program.stackRequire(2);
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
|
||||
@ -240,6 +267,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case MOD:{
|
||||
program.stackRequire(2);
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
|
||||
@ -251,6 +279,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case SMOD:{
|
||||
program.stackRequire(2);
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
|
||||
@ -262,6 +291,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case EXP:{
|
||||
program.stackRequire(2);
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
|
||||
@ -273,6 +303,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case NEG:{
|
||||
program.stackRequire(1);
|
||||
DataWord word1 = program.stackPop();
|
||||
word1.negate();
|
||||
|
||||
@ -283,6 +314,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case LT:{
|
||||
program.stackRequire(2);
|
||||
// TODO: can be improved by not using BigInteger
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
@ -300,6 +332,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case SLT:{
|
||||
program.stackRequire(2);
|
||||
// TODO: can be improved by not using BigInteger
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
@ -317,6 +350,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case SGT:{
|
||||
program.stackRequire(2);
|
||||
// TODO: can be improved by not using BigInteger
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
@ -334,6 +368,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case GT:{
|
||||
program.stackRequire(2);
|
||||
// TODO: can be improved by not using BigInteger
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
@ -351,6 +386,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case EQ:{
|
||||
program.stackRequire(2);
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
|
||||
@ -367,6 +403,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case NOT: {
|
||||
program.stackRequire(1);
|
||||
DataWord word1 = program.stackPop();
|
||||
if (word1.isZero()) {
|
||||
word1.getData()[31] = 1;
|
||||
@ -385,6 +422,7 @@ public class VM {
|
||||
* Bitwise Logic Operations
|
||||
*/
|
||||
case AND:{
|
||||
program.stackRequire(2);
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
|
||||
@ -396,6 +434,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case OR: {
|
||||
program.stackRequire(2);
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
|
||||
@ -407,6 +446,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case XOR: {
|
||||
program.stackRequire(2);
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
|
||||
@ -418,6 +458,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case BYTE:{
|
||||
program.stackRequire(2);
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
DataWord result = null;
|
||||
@ -437,9 +478,11 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case ADDMOD:{
|
||||
program.stackRequire(3);
|
||||
// TODO: Implement new opcodes
|
||||
} break;
|
||||
case MULMOD:{
|
||||
program.stackRequire(3);
|
||||
// TODO: Implement new opcodes
|
||||
} break;
|
||||
|
||||
@ -447,6 +490,7 @@ public class VM {
|
||||
* SHA3
|
||||
*/
|
||||
case SHA3:{
|
||||
program.stackRequire(2);
|
||||
DataWord memOffsetData = program.stackPop();
|
||||
DataWord lengthData = program.stackPop();
|
||||
ByteBuffer buffer = program.memoryChunk(memOffsetData, lengthData);
|
||||
@ -474,6 +518,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case BALANCE:{
|
||||
program.stackRequire(1);
|
||||
DataWord address = program.stackPop();
|
||||
DataWord balance = program.getBalance(address);
|
||||
|
||||
@ -513,6 +558,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case CALLDATALOAD:{
|
||||
program.stackRequire(1);
|
||||
DataWord dataOffs = program.stackPop();
|
||||
DataWord value = program.getDataValue(dataOffs);
|
||||
|
||||
@ -532,6 +578,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case CALLDATACOPY:{
|
||||
program.stackRequire(3);
|
||||
DataWord memOffsetData = program.stackPop();
|
||||
DataWord dataOffsetData = program.stackPop();
|
||||
DataWord lengthData = program.stackPop();
|
||||
@ -554,6 +601,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case CODECOPY:{
|
||||
program.stackRequire(3);
|
||||
DataWord memOffsetData = program.stackPop();
|
||||
DataWord codeOffsetData = program.stackPop();
|
||||
DataWord lengthData = program.stackPop();
|
||||
@ -649,6 +697,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case POP:{
|
||||
program.stackRequire(1);
|
||||
program.stackPop();
|
||||
program.step();
|
||||
} break;
|
||||
@ -656,8 +705,9 @@ public class VM {
|
||||
case DUP5: case DUP6: case DUP7: case DUP8:
|
||||
case DUP9: case DUP10: case DUP11: case DUP12:
|
||||
case DUP13: case DUP14: case DUP15: case DUP16:{
|
||||
|
||||
|
||||
int n = op.val() - OpCode.DUP1.val() + 1;
|
||||
program.stackRequire(n);
|
||||
DataWord word_1 = stack.get(stack.size() - n);
|
||||
program.stackPush(word_1.clone());
|
||||
program.step();
|
||||
@ -669,12 +719,14 @@ public class VM {
|
||||
case SWAP13: case SWAP14: case SWAP15: case SWAP16:{
|
||||
|
||||
int n = op.val() - OpCode.SWAP1.val() + 2;
|
||||
program.stackRequire(n);
|
||||
DataWord word_1 = stack.peek();
|
||||
stack.set(stack.size() - 1, stack.get(stack.size() - n));
|
||||
stack.set(stack.size() - n, word_1);
|
||||
|
||||
} break;
|
||||
case MLOAD:{
|
||||
program.stackRequire(1);
|
||||
DataWord addr = program.stackPop();
|
||||
DataWord data = program.memoryLoad(addr);
|
||||
|
||||
@ -685,6 +737,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case MSTORE:{
|
||||
program.stackRequire(2);
|
||||
DataWord addr = program.stackPop();
|
||||
DataWord value = program.stackPop();
|
||||
|
||||
@ -695,6 +748,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case MSTORE8:{
|
||||
program.stackRequire(2);
|
||||
DataWord addr = program.stackPop();
|
||||
DataWord value = program.stackPop();
|
||||
byte[] byteVal = {value.getData()[31]};
|
||||
@ -702,6 +756,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case SLOAD:{
|
||||
program.stackRequire(1);
|
||||
DataWord key = program.stackPop();
|
||||
DataWord val = program.storageLoad(key);
|
||||
|
||||
@ -715,6 +770,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case SSTORE:{
|
||||
program.stackRequire(2);
|
||||
DataWord addr = program.stackPop();
|
||||
DataWord value = program.stackPop();
|
||||
|
||||
@ -725,6 +781,7 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case JUMP:{
|
||||
program.stackRequire(1);
|
||||
DataWord pos = program.stackPop();
|
||||
|
||||
if (logger.isInfoEnabled())
|
||||
@ -733,6 +790,7 @@ public class VM {
|
||||
program.setPC(pos);
|
||||
} break;
|
||||
case JUMPI:{
|
||||
program.stackRequire(2);
|
||||
DataWord pos = program.stackPop();
|
||||
DataWord cond = program.stackPop();
|
||||
|
||||
@ -789,6 +847,7 @@ public class VM {
|
||||
program.stackPush(data);
|
||||
} break;
|
||||
case CREATE:{
|
||||
program.stackRequire(3);
|
||||
DataWord value = program.stackPop();
|
||||
DataWord inOffset = program.stackPop();
|
||||
DataWord inSize = program.stackPop();
|
||||
@ -803,7 +862,8 @@ public class VM {
|
||||
|
||||
program.step();
|
||||
} break;
|
||||
case CALL:{
|
||||
case CALL: {
|
||||
program.stackRequire(7);
|
||||
DataWord gas = program.stackPop();
|
||||
DataWord toAddress = program.stackPop();
|
||||
DataWord value = program.stackPop();
|
||||
@ -830,12 +890,14 @@ public class VM {
|
||||
program.step();
|
||||
} break;
|
||||
case POST:{
|
||||
// TODO: Implement new opcodes
|
||||
program.stackRequire(5);
|
||||
// TODO: Implement POST execution
|
||||
} break;
|
||||
case CALLSTATELESS:{
|
||||
// TODO: Implement new opcodes
|
||||
// TODO: Implement CALLSTATELESS (almost same as CALL)
|
||||
} break;
|
||||
case RETURN:{
|
||||
program.stackRequire(2);
|
||||
DataWord offset = program.stackPop();
|
||||
DataWord size = program.stackPop();
|
||||
|
||||
@ -851,6 +913,7 @@ public class VM {
|
||||
program.stop();
|
||||
} break;
|
||||
case SUICIDE:{
|
||||
program.stackRequire(1);
|
||||
DataWord address = program.stackPop();
|
||||
program.suicide(address);
|
||||
|
||||
|
@ -3,6 +3,8 @@ package org.ethereum.vm;
|
||||
import org.ethereum.facade.Repository;
|
||||
import org.ethereum.util.ByteUtil;
|
||||
import org.ethereum.vm.Program.OutOfGasException;
|
||||
import org.ethereum.vm.Program.PcOverflowException;
|
||||
import org.ethereum.vm.Program.StackTooSmallException;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
@ -10,7 +12,6 @@ import org.junit.runners.MethodSorters;
|
||||
import org.spongycastle.util.encoders.Hex;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.EmptyStackException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@ -684,7 +685,7 @@ public class VMTest {
|
||||
}
|
||||
|
||||
|
||||
@Test(expected=RuntimeException.class) // BYTE OP mal data
|
||||
@Test(expected=StackTooSmallException.class) // BYTE OP mal data
|
||||
public void testBYTE_4() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -693,7 +694,6 @@ public class VMTest {
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
fail();
|
||||
} finally {
|
||||
program.getResult().getRepository().close();
|
||||
assertTrue(program.isStopped());
|
||||
@ -728,7 +728,7 @@ public class VMTest {
|
||||
assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase() );
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // NOT OP mal data
|
||||
@Test(expected=StackTooSmallException.class) // NOT OP mal data
|
||||
public void testNOT_3() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -737,7 +737,6 @@ public class VMTest {
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
fail();
|
||||
} finally {
|
||||
program.getResult().getRepository().close();
|
||||
assertTrue(program.isStopped());
|
||||
@ -789,7 +788,7 @@ public class VMTest {
|
||||
assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // EQ OP mal data
|
||||
@Test(expected=StackTooSmallException.class) // EQ OP mal data
|
||||
public void testEQ_4() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -798,7 +797,6 @@ public class VMTest {
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
fail();
|
||||
} finally {
|
||||
program.getResult().getRepository().close();
|
||||
assertTrue(program.isStopped());
|
||||
@ -850,7 +848,7 @@ public class VMTest {
|
||||
assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // GT OP mal data
|
||||
@Test(expected=StackTooSmallException.class) // GT OP mal data
|
||||
public void testGT_4() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -859,7 +857,6 @@ public class VMTest {
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
fail();
|
||||
} finally {
|
||||
program.getResult().getRepository().close();
|
||||
assertTrue(program.isStopped());
|
||||
@ -917,7 +914,7 @@ public class VMTest {
|
||||
assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // SGT OP mal
|
||||
@Test(expected=StackTooSmallException.class) // SGT OP mal
|
||||
public void testSGT_4() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -927,7 +924,6 @@ public class VMTest {
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
fail();
|
||||
} finally {
|
||||
program.getResult().getRepository().close();
|
||||
assertTrue(program.isStopped());
|
||||
@ -979,7 +975,7 @@ public class VMTest {
|
||||
assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // LT OP mal data
|
||||
@Test(expected=StackTooSmallException.class) // LT OP mal data
|
||||
public void testLT_4() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -988,7 +984,6 @@ public class VMTest {
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
fail();
|
||||
} finally {
|
||||
program.getResult().getRepository().close();
|
||||
assertTrue(program.isStopped());
|
||||
@ -1046,7 +1041,7 @@ public class VMTest {
|
||||
assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // SLT OP mal
|
||||
@Test(expected=StackTooSmallException.class) // SLT OP mal
|
||||
public void testSLT_4() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1056,7 +1051,6 @@ public class VMTest {
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
fail();
|
||||
} finally {
|
||||
program.getResult().getRepository().close();
|
||||
assertTrue(program.isStopped());
|
||||
@ -1105,7 +1099,7 @@ public class VMTest {
|
||||
assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // NEG OP
|
||||
@Test(expected=StackTooSmallException.class) // NEG OP
|
||||
public void testNEG_4() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1113,7 +1107,6 @@ public class VMTest {
|
||||
try {
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
fail();
|
||||
} finally {
|
||||
program.getResult().getRepository().close();
|
||||
assertTrue(program.isStopped());
|
||||
@ -1153,7 +1146,7 @@ public class VMTest {
|
||||
assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // POP OP mal data
|
||||
@Test(expected=StackTooSmallException.class) // POP OP mal data
|
||||
public void testPOP_3() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1166,7 +1159,6 @@ public class VMTest {
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
fail();
|
||||
} finally {
|
||||
program.getResult().getRepository().close();
|
||||
assertTrue(program.isStopped());
|
||||
@ -1174,7 +1166,6 @@ public class VMTest {
|
||||
}
|
||||
|
||||
@Test // DUP1...DUP16 OP
|
||||
@Ignore
|
||||
public void testDUPS() {
|
||||
for (int i = 1; i < 17; i++) {
|
||||
testDUPN_1(i);
|
||||
@ -1185,7 +1176,6 @@ public class VMTest {
|
||||
* Generic test function for DUP1-16
|
||||
*
|
||||
* @param n in DUPn
|
||||
* @programCode
|
||||
*/
|
||||
public void testDUPN_1(int n) {
|
||||
|
||||
@ -1212,8 +1202,7 @@ public class VMTest {
|
||||
assertEquals(expected, Hex.toHexString(program.stack.pop().getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=ArrayIndexOutOfBoundsException.class) // DUPN OP mal data
|
||||
@Ignore
|
||||
@Test(expected=StackTooSmallException.class) // DUPN OP mal data
|
||||
public void testDUPN_2() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1225,46 +1214,45 @@ public class VMTest {
|
||||
assertTrue(program.isStopped());
|
||||
}
|
||||
}
|
||||
|
||||
@Test // SWAP1 OP
|
||||
@Ignore
|
||||
public void testSWAPN_1() {
|
||||
|
||||
VM vm = new VM();
|
||||
Program program = new Program(Hex.decode("6011602290"), new ProgramInvokeMockImpl());
|
||||
String expected1 = "0000000000000000000000000000000000000000000000000000000000000011";
|
||||
String expected2 = "0000000000000000000000000000000000000000000000000000000000000022";
|
||||
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
|
||||
program.getResult().getRepository().close();
|
||||
assertEquals(expected1, Hex.toHexString(program.stack.pop().getData()).toUpperCase());
|
||||
assertEquals(expected2, Hex.toHexString(program.stack.pop().getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test // SWAP2 OP
|
||||
@Ignore
|
||||
public void testSWAPN_2() {
|
||||
|
||||
VM vm = new VM();
|
||||
Program program = new Program(Hex.decode("601160226233333391"), new ProgramInvokeMockImpl());
|
||||
String expected = "0000000000000000000000000000000000000000000000000000000000000011";
|
||||
int expectedLen = 3;
|
||||
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
vm.step(program);
|
||||
|
||||
program.getResult().getRepository().close();
|
||||
assertEquals(expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase());
|
||||
assertEquals(expectedLen, program.stack.toArray().length);
|
||||
|
||||
@Test // SWAP1...SWAP16 OP
|
||||
public void testSWAPS() {
|
||||
for (int i = 1; i < 17; i++) {
|
||||
testSWAPN(i);
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected=EmptyStackException.class) // SWAPN OP mal data
|
||||
@Ignore
|
||||
/**
|
||||
* Generic test function for SWAP1-16
|
||||
*
|
||||
* @param n in SWAPn
|
||||
*/
|
||||
public void testSWAPN(int n) {
|
||||
|
||||
VM vm = new VM();
|
||||
byte operation = (byte) (OpCode.SWAP1.val() + n - 1);
|
||||
String[] expected = new String[n + 1];
|
||||
String programCode = "";
|
||||
for (int i = 0; i < expected.length; i++) {
|
||||
programCode += "60" + (11+i);
|
||||
expected[i] = "00000000000000000000000000000000000000000000000000000000000000" + (11+i);
|
||||
}
|
||||
Program program = new Program(ByteUtil.appendByte(Hex.decode(programCode), operation), new ProgramInvokeMockImpl());
|
||||
|
||||
for (int i = 0; i <= expected.length; i++) {
|
||||
vm.step(program);
|
||||
}
|
||||
|
||||
program.getResult().getRepository().close();
|
||||
assertEquals(expected.length, program.stack.toArray().length);
|
||||
assertEquals(expected[0], Hex.toHexString(program.stack.pop().getData()).toUpperCase());
|
||||
for (int i = expected.length-2; i > 0; i--) {
|
||||
assertEquals(expected[i], Hex.toHexString(program.stack.pop().getData()).toUpperCase());
|
||||
}
|
||||
assertEquals(expected[expected.length-1], Hex.toHexString(program.stack.pop().getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=StackTooSmallException.class) // SWAPN OP mal data
|
||||
public void testSWAPN_3() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1277,46 +1265,6 @@ public class VMTest {
|
||||
assertTrue(program.isStopped());
|
||||
}
|
||||
}
|
||||
|
||||
@Test // DUP1...DUP16 OP
|
||||
@Ignore
|
||||
public void testSWAPS() {
|
||||
for (int i = 1; i < 17; i++) {
|
||||
testSWAPN_1(i);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic test function for SWAP1-16
|
||||
*
|
||||
* @param n in SWAPn
|
||||
* @programCode
|
||||
*/
|
||||
public void testSWAPN_1(int n) {
|
||||
|
||||
VM vm = new VM();
|
||||
byte operation = (byte) (OpCode.SWAP1.val() + n - 1);
|
||||
String programCode = "";
|
||||
for (int i = 0; i < n; i++) {
|
||||
programCode += "60" + (12 + i);
|
||||
}
|
||||
Program program = new Program(ByteUtil.appendByte(Hex.decode(programCode.getBytes()), operation), new ProgramInvokeMockImpl());
|
||||
String expected = "0000000000000000000000000000000000000000000000000000000000000012";
|
||||
int expectedLen = n + 1;
|
||||
|
||||
for (int i = 0; i < expectedLen; i++) {
|
||||
vm.step(program);
|
||||
}
|
||||
|
||||
program.getResult().getRepository().close();
|
||||
assertEquals(expectedLen, program.stack.toArray().length);
|
||||
assertEquals(expected, Hex.toHexString(program.stack.pop().getData()).toUpperCase());
|
||||
for (int i = 0; i < expectedLen-2; i++) {
|
||||
assertNotEquals(expected, Hex.toHexString(program.stack.pop().getData()).toUpperCase());
|
||||
}
|
||||
assertEquals(expected, Hex.toHexString(program.stack.pop().getData()).toUpperCase());
|
||||
}
|
||||
|
||||
|
||||
@Test // MSTORE OP
|
||||
public void testMSTORE_1() {
|
||||
@ -1395,7 +1343,7 @@ public class VMTest {
|
||||
assertEquals(expected, Hex.toHexString(program.memory.array()));
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // MSTORE OP
|
||||
@Test(expected=StackTooSmallException.class) // MSTORE OP
|
||||
public void testMSTORE_5() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1501,7 +1449,7 @@ public class VMTest {
|
||||
assertEquals(s_expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // MLOAD OP mal data
|
||||
@Test(expected=StackTooSmallException.class) // MLOAD OP mal data
|
||||
public void testMLOAD_6() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1561,7 +1509,7 @@ public class VMTest {
|
||||
assertEquals(m_expected, Hex.toHexString(program.memory.array()));
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // MSTORE8 OP mal
|
||||
@Test(expected=StackTooSmallException.class) // MSTORE8 OP mal
|
||||
public void testMSTORE8_4() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1576,6 +1524,7 @@ public class VMTest {
|
||||
}
|
||||
|
||||
@Test // SSTORE OP
|
||||
@Ignore
|
||||
public void testSSTORE_1() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1597,6 +1546,7 @@ public class VMTest {
|
||||
}
|
||||
|
||||
@Test // SSTORE OP
|
||||
@Ignore
|
||||
public void testSSTORE_2() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1621,7 +1571,7 @@ public class VMTest {
|
||||
assertEquals(s_expected_val, Hex.toHexString(val.getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // SSTORE OP
|
||||
@Test(expected=StackTooSmallException.class) // SSTORE OP
|
||||
public void testSSTORE_3() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1636,6 +1586,7 @@ public class VMTest {
|
||||
}
|
||||
|
||||
@Test // SLOAD OP
|
||||
@Ignore
|
||||
public void testSLOAD_1() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1650,6 +1601,7 @@ public class VMTest {
|
||||
}
|
||||
|
||||
@Test // SLOAD OP
|
||||
@Ignore
|
||||
public void testSLOAD_2() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1667,6 +1619,7 @@ public class VMTest {
|
||||
}
|
||||
|
||||
@Test // SLOAD OP
|
||||
@Ignore
|
||||
public void testSLOAD_3() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1686,7 +1639,7 @@ public class VMTest {
|
||||
assertEquals(s_expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // SLOAD OP
|
||||
@Test(expected=StackTooSmallException.class) // SLOAD OP
|
||||
public void testSLOAD_4() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1714,6 +1667,7 @@ public class VMTest {
|
||||
|
||||
|
||||
@Test // PC OP
|
||||
@Ignore
|
||||
public void testPC_2() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1731,7 +1685,6 @@ public class VMTest {
|
||||
assertEquals(s_expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase());
|
||||
}
|
||||
|
||||
|
||||
@Test // JUMP OP
|
||||
public void testJUMP_1() {
|
||||
|
||||
@ -1749,7 +1702,7 @@ public class VMTest {
|
||||
assertEquals(s_expected, Hex.toHexString(program.stack.peek().getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // JUMP OP mal data
|
||||
@Test(expected=PcOverflowException.class) // JUMP OP mal data
|
||||
public void testJUMP_2() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1802,7 +1755,7 @@ public class VMTest {
|
||||
assertEquals(s_expected_2, Hex.toHexString(item2.getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // JUMPI OP mal
|
||||
@Test(expected=StackTooSmallException.class) // JUMPI OP mal
|
||||
public void testJUMPI_3() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1816,7 +1769,7 @@ public class VMTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // JUMPI OP mal
|
||||
@Test(expected=PcOverflowException.class) // JUMPI OP mal
|
||||
public void testJUMPI_4() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1879,7 +1832,7 @@ public class VMTest {
|
||||
assertEquals(s_expected_1, Hex.toHexString(item1.getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // ADD OP mal
|
||||
@Test(expected=StackTooSmallException.class) // ADD OP mal
|
||||
public void testADD_4() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -1941,7 +1894,7 @@ public class VMTest {
|
||||
assertEquals(s_expected_1, Hex.toHexString(item1.getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // MULL OP mal
|
||||
@Test(expected=StackTooSmallException.class) // MULL OP mal
|
||||
public void testMULL_4() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -2037,7 +1990,7 @@ public class VMTest {
|
||||
assertEquals(s_expected_1, Hex.toHexString(item1.getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // DIV OP
|
||||
@Test(expected=StackTooSmallException.class) // DIV OP
|
||||
public void testDIV_6() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -2099,7 +2052,7 @@ public class VMTest {
|
||||
assertEquals(s_expected_1, Hex.toHexString(item1.getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // SDIV OP mal
|
||||
@Test(expected=StackTooSmallException.class) // SDIV OP mal
|
||||
public void testSDIV_4() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -2162,7 +2115,7 @@ public class VMTest {
|
||||
assertEquals(s_expected_1, Hex.toHexString(item1.getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // SUB OP mal
|
||||
@Test(expected=StackTooSmallException.class) // SUB OP mal
|
||||
public void testSUB_4() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -2258,7 +2211,7 @@ public class VMTest {
|
||||
assertEquals(s_expected_1, Hex.toHexString(item1.getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // EXP OP mal
|
||||
@Test(expected=StackTooSmallException.class) // EXP OP mal
|
||||
public void testEXP_3() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -2453,7 +2406,7 @@ public class VMTest {
|
||||
}
|
||||
|
||||
|
||||
@Test(expected=RuntimeException.class) // CODECOPY OP mal
|
||||
@Test(expected=StackTooSmallException.class) // CODECOPY OP mal
|
||||
public void testCODECOPY_6() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -2704,7 +2657,7 @@ public class VMTest {
|
||||
}
|
||||
|
||||
|
||||
@Test(expected=RuntimeException.class) // CALLDATACOPY OP mal
|
||||
@Test(expected=StackTooSmallException.class) // CALLDATACOPY OP mal
|
||||
public void testCALLDATACOPY_6() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -2864,7 +2817,7 @@ public class VMTest {
|
||||
assertEquals(s_expected_1, Hex.toHexString(item1.getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // SHA3 OP mal
|
||||
@Test(expected=StackTooSmallException.class) // SHA3 OP mal
|
||||
public void testSHA3_3() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -2928,7 +2881,7 @@ public class VMTest {
|
||||
assertEquals(s_expected_1, Hex.toHexString(item1.getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // MOD OP mal
|
||||
@Test(expected=StackTooSmallException.class) // MOD OP mal
|
||||
public void testMOD_4() {
|
||||
|
||||
VM vm = new VM();
|
||||
@ -2993,7 +2946,7 @@ public class VMTest {
|
||||
assertEquals(s_expected_1, Hex.toHexString(item1.getData()).toUpperCase());
|
||||
}
|
||||
|
||||
@Test(expected=RuntimeException.class) // SMOD OP mal
|
||||
@Test(expected=StackTooSmallException.class) // SMOD OP mal
|
||||
public void testSMOD_4() {
|
||||
VM vm = new VM();
|
||||
Program program = new Program(Hex.decode("7F000000000000000000000000000000000000000000000000000000000000001E" + // 30
|
||||
|
Loading…
x
Reference in New Issue
Block a user