Search in sources :

Example 1 with RepositoryImplForTesting

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

the class RemascStorageProviderTest method setSaveRetrieveAndGetManySiblings.

@Test
public void setSaveRetrieveAndGetManySiblings() throws IOException {
    RskAddress accountAddress = randomAddress();
    Repository repository = new RepositoryImplForTesting();
    RemascStorageProvider provider = new RemascStorageProvider(repository, accountAddress);
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    Block block1 = blockGenerator.createChildBlock(genesis);
    Block block2 = blockGenerator.createChildBlock(block1);
    Block block3 = blockGenerator.createChildBlock(block2);
    Block block4 = blockGenerator.createChildBlock(block3);
    Block block5 = blockGenerator.createChildBlock(block4);
    Sibling sibling1 = new Sibling(genesis.getHeader(), genesis.getCoinbase(), 1);
    Sibling sibling2 = new Sibling(block1.getHeader(), block1.getCoinbase(), 2);
    Sibling sibling3 = new Sibling(block2.getHeader(), block2.getCoinbase(), 3);
    Sibling sibling4 = new Sibling(block3.getHeader(), block3.getCoinbase(), 4);
    Sibling sibling5 = new Sibling(block4.getHeader(), block4.getCoinbase(), 5);
    Sibling sibling6 = new Sibling(block5.getHeader(), block5.getCoinbase(), 6);
    List<Sibling> siblings0 = new ArrayList<>();
    List<Sibling> siblings1 = new ArrayList<>();
    List<Sibling> siblings2 = new ArrayList<>();
    siblings0.add(sibling1);
    siblings0.add(sibling2);
    siblings1.add(sibling3);
    siblings1.add(sibling4);
    siblings2.add(sibling5);
    siblings2.add(sibling6);
    provider.getSiblings().put(Long.valueOf(0), siblings0);
    provider.getSiblings().put(Long.valueOf(1), siblings1);
    provider.getSiblings().put(Long.valueOf(2), siblings2);
    provider.save();
    RemascStorageProvider newProvider = new RemascStorageProvider(repository, accountAddress);
    SortedMap<Long, List<Sibling>> map = newProvider.getSiblings();
    Assert.assertNotNull(map);
    Assert.assertFalse(map.isEmpty());
    Assert.assertTrue(map.containsKey(Long.valueOf(0)));
    Assert.assertTrue(map.containsKey(Long.valueOf(1)));
    Assert.assertTrue(map.containsKey(Long.valueOf(2)));
    Assert.assertEquals(2, map.get(Long.valueOf(0)).size());
    Assert.assertEquals(2, map.get(Long.valueOf(1)).size());
    Assert.assertEquals(2, map.get(Long.valueOf(2)).size());
    List<Sibling> list0 = map.get(Long.valueOf(0));
    List<Sibling> list1 = map.get(Long.valueOf(1));
    List<Sibling> list2 = map.get(Long.valueOf(2));
    Assert.assertEquals(1, list0.get(0).getIncludedHeight());
    Assert.assertArrayEquals(genesis.getHeader().getHash().getBytes(), list0.get(0).getHash());
    Assert.assertEquals(2, list0.get(1).getIncludedHeight());
    Assert.assertArrayEquals(block1.getHeader().getHash().getBytes(), list0.get(1).getHash());
    Assert.assertEquals(3, list1.get(0).getIncludedHeight());
    Assert.assertArrayEquals(block2.getHeader().getHash().getBytes(), list1.get(0).getHash());
    Assert.assertEquals(4, list1.get(1).getIncludedHeight());
    Assert.assertArrayEquals(block3.getHeader().getHash().getBytes(), list1.get(1).getHash());
    Assert.assertEquals(5, list2.get(0).getIncludedHeight());
    Assert.assertArrayEquals(block4.getHeader().getHash().getBytes(), list2.get(0).getHash());
    Assert.assertEquals(6, list2.get(1).getIncludedHeight());
    Assert.assertArrayEquals(block5.getHeader().getHash().getBytes(), list2.get(1).getHash());
}
Also used : Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) RepositoryImplForTesting(co.rsk.db.RepositoryImplForTesting) ArrayList(java.util.ArrayList) Block(org.ethereum.core.Block) ArrayList(java.util.ArrayList) List(java.util.List) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 2 with RepositoryImplForTesting

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

the class RemascStorageProviderTest method setSaveRetrieveAndGetBurnedBalance.

@Test
public void setSaveRetrieveAndGetBurnedBalance() throws IOException {
    RskAddress accountAddress = randomAddress();
    Repository repository = new RepositoryImplForTesting();
    RemascStorageProvider provider = new RemascStorageProvider(repository, accountAddress);
    provider.setBurnedBalance(Coin.valueOf(255));
    provider.save();
    RemascStorageProvider newProvider = new RemascStorageProvider(repository, accountAddress);
    Assert.assertEquals(Coin.valueOf(255), newProvider.getBurnedBalance());
}
Also used : Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) RepositoryImplForTesting(co.rsk.db.RepositoryImplForTesting) Test(org.junit.Test)

Example 3 with RepositoryImplForTesting

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

the class RemascStorageProviderTest method setSaveRetrieveAndGetBrokenSelectionRule.

@Test
public void setSaveRetrieveAndGetBrokenSelectionRule() throws IOException {
    RskAddress accountAddress = randomAddress();
    Repository repository = new RepositoryImplForTesting();
    RemascStorageProvider provider = new RemascStorageProvider(repository, accountAddress);
    provider.setBrokenSelectionRule(Boolean.TRUE);
    provider.save();
    RemascStorageProvider newProvider = new RemascStorageProvider(repository, accountAddress);
    Assert.assertEquals(Boolean.TRUE, newProvider.getBrokenSelectionRule());
}
Also used : Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) RepositoryImplForTesting(co.rsk.db.RepositoryImplForTesting) Test(org.junit.Test)

Example 4 with RepositoryImplForTesting

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

the class RepositoryBlockStoreTest method test.

@Test
public void test() throws Exception {
    // This Is how I produced RepositoryBlockStore_data.ser. I had a bitcoind in regtest with 613 blocks + genesis block
    // NetworkParameters params = RegTestParams.get();
    // Context context = new Context(params);
    // Wallet wallet = new Wallet(context);
    // BlockStore store = new SPVBlockStore(params, new File("spvBlockstore"));
    // AbstractBlockChain chain = new BlockChain(context, wallet, store);
    // PeerGroup peerGroup = new PeerGroup(context, chain);
    // peerGroup.start();
    // final DownloadProgressTracker listener = new DownloadProgressTracker();
    // peerGroup.startBlockChainDownload(listener);
    // listener.await();
    // peerGroup.stop();
    // StoredBlock storedBlock = chain.getChainHead();
    // FileOutputStream fos = new FileOutputStream("RepositoryBlockStore_data.ser");
    // ObjectOutputStream oos = new ObjectOutputStream(fos);
    // for (int i = 0; i < 614; i++) {
    // Triple<byte[], BigInteger , Integer> tripleStoredBlock = new ImmutableTriple<>(storedBlock.getHeader().bitcoinSerialize(), storedBlock.getChainWork(), storedBlock.getHeight());
    // oos.writeObject(tripleStoredBlock);
    // storedBlock = store.get(storedBlock.getHeader().getPrevBlockHash());
    // }
    // oos.close();
    // Read original store
    InputStream fileInputStream = ClassLoader.getSystemResourceAsStream("peg/RepositoryBlockStore_data.ser");
    ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
    Repository repository = new RepositoryImplForTesting();
    RskSystemProperties config = new RskSystemProperties();
    RepositoryBlockStore store = new RepositoryBlockStore(config, repository, PrecompiledContracts.BRIDGE_ADDR);
    for (int i = 0; i < 614; i++) {
        Triple<byte[], BigInteger, Integer> tripleStoredBlock = (Triple<byte[], BigInteger, Integer>) objectInputStream.readObject();
        BtcBlock header = RegTestParams.get().getDefaultSerializer().makeBlock(tripleStoredBlock.getLeft());
        StoredBlock storedBlock = new StoredBlock(header, tripleStoredBlock.getMiddle(), tripleStoredBlock.getRight());
        if (i == 0) {
            store.setChainHead(storedBlock);
        }
        store.put(storedBlock);
    }
    // Create a new instance of the store
    RepositoryBlockStore store2 = new RepositoryBlockStore(config, repository, PrecompiledContracts.BRIDGE_ADDR);
    // Check a specific block that used to fail when we had a bug
    assertEquals(store.get(Sha256Hash.wrap("373941fe83961cf70e181e468abc5f9f7cc440c711c3d06948fa66f3912ed27a")), store2.get(Sha256Hash.wrap("373941fe83961cf70e181e468abc5f9f7cc440c711c3d06948fa66f3912ed27a")));
    // Check new instance content is identical to the original one
    StoredBlock storedBlock = store.getChainHead();
    StoredBlock storedBlock2 = store2.getChainHead();
    int headHeight = storedBlock.getHeight();
    for (int i = 0; i < headHeight; i++) {
        assertNotNull(storedBlock);
        assertEquals(storedBlock, storedBlock2);
        Sha256Hash prevBlockHash = storedBlock.getHeader().getPrevBlockHash();
        storedBlock = store.get(prevBlockHash);
        storedBlock2 = store2.get(prevBlockHash);
    }
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) RepositoryImplForTesting(co.rsk.db.RepositoryImplForTesting) BigInteger(java.math.BigInteger) Triple(org.apache.commons.lang3.tuple.Triple) Repository(org.ethereum.core.Repository) BigInteger(java.math.BigInteger) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) RskSystemProperties(co.rsk.config.RskSystemProperties) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 5 with RepositoryImplForTesting

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

the class RemascStorageProviderTest method setSaveRetrieveAndGetRewardBalance.

@Test
public void setSaveRetrieveAndGetRewardBalance() throws IOException {
    RskAddress accountAddress = randomAddress();
    Repository repository = new RepositoryImplForTesting();
    RemascStorageProvider provider = new RemascStorageProvider(repository, accountAddress);
    provider.setRewardBalance(Coin.valueOf(255));
    provider.save();
    RemascStorageProvider newProvider = new RemascStorageProvider(repository, accountAddress);
    Assert.assertEquals(Coin.valueOf(255), newProvider.getRewardBalance());
}
Also used : Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) RepositoryImplForTesting(co.rsk.db.RepositoryImplForTesting) Test(org.junit.Test)

Aggregations

RepositoryImplForTesting (co.rsk.db.RepositoryImplForTesting)6 Repository (org.ethereum.core.Repository)6 Test (org.junit.Test)6 RskAddress (co.rsk.core.RskAddress)5 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Block (org.ethereum.core.Block)2 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)1 Sha256Hash (co.rsk.bitcoinj.core.Sha256Hash)1 StoredBlock (co.rsk.bitcoinj.core.StoredBlock)1 RskSystemProperties (co.rsk.config.RskSystemProperties)1 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 BigInteger (java.math.BigInteger)1 Triple (org.apache.commons.lang3.tuple.Triple)1