Skip to content

Commit

Permalink
test contract
Browse files Browse the repository at this point in the history
  • Loading branch information
lhhong committed Feb 4, 2017
1 parent fb86ce3 commit 88aa4ed
Show file tree
Hide file tree
Showing 12 changed files with 530 additions and 169 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ ethereum/privateDB
logs/
privateDB/
ethereum/src/main/resources/user.conf
datafile.mv.db
datafile.mv.db
datafile.trace.db
110 changes: 110 additions & 0 deletions ethereum/src/main/java/com/crumbs/ethereum/CrumbsEthereumListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.crumbs.ethereum;

import org.ethereum.core.Block;
import org.ethereum.core.PendingState;
import org.ethereum.core.Transaction;
import org.ethereum.core.TransactionReceipt;
import org.ethereum.facade.Ethereum;
import org.ethereum.listener.EthereumListener;
import org.ethereum.listener.EthereumListenerAdapter;
import org.ethereum.util.BIUtil;
import org.ethereum.util.ByteUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigInteger;
import java.util.List;

public class CrumbsEthereumListener extends EthereumListenerAdapter {

private static final Logger logger = LoggerFactory.getLogger(CrumbsEthereumListener.class);
private Ethereum ethereum;
private boolean syncDone = false;

public CrumbsEthereumListener(Ethereum ethereum) {
this.ethereum = ethereum;
}

@Override
public void onPendingTransactionUpdate(TransactionReceipt txReceipt, PendingTransactionState state, Block block) {
logger.info("@@@@@@@@@@ onPendingTransactionUpdate invoked @@@@@@@@@");
logger.info("State: " + state.name());
logger.info("Tx receipt: " + txReceipt.toString());
logger.info("TX: " + txReceipt.getTransaction().toString());
if (state.compareTo(PendingTransactionState.PENDING) == 0) {
//TODO check if database has the transaction and remove it
}
if (state.compareTo(PendingTransactionState.INCLUDED) == 0) {
//TODO process and save transaction to db
}
}

@Override
public void onPendingStateChanged(PendingState pendingState) {
List<Transaction> txs = pendingState.getPendingTransactions();
logger.info("@@@@@@@@@@ onPendingStateChanged invoked @@@@@@@@@");
for (Transaction tx: txs) {
logger.info("Pending transactions: " + tx.toString());
}
}

@Override
public void onBlock(Block block, List<TransactionReceipt> receipts) {
System.out.println();
System.out.println("Do something on block: " + block.getNumber());

List<Transaction> txs = block.getTransactionsList();

for (Transaction tx : txs) {
logger.info("RECEIVED TRANSACTION FROM: " + ByteUtil.toHexString(tx.getSender()));
logger.info("DATA: " + ByteUtil.toHexString(tx.getData()));
}
if (syncDone)
calcNetHashRate(block);

System.out.println();
}



/**
* Mark the fact that you are touching
* the head of the chain
*/
@Override
public void onSyncDone(SyncState state) {

System.out.println(" ** SYNC DONE ** ");
syncDone = true;
}

/**
* Just small method to estimate total power off all miners on the net
* @param block
*/
private void calcNetHashRate(Block block){

if ( block.getNumber() > 1000){

long avgTime = 1;
long cumTimeDiff = 0;
Block currBlock = block;
for (int i=0; i < 1000; ++i){

Block parent = ethereum.getBlockchain().getBlockByHash(currBlock.getParentHash());
long diff = currBlock.getTimestamp() - parent.getTimestamp();
cumTimeDiff += Math.abs(diff);
currBlock = parent;
}

avgTime = cumTimeDiff / 1000;

BigInteger netHashRate = block.getDifficultyBI().divide(BIUtil.toBI(avgTime));
double hashRate = netHashRate.divide(new BigInteger("1000000000")).doubleValue();

System.out.println("Net hash rate: " + hashRate + " GH/s");
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.crumbs.ethereum;

import org.ethereum.core.Block;
import org.ethereum.mine.MinerListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Created by low on 4/2/17 11:00 AM.
*/
public class CrumbsMinerListener implements MinerListener {

private static final Logger logger = LoggerFactory.getLogger(CrumbsMinerListener.class);

@Override
public void miningStarted() {
logger.info("Miner started");
}

@Override
public void miningStopped() {
logger.info("Miner stopped");
}

@Override
public void blockMiningStarted(Block block) {
logger.info("Start mining block: " + block.getShortDescr());
}

@Override
public void blockMined(Block block) {
//logger.info("Block mined! : \n" + block);
}

@Override
public void blockMiningCanceled(Block block) {
logger.info("Cancel mining block: " + block.getShortDescr());
}
}

61 changes: 57 additions & 4 deletions ethereum/src/main/java/com/crumbs/ethereum/EthereumBean.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
package com.crumbs.ethereum;

import com.alibaba.fastjson.JSON;
import org.ethereum.core.CallTransaction;
import org.ethereum.core.Transaction;
import org.ethereum.crypto.ECKey;
import org.ethereum.facade.Ethereum;
import org.ethereum.facade.EthereumFactory;
import org.ethereum.mine.MinerListener;
import org.ethereum.util.ByteUtil;
import org.ethereum.vm.program.ProgramResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigInteger;
import java.util.concurrent.Future;


public class EthereumBean{

@Autowired
private AccountBean accountBean;

Ethereum ethereum;
public final Logger logger = LoggerFactory.getLogger(EthereumBean.class);

public void start() {
logger.info("EtehreumBean started");
this.ethereum = EthereumFactory.createEthereum();
this.ethereum.addListener(new EthereumListener(ethereum));
this.ethereum.addListener(new CrumbsEthereumListener(ethereum));
this.ethereum.getBlockMiner().addListener(new CrumbsMinerListener());
this.ethereum.getBlockMiner().startMining();
}

Expand Down Expand Up @@ -51,11 +57,58 @@ public void sendMockTx(String sender, String receiver) {
ethereum.submitTransaction(tx);
}

public ProgramResult callConstantFunction(String receiveAddress, ECKey senderPrivateKey,
CallTransaction.Function function, Object... funcArgs) {
return ethereum.callConstantFunction(receiveAddress, senderPrivateKey, function, funcArgs);
}

public final String RICH_KEY = "9afb9a8e71fa44275fca9d421760cd712abb1493c396d4d36fd3f0a01f1cc9f6";
public final String RICH_ADDR = "c82f55da06ec7a3b1c878aa48ad0f8b78257e6d0";

public void sendTransaction(byte[] data) {
Transaction tx = createTx(data);
sendTransaction(tx);
}

public void sendTransaction(byte[] data, SendingTxListener listener) {
sendTransaction(createTx(data), listener);
}

public void sendTransaction(Transaction tx, SendingTxListener listener) {
Future<Transaction> ft = ethereum.submitTransaction(tx);
Thread t = new Thread(new WaitingThread(tx, ft, listener));
t.start();
}

public void sendTransaction(byte[] receiverAddress, byte[] data) {
sendTransaction(createTx(receiverAddress, data));
}

public void sendTransaction(Transaction tx) {
SendingTxListener listener = new SendingTxListener() {
@Override
public void isDone(Transaction tx) {
//TODO save tx in database??
}

@Override
public void isCancelled() {
//TODO send error msg??
}
};
sendTransaction(tx, listener);
}

public void sendEtherFromRich (byte[] receiveAddr) {
ethereum.submitTransaction(createTx(RICH_KEY, receiveAddr, 100000000, null));
sendTransaction(createTx(RICH_KEY, receiveAddr, 100000000, null));
}

public Transaction createTx(byte[] data) {
return createTx(new byte[0], data);
}

public Transaction createTx(byte[] receiverAddr, byte[] data) {
return createTx(accountBean.getKey(), receiverAddr, 0, data);
}

public Transaction createTx(String senderPrivKey, String receiverAddr, long etherToTransact, byte[] data) {
Expand Down
110 changes: 0 additions & 110 deletions ethereum/src/main/java/com/crumbs/ethereum/EthereumListener.java

This file was deleted.

11 changes: 11 additions & 0 deletions ethereum/src/main/java/com/crumbs/ethereum/SendingTxListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.crumbs.ethereum;

import org.ethereum.core.Transaction;

/**
* Created by low on 4/2/17 3:15 PM.
*/
public interface SendingTxListener {
void isDone(Transaction tx);
void isCancelled();
}
Loading

0 comments on commit 88aa4ed

Please sign in to comment.