Skip to content

Instantly share code, notes, and snippets.

@fangyun
Last active November 14, 2018 09:53
Show Gist options
  • Save fangyun/a058ef8ca226cd18acd804aa00b45c73 to your computer and use it in GitHub Desktop.
Save fangyun/a058ef8ca226cd18acd804aa00b45c73 to your computer and use it in GitHub Desktop.
{
"alloc": {
"622edbfd509b2625b2e5e3debbecb2fd329abc29": {
"balance": "1606938044258990275541962092341162602522202993782792835301376"
},
"310bc31996904988b3f324d0c55b5e530d3ef9e3": {
"balance": "1606938044258990275541962092341162602522202993782792835301376"
},
"923e27e786dd26fcf657e3881c51a8dc07019598": {
"balance": "1606938044258990275541962092341162602522202993782792835301376"
}
},
"nonce": "0x0000000000000000",
"difficulty": "0x100000",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
"gasLimit": "0x1000000000"
}
/**
* Copyright 2018 Refinitiv
*/
package com.refinitiv.ethereum.samples;
import org.ethereum.core.Block;
import org.ethereum.core.Transaction;
import org.ethereum.crypto.ECKey;
import org.ethereum.facade.EthereumFactory;
import org.ethereum.mine.EthashListener;
import org.ethereum.samples.BasicSample;
import org.ethereum.util.ByteUtil;
import org.spongycastle.util.encoders.Hex;
import org.springframework.context.annotation.Bean;
/**
* 示例功能: 私网上发现Peer,挖矿,合约交易.
*
* @author Yun Fang (yun.fang@refinitiv.com)
*
*/
public class EJVSample {
private static class MinerConfig {
@Bean
public MinerNode node() {
return new MinerNode();
}
}
static class MinerNode extends BasicSample implements EthashListener {
public MinerNode() {
super("sampleMiner");
}
@Override
public void run() {
ethereum.getBlockMiner().addListener(this);
ethereum.getBlockMiner().startMining();
}
@Override
public void onDatasetUpdate(EthashListener.DatasetStatus minerStatus) {
logger.info("Miner status updated: {}", minerStatus);
if (minerStatus.equals(EthashListener.DatasetStatus.FULL_DATASET_GENERATE_START)) {
logger.info("Generating Full Dataset (may take up to 10 min if not cached)...");
}
if (minerStatus.equals(DatasetStatus.FULL_DATASET_GENERATED)) {
logger.info("Full dataset generated.");
}
}
@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());
}
}
private static class RegularConfig {
@Bean
public RegularNode node() {
return new RegularNode("sampleRegular");
}
}
static class RegularNode extends BasicSample {
RegularNode(String name) {
super(name);
}
@Override
public void onSyncDone() {
new Thread(() -> {
try {
generateTransactions();
} catch (Exception e) {
logger.error("Error generating tx: ", e);
}
}).start();
}
/**
* Generate one simple value transfer transaction each 7 seconds. Thus blocks
* will include one, several and none transactions
*/
private void generateTransactions() throws Exception {
if (config.getConfig().getString("sample.name").equals("regular1")) {
logger.info("Start generating transactions...");
ECKey senderKey = ECKey.fromPrivate(Hex.decode("166a94b0bb06d32152e2ada5b46c4adb9618fef66b2f3b3df78a4f98c4cb56f8"));
byte[] receiverAddr = Hex.decode("923e27e786dd26fcf657e3881c51a8dc07019598"); // regular2
for (int i = ethereum.getRepository().getNonce(senderKey.getAddress())
.intValue(), j = 0; j < 20000; i++, j++) {
{
Transaction tx = new Transaction(ByteUtil.intToBytesNoLeadZeroes(i),
ByteUtil.longToBytesNoLeadZeroes(50_000_000_000L),
ByteUtil.longToBytesNoLeadZeroes(0xfffff), receiverAddr, new byte[] { 77 }, new byte[0],
ethereum.getChainIdForNextBlock());
tx.sign(senderKey);
logger.info("<== Submitting tx: " + tx);
ethereum.submitTransaction(tx);
}
Thread.sleep(7000);
}
}
}
}
/**
* Creating two EthereumJ instances with different config classes
*/
public static void main(String[] args) throws Exception {
String conf = System.getProperty("ethereumj.conf.file");
if (conf.contains("-miner")) {
if (Runtime.getRuntime().maxMemory() < (1250L << 20)) {
MinerNode.sLogger.error("Not enough JVM heap (" + (Runtime.getRuntime().maxMemory() >> 20)
+ "Mb) to generate DAG for mining (DAG requires min 1G). For this sample it is recommended to set -Xmx2G JVM option");
return;
}
BasicSample.sLogger.info("Starting EthtereumJ miner instance!");
EthereumFactory.createEthereum(MinerConfig.class);
} else if (conf.contains("-regular")) {
BasicSample.sLogger.info("Starting EthtereumJ regular instance!");
EthereumFactory.createEthereum(RegularConfig.class);
}
}
}
sample{
name="miner1"
address=622edbfd509b2625b2e5e3debbecb2fd329abc29
nodeId=d21358147c7e14b48a919e7c7df72155ab9ea8a03c382aa575f3a142f084cdbf51e8934d1dd8fbdf68bfa2515fbf6dc47b7c943151299a70eb01a974b073d863
machine="U6066876-TPL-A"
}
peer{
discovery{
enabled=false
external.ip=10.35.51.233
bind.ip=10.35.51.233
persist=false
ip.list=[]
}
listen.port=30000
privateKey=c54d96a696bcd0723b1424fb27616019269c3483900b1dea40c39902a9d44ce7
networkId=40001
active=[
]
}
sync{
enabled=true
}
mine{
extraData = "EJV Miner1 at U6066876-TPL-A"
}
genesis=ejv-private.json
database.dir=ejvDB-miner1
sample{
name="regular1"
address=310bc31996904988b3f324d0c55b5e530d3ef9e3
nodeId=7fd910e93689943f953aef13f66d719907213242556e9e0c3751e7512341375e2f069f2eff6fdcf8da4a4f83d105c2e6134b307cd424d3f0602d68eaf078ab53
machine="T470"
}
peer{
discovery{
enabled=false
external.ip=10.35.14.55
bind.ip=10.35.14.55
persist=false
ip.list=["10.35.51.233:30000"]
}
listen.port=30000
privateKey=166a94b0bb06d32152e2ada5b46c4adb9618fef66b2f3b3df78a4f98c4cb56f8
networkId=40001
active=[
{ url = "enode://d21358147c7e14b48a919e7c7df72155ab9ea8a03c382aa575f3a142f084cdbf51e8934d1dd8fbdf68bfa2515fbf6dc47b7c943151299a70eb01a974b073d863@10.35.51.233:30000" }
]
}
sync{
enabled=true
}
genesis=ejv-private.json
database.dir=ejvDB-regular1
sample{
name="regular2"
address=923e27e786dd26fcf657e3881c51a8dc07019598
nodeId=c5a9ebf906895494e0c14d096a6af22126fe5f1f1f8131c5a5b05476a5e72fe2f9155302fdb4359675b59d411c6b58bb91fb8ef194ebc35d6c468d7aff230c16
machine="Venus"
}
peer{
discovery{
enabled=false
external.ip=10.204.4.150
bind.ip=10.204.4.150
persist=false
ip.list=["10.35.51.233:30000"]
}
listen.port=30000
privateKey=27b1d4ca36b35c93503124db1bf09206e1d3a6102d87528bde438382edaa2ad5
networkId=40001
active=[
{ url = "enode://d21358147c7e14b48a919e7c7df72155ab9ea8a03c382aa575f3a142f084cdbf51e8934d1dd8fbdf68bfa2515fbf6dc47b7c943151299a70eb01a974b073d863@10.35.51.233:30000" }
]
}
sync{
enabled=true
}
genesis=ejv-private.json
database.dir=ejvDB-regular2
set WORK_DIR=C:/Users/U6066876/workspace/ethereumj/ethereumj-core
gradlew run -PjvmArgs=-Dethereumj.conf.file=%WORK_DIR%/config/ethereumj-miner1.conf -PmainClass=com.refinitiv.ethereum.samples.EJVSample
WORK_DIR=/home/yunfang/workspace/ethereumj/ethereumj-core
./gradlew run -PjvmArgs=-Dethereumj.conf.file=$WORK_DIR/config/ethereumj-regular1.conf -PmainClass=com.refinitiv.ethereum.samples.EJVSample
WORK_DIR=/data/1/user/m6066876/workspace/ethereumj/ethereumj-core
./gradlew run -PjvmArgs=-Dethereumj.conf.file=$WORK_DIR/config/ethereumj-regular2.conf -PmainClass=com.refinitiv.ethereum.samples.EJVSample
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment