Search in sources :

Example 41 with RepositoryImpl

use of co.rsk.db.RepositoryImpl in project rskj by rsksmart.

the class BridgeSupportTest method minimumProcessFundsMigrationValue.

@Test
public void minimumProcessFundsMigrationValue() throws IOException, BlockStoreException {
    BridgeConstants bridgeConstants = BridgeRegTestConstants.getInstance();
    Federation oldFederation = bridgeConstants.getGenesisFederation();
    Federation newFederation = new Federation(Collections.singletonList(new BtcECKey(new SecureRandom())), Instant.EPOCH, 5L, bridgeConstants.getBtcParams());
    BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
    when(provider.getFeePerKb()).thenReturn(Coin.MILLICOIN);
    when(provider.getReleaseRequestQueue()).thenReturn(new ReleaseRequestQueue(Collections.emptyList()));
    when(provider.getReleaseTransactionSet()).thenReturn(new ReleaseTransactionSet(Collections.emptySet()));
    when(provider.getOldFederation()).thenReturn(oldFederation);
    when(provider.getNewFederation()).thenReturn(newFederation);
    BlockGenerator blockGenerator = new BlockGenerator();
    // Old federation will be in migration age at block 35
    org.ethereum.core.Block rskCurrentBlock = blockGenerator.createBlock(35, 1);
    Transaction tx = Transaction.create(config, TO_ADDRESS, DUST_AMOUNT, NONCE, GAS_PRICE, GAS_LIMIT, DATA);
    Repository repository = new RepositoryImpl(config);
    Repository track = repository.startTracking();
    BridgeSupport bridgeSupport = new BridgeSupport(config, track, mock(BridgeEventLogger.class), provider, rskCurrentBlock);
    // One MICROCOIN is less than half the fee per kb, which is the minimum funds to migrate,
    // and so it won't be removed from the old federation UTXOs list for migration.
    List<UTXO> unsufficientUTXOsForMigration1 = new ArrayList<>();
    unsufficientUTXOsForMigration1.add(createUTXO(Coin.MICROCOIN, oldFederation.getAddress()));
    when(provider.getOldFederationBtcUTXOs()).thenReturn(unsufficientUTXOsForMigration1);
    bridgeSupport.updateCollections(tx);
    assertThat(unsufficientUTXOsForMigration1.size(), is(1));
    // MILLICOIN is greater than half the fee per kb,
    // and it will be removed from the old federation UTXOs list for migration.
    List<UTXO> sufficientUTXOsForMigration1 = new ArrayList<>();
    sufficientUTXOsForMigration1.add(createUTXO(Coin.MILLICOIN, oldFederation.getAddress()));
    when(provider.getOldFederationBtcUTXOs()).thenReturn(sufficientUTXOsForMigration1);
    bridgeSupport.updateCollections(tx);
    assertThat(sufficientUTXOsForMigration1.size(), is(0));
    // 2 smaller coins should work exactly like 1 MILLICOIN
    List<UTXO> sufficientUTXOsForMigration2 = new ArrayList<>();
    sufficientUTXOsForMigration2.add(createUTXO(Coin.MILLICOIN.divide(2), oldFederation.getAddress()));
    sufficientUTXOsForMigration2.add(createUTXO(Coin.MILLICOIN.divide(2), oldFederation.getAddress()));
    when(provider.getOldFederationBtcUTXOs()).thenReturn(sufficientUTXOsForMigration2);
    bridgeSupport.updateCollections(tx);
    assertThat(sufficientUTXOsForMigration2.size(), is(0));
    // higher fee per kb prevents funds migration
    List<UTXO> unsufficientUTXOsForMigration2 = new ArrayList<>();
    unsufficientUTXOsForMigration2.add(createUTXO(Coin.MILLICOIN, oldFederation.getAddress()));
    when(provider.getOldFederationBtcUTXOs()).thenReturn(unsufficientUTXOsForMigration2);
    when(provider.getFeePerKb()).thenReturn(Coin.COIN);
    bridgeSupport.updateCollections(tx);
    assertThat(unsufficientUTXOsForMigration2.size(), is(1));
}
Also used : SecureRandom(java.security.SecureRandom) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) org.ethereum.core(org.ethereum.core) SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) RepositoryImpl(co.rsk.db.RepositoryImpl) BridgeConstants(co.rsk.config.BridgeConstants) BridgeEventLogger(co.rsk.peg.utils.BridgeEventLogger) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 42 with RepositoryImpl

use of co.rsk.db.RepositoryImpl in project rskj by rsksmart.

the class BridgeTest method callUpdateCollectionsWithTransactionsWaitingForConfirmationWithEnoughConfirmations.

