Search in sources :

Example 81 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class TrieTest method getSampleTrieUpdates.

/**
 * Trie updates taken from real blockchain use with sample accounts.
 */
private Map<ByteArrayWrapper, byte[]> getSampleTrieUpdates() {
    Map<ByteArrayWrapper, byte[]> data = new HashMap<>();
    ByteArrayWrapper key;
    byte[] value;
    key = ByteArrayWrapper.wrap(new byte[] { -96, 29, -93, -108, -55, -59, 67, -121, -3, 10, 44, -128, -127, 73, 90, -26, 3, 62, 55, -60, -82, -11, -96, -54, 106, 114, 42, 70, 46, 31, 37, 29 });
    value = new byte[] { -8, 78, -128, -118, -45, -62, 27, -50, -52, -19, -95, 0, 0, 0, -96, 69, -80, -49, -62, 32, -50, -20, 91, 124, 28, 98, -60, -44, 25, 61, 56, -28, -21, -92, -114, -120, 21, 114, -100, -25, 95, -100, 10, -80, -28, -63, -64, -96, 14, 87, 81, -64, 38, -27, 67, -78, -24, -85, 46, -80, 96, -103, -38, -95, -47, -27, -33, 71, 119, -113, 119, -121, -6, -85, 69, -51, -15, 47, -29, -88 };
    data.put(key, value);
    key = ByteArrayWrapper.wrap(new byte[] { -96, 44, -96, -3, -97, 10, 112, 111, 28, -32, 44, 18, 101, -106, 51, 6, -107, 0, 24, 13, 50, 81, -84, 68, 125, 110, 118, 97, -109, -96, -30, 107 });
    value = new byte[] { -8, 78, -128, -118, -45, -62, 27, -50, -52, -19, -95, 0, 0, 0, -96, 69, -80, -49, -62, 32, -50, -20, 91, 124, 28, 98, -60, -44, 25, 61, 56, -28, -21, -92, -114, -120, 21, 114, -100, -25, 95, -100, 10, -80, -28, -63, -64, -96, 14, 87, 81, -64, 38, -27, 67, -78, -24, -85, 46, -80, 96, -103, -38, -95, -47, -27, -33, 71, 119, -113, 119, -121, -6, -85, 69, -51, -15, 47, -29, -88 };
    data.put(key, value);
    key = ByteArrayWrapper.wrap(new byte[] { -96, 53, 87, -10, 21, -118, 120, -87, 69, -83, 111, 41, -26, -81, 41, -82, -90, -108, -59, -19, -60, 87, -98, -88, 50, -127, 89, -11, -56, 114, -113, 27 });
    value = new byte[] { -8, 78, -128, -118, -45, -62, 27, -50, -52, -19, -95, 0, 0, 0, -96, 69, -80, -49, -62, 32, -50, -20, 91, 124, 28, 98, -60, -44, 25, 61, 56, -28, -21, -92, -114, -120, 21, 114, -100, -25, 95, -100, 10, -80, -28, -63, -64, -96, 14, 87, 81, -64, 38, -27, 67, -78, -24, -85, 46, -80, 96, -103, -38, -95, -47, -27, -33, 71, 119, -113, 119, -121, -6, -85, 69, -51, -15, 47, -29, -88 };
    data.put(key, value);
    key = ByteArrayWrapper.wrap(new byte[] { -96, -118, 43, 113, -6, -115, 90, 79, -76, -32, -70, -7, -91, -98, 70, 111, 32, 97, 96, 55, -119, -89, 64, -34, -92, 94, 1, 58, -61, 63, 102, -92 });
    value = new byte[] { -8, 78, -128, -118, -45, -62, 27, -50, -52, -19, -95, 0, 0, 0, -96, 69, -80, -49, -62, 32, -50, -20, 91, 124, 28, 98, -60, -44, 25, 61, 56, -28, -21, -92, -114, -120, 21, 114, -100, -25, 95, -100, 10, -80, -28, -63, -64, -96, 14, 87, 81, -64, 38, -27, 67, -78, -24, -85, 46, -80, 96, -103, -38, -95, -47, -27, -33, 71, 119, -113, 119, -121, -6, -85, 69, -51, -15, 47, -29, -88 };
    data.put(key, value);
    return data;
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) HashMap(java.util.HashMap)

Example 82 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class TrieTest method testGetMissingNodes_wIncompleteTrie.

