From 8b545d66206d977662a003b7911b0a3ba9a7b8e7 Mon Sep 17 00:00:00 2001 From: romanman Date: Sat, 7 Jun 2014 18:04:06 +0100 Subject: [PATCH] some minor changes and fixes --- .../java/org/ethereum/core/Blockchain.java | 2 +- .../org/ethereum/manager/WorldManager.java | 37 ++++++++++++++----- .../net/client/EthereumProtocolHandler.java | 2 + .../java/org/ethereum/vm/ProgramResult.java | 27 ++++++++++++++ .../src/main/java/org/ethereum/vm/VM.java | 7 ++++ .../src/main/resources/log4j.properties | 5 ++- 6 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 ethereumj-core/src/main/java/org/ethereum/vm/ProgramResult.java diff --git a/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java b/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java index 9ba69c9a..11675bdc 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java @@ -71,7 +71,7 @@ public class Blockchain extends ArrayList { db.put(block.getParentHash(), block.getEncoded()); if (logger.isDebugEnabled()) logger.debug("block added to the chain with hash: {}", Hex.toHexString(block.getHash())); - } + } // Remove all wallet transactions as they already approved by the net for (Block block : blocks) { for (Transaction tx : block.getTransactionsList()) { 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 7e0aa372..b4cf6666 100644 --- a/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java +++ b/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java @@ -1,12 +1,14 @@ package org.ethereum.manager; import org.ethereum.core.AccountState; +import org.ethereum.core.Block; import org.ethereum.core.Transaction; import org.ethereum.db.Database; import org.ethereum.trie.Trie; +import org.ethereum.vm.Program; +import org.ethereum.vm.VM; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.spongycastle.util.Arrays; import org.spongycastle.util.encoders.Hex; import java.math.BigInteger; @@ -24,6 +26,7 @@ import java.util.Map; public class WorldManager { Logger logger = LoggerFactory.getLogger("main"); + Logger stateLogger = LoggerFactory.getLogger("state"); public static WorldManager instance = new WorldManager(); @@ -45,17 +48,17 @@ public class WorldManager { byte[] senderAddress = tx.getSender(); byte[] stateData = allAccountsState.get(senderAddress); - if (stateData == null) { - if (logger.isWarnEnabled()) - logger.warn("No such address: {}", Hex.toHexString(senderAddress)); + if (stateData == null || stateData.length == 0) { + if (stateLogger.isWarnEnabled()) + stateLogger.warn("No such address: {}", Hex.toHexString(senderAddress)); return; } AccountState senderState = new AccountState(stateData); if (senderState.getNonce().compareTo(new BigInteger(tx.getNonce())) != 0){ - if (logger.isWarnEnabled()) - logger.warn("Invalid nonce account.nonce={} tx.nonce={}", + if (stateLogger.isWarnEnabled()) + stateLogger.warn("Invalid nonce account.nonce={} tx.nonce={}", senderState.getNonce(), new BigInteger(tx.getNonce())); return; @@ -67,6 +70,9 @@ public class WorldManager { // todo 0. run the init method + VM vm = new VM(); + Program program = new Program(null, null); + } else{ @@ -74,12 +80,15 @@ public class WorldManager { byte[] accountData = this.allAccountsState.get(tx.getReceiveAddress()); if (accountData.length == 0){ - if (logger.isInfoEnabled()) - logger.info("New account created address={}", - Hex.toHexString(tx.getReceiveAddress())); recieverState = new AccountState(tx.getKey()); + if (stateLogger.isInfoEnabled()) + stateLogger.info("New account created address={}", + Hex.toHexString(tx.getReceiveAddress())); } else { recieverState = new AccountState(accountData); + if (stateLogger.isInfoEnabled()) + stateLogger.info("Account updated address={}", + Hex.toHexString(tx.getReceiveAddress())); } // APPLY THE BALANCE VALUE @@ -108,8 +117,16 @@ public class WorldManager { } } - public void applyBlock(){ + public void applyBlock(Block block){ + List txList = block.getTransactionsList(); + applyTransactionList(txList); + } + + public void applyBlockList(List blocks){ + for (int i = blocks.size() - 1; i >= 0 ; --i){ + applyBlock(blocks.get(i)); + } } diff --git a/ethereumj-core/src/main/java/org/ethereum/net/client/EthereumProtocolHandler.java b/ethereumj-core/src/main/java/org/ethereum/net/client/EthereumProtocolHandler.java index b82f5939..73c92346 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/client/EthereumProtocolHandler.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/client/EthereumProtocolHandler.java @@ -256,6 +256,8 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter { } MainData.instance.getBlockchain().addBlocks(blockList); + WorldManager.instance.applyBlockList(blockList); + logger.info(blocksMessage.toString()); if (peerListener != null) peerListener.console(blocksMessage.toString()); } diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/ProgramResult.java b/ethereumj-core/src/main/java/org/ethereum/vm/ProgramResult.java new file mode 100644 index 00000000..26be9660 --- /dev/null +++ b/ethereumj-core/src/main/java/org/ethereum/vm/ProgramResult.java @@ -0,0 +1,27 @@ +package org.ethereum.vm; + +import java.nio.ByteBuffer; + +/** + * www.ethereumJ.com + * User: Roman Mandeleil + * Created on: 07/06/2014 17:45 + */ + +public class ProgramResult { + + private int gasUsed = 0; + private ByteBuffer hReturn = null; + + public void spendGas(int gas){ + gasUsed += gas; + } + + public void setHReturn(byte[] hReturn){ + + this.hReturn = ByteBuffer.allocate(hReturn.length); + this.hReturn.put(hReturn); + } + + +} 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 b95177db..93e3d04b 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/VM.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/VM.java @@ -583,4 +583,11 @@ public class VM { } + + public void play(Program program){ + while(!program.isStopped()) + this.step(program); + + } + } diff --git a/ethereumj-core/src/main/resources/log4j.properties b/ethereumj-core/src/main/resources/log4j.properties index 44e22534..803316b7 100644 --- a/ethereumj-core/src/main/resources/log4j.properties +++ b/ethereumj-core/src/main/resources/log4j.properties @@ -8,7 +8,10 @@ log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern= %d{HH:mm:ss} [%c{1}] %m%n # filter noisy classes +log4j.logger.org.ethereum.net = FATAL log4j.logger.org.ethereum.net.peerdiscovery = WARN log4j.logger.java.nio = WARN log4j.logger.io.netty = FATAL -log4j.logger.org.ethereum.vm = DEBUG \ No newline at end of file +log4j.logger.org.ethereum.vm = DEBUG +log4j.logger.org.ethereum.core = FATAL +log4j.logger.wire = FATAL