Merge pull request #16 from nicksavers/master

Fix warning for unchecked operation in TransactionExecutor
This commit is contained in:
romanman 2014-05-28 17:00:01 +03:00
commit 1d5584885a
15 changed files with 54 additions and 80 deletions

View File

@ -1,7 +1,7 @@
<project name="enthereumJ-postpackage" default="run" basedir="."> <project name="ethereumj-postpackage" default="run" basedir=".">
<description> <description>
simple example build file Build file to package the EthereumJ client and dependencies into a zip-file
</description> </description>
<!-- set global properties for this build --> <!-- set global properties for this build -->
<property name="src" location="src"/> <property name="src" location="src"/>
@ -38,7 +38,7 @@
<copy file="./target/classes/GeoLiteCity.dat" todir="./target/package/ethereumj/config"/> <copy file="./target/classes/GeoLiteCity.dat" todir="./target/package/ethereumj/config"/>
<zip destfile="ethereumJ-${DSTAMP}-${TSTAMP}.zip" basedir="./target/package"> <zip destfile="ethereumj-${DSTAMP}-${TSTAMP}.zip" basedir="./target/package">
</zip> </zip>

View File

@ -1,4 +1,4 @@
package org.ethereum.wallet; package org.ethereum.core;
import org.ethereum.crypto.ECKey; import org.ethereum.crypto.ECKey;
import org.ethereum.util.Utils; import org.ethereum.util.Utils;
@ -10,23 +10,19 @@ import java.math.BigInteger;
* User: Roman Mandeleil * User: Roman Mandeleil
* Created on: 21/05/2014 10:43 * Created on: 21/05/2014 10:43
*/ */
public class AddressState { public class AddressState {
private ECKey ecKey; private ECKey ecKey;
private BigInteger nonce; private BigInteger nonce;
private BigInteger balance; private BigInteger balance;
public AddressState() { public AddressState() {
ecKey = new ECKey(Utils.getRandom()); ecKey = new ECKey(Utils.getRandom());
nonce = BigInteger.ZERO; nonce = BigInteger.ZERO;
balance = BigInteger.ZERO; balance = BigInteger.ZERO;
} }
public AddressState(ECKey ecKey) { public AddressState(ECKey ecKey) {
this(); this();
this.ecKey = ecKey; this.ecKey = ecKey;
} }
@ -56,6 +52,4 @@ public class AddressState {
public void addToBalance(BigInteger value){ public void addToBalance(BigInteger value){
balance = balance.add(value); balance = balance.add(value);
} }
} }

View File

@ -27,17 +27,9 @@ public class Transaction {
Logger logger = LoggerFactory.getLogger(this.getClass()); Logger logger = LoggerFactory.getLogger(this.getClass());
private static final int CALL_SIZE = 9; public static final byte[] ZERO_ADDRESS = new byte[20];
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};
/* creation contract tx /* SHA3 hash of the RLP encoded transaction */
* [ 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 */
private byte[] hash; private byte[] hash;
/* a counter used to make sure each transaction can only be processed once */ /* a counter used to make sure each transaction can only be processed once */
@ -61,8 +53,8 @@ public class Transaction {
private byte[] gasLimit; private byte[] gasLimit;
/* An unlimited size byte array specifying /* An unlimited size byte array specifying
* input [data] of the message call */ * input [data] of the message call or
/* Initialisation code for a new contract */ * Initialization code for a new contract */
private byte[] data; private byte[] data;
/* the elliptic curve signature /* the elliptic curve signature
@ -73,7 +65,7 @@ public class Transaction {
private byte[] rlpEncoded; private byte[] rlpEncoded;
private byte[] rlpRaw; private byte[] rlpRaw;
/* Indicates if this transaction has been parsed /* Indicates if this transaction has been parsed
* from the rlp-encoded data */ * from the RLP-encoded data */
private boolean parsed = false; private boolean parsed = false;
public Transaction(byte[] rawData) { public Transaction(byte[] rawData) {
@ -81,6 +73,11 @@ public class Transaction {
parsed = false; 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) { public Transaction(byte[] nonce, byte[] gasPrice, byte[] gasLimit, byte[] receiveAddress, byte[] value, byte[] data) {
this.nonce = nonce; this.nonce = nonce;
this.gasPrice = gasPrice; this.gasPrice = gasPrice;
@ -95,7 +92,7 @@ public class Transaction {
parsed = true; parsed = true;
} }
public void rlpParse(){ public void rlpParse() {
RLPList decodedTxList = RLP.decode2(rlpEncoded); RLPList decodedTxList = RLP.decode2(rlpEncoded);
RLPList transaction = (RLPList) decodedTxList.get(0); RLPList transaction = (RLPList) decodedTxList.get(0);
@ -106,7 +103,6 @@ public class Transaction {
this.receiveAddress = ((RLPItem) transaction.get(3)).getRLPData(); this.receiveAddress = ((RLPItem) transaction.get(3)).getRLPData();
this.value = ((RLPItem) transaction.get(4)).getRLPData(); this.value = ((RLPItem) transaction.get(4)).getRLPData();
this.data = ((RLPItem) transaction.get(5)).getRLPData(); this.data = ((RLPItem) transaction.get(5)).getRLPData();
// only parse signature in case tx is signed // only parse signature in case tx is signed
if(((RLPItem) transaction.get(6)).getRLPData() != null) { if(((RLPItem) transaction.get(6)).getRLPData() != null) {
@ -117,7 +113,6 @@ public class Transaction {
} else { } else {
logger.debug("RLP encoded tx is not signed!"); logger.debug("RLP encoded tx is not signed!");
} }
this.parsed = true; this.parsed = true;
this.hash = this.getHash(); this.hash = this.getHash();
} }
@ -127,7 +122,6 @@ public class Transaction {
} }
public byte[] getHash() { public byte[] getHash() {
if (!parsed) rlpParse(); if (!parsed) rlpParse();
byte[] plainMsg = this.getEncodedRaw(); byte[] plainMsg = this.getEncodedRaw();
return HashUtil.sha3(plainMsg); return HashUtil.sha3(plainMsg);
@ -177,11 +171,9 @@ public class Transaction {
if (!isContract()) return null; if (!isContract()) return null;
byte[] val1 = RLP.encodeElement(getSender()); byte[] encSender = RLP.encodeElement(getSender());
byte[] val2 = RLP.encodeElement(nonce); byte[] encNonce = RLP.encodeElement(nonce);
byte[] val = HashUtil.sha3omit12(RLP.encodeList(val1, val2)); return HashUtil.sha3omit12(RLP.encodeList(encSender, encNonce));
return val;
} }
public boolean isContract() { public boolean isContract() {
@ -231,8 +223,8 @@ public class Transaction {
} }
/** /**
* For signature games you have to keep also * For signatures you have to keep also
* rlp of the transaction without any signature data * RLP of the transaction without any signature data
*/ */
public byte[] getEncodedRaw(){ public byte[] getEncodedRaw(){
@ -244,10 +236,10 @@ public class Transaction {
byte[] gasLimit = RLP.encodeElement(this.gasLimit); byte[] gasLimit = RLP.encodeElement(this.gasLimit);
byte[] receiveAddress = RLP.encodeElement(this.receiveAddress); byte[] receiveAddress = RLP.encodeElement(this.receiveAddress);
byte[] value = RLP.encodeElement(this.value); 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, this.rlpRaw = RLP.encodeList(nonce, gasPrice, gasLimit, receiveAddress,
init); value, data);
return rlpRaw; return rlpRaw;
} }
@ -260,7 +252,7 @@ public class Transaction {
byte[] gasLimit = RLP.encodeElement(this.gasLimit); byte[] gasLimit = RLP.encodeElement(this.gasLimit);
byte[] receiveAddress = RLP.encodeElement(this.receiveAddress); byte[] receiveAddress = RLP.encodeElement(this.receiveAddress);
byte[] value = RLP.encodeElement(this.value); byte[] value = RLP.encodeElement(this.value);
byte[] init = RLP.encodeElement(this.data); byte[] data = RLP.encodeElement(this.data);
byte[] v, r, s; byte[] v, r, s;
@ -274,9 +266,8 @@ public class Transaction {
s = RLP.encodeElement(new byte[0]); s = RLP.encodeElement(new byte[0]);
} }
this.rlpEncoded = RLP.encodeList(nonce, gasPrice, gasLimit, receiveAddress, value, this.rlpEncoded = RLP.encodeList(nonce, gasPrice, gasLimit,
init, v, r, s); receiveAddress, value, data, v, r, s);
return rlpEncoded; return rlpEncoded;
} }

View File

@ -1,7 +1,6 @@
package org.ethereum.core; package org.ethereum.core;
import org.ethereum.crypto.ECKey; import org.ethereum.crypto.ECKey;
import org.ethereum.wallet.AddressState;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
import org.w3c.dom.*; import org.w3c.dom.*;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
@ -14,6 +13,7 @@ import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.math.BigInteger; import java.math.BigInteger;
@ -71,23 +71,19 @@ public class Wallet {
return rows.get(address); return rows.get(address);
} }
public BigInteger getBalance(byte[] addressBytes){ public BigInteger getBalance(byte[] addressBytes){
String address = Hex.toHexString(addressBytes); String address = Hex.toHexString(addressBytes);
return rows.get(address).getBalance(); return rows.get(address).getBalance();
} }
public BigInteger totalBalance(){ public BigInteger totalBalance(){
BigInteger sum = BigInteger.ZERO; BigInteger sum = BigInteger.ZERO;
for (AddressState addressState : rows.values()){ for (AddressState addressState : rows.values()){
sum = sum.add(addressState.getBalance()); sum = sum.add(addressState.getBalance());
} }
return sum; return sum;
} }
public void applyTransaction(Transaction transaction){ public void applyTransaction(Transaction transaction){
transactionMap.put(new BigInteger(transaction.getHash()), transaction ); transactionMap.put(new BigInteger(transaction.getHash()), transaction );

View File

@ -1,11 +1,11 @@
package org.ethereum.gui; package org.ethereum.gui;
import org.ethereum.core.AddressState;
import org.ethereum.core.Transaction; import org.ethereum.core.Transaction;
import org.ethereum.manager.MainData; import org.ethereum.manager.MainData;
import org.ethereum.net.client.ClientPeer; import org.ethereum.net.client.ClientPeer;
import org.ethereum.util.ByteUtil; import org.ethereum.util.ByteUtil;
import org.ethereum.util.Utils; import org.ethereum.util.Utils;
import org.ethereum.wallet.AddressState;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.spongycastle.util.BigIntegers; import org.spongycastle.util.BigIntegers;
@ -16,6 +16,7 @@ import javax.swing.border.Border;
import javax.swing.plaf.ComboBoxUI; import javax.swing.plaf.ComboBoxUI;
import javax.swing.plaf.basic.BasicComboBoxUI; import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup; import javax.swing.plaf.basic.BasicComboPopup;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;

View File

@ -1,10 +1,10 @@
package org.ethereum.gui; package org.ethereum.gui;
import org.ethereum.core.AddressState;
import org.ethereum.core.Transaction; import org.ethereum.core.Transaction;
import org.ethereum.manager.MainData; import org.ethereum.manager.MainData;
import org.ethereum.net.client.ClientPeer; import org.ethereum.net.client.ClientPeer;
import org.ethereum.util.Utils; import org.ethereum.util.Utils;
import org.ethereum.wallet.AddressState;
import org.spongycastle.util.BigIntegers; import org.spongycastle.util.BigIntegers;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
@ -13,6 +13,7 @@ import javax.swing.border.Border;
import javax.swing.plaf.ComboBoxUI; import javax.swing.plaf.ComboBoxUI;
import javax.swing.plaf.basic.BasicComboBoxUI; import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup; import javax.swing.plaf.basic.BasicComboPopup;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;

View File

@ -1,12 +1,9 @@
package org.ethereum.gui; package org.ethereum.gui;
import com.google.common.primitives.Longs; import org.ethereum.core.AddressState;
import org.ethereum.core.Transaction; import org.ethereum.core.Transaction;
import org.ethereum.manager.MainData; import org.ethereum.manager.MainData;
import org.ethereum.net.client.ClientPeer; 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.BigIntegers;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
@ -16,16 +13,10 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.URL; 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 java.util.regex.Pattern;
import javax.swing.*; import javax.swing.*;
import static org.ethereum.config.SystemProperties.CONFIG;
/** /**
* www.ethereumJ.com * www.ethereumJ.com
* User: Roman Mandeleil * User: Roman Mandeleil
@ -210,8 +201,6 @@ class PayOutDialog extends JDialog implements MessageAwareDialog{
alertStatusMsg("The address can't afford this transaction"); alertStatusMsg("The address can't afford this transaction");
return false; return false;
} }
return true; return true;
} }
@ -270,7 +259,6 @@ class PayOutDialog extends JDialog implements MessageAwareDialog{
}); });
} }
public static void main(String args[]) { public static void main(String args[]) {
AddressState as = new AddressState(); AddressState as = new AddressState();
PayOutDialog pod = new PayOutDialog(null, as); PayOutDialog pod = new PayOutDialog(null, as);

View File

@ -1,13 +1,14 @@
package org.ethereum.gui; package org.ethereum.gui;
import org.ethereum.core.AddressState;
import org.ethereum.util.Utils; import org.ethereum.util.Utils;
import org.ethereum.wallet.AddressState;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.Border; import javax.swing.border.Border;
import javax.swing.border.CompoundBorder; import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import java.awt.*; import java.awt.*;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;

View File

@ -1,10 +1,11 @@
package org.ethereum.gui; package org.ethereum.gui;
import org.ethereum.core.AddressState;
import org.ethereum.core.Wallet; import org.ethereum.core.Wallet;
import org.ethereum.manager.MainData; import org.ethereum.manager.MainData;
import org.ethereum.wallet.AddressState;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;

View File

@ -6,6 +6,8 @@ import java.net.UnknownHostException;
import java.util.*; import java.util.*;
import com.maxmind.geoip.Location; import com.maxmind.geoip.Location;
import org.ethereum.core.AddressState;
import org.ethereum.core.Block; import org.ethereum.core.Block;
import org.ethereum.core.Transaction; import org.ethereum.core.Transaction;
import org.ethereum.core.Wallet; 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.message.StaticMessages;
import org.ethereum.net.peerdiscovery.PeerDiscovery; import org.ethereum.net.peerdiscovery.PeerDiscovery;
import org.ethereum.net.submit.PendingTransaction; import org.ethereum.net.submit.PendingTransaction;
import org.ethereum.wallet.AddressState;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
@ -150,7 +151,6 @@ public class MainData {
* 2) the dialog send the transaction to a net * 2) the dialog send the transaction to a net
* 3) wherever the transaction got for the wire in will change to approve state * 3) wherever the transaction got for the wire in will change to approve state
* 4) only after the approve a) Wallet state changes * 4) only after the approve a) Wallet state changes
*
* 5) After the block is received with that tx the pending been clean up * 5) After the block is received with that tx the pending been clean up
*/ */
public PendingTransaction addPendingTransaction(Transaction transaction) { public PendingTransaction addPendingTransaction(Transaction transaction) {

View File

@ -4,8 +4,6 @@ import org.ethereum.core.Genesis;
import org.ethereum.crypto.HashUtil; import org.ethereum.crypto.HashUtil;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
import static org.ethereum.config.SystemProperties.CONFIG;
/** /**
* www.ethereumJ.com * www.ethereumJ.com
* User: Roman Mandeleil * User: Roman Mandeleil

View File

@ -4,6 +4,8 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import org.ethereum.core.Transaction;
/** /**
* www.ethereumJ.com * www.ethereumJ.com
* User: Roman Mandeleil * User: Roman Mandeleil
@ -17,7 +19,7 @@ public class TransactionExecutor {
ExecutorService executor = Executors.newFixedThreadPool(1); ExecutorService executor = Executors.newFixedThreadPool(1);
public Future submitTransaction(TransactionTask task){ public Future<Transaction> submitTransaction(TransactionTask task){
return executor.submit(task); return executor.submit(task);
} }

View File

@ -15,21 +15,19 @@ import static java.lang.Thread.sleep;
* User: Roman Mandeleil * User: Roman Mandeleil
* Created on: 23/05/2014 18:33 * Created on: 23/05/2014 18:33
*/ */
public class TransactionTask implements Callable<Transaction> {
public class TransactionTask implements Callable {
Logger logger = LoggerFactory.getLogger("TransactionTask"); Logger logger = LoggerFactory.getLogger("TransactionTask");
Transaction tx; Transaction tx;
boolean obsolete = false; boolean obsolete = false;
public TransactionTask(Transaction tx) { public TransactionTask(Transaction tx) {
this.tx = tx; this.tx = tx;
} }
@Override @Override
public Object call() throws Exception { public Transaction call() throws Exception {
try { try {
logger.info("call() tx: {}", tx.toString()); logger.info("call() tx: {}", tx.toString());
@ -39,10 +37,7 @@ public class TransactionTask implements Callable {
PendingTransaction pendingTransaction = MainData.instance.addPendingTransaction(tx); PendingTransaction pendingTransaction = MainData.instance.addPendingTransaction(tx);
peer.sendTransaction(tx); peer.sendTransaction(tx);
int i = 0;
while(pendingTransaction.getApproved() < 1 ){ while(pendingTransaction.getApproved() < 1 ){
++i;
sleep(10); sleep(10);
} }

View File

@ -29,6 +29,13 @@ public class Utils {
return (new BigInteger(1, numberBytes)).toString(); 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) { public static String longToDateTime(long timestamp) {
Date date = new Date(timestamp * 1000); Date date = new Date(timestamp * 1000);
DateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); DateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
@ -43,7 +50,6 @@ public class Utils {
static BigInteger _1000_ = new BigInteger("1000"); static BigInteger _1000_ = new BigInteger("1000");
public static String getValueShortString(BigInteger number){ public static String getValueShortString(BigInteger number){
BigInteger result = number; BigInteger result = number;
int pow = 0; int pow = 0;
while (result.compareTo(_1000_) == 1 || result.compareTo(_1000_) == 0){ while (result.compareTo(_1000_) == 1 || result.compareTo(_1000_) == 0){

View File

@ -2,12 +2,12 @@ package org.ethereum.core;
import org.ethereum.crypto.ECKey; import org.ethereum.crypto.ECKey;
import org.ethereum.crypto.HashUtil; import org.ethereum.crypto.HashUtil;
import org.ethereum.wallet.AddressState;
import org.junit.Test; import org.junit.Test;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerException;
import java.io.IOException; import java.io.IOException;
import java.math.BigInteger; import java.math.BigInteger;