@Test
public void testGetMissingNodes_wIncompleteTrie() {
    MockDB mockDB = new MockDB("temp", log);
    mockDB.open();
    TrieImpl trie = new TrieImpl(mockDB);
    for (Map.Entry<ByteArrayWrapper, byte[]> e : getSampleTrieUpdates().entrySet()) {
        trie.update(e.getKey().toBytes(), e.getValue());
    }
    trie.getCache().commitForTest();
    byte[] root = trie.getRootHash();
    // System.out.println(trie.getTrieDump(root));
    // removing two of the nodes from the trie
    Set<ByteArrayWrapper> expected = new HashSet<>();
    expected.add(ByteArrayWrapper.wrap(Hex.decode("59c2a26cebd0ed50053bba185a7d13e1ae58314e2c37d46c1f7b885fd93b687a")));
    expected.add(ByteArrayWrapper.wrap(Hex.decode("ddf1b495a3e98e1897a9b1257d4172d59fcbe0dba23b8b87812ca2a55919d9ab")));
    for (ByteArrayWrapper key : expected) {
        mockDB.deleteAndCommit(key.toBytes());
    }
    trie = new TrieImpl(mockDB);
    Set<ByteArrayWrapper> missing = trie.getMissingNodes(root);
    assertThat(missing).hasSize(2);
    assertThat(missing).isEqualTo(expected);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) MockDB(org.aion.db.impl.mockdb.MockDB) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 83 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class TrieTest method testGetMissingNodes_wIncompleteTrie_wStartFromValue.

@Test
public void testGetMissingNodes_wIncompleteTrie_wStartFromValue() {
    MockDB mockDB = new MockDB("temp", log);
    mockDB.open();
    TrieImpl trie = new TrieImpl(mockDB);
    for (Map.Entry<ByteArrayWrapper, byte[]> e : getSampleTrieUpdates().entrySet()) {
        trie.update(e.getKey().toBytes(), e.getValue());
    }
    trie.getCache().commitForTest();
    byte[] root = trie.getRootHash();
    // System.out.println(trie.getTrieDump(root));
    byte[] value = mockDB.get(root).get();
    // removing two of the nodes from the trie
    Set<ByteArrayWrapper> expected = new HashSet<>();
    expected.add(ByteArrayWrapper.wrap(Hex.decode("59c2a26cebd0ed50053bba185a7d13e1ae58314e2c37d46c1f7b885fd93b687a")));
    expected.add(ByteArrayWrapper.wrap(Hex.decode("ddf1b495a3e98e1897a9b1257d4172d59fcbe0dba23b8b87812ca2a55919d9ab")));
    for (ByteArrayWrapper key : expected) {
        mockDB.deleteAndCommit(key.toBytes());
    }
    trie = new TrieImpl(mockDB);
    Set<ByteArrayWrapper> missing = trie.getMissingNodes(value);
    assertThat(missing).hasSize(2);
    assertThat(missing).isEqualTo(expected);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) MockDB(org.aion.db.impl.mockdb.MockDB) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 84 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class JournalPruneDataSource method prune.

public void prune(ByteArrayWrapper blockHash, long blockNumber) {
    if (!enabled.get()) {
        return;
    }
    lock.writeLock().lock();
    try {
        Updates updates = blockUpdates.remove(blockHash);
        if (updates != null) {
            for (ByteArrayWrapper insertedKey : updates.insertedKeys) {
                decRef(insertedKey).dbRef = true;
            }
            List<byte[]> batchRemove = new ArrayList<>();
            for (ByteArrayWrapper key : updates.deletedKeys) {
                Ref ref = refCount.get(key);
                if (ref == null || ref.journalRefs == 0) {
                    batchRemove.add(key.toBytes());
                } else if (ref != null) {
                    ref.dbRef = false;
                }
            }
            src.deleteBatch(batchRemove);
            rollbackForkBlocks(blockNumber);
        }
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ArrayList(java.util.ArrayList)

Example 85 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class JournalPruneDataSource method rollback.

private void rollback(ByteArrayWrapper blockHashW) {
    Updates updates = blockUpdates.remove(blockHashW);
    List<byte[]> batchRemove = new ArrayList<>();
    for (ByteArrayWrapper insertedKey : updates.insertedKeys) {
        Ref ref = decRef(insertedKey);
        if (ref.getTotRefs() == 0) {
            batchRemove.add(insertedKey.toBytes());
        }
    }
    src.deleteBatch(batchRemove);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ArrayList(java.util.ArrayList)

Aggregations

ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)130 Test (org.junit.Test)51 HashMap (java.util.HashMap)39 ArrayList (java.util.ArrayList)33 AionAddress (org.aion.types.AionAddress)26 Block (org.aion.zero.impl.types.Block)24 Map (java.util.Map)20 BigInteger (java.math.BigInteger)14 MiningBlock (org.aion.zero.impl.types.MiningBlock)14 IOException (java.io.IOException)13 MockDB (org.aion.db.impl.mockdb.MockDB)13 DataWord (org.aion.util.types.DataWord)13 PooledTransaction (org.aion.base.PooledTransaction)11 List (java.util.List)10 AionTransaction (org.aion.base.AionTransaction)10 Properties (java.util.Properties)8 HashSet (java.util.HashSet)5 Optional (java.util.Optional)5 ECKey (org.aion.crypto.ECKey)5 RLPElement (org.aion.rlp.RLPElement)5