From bcb934bed7dcb336a5473814e5d5422673a5bdd2 Mon Sep 17 00:00:00 2001 From: nicksavers Date: Wed, 28 May 2014 15:01:17 +0200 Subject: [PATCH 1/4] Fix warning for unchecked operation in TransactionExecutor --- .../org/ethereum/net/submit/TransactionExecutor.java | 4 +++- .../java/org/ethereum/net/submit/TransactionTask.java | 9 ++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/net/submit/TransactionExecutor.java b/ethereumj-core/src/main/java/org/ethereum/net/submit/TransactionExecutor.java index 8d3c2338..127b8fc4 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/submit/TransactionExecutor.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/submit/TransactionExecutor.java @@ -4,6 +4,8 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import org.ethereum.core.Transaction; + /** * www.ethereumJ.com * User: Roman Mandeleil @@ -17,7 +19,7 @@ public class TransactionExecutor { ExecutorService executor = Executors.newFixedThreadPool(1); - public Future submitTransaction(TransactionTask task){ + public Future submitTransaction(TransactionTask task){ return executor.submit(task); } diff --git a/ethereumj-core/src/main/java/org/ethereum/net/submit/TransactionTask.java b/ethereumj-core/src/main/java/org/ethereum/net/submit/TransactionTask.java index 44c70c85..0cf9208c 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/submit/TransactionTask.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/submit/TransactionTask.java @@ -15,21 +15,19 @@ import static java.lang.Thread.sleep; * User: Roman Mandeleil * Created on: 23/05/2014 18:33 */ - -public class TransactionTask implements Callable { +public class TransactionTask implements Callable { Logger logger = LoggerFactory.getLogger("TransactionTask"); Transaction tx; boolean obsolete = false; - public TransactionTask(Transaction tx) { this.tx = tx; } @Override - public Object call() throws Exception { + public Transaction call() throws Exception { try { logger.info("call() tx: {}", tx.toString()); @@ -39,10 +37,7 @@ public class TransactionTask implements Callable { PendingTransaction pendingTransaction = MainData.instance.addPendingTransaction(tx); peer.sendTransaction(tx); - int i = 0; while(pendingTransaction.getApproved() < 1 ){ - - ++i; sleep(10); } From e768d03ca1986f542c12b1951154c2cc6da0afa6 Mon Sep 17 00:00:00 2001 From: nicksavers Date: Wed, 28 May 2014 15:37:18 +0200 Subject: [PATCH 2/4] Clean and update documentation Transaction class --- .../java/org/ethereum/core/Transaction.java | 53 ++++++++----------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/core/Transaction.java b/ethereumj-core/src/main/java/org/ethereum/core/Transaction.java index 296c3006..c2312076 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Transaction.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Transaction.java @@ -27,17 +27,9 @@ public class Transaction { Logger logger = LoggerFactory.getLogger(this.getClass()); - private static final int CALL_SIZE = 9; - private static final int CONTRACT_SIZE = 10; - public static final byte[] ZERO_ADDRESS = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + public static final byte[] ZERO_ADDRESS = new byte[20]; - /* creation contract tx - * [ nonce, endowment, 0000000000000000, gasPrice, gasDeposit data, signature(v, r, s) ] - * or simple send tx - * [ nonce, value, receiveAddress, gasPrice, gasDeposit, data, signature(v, r, s) ] - */ - - /* SHA3 hash of the rlpEncoded transaction */ + /* SHA3 hash of the RLP encoded transaction */ private byte[] hash; /* a counter used to make sure each transaction can only be processed once */ @@ -61,8 +53,8 @@ public class Transaction { private byte[] gasLimit; /* An unlimited size byte array specifying - * input [data] of the message call */ - /* Initialisation code for a new contract */ + * input [data] of the message call or + * Initialization code for a new contract */ private byte[] data; /* the elliptic curve signature @@ -73,7 +65,7 @@ public class Transaction { private byte[] rlpEncoded; private byte[] rlpRaw; /* Indicates if this transaction has been parsed - * from the rlp-encoded data */ + * from the RLP-encoded data */ private boolean parsed = false; public Transaction(byte[] rawData) { @@ -81,6 +73,11 @@ public class Transaction { parsed = false; } + /* creation contract tx + * [ nonce, gasPrice, gasLimit, 0000000000000000, endowment, init, signature(v, r, s) ] + * or simple send tx + * [ nonce, gasPrice, gasLimit, receiveAddress, value, data, signature(v, r, s) ] + */ public Transaction(byte[] nonce, byte[] gasPrice, byte[] gasLimit, byte[] receiveAddress, byte[] value, byte[] data) { this.nonce = nonce; this.gasPrice = gasPrice; @@ -95,7 +92,7 @@ public class Transaction { parsed = true; } - public void rlpParse(){ + public void rlpParse() { RLPList decodedTxList = RLP.decode2(rlpEncoded); RLPList transaction = (RLPList) decodedTxList.get(0); @@ -106,7 +103,6 @@ public class Transaction { this.receiveAddress = ((RLPItem) transaction.get(3)).getRLPData(); this.value = ((RLPItem) transaction.get(4)).getRLPData(); - this.data = ((RLPItem) transaction.get(5)).getRLPData(); // only parse signature in case tx is signed if(((RLPItem) transaction.get(6)).getRLPData() != null) { @@ -117,7 +113,6 @@ public class Transaction { } else { logger.debug("RLP encoded tx is not signed!"); } - this.parsed = true; this.hash = this.getHash(); } @@ -127,7 +122,6 @@ public class Transaction { } public byte[] getHash() { - if (!parsed) rlpParse(); byte[] plainMsg = this.getEncodedRaw(); return HashUtil.sha3(plainMsg); @@ -177,11 +171,9 @@ public class Transaction { if (!isContract()) return null; - byte[] val1 = RLP.encodeElement(getSender()); - byte[] val2 = RLP.encodeElement(nonce); - byte[] val = HashUtil.sha3omit12(RLP.encodeList(val1, val2)); - - return val; + byte[] encSender = RLP.encodeElement(getSender()); + byte[] encNonce = RLP.encodeElement(nonce); + return HashUtil.sha3omit12(RLP.encodeList(encSender, encNonce)); } public boolean isContract() { @@ -231,8 +223,8 @@ public class Transaction { } /** - * For signature games you have to keep also - * rlp of the transaction without any signature data + * For signatures you have to keep also + * RLP of the transaction without any signature data */ public byte[] getEncodedRaw(){ @@ -244,10 +236,10 @@ public class Transaction { byte[] gasLimit = RLP.encodeElement(this.gasLimit); byte[] receiveAddress = RLP.encodeElement(this.receiveAddress); byte[] value = RLP.encodeElement(this.value); - byte[] init = RLP.encodeElement(this.data); + byte[] data = RLP.encodeElement(this.data); - this.rlpRaw = RLP.encodeList(nonce, gasPrice, gasLimit, receiveAddress, value, - init); + this.rlpRaw = RLP.encodeList(nonce, gasPrice, gasLimit, receiveAddress, + value, data); return rlpRaw; } @@ -260,7 +252,7 @@ public class Transaction { byte[] gasLimit = RLP.encodeElement(this.gasLimit); byte[] receiveAddress = RLP.encodeElement(this.receiveAddress); byte[] value = RLP.encodeElement(this.value); - byte[] init = RLP.encodeElement(this.data); + byte[] data = RLP.encodeElement(this.data); byte[] v, r, s; @@ -274,9 +266,8 @@ public class Transaction { s = RLP.encodeElement(new byte[0]); } - this.rlpEncoded = RLP.encodeList(nonce, gasPrice, gasLimit, receiveAddress, value, - init, v, r, s); - + this.rlpEncoded = RLP.encodeList(nonce, gasPrice, gasLimit, + receiveAddress, value, data, v, r, s); return rlpEncoded; } From df38d1b6b3b7205dd9c3e03c81e7420a137835ef Mon Sep 17 00:00:00 2001 From: nicksavers Date: Wed, 28 May 2014 15:43:37 +0200 Subject: [PATCH 3/4] Write description for build file and changed to lowercase filename --- build-post-package.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/build-post-package.xml b/build-post-package.xml index 5bc457ba..38c7355e 100644 --- a/build-post-package.xml +++ b/build-post-package.xml @@ -1,7 +1,7 @@ - + - simple example build file + Build file to package the EthereumJ client and dependencies into a zip-file @@ -38,7 +38,7 @@ - + @@ -48,4 +48,4 @@ - \ No newline at end of file + From cb5d611b1020a78d7deb1e3a41ef46e19cdd39d2 Mon Sep 17 00:00:00 2001 From: nicksavers Date: Wed, 28 May 2014 15:57:26 +0200 Subject: [PATCH 4/4] Various line/import cleanup and move AddressState to core-package --- .../ethereum/{wallet => core}/AddressState.java | 8 +------- .../src/main/java/org/ethereum/core/Wallet.java | 10 +++------- .../java/org/ethereum/gui/ContractCallDialog.java | 3 ++- .../org/ethereum/gui/ContractSubmitDialog.java | 3 ++- .../main/java/org/ethereum/gui/PayOutDialog.java | 14 +------------- .../java/org/ethereum/gui/WalletAddressPanel.java | 3 ++- .../main/java/org/ethereum/gui/WalletWindow.java | 3 ++- .../main/java/org/ethereum/manager/MainData.java | 4 ++-- .../org/ethereum/net/message/StaticMessages.java | 2 -- .../src/main/java/org/ethereum/util/Utils.java | 8 +++++++- .../test/java/org/ethereum/core/WalletTest.java | 2 +- 11 files changed, 23 insertions(+), 37 deletions(-) rename ethereumj-core/src/main/java/org/ethereum/{wallet => core}/AddressState.java (96%) diff --git a/ethereumj-core/src/main/java/org/ethereum/wallet/AddressState.java b/ethereumj-core/src/main/java/org/ethereum/core/AddressState.java similarity index 96% rename from ethereumj-core/src/main/java/org/ethereum/wallet/AddressState.java rename to ethereumj-core/src/main/java/org/ethereum/core/AddressState.java index 30a06e4d..e73b1f06 100644 --- a/ethereumj-core/src/main/java/org/ethereum/wallet/AddressState.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/AddressState.java @@ -1,4 +1,4 @@ -package org.ethereum.wallet; +package org.ethereum.core; import org.ethereum.crypto.ECKey; import org.ethereum.util.Utils; @@ -10,23 +10,19 @@ import java.math.BigInteger; * User: Roman Mandeleil * Created on: 21/05/2014 10:43 */ - public class AddressState { private ECKey ecKey; private BigInteger nonce; private BigInteger balance; - public AddressState() { - ecKey = new ECKey(Utils.getRandom()); nonce = BigInteger.ZERO; balance = BigInteger.ZERO; } public AddressState(ECKey ecKey) { - this(); this.ecKey = ecKey; } @@ -56,6 +52,4 @@ public class AddressState { public void addToBalance(BigInteger value){ balance = balance.add(value); } - - } diff --git a/ethereumj-core/src/main/java/org/ethereum/core/Wallet.java b/ethereumj-core/src/main/java/org/ethereum/core/Wallet.java index 21931417..7a514384 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Wallet.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Wallet.java @@ -1,7 +1,6 @@ package org.ethereum.core; import org.ethereum.crypto.ECKey; -import org.ethereum.wallet.AddressState; import org.spongycastle.util.encoders.Hex; import org.w3c.dom.*; import org.xml.sax.SAXException; @@ -14,6 +13,7 @@ import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; + import java.io.File; import java.io.IOException; import java.math.BigInteger; @@ -71,23 +71,19 @@ public class Wallet { return rows.get(address); } - public BigInteger getBalance(byte[] addressBytes){ String address = Hex.toHexString(addressBytes); return rows.get(address).getBalance(); } public BigInteger totalBalance(){ - BigInteger sum = BigInteger.ZERO; - for (AddressState addressState : rows.values()){ sum = sum.add(addressState.getBalance()); } return sum; } - public void applyTransaction(Transaction transaction){ transactionMap.put(new BigInteger(transaction.getHash()), transaction ); @@ -140,7 +136,7 @@ public class Wallet { */ public void load() throws IOException, SAXException, ParserConfigurationException { - /** + /** @@ -154,7 +150,7 @@ public class Wallet { 900099909 - + */ String dir = System.getProperty("user.dir"); diff --git a/ethereumj-core/src/main/java/org/ethereum/gui/ContractCallDialog.java b/ethereumj-core/src/main/java/org/ethereum/gui/ContractCallDialog.java index c4dbb1bb..a589a9dd 100644 --- a/ethereumj-core/src/main/java/org/ethereum/gui/ContractCallDialog.java +++ b/ethereumj-core/src/main/java/org/ethereum/gui/ContractCallDialog.java @@ -1,11 +1,11 @@ package org.ethereum.gui; +import org.ethereum.core.AddressState; import org.ethereum.core.Transaction; import org.ethereum.manager.MainData; import org.ethereum.net.client.ClientPeer; import org.ethereum.util.ByteUtil; import org.ethereum.util.Utils; -import org.ethereum.wallet.AddressState; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spongycastle.util.BigIntegers; @@ -16,6 +16,7 @@ import javax.swing.border.Border; import javax.swing.plaf.ComboBoxUI; import javax.swing.plaf.basic.BasicComboBoxUI; import javax.swing.plaf.basic.BasicComboPopup; + import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; diff --git a/ethereumj-core/src/main/java/org/ethereum/gui/ContractSubmitDialog.java b/ethereumj-core/src/main/java/org/ethereum/gui/ContractSubmitDialog.java index 1444a7b7..c634e104 100644 --- a/ethereumj-core/src/main/java/org/ethereum/gui/ContractSubmitDialog.java +++ b/ethereumj-core/src/main/java/org/ethereum/gui/ContractSubmitDialog.java @@ -1,10 +1,10 @@ package org.ethereum.gui; +import org.ethereum.core.AddressState; import org.ethereum.core.Transaction; import org.ethereum.manager.MainData; import org.ethereum.net.client.ClientPeer; import org.ethereum.util.Utils; -import org.ethereum.wallet.AddressState; import org.spongycastle.util.BigIntegers; import org.spongycastle.util.encoders.Hex; @@ -13,6 +13,7 @@ import javax.swing.border.Border; import javax.swing.plaf.ComboBoxUI; import javax.swing.plaf.basic.BasicComboBoxUI; import javax.swing.plaf.basic.BasicComboPopup; + import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; diff --git a/ethereumj-core/src/main/java/org/ethereum/gui/PayOutDialog.java b/ethereumj-core/src/main/java/org/ethereum/gui/PayOutDialog.java index a83d651c..c0e3bd1b 100644 --- a/ethereumj-core/src/main/java/org/ethereum/gui/PayOutDialog.java +++ b/ethereumj-core/src/main/java/org/ethereum/gui/PayOutDialog.java @@ -1,12 +1,9 @@ package org.ethereum.gui; -import com.google.common.primitives.Longs; +import org.ethereum.core.AddressState; import org.ethereum.core.Transaction; import org.ethereum.manager.MainData; import org.ethereum.net.client.ClientPeer; -import org.ethereum.net.submit.TransactionExecutor; -import org.ethereum.net.submit.TransactionTask; -import org.ethereum.wallet.AddressState; import org.spongycastle.util.BigIntegers; import org.spongycastle.util.encoders.Hex; @@ -16,16 +13,10 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.math.BigInteger; import java.net.URL; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import java.util.regex.Pattern; import javax.swing.*; -import static org.ethereum.config.SystemProperties.CONFIG; - /** * www.ethereumJ.com * User: Roman Mandeleil @@ -210,8 +201,6 @@ class PayOutDialog extends JDialog implements MessageAwareDialog{ alertStatusMsg("The address can't afford this transaction"); return false; } - - return true; } @@ -270,7 +259,6 @@ class PayOutDialog extends JDialog implements MessageAwareDialog{ }); } - public static void main(String args[]) { AddressState as = new AddressState(); PayOutDialog pod = new PayOutDialog(null, as); diff --git a/ethereumj-core/src/main/java/org/ethereum/gui/WalletAddressPanel.java b/ethereumj-core/src/main/java/org/ethereum/gui/WalletAddressPanel.java index 65d4f756..50c671ba 100644 --- a/ethereumj-core/src/main/java/org/ethereum/gui/WalletAddressPanel.java +++ b/ethereumj-core/src/main/java/org/ethereum/gui/WalletAddressPanel.java @@ -1,13 +1,14 @@ package org.ethereum.gui; +import org.ethereum.core.AddressState; import org.ethereum.util.Utils; -import org.ethereum.wallet.AddressState; import org.spongycastle.util.encoders.Hex; import javax.swing.*; import javax.swing.border.Border; import javax.swing.border.CompoundBorder; import javax.swing.border.EmptyBorder; + import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; diff --git a/ethereumj-core/src/main/java/org/ethereum/gui/WalletWindow.java b/ethereumj-core/src/main/java/org/ethereum/gui/WalletWindow.java index de10a955..5499478e 100644 --- a/ethereumj-core/src/main/java/org/ethereum/gui/WalletWindow.java +++ b/ethereumj-core/src/main/java/org/ethereum/gui/WalletWindow.java @@ -1,10 +1,11 @@ package org.ethereum.gui; +import org.ethereum.core.AddressState; import org.ethereum.core.Wallet; import org.ethereum.manager.MainData; -import org.ethereum.wallet.AddressState; import javax.swing.*; + import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; diff --git a/ethereumj-core/src/main/java/org/ethereum/manager/MainData.java b/ethereumj-core/src/main/java/org/ethereum/manager/MainData.java index 2e84fc19..f135f509 100644 --- a/ethereumj-core/src/main/java/org/ethereum/manager/MainData.java +++ b/ethereumj-core/src/main/java/org/ethereum/manager/MainData.java @@ -6,6 +6,8 @@ import java.net.UnknownHostException; import java.util.*; import com.maxmind.geoip.Location; + +import org.ethereum.core.AddressState; import org.ethereum.core.Block; import org.ethereum.core.Transaction; import org.ethereum.core.Wallet; @@ -17,7 +19,6 @@ import org.ethereum.net.client.PeerData; import org.ethereum.net.message.StaticMessages; import org.ethereum.net.peerdiscovery.PeerDiscovery; import org.ethereum.net.submit.PendingTransaction; -import org.ethereum.wallet.AddressState; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spongycastle.util.encoders.Hex; @@ -150,7 +151,6 @@ public class MainData { * 2) the dialog send the transaction to a net * 3) wherever the transaction got for the wire in will change to approve state * 4) only after the approve a) Wallet state changes - * * 5) After the block is received with that tx the pending been clean up */ public PendingTransaction addPendingTransaction(Transaction transaction) { diff --git a/ethereumj-core/src/main/java/org/ethereum/net/message/StaticMessages.java b/ethereumj-core/src/main/java/org/ethereum/net/message/StaticMessages.java index 45b44050..118d4be6 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/message/StaticMessages.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/message/StaticMessages.java @@ -4,8 +4,6 @@ import org.ethereum.core.Genesis; import org.ethereum.crypto.HashUtil; import org.spongycastle.util.encoders.Hex; -import static org.ethereum.config.SystemProperties.CONFIG; - /** * www.ethereumJ.com * User: Roman Mandeleil diff --git a/ethereumj-core/src/main/java/org/ethereum/util/Utils.java b/ethereumj-core/src/main/java/org/ethereum/util/Utils.java index 88c6feb3..5d32247e 100644 --- a/ethereumj-core/src/main/java/org/ethereum/util/Utils.java +++ b/ethereumj-core/src/main/java/org/ethereum/util/Utils.java @@ -29,6 +29,13 @@ public class Utils { return (new BigInteger(1, numberBytes)).toString(); } + /** + * Return formatted Date String: yyyy.MM.dd HH:mm:ss + * Based on Unix's time() input in seconds + * + * @param timestamp seconds since start of Unix-time + * @return String formatted as - yyyy.MM.dd HH:mm:ss + */ public static String longToDateTime(long timestamp) { Date date = new Date(timestamp * 1000); DateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); @@ -43,7 +50,6 @@ public class Utils { static BigInteger _1000_ = new BigInteger("1000"); public static String getValueShortString(BigInteger number){ - BigInteger result = number; int pow = 0; while (result.compareTo(_1000_) == 1 || result.compareTo(_1000_) == 0){ diff --git a/ethereumj-core/src/test/java/org/ethereum/core/WalletTest.java b/ethereumj-core/src/test/java/org/ethereum/core/WalletTest.java index 1ba7319a..a86fc66a 100644 --- a/ethereumj-core/src/test/java/org/ethereum/core/WalletTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/core/WalletTest.java @@ -2,12 +2,12 @@ package org.ethereum.core; import org.ethereum.crypto.ECKey; import org.ethereum.crypto.HashUtil; -import org.ethereum.wallet.AddressState; import org.junit.Test; import org.xml.sax.SAXException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; + import java.io.IOException; import java.math.BigInteger;