Re-use static variable for Word with EMPTY_BYTE_ARRAY

This commit is contained in:
nicksavers 2014-08-02 11:35:37 +02:00
parent be0f70575b
commit d8063c5ff3
5 changed files with 155 additions and 154 deletions

View File

@ -18,12 +18,12 @@ import java.nio.ByteBuffer;
*/
public class DataWord implements Comparable<DataWord> {
public static final DataWord ZERO = new DataWord(new byte[32]); // don't push it in to the stack
public static final DataWord ZERO = new DataWord(new byte[32]); // don't push it in to the stack
public static final DataWord ZERO_EMPTY_ARRAY = new DataWord(new byte[0]); // don't push it in to the stack
byte[] data = new byte[32];
private byte[] data = new byte[32];
public DataWord() {
this.data = new byte[32];
}
public DataWord(int num) {

View File

@ -3,6 +3,7 @@ package org.ethereum.vm;
import org.ethereum.crypto.HashUtil;
import org.ethereum.db.ContractDetails;
import org.ethereum.db.Repository;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -25,7 +26,7 @@ public class Program {
private int invokeHash;
private ProgramListener listener;
Stack<DataWord> stack = new Stack<DataWord>();
Stack<DataWord> stack = new Stack<>();
ByteBuffer memory = null;
DataWord programAddress;
@ -147,7 +148,7 @@ public class Program {
}
public void memorySave(DataWord addrB, DataWord value) {
memorySave(addrB.data, value.data);
memorySave(addrB.getData(), value.getData());
}
public void memorySave(byte[] addr, byte[] value) {
@ -160,7 +161,7 @@ public class Program {
public DataWord memoryLoad(DataWord addr) {
int address = new BigInteger(1, addr.getData()).intValue();
allocateMemory(address, DataWord.ZERO.data);
allocateMemory(address, DataWord.ZERO.getData());
byte[] data = new byte[32];
System.arraycopy(memory.array(), address, data , 0 ,32);
@ -465,12 +466,12 @@ public class Program {
}
public DataWord getOwnerAddress() {
if (invokeData == null) return new DataWord( new byte[0]);
if (invokeData == null) return DataWord.ZERO_EMPTY_ARRAY;
return this.programAddress.clone();
}
public DataWord getBalance(DataWord address) {
if (invokeData == null) return new DataWord( new byte[0]);
if (invokeData == null) return DataWord.ZERO_EMPTY_ARRAY;
BigInteger balance = result.getRepository().getBalance(address.getLast20Bytes());
DataWord balanceData = new DataWord(balance.toByteArray());
@ -479,43 +480,43 @@ public class Program {
}
public DataWord getOriginAddress() {
if (invokeData == null) return new DataWord( new byte[0]);
if (invokeData == null) return DataWord.ZERO_EMPTY_ARRAY;
return invokeData.getOriginAddress().clone();
}
public DataWord getCallerAddress() {
if (invokeData == null) return new DataWord( new byte[0]);
if (invokeData == null) return DataWord.ZERO_EMPTY_ARRAY;
return invokeData.getCallerAddress().clone();
}
public DataWord getGasPrice() {
if (invokeData == null) return new DataWord( new byte[0]);
if (invokeData == null) return DataWord.ZERO_EMPTY_ARRAY;
return invokeData.getMinGasPrice().clone();
}
public DataWord getGas() {
if (invokeData == null) return new DataWord( new byte[0]);
if (invokeData == null) return DataWord.ZERO_EMPTY_ARRAY;
long afterSpend = invokeData.getGas().longValue() - result.getGasUsed();
return new DataWord(afterSpend);
}
public DataWord getCallValue() {
if (invokeData == null) return new DataWord( new byte[0]);
if (invokeData == null) return DataWord.ZERO_EMPTY_ARRAY;
return invokeData.getCallValue().clone();
}
public DataWord getDataSize() {
if (invokeData == null) return new DataWord( new byte[0]);
if (invokeData == null) return DataWord.ZERO_EMPTY_ARRAY;
return invokeData.getDataSize().clone();
}
public DataWord getDataValue(DataWord index) {
if (invokeData == null) return new DataWord( new byte[0]);
if (invokeData == null) return DataWord.ZERO_EMPTY_ARRAY;
return invokeData.getDataValue(index);
}
public byte[] getDataCopy(DataWord offset, DataWord length) {
if (invokeData == null) return new byte[0];
if (invokeData == null) return ByteUtil.EMPTY_BYTE_ARRAY;
return invokeData.getDataCopy(offset, length);
}

View File

@ -449,7 +449,7 @@ public class VM {
if (logger.isInfoEnabled())
hint = "data: " + Hex.toHexString(msgData);
program.memorySave(memOffsetData.data, msgData);
program.memorySave(memOffsetData.getData(), msgData);
program.step();
} break;
case CODESIZE:{
@ -759,7 +759,7 @@ public class VM {
if (memoryUsage > 0)
program.spendGas(GasCost.MEMORY * memoryUsage, OpCode.code(op).name() + " (memory usage)");
program.fullTrace();
// program.fullTrace();
} catch (RuntimeException e) {
program.stop();
if(e instanceof OutOfGasException)

View File

@ -44,11 +44,11 @@ public class DataWordTest {
DataWord x = new DataWord(two);
x.add(new DataWord(two));
System.out.println(Hex.toHexString(x.data));
System.out.println(Hex.toHexString(x.getData()));
DataWord y = new DataWord(two);
y.add2(new DataWord(two));
System.out.println(Hex.toHexString(y.data));
System.out.println(Hex.toHexString(y.getData()));
}
@Test
@ -60,13 +60,13 @@ public class DataWordTest {
DataWord x = new DataWord(three);
x.add(new DataWord(three));
assertEquals(32, x.data.length);
System.out.println(Hex.toHexString(x.data));
assertEquals(32, x.getData().length);
System.out.println(Hex.toHexString(x.getData()));
// FAIL
// DataWord y = new DataWord(three);
// y.add2(new DataWord(three));
// System.out.println(Hex.toHexString(y.data));
// System.out.println(Hex.toHexString(y.getData()));
}
@Test
@ -85,7 +85,7 @@ public class DataWordTest {
DataWord x = new DataWord(one);// System.out.println(x.value());
DataWord y = new DataWord(two);// System.out.println(y.value());
y.mod(x);
assertEquals(32, y.data.length);
assertEquals(expected, Hex.toHexString(y.data));
assertEquals(32, y.getData().length);
assertEquals(expected, Hex.toHexString(y.getData()));
}
}

File diff suppressed because it is too large Load Diff