-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
530 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
ethereum/src/main/java/com/crumbs/ethereum/CrumbsEthereumListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
ethereum/src/main/java/com/crumbs/ethereum/CrumbsMinerListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 0 additions & 110 deletions
110
ethereum/src/main/java/com/crumbs/ethereum/EthereumListener.java
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
ethereum/src/main/java/com/crumbs/ethereum/SendingTxListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Oops, something went wrong.