Adapting for Ethereum as a library style:

+ APIs for various simple functions exposed to the user
This commit is contained in:
romanman 2014-09-08 10:41:18 +03:00
parent 7f8364bd0a
commit a7231cd5bd
9 changed files with 84 additions and 45 deletions

View File

@ -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");

View File

@ -14,4 +14,5 @@ public interface Blockchain {
public int getSize();
public Block getBlockByNumber(long blockNr);
public long getGasPrice();
public Block getLastBlock();
}

View File

@ -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<Transaction> 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

View File

@ -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<Transaction> submitTransaction() -- wait for approve (like in wallet dialog)
@Override
public Wallet getWallet(){
return WorldManager.getInstance().getWallet();
}
@Override
public Repository getRepository(){
return WorldManager.getInstance().getRepository();
}
}

View File

@ -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);
}

View File

@ -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<Account> accounts =
WorldManager.getInstance().getWallet().getAccountCollection();
Wallet wallet = UIEthereumManager.ethereum.getWallet();
Collection<Account> 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();
}
});
}

View File

@ -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<Account> accounts =
WorldManager.getInstance().getWallet().getAccountCollection();
Wallet wallet = UIEthereumManager.ethereum.getWallet();
Collection<Account> 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;

View File

@ -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,6 +14,7 @@ import static org.ethereum.config.SystemProperties.CONFIG;
/**
* www.ethereumJ.com
*
* @author: Roman Mandeleil
* Created on: 26/05/2014 12:27
*/

View File

@ -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");