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

@ -19,11 +19,11 @@ import java.nio.ByteBuffer;
public class DataWord implements Comparable<DataWord> { 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() { public DataWord() {
this.data = new byte[32];
} }
public DataWord(int num) { public DataWord(int num) {

View File

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

View File

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

View File

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

File diff suppressed because it is too large Load Diff