@Test
public void callUpdateCollectionsWithTransactionsWaitingForConfirmationWithEnoughConfirmations() throws IOException {
    BtcTransaction tx1 = createTransaction();
    BtcTransaction tx2 = createTransaction();
    BtcTransaction tx3 = createTransaction();
    Repository repository = new RepositoryImpl(config);
    Repository track = repository.startTracking();
    BridgeStorageProvider provider0 = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    provider0.getReleaseTransactionSet().add(tx1, 1L);
    provider0.getReleaseTransactionSet().add(tx2, 2L);
    provider0.getReleaseTransactionSet().add(tx3, 3L);
    provider0.save();
    track.commit();
    track = repository.startTracking();
    World world = new World();
    List<Block> blocks = new BlockGenerator().getSimpleBlockChain(world.getBlockChain().getBestBlock(), 10);
    Transaction rskTx = Transaction.create(config, PrecompiledContracts.BRIDGE_ADDR_STR, AMOUNT, NONCE, GAS_PRICE, GAS_LIMIT, DATA);
    rskTx.sign(new ECKey().getPrivKeyBytes());
    world.getBlockChain().getBlockStore().saveBlock(blocks.get(1), new BlockDifficulty(BigInteger.ONE), true);
    Bridge bridge = new Bridge(config, PrecompiledContracts.BRIDGE_ADDR);
    bridge.init(rskTx, blocks.get(9), track, world.getBlockChain().getBlockStore(), null, new LinkedList<>());
    bridge.execute(Bridge.UPDATE_COLLECTIONS.encode());
    track.commit();
    BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    Assert.assertEquals(2, provider.getReleaseTransactionSet().getEntries().size());
    Assert.assertEquals(1, provider.getRskTxsWaitingForSignatures().size());
}
Also used : SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) ECKey(org.ethereum.crypto.ECKey) World(co.rsk.test.World) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockDifficulty(co.rsk.core.BlockDifficulty) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) RepositoryImpl(co.rsk.db.RepositoryImpl) Test(org.junit.Test)

Example 43 with RepositoryImpl

use of co.rsk.db.RepositoryImpl in project rskj by rsksmart.

the class SamplePrecompiledContractTest method samplePrecompiledContractGetBalanceInitialBalance.

@Test
public void samplePrecompiledContractGetBalanceInitialBalance() {
    int balance = this.GetBalance(new RepositoryImpl(config));
    assertEquals(0, balance);
}
Also used : RepositoryImpl(co.rsk.db.RepositoryImpl) Test(org.junit.Test)

Example 44 with RepositoryImpl

use of co.rsk.db.RepositoryImpl in project rskj by rsksmart.

the class SamplePrecompiledContractTest method samplePrecompiledContractGetResultInitialValue.

@Test
public void samplePrecompiledContractGetResultInitialValue() {
    int result = this.GetResult(new RepositoryImpl(config));
    assertEquals(0, result);
}
Also used : RepositoryImpl(co.rsk.db.RepositoryImpl) Test(org.junit.Test)

Example 45 with RepositoryImpl

use of co.rsk.db.RepositoryImpl in project rskj by rsksmart.

the class SamplePrecompiledContractTest method samplePrecompiledContractMethod1WrongData.

@Test
public void samplePrecompiledContractMethod1WrongData() {
    DataWord addr = new DataWord(PrecompiledContracts.SAMPLE_ADDR.getBytes());
    SamplePrecompiledContract contract = (SamplePrecompiledContract) precompiledContracts.getContractForAddress(addr);
    String funcJson = "{\n" + "   'constant':false, \n" + "   'inputs':[{'name':'param0','type':'int'}, \n" + "               {'name':'param1','type':'bytes'}, \n" + "               {'name':'param2','type':'int'}], \n" + "    'name':'Method1', \n" + "   'outputs':[{'name':'output0','type':'int'}], \n" + "    'type':'function' \n" + "}\n";
    funcJson = funcJson.replaceAll("'", "\"");
    CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson);
    byte[] data = new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef };
    contract.init(null, null, new RepositoryImpl(config), null, null, new ArrayList<LogInfo>());
    byte[] result = contract.execute(data);
    assertNull(result);
}
Also used : LogInfo(org.ethereum.vm.LogInfo) RepositoryImpl(co.rsk.db.RepositoryImpl) CallTransaction(org.ethereum.core.CallTransaction) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test)

Aggregations

RepositoryImpl (co.rsk.db.RepositoryImpl)62 Test (org.junit.Test)54 Repository (org.ethereum.core.Repository)25 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)22 RskAddress (co.rsk.core.RskAddress)19 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)16 HashMapDB (org.ethereum.datasource.HashMapDB)15 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)13 org.ethereum.core (org.ethereum.core)13 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)12 LogInfo (org.ethereum.vm.LogInfo)12 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)11 BridgeConstants (co.rsk.config.BridgeConstants)10 BridgeEventLogger (co.rsk.peg.utils.BridgeEventLogger)10 DataWord (org.ethereum.vm.DataWord)9 ArrayList (java.util.ArrayList)8 BtcBlockStore (co.rsk.bitcoinj.store.BtcBlockStore)7 ECKey (org.ethereum.crypto.ECKey)7 Keccak256 (co.rsk.crypto.Keccak256)6 SimpleBlockChain (co.rsk.peg.simples.SimpleBlockChain)6