Search in sources :

Example 16 with TrieImpl

use of org.aion.mcf.trie.TrieImpl in project aion by aionnetwork.

the class TrieTestWithRootHashValues method testDeleteMultipleItems2.

@Test
public void testDeleteMultipleItems2() {
    String ROOT_HASH_BEFORE = "cf1ed2b6c4b6558f70ef0ecf76bfbee96af785cb5d5e7bfc37f9804ad8d0fb56";
    String ROOT_HASH_AFTER1 = "f586af4a476ba853fca8cea1fbde27cd17d537d18f64269fe09b02aa7fe55a9e";
    String ROOT_HASH_AFTER2 = "c59fdc16a80b11cc2f7a8b107bb0c954c0d8059e49c760ec3660eea64053ac91";
    TrieImpl trie = new TrieImpl(mockDb);
    trie.update(c, LONG_STRING);
    assertEquals(LONG_STRING, new String(trie.get(c)));
    trie.update(ca, LONG_STRING);
    assertEquals(LONG_STRING, new String(trie.get(ca)));
    trie.update(cat, LONG_STRING);
    assertEquals(LONG_STRING, new String(trie.get(cat)));
    assertEquals(ROOT_HASH_BEFORE, Hex.toHexString(trie.getRootHash()));
    trie.delete(ca);
    assertEquals("", new String(trie.get(ca)));
    assertEquals(ROOT_HASH_AFTER1, Hex.toHexString(trie.getRootHash()));
    trie.delete(cat);
    assertEquals("", new String(trie.get(cat)));
    assertEquals(ROOT_HASH_AFTER2, Hex.toHexString(trie.getRootHash()));
}
Also used : TrieImpl(org.aion.mcf.trie.TrieImpl) Test(org.junit.Test)

Example 17 with TrieImpl

use of org.aion.mcf.trie.TrieImpl in project aion by aionnetwork.

the class AionBlock method parseTxs.

private void parseTxs(RLPList txTransactions) {
    this.txsState = new TrieImpl(null);
    for (int i = 0; i < txTransactions.size(); i++) {
        RLPElement transactionRaw = txTransactions.get(i);
        this.transactionsList.add(new AionTransaction(transactionRaw.getRLPData()));
        this.txsState.update(RLP.encodeInt(i), transactionRaw.getRLPData());
    }
}
Also used : TrieImpl(org.aion.mcf.trie.TrieImpl) RLPElement(org.aion.rlp.RLPElement) AionTransaction(org.aion.zero.types.AionTransaction)

Example 18 with TrieImpl

use of org.aion.mcf.trie.TrieImpl in project aion by aionnetwork.

the class BlockchainDataRecoveryTest method testRecoverWorldStateWithStartFromGenesis.

/**
 * Test the recovery of the world state in the case where it is missing from the database.
 */
@Test
public void testRecoverWorldStateWithStartFromGenesis() {
    final int NUMBER_OF_BLOCKS = 10;
    // build a blockchain with a few blocks
    StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
    StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").build();
    StandaloneBlockchain chain = bundle.bc;
    // all blocks will be incorrect
    ImportResult result;
    List<byte[]> statesToDelete = new ArrayList<>();
    for (int i = 0; i < NUMBER_OF_BLOCKS; i++) {
        AionBlock next = chain.createNewBlock(chain.getBestBlock(), Collections.emptyList(), true);
        result = chain.tryToConnect(next);
        assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
        statesToDelete.add(next.getStateRoot());
    }
    AionBlock bestBlock = chain.getBestBlock();
    assertThat(bestBlock.getNumber()).isEqualTo(NUMBER_OF_BLOCKS);
    chain.getRepository().flush();
    // System.out.println(Hex.toHexString(chain.getRepository().getRoot()));
    // delete some world state root entries from the database
    TrieImpl trie = (TrieImpl) ((AionRepositoryImpl) chain.getRepository()).getWorldState();
    IByteArrayKeyValueDatabase database = (IByteArrayKeyValueDatabase) trie.getCache().getDb();
    for (byte[] key : statesToDelete) {
        database.delete(key);
        assertThat(trie.isValidRoot(key)).isFalse();
    }
    // System.out.println(Hex.toHexString(chain.getRepository().getRoot()));
    // ensure that the world state was corrupted
    assertThat(trie.isValidRoot(chain.getBestBlock().getStateRoot())).isFalse();
    // call the recovery functionality
    boolean worked = chain.recoverWorldState(chain.getRepository(), bestBlock.getNumber());
    // ensure that the blockchain is ok
    assertThat(chain.getBestBlockHash()).isEqualTo(bestBlock.getHash());
    // ensure that the world state is ok
    assertThat(worked).isTrue();
    assertThat(trie.isValidRoot(chain.getBestBlock().getStateRoot())).isTrue();
}
Also used : ImportResult(org.aion.mcf.core.ImportResult) ArrayList(java.util.ArrayList) TrieImpl(org.aion.mcf.trie.TrieImpl) IByteArrayKeyValueDatabase(org.aion.base.db.IByteArrayKeyValueDatabase) AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

Example 19 with TrieImpl

use of org.aion.mcf.trie.TrieImpl in project aion by aionnetwork.

the class TrieTest method testDeleteCompletellyDiferentItems.

@Test
public void testDeleteCompletellyDiferentItems() {
    TrieImpl trie = new TrieImpl(null);
    String val_1 = "1000000000000000000000000000000000000000000000000000000000000000";
    String val_2 = "2000000000000000000000000000000000000000000000000000000000000000";
    String val_3 = "3000000000000000000000000000000000000000000000000000000000000000";
    trie.update(Hex.decode(val_1), Hex.decode(val_1));
    trie.update(Hex.decode(val_2), Hex.decode(val_2));
    String root1 = Hex.toHexString(trie.getRootHash());
    trie.update(Hex.decode(val_3), Hex.decode(val_3));
    trie.delete(Hex.decode(val_3));
    String root1_ = Hex.toHexString(trie.getRootHash());
    Assert.assertEquals(root1, root1_);
}
Also used : TrieImpl(org.aion.mcf.trie.TrieImpl) Test(org.junit.Test)

Example 20 with TrieImpl

use of org.aion.mcf.trie.TrieImpl in project aion by aionnetwork.

the class TrieTest method testTrieCopy.

@Test
public void testTrieCopy() {
    TrieImpl trie = new TrieImpl(null);
    trie.update("doe", "reindeer");
    TrieImpl trie2 = trie.copy();
    // avoid possibility that its just a reference copy
    assertNotEquals(trie.hashCode(), trie2.hashCode());
    assertEquals(Hex.toHexString(trie.getRootHash()), Hex.toHexString(trie2.getRootHash()));
    assertTrue(trie.equals(trie2));
}
Also used : TrieImpl(org.aion.mcf.trie.TrieImpl) Test(org.junit.Test)

Aggregations

TrieImpl (org.aion.mcf.trie.TrieImpl)32 Test (org.junit.Test)31 Parameters (junitparams.Parameters)5 MockDB (org.aion.db.impl.mockdb.MockDB)5 ArrayList (java.util.ArrayList)3 IByteArrayKeyValueDatabase (org.aion.base.db.IByteArrayKeyValueDatabase)3 ImportResult (org.aion.mcf.core.ImportResult)3 AionBlock (org.aion.zero.impl.types.AionBlock)3 RLPElement (org.aion.rlp.RLPElement)1 AionTransaction (org.aion.zero.types.AionTransaction)1