Search in sources :

Example 1 with BlockchainNetConfig

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);
}
Also used : co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) org.ethereum.core(org.ethereum.core) BlockchainNetConfig(org.ethereum.config.BlockchainNetConfig) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 2 with BlockchainNetConfig

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);
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) EthereumListener(org.ethereum.listener.EthereumListener) BlockStore(org.ethereum.db.BlockStore) Constants(org.ethereum.config.Constants) DataWord(org.ethereum.vm.DataWord) HashMapDB(org.ethereum.datasource.HashMapDB) Repository(org.ethereum.core.Repository) RepositoryImpl(co.rsk.db.RepositoryImpl) RskAddress(co.rsk.core.RskAddress) BlockchainNetConfig(org.ethereum.config.BlockchainNetConfig) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test) BlockChainImplTest(co.rsk.core.bc.BlockChainImplTest)

Example 3 with BlockchainNetConfig

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();
    }
}
Also used : Constants(org.ethereum.config.Constants) ECKey(org.ethereum.crypto.ECKey) BlockchainNetConfig(org.ethereum.config.BlockchainNetConfig) EthereumImpl(org.ethereum.facade.EthereumImpl) File(java.io.File) RskSystemProperties(co.rsk.config.RskSystemProperties) IOException(java.io.IOException) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test) BlockChainImplTest(co.rsk.core.bc.BlockChainImplTest)

Example 4 with BlockchainNetConfig

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));
}
Also used : RskAddress(co.rsk.core.RskAddress) BlockchainNetConfig(org.ethereum.config.BlockchainNetConfig) BridgeConstants(co.rsk.config.BridgeConstants)

Example 5 with BlockchainNetConfig

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);
}
Also used : co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) org.ethereum.core(org.ethereum.core) BlockchainNetConfig(org.ethereum.config.BlockchainNetConfig) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator)

Aggregations

BlockchainNetConfig (org.ethereum.config.BlockchainNetConfig)5 Test (org.junit.Test)3 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)2 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)2 RskSystemProperties (co.rsk.config.RskSystemProperties)2 RskAddress (co.rsk.core.RskAddress)2 BlockChainImplTest (co.rsk.core.bc.BlockChainImplTest)2 Constants (org.ethereum.config.Constants)2 org.ethereum.core (org.ethereum.core)2 BridgeConstants (co.rsk.config.BridgeConstants)1 RepositoryImpl (co.rsk.db.RepositoryImpl)1 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)1 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)1 File (java.io.File)1 IOException (java.io.IOException)1 Repository (org.ethereum.core.Repository)1 ECKey (org.ethereum.crypto.ECKey)1 HashMapDB (org.ethereum.datasource.HashMapDB)1 BlockStore (org.ethereum.db.BlockStore)1 EthereumImpl (org.ethereum.facade.EthereumImpl)1