Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web3 #352

Closed
wants to merge 20 commits into from
Closed

Web3 #352

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Expand Up @@ -36,4 +36,8 @@ test_db*

# Local configuration
**/user.conf
**/out.log
**/out.log

# Nodes
node1
node2
9 changes: 8 additions & 1 deletion ethereumj-core/build.gradle
Expand Up @@ -125,6 +125,11 @@ dependencies {
compile "org.slf4j:slf4j-log4j12:${slf4jVersion}"
compile "log4j:apache-log4j-extras:${log4jVersion}"

compile "org.eclipse.jetty:jetty-server:9.0.0.RC2"
compile "org.eclipse.jetty:jetty-servlet:9.0.0.RC2"
compile "javax.portlet:portlet-api:2.0"
compile "com.github.briandilley.jsonrpc4j:jsonrpc4j:1.1"

compile("com.googlecode.json-simple:json-simple:1.1.1") {
exclude group: 'junit', module: 'junit'
}
Expand Down Expand Up @@ -216,7 +221,9 @@ bintray {
}

bintrayUpload.onlyIf {
(!pullRequest || pullRequest == 'false') && !project.version.endsWith('-SNAPSHOT')
// comment 'false' and uncomment the next line to publish next release
// (!pullRequest || pullRequest == 'false') && !project.version.endsWith('-SNAPSHOT')
false
}

install {
Expand Down
9 changes: 7 additions & 2 deletions ethereumj-core/src/main/java/org/ethereum/Start.java
Expand Up @@ -4,6 +4,7 @@
import org.ethereum.facade.Ethereum;
import org.ethereum.facade.EthereumFactory;
import org.ethereum.net.rlpx.Node;
import org.ethereum.rpc.JsonRpcListener;
import org.spongycastle.util.encoders.Hex;

import java.io.IOException;
Expand All @@ -17,7 +18,7 @@
*/
public class Start {

public static void main(String args[]) throws IOException, URISyntaxException {
public static void main(String args[]) throws Exception {
CLIInterface.call(args);

if (!CONFIG.blocksLoader().equals("")) {
Expand All @@ -29,6 +30,10 @@ public static void main(String args[]) throws IOException, URISyntaxException {

if (!CONFIG.blocksLoader().equals(""))
ethereum.getBlockLoader().loadBlocks();
}

// TODO adding rpc
if (CONFIG.isRpcEnabled()) {
new JsonRpcListener(ethereum).start();
}
}
}
12 changes: 12 additions & 0 deletions ethereumj-core/src/main/java/org/ethereum/cli/CLIInterface.java
Expand Up @@ -62,6 +62,18 @@ public static void call(String[] args) {
logger.info("Resetting db set to [{}]", resetStr);
cliOptions.put(SystemProperties.PROPERTY_DB_RESET, resetStr.toString());
}

// TODO added parameter
// override the rpc parameter
if (args[i].equals("-rpc")) {
if (i + 1 < args.length && !args[i + 1].startsWith("-")) {
String portStr = args[i + 1];
cliOptions.put(SystemProperties.PROPERTY_RPC_PORT, portStr);
logger.info("RPC port set to [{}]", portStr);
}

cliOptions.put(SystemProperties.PROPERTY_RPC_ENABLED, "true");
}
}

logger.info("Overriding config file with CLI options: " + cliOptions);
Expand Down
Expand Up @@ -12,7 +12,11 @@ public class Constants {
public static int GENESIS_GAS_LIMIT = 3_141_592;
public static int MIN_GAS_LIMIT = 125000;
public static int GAS_LIMIT_BOUND_DIVISOR = 1024;
public static BigInteger MINIMUM_DIFFICULTY = BigInteger.valueOf(131072);

// TODO review min difficulty for dev machine
//public static BigInteger MINIMUM_DIFFICULTY = BigInteger.valueOf(131072);
public static BigInteger MINIMUM_DIFFICULTY = BigInteger.valueOf(0x200);

public static BigInteger DIFFICULTY_BOUND_DIVISOR = BigInteger.valueOf(2048);
public static int EXP_DIFFICULTY_PERIOD = 100000;

Expand Down
Expand Up @@ -5,6 +5,8 @@
import org.ethereum.datasource.LevelDbDataSource;
import org.ethereum.db.BlockStore;
import org.ethereum.db.IndexedBlockStore;
import org.ethereum.db.ReceiptStore;
import org.ethereum.db.ReceiptStoreImpl;
import org.mapdb.DB;
import org.mapdb.DBMaker;
import org.mapdb.Serializer;
Expand Down Expand Up @@ -94,4 +96,15 @@ LevelDbDataSource levelDbDataSource() {
LevelDbDataSource levelDbDataSource(String name) {
return new LevelDbDataSource(name);
}

@Bean
public ReceiptStore receiptStore(){

KeyValueDataSource ds = new LevelDbDataSource("receipts");
ds.init();

ReceiptStore store = new ReceiptStoreImpl(ds);

return store;
}
}
Expand Up @@ -50,6 +50,9 @@ public class SystemProperties {
public final static String PROPERTY_LISTEN_PORT = "peer.listen.port";
public final static String PROPERTY_PEER_ACTIVE = "peer.active";
public final static String PROPERTY_DB_RESET = "database.reset";
// TODO review rpc properties
public final static String PROPERTY_RPC_ENABLED = "rpc.enabled";
public final static String PROPERTY_RPC_PORT = "rpc.port";

/* Testing */
private final static Boolean DEFAULT_VMTEST_LOAD_LOCAL = false;
Expand Down Expand Up @@ -716,4 +719,18 @@ public String blocksLoader() {
return config.hasPath("blocks.loader") ?
config.getString("blocks.loader") : DEFAULT_BLOCKS_LOADER;
}

// TODO review added method
public boolean isRpcEnabled() {
return config.hasPath("rpc.enabled") ?
config.getBoolean("rpc.enabled") : false;

}

// TODO review added method
public int RpcPort() {
return config.hasPath("rpc.port") ?
config.getInt("rpc.port") : 4444;

}
}
13 changes: 12 additions & 1 deletion ethereumj-core/src/main/java/org/ethereum/core/BlockHeader.java
Expand Up @@ -20,7 +20,9 @@
* Block header is a value object containing
* the basic information of a block
*/
public class BlockHeader {

// TODO review implements SerializableObject
public class BlockHeader implements SerializableObject {


/* The SHA3 256-bit hash of the parent block, in its entirety */
Expand Down Expand Up @@ -391,4 +393,13 @@ public String toFlatString() {
return toStringWithSuffix("");
}

// TODO added to comply with SerializableObject
public byte[] getRawHash() {
return getHash();
}; // encoding without any signature

// TODO added to comply with SerializableObject
public byte[] getEncodedRaw() {
return getEncoded();
};// encoding without any signature
}
@@ -1,5 +1,7 @@
package org.ethereum.core;

import org.ethereum.db.TransactionInfo;

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

Expand All @@ -19,6 +21,8 @@ public interface Blockchain {

public Block getBestBlock();

TransactionInfo getTransactionInfo(byte[] hash);

public boolean hasParentOnTheChain(Block block);

void close();
Expand Down
30 changes: 28 additions & 2 deletions ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java
Expand Up @@ -5,6 +5,8 @@
import org.ethereum.crypto.SHA3Helper;
import org.ethereum.db.BlockStore;
import org.ethereum.db.ByteArrayWrapper;
import org.ethereum.db.ReceiptStore;
import org.ethereum.db.TransactionInfo;
import org.ethereum.listener.EthereumListener;
import org.ethereum.manager.AdminInfo;
import org.ethereum.trie.Trie;
Expand Down Expand Up @@ -87,6 +89,9 @@ public class BlockchainImpl implements Blockchain, org.ethereum.facade.Blockchai
@Autowired
private BlockStore blockStore;

@Autowired
private ReceiptStore receiptsStore;

private Block bestBlock;

private BigInteger totalDifficulty = ZERO;
Expand Down Expand Up @@ -131,13 +136,15 @@ public BlockchainImpl() {
//todo: autowire over constructor
public BlockchainImpl(BlockStore blockStore, Repository repository,
Wallet wallet, AdminInfo adminInfo,
EthereumListener listener, ParentBlockHeaderValidator parentHeaderValidator) {
EthereumListener listener, ParentBlockHeaderValidator parentHeaderValidator,
ReceiptStore receiptsStore) {
this.blockStore = blockStore;
this.repository = repository;
this.wallet = wallet;
this.adminInfo = adminInfo;
this.listener = listener;
this.parentHeaderValidator = parentHeaderValidator;
this.receiptsStore = receiptsStore;
}

@PostConstruct
Expand All @@ -161,6 +168,20 @@ public Block getBlockByNumber(long blockNr) {
return blockStore.getChainBlockByNumber(blockNr);
}

@Override
public TransactionInfo getTransactionInfo(byte[] hash) {

TransactionInfo txInfo = receiptsStore.get(hash);

if (txInfo == null)
return null;

Transaction tx = this.getBlockByHash(txInfo.getBlockHash()).getTransactionsList().get(txInfo.getIndex());
txInfo.setTransaction(tx);

return txInfo;
}

@Override
public TransactionReceipt getTransactionReceiptByHash(byte[] hash) {
throw new UnsupportedOperationException("TODO: will be implemented soon "); // FIXME: go and fix me
Expand Down Expand Up @@ -706,6 +727,7 @@ private List<TransactionReceipt> applyBlock(Block block) {
long saveTime = System.nanoTime();
int i = 1;
long totalGasUsed = 0;
long gasUsed = 0;
List<TransactionReceipt> receipts = new ArrayList<>();

for (Transaction tx : block.getTransactionsList()) {
Expand All @@ -720,10 +742,12 @@ private List<TransactionReceipt> applyBlock(Block block) {
executor.go();
executor.finalization();

totalGasUsed += executor.getGasUsed();
gasUsed = executor.getGasUsed();
totalGasUsed += gasUsed;

track.commit();
TransactionReceipt receipt = new TransactionReceipt();
receipt.setGasUsed(gasUsed);
receipt.setCumulativeGas(totalGasUsed);
receipt.setPostTxState(repository.getRoot());
receipt.setTransaction(tx);
Expand Down Expand Up @@ -816,6 +840,8 @@ public synchronized void storeBlock(Block block, List<TransactionReceipt> receip
else
blockStore.saveBlock(block, totalDifficulty, true);

receiptsStore.saveMultiple(block.getHash(), receipts);

logger.info("Block saved: number: {}, hash: {}, TD: {}",
block.getNumber(), block.getShortHash(), totalDifficulty);

Expand Down
@@ -0,0 +1,13 @@
package org.ethereum.core;

/**
* Created by Sergio on 03/11/2015.
*/
public interface SerializableObject {

public byte[] getHash();
public byte[] getRawHash(); // encoding without any signature
public byte[] getEncoded();
public byte[] getEncodedRaw();// encoding without any signature

}
18 changes: 13 additions & 5 deletions ethereumj-core/src/main/java/org/ethereum/core/Transaction.java
Expand Up @@ -30,7 +30,9 @@
* There are two types of transactions: those which result in message calls
* and those which result in the creation of new contracts.
*/
public class Transaction {

// TODO review implements SerializableObejct
public class Transaction implements SerializableObject {

private static final Logger logger = LoggerFactory.getLogger(Transaction.class);
private static final BigInteger DEFAULT_GAS_PRICE = new BigInteger("10000000000000");
Expand Down Expand Up @@ -372,11 +374,17 @@ public static Transaction createDefault(String to, BigInteger amount, BigInteger
}

public static Transaction create(String to, BigInteger amount, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit){
return create(to, amount, nonce, gasPrice, gasLimit, null);
}

public static Transaction create(String to, BigInteger amount, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String data){

byte[] decodedData = data == null ? null : Hex.decode(data);

return new Transaction(BigIntegers.asUnsignedByteArray(nonce),
BigIntegers.asUnsignedByteArray(gasPrice),
BigIntegers.asUnsignedByteArray(gasLimit),
Hex.decode(to),
to != null ? Hex.decode(to) : null,
BigIntegers.asUnsignedByteArray(amount),
null);
}
}
decodedData);
}}