Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalinin committed Apr 9, 2019
2 parents 1884df3 + 237259b commit 4de63de
Show file tree
Hide file tree
Showing 13 changed files with 735 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,9 @@ public synchronized BlockSummary add(Repository repo, final Block block) {
public synchronized BlockSummary addImpl(Repository repo, final Block block) {

if (exitOn < block.getNumber()) {
System.out.print("Exiting after block.number: " + bestBlock.getNumber());
String msg = String.format("Exiting after block.number: %d", bestBlock.getNumber());
logger.info(msg);
System.out.println(msg);
dbFlushManager.flushSync();
System.exit(-1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ public byte[] getEncodedRaw() {
return rlpRaw;
}

public byte[] getEncoded() {
public synchronized byte[] getEncoded() {

if (rlpEncoded != null) return rlpEncoded;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public ScheduledExecutorService getPongTimer() {
return pongTimer;
}

void setBootNodes(List<Node> bootNodes) {
public void setBootNodes(List<Node> bootNodes) {
this.bootNodes = bootNodes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.ethereum.config.blockchain.DaoNoHFConfig;
import org.ethereum.config.blockchain.FrontierConfig;
import org.ethereum.config.blockchain.HomesteadConfig;
import org.ethereum.config.blockchain.PetersburgConfig;
import org.ethereum.core.*;
import org.ethereum.core.genesis.GenesisLoader;
import org.ethereum.crypto.ECKey;
Expand Down Expand Up @@ -765,8 +766,8 @@ public synchronized void updateBatch(Map<byte[], byte[]> rows) {
}

// Override blockchain net config for fast mining
public static ByzantiumConfig getEasyMiningConfig() {
return new ByzantiumConfig(new DaoNoHFConfig(new HomesteadConfig(new HomesteadConfig.HomesteadConstants() {
public static PetersburgConfig getEasyMiningConfig() {
return new PetersburgConfig(new DaoNoHFConfig(new HomesteadConfig(new HomesteadConfig.HomesteadConstants() {
@Override
public BigInteger getMINIMUM_DIFFICULTY() {
return BigInteger.ONE;
Expand Down
29 changes: 15 additions & 14 deletions ethereumj-core/src/main/java/org/ethereum/vm/program/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -549,23 +549,24 @@ private void createContractImpl(DataWord value, byte[] programCode, byte[] newAd
}

// 4. CREATE THE CONTRACT OUT OF RETURN
byte[] code = result.getHReturn();

long storageCost = getLength(code) * getBlockchainConfig().getGasCost().getCREATE_DATA();
long afterSpend = programInvoke.getGas().longValue() - storageCost - result.getGasUsed();
if (afterSpend < 0) {
if (!blockchainConfig.getConstants().createEmptyContractOnOOG()) {
result.setException(Program.Exception.notEnoughSpendingGas("No gas to return just created contract",
if (!result.isRevert() && result.getException() == null) {
byte[] code = result.getHReturn();
long storageCost = getLength(code) * getBlockchainConfig().getGasCost().getCREATE_DATA();
long afterSpend = programInvoke.getGas().longValue() - result.getGasUsed() - storageCost;
if (afterSpend < 0) {
if (!blockchainConfig.getConstants().createEmptyContractOnOOG()) {
result.setException(Program.Exception.notEnoughSpendingGas("No gas to return just created contract",
storageCost, this));
} else {
track.saveCode(newAddress, EMPTY_BYTE_ARRAY);
}
} else if (getLength(code) > blockchainConfig.getConstants().getMAX_CONTRACT_SZIE()) {
result.setException(Program.Exception.notEnoughSpendingGas("Contract size too large: " + getLength(result.getHReturn()),
storageCost, this));
} else {
track.saveCode(newAddress, EMPTY_BYTE_ARRAY);
result.spendGas(storageCost);
track.saveCode(newAddress, code);
}
} else if (getLength(code) > blockchainConfig.getConstants().getMAX_CONTRACT_SZIE()) {
result.setException(Program.Exception.notEnoughSpendingGas("Contract size too large: " + getLength(result.getHReturn()),
storageCost, this));
} else if (!result.isRevert()){
result.spendGas(storageCost);
track.saveCode(newAddress, code);
}

getResult().merge(result);
Expand Down
21 changes: 13 additions & 8 deletions ethereumj-core/src/main/resources/ethereumj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@ peer.discovery = {
"52.74.57.123:30303",

# Parity discovery nodes
"193.70.55.37:30303",
"144.217.139.5:30303",
"139.99.51.203:30303",
"139.99.160.213:30303",
"163.172.131.191:30303",
"212.47.247.103:30303",
"163.172.157.114:30303"
"138.201.223.35:30303",
"138.201.144.135:30303",
"51.15.42.252:30303",
"163.172.171.38:30303"
"163.172.187.252:30303",
"163.172.157.114:30303",
"136.243.154.244:30303",
"88.212.206.70:30303",
"37.128.191.230:30303",
"46.20.235.22:30303",
"216.158.85.185:30303",
"212.47.247.103:30303",
"138.201.144.135:30303",
"45.55.33.62:30303",
"188.166.255.12:30303",
"159.203.210.80:30303",
"51.15.42.252:30303",
"163.172.171.38:30303"
"52.79.241.155:30303",
"52.78.149.82:30303",
]

# external IP/hostname which is reported as our host during discovery
Expand Down
2 changes: 1 addition & 1 deletion ethereumj-core/src/main/resources/version.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
versionNumber='1.11.0'
versionNumber='1.12.0'
// Remove org.ethereum.db.migrate.MigrateHeaderSourceTotalDiff with databaseVersion > 6
databaseVersion=6
64 changes: 64 additions & 0 deletions ethereumj-core/src/test/java/org/ethereum/core/ChainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.ethereum.core;

import org.ethereum.core.genesis.GenesisJson;
import org.ethereum.core.genesis.GenesisLoader;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.math.BigInteger;

import static org.junit.Assert.*;

/**
* @author alexbraz
* @since 29/03/2019
*/
public class ChainTest {

private static final Logger logger = LoggerFactory.getLogger("test");

Block genesis = GenesisLoader.loadGenesis(getClass().getResourceAsStream("/genesis/olympic.json"));
GenesisJson genesisJson = GenesisLoader.loadGenesisJson((InputStream) getClass().getResourceAsStream("/genesis/olympic.json"));

@Test
public void testContainsBlock() {
Chain c = new Chain();
c.add(genesis);
assertEquals(genesis, c.getLast());
}

@Test
public void testBlockHashNotNull() {
Chain c = new Chain();
c.add(genesis);
assertNotNull(c.getLast().getHash());
}

@Test
public void testDifficultyGenesisCorrectLoadedAndConverted() {
Chain c = new Chain();
c.add(genesis);
assertEquals(new BigInteger(genesisJson.getDifficulty().replace("0x", ""), 16).intValue(), c.getLast().getDifficultyBI().intValue());
}

@Test
public void testParentOnTheChain() {
Chain c = new Chain();
c.add(genesis);
Block block = new Block(genesis.getHeader(), genesis.getTransactionsList(), null);
assertFalse(c.isParentOnTheChain(block));
}

@Test
public void testParentOnTheChain2() {
Chain c = new Chain();
c.add(genesis);
assertFalse(c.isParentOnTheChain(genesis));
}




}
25 changes: 25 additions & 0 deletions ethereumj-core/src/test/java/org/ethereum/core/PremineRawTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.ethereum.core;

import org.junit.Test;

import java.math.BigInteger;

import static org.junit.Assert.*;

/**
* @author alexbraz
* @since 29/03/2019
*/
public class PremineRawTest {

@Test
public void testPremineRawNotNull() {

byte[] addr = "0xcf0f482f2c1ef1f221f09e3cf14122fce0424f94".getBytes();
PremineRaw pr = new PremineRaw(addr, BigInteger.ONE, Denomination.ETHER);

assertTrue(pr.getDenomination() == Denomination.ETHER);
assertEquals(pr.value, BigInteger.ONE);
assertNotNull(pr.getAddr());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.ethereum.datasource;

import org.junit.Test;

import java.math.BigInteger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

/**
* @author alexbraz
* @since 29/03/2019
*/
public class BatchSourceWriterTest {

@Test
public void testFlush() {
BatchSource batchSource = mock(BatchSource.class);
BatchSourceWriter<String, BigInteger> bsw = new BatchSourceWriter(batchSource);
bsw.put("KEY", BigInteger.ONE);
assertTrue(bsw.flushImpl());
}

@Test
public void testValues() {
BatchSource batchSource = mock(BatchSource.class);
BatchSourceWriter<String, BigInteger> bsw = new BatchSourceWriter(batchSource);
bsw.put("ONE", BigInteger.ONE);
bsw.put("TEN", BigInteger.TEN);
bsw.put("ZERO", BigInteger.ZERO);

bsw.buf.forEach((K, v) -> {
assertEquals(v, bsw.buf.get(K));
});

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package org.ethereum.datasource;

import org.ethereum.core.Block;
import org.ethereum.core.Genesis;
import org.ethereum.datasource.inmem.HashMapDB;
import org.ethereum.db.IndexedBlockStore;
import org.ethereum.db.TransactionStore;
import org.ethereum.listener.BlockReplay;
import org.ethereum.listener.EthereumListener;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

import static java.math.BigInteger.ZERO;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

/**
* @author alexbraz
* @since 29/03/2019
*/
public class BlockReplayTest {
private static final Logger logger = LoggerFactory.getLogger("test");


BlockReplay replay;
EthereumListener listener;
private List<Block> blocks = new ArrayList<>();
private BigInteger totDifficulty = ZERO;
Block genesis = Genesis.getInstance();

@Before
public void setup() throws URISyntaxException, IOException {
URL scenario1 = ClassLoader
.getSystemResource("blockstore/load.dmp");

File file = new File(scenario1.toURI());
List<String> strData = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);

IndexedBlockStore indexedBlockStore = indexedBlockStore = new IndexedBlockStore();
indexedBlockStore.init(new HashMapDB<byte[]>(), new HashMapDB<byte[]>());

TransactionStore txStore = new TransactionStore(new HashMapDB<>());

listener = mock(EthereumListener.class);

replay = new BlockReplay(indexedBlockStore, txStore, listener, 0L);
for (String blockRLP : strData) {

Block block = new Block(
Hex.decode(blockRLP));

if (block.getNumber() % 1000 == 0)
logger.info("adding block.hash: [{}] block.number: [{}]",
block.getShortHash(),
block.getNumber());

blocks.add(block);
totDifficulty = totDifficulty.add(block.getDifficultyBI());
indexedBlockStore.saveBlock(block, totDifficulty, true);
}

}

@Test
public void testReplayBlock() {
IndexedBlockStore i = mock(IndexedBlockStore.class);
when(i.getChainBlockByNumber(anyLong())).thenReturn(genesis);
TransactionStore txStore = new TransactionStore(new HashMapDB<>());
replay = new BlockReplay(i, txStore, listener, 0L);
replay.replay();

verify(listener, times(1)).onBlock(any());
}

@Test
public void testListenerNoConnection() {
replay.onNoConnections();
verify(listener, times(1)).onNoConnections();

replay.onSyncDone(null);
verify(listener, times(1)).onSyncDone(any());

replay.onNodeDiscovered(any());
verify(listener, times(1)).onNodeDiscovered(any());

replay.onEthStatusUpdated(any(), any());
verify(listener, times(1)).onEthStatusUpdated(any(), any());

replay.onHandShakePeer(any(), any());
verify(listener, times(1)).onHandShakePeer(any(), any());

replay.onPeerAddedToSyncPool(any());
verify(listener, times(1)).onPeerAddedToSyncPool(any());

replay.onPeerDisconnect(anyString(), anyLong());
verify(listener, times(1)).onPeerDisconnect(anyString(), anyLong());

replay.onPendingStateChanged(any());
verify(listener, times(1)).onPendingStateChanged(any());

replay.onPendingTransactionUpdate(any(), any(), any());
verify(listener, times(1)).onPendingTransactionUpdate(any(), any(), any());

replay.onRecvMessage(any(), any());
verify(listener, times(1)).onRecvMessage(any(), any());

replay.onTransactionExecuted(any());
verify(listener, times(1)).onTransactionExecuted(any());

replay.onSendMessage(any(), any());
verify(listener, times(1)).onSendMessage(any(), any());

replay.onVMTraceCreated(anyString(), anyString());
verify(listener, times(1)).onVMTraceCreated(anyString(), anyString());

}



}
Loading

0 comments on commit 4de63de

Please sign in to comment.