Search in sources :

Example 6 with ByteArrayWrapper

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

the class Cache method get.

public Node get(byte[] key) {
    ByteArrayWrapper wrappedKey = wrap(key);
    Node node = nodes.get(wrappedKey);
    if (node != null) {
        // cachehits++;
        return node;
    } else if (this.dataSource != null) {
        Optional<byte[]> data = this.dataSource.get(key);
        if (data.isPresent()) {
            // dbhits++;
            node = new Node(fromRlpEncoded(data.get()), false);
            nodes.put(wrappedKey, node);
            return node;
        }
    }
    return null;
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) Optional(java.util.Optional)

Example 7 with ByteArrayWrapper

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

the class AionRepositoryCacheTest method testGetStorageValueIsDoubleZero.

@Test
public void testGetStorageValueIsDoubleZero() {
    AionAddress address = getNewAddress();
    ByteArrayWrapper key = ByteArrayWrapper.wrap(RandomUtils.nextBytes(16));
    cache.removeStorageRow(address, key);
    assertNull(cache.getStorageValue(address, key));
    key = ByteArrayWrapper.wrap(RandomUtils.nextBytes(DOUBLE_BYTES));
    cache.removeStorageRow(address, key);
    assertNull(cache.getStorageValue(address, key));
}
Also used : AionAddress(org.aion.types.AionAddress) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) Test(org.junit.Test)

Example 8 with ByteArrayWrapper

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

the class AionRepositoryCacheTest method testOverwriteValueWithDoubleZero.

@Test
public void testOverwriteValueWithDoubleZero() {
    AionAddress address = getNewAddress();
    ByteArrayWrapper key = ByteArrayWrapper.wrap(RandomUtils.nextBytes(DOUBLE_BYTES));
    ByteArrayWrapper value = new DataWord(RandomUtils.nextBytes(DataWord.BYTES)).toWrapper();
    cache.addStorageRow(address, key, value);
    assertEquals(value, cache.getStorageValue(address, key));
    cache.removeStorageRow(address, key);
    assertNull(cache.getStorageValue(address, key));
}
Also used : AionAddress(org.aion.types.AionAddress) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) DataWord(org.aion.util.types.DataWord) Test(org.junit.Test)

Example 9 with ByteArrayWrapper

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

the class FvmContractDetailsTest method testDecodeFromEncodingWithSmallStorage.

@Test
public void testDecodeFromEncodingWithSmallStorage() {
    ByteArrayKeyValueDatabase db = new MockDB("db", log);
    db.open();
    assertThat(db.isEmpty()).isTrue();
    byte[] code = ByteUtil.hexStringToBytes("60016002");
    ByteArrayWrapper key_1 = ByteArrayWrapper.fromHex("111111");
    ByteArrayWrapper val_1 = ByteArrayWrapper.fromHex("aaaaaa");
    ByteArrayWrapper key_2 = ByteArrayWrapper.fromHex("222222");
    ByteArrayWrapper val_2 = ByteArrayWrapper.fromHex("bbbbbb");
    FvmContractDetails details = new FvmContractDetails(AddressUtils.ZERO_ADDRESS, db);
    details.setCode(code);
    details.put(key_1, val_1);
    details.put(key_2, val_2);
    // flush changes to the database
    details.syncStorage();
    FvmContractDetails decoded = FvmContractDetails.decodeAtRoot(fromEncoding(details.getEncoded()), db, details.getStorageHash());
    // check code
    byte[] codeHash = h256(code);
    assertThat(decoded.getCode(codeHash)).isEqualTo(code);
    // check address
    assertThat(decoded.address).isEqualTo(AddressUtils.ZERO_ADDRESS);
    // check storage
    assertThat(decoded.get(key_1)).isEqualTo(val_1);
    assertThat(decoded.get(key_2)).isEqualTo(val_2);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ByteArrayKeyValueDatabase(org.aion.db.impl.ByteArrayKeyValueDatabase) MockDB(org.aion.db.impl.mockdb.MockDB) Test(org.junit.Test)

Example 10 with ByteArrayWrapper

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

the class FvmContractDetailsTest method testDecode_withInLineStorageAndTransition.

@Test
public void testDecode_withInLineStorageAndTransition() {
    SecureTrie trie = new SecureTrie(null);
    Map<ByteArrayWrapper, ByteArrayWrapper> storage = new HashMap<>();
    for (int i = 0; i < 3; i++) {
        byte[] key = RandomUtils.nextBytes(32);
        byte[] value = RandomUtils.nextBytes(100);
        trie.update(key, RLP.encodeElement(value));
        storage.put(ByteArrayWrapper.wrap(key), ByteArrayWrapper.wrap(value));
    }
    RLPElement storageTrie = RLP.decode2SharedList(trie.serialize());
    AionAddress address = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
    byte[] codeBytes = RandomUtils.nextBytes(100);
    RLPElement code = mock(SharedRLPItem.class);
    when(code.getRLPData()).thenReturn(codeBytes);
    byte[] rootHash = RandomUtils.nextBytes(32);
    RLPElement root = mock(SharedRLPItem.class);
    when(root.getRLPData()).thenReturn(rootHash);
    Logger log = mock(Logger.class);
    ByteArrayKeyValueDatabase db = new MockDB("db", log);
    db.open();
    assertThat(db.isEmpty()).isTrue();
    RLPContractDetails input = new RLPContractDetails(address, false, root, storageTrie, code);
    FvmContractDetails details = FvmContractDetails.decodeAtRoot(input, db, rootHash);
    assertThat(details.address).isEqualTo(address);
    // because it uses the setCodes method
    assertThat(details.isDirty()).isTrue();
    assertThat(details.isDeleted()).isFalse();
    assertThat(details.getCodes().size()).isEqualTo(1);
    assertThat(details.getCodes().values()).contains(ByteArrayWrapper.wrap(codeBytes));
    assertThat(details.getCode(h256(codeBytes))).isEqualTo(codeBytes);
    byte[] storageHash = trie.getRootHash();
    assertThat(details.getStorageHash()).isEqualTo(storageHash);
    for (ByteArrayWrapper key : storage.keySet()) {
        assertThat(details.get(key)).isEqualTo(storage.get(key));
    }
    assertThat(db.isEmpty()).isFalse();
}
Also used : AionAddress(org.aion.types.AionAddress) HashMap(java.util.HashMap) RLPContractDetails(org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails) MockDB(org.aion.db.impl.mockdb.MockDB) Logger(org.slf4j.Logger) SecureTrie(org.aion.zero.impl.trie.SecureTrie) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) RLPElement(org.aion.rlp.RLPElement) ByteArrayKeyValueDatabase(org.aion.db.impl.ByteArrayKeyValueDatabase) Test(org.junit.Test)

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