Fix for last commit

This commit is contained in:
romanman 2014-07-14 10:27:58 +03:00
parent aa0dab8d18
commit db0e135278
5 changed files with 55 additions and 20 deletions

View File

@ -6,6 +6,7 @@ import org.ethereum.crypto.HashUtil;
import org.ethereum.json.JSONHelper;
import org.ethereum.trie.TrackTrie;
import org.ethereum.trie.Trie;
import org.ethereum.util.ByteUtil;
import org.ethereum.vm.DataWord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -95,12 +96,8 @@ public class Repository {
public AccountState createAccount(byte[] addr) {
if (addr.length < 20) {
byte[] newAddr = new byte[20];
System.arraycopy(addr, 0, newAddr, newAddr.length - addr.length, addr.length);
addr = newAddr;
}
addr = ByteUtil.padAddressWithZeroes(addr);
// 1. Save AccountState
AccountState state = new AccountState();
accountStateDB.update(addr, state.getEncoded());
@ -121,6 +118,8 @@ public class Repository {
public AccountState getAccountState(byte[] addr) {
addr = ByteUtil.padAddressWithZeroes(addr);
byte[] accountStateRLP = accountStateDB.get(addr);
if (accountStateRLP.length == 0) {
@ -134,6 +133,8 @@ public class Repository {
public ContractDetails getContractDetails(byte[] addr) {
addr = ByteUtil.padAddressWithZeroes(addr);
if (logger.isInfoEnabled())
logger.info("Account: [ {} ]", Hex.toHexString(addr));
@ -153,6 +154,8 @@ public class Repository {
public BigInteger addBalance(byte[] address, BigInteger value) {
address = ByteUtil.padAddressWithZeroes(address);
AccountState state = getAccountState(address);
if (state == null){
@ -162,8 +165,8 @@ public class Repository {
BigInteger newBalance = state.addToBalance(value);
if (logger.isInfoEnabled())
logger.info("Changing balance: account: [ {} ] new balance: [ {} ]",
Hex.toHexString(address), newBalance.toString());
logger.info("Changing balance: account: [ {} ] new balance: [ {} ] delta: [ {} ]",
Hex.toHexString(address), newBalance.toString(), value);
accountStateDB.update(address, state.getEncoded());
return newBalance;
@ -198,6 +201,8 @@ public class Repository {
public void addStorageRow(byte[] address, DataWord key, DataWord value) {
if (address == null || key == null) return;
address = ByteUtil.padAddressWithZeroes(address);
AccountState state = getAccountState(address);
ContractDetails details = getContractDetails(address);
@ -220,7 +225,9 @@ public class Repository {
public DataWord getStorageValue(byte[] address, DataWord key) {
if (key == null) return null;
if (key == null || address == null) return null;
address = ByteUtil.padAddressWithZeroes(address);
AccountState state = getAccountState(address);
if (state == null) return null;
@ -231,6 +238,8 @@ public class Repository {
}
public byte[] getCode(byte[] address) {
address = ByteUtil.padAddressWithZeroes(address);
ContractDetails details = getContractDetails(address);
if (details == null) return null;
return details.getCode();
@ -238,7 +247,8 @@ public class Repository {
public void saveCode(byte[] address, byte[] code) {
if (code == null) return;
if (code == null || address == null) return;
address = ByteUtil.padAddressWithZeroes(address);
if (logger.isDebugEnabled())
logger.debug("saveCode: \n address: [ {} ], \n code: [ {} ]",
@ -274,9 +284,12 @@ public class Repository {
return this.worldState.getRootHash();
}
public void delete(byte[] account){
accountStateDB.delete(account);
contractDetailsDB.delete(account);
public void delete(byte[] address){
if (address == null) return;
address = ByteUtil.padAddressWithZeroes(address);
accountStateDB.delete(address);
contractDetailsDB.delete(address);
}
public List<ByteArrayWrapper> dumpKeys(){
@ -287,14 +300,14 @@ public class Repository {
if (!CONFIG.dumpFull()) return;
if (txHash == null)
if (CONFIG.dumpCleanOnRestart()) {
try {FileUtils.deleteDirectory(CONFIG.dumpDir());} catch (IOException e) {}
}
if (blockNumber == 0 && txNumber == 0)
if (CONFIG.dumpCleanOnRestart()) {
try {FileUtils.deleteDirectory(CONFIG.dumpDir());} catch (IOException e) {}
}
String dir = CONFIG.dumpDir() + "/";
String fileName = "0.dmp";
String fileName = blockNumber + ".dmp";
if (txHash != null)
fileName = String.format("%d_%d_%s.dmp",
blockNumber, txNumber, txHash.substring(0, 8));

View File

@ -65,8 +65,7 @@ public class TestRunner {
Env env = testCase.getEnv();
Exec exec = testCase.getExec();
byte[] address = exec.getAddress();
if(repository.getAccountState(address) == null) { repository.createAccount(address); }
byte[] address = exec.getAddress();
byte[] origin = exec.getOrigin();
byte[] caller = exec.getCaller();
byte[] balance = ByteUtil.bigIntegerToBytes(repository.getBalance(exec.getAddress()));
@ -93,6 +92,8 @@ public class TestRunner {
try {
while(!program.isStopped())
vm.step(program);
System.out.println();
} catch (RuntimeException e) {
program.setRuntimeFailure(e);
}

View File

@ -202,4 +202,17 @@ public class ByteUtil {
// we return false when all bytes are 0 again
return (i >= startIndex || bytes[startIndex] != 0);
}
public static byte[] padAddressWithZeroes(byte[] address){
if (address.length < 20) {
byte[] newAddr = new byte[20];
System.arraycopy(address, 0, newAddr, newAddr.length - address.length, address.length);
address = newAddr;
return address;
}
return address;
}
}

View File

@ -60,6 +60,13 @@ public class DataWord implements Comparable<DataWord> {
return ByteUtil.stripLeadingZeroes(data);
}
public byte[] get20LastBytes(){
byte[] last20bytes = new byte[20];
System.arraycopy(this.data, 12, last20bytes, 0, 20);
return last20bytes;
}
public BigInteger value() {
return new BigInteger(1, data);
}

View File

@ -221,6 +221,7 @@ public class Program {
, balance.longValue());
this.result.getRepository().addBalance(obtainer.getNoLeadZeroesData(), balance.value());
this.result.getRepository().addBalance(getOwnerAddress().getNoLeadZeroesData(), balance.value().negate());
// 2) mark the account as for delete
result.addDeleteAccount(getOwnerAddress());