use of org.ethereum.config.BlockchainNetConfig in project rskj by rsksmart.
the class BridgeTest method getGasForDataFreeTx.
@Test
public void getGasForDataFreeTx() {
BlockchainNetConfig blockchainNetConfigOriginal = config.getBlockchainConfig();
config.setBlockchainConfig(new UnitTestBlockchainNetConfig());
Bridge bridge = new Bridge(config, PrecompiledContracts.BRIDGE_ADDR);
org.ethereum.core.Transaction rskTx = CallTransaction.createCallTransaction(config, 0, 1, 1, PrecompiledContracts.BRIDGE_ADDR, 0, Bridge.UPDATE_COLLECTIONS);
rskTx.sign(((BridgeRegTestConstants) bridgeConstants).getFederatorPrivateKeys().get(0).getPrivKeyBytes());
Block rskExecutionBlock = new BlockGenerator().createChildBlock(Genesis.getInstance(config));
bridge.init(rskTx, rskExecutionBlock, null, null, null, null);
Assert.assertEquals(0, bridge.getGasForData(rskTx.getData()));
config.setBlockchainConfig(blockchainNetConfigOriginal);
}
use of org.ethereum.config.BlockchainNetConfig in project rskj by rsksmart.
the class BlockchainLoaderTest method testLoadBlockchainEmptyBlockchain.
@Test
public void testLoadBlockchainEmptyBlockchain() throws IOException {
String jsonFile = "blockchain_loader_genesis.json";
RskSystemProperties systemProperties = Mockito.mock(RskSystemProperties.class);
Constants constants = Mockito.mock(Constants.class);
Mockito.when(constants.getInitialNonce()).thenReturn(BigInteger.ZERO);
BlockchainNetConfig blockchainNetConfig = Mockito.mock(BlockchainNetConfig.class);
Mockito.when(blockchainNetConfig.getCommonConstants()).thenReturn(constants);
Mockito.when(systemProperties.databaseDir()).thenReturn(new RskSystemProperties().databaseDir());
Mockito.when(systemProperties.getBlockchainConfig()).thenReturn(blockchainNetConfig);
Mockito.when(systemProperties.genesisInfo()).thenReturn(jsonFile);
BlockStore blockStore = Mockito.mock(BlockStore.class);
Mockito.when(blockStore.getBestBlock()).thenReturn(null);
EthereumListener ethereumListener = Mockito.mock(EthereumListener.class);
Repository repository = new RepositoryImpl(systemProperties, new TrieStoreImpl(new HashMapDB().setClearOnClose(false)));
;
BlockChainLoader blockChainLoader = new BlockChainLoader(systemProperties, repository, blockStore, null, null, ethereumListener, null, null);
blockChainLoader.loadBlockchain();
Assert.assertEquals(5, repository.getAccountsKeys().size());
Assert.assertEquals(Coin.valueOf(2000), repository.getBalance(new RskAddress("dabadabadabadabadabadabadabadabadaba0001")));
Assert.assertEquals(BigInteger.valueOf(24), repository.getNonce(new RskAddress("dabadabadabadabadabadabadabadabadaba0001")));
Assert.assertEquals(Coin.valueOf(1000), repository.getBalance(new RskAddress("dabadabadabadabadabadabadabadabadaba0002")));
Assert.assertEquals(BigInteger.ZERO, repository.getNonce(new RskAddress("dabadabadabadabadabadabadabadabadaba0002")));
Assert.assertEquals(Coin.valueOf(10), repository.getBalance(new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051")));
Assert.assertEquals(BigInteger.valueOf(25), repository.getNonce(new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051")));
Assert.assertEquals(DataWord.ONE, repository.getContractDetails(new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051")).get(DataWord.ZERO));
Assert.assertEquals(new DataWord(3), repository.getContractDetails(new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051")).get(DataWord.ONE));
Assert.assertEquals(274, repository.getContractDetails(new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051")).getCode().length);
}
use of org.ethereum.config.BlockchainNetConfig in project rskj by rsksmart.
the class MainNetMinerTest method generateFallbackMinedBlock.
@Test
public void generateFallbackMinedBlock() throws InterruptedException, IOException {
// generate private keys for testing now.
ECKey privateMiningKey0 = ECKey.fromPrivate(BigInteger.TEN);
ECKey privateMiningKey1 = ECKey.fromPrivate(BigInteger.TEN.add(BigInteger.ONE));
byte[] privKey0 = privateMiningKey0.getPrivKeyBytes();
saveToFile(privKey0, new File(folder.getRoot().getCanonicalPath(), "privkey0.bin"));
byte[] privKey1 = privateMiningKey1.getPrivKeyBytes();
saveToFile(privKey1, new File(folder.getRoot().getCanonicalPath(), "privkey1.bin"));
RskSystemProperties tempConfig = new RskSystemProperties() {
BlockchainNetConfig blockchainNetConfig = config.getBlockchainConfig();
@Override
public String fallbackMiningKeysDir() {
try {
return folder.getRoot().getCanonicalPath();
} catch (Exception e) {
}
return null;
}
@Override
public BlockchainNetConfig getBlockchainConfig() {
return new BlockchainNetConfig() {
@Override
public BlockchainConfig getConfigForBlock(long blockNumber) {
return blockchainNetConfig.getConfigForBlock(blockNumber);
}
@Override
public Constants getCommonConstants() {
return new Constants() {
@Override
public byte[] getFallbackMiningPubKey0() {
return privateMiningKey0.getPubKey();
}
@Override
public byte[] getFallbackMiningPubKey1() {
return privateMiningKey1.getPubKey();
}
};
}
};
}
};
EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
Mockito.when(ethereumImpl.addNewMinedBlock(Mockito.any())).thenReturn(ImportResult.IMPORTED_BEST);
MinerServer minerServer = new MinerServerImpl(tempConfig, ethereumImpl, blockchain, null, DIFFICULTY_CALCULATOR, new ProofOfWorkRule(tempConfig).setFallbackMiningEnabled(true), blockToMineBuilder(), ConfigUtils.getDefaultMiningConfig());
try {
minerServer.setFallbackMining(true);
// Accelerate mining
((MinerServerImpl) minerServer).setSecsBetweenFallbackMinedBlocks(1);
minerServer.start();
// Blocks are generated auomatically
// but we can call minerServer.generateFallbackBlock() to generate it manually
// boolean result = minerServer.generateFallbackBlock();
// Assert.assertTrue(result);
long start = System.currentTimeMillis();
while (((MinerServerImpl) minerServer).getFallbackBlocksGenerated() == 0) {
if (System.currentTimeMillis() - start > 20 * 1000) {
Assert.assertTrue(false);
}
//
Thread.sleep(1000);
}
Mockito.verify(ethereumImpl, Mockito.times(1)).addNewMinedBlock(Mockito.any());
// mine another
// NOTE that is NOT using the next block (parity change) because of the blockchain mockito
// to mine a subsequent block, use a real blockchain, not the mockito.
minerServer.buildBlockToMine(blockchain.getBestBlock(), false);
// result = minerServer.generateFallbackBlock();
// Assert.assertTrue(result);
start = System.currentTimeMillis();
while (((MinerServerImpl) minerServer).getFallbackBlocksGenerated() == 1) {
if (System.currentTimeMillis() - start > 20 * 1000) {
Assert.assertTrue(false);
}
//
Thread.sleep(1000);
}
Mockito.verify(ethereumImpl, Mockito.times(2)).addNewMinedBlock(Mockito.any());
} finally {
minerServer.stop();
}
}
use of org.ethereum.config.BlockchainNetConfig in project rskj by rsksmart.
the class BridgeUtils method isFreeBridgeTx.
public static boolean isFreeBridgeTx(SystemProperties config, Transaction rskTx, long blockNumber) {
BlockchainNetConfig blockchainConfig = config.getBlockchainConfig();
RskAddress receiveAddress = rskTx.getReceiveAddress();
if (receiveAddress.equals(RskAddress.nullAddress())) {
return false;
}
BridgeConstants bridgeConstants = blockchainConfig.getCommonConstants().getBridgeConstants();
// Once the original federation changes, txs are always paid.
return PrecompiledContracts.BRIDGE_ADDR.equals(receiveAddress) && blockchainConfig.getConfigForBlock(blockNumber).areBridgeTxsFree() && rskTx.acceptTransactionSignature(config.getBlockchainConfig().getCommonConstants().getChainId()) && (isFromFederateMember(rskTx, bridgeConstants.getGenesisFederation()) || isFromFederationChangeAuthorizedSender(rskTx, bridgeConstants) || isFromLockWhitelistChangeAuthorizedSender(rskTx, bridgeConstants) || isFromFeePerKbChangeAuthorizedSender(rskTx, bridgeConstants));
}
use of org.ethereum.config.BlockchainNetConfig in project rskj by rsksmart.
the class BridgeTest method getGasForDataPaidTx.
private void getGasForDataPaidTx(int expected, CallTransaction.Function function, Object... funcArgs) {
BlockchainNetConfig blockchainNetConfigOriginal = config.getBlockchainConfig();
config.setBlockchainConfig(new UnitTestBlockchainNetConfig());
Bridge bridge = new Bridge(config, PrecompiledContracts.BRIDGE_ADDR);
org.ethereum.core.Transaction rskTx;
if (function == null) {
rskTx = CallTransaction.createRawTransaction(config, 0, 1, 1, PrecompiledContracts.BRIDGE_ADDR, 0, new byte[] { 1, 2, 3 });
} else {
rskTx = CallTransaction.createCallTransaction(config, 0, 1, 1, PrecompiledContracts.BRIDGE_ADDR, 0, function, funcArgs);
}
rskTx.sign(((BridgeRegTestConstants) bridgeConstants).getFederatorPrivateKeys().get(0).getPrivKeyBytes());
BlockGenerator blockGenerator = new BlockGenerator();
Block rskExecutionBlock = blockGenerator.createChildBlock(Genesis.getInstance(config));
for (int i = 0; i < 20; i++) {
rskExecutionBlock = blockGenerator.createChildBlock(rskExecutionBlock);
}
bridge.init(rskTx, rskExecutionBlock, null, null, null, null);
Assert.assertEquals(expected, bridge.getGasForData(rskTx.getData()));
config.setBlockchainConfig(blockchainNetConfigOriginal);
}
Aggregations