Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Increase coverage for .config.net to 100%
Browse files Browse the repository at this point in the history
(cherry picked from commit 77a01b0)
  • Loading branch information
Erik committed Oct 31, 2017
1 parent 9d1fdaa commit f7f7d30
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.config.net;

import org.ethereum.config.BlockchainConfig;
import org.ethereum.config.blockchain.AbstractConfig;
import org.ethereum.core.BlockHeader;
import org.ethereum.core.Transaction;
import org.junit.Test;

import java.math.BigInteger;

import static org.junit.Assert.*;

public class BaseNetConfigTest {
@Test(expected = RuntimeException.class)
public void addedBlockShouldHaveIncrementedBlockNumber() throws Exception {
BlockchainConfig blockchainConfig = new TestBlockchainConfig();

BaseNetConfig config = new BaseNetConfig();
config.add(0, blockchainConfig);
config.add(1, blockchainConfig);
config.add(0, blockchainConfig);
}

@Test
public void toStringShouldCaterForNulls() throws Exception {
BaseNetConfig config = new BaseNetConfig();
assertEquals("BaseNetConfig{blockNumbers=[], configs=[], count=0}", config.toString());

BlockchainConfig blockchainConfig = new TestBlockchainConfig() {
@Override
public String toString() {
return "TestBlockchainConfig";
}
};
config.add(0, blockchainConfig);
assertEquals("BaseNetConfig{blockNumbers=[0], configs=[TestBlockchainConfig], count=1}", config.toString());

config.add(1, blockchainConfig);
assertEquals("BaseNetConfig{blockNumbers=[0, 1], configs=[TestBlockchainConfig, TestBlockchainConfig], count=2}", config.toString());
}

private static class TestBlockchainConfig extends AbstractConfig {
@Override
public BigInteger getCalcDifficultyMultiplier(BlockHeader curBlock, BlockHeader parent) {
return BigInteger.ZERO;
}

@Override
public long getTransactionCost(Transaction tx) {
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.config.net;

import org.ethereum.config.BlockchainConfig;
import org.ethereum.config.BlockchainNetConfig;
import org.ethereum.config.blockchain.Eip150HFConfig;
import org.ethereum.config.blockchain.Eip160HFConfig;
import org.ethereum.config.blockchain.FrontierConfig;
import org.ethereum.config.blockchain.HomesteadConfig;
import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class ETCNetConfigTest {

@Test
public void verifyETCNetConfigConstruction() {
ETCNetConfig config = new ETCNetConfig();

assertBlockchainConfigExistsAt(config, 0, FrontierConfig.class);
assertBlockchainConfigExistsAt(config, 1_150_000, HomesteadConfig.class);
assertBlockchainConfigExistsAt(config, 2_500_000, Eip150HFConfig.class);
assertBlockchainConfigExistsAt(config, 3_000_000, Eip160HFConfig.class);
}

private <T extends BlockchainConfig> void assertBlockchainConfigExistsAt(BlockchainNetConfig netConfig, long blockNumber, Class<T> configType) {
BlockchainConfig block = netConfig.getConfigForBlock(blockNumber);
assertTrue(configType.isAssignableFrom(block.getClass()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright (c) [2017] [ <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.config.net;

import org.ethereum.config.BlockchainConfig;
import org.ethereum.config.BlockchainNetConfig;
import org.ethereum.config.blockchain.ByzantiumConfig;
import org.ethereum.config.blockchain.Eip150HFConfig;
import org.ethereum.config.blockchain.Eip160HFConfig;
import org.ethereum.config.blockchain.FrontierConfig;
import org.ethereum.core.genesis.GenesisConfig;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class JsonNetConfigTest {
@Test
public void testCreationBasedOnGenesis() {
GenesisConfig genesisConfig = new GenesisConfig();
genesisConfig.eip155Block = 10;
JsonNetConfig config = new JsonNetConfig(genesisConfig);
assertBlockchainConfigExistsAt(config, 0, FrontierConfig.class);
assertBlockchainConfigExistsAt(config, 10, Eip150HFConfig.class);
}

@Test
public void testEip158WithoutEip155CreatesEip160HFConfig() {
GenesisConfig genesisConfig = new GenesisConfig();
genesisConfig.eip158Block = 10;

JsonNetConfig config = new JsonNetConfig(genesisConfig);
assertBlockchainConfigExistsAt(config, 10, Eip160HFConfig.class);
}

@Test
public void testEip155WithoutEip158CreatesEip160HFConfig() {
GenesisConfig genesisConfig = new GenesisConfig();
genesisConfig.eip155Block = 10;

JsonNetConfig config = new JsonNetConfig(genesisConfig);
assertBlockchainConfigExistsAt(config, 10, Eip160HFConfig.class);
}

@Test
public void testChainIdIsCorrectlySetOnEip160HFConfig() {
GenesisConfig genesisConfig = new GenesisConfig();
genesisConfig.eip155Block = 10;

JsonNetConfig config = new JsonNetConfig(genesisConfig);
BlockchainConfig eip160 = config.getConfigForBlock(10);
assertEquals("Default chainId must be '1'", new Integer(1), eip160.getChainId());

genesisConfig.chainId = 99;

config = new JsonNetConfig(genesisConfig);
eip160 = config.getConfigForBlock(10);
assertEquals("chainId should be copied from genesis config", new Integer(99), eip160.getChainId());
}

@Test
public void testEip155MustMatchEip158IfBothExist() {
GenesisConfig genesisConfig = new GenesisConfig();
genesisConfig.eip155Block = 10;
genesisConfig.eip158Block = 10;
JsonNetConfig config = new JsonNetConfig(genesisConfig);
assertBlockchainConfigExistsAt(config, 10, Eip160HFConfig.class);

try {
genesisConfig.eip158Block = 13;
new JsonNetConfig(genesisConfig);
fail("Must fail. EIP155 and EIP158 must have same blocks");
} catch (RuntimeException e) {
assertEquals("Unable to build config with different blocks for EIP155 (10) and EIP158 (13)", e.getMessage());
}
}

@Test
public void testByzantiumBlock() {
GenesisConfig genesisConfig = new GenesisConfig();
genesisConfig.byzantiumBlock = 50;

JsonNetConfig config = new JsonNetConfig(genesisConfig);
assertBlockchainConfigExistsAt(config, 50, ByzantiumConfig.class);

BlockchainConfig eip160 = config.getConfigForBlock(50);
assertEquals("Default chainId must be '1'", new Integer(1), eip160.getChainId());

genesisConfig.chainId = 99;

config = new JsonNetConfig(genesisConfig);
eip160 = config.getConfigForBlock(50);
assertEquals("chainId should be copied from genesis config", new Integer(99), eip160.getChainId());
}

private <T extends BlockchainConfig> void assertBlockchainConfigExistsAt(BlockchainNetConfig netConfig, long blockNumber, Class<T> configType) {
BlockchainConfig block = netConfig.getConfigForBlock(blockNumber);
if (!configType.isAssignableFrom(block.getClass())) {
fail(block.getClass().getName() + " is not of type " + configType);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) [2017] [ <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.config.net;

import org.ethereum.config.BlockchainConfig;
import org.ethereum.config.BlockchainNetConfig;
import org.ethereum.config.blockchain.FrontierConfig;
import org.ethereum.config.blockchain.HomesteadConfig;
import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class TestNetConfigTest {
@Test
public void verifyETCNetConfigConstruction() {
TestNetConfig config = new TestNetConfig();

assertBlockchainConfigExistsAt(config, 0, FrontierConfig.class);
assertBlockchainConfigExistsAt(config, 1_150_000, HomesteadConfig.class);
}

private <T extends BlockchainConfig> void assertBlockchainConfigExistsAt(BlockchainNetConfig netConfig, long blockNumber, Class<T> configType) {
BlockchainConfig block = netConfig.getConfigForBlock(blockNumber);
assertTrue(configType.isAssignableFrom(block.getClass()));
}

}

0 comments on commit f7f7d30

Please sign in to comment.