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()));
}
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());
}
}
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();
}
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_);
}
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));
}
Aggregations