From 287684aa62b4001983fb1aa022b9894d45ae4289 Mon Sep 17 00:00:00 2001 From: romanman Date: Mon, 7 Jul 2014 11:34:26 +0100 Subject: [PATCH] 2 Bugs: + VM CODECOPY wrong param + Apply tx run results - different when init code and body code --- .../org/ethereum/manager/WorldManager.java | 38 ++++++++++--------- .../src/main/java/org/ethereum/vm/VM.java | 3 +- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java b/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java index 41b37a1a..5f77f4d7 100644 --- a/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java +++ b/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java @@ -173,7 +173,6 @@ public class WorldManager { if (tx.getValue() != null) { BigInteger senderBalance = repository.getBalance(senderAddress); - BigInteger contractBalance = repository.getBalance(contractAddress); if (senderBalance.compareTo(new BigInteger(1, tx.getValue())) >= 0) { @@ -210,7 +209,7 @@ public class WorldManager { vm.play(program); ProgramResult result = program.getResult(); applyProgramResult(result, gasDebit, trackRepository, - senderAddress, tx.getContractAddress(), coinbase); + senderAddress, tx.getContractAddress(), coinbase, true); } else { @@ -231,7 +230,7 @@ public class WorldManager { vm.play(program); ProgramResult result = program.getResult(); applyProgramResult(result, gasDebit, trackRepository, - senderAddress, tx.getReceiveAddress(), coinbase); + senderAddress, tx.getReceiveAddress(), coinbase,false); } } } catch (RuntimeException e) { @@ -253,7 +252,7 @@ public class WorldManager { */ private void applyProgramResult(ProgramResult result, BigInteger gasDebit, Repository repository, byte[] senderAddress, - byte[] contractAddress, byte[] coinbase) { + byte[] contractAddress, byte[] coinbase, boolean initResults) { if (result.getException() != null && result.getException() instanceof Program.OutOfGasException) { @@ -263,11 +262,6 @@ public class WorldManager { throw result.getException(); } - // Save the code created by init - byte[] bodyCode = null; - if (result.getHReturn() != null) { - bodyCode = result.getHReturn().array(); - } BigInteger gasPrice = BigInteger.valueOf(blockchain.getGasPrice()); BigInteger refund = gasDebit.subtract(BigInteger.valueOf( @@ -285,14 +279,24 @@ public class WorldManager { repository.addBalance(coinbase, refund.negate()); } - if (bodyCode != null) { - repository.saveCode(contractAddress, bodyCode); - if (stateLogger.isInfoEnabled()) - stateLogger - .info("saving code of the contract to the db:\n contract={} code={}", - Hex.toHexString(contractAddress), - Hex.toHexString(bodyCode)); - } + + if (initResults){ + + // Save the code created by init + byte[] bodyCode = null; + if (result.getHReturn() != null) { + bodyCode = result.getHReturn().array(); + } + + if (bodyCode != null) { + repository.saveCode(contractAddress, bodyCode); + if (stateLogger.isInfoEnabled()) + stateLogger + .info("saving code of the contract to the db:\n contract={} code={}", + Hex.toHexString(contractAddress), + Hex.toHexString(bodyCode)); + } + } } public void applyBlock(Block block) { diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/VM.java b/ethereumj-core/src/main/java/org/ethereum/vm/VM.java index 23839741..8021df3a 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/VM.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/VM.java @@ -349,7 +349,6 @@ public class VM { int length = lengthData.value().intValue(); int codeOffset = codeOffsetData.value().intValue(); - int memOffset = memOffsetData.value().intValue(); if (program.ops.length < length + codeOffset) { program.stop(); @@ -357,7 +356,7 @@ public class VM { } byte[] code = new byte[length]; - System.arraycopy(program.ops, codeOffset, code, memOffset, length); + System.arraycopy(program.ops, codeOffset, code, 0, length); program.memorySave(memOffsetData.getData(), code); program.step();