From a7231cd5bd7315bb4ed3c24f70b65e950d2c5b6f Mon Sep 17 00:00:00 2001 From: romanman Date: Mon, 8 Sep 2014 10:41:18 +0300 Subject: [PATCH] Adapting for Ethereum as a library style: + APIs for various simple functions exposed to the user --- .../main/java/org/ethereum/db/Repository.java | 2 +- .../java/org/ethereum/facade/Blockchain.java | 1 + .../java/org/ethereum/facade/Ethereum.java | 15 +++++++-- .../org/ethereum/facade/EthereumImpl.java | 13 +++++++- .../java/org/ethereum/facade/Repository.java | 20 ++++++++++++ .../org/ethereum/gui/ContractCallDialog.java | 32 +++++++++++-------- .../ethereum/gui/ContractSubmitDialog.java | 23 ++++++------- .../java/org/ethereum/gui/DialogWorker.java | 15 ++++----- .../java/org/ethereum/gui/WalletWindow.java | 8 ++--- 9 files changed, 84 insertions(+), 45 deletions(-) create mode 100644 ethereumj-core/src/main/java/org/ethereum/facade/Repository.java diff --git a/ethereumj-core/src/main/java/org/ethereum/db/Repository.java b/ethereumj-core/src/main/java/org/ethereum/db/Repository.java index 22fa80ab..4ebe5698 100644 --- a/ethereumj-core/src/main/java/org/ethereum/db/Repository.java +++ b/ethereumj-core/src/main/java/org/ethereum/db/Repository.java @@ -51,7 +51,7 @@ import static org.ethereum.config.SystemProperties.CONFIG; * @author: Roman Mandeleil * Created on: 23/06/2014 23:01 */ -public class Repository { +public class Repository implements org.ethereum.facade.Repository{ private Logger logger = LoggerFactory.getLogger("repository"); diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/Blockchain.java b/ethereumj-core/src/main/java/org/ethereum/facade/Blockchain.java index 844f07e4..0b5d20f0 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/Blockchain.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/Blockchain.java @@ -14,4 +14,5 @@ public interface Blockchain { public int getSize(); public Block getBlockByNumber(long blockNr); public long getGasPrice(); + public Block getLastBlock(); } diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java b/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java index 3222875c..a51b6aa0 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java @@ -1,7 +1,8 @@ package org.ethereum.facade; -import org.ethereum.core.Block; import org.ethereum.core.Transaction; +import org.ethereum.core.Wallet; +import org.ethereum.facade.Repository; import org.ethereum.listener.EthereumListener; import org.ethereum.net.client.ClientPeer; import org.ethereum.net.client.PeerData; @@ -109,8 +110,18 @@ public interface Ethereum { public Future submitTransaction(Transaction transaction); + /** + * @return wallet object which is the manager + * of internal accounts + */ + public Wallet getWallet(); + + + /** + * @return - repository for all state data. + */ + public Repository getRepository(); -// 1. WorldManager.getInstance().getWallet(); // 2. // is blockchain still loading - if buffer is not empty diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java index e2685431..9df585af 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java @@ -6,6 +6,8 @@ import java.util.Set; import java.util.concurrent.Future; import org.ethereum.core.Transaction; +import org.ethereum.core.Wallet; +import org.ethereum.db.Repository; import org.ethereum.listener.EthereumListener; import org.ethereum.manager.WorldManager; import org.ethereum.net.client.ClientPeer; @@ -186,6 +188,15 @@ public class EthereumImpl implements Ethereum { } - // public Future submitTransaction() -- wait for approve (like in wallet dialog) + @Override + public Wallet getWallet(){ + return WorldManager.getInstance().getWallet(); + } + + + @Override + public Repository getRepository(){ + return WorldManager.getInstance().getRepository(); + } } diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/Repository.java b/ethereumj-core/src/main/java/org/ethereum/facade/Repository.java new file mode 100644 index 00000000..5b7dbc62 --- /dev/null +++ b/ethereumj-core/src/main/java/org/ethereum/facade/Repository.java @@ -0,0 +1,20 @@ +package org.ethereum.facade; + +import org.ethereum.core.AccountState; +import org.ethereum.db.ContractDetails; + +import java.math.BigInteger; + +/** + * www.ethereumJ.com + * + * @author: Roman Mandeleil + * Created on: 08/09/2014 10:25 + */ + +public interface Repository { + + public AccountState getAccountState(byte[] addr); + public ContractDetails getContractDetails(byte[] addr); + +} diff --git a/ethereumj-studio/src/main/java/org/ethereum/gui/ContractCallDialog.java b/ethereumj-studio/src/main/java/org/ethereum/gui/ContractCallDialog.java index a5884d1c..609ebd09 100644 --- a/ethereumj-studio/src/main/java/org/ethereum/gui/ContractCallDialog.java +++ b/ethereumj-studio/src/main/java/org/ethereum/gui/ContractCallDialog.java @@ -1,10 +1,10 @@ package org.ethereum.gui; import org.ethereum.core.Account; +import org.ethereum.core.Block; import org.ethereum.core.Transaction; +import org.ethereum.core.Wallet; import org.ethereum.db.ContractDetails; -import org.ethereum.manager.WorldManager; -import org.ethereum.net.client.ClientPeer; import org.ethereum.util.ByteUtil; import org.ethereum.util.Utils; import org.slf4j.Logger; @@ -18,7 +18,6 @@ import javax.swing.border.EtchedBorder; import javax.swing.plaf.ComboBoxUI; import javax.swing.plaf.basic.BasicComboPopup; import javax.swing.table.DefaultTableModel; - import java.awt.*; import java.awt.event.*; import java.math.BigInteger; @@ -172,8 +171,8 @@ class ContractCallDialog extends JDialog implements MessageAwareDialog { JComponent editor = (JComponent)(creatorAddressCombo.getEditor().getEditorComponent()); editor.setForeground(Color.RED); - Collection accounts = - WorldManager.getInstance().getWallet().getAccountCollection(); + Wallet wallet = UIEthereumManager.ethereum.getWallet(); + Collection accounts = wallet.getAccountCollection(); for (Account account : accounts) { creatorAddressCombo.addItem(new AccountWrapper(account)); @@ -225,8 +224,12 @@ class ContractCallDialog extends JDialog implements MessageAwareDialog { } byte[] contractAddress = Hex.decode( contractAddr ); - final byte[] programCode = WorldManager.getInstance().getRepository().getCode(contractAddress); - final Map storageMap = WorldManager.getInstance().getRepository().getContractDetails(contractAddress).getStorage(); + + ContractDetails contractDetails = + UIEthereumManager.ethereum.getRepository().getContractDetails(contractAddress); + + final byte[] programCode = contractDetails.getCode(); + final Map storageMap = contractDetails.getStorage(); contractDataInput.setBounds(70, 80, 350, 145); contractDataInput.setViewportView(msgDataTA); @@ -300,13 +303,13 @@ class ContractCallDialog extends JDialog implements MessageAwareDialog { private void playContractCall() { byte[] contractAddress = Hex.decode(contractAddrInput.getText()); - ContractDetails contractDetails = WorldManager.getInstance().getRepository().getContractDetails(contractAddress); + ContractDetails contractDetails = UIEthereumManager.ethereum.getRepository().getContractDetails(contractAddress); if (contractDetails == null) { alertStatusMsg("No contract for that address"); return; } - byte[] programCode = WorldManager.getInstance().getRepository().getCode(contractAddress); + byte[] programCode = contractDetails.getCode(); if (programCode == null || programCode.length == 0) { alertStatusMsg("Such account exist but no code in the db"); return; @@ -315,7 +318,8 @@ class ContractCallDialog extends JDialog implements MessageAwareDialog { Transaction tx = createTransaction(); if (tx == null) return; - ProgramPlayDialog.createAndShowGUI(programCode, tx, WorldManager.getInstance().getBlockchain().getLastBlock()); + Block lastBlock = UIEthereumManager.ethereum.getBlockChain().getLastBlock(); + ProgramPlayDialog.createAndShowGUI(programCode, tx, lastBlock); } protected JRootPane createRootPane() { @@ -357,8 +361,7 @@ class ContractCallDialog extends JDialog implements MessageAwareDialog { public void submitContractCall() { - ClientPeer peer = WorldManager.getInstance().getActivePeer(); - if (peer == null) { + if (!UIEthereumManager.ethereum.isConnected()) { dialog.alertStatusMsg("Not connected to any peer"); return; } @@ -406,7 +409,8 @@ class ContractCallDialog extends JDialog implements MessageAwareDialog { logger.info("tx.data: {}", Hex.toHexString(data)); } - Transaction tx = new Transaction(nonce, gasPrice, gasValue, + + Transaction tx = UIEthereumManager.ethereum.createTransaction(nonce, gasPrice, gasValue, contractAddress, endowment, data); try { @@ -465,7 +469,7 @@ class ContractCallDialog extends JDialog implements MessageAwareDialog { ccd.addWindowListener(new WindowAdapter() { @Override public void windowClosed(WindowEvent e) { - WorldManager.getInstance().close(); + UIEthereumManager.ethereum.close(); } }); } diff --git a/ethereumj-studio/src/main/java/org/ethereum/gui/ContractSubmitDialog.java b/ethereumj-studio/src/main/java/org/ethereum/gui/ContractSubmitDialog.java index c65efc06..6791d422 100644 --- a/ethereumj-studio/src/main/java/org/ethereum/gui/ContractSubmitDialog.java +++ b/ethereumj-studio/src/main/java/org/ethereum/gui/ContractSubmitDialog.java @@ -1,10 +1,6 @@ package org.ethereum.gui; -import org.ethereum.core.Account; -import org.ethereum.core.AccountState; -import org.ethereum.core.Transaction; -import org.ethereum.manager.WorldManager; -import org.ethereum.net.client.ClientPeer; +import org.ethereum.core.*; import org.ethereum.util.Utils; import org.spongycastle.util.BigIntegers; import org.spongycastle.util.encoders.Hex; @@ -13,7 +9,6 @@ import javax.swing.*; import javax.swing.border.Border; import javax.swing.plaf.ComboBoxUI; import javax.swing.plaf.basic.BasicComboPopup; - import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; @@ -106,8 +101,8 @@ class ContractSubmitDialog extends JDialog implements MessageAwareDialog { } contractAddrInput.setText(Hex.toHexString(tx.getContractAddress())); - ProgramPlayDialog.createAndShowGUI(tx.getData(), tx, - WorldManager.getInstance().getBlockchain().getLastBlock()); + Block lastBlock = UIEthereumManager.ethereum.getBlockChain().getLastBlock(); + ProgramPlayDialog.createAndShowGUI(tx.getData(), tx, lastBlock); }} ); @@ -168,8 +163,8 @@ class ContractSubmitDialog extends JDialog implements MessageAwareDialog { JComponent editor = (JComponent)(creatorAddressCombo.getEditor().getEditorComponent()); editor.setForeground(Color.RED); - Collection accounts = - WorldManager.getInstance().getWallet().getAccountCollection(); + Wallet wallet = UIEthereumManager.ethereum.getWallet(); + Collection accounts = wallet.getAccountCollection(); for (Account account : accounts) { creatorAddressCombo.addItem(new AccountWrapper(account)); @@ -265,11 +260,11 @@ class ContractSubmitDialog extends JDialog implements MessageAwareDialog { } contractAddrInput.setText(Hex.toHexString(tx.getContractAddress())); - ClientPeer peer = WorldManager.getInstance().getActivePeer(); - if (peer == null) { + if (!UIEthereumManager.ethereum.isConnected()) { dialog.alertStatusMsg("Not connected to any peer"); return; } + // SwingWorker DialogWorker worker = new DialogWorker(tx, this); worker.execute(); @@ -301,7 +296,9 @@ class ContractSubmitDialog extends JDialog implements MessageAwareDialog { Account account = ((AccountWrapper)creatorAddressCombo.getSelectedItem()).getAccount(); BigInteger currentBalance = account.getBalance(); - BigInteger gasPrice = BigInteger.valueOf(WorldManager.getInstance().getBlockchain().getGasPrice()); + + long currGasPrice = UIEthereumManager.ethereum.getBlockChain().getGasPrice(); + BigInteger gasPrice = BigInteger.valueOf(currGasPrice); BigInteger gasInput = new BigInteger( this.gasInput.getText()); boolean canAfford = currentBalance.compareTo(gasPrice.multiply(gasInput)) >= 0; diff --git a/ethereumj-studio/src/main/java/org/ethereum/gui/DialogWorker.java b/ethereumj-studio/src/main/java/org/ethereum/gui/DialogWorker.java index b2192b7d..d46dc7cf 100644 --- a/ethereumj-studio/src/main/java/org/ethereum/gui/DialogWorker.java +++ b/ethereumj-studio/src/main/java/org/ethereum/gui/DialogWorker.java @@ -1,14 +1,10 @@ package org.ethereum.gui; import org.ethereum.core.Transaction; -import org.ethereum.manager.WorldManager; -import org.ethereum.net.submit.TransactionExecutor; -import org.ethereum.net.submit.TransactionTask; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.swing.*; - import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -18,13 +14,14 @@ import static org.ethereum.config.SystemProperties.CONFIG; /** * www.ethereumJ.com + * * @author: Roman Mandeleil * Created on: 26/05/2014 12:27 */ public class DialogWorker extends SwingWorker { - private static Logger logger = LoggerFactory.getLogger(DialogWorker.class); - + private static Logger logger = LoggerFactory.getLogger(DialogWorker.class); + private Transaction tx; private MessageAwareDialog dialog; @@ -36,7 +33,7 @@ public class DialogWorker extends SwingWorker { @Override protected Transaction doInBackground() throws Exception { - Future future = UIEthereumManager.ethereum.submitTransaction(tx); + Future future = UIEthereumManager.ethereum.submitTransaction(tx); dialog.infoStatusMsg("Transaction sent to the network, waiting for approve"); try { @@ -46,11 +43,11 @@ public class DialogWorker extends SwingWorker { dialog.alertStatusMsg("Transaction wasn't approved, network timeout"); return null; } catch (InterruptedException ie) { - logger.error(ie.getMessage(), ie); + logger.error(ie.getMessage(), ie); dialog.alertStatusMsg("Transaction wasn't approved"); return null; } catch (ExecutionException ee) { - logger.error(ee.getMessage(), ee); + logger.error(ee.getMessage(), ee); dialog.alertStatusMsg("Transaction wasn't approved"); return null; } finally { diff --git a/ethereumj-studio/src/main/java/org/ethereum/gui/WalletWindow.java b/ethereumj-studio/src/main/java/org/ethereum/gui/WalletWindow.java index 13cb7f2d..78e74d99 100644 --- a/ethereumj-studio/src/main/java/org/ethereum/gui/WalletWindow.java +++ b/ethereumj-studio/src/main/java/org/ethereum/gui/WalletWindow.java @@ -2,10 +2,8 @@ package org.ethereum.gui; import org.ethereum.core.Account; import org.ethereum.core.Wallet; -import org.ethereum.manager.WorldManager; import javax.swing.*; - import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; @@ -41,7 +39,7 @@ public class WalletWindow extends JFrame implements Wallet.WalletListener{ Container contentPane = this.getContentPane(); contentPane.setBackground(new Color(255, 255, 255)); - Wallet wallet = WorldManager.getInstance().getWallet(); + Wallet wallet = UIEthereumManager.ethereum.getWallet(); wallet.addListener(this); loadWallet(); @@ -53,7 +51,7 @@ public class WalletWindow extends JFrame implements Wallet.WalletListener{ contentPane.removeAll(); contentPane.setLayout(new FlowLayout()); - Wallet wallet = WorldManager.getInstance().getWallet(); + Wallet wallet = UIEthereumManager.ethereum.getWallet(); for (Account account : wallet.getAccountCollection()) { WalletAddressPanel rowPanel = new WalletAddressPanel(account); @@ -73,7 +71,7 @@ public class WalletWindow extends JFrame implements Wallet.WalletListener{ @Override public void mouseClicked(MouseEvent e) { - Wallet wallet = WorldManager.getInstance().getWallet(); + Wallet wallet = UIEthereumManager.ethereum.getWallet(); if (wallet.getAccountCollection().size() >= 5) { JOptionPane.showMessageDialog(walletWindow, "Hey do you really need more than 5 address for a demo wallet");