Skip to content

Commit

Permalink
Merge f4b29b6 into 20626ff
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalinin committed Jul 27, 2018
2 parents 20626ff + f4b29b6 commit ff18f25
Show file tree
Hide file tree
Showing 27 changed files with 1,178 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,11 @@ public String getEthashMode() {
return config.getString("sync.ethash");
}

@ValidateMe
public boolean processBeaconChain() {
return config.getBoolean("beacon.enabled");
}

private GenesisJson getGenesisJson() {
if (genesisJson == null) {
genesisJson = GenesisLoader.loadGenesisJson(this, classLoader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,10 @@ public void sign(byte[] privKeyBytes) throws MissingPrivateKeyException {
sign(ECKey.fromPrivate(privKeyBytes));
}

public void sign(ECKey key) throws MissingPrivateKeyException {
public Transaction sign(ECKey key) throws MissingPrivateKeyException {
this.signature = key.sign(this.getRawHash());
this.rlpEncoded = null;
return this;
}

@Override
Expand Down
11 changes: 11 additions & 0 deletions ethereumj-core/src/main/java/org/ethereum/db/BlockStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ public interface BlockStore {

List<Block> getListBlocksEndWith(byte[] hash, long qty);

/**
* Returns a list of blocks starting from toHash (inclusively) down to fromHash (inclusively).
* Both hashes should be of blocks belonging the same chain, otherwise method returns an empty list.
* If some blocks between from and to are missed then empty list is returned.
*
* @param fromHash lower bound of a list
* @param toHash upper bound of a list
* @return returns a list with inverted block order
*/
List<Block> listBlocks(byte[] fromHash, byte[] toHash);

void saveBlock(Block block, BigInteger totalDifficulty, boolean mainChain);

BigInteger getTotalDifficultyForHash(byte[] hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.math.BigInteger;

import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -73,6 +74,11 @@ public List<Block> getListBlocksEndWith(byte[] hash, long qty) {
return null;
}

@Override
public List<Block> listBlocks(byte[] fromHash, byte[] toHash) {
return Collections.emptyList();
}

@Override
public void saveBlock(Block block, BigInteger totalDifficulty, boolean mainChain) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.*;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static java.math.BigInteger.ZERO;
Expand Down Expand Up @@ -290,6 +291,30 @@ public synchronized List<Block> getListBlocksEndWith(byte[] hash, long qty) {
return getListBlocksEndWithInner(hash, qty);
}

@Override
public List<Block> listBlocks(byte[] fromHash, byte[] toHash) {
Block from = blocks.get(fromHash);
Block to = blocks.get(toHash);

// sanity checks
if (from == null || to == null || to.getNumber() < from.getNumber())
return Collections.emptyList();

long qty = to.getNumber() - from.getNumber() + 1;

List<Block> ret = new ArrayList<>();
Block block = to;
for (long i = 0; i < qty && block != null && !from.isEqual(block); ++i) {
ret.add(block);
block = blocks.get(block.getParentHash());
}

if (ret.size() > 0 && !ret.get(ret.size() - 1).isEqual(from))
return Collections.emptyList();

return ret;
}

private List<Block> getListBlocksEndWithInner(byte[] hash, long qty) {

Block block = this.blocks.get(hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public class WorldManager {
private NodeManager nodeManager;

@Autowired
private SyncManager syncManager;
protected SyncManager syncManager;

@Autowired
private SyncPool pool;
protected SyncPool pool;

@Autowired
private PendingState pendingState;
Expand Down Expand Up @@ -116,7 +116,7 @@ public WorldManager(final SystemProperties config, final Repository repository,
}

@PostConstruct
private void init() {
protected void init() {
fastSyncDbJobs();
syncManager.init(channelManager, pool);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/
package org.ethereum.sharding;

import org.apache.commons.lang3.StringUtils;
import org.ethereum.cli.CLIInterface;
import org.ethereum.config.SystemProperties;
import org.ethereum.facade.Ethereum;
import org.ethereum.facade.EthereumFactory;
import org.ethereum.mine.Ethash;
import org.ethereum.sharding.config.ShardingConfig;

import java.io.IOException;
import java.net.URISyntaxException;

/**
* @author Mikhail Kalinin
* @since 26.07.2018
*/
public class ShardingEntry {

public static void main(String args[]) throws IOException, URISyntaxException {
System.setProperty("ethereumj.conf.res", "sharding.conf");

CLIInterface.call(args);

final SystemProperties config = SystemProperties.getDefault();
final boolean actionBlocksLoader = !config.blocksLoader().isEmpty();
final boolean actionGenerateDag = !StringUtils.isEmpty(System.getProperty("ethash.blockNumber"));

if (actionBlocksLoader || actionGenerateDag) {
config.setSyncEnabled(false);
config.setDiscoveryEnabled(false);
}

if (actionGenerateDag) {
new Ethash(config, Long.parseLong(System.getProperty("ethash.blockNumber"))).getFullDataset();
// DAG file has been created, lets exit
System.exit(0);
} else {
Ethereum ethereum = EthereumFactory.createEthereum(new Class[] { ShardingConfig.class });

if (actionBlocksLoader) {
ethereum.getBlockLoader().loadBlocks();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/
package org.ethereum.sharding.config;

import org.ethereum.config.CommonConfig;
import org.ethereum.config.SystemProperties;
import org.ethereum.datasource.BatchSourceWriter;
import org.ethereum.datasource.DbSource;
import org.ethereum.datasource.WriteCache;
import org.ethereum.facade.Ethereum;
import org.ethereum.manager.WorldManager;
import org.ethereum.sharding.manager.ShardingWorldManager;
import org.ethereum.sharding.service.ValidatorService;
import org.ethereum.sharding.crypto.DepositAuthority;
import org.ethereum.sharding.contract.DepositContract;
import org.ethereum.sharding.crypto.UnsecuredDepositAuthority;
import org.ethereum.sharding.util.Randao;
import org.ethereum.sharding.service.ValidatorRepository;
import org.ethereum.sharding.service.ValidatorServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
* @author Mikhail Kalinin
* @since 21.07.2018
*/
@Configuration
@Conditional(BeaconConfig.Enabled.class)
public class BeaconConfig {

@Autowired
CommonConfig commonConfig;

@Autowired
Ethereum ethereum;

@Autowired
WorldManager worldManager;

@Autowired
DepositContractConfig depositContractConfig;

@Bean
public ValidatorConfig validatorConfig() {
return ValidatorConfig.fromFile();
}

@Bean @Primary
public ValidatorService validatorService() {
if (validatorConfig().isEnabled()) {
ValidatorService ret = new ValidatorServiceImpl(ethereum, validatorConfig(), depositContract(),
depositAuthority(), randao());
((ShardingWorldManager) worldManager).setValidatorService(ret);
return ret;
} else {
return new ValidatorService() {};
}
}

@Bean
public ValidatorRepository validatorRepository() {
return new ValidatorRepository();
}

@Bean
public DepositContract depositContract() {
return new DepositContract(ethereum, depositContractConfig.getBin(), depositContractConfig.getAbi());
}

public DepositAuthority depositAuthority() {
return new UnsecuredDepositAuthority(validatorConfig());
}

public Randao randao() {
DbSource<byte[]> src = commonConfig.keyValueDataSource("randao");
WriteCache.BytesKey<byte[]> cache = new WriteCache.BytesKey<>(
new BatchSourceWriter<>(src), WriteCache.CacheType.SIMPLE);
cache.setFlushSource(true);
commonConfig.dbFlushManager().addCache(cache);

return new Randao(cache);
}

public static class Enabled implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return SystemProperties.getDefault().processBeaconChain();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/
package org.ethereum.sharding.config;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.spongycastle.util.encoders.Hex;

/**
* @author Mikhail Kalinin
* @since 26.07.2018
*/
public class DepositContractConfig {

private String abi;
private byte[] bin;
private byte[] address;

public DepositContractConfig(String abi, byte[] bin, byte[] address) {
this.abi = abi;
this.bin = bin;
this.address = address;
}

public static DepositContractConfig fromFile() {
Config config = ConfigFactory.parseResources("deposit-contract.conf");

return new DepositContractConfig(
config.getString("beacon.deposit.contract.abi"),
Hex.decode(config.getString("beacon.deposit.contract.bin")),
Hex.decode(config.getString("beacon.deposit.contract.address"))
);
}

public String getAbi() {
return abi;
}

public byte[] getBin() {
return bin;
}

public byte[] getAddress() {
return address;
}
}

0 comments on commit ff18f25

Please sign in to comment.